]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/gnu/fs/ext2fs/ext2_vnops.c
This commit was generated by cvs2svn to compensate for changes in r165538,
[FreeBSD/FreeBSD.git] / sys / gnu / 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  * Copyright (c) 1982, 1986, 1989, 1993
9  *      The Regents of the University of California.  All rights reserved.
10  * (c) UNIX System Laboratories, Inc.
11  * All or some portions of this file are derived from material licensed
12  * to the University of California by American Telephone and Telegraph
13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14  * the permission of UNIX System Laboratories, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
41  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
42  * $FreeBSD$
43  */
44
45 #include "opt_suiddir.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/resourcevar.h>
50 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/stat.h>
53 #include <sys/bio.h>
54 #include <sys/buf.h>
55 #include <sys/priv.h>
56 #include <sys/proc.h>
57 #include <sys/mount.h>
58 #include <sys/unistd.h>
59 #include <sys/time.h>
60 #include <sys/vnode.h>
61 #include <sys/namei.h>
62 #include <sys/lockf.h>
63 #include <sys/event.h>
64 #include <sys/conf.h>
65 #include <sys/file.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vnode_pager.h>
70
71 #include <fs/fifofs/fifo.h>
72
73 #include <sys/signalvar.h>
74 #include <ufs/ufs/dir.h>
75
76 #include <gnu/fs/ext2fs/inode.h>
77 #include <gnu/fs/ext2fs/ext2_mount.h>
78 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
79 #include <gnu/fs/ext2fs/fs.h>
80 #include <gnu/fs/ext2fs/ext2_extern.h>
81 #include <gnu/fs/ext2fs/ext2_fs.h>
82
83 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
84
85 static vop_access_t     ext2_access;
86 static vop_advlock_t    ext2_advlock;
87 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
88 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
89     struct thread *);
90 static vop_close_t      ext2_close;
91 static vop_create_t     ext2_create;
92 static vop_fsync_t      ext2_fsync;
93 static vop_getattr_t    ext2_getattr;
94 static vop_kqfilter_t   ext2_kqfilter;
95 static vop_link_t       ext2_link;
96 static vop_mkdir_t      ext2_mkdir;
97 static vop_mknod_t      ext2_mknod;
98 static vop_open_t       ext2_open;
99 static vop_pathconf_t   ext2_pathconf;
100 static vop_print_t      ext2_print;
101 static vop_read_t       ext2_read;
102 static vop_readlink_t   ext2_readlink;
103 static vop_remove_t     ext2_remove;
104 static vop_rename_t     ext2_rename;
105 static vop_rmdir_t      ext2_rmdir;
106 static vop_setattr_t    ext2_setattr;
107 static vop_strategy_t   ext2_strategy;
108 static vop_symlink_t    ext2_symlink;
109 static vop_write_t      ext2_write;
110 static vop_close_t      ext2fifo_close;
111 static vop_kqfilter_t   ext2fifo_kqfilter;
112 static int filt_ext2read(struct knote *kn, long hint);
113 static int filt_ext2write(struct knote *kn, long hint);
114 static int filt_ext2vnode(struct knote *kn, long hint);
115 static void filt_ext2detach(struct knote *kn);
116
117 /* Global vfs data structures for ext2. */
118 struct vop_vector ext2_vnodeops = {
119         .vop_default =          &default_vnodeops,
120         .vop_access =           ext2_access,
121         .vop_advlock =          ext2_advlock,
122         .vop_bmap =             ext2_bmap,
123         .vop_cachedlookup =     ext2_lookup,
124         .vop_close =            ext2_close,
125         .vop_create =           ext2_create,
126         .vop_fsync =            ext2_fsync,
127         .vop_getattr =          ext2_getattr,
128         .vop_inactive =         ext2_inactive,
129         .vop_link =             ext2_link,
130         .vop_lookup =           vfs_cache_lookup,
131         .vop_mkdir =            ext2_mkdir,
132         .vop_mknod =            ext2_mknod,
133         .vop_open =             ext2_open,
134         .vop_pathconf =         ext2_pathconf,
135         .vop_poll =             vop_stdpoll,
136         .vop_kqfilter =         ext2_kqfilter,
137         .vop_print =            ext2_print,
138         .vop_read =             ext2_read,
139         .vop_readdir =          ext2_readdir,
140         .vop_readlink =         ext2_readlink,
141         .vop_reallocblks =      ext2_reallocblks,
142         .vop_reclaim =          ext2_reclaim,
143         .vop_remove =           ext2_remove,
144         .vop_rename =           ext2_rename,
145         .vop_rmdir =            ext2_rmdir,
146         .vop_setattr =          ext2_setattr,
147         .vop_strategy =         ext2_strategy,
148         .vop_symlink =          ext2_symlink,
149         .vop_write =            ext2_write,
150 };
151
152 struct vop_vector ext2_fifoops = {
153         .vop_default =          &fifo_specops,
154         .vop_access =           ext2_access,
155         .vop_close =            ext2fifo_close,
156         .vop_fsync =            ext2_fsync,
157         .vop_getattr =          ext2_getattr,
158         .vop_inactive =         ext2_inactive,
159         .vop_kqfilter =         ext2fifo_kqfilter,
160         .vop_print =            ext2_print,
161         .vop_read =             VOP_PANIC,
162         .vop_reclaim =          ext2_reclaim,
163         .vop_setattr =          ext2_setattr,
164         .vop_write =            VOP_PANIC,
165 };
166
167 #include <gnu/fs/ext2fs/ext2_readwrite.c>
168
169 /*
170  * A virgin directory (no blushing please).
171  * Note that the type and namlen fields are reversed relative to ext2.
172  * Also, we don't use `struct odirtemplate', since it would just cause
173  * endianness problems.
174  */
175 static struct dirtemplate mastertemplate = {
176         0, 12, 1, EXT2_FT_DIR, ".",
177         0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
178 };
179 static struct dirtemplate omastertemplate = {
180         0, 12, 1, EXT2_FT_UNKNOWN, ".",
181         0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
182 };
183
184 void
185 ext2_itimes(vp)
186         struct vnode *vp;
187 {
188         struct inode *ip;
189         struct timespec ts;
190
191         ip = VTOI(vp);
192         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
193                 return;
194         if ((vp->v_type == VBLK || vp->v_type == VCHR))
195                 ip->i_flag |= IN_LAZYMOD;
196         else
197                 ip->i_flag |= IN_MODIFIED;
198         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
199                 vfs_timestamp(&ts);
200                 if (ip->i_flag & IN_ACCESS) {
201                         ip->i_atime = ts.tv_sec;
202                         ip->i_atimensec = ts.tv_nsec;
203                 }
204                 if (ip->i_flag & IN_UPDATE) {
205                         ip->i_mtime = ts.tv_sec;
206                         ip->i_mtimensec = ts.tv_nsec;
207                         ip->i_modrev++;
208                 }
209                 if (ip->i_flag & IN_CHANGE) {
210                         ip->i_ctime = ts.tv_sec;
211                         ip->i_ctimensec = ts.tv_nsec;
212                 }
213         }
214         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
215 }
216
217 /*
218  * Create a regular file
219  */
220 static int
221 ext2_create(ap)
222         struct vop_create_args /* {
223                 struct vnode *a_dvp;
224                 struct vnode **a_vpp;
225                 struct componentname *a_cnp;
226                 struct vattr *a_vap;
227         } */ *ap;
228 {
229         int error;
230
231         error =
232             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
233             ap->a_dvp, ap->a_vpp, ap->a_cnp);
234         if (error)
235                 return (error);
236         return (0);
237 }
238
239 static int
240 ext2_open(ap)
241         struct vop_open_args /* {
242                 struct vnode *a_vp;
243                 int  a_mode;
244                 struct ucred *a_cred;
245                 struct thread *a_td;
246         } */ *ap;
247 {
248
249         if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
250                 return (EOPNOTSUPP);
251
252         /*
253          * Files marked append-only must be opened for appending.
254          */
255         if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
256             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
257                 return (EPERM);
258
259         vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
260
261         return (0);
262 }
263
264 /*
265  * Close called.
266  *
267  * Update the times on the inode.
268  */
269 static int
270 ext2_close(ap)
271         struct vop_close_args /* {
272                 struct vnode *a_vp;
273                 int  a_fflag;
274                 struct ucred *a_cred;
275                 struct thread *a_td;
276         } */ *ap;
277 {
278         struct vnode *vp = ap->a_vp;
279
280         VI_LOCK(vp);
281         if (vp->v_usecount > 1)
282                 ext2_itimes(vp);
283         VI_UNLOCK(vp);
284         return (0);
285 }
286
287 static int
288 ext2_access(ap)
289         struct vop_access_args /* {
290                 struct vnode *a_vp;
291                 int  a_mode;
292                 struct ucred *a_cred;
293                 struct thread *a_td;
294         } */ *ap;
295 {
296         struct vnode *vp = ap->a_vp;
297         struct inode *ip = VTOI(vp);
298         mode_t mode = ap->a_mode;
299         int error;
300
301         if (vp->v_type == VBLK || vp->v_type == VCHR)
302                 return (EOPNOTSUPP);
303
304         /*
305          * Disallow write attempts on read-only file systems;
306          * unless the file is a socket, fifo, or a block or
307          * character device resident on the file system.
308          */
309         if (mode & VWRITE) {
310                 switch (vp->v_type) {
311                 case VDIR:
312                 case VLNK:
313                 case VREG:
314                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
315                                 return (EROFS);
316                         break;
317                 default:
318                         break;
319                 }
320         }
321
322         /* If immutable bit set, nobody gets to write it. */
323         if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
324                 return (EPERM);
325
326         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
327             ap->a_mode, ap->a_cred, NULL);
328         return (error);
329 }
330
331 static int
332 ext2_getattr(ap)
333         struct vop_getattr_args /* {
334                 struct vnode *a_vp;
335                 struct vattr *a_vap;
336                 struct ucred *a_cred;
337                 struct thread *a_td;
338         } */ *ap;
339 {
340         struct vnode *vp = ap->a_vp;
341         struct inode *ip = VTOI(vp);
342         struct vattr *vap = ap->a_vap;
343
344         ext2_itimes(vp);
345         /*
346          * Copy from inode table
347          */
348         vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
349         vap->va_fileid = ip->i_number;
350         vap->va_mode = ip->i_mode & ~IFMT;
351         vap->va_nlink = ip->i_nlink;
352         vap->va_uid = ip->i_uid;
353         vap->va_gid = ip->i_gid;
354         vap->va_rdev = ip->i_rdev;
355         vap->va_size = ip->i_size;
356         vap->va_atime.tv_sec = ip->i_atime;
357         vap->va_atime.tv_nsec = ip->i_atimensec;
358         vap->va_mtime.tv_sec = ip->i_mtime;
359         vap->va_mtime.tv_nsec = ip->i_mtimensec;
360         vap->va_ctime.tv_sec = ip->i_ctime;
361         vap->va_ctime.tv_nsec = ip->i_ctimensec;
362         vap->va_flags = ip->i_flags;
363         vap->va_gen = ip->i_gen;
364         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
365         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
366         vap->va_type = IFTOVT(ip->i_mode);
367         vap->va_filerev = ip->i_modrev;
368         return (0);
369 }
370
371 /*
372  * Set attribute vnode op. called from several syscalls
373  */
374 static int
375 ext2_setattr(ap)
376         struct vop_setattr_args /* {
377                 struct vnode *a_vp;
378                 struct vattr *a_vap;
379                 struct ucred *a_cred;
380                 struct thread *a_td;
381         } */ *ap;
382 {
383         struct vattr *vap = ap->a_vap;
384         struct vnode *vp = ap->a_vp;
385         struct inode *ip = VTOI(vp);
386         struct ucred *cred = ap->a_cred;
387         struct thread *td = ap->a_td;
388         int error;
389
390         /*
391          * Check for unsettable attributes.
392          */
393         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
394             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
395             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
396             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
397                 return (EINVAL);
398         }
399         if (vap->va_flags != VNOVAL) {
400                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
401                         return (EROFS);
402                 /*
403                  * Callers may only modify the file flags on objects they
404                  * have VADMIN rights for.
405                  */
406                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
407                         return (error);
408                 /*
409                  * Unprivileged processes and privileged processes in
410                  * jail() are not permitted to unset system flags, or
411                  * modify flags if any system flags are set.
412                  * Privileged non-jail processes may not modify system flags
413                  * if securelevel > 0 and any existing system flags are set.
414                  */
415                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS,
416                     SUSER_ALLOWJAIL)) {
417                         if (ip->i_flags
418                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
419                                 error = securelevel_gt(cred, 0);
420                                 if (error)
421                                         return (error);
422                         }
423                         ip->i_flags = vap->va_flags;
424                 } else {
425                         if (ip->i_flags
426                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
427                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
428                                 return (EPERM);
429                         ip->i_flags &= SF_SETTABLE;
430                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
431                 }
432                 ip->i_flag |= IN_CHANGE;
433                 if (vap->va_flags & (IMMUTABLE | APPEND))
434                         return (0);
435         }
436         if (ip->i_flags & (IMMUTABLE | APPEND))
437                 return (EPERM);
438         /*
439          * Go through the fields and update iff not VNOVAL.
440          */
441         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
442                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
443                         return (EROFS);
444                 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
445                     td)) != 0)
446                         return (error);
447         }
448         if (vap->va_size != VNOVAL) {
449                 /*
450                  * Disallow write attempts on read-only file systems;
451                  * unless the file is a socket, fifo, or a block or
452                  * character device resident on the file system.
453                  */
454                 switch (vp->v_type) {
455                 case VDIR:
456                         return (EISDIR);
457                 case VLNK:
458                 case VREG:
459                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
460                                 return (EROFS);
461                         break;
462                 default:
463                         break;
464                 }
465                 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
466                         return (error);
467         }
468         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
469                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
470                         return (EROFS);
471                 /*
472                  * From utimes(2):
473                  * If times is NULL, ... The caller must be the owner of
474                  * the file, have permission to write the file, or be the
475                  * super-user.
476                  * If times is non-NULL, ... The caller must be the owner of
477                  * the file or be the super-user.
478                  */
479                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
480                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
481                     (error = VOP_ACCESS(vp, VWRITE, cred, td))))
482                         return (error);
483                 if (vap->va_atime.tv_sec != VNOVAL)
484                         ip->i_flag |= IN_ACCESS;
485                 if (vap->va_mtime.tv_sec != VNOVAL)
486                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
487                 ext2_itimes(vp);
488                 if (vap->va_atime.tv_sec != VNOVAL) {
489                         ip->i_atime = vap->va_atime.tv_sec;
490                         ip->i_atimensec = vap->va_atime.tv_nsec;
491                 }
492                 if (vap->va_mtime.tv_sec != VNOVAL) {
493                         ip->i_mtime = vap->va_mtime.tv_sec;
494                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
495                 }
496                 error = ext2_update(vp, 0);
497                 if (error)
498                         return (error);
499         }
500         error = 0;
501         if (vap->va_mode != (mode_t)VNOVAL) {
502                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
503                         return (EROFS);
504                 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
505         }
506         return (error);
507 }
508
509 /*
510  * Change the mode on a file.
511  * Inode must be locked before calling.
512  */
513 static int
514 ext2_chmod(vp, mode, cred, td)
515         struct vnode *vp;
516         int mode;
517         struct ucred *cred;
518         struct thread *td;
519 {
520         struct inode *ip = VTOI(vp);
521         int error;
522
523         /*
524          * To modify the permissions on a file, must possess VADMIN
525          * for that file.
526          */
527         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
528                 return (error);
529         /*
530          * Privileged processes may set the sticky bit on non-directories,
531          * as well as set the setgid bit on a file with a group that the
532          * process is not a member of.
533          */
534         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
535                 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE,
536                     SUSER_ALLOWJAIL);
537                 if (error)
538                         return (EFTYPE);
539         }
540         if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
541                 error = priv_check_cred(cred, PRIV_VFS_SETGID,
542                     SUSER_ALLOWJAIL);
543                 if (error)
544                         return (error);
545         }
546         ip->i_mode &= ~ALLPERMS;
547         ip->i_mode |= (mode & ALLPERMS);
548         ip->i_flag |= IN_CHANGE;
549         return (0);
550 }
551
552 /*
553  * Perform chown operation on inode ip;
554  * inode must be locked prior to call.
555  */
556 static int
557 ext2_chown(vp, uid, gid, cred, td)
558         struct vnode *vp;
559         uid_t uid;
560         gid_t gid;
561         struct ucred *cred;
562         struct thread *td;
563 {
564         struct inode *ip = VTOI(vp);
565         uid_t ouid;
566         gid_t ogid;
567         int error = 0;
568
569         if (uid == (uid_t)VNOVAL)
570                 uid = ip->i_uid;
571         if (gid == (gid_t)VNOVAL)
572                 gid = ip->i_gid;
573         /*
574          * To modify the ownership of a file, must possess VADMIN
575          * for that file.
576          */
577         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
578                 return (error);
579         /*
580          * To change the owner of a file, or change the group of a file
581          * to a group of which we are not a member, the caller must
582          * have privilege.
583          */
584         if (uid != ip->i_uid || (gid != ip->i_gid &&
585             !groupmember(gid, cred))) {
586                 error = priv_check_cred(cred, PRIV_VFS_CHOWN,
587                     SUSER_ALLOWJAIL);
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 (ouid != uid || ogid != gid) {
597                 if (priv_check_cred(cred, PRIV_VFS_CLEARSUGID,
598                     SUSER_ALLOWJAIL) != 0)
599                         ip->i_mode &= ~(ISUID | ISGID);
600         }
601         return (0);
602 }
603
604 /*
605  * Synch an open file.
606  */
607 /* ARGSUSED */
608 static int
609 ext2_fsync(ap)
610         struct vop_fsync_args /* {
611                 struct vnode *a_vp;
612                 struct ucred *a_cred;
613                 int a_waitfor;
614                 struct thread *a_td;
615         } */ *ap;
616 {
617         /*
618          * Flush all dirty buffers associated with a vnode.
619          */
620         ext2_discard_prealloc(VTOI(ap->a_vp));
621
622         vop_stdfsync(ap);
623
624         return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
625 }
626
627 /*
628  * Mknod vnode call
629  */
630 /* ARGSUSED */
631 static int
632 ext2_mknod(ap)
633         struct vop_mknod_args /* {
634                 struct vnode *a_dvp;
635                 struct vnode **a_vpp;
636                 struct componentname *a_cnp;
637                 struct vattr *a_vap;
638         } */ *ap;
639 {
640         struct vattr *vap = ap->a_vap;
641         struct vnode **vpp = ap->a_vpp;
642         struct inode *ip;
643         ino_t ino;
644         int error;
645
646         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
647             ap->a_dvp, vpp, ap->a_cnp);
648         if (error)
649                 return (error);
650         ip = VTOI(*vpp);
651         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
652         if (vap->va_rdev != VNOVAL) {
653                 /*
654                  * Want to be able to use this to make badblock
655                  * inodes, so don't truncate the dev number.
656                  */
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(ap)
678         struct vop_remove_args /* {
679                 struct vnode *a_dvp;
680                 struct vnode *a_vp;
681                 struct componentname *a_cnp;
682         } */ *ap;
683 {
684         struct inode *ip;
685         struct vnode *vp = ap->a_vp;
686         struct vnode *dvp = ap->a_dvp;
687         int error;
688
689         ip = VTOI(vp);
690         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
691             (VTOI(dvp)->i_flags & APPEND)) {
692                 error = EPERM;
693                 goto out;
694         }
695         error = ext2_dirremove(dvp, ap->a_cnp);
696         if (error == 0) {
697                 ip->i_nlink--;
698                 ip->i_flag |= IN_CHANGE;
699         }
700 out:
701         return (error);
702 }
703
704 /*
705  * link vnode call
706  */
707 static int
708 ext2_link(ap)
709         struct vop_link_args /* {
710                 struct vnode *a_tdvp;
711                 struct vnode *a_vp;
712                 struct componentname *a_cnp;
713         } */ *ap;
714 {
715         struct vnode *vp = ap->a_vp;
716         struct vnode *tdvp = ap->a_tdvp;
717         struct componentname *cnp = ap->a_cnp;
718         struct inode *ip;
719         int error;
720
721 #ifdef DIAGNOSTIC
722         if ((cnp->cn_flags & HASBUF) == 0)
723                 panic("ext2_link: no name");
724 #endif
725         if (tdvp->v_mount != vp->v_mount) {
726                 error = EXDEV;
727                 goto out;
728         }
729         ip = VTOI(vp);
730         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
731                 error = EMLINK;
732                 goto out;
733         }
734         if (ip->i_flags & (IMMUTABLE | APPEND)) {
735                 error = EPERM;
736                 goto out;
737         }
738         ip->i_nlink++;
739         ip->i_flag |= IN_CHANGE;
740         error = ext2_update(vp, 1);
741         if (!error)
742                 error = ext2_direnter(ip, tdvp, cnp);
743         if (error) {
744                 ip->i_nlink--;
745                 ip->i_flag |= IN_CHANGE;
746         }
747 out:
748         return (error);
749 }
750
751 /*
752  * Rename system call.
753  *   See comments in sys/ufs/ufs/ufs_vnops.c
754  */
755 static int
756 ext2_rename(ap)
757         struct vop_rename_args  /* {
758                 struct vnode *a_fdvp;
759                 struct vnode *a_fvp;
760                 struct componentname *a_fcnp;
761                 struct vnode *a_tdvp;
762                 struct vnode *a_tvp;
763                 struct componentname *a_tcnp;
764         } */ *ap;
765 {
766         struct vnode *tvp = ap->a_tvp;
767         struct vnode *tdvp = ap->a_tdvp;
768         struct vnode *fvp = ap->a_fvp;
769         struct vnode *fdvp = ap->a_fdvp;
770         struct componentname *tcnp = ap->a_tcnp;
771         struct componentname *fcnp = ap->a_fcnp;
772         struct thread *td = fcnp->cn_thread;
773         struct inode *ip, *xp, *dp;
774         struct dirtemplate dirbuf;
775         int doingdirectory = 0, oldparent = 0, newparent = 0;
776         int error = 0;
777         u_char namlen;
778
779 #ifdef DIAGNOSTIC
780         if ((tcnp->cn_flags & HASBUF) == 0 ||
781             (fcnp->cn_flags & HASBUF) == 0)
782                 panic("ext2_rename: no name");
783 #endif
784         /*
785          * Check for cross-device rename.
786          */
787         if ((fvp->v_mount != tdvp->v_mount) ||
788             (tvp && (fvp->v_mount != tvp->v_mount))) {
789                 error = EXDEV;
790 abortit:
791                 if (tdvp == tvp)
792                         vrele(tdvp);
793                 else
794                         vput(tdvp);
795                 if (tvp)
796                         vput(tvp);
797                 vrele(fdvp);
798                 vrele(fvp);
799                 return (error);
800         }
801
802         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
803             (VTOI(tdvp)->i_flags & APPEND))) {
804                 error = EPERM;
805                 goto abortit;
806         }
807
808         /*
809          * Renaming a file to itself has no effect.  The upper layers should
810          * not call us in that case.  Temporarily just warn if they do.
811          */
812         if (fvp == tvp) {
813                 printf("ext2_rename: fvp == tvp (can't happen)\n");
814                 error = 0;
815                 goto abortit;
816         }
817
818         if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
819                 goto abortit;
820         dp = VTOI(fdvp);
821         ip = VTOI(fvp);
822         if (ip->i_nlink >= LINK_MAX) {
823                 VOP_UNLOCK(fvp, 0, td);
824                 error = EMLINK;
825                 goto abortit;
826         }
827         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
828             || (dp->i_flags & APPEND)) {
829                 VOP_UNLOCK(fvp, 0, td);
830                 error = EPERM;
831                 goto abortit;
832         }
833         if ((ip->i_mode & IFMT) == IFDIR) {
834                 /*
835                  * Avoid ".", "..", and aliases of "." for obvious reasons.
836                  */
837                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
838                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
839                     (ip->i_flag & IN_RENAME)) {
840                         VOP_UNLOCK(fvp, 0, td);
841                         error = EINVAL;
842                         goto abortit;
843                 }
844                 ip->i_flag |= IN_RENAME;
845                 oldparent = dp->i_number;
846                 doingdirectory++;
847         }
848         vrele(fdvp);
849
850         /*
851          * When the target exists, both the directory
852          * and target vnodes are returned locked.
853          */
854         dp = VTOI(tdvp);
855         xp = NULL;
856         if (tvp)
857                 xp = VTOI(tvp);
858
859         /*
860          * 1) Bump link count while we're moving stuff
861          *    around.  If we crash somewhere before
862          *    completing our work, the link count
863          *    may be wrong, but correctable.
864          */
865         ip->i_nlink++;
866         ip->i_flag |= IN_CHANGE;
867         if ((error = ext2_update(fvp, 1)) != 0) {
868                 VOP_UNLOCK(fvp, 0, td);
869                 goto bad;
870         }
871
872         /*
873          * If ".." must be changed (ie the directory gets a new
874          * parent) then the source directory must not be in the
875          * directory heirarchy above the target, as this would
876          * orphan everything below the source directory. Also
877          * the user must have write permission in the source so
878          * as to be able to change "..". We must repeat the call
879          * to namei, as the parent directory is unlocked by the
880          * call to checkpath().
881          */
882         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
883         VOP_UNLOCK(fvp, 0, td);
884         if (oldparent != dp->i_number)
885                 newparent = dp->i_number;
886         if (doingdirectory && newparent) {
887                 if (error)      /* write access check above */
888                         goto bad;
889                 if (xp != NULL)
890                         vput(tvp);
891                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
892                 if (error)
893                         goto out;
894                 VREF(tdvp);
895                 error = relookup(tdvp, &tvp, tcnp);
896                 if (error)
897                         goto out;
898                 vrele(tdvp);
899                 dp = VTOI(tdvp);
900                 xp = NULL;
901                 if (tvp)
902                         xp = VTOI(tvp);
903         }
904         /*
905          * 2) If target doesn't exist, link the target
906          *    to the source and unlink the source.
907          *    Otherwise, rewrite the target directory
908          *    entry to reference the source inode and
909          *    expunge the original entry's existence.
910          */
911         if (xp == NULL) {
912                 if (dp->i_devvp != ip->i_devvp)
913                         panic("ext2_rename: EXDEV");
914                 /*
915                  * Account for ".." in new directory.
916                  * When source and destination have the same
917                  * parent we don't fool with the link count.
918                  */
919                 if (doingdirectory && newparent) {
920                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
921                                 error = EMLINK;
922                                 goto bad;
923                         }
924                         dp->i_nlink++;
925                         dp->i_flag |= IN_CHANGE;
926                         error = ext2_update(tdvp, 1);
927                         if (error)
928                                 goto bad;
929                 }
930                 error = ext2_direnter(ip, tdvp, tcnp);
931                 if (error) {
932                         if (doingdirectory && newparent) {
933                                 dp->i_nlink--;
934                                 dp->i_flag |= IN_CHANGE;
935                                 (void)ext2_update(tdvp, 1);
936                         }
937                         goto bad;
938                 }
939                 vput(tdvp);
940         } else {
941                 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
942                        panic("ext2_rename: EXDEV");
943                 /*
944                  * Short circuit rename(foo, foo).
945                  */
946                 if (xp->i_number == ip->i_number)
947                         panic("ext2_rename: same file");
948                 /*
949                  * If the parent directory is "sticky", then the user must
950                  * own the parent directory, or the destination of the rename,
951                  * otherwise the destination may not be changed (except by
952                  * root). This implements append-only directories.
953                  */
954                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
955                     tcnp->cn_cred->cr_uid != dp->i_uid &&
956                     xp->i_uid != tcnp->cn_cred->cr_uid) {
957                         error = EPERM;
958                         goto bad;
959                 }
960                 /*
961                  * Target must be empty if a directory and have no links
962                  * to it. Also, ensure source and target are compatible
963                  * (both directories, or both not directories).
964                  */
965                 if ((xp->i_mode&IFMT) == IFDIR) {
966                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
967                             xp->i_nlink > 2) {
968                                 error = ENOTEMPTY;
969                                 goto bad;
970                         }
971                         if (!doingdirectory) {
972                                 error = ENOTDIR;
973                                 goto bad;
974                         }
975                         cache_purge(tdvp);
976                 } else if (doingdirectory) {
977                         error = EISDIR;
978                         goto bad;
979                 }
980                 error = ext2_dirrewrite(dp, ip, tcnp);
981                 if (error)
982                         goto bad;
983                 /*
984                  * If the target directory is in the same
985                  * directory as the source directory,
986                  * decrement the link count on the parent
987                  * of the target directory.
988                  */
989                 if (doingdirectory && !newparent) {
990                        dp->i_nlink--;
991                        dp->i_flag |= IN_CHANGE;
992                 }
993                 vput(tdvp);
994                 /*
995                  * Adjust the link count of the target to
996                  * reflect the dirrewrite above.  If this is
997                  * a directory it is empty and there are
998                  * no links to it, so we can squash the inode and
999                  * any space associated with it.  We disallowed
1000                  * renaming over top of a directory with links to
1001                  * it above, as the remaining link would point to
1002                  * a directory without "." or ".." entries.
1003                  */
1004                 xp->i_nlink--;
1005                 if (doingdirectory) {
1006                         if (--xp->i_nlink != 0)
1007                                 panic("ext2_rename: linked directory");
1008                         error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1009                             tcnp->cn_cred, tcnp->cn_thread);
1010                 }
1011                 xp->i_flag |= IN_CHANGE;
1012                 vput(tvp);
1013                 xp = NULL;
1014         }
1015
1016         /*
1017          * 3) Unlink the source.
1018          */
1019         fcnp->cn_flags &= ~MODMASK;
1020         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1021         VREF(fdvp);
1022         error = relookup(fdvp, &fvp, fcnp);
1023         if (error == 0)
1024                 vrele(fdvp);
1025         if (fvp != NULL) {
1026                 xp = VTOI(fvp);
1027                 dp = VTOI(fdvp);
1028         } else {
1029                 /*
1030                  * From name has disappeared.
1031                  */
1032                 if (doingdirectory)
1033                         panic("ext2_rename: lost dir entry");
1034                 vrele(ap->a_fvp);
1035                 return (0);
1036         }
1037         /*
1038          * Ensure that the directory entry still exists and has not
1039          * changed while the new name has been entered. If the source is
1040          * a file then the entry may have been unlinked or renamed. In
1041          * either case there is no further work to be done. If the source
1042          * is a directory then it cannot have been rmdir'ed; its link
1043          * count of three would cause a rmdir to fail with ENOTEMPTY.
1044          * The IN_RENAME flag ensures that it cannot be moved by another
1045          * rename.
1046          */
1047         if (xp != ip) {
1048                 if (doingdirectory)
1049                         panic("ext2_rename: lost dir entry");
1050         } else {
1051                 /*
1052                  * If the source is a directory with a
1053                  * new parent, the link count of the old
1054                  * parent directory must be decremented
1055                  * and ".." set to point to the new parent.
1056                  */
1057                 if (doingdirectory && newparent) {
1058                         dp->i_nlink--;
1059                         dp->i_flag |= IN_CHANGE;
1060                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
1061                                 sizeof (struct dirtemplate), (off_t)0,
1062                                 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1063                                 tcnp->cn_cred, NOCRED, (int *)0,
1064                                 (struct thread *)0);
1065                         if (error == 0) {
1066                                 /* Like ufs little-endian: */
1067                                 namlen = dirbuf.dotdot_type;
1068                                 if (namlen != 2 ||
1069                                     dirbuf.dotdot_name[0] != '.' ||
1070                                     dirbuf.dotdot_name[1] != '.') {
1071                                         ext2_dirbad(xp, (doff_t)12,
1072                                             "rename: mangled dir");
1073                                 } else {
1074                                         dirbuf.dotdot_ino = newparent;
1075                                         (void) vn_rdwr(UIO_WRITE, fvp,
1076                                             (caddr_t)&dirbuf,
1077                                             sizeof (struct dirtemplate),
1078                                             (off_t)0, UIO_SYSSPACE,
1079                                             IO_NODELOCKED | IO_SYNC |
1080                                             IO_NOMACCHECK, tcnp->cn_cred,
1081                                             NOCRED, (int *)0,
1082                                             (struct thread *)0);
1083                                         cache_purge(fdvp);
1084                                 }
1085                         }
1086                 }
1087                 error = ext2_dirremove(fdvp, fcnp);
1088                 if (!error) {
1089                         xp->i_nlink--;
1090                         xp->i_flag |= IN_CHANGE;
1091                 }
1092                 xp->i_flag &= ~IN_RENAME;
1093         }
1094         if (dp)
1095                 vput(fdvp);
1096         if (xp)
1097                 vput(fvp);
1098         vrele(ap->a_fvp);
1099         return (error);
1100
1101 bad:
1102         if (xp)
1103                 vput(ITOV(xp));
1104         vput(ITOV(dp));
1105 out:
1106         if (doingdirectory)
1107                 ip->i_flag &= ~IN_RENAME;
1108         if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
1109                 ip->i_nlink--;
1110                 ip->i_flag |= IN_CHANGE;
1111                 ip->i_flag &= ~IN_RENAME;
1112                 vput(fvp);
1113         } else
1114                 vrele(fvp);
1115         return (error);
1116 }
1117
1118 /*
1119  * Mkdir system call
1120  */
1121 static int
1122 ext2_mkdir(ap)
1123         struct vop_mkdir_args /* {
1124                 struct vnode *a_dvp;
1125                 struct vnode **a_vpp;
1126                 struct componentname *a_cnp;
1127                 struct vattr *a_vap;
1128         } */ *ap;
1129 {
1130         struct vnode *dvp = ap->a_dvp;
1131         struct vattr *vap = ap->a_vap;
1132         struct componentname *cnp = ap->a_cnp;
1133         struct inode *ip, *dp;
1134         struct vnode *tvp;
1135         struct dirtemplate dirtemplate, *dtp;
1136         int error, dmode;
1137
1138 #ifdef DIAGNOSTIC
1139         if ((cnp->cn_flags & HASBUF) == 0)
1140                 panic("ext2_mkdir: no name");
1141 #endif
1142         dp = VTOI(dvp);
1143         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
1144                 error = EMLINK;
1145                 goto out;
1146         }
1147         dmode = vap->va_mode & 0777;
1148         dmode |= IFDIR;
1149         /*
1150          * Must simulate part of ext2_makeinode here to acquire the inode,
1151          * but not have it entered in the parent directory. The entry is
1152          * made later after writing "." and ".." entries.
1153          */
1154         error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1155         if (error)
1156                 goto out;
1157         ip = VTOI(tvp);
1158         ip->i_gid = dp->i_gid;
1159 #ifdef SUIDDIR
1160         {
1161                 /*
1162                  * if we are hacking owners here, (only do this where told to)
1163                  * and we are not giving it TOO root, (would subvert quotas)
1164                  * then go ahead and give it to the other user.
1165                  * The new directory also inherits the SUID bit. 
1166                  * If user's UID and dir UID are the same,
1167                  * 'give it away' so that the SUID is still forced on.
1168                  */
1169                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1170                    (dp->i_mode & ISUID) && dp->i_uid) {
1171                         dmode |= ISUID;
1172                         ip->i_uid = dp->i_uid;
1173                 } else {
1174                         ip->i_uid = cnp->cn_cred->cr_uid;
1175                 }
1176         }
1177 #else
1178         ip->i_uid = cnp->cn_cred->cr_uid;
1179 #endif
1180         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1181         ip->i_mode = dmode;
1182         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
1183         ip->i_nlink = 2;
1184         if (cnp->cn_flags & ISWHITEOUT)
1185                 ip->i_flags |= UF_OPAQUE;
1186         error = ext2_update(tvp, 1);
1187
1188         /*
1189          * Bump link count in parent directory
1190          * to reflect work done below.  Should
1191          * be done before reference is created
1192          * so reparation is possible if we crash.
1193          */
1194         dp->i_nlink++;
1195         dp->i_flag |= IN_CHANGE;
1196         error = ext2_update(dvp, 1);
1197         if (error)
1198                 goto bad;
1199
1200         /* Initialize directory with "." and ".." from static template. */
1201         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
1202             EXT2_FEATURE_INCOMPAT_FILETYPE))
1203                 dtp = &mastertemplate;
1204         else
1205                 dtp = &omastertemplate;
1206         dirtemplate = *dtp;
1207         dirtemplate.dot_ino = ip->i_number;
1208         dirtemplate.dotdot_ino = dp->i_number;
1209         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
1210          * so let's just redefine it - for this function only
1211          */
1212 #undef  DIRBLKSIZ 
1213 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->s_blocksize
1214         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1215         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1216             sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1217             IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1218             (int *)0, (struct thread *)0);
1219         if (error) {
1220                 dp->i_nlink--;
1221                 dp->i_flag |= IN_CHANGE;
1222                 goto bad;
1223         }
1224         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1225                 /* XXX should grow with balloc() */
1226                 panic("ext2_mkdir: blksize");
1227         else {
1228                 ip->i_size = DIRBLKSIZ;
1229                 ip->i_flag |= IN_CHANGE;
1230         }
1231
1232         /* Directory set up, now install its entry in the parent directory. */
1233         error = ext2_direnter(ip, dvp, cnp);
1234         if (error) {
1235                 dp->i_nlink--;
1236                 dp->i_flag |= IN_CHANGE;
1237         }
1238 bad:
1239         /*
1240          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1241          * for us because we set the link count to 0.
1242          */
1243         if (error) {
1244                 ip->i_nlink = 0;
1245                 ip->i_flag |= IN_CHANGE;
1246                 vput(tvp);
1247         } else
1248                 *ap->a_vpp = tvp;
1249 out:
1250         return (error);
1251 #undef  DIRBLKSIZ
1252 #define DIRBLKSIZ  DEV_BSIZE
1253 }
1254
1255 /*
1256  * Rmdir system call.
1257  */
1258 static int
1259 ext2_rmdir(ap)
1260         struct vop_rmdir_args /* {
1261                 struct vnode *a_dvp;
1262                 struct vnode *a_vp;
1263                 struct componentname *a_cnp;
1264         } */ *ap;
1265 {
1266         struct vnode *vp = ap->a_vp;
1267         struct vnode *dvp = ap->a_dvp;
1268         struct componentname *cnp = ap->a_cnp;
1269         struct thread *td = cnp->cn_thread;
1270         struct inode *ip, *dp;
1271         int error;
1272
1273         ip = VTOI(vp);
1274         dp = VTOI(dvp);
1275
1276         /*
1277          * Verify the directory is empty (and valid).
1278          * (Rmdir ".." won't be valid since
1279          *  ".." will contain a reference to
1280          *  the current directory and thus be
1281          *  non-empty.)
1282          */
1283         error = 0;
1284         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1285                 error = ENOTEMPTY;
1286                 goto out;
1287         }
1288         if ((dp->i_flags & APPEND)
1289             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1290                 error = EPERM;
1291                 goto out;
1292         }
1293         /*
1294          * Delete reference to directory before purging
1295          * inode.  If we crash in between, the directory
1296          * will be reattached to lost+found,
1297          */
1298         error = ext2_dirremove(dvp, cnp);
1299         if (error)
1300                 goto out;
1301         dp->i_nlink--;
1302         dp->i_flag |= IN_CHANGE;
1303         cache_purge(dvp);
1304         VOP_UNLOCK(dvp, 0, td);
1305         /*
1306          * Truncate inode.  The only stuff left
1307          * in the directory is "." and "..".  The
1308          * "." reference is inconsequential since
1309          * we're quashing it.  The ".." reference
1310          * has already been adjusted above.  We've
1311          * removed the "." reference and the reference
1312          * in the parent directory, but there may be
1313          * other hard links so decrement by 2 and
1314          * worry about them later.
1315          */
1316         ip->i_nlink -= 2;
1317         error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred, td);
1318         cache_purge(ITOV(ip));
1319         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1320 out:
1321         return (error);
1322 }
1323
1324 /*
1325  * symlink -- make a symbolic link
1326  */
1327 static int
1328 ext2_symlink(ap)
1329         struct vop_symlink_args /* {
1330                 struct vnode *a_dvp;
1331                 struct vnode **a_vpp;
1332                 struct componentname *a_cnp;
1333                 struct vattr *a_vap;
1334                 char *a_target;
1335         } */ *ap;
1336 {
1337         struct vnode *vp, **vpp = ap->a_vpp;
1338         struct inode *ip;
1339         int len, error;
1340
1341         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1342             vpp, ap->a_cnp);
1343         if (error)
1344                 return (error);
1345         vp = *vpp;
1346         len = strlen(ap->a_target);
1347         if (len < vp->v_mount->mnt_maxsymlinklen) {
1348                 ip = VTOI(vp);
1349                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1350                 ip->i_size = len;
1351                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1352         } else
1353                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1354                     UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1355                     ap->a_cnp->cn_cred, NOCRED, (int *)0, (struct thread *)0);
1356         if (error)
1357                 vput(vp);
1358         return (error);
1359 }
1360
1361 /*
1362  * Return target name of a symbolic link
1363  */
1364 static int
1365 ext2_readlink(ap)
1366         struct vop_readlink_args /* {
1367                 struct vnode *a_vp;
1368                 struct uio *a_uio;
1369                 struct ucred *a_cred;
1370         } */ *ap;
1371 {
1372         struct vnode *vp = ap->a_vp;
1373         struct inode *ip = VTOI(vp);
1374         int isize;
1375
1376         isize = ip->i_size;
1377         if (isize < vp->v_mount->mnt_maxsymlinklen) {
1378                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1379                 return (0);
1380         }
1381         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1382 }
1383
1384 /*
1385  * Calculate the logical to physical mapping if not done already,
1386  * then call the device strategy routine.
1387  *
1388  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1389  * deadlock on memory.  See ext2_bmap() for details.
1390  */
1391 static int
1392 ext2_strategy(ap)
1393         struct vop_strategy_args /* {
1394                 struct vnode *a_vp;
1395                 struct buf *a_bp;
1396         } */ *ap;
1397 {
1398         struct buf *bp = ap->a_bp;
1399         struct vnode *vp = ap->a_vp;
1400         struct inode *ip;
1401         struct bufobj *bo;
1402         int32_t blkno;
1403         int error;
1404
1405         ip = VTOI(vp);
1406         if (vp->v_type == VBLK || vp->v_type == VCHR)
1407                 panic("ext2_strategy: spec");
1408         if (bp->b_blkno == bp->b_lblkno) {
1409                 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1410                 bp->b_blkno = blkno;
1411                 if (error) {
1412                         bp->b_error = error;
1413                         bp->b_ioflags |= BIO_ERROR;
1414                         bufdone(bp);
1415                         return (error);
1416                 }
1417                 if ((long)bp->b_blkno == -1)
1418                         vfs_bio_clrbuf(bp);
1419         }
1420         if ((long)bp->b_blkno == -1) {
1421                 bufdone(bp);
1422                 return (0);
1423         }
1424         bp->b_iooffset = dbtob(bp->b_blkno);
1425         bo = VFSTOEXT2(vp->v_mount)->um_bo;
1426         BO_STRATEGY(bo, bp);
1427         return (0);
1428 }
1429
1430 /*
1431  * Print out the contents of an inode.
1432  */
1433 static int
1434 ext2_print(ap)
1435         struct vop_print_args /* {
1436                 struct vnode *a_vp;
1437         } */ *ap;
1438 {
1439         struct vnode *vp = ap->a_vp;
1440         struct inode *ip = VTOI(vp);
1441
1442         vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
1443         if (vp->v_type == VFIFO)
1444                 fifo_printinfo(vp);
1445         printf("\n");
1446         return (0);
1447 }
1448
1449 /*
1450  * Close wrapper for fifos.
1451  *
1452  * Update the times on the inode then do device close.
1453  */
1454 static int
1455 ext2fifo_close(ap)
1456         struct vop_close_args /* {
1457                 struct vnode *a_vp;
1458                 int  a_fflag;
1459                 struct ucred *a_cred;
1460                 struct thread *a_td;
1461         } */ *ap;
1462 {
1463         struct vnode *vp = ap->a_vp;
1464
1465         VI_LOCK(vp);
1466         if (vp->v_usecount > 1)
1467                 ext2_itimes(vp);
1468         VI_UNLOCK(vp);
1469         return (fifo_specops.vop_close(ap));
1470 }
1471
1472 /*
1473  * Kqfilter wrapper for fifos.
1474  *
1475  * Fall through to ext2 kqfilter routines if needed 
1476  */
1477 static int
1478 ext2fifo_kqfilter(ap)
1479         struct vop_kqfilter_args *ap;
1480 {
1481         int error;
1482
1483         error = fifo_specops.vop_kqfilter(ap);
1484         if (error)
1485                 error = ext2_kqfilter(ap);
1486         return (error);
1487 }
1488
1489 /*
1490  * Return POSIX pathconf information applicable to ext2 filesystems.
1491  */
1492 static int
1493 ext2_pathconf(ap)
1494         struct vop_pathconf_args /* {
1495                 struct vnode *a_vp;
1496                 int a_name;
1497                 int *a_retval;
1498         } */ *ap;
1499 {
1500
1501         switch (ap->a_name) {
1502         case _PC_LINK_MAX:
1503                 *ap->a_retval = LINK_MAX;
1504                 return (0);
1505         case _PC_NAME_MAX:
1506                 *ap->a_retval = NAME_MAX;
1507                 return (0);
1508         case _PC_PATH_MAX:
1509                 *ap->a_retval = PATH_MAX;
1510                 return (0);
1511         case _PC_PIPE_BUF:
1512                 *ap->a_retval = PIPE_BUF;
1513                 return (0);
1514         case _PC_CHOWN_RESTRICTED:
1515                 *ap->a_retval = 1;
1516                 return (0);
1517         case _PC_NO_TRUNC:
1518                 *ap->a_retval = 1;
1519                 return (0);
1520         default:
1521                 return (EINVAL);
1522         }
1523         /* NOTREACHED */
1524 }
1525
1526 /*
1527  * Advisory record locking support
1528  */
1529 static int
1530 ext2_advlock(ap)
1531         struct vop_advlock_args /* {
1532                 struct vnode *a_vp;
1533                 caddr_t  a_id;
1534                 int  a_op;
1535                 struct flock *a_fl;
1536                 int  a_flags;
1537         } */ *ap;
1538 {
1539         struct inode *ip = VTOI(ap->a_vp);
1540
1541         return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1542 }
1543
1544 /*
1545  * Initialize the vnode associated with a new inode, handle aliased
1546  * vnodes.
1547  */
1548 int
1549 ext2_vinit(mntp, fifoops, vpp)
1550         struct mount *mntp;
1551         struct vop_vector *fifoops;
1552         struct vnode **vpp;
1553 {
1554         struct inode *ip;
1555         struct vnode *vp;
1556
1557         vp = *vpp;
1558         ip = VTOI(vp);
1559         vp->v_type = IFTOVT(ip->i_mode);
1560         if (vp->v_type == VFIFO)
1561                 vp->v_op = fifoops;
1562
1563         if (ip->i_number == ROOTINO)
1564                 vp->v_vflag |= VV_ROOT;
1565         ip->i_modrev = init_va_filerev();
1566         *vpp = vp;
1567         return (0);
1568 }
1569
1570 /*
1571  * Allocate a new inode.
1572  */
1573 static int
1574 ext2_makeinode(mode, dvp, vpp, cnp)
1575         int mode;
1576         struct vnode *dvp;
1577         struct vnode **vpp;
1578         struct componentname *cnp;
1579 {
1580         struct inode *ip, *pdir;
1581         struct vnode *tvp;
1582         int error;
1583
1584         pdir = VTOI(dvp);
1585 #ifdef DIAGNOSTIC
1586         if ((cnp->cn_flags & HASBUF) == 0)
1587                 panic("ext2_makeinode: no name");
1588 #endif
1589         *vpp = NULL;
1590         if ((mode & IFMT) == 0)
1591                 mode |= IFREG;
1592
1593         error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1594         if (error) {
1595                 return (error);
1596         }
1597         ip = VTOI(tvp);
1598         ip->i_gid = pdir->i_gid;
1599 #ifdef SUIDDIR
1600         {
1601                 /*
1602                  * if we are
1603                  * not the owner of the directory,
1604                  * and we are hacking owners here, (only do this where told to)
1605                  * and we are not giving it TOO root, (would subvert quotas)
1606                  * then go ahead and give it to the other user.
1607                  * Note that this drops off the execute bits for security.
1608                  */
1609                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1610                      (pdir->i_mode & ISUID) &&
1611                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1612                         ip->i_uid = pdir->i_uid;
1613                         mode &= ~07111;
1614                 } else {
1615                         ip->i_uid = cnp->cn_cred->cr_uid;
1616                 }
1617         }
1618 #else
1619         ip->i_uid = cnp->cn_cred->cr_uid;
1620 #endif
1621         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1622         ip->i_mode = mode;
1623         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
1624         ip->i_nlink = 1;
1625         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1626                 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_CLEARSUGID,
1627                     SUSER_ALLOWJAIL))
1628                         ip->i_mode &= ~ISGID;
1629         }
1630
1631         if (cnp->cn_flags & ISWHITEOUT)
1632                 ip->i_flags |= UF_OPAQUE;
1633
1634         /*
1635          * Make sure inode goes to disk before directory entry.
1636          */
1637         error = ext2_update(tvp, 1);
1638         if (error)
1639                 goto bad;
1640         error = ext2_direnter(ip, dvp, cnp);
1641         if (error)
1642                 goto bad;
1643
1644         *vpp = tvp;
1645         return (0);
1646
1647 bad:
1648         /*
1649          * Write error occurred trying to update the inode
1650          * or the directory so must deallocate the inode.
1651          */
1652         ip->i_nlink = 0;
1653         ip->i_flag |= IN_CHANGE;
1654         vput(tvp);
1655         return (error);
1656 }
1657
1658 static struct filterops ext2read_filtops = 
1659         { 1, NULL, filt_ext2detach, filt_ext2read };
1660 static struct filterops ext2write_filtops = 
1661         { 1, NULL, filt_ext2detach, filt_ext2write };
1662 static struct filterops ext2vnode_filtops = 
1663         { 1, NULL, filt_ext2detach, filt_ext2vnode };
1664
1665 static int
1666 ext2_kqfilter(ap)
1667         struct vop_kqfilter_args /* {
1668                 struct vnode *a_vp;
1669                 struct knote *a_kn;
1670         } */ *ap;
1671 {
1672         struct vnode *vp = ap->a_vp;
1673         struct knote *kn = ap->a_kn;
1674
1675         switch (kn->kn_filter) {
1676         case EVFILT_READ:
1677                 kn->kn_fop = &ext2read_filtops;
1678                 break;
1679         case EVFILT_WRITE:
1680                 kn->kn_fop = &ext2write_filtops;
1681                 break;
1682         case EVFILT_VNODE:
1683                 kn->kn_fop = &ext2vnode_filtops;
1684                 break;
1685         default:
1686                 return (1);
1687         }
1688
1689         kn->kn_hook = (caddr_t)vp;
1690
1691         if (vp->v_pollinfo == NULL)
1692                 v_addpollinfo(vp);
1693         if (vp->v_pollinfo == NULL)
1694                 return ENOMEM;
1695         knlist_add(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1696
1697         return (0);
1698 }
1699
1700 static void
1701 filt_ext2detach(struct knote *kn)
1702 {
1703         struct vnode *vp = (struct vnode *)kn->kn_hook;
1704
1705         KASSERT(vp->v_pollinfo != NULL, ("Mising v_pollinfo"));
1706         knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
1707 }
1708
1709 /*ARGSUSED*/
1710 static int
1711 filt_ext2read(struct knote *kn, long hint)
1712 {
1713         struct vnode *vp = (struct vnode *)kn->kn_hook;
1714         struct inode *ip = VTOI(vp);
1715
1716         /*
1717          * filesystem is gone, so set the EOF flag and schedule 
1718          * the knote for deletion.
1719          */
1720         if (hint == NOTE_REVOKE) {
1721                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1722                 return (1);
1723         }
1724
1725         kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
1726         return (kn->kn_data != 0);
1727 }
1728
1729 /*ARGSUSED*/
1730 static int
1731 filt_ext2write(struct knote *kn, long hint)
1732 {
1733
1734         /*
1735          * filesystem is gone, so set the EOF flag and schedule 
1736          * the knote for deletion.
1737          */
1738         if (hint == NOTE_REVOKE)
1739                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1740
1741         kn->kn_data = 0;
1742         return (1);
1743 }
1744
1745 static int
1746 filt_ext2vnode(struct knote *kn, long hint)
1747 {
1748
1749         if (kn->kn_sfflags & hint)
1750                 kn->kn_fflags |= hint;
1751         if (hint == NOTE_REVOKE) {
1752                 kn->kn_flags |= EV_EOF;
1753                 return (1);
1754         }
1755         return (kn->kn_fflags != 0);
1756 }