]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/ext2fs/ext2_vnops.c
zfs: merge openzfs/zfs@e13538856
[FreeBSD/FreeBSD.git] / sys / fs / ext2fs / ext2_vnops.c
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  * SPDX-License-Identifier: BSD-3-Clause
9  *
10  * Copyright (c) 1982, 1986, 1989, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  * (c) UNIX System Laboratories, Inc.
13  * All or some portions of this file are derived from material licensed
14  * to the University of California by American Telephone and Telegraph
15  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16  * the permission of UNIX System Laboratories, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *      @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
43  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
44  */
45
46 #include "opt_suiddir.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/filio.h>
53 #include <sys/limits.h>
54 #include <sys/sdt.h>
55 #include <sys/stat.h>
56 #include <sys/bio.h>
57 #include <sys/buf.h>
58 #include <sys/endian.h>
59 #include <sys/priv.h>
60 #include <sys/rwlock.h>
61 #include <sys/mount.h>
62 #include <sys/unistd.h>
63 #include <sys/time.h>
64 #include <sys/vnode.h>
65 #include <sys/namei.h>
66 #include <sys/lockf.h>
67 #include <sys/event.h>
68 #include <sys/conf.h>
69 #include <sys/file.h>
70 #include <sys/extattr.h>
71 #include <sys/vmmeter.h>
72
73 #include <vm/vm.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vnode_pager.h>
80
81 #include "opt_directio.h"
82
83 #include <ufs/ufs/dir.h>
84
85 #include <fs/ext2fs/fs.h>
86 #include <fs/ext2fs/inode.h>
87 #include <fs/ext2fs/ext2_acl.h>
88 #include <fs/ext2fs/ext2fs.h>
89 #include <fs/ext2fs/ext2_extern.h>
90 #include <fs/ext2fs/ext2_dinode.h>
91 #include <fs/ext2fs/ext2_dir.h>
92 #include <fs/ext2fs/ext2_mount.h>
93 #include <fs/ext2fs/ext2_extattr.h>
94 #include <fs/ext2fs/ext2_extents.h>
95
96 SDT_PROVIDER_DECLARE(ext2fs);
97 /*
98  * ext2fs trace probe:
99  * arg0: verbosity. Higher numbers give more verbose messages
100  * arg1: Textual message
101  */
102 SDT_PROBE_DEFINE2(ext2fs, , vnops, trace, "int", "char*");
103
104 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
105 static void ext2_itimes_locked(struct vnode *);
106
107 static vop_access_t     ext2_access;
108 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
109 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
110     struct thread *);
111 static vop_close_t      ext2_close;
112 static vop_create_t     ext2_create;
113 static vop_fsync_t      ext2_fsync;
114 static vop_getattr_t    ext2_getattr;
115 static vop_ioctl_t      ext2_ioctl;
116 static vop_link_t       ext2_link;
117 static vop_mkdir_t      ext2_mkdir;
118 static vop_mknod_t      ext2_mknod;
119 static vop_open_t       ext2_open;
120 static vop_pathconf_t   ext2_pathconf;
121 static vop_print_t      ext2_print;
122 static vop_read_t       ext2_read;
123 static vop_readlink_t   ext2_readlink;
124 static vop_remove_t     ext2_remove;
125 static vop_rename_t     ext2_rename;
126 static vop_rmdir_t      ext2_rmdir;
127 static vop_setattr_t    ext2_setattr;
128 static vop_strategy_t   ext2_strategy;
129 static vop_symlink_t    ext2_symlink;
130 static vop_write_t      ext2_write;
131 static vop_deleteextattr_t      ext2_deleteextattr;
132 static vop_getextattr_t ext2_getextattr;
133 static vop_listextattr_t        ext2_listextattr;
134 static vop_setextattr_t ext2_setextattr;
135 static vop_vptofh_t     ext2_vptofh;
136 static vop_close_t      ext2fifo_close;
137
138 /* Global vfs data structures for ext2. */
139 struct vop_vector ext2_vnodeops = {
140         .vop_default =          &default_vnodeops,
141         .vop_access =           ext2_access,
142         .vop_bmap =             ext2_bmap,
143         .vop_cachedlookup =     ext2_lookup,
144         .vop_close =            ext2_close,
145         .vop_create =           ext2_create,
146         .vop_fsync =            ext2_fsync,
147         .vop_getpages =         vnode_pager_local_getpages,
148         .vop_getpages_async =   vnode_pager_local_getpages_async,
149         .vop_getattr =          ext2_getattr,
150         .vop_inactive =         ext2_inactive,
151         .vop_ioctl =            ext2_ioctl,
152         .vop_link =             ext2_link,
153         .vop_lookup =           vfs_cache_lookup,
154         .vop_mkdir =            ext2_mkdir,
155         .vop_mknod =            ext2_mknod,
156         .vop_open =             ext2_open,
157         .vop_pathconf =         ext2_pathconf,
158         .vop_poll =             vop_stdpoll,
159         .vop_print =            ext2_print,
160         .vop_read =             ext2_read,
161         .vop_readdir =          ext2_readdir,
162         .vop_readlink =         ext2_readlink,
163         .vop_reallocblks =      ext2_reallocblks,
164         .vop_reclaim =          ext2_reclaim,
165         .vop_remove =           ext2_remove,
166         .vop_rename =           ext2_rename,
167         .vop_rmdir =            ext2_rmdir,
168         .vop_setattr =          ext2_setattr,
169         .vop_strategy =         ext2_strategy,
170         .vop_symlink =          ext2_symlink,
171         .vop_write =            ext2_write,
172         .vop_deleteextattr =    ext2_deleteextattr,
173         .vop_getextattr =       ext2_getextattr,
174         .vop_listextattr =      ext2_listextattr,
175         .vop_setextattr =       ext2_setextattr,
176 #ifdef UFS_ACL
177         .vop_getacl =           ext2_getacl,
178         .vop_setacl =           ext2_setacl,
179         .vop_aclcheck =         ext2_aclcheck,
180 #endif /* UFS_ACL */
181         .vop_vptofh =           ext2_vptofh,
182 };
183 VFS_VOP_VECTOR_REGISTER(ext2_vnodeops);
184
185 struct vop_vector ext2_fifoops = {
186         .vop_default =          &fifo_specops,
187         .vop_access =           ext2_access,
188         .vop_close =            ext2fifo_close,
189         .vop_fsync =            ext2_fsync,
190         .vop_getattr =          ext2_getattr,
191         .vop_inactive =         ext2_inactive,
192         .vop_pathconf =         ext2_pathconf,
193         .vop_print =            ext2_print,
194         .vop_read =             VOP_PANIC,
195         .vop_reclaim =          ext2_reclaim,
196         .vop_setattr =          ext2_setattr,
197         .vop_write =            VOP_PANIC,
198         .vop_vptofh =           ext2_vptofh,
199 };
200 VFS_VOP_VECTOR_REGISTER(ext2_fifoops);
201
202 /*
203  * A virgin directory (no blushing please).
204  * Note that the type and namlen fields are reversed relative to ext2.
205  * Also, we don't use `struct odirtemplate', since it would just cause
206  * endianness problems.
207  */
208 static struct dirtemplate mastertemplate = {
209         0, htole16(12), 1, EXT2_FT_DIR, ".",
210         0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_DIR, ".."
211 };
212 static struct dirtemplate omastertemplate = {
213         0, htole16(12), 1, EXT2_FT_UNKNOWN, ".",
214         0, htole16(DIRBLKSIZ - 12), 2, EXT2_FT_UNKNOWN, ".."
215 };
216
217 static void
218 ext2_itimes_locked(struct vnode *vp)
219 {
220         struct inode *ip;
221         struct timespec ts;
222
223         ASSERT_VI_LOCKED(vp, __func__);
224
225         ip = VTOI(vp);
226         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
227                 return;
228         if ((vp->v_type == VBLK || vp->v_type == VCHR))
229                 ip->i_flag |= IN_LAZYMOD;
230         else
231                 ip->i_flag |= IN_MODIFIED;
232         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
233                 vfs_timestamp(&ts);
234                 if (ip->i_flag & IN_ACCESS) {
235                         ip->i_atime = ts.tv_sec;
236                         ip->i_atimensec = ts.tv_nsec;
237                 }
238                 if (ip->i_flag & IN_UPDATE) {
239                         ip->i_mtime = ts.tv_sec;
240                         ip->i_mtimensec = ts.tv_nsec;
241                         ip->i_modrev++;
242                 }
243                 if (ip->i_flag & IN_CHANGE) {
244                         ip->i_ctime = ts.tv_sec;
245                         ip->i_ctimensec = ts.tv_nsec;
246                 }
247         }
248         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
249 }
250
251 void
252 ext2_itimes(struct vnode *vp)
253 {
254
255         VI_LOCK(vp);
256         ext2_itimes_locked(vp);
257         VI_UNLOCK(vp);
258 }
259
260 /*
261  * Create a regular file
262  */
263 static int
264 ext2_create(struct vop_create_args *ap)
265 {
266         int error;
267
268         error =
269             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
270             ap->a_dvp, ap->a_vpp, ap->a_cnp);
271         if (error != 0)
272                 return (error);
273         if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
274                 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
275         return (0);
276 }
277
278 static int
279 ext2_open(struct vop_open_args *ap)
280 {
281
282         if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
283                 return (EOPNOTSUPP);
284
285         /*
286          * Files marked append-only must be opened for appending.
287          */
288         if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
289             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
290                 return (EPERM);
291
292         vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
293
294         return (0);
295 }
296
297 /*
298  * Close called.
299  *
300  * Update the times on the inode.
301  */
302 static int
303 ext2_close(struct vop_close_args *ap)
304 {
305         struct vnode *vp = ap->a_vp;
306
307         VI_LOCK(vp);
308         if (vp->v_usecount > 1)
309                 ext2_itimes_locked(vp);
310         VI_UNLOCK(vp);
311         return (0);
312 }
313
314 static int
315 ext2_access(struct vop_access_args *ap)
316 {
317         struct vnode *vp = ap->a_vp;
318         struct inode *ip = VTOI(vp);
319         accmode_t accmode = ap->a_accmode;
320         int error;
321
322         /*
323          * Disallow write attempts on read-only file systems;
324          * unless the file is a socket, fifo, or a block or
325          * character device resident on the file system.
326          */
327         if (accmode & VWRITE) {
328                 switch (vp->v_type) {
329                 case VDIR:
330                 case VLNK:
331                 case VREG:
332                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
333                                 return (EROFS);
334                         break;
335                 default:
336                         break;
337                 }
338         }
339
340         /* If immutable bit set, nobody gets to write it. */
341         if ((accmode & VWRITE) && (ip->i_flags & SF_IMMUTABLE))
342                 return (EPERM);
343
344         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
345             ap->a_accmode, ap->a_cred);
346         return (error);
347 }
348
349 static int
350 ext2_getattr(struct vop_getattr_args *ap)
351 {
352         struct vnode *vp = ap->a_vp;
353         struct inode *ip = VTOI(vp);
354         struct vattr *vap = ap->a_vap;
355
356         ext2_itimes(vp);
357         /*
358          * Copy from inode table
359          */
360         vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
361         vap->va_fileid = ip->i_number;
362         vap->va_mode = ip->i_mode & ~IFMT;
363         vap->va_nlink = ip->i_nlink;
364         vap->va_uid = ip->i_uid;
365         vap->va_gid = ip->i_gid;
366         vap->va_rdev = ip->i_rdev;
367         vap->va_size = ip->i_size;
368         vap->va_atime.tv_sec = ip->i_atime;
369         vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
370         vap->va_mtime.tv_sec = ip->i_mtime;
371         vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
372         vap->va_ctime.tv_sec = ip->i_ctime;
373         vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
374         if (E2DI_HAS_XTIME(ip)) {
375                 vap->va_birthtime.tv_sec = ip->i_birthtime;
376                 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
377         }
378         vap->va_flags = ip->i_flags;
379         vap->va_gen = ip->i_gen;
380         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
381         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
382         vap->va_type = IFTOVT(ip->i_mode);
383         vap->va_filerev = ip->i_modrev;
384         return (0);
385 }
386
387 /*
388  * Set attribute vnode op. called from several syscalls
389  */
390 static int
391 ext2_setattr(struct vop_setattr_args *ap)
392 {
393         struct vattr *vap = ap->a_vap;
394         struct vnode *vp = ap->a_vp;
395         struct inode *ip = VTOI(vp);
396         struct ucred *cred = ap->a_cred;
397         struct thread *td = curthread;
398         int error;
399
400         /*
401          * Check for unsettable attributes.
402          */
403         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
404             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
405             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
406             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
407                 return (EINVAL);
408         }
409         if (vap->va_flags != VNOVAL) {
410                 /* Disallow flags not supported by ext2fs. */
411                 if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
412                         return (EOPNOTSUPP);
413
414                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
415                         return (EROFS);
416                 /*
417                  * Callers may only modify the file flags on objects they
418                  * have VADMIN rights for.
419                  */
420                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
421                         return (error);
422                 /*
423                  * Unprivileged processes and privileged processes in
424                  * jail() are not permitted to unset system flags, or
425                  * modify flags if any system flags are set.
426                  * Privileged non-jail processes may not modify system flags
427                  * if securelevel > 0 and any existing system flags are set.
428                  */
429                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS)) {
430                         if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
431                                 error = securelevel_gt(cred, 0);
432                                 if (error)
433                                         return (error);
434                         }
435                 } else {
436                         if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
437                             ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
438                                 return (EPERM);
439                 }
440                 ip->i_flags = vap->va_flags;
441                 ip->i_flag |= IN_CHANGE;
442                 if (ip->i_flags & (IMMUTABLE | APPEND))
443                         return (0);
444         }
445         if (ip->i_flags & (IMMUTABLE | APPEND))
446                 return (EPERM);
447         /*
448          * Go through the fields and update iff not VNOVAL.
449          */
450         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
451                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
452                         return (EROFS);
453                 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
454                     td)) != 0)
455                         return (error);
456         }
457         if (vap->va_size != VNOVAL) {
458                 /*
459                  * Disallow write attempts on read-only file systems;
460                  * unless the file is a socket, fifo, or a block or
461                  * character device resident on the file system.
462                  */
463                 switch (vp->v_type) {
464                 case VDIR:
465                         return (EISDIR);
466                 case VLNK:
467                 case VREG:
468                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
469                                 return (EROFS);
470                         break;
471                 default:
472                         break;
473                 }
474                 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
475                         return (error);
476         }
477         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
478                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
479                         return (EROFS);
480                 /*
481                  * From utimes(2):
482                  * If times is NULL, ... The caller must be the owner of
483                  * the file, have permission to write the file, or be the
484                  * super-user.
485                  * If times is non-NULL, ... The caller must be the owner of
486                  * the file or be the super-user.
487                  */
488                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
489                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
490                     (error = VOP_ACCESS(vp, VWRITE, cred, td))))
491                         return (error);
492                 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
493                 if (vap->va_atime.tv_sec != VNOVAL) {
494                         ip->i_flag &= ~IN_ACCESS;
495                         ip->i_atime = vap->va_atime.tv_sec;
496                         ip->i_atimensec = vap->va_atime.tv_nsec;
497                 }
498                 if (vap->va_mtime.tv_sec != VNOVAL) {
499                         ip->i_flag &= ~IN_UPDATE;
500                         ip->i_mtime = vap->va_mtime.tv_sec;
501                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
502                 }
503                 if (E2DI_HAS_XTIME(ip) && vap->va_birthtime.tv_sec != VNOVAL) {
504                         ip->i_birthtime = vap->va_birthtime.tv_sec;
505                         ip->i_birthnsec = vap->va_birthtime.tv_nsec;
506                 }
507                 error = ext2_update(vp, 0);
508                 if (error)
509                         return (error);
510         }
511         error = 0;
512         if (vap->va_mode != (mode_t)VNOVAL) {
513                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
514                         return (EROFS);
515                 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
516         }
517         return (error);
518 }
519
520 /*
521  * Change the mode on a file.
522  * Inode must be locked before calling.
523  */
524 static int
525 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
526 {
527         struct inode *ip = VTOI(vp);
528         int error;
529
530         /*
531          * To modify the permissions on a file, must possess VADMIN
532          * for that file.
533          */
534         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
535                 return (error);
536         /*
537          * Privileged processes may set the sticky bit on non-directories,
538          * as well as set the setgid bit on a file with a group that the
539          * process is not a member of.
540          */
541         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
542                 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE);
543                 if (error)
544                         return (EFTYPE);
545         }
546         if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
547                 error = priv_check_cred(cred, PRIV_VFS_SETGID);
548                 if (error)
549                         return (error);
550         }
551         ip->i_mode &= ~ALLPERMS;
552         ip->i_mode |= (mode & ALLPERMS);
553         ip->i_flag |= IN_CHANGE;
554         return (0);
555 }
556
557 /*
558  * Perform chown operation on inode ip;
559  * inode must be locked prior to call.
560  */
561 static int
562 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
563     struct thread *td)
564 {
565         struct inode *ip = VTOI(vp);
566         uid_t ouid;
567         gid_t ogid;
568         int error = 0;
569
570         if (uid == (uid_t)VNOVAL)
571                 uid = ip->i_uid;
572         if (gid == (gid_t)VNOVAL)
573                 gid = ip->i_gid;
574         /*
575          * To modify the ownership of a file, must possess VADMIN
576          * for that file.
577          */
578         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
579                 return (error);
580         /*
581          * To change the owner of a file, or change the group of a file
582          * to a group of which we are not a member, the caller must
583          * have privilege.
584          */
585         if (uid != ip->i_uid || (gid != ip->i_gid &&
586             !groupmember(gid, cred))) {
587                 error = priv_check_cred(cred, PRIV_VFS_CHOWN);
588                 if (error)
589                         return (error);
590         }
591         ogid = ip->i_gid;
592         ouid = ip->i_uid;
593         ip->i_gid = gid;
594         ip->i_uid = uid;
595         ip->i_flag |= IN_CHANGE;
596         if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
597                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID) != 0)
598                         ip->i_mode &= ~(ISUID | ISGID);
599         }
600         return (0);
601 }
602
603 /*
604  * Synch an open file.
605  */
606 /* ARGSUSED */
607 static int
608 ext2_fsync(struct vop_fsync_args *ap)
609 {
610         /*
611          * Flush all dirty buffers associated with a vnode.
612          */
613
614         vop_stdfsync(ap);
615
616         return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
617 }
618
619 static int
620 ext2_check_mknod_limits(dev_t dev)
621 {
622         unsigned maj = major(dev);
623         unsigned min = minor(dev);
624
625         if (maj > EXT2_MAJOR_MAX || min > EXT2_MINOR_MAX)
626                 return (EINVAL);
627
628         return (0);
629 }
630
631 /*
632  * Mknod vnode call
633  */
634 /* ARGSUSED */
635 static int
636 ext2_mknod(struct vop_mknod_args *ap)
637 {
638         struct vattr *vap = ap->a_vap;
639         struct vnode **vpp = ap->a_vpp;
640         struct inode *ip;
641         ino_t ino;
642         int error;
643
644         if (vap->va_rdev != VNOVAL) {
645                 error = ext2_check_mknod_limits(vap->va_rdev);
646                 if (error)
647                         return (error);
648         }
649
650         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
651             ap->a_dvp, vpp, ap->a_cnp);
652         if (error)
653                 return (error);
654         ip = VTOI(*vpp);
655         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
656         if (vap->va_rdev != VNOVAL)
657                 ip->i_rdev = vap->va_rdev;
658
659         /*
660          * Remove inode, then reload it through VFS_VGET so it is
661          * checked to see if it is an alias of an existing entry in
662          * the inode cache.      XXX I don't believe this is necessary now.
663          */
664         (*vpp)->v_type = VNON;
665         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
666         vgone(*vpp);
667         vput(*vpp);
668         error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
669         if (error) {
670                 *vpp = NULL;
671                 return (error);
672         }
673         return (0);
674 }
675
676 static int
677 ext2_remove(struct vop_remove_args *ap)
678 {
679         struct inode *ip;
680         struct vnode *vp = ap->a_vp;
681         struct vnode *dvp = ap->a_dvp;
682         int error;
683
684         ip = VTOI(vp);
685         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
686             (VTOI(dvp)->i_flags & APPEND)) {
687                 error = EPERM;
688                 goto out;
689         }
690         error = ext2_dirremove(dvp, ap->a_cnp);
691         if (error == 0) {
692                 ip->i_nlink--;
693                 ip->i_flag |= IN_CHANGE;
694         }
695 out:
696         return (error);
697 }
698
699 /*
700  * link vnode call
701  */
702 static int
703 ext2_link(struct vop_link_args *ap)
704 {
705         struct vnode *vp = ap->a_vp;
706         struct vnode *tdvp = ap->a_tdvp;
707         struct componentname *cnp = ap->a_cnp;
708         struct inode *ip;
709         int error;
710
711         ip = VTOI(vp);
712         if ((nlink_t)ip->i_nlink >= EXT4_LINK_MAX) {
713                 error = EMLINK;
714                 goto out;
715         }
716         if (ip->i_flags & (IMMUTABLE | APPEND)) {
717                 error = EPERM;
718                 goto out;
719         }
720         ip->i_nlink++;
721         ip->i_flag |= IN_CHANGE;
722         error = ext2_update(vp, !DOINGASYNC(vp));
723         if (!error)
724                 error = ext2_direnter(ip, tdvp, cnp);
725         if (error) {
726                 ip->i_nlink--;
727                 ip->i_flag |= IN_CHANGE;
728         }
729 out:
730         return (error);
731 }
732
733 static int
734 ext2_inc_nlink(struct inode *ip)
735 {
736
737         ip->i_nlink++;
738
739         if (S_ISDIR(ip->i_mode) &&
740             EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK) &&
741             ip->i_nlink > 1) {
742                 if (ip->i_nlink >= EXT4_LINK_MAX || ip->i_nlink == 2)
743                         ip->i_nlink = 1;
744         } else if (ip->i_nlink > EXT4_LINK_MAX) {
745                 ip->i_nlink--;
746                 return (EMLINK);
747         }
748
749         return (0);
750 }
751
752 static void
753 ext2_dec_nlink(struct inode *ip)
754 {
755
756         if (!S_ISDIR(ip->i_mode) || ip->i_nlink > 2)
757                 ip->i_nlink--;
758 }
759
760 /*
761  * Rename system call.
762  *      rename("foo", "bar");
763  * is essentially
764  *      unlink("bar");
765  *      link("foo", "bar");
766  *      unlink("foo");
767  * but ``atomically''.  Can't do full commit without saving state in the
768  * inode on disk which isn't feasible at this time.  Best we can do is
769  * always guarantee the target exists.
770  *
771  * Basic algorithm is:
772  *
773  * 1) Bump link count on source while we're linking it to the
774  *    target.  This also ensure the inode won't be deleted out
775  *    from underneath us while we work (it may be truncated by
776  *    a concurrent `trunc' or `open' for creation).
777  * 2) Link source to destination.  If destination already exists,
778  *    delete it first.
779  * 3) Unlink source reference to inode if still around. If a
780  *    directory was moved and the parent of the destination
781  *    is different from the source, patch the ".." entry in the
782  *    directory.
783  */
784 static int
785 ext2_rename(struct vop_rename_args *ap)
786 {
787         struct vnode *tvp = ap->a_tvp;
788         struct vnode *tdvp = ap->a_tdvp;
789         struct vnode *fvp = ap->a_fvp;
790         struct vnode *fdvp = ap->a_fdvp;
791         struct componentname *tcnp = ap->a_tcnp;
792         struct componentname *fcnp = ap->a_fcnp;
793         struct inode *ip, *xp, *dp;
794         struct dirtemplate *dirbuf;
795         int doingdirectory = 0, oldparent = 0, newparent = 0;
796         int error = 0;
797         u_char namlen;
798
799         /*
800          * Check for cross-device rename.
801          */
802         if ((fvp->v_mount != tdvp->v_mount) ||
803             (tvp && (fvp->v_mount != tvp->v_mount))) {
804                 error = EXDEV;
805 abortit:
806                 if (tdvp == tvp)
807                         vrele(tdvp);
808                 else
809                         vput(tdvp);
810                 if (tvp)
811                         vput(tvp);
812                 vrele(fdvp);
813                 vrele(fvp);
814                 return (error);
815         }
816
817         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
818             (VTOI(tdvp)->i_flags & APPEND))) {
819                 error = EPERM;
820                 goto abortit;
821         }
822
823         /*
824          * Renaming a file to itself has no effect.  The upper layers should
825          * not call us in that case.  Temporarily just warn if they do.
826          */
827         if (fvp == tvp) {
828                 SDT_PROBE2(ext2fs, , vnops, trace, 1,
829                     "rename: fvp == tvp (can't happen)");
830                 error = 0;
831                 goto abortit;
832         }
833
834         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
835                 goto abortit;
836         dp = VTOI(fdvp);
837         ip = VTOI(fvp);
838         if (ip->i_nlink >= EXT4_LINK_MAX &&
839             !EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
840                 VOP_UNLOCK(fvp);
841                 error = EMLINK;
842                 goto abortit;
843         }
844         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
845             || (dp->i_flags & APPEND)) {
846                 VOP_UNLOCK(fvp);
847                 error = EPERM;
848                 goto abortit;
849         }
850         if ((ip->i_mode & IFMT) == IFDIR) {
851                 /*
852                  * Avoid ".", "..", and aliases of "." for obvious reasons.
853                  */
854                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
855                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
856                     (ip->i_flag & IN_RENAME)) {
857                         VOP_UNLOCK(fvp);
858                         error = EINVAL;
859                         goto abortit;
860                 }
861                 ip->i_flag |= IN_RENAME;
862                 oldparent = dp->i_number;
863                 doingdirectory++;
864         }
865         vrele(fdvp);
866
867         /*
868          * When the target exists, both the directory
869          * and target vnodes are returned locked.
870          */
871         dp = VTOI(tdvp);
872         xp = NULL;
873         if (tvp)
874                 xp = VTOI(tvp);
875
876         /*
877          * 1) Bump link count while we're moving stuff
878          *    around.  If we crash somewhere before
879          *    completing our work, the link count
880          *    may be wrong, but correctable.
881          */
882         ext2_inc_nlink(ip);
883         ip->i_flag |= IN_CHANGE;
884         if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
885                 VOP_UNLOCK(fvp);
886                 goto bad;
887         }
888
889         /*
890          * If ".." must be changed (ie the directory gets a new
891          * parent) then the source directory must not be in the
892          * directory hierarchy above the target, as this would
893          * orphan everything below the source directory. Also
894          * the user must have write permission in the source so
895          * as to be able to change "..". We must repeat the call
896          * to namei, as the parent directory is unlocked by the
897          * call to checkpath().
898          */
899         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, curthread);
900         VOP_UNLOCK(fvp);
901         if (oldparent != dp->i_number)
902                 newparent = dp->i_number;
903         if (doingdirectory && newparent) {
904                 if (error)      /* write access check above */
905                         goto bad;
906                 if (xp != NULL)
907                         vput(tvp);
908                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
909                 if (error)
910                         goto out;
911                 VREF(tdvp);
912                 error = vfs_relookup(tdvp, &tvp, tcnp, true);
913                 if (error)
914                         goto out;
915                 vrele(tdvp);
916                 dp = VTOI(tdvp);
917                 xp = NULL;
918                 if (tvp)
919                         xp = VTOI(tvp);
920         }
921         /*
922          * 2) If target doesn't exist, link the target
923          *    to the source and unlink the source.
924          *    Otherwise, rewrite the target directory
925          *    entry to reference the source inode and
926          *    expunge the original entry's existence.
927          */
928         if (xp == NULL) {
929                 if (dp->i_devvp != ip->i_devvp)
930                         panic("ext2_rename: EXDEV");
931                 /*
932                  * Account for ".." in new directory.
933                  * When source and destination have the same
934                  * parent we don't fool with the link count.
935                  */
936                 if (doingdirectory && newparent) {
937                         error = ext2_inc_nlink(dp);
938                         if (error)
939                                 goto bad;
940
941                         dp->i_flag |= IN_CHANGE;
942                         error = ext2_update(tdvp, !DOINGASYNC(tdvp));
943                         if (error)
944                                 goto bad;
945                 }
946                 error = ext2_direnter(ip, tdvp, tcnp);
947                 if (error) {
948                         if (doingdirectory && newparent) {
949                                 ext2_dec_nlink(dp);
950                                 dp->i_flag |= IN_CHANGE;
951                                 (void)ext2_update(tdvp, 1);
952                         }
953                         goto bad;
954                 }
955                 vput(tdvp);
956         } else {
957                 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
958                         panic("ext2_rename: EXDEV");
959                 /*
960                  * Short circuit rename(foo, foo).
961                  */
962                 if (xp->i_number == ip->i_number)
963                         panic("ext2_rename: same file");
964                 /*
965                  * If the parent directory is "sticky", then the user must
966                  * own the parent directory, or the destination of the rename,
967                  * otherwise the destination may not be changed (except by
968                  * root). This implements append-only directories.
969                  */
970                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
971                     tcnp->cn_cred->cr_uid != dp->i_uid &&
972                     xp->i_uid != tcnp->cn_cred->cr_uid) {
973                         error = EPERM;
974                         goto bad;
975                 }
976                 /*
977                  * Target must be empty if a directory and have no links
978                  * to it. Also, ensure source and target are compatible
979                  * (both directories, or both not directories).
980                  */
981                 if ((xp->i_mode & IFMT) == IFDIR) {
982                         if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
983                                 error = ENOTEMPTY;
984                                 goto bad;
985                         }
986                         if (!doingdirectory) {
987                                 error = ENOTDIR;
988                                 goto bad;
989                         }
990                         cache_purge(tdvp);
991                 } else if (doingdirectory) {
992                         error = EISDIR;
993                         goto bad;
994                 }
995                 error = ext2_dirrewrite(dp, ip, tcnp);
996                 if (error)
997                         goto bad;
998                 /*
999                  * If the target directory is in the same
1000                  * directory as the source directory,
1001                  * decrement the link count on the parent
1002                  * of the target directory.
1003                  */
1004                 if (doingdirectory && !newparent) {
1005                         ext2_dec_nlink(dp);
1006                         dp->i_flag |= IN_CHANGE;
1007                 }
1008                 vput(tdvp);
1009                 /*
1010                  * Adjust the link count of the target to
1011                  * reflect the dirrewrite above.  If this is
1012                  * a directory it is empty and there are
1013                  * no links to it, so we can squash the inode and
1014                  * any space associated with it.  We disallowed
1015                  * renaming over top of a directory with links to
1016                  * it above, as the remaining link would point to
1017                  * a directory without "." or ".." entries.
1018                  */
1019                 ext2_dec_nlink(xp);
1020                 if (doingdirectory) {
1021                         if (xp->i_nlink > 2)
1022                                 panic("ext2_rename: linked directory");
1023                         error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1024                             tcnp->cn_cred, curthread);
1025                         xp->i_nlink = 0;
1026                 }
1027                 xp->i_flag |= IN_CHANGE;
1028                 vput(tvp);
1029                 xp = NULL;
1030         }
1031
1032         /*
1033          * 3) Unlink the source.
1034          */
1035         fcnp->cn_flags &= ~MODMASK;
1036         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1037         VREF(fdvp);
1038         error = vfs_relookup(fdvp, &fvp, fcnp, true);
1039         if (error == 0)
1040                 vrele(fdvp);
1041         if (fvp != NULL) {
1042                 xp = VTOI(fvp);
1043                 dp = VTOI(fdvp);
1044         } else {
1045                 /*
1046                  * From name has disappeared.  IN_RENAME is not sufficient
1047                  * to protect against directory races due to timing windows,
1048                  * so we can't panic here.
1049                  */
1050                 vrele(ap->a_fvp);
1051                 return (0);
1052         }
1053         /*
1054          * Ensure that the directory entry still exists and has not
1055          * changed while the new name has been entered. If the source is
1056          * a file then the entry may have been unlinked or renamed. In
1057          * either case there is no further work to be done. If the source
1058          * is a directory then it cannot have been rmdir'ed; its link
1059          * count of three would cause a rmdir to fail with ENOTEMPTY.
1060          * The IN_RENAME flag ensures that it cannot be moved by another
1061          * rename.
1062          */
1063         if (xp != ip) {
1064                 /*
1065                  * From name resolves to a different inode.  IN_RENAME is
1066                  * not sufficient protection against timing window races
1067                  * so we can't panic here.
1068                  */
1069         } else {
1070                 /*
1071                  * If the source is a directory with a
1072                  * new parent, the link count of the old
1073                  * parent directory must be decremented
1074                  * and ".." set to point to the new parent.
1075                  */
1076                 if (doingdirectory && newparent) {
1077                         ext2_dec_nlink(dp);
1078                         dp->i_flag |= IN_CHANGE;
1079                         dirbuf = malloc(dp->i_e2fs->e2fs_bsize, M_TEMP, M_WAITOK | M_ZERO);
1080                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)dirbuf,
1081                             ip->i_e2fs->e2fs_bsize, (off_t)0,
1082                             UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1083                             tcnp->cn_cred, NOCRED, NULL, NULL);
1084                         if (error == 0) {
1085                                 /* Like ufs little-endian: */
1086                                 namlen = dirbuf->dotdot_type;
1087                                 if (namlen != 2 ||
1088                                     dirbuf->dotdot_name[0] != '.' ||
1089                                     dirbuf->dotdot_name[1] != '.') {
1090                                         /*
1091                                          * The filesystem is in corrupted state,
1092                                          * need to run fsck to fix mangled dir
1093                                          * entry. From other side this error
1094                                          * need to be ignored because it is
1095                                          * too difficult to revert directories
1096                                          * to state before rename from this
1097                                          * point.
1098                                          */
1099                                         ext2_dirbad(xp, (doff_t)12,
1100                                             "rename: mangled dir");
1101                                 } else {
1102                                         dirbuf->dotdot_ino = htole32(newparent);
1103                                         /*
1104                                          * dirblock 0 could be htree root,
1105                                          * try both csum update functions.
1106                                          */
1107                                         ext2_dirent_csum_set(ip,
1108                                             (struct ext2fs_direct_2 *)dirbuf);
1109                                         ext2_dx_csum_set(ip,
1110                                             (struct ext2fs_direct_2 *)dirbuf);
1111                                         (void)vn_rdwr(UIO_WRITE, fvp,
1112                                             (caddr_t)dirbuf,
1113                                             ip->i_e2fs->e2fs_bsize,
1114                                             (off_t)0, UIO_SYSSPACE,
1115                                             IO_NODELOCKED | IO_SYNC |
1116                                             IO_NOMACCHECK, tcnp->cn_cred,
1117                                             NOCRED, NULL, NULL);
1118                                         cache_purge(fdvp);
1119                                 }
1120                         }
1121                         free(dirbuf, M_TEMP);
1122                 }
1123                 error = ext2_dirremove(fdvp, fcnp);
1124                 if (!error) {
1125                         ext2_dec_nlink(xp);
1126                         xp->i_flag |= IN_CHANGE;
1127                 }
1128                 xp->i_flag &= ~IN_RENAME;
1129         }
1130         if (dp)
1131                 vput(fdvp);
1132         if (xp)
1133                 vput(fvp);
1134         vrele(ap->a_fvp);
1135         return (error);
1136
1137 bad:
1138         if (xp)
1139                 vput(ITOV(xp));
1140         vput(ITOV(dp));
1141 out:
1142         if (doingdirectory)
1143                 ip->i_flag &= ~IN_RENAME;
1144         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1145                 ext2_dec_nlink(ip);
1146                 ip->i_flag |= IN_CHANGE;
1147                 ip->i_flag &= ~IN_RENAME;
1148                 vput(fvp);
1149         } else
1150                 vrele(fvp);
1151         return (error);
1152 }
1153
1154 #ifdef UFS_ACL
1155 static int
1156 ext2_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1157     mode_t dmode, struct ucred *cred, struct thread *td)
1158 {
1159         int error;
1160         struct inode *ip = VTOI(tvp);
1161         struct acl *dacl, *acl;
1162
1163         acl = acl_alloc(M_WAITOK);
1164         dacl = acl_alloc(M_WAITOK);
1165
1166         /*
1167          * Retrieve default ACL from parent, if any.
1168          */
1169         error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1170         switch (error) {
1171         case 0:
1172                 /*
1173                  * Retrieved a default ACL, so merge mode and ACL if
1174                  * necessary.  If the ACL is empty, fall through to
1175                  * the "not defined or available" case.
1176                  */
1177                 if (acl->acl_cnt != 0) {
1178                         dmode = acl_posix1e_newfilemode(dmode, acl);
1179                         ip->i_mode = dmode;
1180                         *dacl = *acl;
1181                         ext2_sync_acl_from_inode(ip, acl);
1182                         break;
1183                 }
1184                 /* FALLTHROUGH */
1185
1186         case EOPNOTSUPP:
1187                 /*
1188                  * Just use the mode as-is.
1189                  */
1190                 ip->i_mode = dmode;
1191                 error = 0;
1192                 goto out;
1193
1194         default:
1195                 goto out;
1196         }
1197
1198         error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1199         if (error == 0)
1200                 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1201         switch (error) {
1202         case 0:
1203                 break;
1204
1205         case EOPNOTSUPP:
1206                 /*
1207                  * XXX: This should not happen, as EOPNOTSUPP above
1208                  * was supposed to free acl.
1209                  */
1210 #ifdef DEBUG
1211                 printf("ext2_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1212 #endif  /* DEBUG */
1213                 break;
1214
1215         default:
1216                 goto out;
1217         }
1218
1219 out:
1220         acl_free(acl);
1221         acl_free(dacl);
1222
1223         return (error);
1224 }
1225
1226 static int
1227 ext2_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1228     mode_t mode, struct ucred *cred, struct thread *td)
1229 {
1230         int error;
1231         struct inode *ip = VTOI(tvp);
1232         struct acl *acl;
1233
1234         acl = acl_alloc(M_WAITOK);
1235
1236         /*
1237          * Retrieve default ACL for parent, if any.
1238          */
1239         error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1240         switch (error) {
1241         case 0:
1242                 /*
1243                  * Retrieved a default ACL, so merge mode and ACL if
1244                  * necessary.
1245                  */
1246                 if (acl->acl_cnt != 0) {
1247                         /*
1248                          * Two possible ways for default ACL to not
1249                          * be present.  First, the EA can be
1250                          * undefined, or second, the default ACL can
1251                          * be blank.  If it's blank, fall through to
1252                          * the it's not defined case.
1253                          */
1254                         mode = acl_posix1e_newfilemode(mode, acl);
1255                         ip->i_mode = mode;
1256                         ext2_sync_acl_from_inode(ip, acl);
1257                         break;
1258                 }
1259                 /* FALLTHROUGH */
1260
1261         case EOPNOTSUPP:
1262                 /*
1263                  * Just use the mode as-is.
1264                  */
1265                 ip->i_mode = mode;
1266                 error = 0;
1267                 goto out;
1268
1269         default:
1270                 goto out;
1271         }
1272
1273         error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1274         switch (error) {
1275         case 0:
1276                 break;
1277
1278         case EOPNOTSUPP:
1279                 /*
1280                  * XXX: This should not happen, as EOPNOTSUPP above was
1281                  * supposed to free acl.
1282                  */
1283                 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1284                     "but no VOP_SETACL()\n");
1285                 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1286                     "but no VOP_SETACL()"); */
1287                 break;
1288
1289         default:
1290                 goto out;
1291         }
1292
1293 out:
1294         acl_free(acl);
1295
1296         return (error);
1297 }
1298
1299 #endif /* UFS_ACL */
1300
1301 /*
1302  * Mkdir system call
1303  */
1304 static int
1305 ext2_mkdir(struct vop_mkdir_args *ap)
1306 {
1307         struct m_ext2fs *fs;
1308         struct vnode *dvp = ap->a_dvp;
1309         struct vattr *vap = ap->a_vap;
1310         struct componentname *cnp = ap->a_cnp;
1311         struct inode *ip, *dp;
1312         struct vnode *tvp;
1313         struct dirtemplate dirtemplate, *dtp;
1314         char *buf = NULL;
1315         int error, dmode;
1316
1317         dp = VTOI(dvp);
1318         if ((nlink_t)dp->i_nlink >= EXT4_LINK_MAX &&
1319             !EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
1320                 error = EMLINK;
1321                 goto out;
1322         }
1323         dmode = vap->va_mode & 0777;
1324         dmode |= IFDIR;
1325         /*
1326          * Must simulate part of ext2_makeinode here to acquire the inode,
1327          * but not have it entered in the parent directory. The entry is
1328          * made later after writing "." and ".." entries.
1329          */
1330         error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1331         if (error)
1332                 goto out;
1333         ip = VTOI(tvp);
1334         fs = ip->i_e2fs;
1335         ip->i_gid = dp->i_gid;
1336 #ifdef SUIDDIR
1337         {
1338                 /*
1339                  * if we are hacking owners here, (only do this where told to)
1340                  * and we are not giving it TOO root, (would subvert quotas)
1341                  * then go ahead and give it to the other user.
1342                  * The new directory also inherits the SUID bit.
1343                  * If user's UID and dir UID are the same,
1344                  * 'give it away' so that the SUID is still forced on.
1345                  */
1346                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1347                     (dp->i_mode & ISUID) && dp->i_uid) {
1348                         dmode |= ISUID;
1349                         ip->i_uid = dp->i_uid;
1350                 } else {
1351                         ip->i_uid = cnp->cn_cred->cr_uid;
1352                 }
1353         }
1354 #else
1355         ip->i_uid = cnp->cn_cred->cr_uid;
1356 #endif
1357         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1358         ip->i_mode = dmode;
1359         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
1360         ip->i_nlink = 2;
1361         if (cnp->cn_flags & ISWHITEOUT)
1362                 ip->i_flags |= UF_OPAQUE;
1363         error = ext2_update(tvp, 1);
1364
1365         /*
1366          * Bump link count in parent directory
1367          * to reflect work done below.  Should
1368          * be done before reference is created
1369          * so reparation is possible if we crash.
1370          */
1371         ext2_inc_nlink(dp);
1372         dp->i_flag |= IN_CHANGE;
1373         error = ext2_update(dvp, !DOINGASYNC(dvp));
1374         if (error)
1375                 goto bad;
1376
1377         /* Initialize directory with "." and ".." from static template. */
1378         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1379             EXT2F_INCOMPAT_FTYPE))
1380                 dtp = &mastertemplate;
1381         else
1382                 dtp = &omastertemplate;
1383         dirtemplate = *dtp;
1384         dirtemplate.dot_ino = htole32(ip->i_number);
1385         dirtemplate.dotdot_ino = htole32(dp->i_number);
1386         /*
1387          * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1388          * just redefine it - for this function only
1389          */
1390 #undef  DIRBLKSIZ
1391 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
1392         dirtemplate.dotdot_reclen = htole16(DIRBLKSIZ - 12);
1393         buf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK | M_ZERO);
1394         if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1395                 dirtemplate.dotdot_reclen =
1396                     htole16(le16toh(dirtemplate.dotdot_reclen) -
1397                     sizeof(struct ext2fs_direct_tail));
1398                 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, DIRBLKSIZ));
1399         }
1400         memcpy(buf, &dirtemplate, sizeof(dirtemplate));
1401         ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)buf);
1402         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)buf,
1403             DIRBLKSIZ, (off_t)0, UIO_SYSSPACE,
1404             IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1405             NULL, NULL);
1406         if (error) {
1407                 ext2_dec_nlink(dp);
1408                 dp->i_flag |= IN_CHANGE;
1409                 goto bad;
1410         }
1411         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1412                 /* XXX should grow with balloc() */
1413                 panic("ext2_mkdir: blksize");
1414         else {
1415                 ip->i_size = DIRBLKSIZ;
1416                 ip->i_flag |= IN_CHANGE;
1417         }
1418
1419 #ifdef UFS_ACL
1420         if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1421                 error = ext2_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
1422                     cnp->cn_cred, curthread);
1423                 if (error)
1424                         goto bad;
1425         }
1426
1427 #endif /* UFS_ACL */
1428
1429         /* Directory set up, now install its entry in the parent directory. */
1430         error = ext2_direnter(ip, dvp, cnp);
1431         if (error) {
1432                 ext2_dec_nlink(dp);
1433                 dp->i_flag |= IN_CHANGE;
1434         }
1435 bad:
1436         /*
1437          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1438          * for us because we set the link count to 0.
1439          */
1440         if (error) {
1441                 ip->i_nlink = 0;
1442                 ip->i_flag |= IN_CHANGE;
1443                 vput(tvp);
1444         } else
1445                 *ap->a_vpp = tvp;
1446 out:
1447         free(buf, M_TEMP);
1448         return (error);
1449 #undef  DIRBLKSIZ
1450 #define DIRBLKSIZ  DEV_BSIZE
1451 }
1452
1453 /*
1454  * Rmdir system call.
1455  */
1456 static int
1457 ext2_rmdir(struct vop_rmdir_args *ap)
1458 {
1459         struct vnode *vp = ap->a_vp;
1460         struct vnode *dvp = ap->a_dvp;
1461         struct componentname *cnp = ap->a_cnp;
1462         struct inode *ip, *dp;
1463         int error;
1464
1465         ip = VTOI(vp);
1466         dp = VTOI(dvp);
1467
1468         /*
1469          * Verify the directory is empty (and valid).
1470          * (Rmdir ".." won't be valid since
1471          *  ".." will contain a reference to
1472          *  the current directory and thus be
1473          *  non-empty.)
1474          */
1475         if (!ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1476                 error = ENOTEMPTY;
1477                 goto out;
1478         }
1479         if ((dp->i_flags & APPEND)
1480             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1481                 error = EPERM;
1482                 goto out;
1483         }
1484         /*
1485          * Delete reference to directory before purging
1486          * inode.  If we crash in between, the directory
1487          * will be reattached to lost+found,
1488          */
1489         error = ext2_dirremove(dvp, cnp);
1490         if (error)
1491                 goto out;
1492         ext2_dec_nlink(dp);
1493         dp->i_flag |= IN_CHANGE;
1494         cache_purge(dvp);
1495         VOP_UNLOCK(dvp);
1496         /*
1497          * Truncate inode.  The only stuff left
1498          * in the directory is "." and "..".
1499          */
1500         ip->i_nlink = 0;
1501         error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1502             curthread);
1503         cache_purge(ITOV(ip));
1504         if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1505                 VOP_UNLOCK(vp);
1506                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1507                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1508         }
1509 out:
1510         return (error);
1511 }
1512
1513 /*
1514  * symlink -- make a symbolic link
1515  */
1516 static int
1517 ext2_symlink(struct vop_symlink_args *ap)
1518 {
1519         struct vnode *vp, **vpp = ap->a_vpp;
1520         struct inode *ip;
1521         int len, error;
1522
1523         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1524             vpp, ap->a_cnp);
1525         if (error)
1526                 return (error);
1527         vp = *vpp;
1528         len = strlen(ap->a_target);
1529         if (len < VFSTOEXT2(vp->v_mount)->um_e2fs->e2fs_maxsymlinklen) {
1530                 ip = VTOI(vp);
1531                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1532                 ip->i_size = len;
1533                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1534         } else
1535                 error = vn_rdwr(UIO_WRITE, vp, __DECONST(void *, ap->a_target),
1536                     len, (off_t)0, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1537                     ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1538         if (error)
1539                 vput(vp);
1540         return (error);
1541 }
1542
1543 /*
1544  * Return target name of a symbolic link
1545  */
1546 static int
1547 ext2_readlink(struct vop_readlink_args *ap)
1548 {
1549         struct vnode *vp = ap->a_vp;
1550         struct inode *ip = VTOI(vp);
1551         int isize;
1552
1553         isize = ip->i_size;
1554         if (isize < VFSTOEXT2(vp->v_mount)->um_e2fs->e2fs_maxsymlinklen) {
1555                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1556                 return (0);
1557         }
1558         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1559 }
1560
1561 /*
1562  * Calculate the logical to physical mapping if not done already,
1563  * then call the device strategy routine.
1564  *
1565  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1566  * deadlock on memory.  See ext2_bmap() for details.
1567  */
1568 static int
1569 ext2_strategy(struct vop_strategy_args *ap)
1570 {
1571         struct buf *bp = ap->a_bp;
1572         struct vnode *vp = ap->a_vp;
1573         struct bufobj *bo;
1574         daddr_t blkno;
1575         int error;
1576
1577         if (vp->v_type == VBLK || vp->v_type == VCHR)
1578                 panic("ext2_strategy: spec");
1579         if (bp->b_blkno == bp->b_lblkno) {
1580                 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
1581                         error = ext4_bmapext(vp, bp->b_lblkno, &blkno, NULL, NULL);
1582                 else
1583                         error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1584
1585                 bp->b_blkno = blkno;
1586                 if (error) {
1587                         bp->b_error = error;
1588                         bp->b_ioflags |= BIO_ERROR;
1589                         bufdone(bp);
1590                         return (0);
1591                 }
1592                 if ((long)bp->b_blkno == -1)
1593                         vfs_bio_clrbuf(bp);
1594         }
1595         if ((long)bp->b_blkno == -1) {
1596                 bufdone(bp);
1597                 return (0);
1598         }
1599         bp->b_iooffset = dbtob(bp->b_blkno);
1600         bo = VFSTOEXT2(vp->v_mount)->um_bo;
1601         BO_STRATEGY(bo, bp);
1602         return (0);
1603 }
1604
1605 /*
1606  * Print out the contents of an inode.
1607  */
1608 static int
1609 ext2_print(struct vop_print_args *ap)
1610 {
1611         struct vnode *vp = ap->a_vp;
1612         struct inode *ip = VTOI(vp);
1613
1614         vn_printf(ip->i_devvp, "\tino %ju", (uintmax_t)ip->i_number);
1615         if (vp->v_type == VFIFO)
1616                 fifo_printinfo(vp);
1617         printf("\n");
1618         return (0);
1619 }
1620
1621 /*
1622  * Close wrapper for fifos.
1623  *
1624  * Update the times on the inode then do device close.
1625  */
1626 static int
1627 ext2fifo_close(struct vop_close_args *ap)
1628 {
1629         struct vnode *vp = ap->a_vp;
1630
1631         VI_LOCK(vp);
1632         if (vp->v_usecount > 1)
1633                 ext2_itimes_locked(vp);
1634         VI_UNLOCK(vp);
1635         return (fifo_specops.vop_close(ap));
1636 }
1637
1638 /*
1639  * Return POSIX pathconf information applicable to ext2 filesystems.
1640  */
1641 static int
1642 ext2_pathconf(struct vop_pathconf_args *ap)
1643 {
1644         int error = 0;
1645
1646         switch (ap->a_name) {
1647         case _PC_LINK_MAX:
1648                 if (EXT2_HAS_RO_COMPAT_FEATURE(VTOI(ap->a_vp)->i_e2fs,
1649                     EXT2F_ROCOMPAT_DIR_NLINK))
1650                         *ap->a_retval = INT_MAX;
1651                 else
1652                         *ap->a_retval = EXT4_LINK_MAX;
1653                 break;
1654         case _PC_NAME_MAX:
1655                 *ap->a_retval = NAME_MAX;
1656                 break;
1657         case _PC_PIPE_BUF:
1658                 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
1659                         *ap->a_retval = PIPE_BUF;
1660                 else
1661                         error = EINVAL;
1662                 break;
1663         case _PC_CHOWN_RESTRICTED:
1664                 *ap->a_retval = 1;
1665                 break;
1666         case _PC_NO_TRUNC:
1667                 *ap->a_retval = 1;
1668                 break;
1669
1670 #ifdef UFS_ACL
1671         case _PC_ACL_EXTENDED:
1672                 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1673                         *ap->a_retval = 1;
1674                 else
1675                         *ap->a_retval = 0;
1676                 break;
1677         case _PC_ACL_PATH_MAX:
1678                 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1679                         *ap->a_retval = ACL_MAX_ENTRIES;
1680                 else
1681                         *ap->a_retval = 3;
1682                 break;
1683 #endif /* UFS_ACL */
1684
1685         case _PC_MIN_HOLE_SIZE:
1686                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1687                 break;
1688         case _PC_PRIO_IO:
1689                 *ap->a_retval = 0;
1690                 break;
1691         case _PC_SYNC_IO:
1692                 *ap->a_retval = 0;
1693                 break;
1694         case _PC_ALLOC_SIZE_MIN:
1695                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1696                 break;
1697         case _PC_FILESIZEBITS:
1698                 *ap->a_retval = 64;
1699                 break;
1700         case _PC_REC_INCR_XFER_SIZE:
1701                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1702                 break;
1703         case _PC_REC_MAX_XFER_SIZE:
1704                 *ap->a_retval = -1;     /* means ``unlimited'' */
1705                 break;
1706         case _PC_REC_MIN_XFER_SIZE:
1707                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1708                 break;
1709         case _PC_REC_XFER_ALIGN:
1710                 *ap->a_retval = PAGE_SIZE;
1711                 break;
1712         case _PC_SYMLINK_MAX:
1713                 *ap->a_retval = MAXPATHLEN;
1714                 break;
1715
1716         default:
1717                 error = vop_stdpathconf(ap);
1718                 break;
1719         }
1720         return (error);
1721 }
1722
1723 /*
1724  * Vnode operation to remove a named attribute.
1725  */
1726 static int
1727 ext2_deleteextattr(struct vop_deleteextattr_args *ap)
1728 {
1729         struct inode *ip;
1730         struct m_ext2fs *fs;
1731         int error;
1732
1733         ip = VTOI(ap->a_vp);
1734         fs = ip->i_e2fs;
1735
1736         if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1737                 return (EOPNOTSUPP);
1738
1739         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1740                 return (EOPNOTSUPP);
1741
1742         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1743             ap->a_cred, ap->a_td, VWRITE);
1744         if (error)
1745                 return (error);
1746
1747         error = ENOATTR;
1748
1749         if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1750                 error = ext2_extattr_inode_delete(ip, ap->a_attrnamespace, ap->a_name);
1751                 if (error != ENOATTR)
1752                         return (error);
1753         }
1754
1755         if (ip->i_facl)
1756                 error = ext2_extattr_block_delete(ip, ap->a_attrnamespace, ap->a_name);
1757
1758         return (error);
1759 }
1760
1761 /*
1762  * Vnode operation to retrieve a named extended attribute.
1763  */
1764 static int
1765 ext2_getextattr(struct vop_getextattr_args *ap)
1766 {
1767         struct inode *ip;
1768         struct m_ext2fs *fs;
1769         int error;
1770
1771         ip = VTOI(ap->a_vp);
1772         fs = ip->i_e2fs;
1773
1774         if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1775                 return (EOPNOTSUPP);
1776
1777         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1778                 return (EOPNOTSUPP);
1779
1780         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1781             ap->a_cred, ap->a_td, VREAD);
1782         if (error)
1783                 return (error);
1784
1785         if (ap->a_size != NULL)
1786                 *ap->a_size = 0;
1787
1788         error = ENOATTR;
1789
1790         if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1791                 error = ext2_extattr_inode_get(ip, ap->a_attrnamespace,
1792                     ap->a_name, ap->a_uio, ap->a_size);
1793                 if (error != ENOATTR)
1794                         return (error);
1795         }
1796
1797         if (ip->i_facl)
1798                 error = ext2_extattr_block_get(ip, ap->a_attrnamespace,
1799                     ap->a_name, ap->a_uio, ap->a_size);
1800
1801         return (error);
1802 }
1803
1804 /*
1805  * Vnode operation to retrieve extended attributes on a vnode.
1806  */
1807 static int
1808 ext2_listextattr(struct vop_listextattr_args *ap)
1809 {
1810         struct inode *ip;
1811         struct m_ext2fs *fs;
1812         int error;
1813
1814         ip = VTOI(ap->a_vp);
1815         fs = ip->i_e2fs;
1816
1817         if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1818                 return (EOPNOTSUPP);
1819
1820         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1821                 return (EOPNOTSUPP);
1822
1823         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1824             ap->a_cred, ap->a_td, VREAD);
1825         if (error)
1826                 return (error);
1827
1828         if (ap->a_size != NULL)
1829                 *ap->a_size = 0;
1830
1831         if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1832                 error = ext2_extattr_inode_list(ip, ap->a_attrnamespace,
1833                     ap->a_uio, ap->a_size);
1834                 if (error)
1835                         return (error);
1836         }
1837
1838         if (ip->i_facl)
1839                 error = ext2_extattr_block_list(ip, ap->a_attrnamespace,
1840                     ap->a_uio, ap->a_size);
1841
1842         return (error);
1843 }
1844
1845 /*
1846  * Vnode operation to set a named attribute.
1847  */
1848 static int
1849 ext2_setextattr(struct vop_setextattr_args *ap)
1850 {
1851         struct inode *ip;
1852         struct m_ext2fs *fs;
1853         int error;
1854
1855         ip = VTOI(ap->a_vp);
1856         fs = ip->i_e2fs;
1857
1858         if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1859                 return (EOPNOTSUPP);
1860
1861         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1862                 return (EOPNOTSUPP);
1863
1864         error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1865             ap->a_cred, ap->a_td, VWRITE);
1866         if (error)
1867                 return (error);
1868
1869         error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name);
1870         if (error)
1871                 return (error);
1872
1873         if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1874                 error = ext2_extattr_inode_set(ip, ap->a_attrnamespace,
1875                     ap->a_name, ap->a_uio);
1876                 if (error != ENOSPC)
1877                         return (error);
1878         }
1879
1880         error = ext2_extattr_block_set(ip, ap->a_attrnamespace,
1881             ap->a_name, ap->a_uio);
1882
1883         return (error);
1884 }
1885
1886 /*
1887  * Vnode pointer to File handle
1888  */
1889 /* ARGSUSED */
1890 static int
1891 ext2_vptofh(struct vop_vptofh_args *ap)
1892 {
1893         struct inode *ip;
1894         struct ufid *ufhp;
1895
1896         ip = VTOI(ap->a_vp);
1897         ufhp = (struct ufid *)ap->a_fhp;
1898         ufhp->ufid_len = sizeof(struct ufid);
1899         ufhp->ufid_ino = ip->i_number;
1900         ufhp->ufid_gen = ip->i_gen;
1901         return (0);
1902 }
1903
1904 /*
1905  * Initialize the vnode associated with a new inode, handle aliased
1906  * vnodes.
1907  */
1908 int
1909 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1910 {
1911         struct inode *ip;
1912         struct vnode *vp;
1913
1914         vp = *vpp;
1915         ip = VTOI(vp);
1916         vp->v_type = IFTOVT(ip->i_mode);
1917         /*
1918          * Only unallocated inodes should be of type VNON.
1919          */
1920         if (ip->i_mode != 0 && vp->v_type == VNON)
1921                 return (EINVAL);
1922         if (vp->v_type == VFIFO)
1923                 vp->v_op = fifoops;
1924
1925         if (ip->i_number == EXT2_ROOTINO)
1926                 vp->v_vflag |= VV_ROOT;
1927         ip->i_modrev = init_va_filerev();
1928         *vpp = vp;
1929         return (0);
1930 }
1931
1932 /*
1933  * Allocate a new inode.
1934  */
1935 static int
1936 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1937     struct componentname *cnp)
1938 {
1939         struct inode *ip, *pdir;
1940         struct vnode *tvp;
1941         int error;
1942
1943         pdir = VTOI(dvp);
1944         *vpp = NULL;
1945         if ((mode & IFMT) == 0)
1946                 mode |= IFREG;
1947
1948         error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1949         if (error) {
1950                 return (error);
1951         }
1952         ip = VTOI(tvp);
1953         ip->i_gid = pdir->i_gid;
1954 #ifdef SUIDDIR
1955         {
1956                 /*
1957                  * if we are
1958                  * not the owner of the directory,
1959                  * and we are hacking owners here, (only do this where told to)
1960                  * and we are not giving it TOO root, (would subvert quotas)
1961                  * then go ahead and give it to the other user.
1962                  * Note that this drops off the execute bits for security.
1963                  */
1964                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1965                     (pdir->i_mode & ISUID) &&
1966                     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1967                         ip->i_uid = pdir->i_uid;
1968                         mode &= ~07111;
1969                 } else {
1970                         ip->i_uid = cnp->cn_cred->cr_uid;
1971                 }
1972         }
1973 #else
1974         ip->i_uid = cnp->cn_cred->cr_uid;
1975 #endif
1976         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1977         ip->i_mode = mode;
1978         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1979         ip->i_nlink = 1;
1980         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1981                 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID))
1982                         ip->i_mode &= ~ISGID;
1983         }
1984
1985         if (cnp->cn_flags & ISWHITEOUT)
1986                 ip->i_flags |= UF_OPAQUE;
1987
1988         /*
1989          * Make sure inode goes to disk before directory entry.
1990          */
1991         error = ext2_update(tvp, !DOINGASYNC(tvp));
1992         if (error)
1993                 goto bad;
1994
1995 #ifdef UFS_ACL
1996         if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1997                 error = ext2_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
1998                     cnp->cn_cred, curthread);
1999                 if (error)
2000                         goto bad;
2001         }
2002 #endif /* UFS_ACL */
2003
2004         error = ext2_direnter(ip, dvp, cnp);
2005         if (error)
2006                 goto bad;
2007
2008         *vpp = tvp;
2009         return (0);
2010
2011 bad:
2012         /*
2013          * Write error occurred trying to update the inode
2014          * or the directory so must deallocate the inode.
2015          */
2016         ip->i_nlink = 0;
2017         ip->i_flag |= IN_CHANGE;
2018         vput(tvp);
2019         return (error);
2020 }
2021
2022 /*
2023  * Vnode op for reading.
2024  */
2025 static int
2026 ext2_read(struct vop_read_args *ap)
2027 {
2028         struct vnode *vp;
2029         struct inode *ip;
2030         struct uio *uio;
2031         struct m_ext2fs *fs;
2032         struct buf *bp;
2033         daddr_t lbn, nextlbn;
2034         off_t bytesinfile;
2035         long size, xfersize, blkoffset;
2036         int error, orig_resid, seqcount;
2037         int ioflag;
2038
2039         vp = ap->a_vp;
2040         uio = ap->a_uio;
2041         ioflag = ap->a_ioflag;
2042
2043         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
2044         ip = VTOI(vp);
2045
2046 #ifdef INVARIANTS
2047         if (uio->uio_rw != UIO_READ)
2048                 panic("%s: mode", "ext2_read");
2049
2050         if (vp->v_type == VLNK) {
2051                 if ((int)ip->i_size <
2052                     VFSTOEXT2(vp->v_mount)->um_e2fs->e2fs_maxsymlinklen)
2053                         panic("%s: short symlink", "ext2_read");
2054         } else if (vp->v_type != VREG && vp->v_type != VDIR)
2055                 panic("%s: type %d", "ext2_read", vp->v_type);
2056 #endif
2057         orig_resid = uio->uio_resid;
2058         KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
2059         if (orig_resid == 0)
2060                 return (0);
2061         KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
2062         fs = ip->i_e2fs;
2063         if (uio->uio_offset < ip->i_size &&
2064             uio->uio_offset >= fs->e2fs_maxfilesize)
2065                 return (EOVERFLOW);
2066
2067         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
2068                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
2069                         break;
2070                 lbn = lblkno(fs, uio->uio_offset);
2071                 nextlbn = lbn + 1;
2072                 size = blksize(fs, ip, lbn);
2073                 blkoffset = blkoff(fs, uio->uio_offset);
2074
2075                 xfersize = fs->e2fs_fsize - blkoffset;
2076                 if (uio->uio_resid < xfersize)
2077                         xfersize = uio->uio_resid;
2078                 if (bytesinfile < xfersize)
2079                         xfersize = bytesinfile;
2080
2081                 if (lblktosize(fs, nextlbn) >= ip->i_size)
2082                         error = bread(vp, lbn, size, NOCRED, &bp);
2083                 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
2084                         error = cluster_read(vp, ip->i_size, lbn, size,
2085                             NOCRED, blkoffset + uio->uio_resid, seqcount,
2086                             0, &bp);
2087                 } else if (seqcount > 1) {
2088                         u_int nextsize = blksize(fs, ip, nextlbn);
2089
2090                         error = breadn(vp, lbn,
2091                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
2092                 } else
2093                         error = bread(vp, lbn, size, NOCRED, &bp);
2094                 if (error) {
2095                         brelse(bp);
2096                         bp = NULL;
2097                         break;
2098                 }
2099
2100                 /*
2101                  * We should only get non-zero b_resid when an I/O error
2102                  * has occurred, which should cause us to break above.
2103                  * However, if the short read did not cause an error,
2104                  * then we want to ensure that we do not uiomove bad
2105                  * or uninitialized data.
2106                  */
2107                 size -= bp->b_resid;
2108                 if (size < xfersize) {
2109                         if (size == 0)
2110                                 break;
2111                         xfersize = size;
2112                 }
2113                 error = uiomove((char *)bp->b_data + blkoffset,
2114                     (int)xfersize, uio);
2115                 if (error)
2116                         break;
2117                 vfs_bio_brelse(bp, ioflag);
2118         }
2119
2120         /*
2121          * This can only happen in the case of an error because the loop
2122          * above resets bp to NULL on each iteration and on normal
2123          * completion has not set a new value into it. so it must have come
2124          * from a 'break' statement
2125          */
2126         if (bp != NULL)
2127                 vfs_bio_brelse(bp, ioflag);
2128
2129         if ((error == 0 || uio->uio_resid != orig_resid) &&
2130             (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
2131                 ip->i_flag |= IN_ACCESS;
2132         return (error);
2133 }
2134
2135 static int
2136 ext2_ioctl(struct vop_ioctl_args *ap)
2137 {
2138         struct vnode *vp;
2139         int error;
2140
2141         vp = ap->a_vp;
2142         switch (ap->a_command) {
2143         case FIOSEEKDATA:
2144                 if (!(VTOI(vp)->i_flag & IN_E4EXTENTS)) {
2145                         error = vn_lock(vp, LK_SHARED);
2146                         if (error == 0) {
2147                                 error = ext2_bmap_seekdata(vp,
2148                                     (off_t *)ap->a_data);
2149                                 VOP_UNLOCK(vp);
2150                         } else
2151                                 error = EBADF;
2152                         return (error);
2153                 }
2154         case FIOSEEKHOLE:
2155                 return (vn_bmap_seekhole(vp, ap->a_command,
2156                     (off_t *)ap->a_data, ap->a_cred));
2157         default:
2158                 return (ENOTTY);
2159         }
2160 }
2161
2162 /*
2163  * Vnode op for writing.
2164  */
2165 static int
2166 ext2_write(struct vop_write_args *ap)
2167 {
2168         struct vnode *vp;
2169         struct uio *uio;
2170         struct inode *ip;
2171         struct m_ext2fs *fs;
2172         struct buf *bp;
2173         daddr_t lbn;
2174         off_t osize;
2175         int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
2176
2177         ioflag = ap->a_ioflag;
2178         uio = ap->a_uio;
2179         vp = ap->a_vp;
2180
2181         seqcount = ioflag >> IO_SEQSHIFT;
2182         ip = VTOI(vp);
2183
2184 #ifdef INVARIANTS
2185         if (uio->uio_rw != UIO_WRITE)
2186                 panic("%s: mode", "ext2_write");
2187 #endif
2188
2189         switch (vp->v_type) {
2190         case VREG:
2191                 if (ioflag & IO_APPEND)
2192                         uio->uio_offset = ip->i_size;
2193                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
2194                         return (EPERM);
2195                 /* FALLTHROUGH */
2196         case VLNK:
2197                 break;
2198         case VDIR:
2199                 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
2200                 if ((ioflag & IO_SYNC) == 0)
2201                         panic("ext2_write: nonsync dir write");
2202                 break;
2203         default:
2204                 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
2205                     vp->v_type, (intmax_t)uio->uio_offset,
2206                     (intmax_t)uio->uio_resid);
2207         }
2208
2209         KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
2210         KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
2211         fs = ip->i_e2fs;
2212         if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
2213                 return (EFBIG);
2214         /*
2215          * Maybe this should be above the vnode op call, but so long as
2216          * file servers have no limits, I don't think it matters.
2217          */
2218         error = vn_rlimit_fsize(vp, uio, uio->uio_td);
2219         if (error != 0)
2220                 return (error);
2221
2222         resid = uio->uio_resid;
2223         osize = ip->i_size;
2224         if (seqcount > BA_SEQMAX)
2225                 flags = BA_SEQMAX << BA_SEQSHIFT;
2226         else
2227                 flags = seqcount << BA_SEQSHIFT;
2228         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
2229                 flags |= IO_SYNC;
2230
2231         for (error = 0; uio->uio_resid > 0;) {
2232                 lbn = lblkno(fs, uio->uio_offset);
2233                 blkoffset = blkoff(fs, uio->uio_offset);
2234                 xfersize = fs->e2fs_fsize - blkoffset;
2235                 if (uio->uio_resid < xfersize)
2236                         xfersize = uio->uio_resid;
2237                 if (uio->uio_offset + xfersize > ip->i_size)
2238                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
2239
2240                 /*
2241                  * We must perform a read-before-write if the transfer size
2242                  * does not cover the entire buffer.
2243                  */
2244                 if (fs->e2fs_bsize > xfersize)
2245                         flags |= BA_CLRBUF;
2246                 else
2247                         flags &= ~BA_CLRBUF;
2248                 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2249                     ap->a_cred, &bp, flags);
2250                 if (error != 0)
2251                         break;
2252
2253                 if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2254                         bp->b_flags |= B_NOCACHE;
2255                 if (uio->uio_offset + xfersize > ip->i_size)
2256                         ip->i_size = uio->uio_offset + xfersize;
2257                 size = blksize(fs, ip, lbn) - bp->b_resid;
2258                 if (size < xfersize)
2259                         xfersize = size;
2260
2261                 error =
2262                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2263                 /*
2264                  * If the buffer is not already filled and we encounter an
2265                  * error while trying to fill it, we have to clear out any
2266                  * garbage data from the pages instantiated for the buffer.
2267                  * If we do not, a failed uiomove() during a write can leave
2268                  * the prior contents of the pages exposed to a userland mmap.
2269                  *
2270                  * Note that we need only clear buffers with a transfer size
2271                  * equal to the block size because buffers with a shorter
2272                  * transfer size were cleared above by the call to ext2_balloc()
2273                  * with the BA_CLRBUF flag set.
2274                  *
2275                  * If the source region for uiomove identically mmaps the
2276                  * buffer, uiomove() performed the NOP copy, and the buffer
2277                  * content remains valid because the page fault handler
2278                  * validated the pages.
2279                  */
2280                 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2281                     fs->e2fs_bsize == xfersize)
2282                         vfs_bio_clrbuf(bp);
2283
2284                 vfs_bio_set_flags(bp, ioflag);
2285
2286                 /*
2287                  * If IO_SYNC each buffer is written synchronously.  Otherwise
2288                  * if we have a severe page deficiency write the buffer
2289                  * asynchronously.  Otherwise try to cluster, and if that
2290                  * doesn't do it then either do an async write (if O_DIRECT),
2291                  * or a delayed write (if not).
2292                  */
2293                 if (ioflag & IO_SYNC) {
2294                         (void)bwrite(bp);
2295                 } else if (vm_page_count_severe() ||
2296                             buf_dirty_count_severe() ||
2297                     (ioflag & IO_ASYNC)) {
2298                         bp->b_flags |= B_CLUSTEROK;
2299                         bawrite(bp);
2300                 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2301                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2302                                 bp->b_flags |= B_CLUSTEROK;
2303                                 cluster_write(vp, &ip->i_clusterw, bp,
2304                                     ip->i_size, seqcount, 0);
2305                         } else {
2306                                 bawrite(bp);
2307                         }
2308                 } else if (ioflag & IO_DIRECT) {
2309                         bp->b_flags |= B_CLUSTEROK;
2310                         bawrite(bp);
2311                 } else {
2312                         bp->b_flags |= B_CLUSTEROK;
2313                         bdwrite(bp);
2314                 }
2315                 if (error || xfersize == 0)
2316                         break;
2317         }
2318         /*
2319          * If we successfully wrote any data, and we are not the superuser
2320          * we clear the setuid and setgid bits as a precaution against
2321          * tampering.
2322          */
2323         if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2324             ap->a_cred) {
2325                 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID))
2326                         ip->i_mode &= ~(ISUID | ISGID);
2327         }
2328         if (error) {
2329                 if (ioflag & IO_UNIT) {
2330                         (void)ext2_truncate(vp, osize,
2331                             ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2332                         uio->uio_offset -= resid - uio->uio_resid;
2333                         uio->uio_resid = resid;
2334                 }
2335         }
2336         if (uio->uio_resid != resid) {
2337                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2338                 if (ioflag & IO_SYNC)
2339                         error = ext2_update(vp, 1);
2340         }
2341         return (error);
2342 }