ext2fs_vnops.c revision 1.26 1 /* $NetBSD: ext2fs_vnops.c,v 1.26 2000/05/13 23:43:13 perseant Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer.
5 * Copyright (c) 1982, 1986, 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
42 * Modified for ext2fs by Manuel Bouyer.
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/resourcevar.h>
48 #include <sys/kernel.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/buf.h>
52 #include <sys/proc.h>
53 #include <sys/conf.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/vnode.h>
57 #include <sys/lockf.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/signalvar.h>
61
62 #include <vm/vm.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <miscfs/fifofs/fifo.h>
67 #include <miscfs/genfs/genfs.h>
68 #include <miscfs/specfs/specdev.h>
69
70 #include <ufs/ufs/quota.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ufs/ufsmount.h>
74
75 #include <ufs/ext2fs/ext2fs.h>
76 #include <ufs/ext2fs/ext2fs_extern.h>
77 #include <ufs/ext2fs/ext2fs_dir.h>
78
79
80 static int ext2fs_chmod
81 __P((struct vnode *, int, struct ucred *, struct proc *));
82 static int ext2fs_chown
83 __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *));
84
85 union _qcvt {
86 int64_t qcvt;
87 int32_t val[2];
88 };
89 #define SETHIGH(q, h) { \
90 union _qcvt tmp; \
91 tmp.qcvt = (q); \
92 tmp.val[_QUAD_HIGHWORD] = (h); \
93 (q) = tmp.qcvt; \
94 }
95 #define SETLOW(q, l) { \
96 union _qcvt tmp; \
97 tmp.qcvt = (q); \
98 tmp.val[_QUAD_LOWWORD] = (l); \
99 (q) = tmp.qcvt; \
100 }
101
102 /*
103 * Create a regular file
104 */
105 int
106 ext2fs_create(v)
107 void *v;
108 {
109 struct vop_create_args /* {
110 struct vnode *a_dvp;
111 struct vnode **a_vpp;
112 struct componentname *a_cnp;
113 struct vattr *a_vap;
114 } */ *ap = v;
115 return ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type,
116 ap->a_vap->va_mode),
117 ap->a_dvp, ap->a_vpp, ap->a_cnp);
118 }
119
120 /*
121 * Mknod vnode call
122 */
123 /* ARGSUSED */
124 int
125 ext2fs_mknod(v)
126 void *v;
127 {
128 struct vop_mknod_args /* {
129 struct vnode *a_dvp;
130 struct vnode **a_vpp;
131 struct componentname *a_cnp;
132 struct vattr *a_vap;
133 } */ *ap = v;
134 struct vattr *vap = ap->a_vap;
135 struct vnode **vpp = ap->a_vpp;
136 struct inode *ip;
137 int error;
138
139 if ((error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
140 ap->a_dvp, vpp, ap->a_cnp)) != 0)
141 return (error);
142 ip = VTOI(*vpp);
143 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
144 if (vap->va_rdev != VNOVAL) {
145 /*
146 * Want to be able to use this to make badblock
147 * inodes, so don't truncate the dev number.
148 */
149 ip->i_din.e2fs_din.e2di_rdev = h2fs32(vap->va_rdev);
150 }
151 /*
152 * Remove inode so that it will be reloaded by VFS_VGET and
153 * checked to see if it is an alias of an existing entry in
154 * the inode cache.
155 */
156 vput(*vpp);
157 (*vpp)->v_type = VNON;
158 vgone(*vpp);
159 *vpp = 0;
160 return (0);
161 }
162
163 /*
164 * Open called.
165 *
166 * Just check the APPEND flag.
167 */
168 /* ARGSUSED */
169 int
170 ext2fs_open(v)
171 void *v;
172 {
173 struct vop_open_args /* {
174 struct vnode *a_vp;
175 int a_mode;
176 struct ucred *a_cred;
177 struct proc *a_p;
178 } */ *ap = v;
179
180 /*
181 * Files marked append-only must be opened for appending.
182 */
183 if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
184 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
185 return (EPERM);
186 return (0);
187 }
188
189 int
190 ext2fs_access(v)
191 void *v;
192 {
193 struct vop_access_args /* {
194 struct vnode *a_vp;
195 int a_mode;
196 struct ucred *a_cred;
197 struct proc *a_p;
198 } */ *ap = v;
199 struct vnode *vp = ap->a_vp;
200 struct inode *ip = VTOI(vp);
201 mode_t mode = ap->a_mode;
202
203
204 /*
205 * Disallow write attempts on read-only file systems;
206 * unless the file is a socket, fifo, or a block or
207 * character device resident on the file system.
208 */
209 if (mode & VWRITE) {
210 switch (vp->v_type) {
211 case VDIR:
212 case VLNK:
213 case VREG:
214 if (vp->v_mount->mnt_flag & MNT_RDONLY)
215 return (EROFS);
216 break;
217 default:
218 break;
219 }
220 }
221
222 /* If immutable bit set, nobody gets to write it. */
223 if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
224 return (EPERM);
225
226 return (vaccess(vp->v_type, ip->i_e2fs_mode & ALLPERMS,
227 ip->i_e2fs_uid, ip->i_e2fs_gid, mode, ap->a_cred));
228 }
229
230 /* ARGSUSED */
231 int
232 ext2fs_getattr(v)
233 void *v;
234 {
235 struct vop_getattr_args /* {
236 struct vnode *a_vp;
237 struct vattr *a_vap;
238 struct ucred *a_cred;
239 struct proc *a_p;
240 } */ *ap = v;
241 struct vnode *vp = ap->a_vp;
242 struct inode *ip = VTOI(vp);
243 struct vattr *vap = ap->a_vap;
244 struct timespec ts;
245
246 TIMEVAL_TO_TIMESPEC(&time, &ts);
247 EXT2FS_ITIMES(ip, &ts, &ts, &ts);
248 /*
249 * Copy from inode table
250 */
251 vap->va_fsid = ip->i_dev;
252 vap->va_fileid = ip->i_number;
253 vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
254 vap->va_nlink = ip->i_e2fs_nlink;
255 vap->va_uid = ip->i_e2fs_uid;
256 vap->va_gid = ip->i_e2fs_gid;
257 vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din.e2di_rdev);
258 vap->va_size = ip->i_e2fs_size;
259 vap->va_atime.tv_sec = ip->i_e2fs_atime;
260 vap->va_atime.tv_nsec = 0;
261 vap->va_mtime.tv_sec = ip->i_e2fs_mtime;
262 vap->va_mtime.tv_nsec = 0;
263 vap->va_ctime.tv_sec = ip->i_e2fs_ctime;
264 vap->va_ctime.tv_nsec = 0;
265 #ifdef EXT2FS_SYSTEM_FLAGS
266 vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
267 vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
268 #else
269 vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
270 vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
271 #endif
272 vap->va_gen = ip->i_e2fs_gen;
273 /* this doesn't belong here */
274 if (vp->v_type == VBLK)
275 vap->va_blocksize = BLKDEV_IOSIZE;
276 else if (vp->v_type == VCHR)
277 vap->va_blocksize = MAXBSIZE;
278 else
279 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
280 vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock);
281 vap->va_type = vp->v_type;
282 vap->va_filerev = ip->i_modrev;
283 return (0);
284 }
285
286 /*
287 * Set attribute vnode op. called from several syscalls
288 */
289 int
290 ext2fs_setattr(v)
291 void *v;
292 {
293 struct vop_setattr_args /* {
294 struct vnode *a_vp;
295 struct vattr *a_vap;
296 struct ucred *a_cred;
297 struct proc *a_p;
298 } */ *ap = v;
299 struct vattr *vap = ap->a_vap;
300 struct vnode *vp = ap->a_vp;
301 struct inode *ip = VTOI(vp);
302 struct ucred *cred = ap->a_cred;
303 struct proc *p = ap->a_p;
304 int error;
305
306 /*
307 * Check for unsettable attributes.
308 */
309 if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
310 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
311 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
312 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
313 return (EINVAL);
314 }
315 if (vap->va_flags != VNOVAL) {
316 if (vp->v_mount->mnt_flag & MNT_RDONLY)
317 return (EROFS);
318 if (cred->cr_uid != ip->i_e2fs_uid &&
319 (error = suser(cred, &p->p_acflag)))
320 return (error);
321 #ifdef EXT2FS_SYSTEM_FLAGS
322 if (cred->cr_uid == 0) {
323 if ((ip->i_e2fs_flags &
324 (EXT2_APPEND | EXT2_IMMUTABLE)) && securelevel > 0)
325 return (EPERM);
326 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
327 ip->i_e2fs_flags |=
328 (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
329 (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
330 } else
331 return (EPERM);
332 #else
333 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
334 ip->i_e2fs_flags |=
335 (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
336 (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
337 #endif
338 ip->i_flag |= IN_CHANGE;
339 if (vap->va_flags & (IMMUTABLE | APPEND))
340 return (0);
341 }
342 if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
343 return (EPERM);
344 /*
345 * Go through the fields and update iff not VNOVAL.
346 */
347 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
348 if (vp->v_mount->mnt_flag & MNT_RDONLY)
349 return (EROFS);
350 error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
351 if (error)
352 return (error);
353 }
354 if (vap->va_size != VNOVAL) {
355 /*
356 * Disallow write attempts on read-only file systems;
357 * unless the file is a socket, fifo, or a block or
358 * character device resident on the file system.
359 */
360 switch (vp->v_type) {
361 case VDIR:
362 return (EISDIR);
363 case VLNK:
364 case VREG:
365 if (vp->v_mount->mnt_flag & MNT_RDONLY)
366 return (EROFS);
367 default:
368 break;
369 }
370 error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p);
371 if (error)
372 return (error);
373 }
374 ip = VTOI(vp);
375 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
376 if (vp->v_mount->mnt_flag & MNT_RDONLY)
377 return (EROFS);
378 if (cred->cr_uid != ip->i_e2fs_uid &&
379 (error = suser(cred, &p->p_acflag)) &&
380 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
381 (error = VOP_ACCESS(vp, VWRITE, cred, p))))
382 return (error);
383 if (vap->va_atime.tv_sec != VNOVAL)
384 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
385 ip->i_flag |= IN_ACCESS;
386 if (vap->va_mtime.tv_sec != VNOVAL)
387 ip->i_flag |= IN_CHANGE | IN_UPDATE;
388 error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime,
389 UPDATE_WAIT);
390 if (error)
391 return (error);
392 }
393 error = 0;
394 if (vap->va_mode != (mode_t)VNOVAL) {
395 if (vp->v_mount->mnt_flag & MNT_RDONLY)
396 return (EROFS);
397 error = ext2fs_chmod(vp, (int)vap->va_mode, cred, p);
398 }
399 return (error);
400 }
401
402 /*
403 * Change the mode on a file.
404 * Inode must be locked before calling.
405 */
406 static int
407 ext2fs_chmod(vp, mode, cred, p)
408 struct vnode *vp;
409 int mode;
410 struct ucred *cred;
411 struct proc *p;
412 {
413 struct inode *ip = VTOI(vp);
414 int error;
415
416 if (cred->cr_uid != ip->i_e2fs_uid &&
417 (error = suser(cred, &p->p_acflag)))
418 return (error);
419 if (cred->cr_uid) {
420 if (vp->v_type != VDIR && (mode & S_ISTXT))
421 return (EFTYPE);
422 if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
423 return (EPERM);
424 }
425 ip->i_e2fs_mode &= ~ALLPERMS;
426 ip->i_e2fs_mode |= (mode & ALLPERMS);
427 ip->i_flag |= IN_CHANGE;
428 if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0)
429 (void) uvm_vnp_uncache(vp);
430 return (0);
431 }
432
433 /*
434 * Perform chown operation on inode ip;
435 * inode must be locked prior to call.
436 */
437 static int
438 ext2fs_chown(vp, uid, gid, cred, p)
439 struct vnode *vp;
440 uid_t uid;
441 gid_t gid;
442 struct ucred *cred;
443 struct proc *p;
444 {
445 struct inode *ip = VTOI(vp);
446 uid_t ouid;
447 gid_t ogid;
448 int error = 0;
449
450 if (uid == (uid_t)VNOVAL)
451 uid = ip->i_e2fs_uid;
452 if (gid == (gid_t)VNOVAL)
453 gid = ip->i_e2fs_gid;
454 /*
455 * If we don't own the file, are trying to change the owner
456 * of the file, or are not a member of the target group,
457 * the caller must be superuser or the call fails.
458 */
459 if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
460 (gid != ip->i_e2fs_gid && !groupmember((gid_t)gid, cred))) &&
461 (error = suser(cred, &p->p_acflag)))
462 return (error);
463 ogid = ip->i_e2fs_gid;
464 ouid = ip->i_e2fs_uid;
465
466 ip->i_e2fs_gid = gid;
467 ip->i_e2fs_uid = uid;
468 if (ouid != uid || ogid != gid)
469 ip->i_flag |= IN_CHANGE;
470 if (ouid != uid && cred->cr_uid != 0)
471 ip->i_e2fs_mode &= ~ISUID;
472 if (ogid != gid && cred->cr_uid != 0)
473 ip->i_e2fs_mode &= ~ISGID;
474 return (0);
475 }
476
477 int
478 ext2fs_remove(v)
479 void *v;
480 {
481 struct vop_remove_args /* {
482 struct vnode *a_dvp;
483 struct vnode *a_vp;
484 struct componentname *a_cnp;
485 } */ *ap = v;
486 struct inode *ip;
487 struct vnode *vp = ap->a_vp;
488 struct vnode *dvp = ap->a_dvp;
489 int error;
490
491 ip = VTOI(vp);
492 if (vp->v_type == VDIR ||
493 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
494 (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
495 error = EPERM;
496 goto out;
497 }
498 error = ext2fs_dirremove(dvp, ap->a_cnp);
499 if (error == 0) {
500 ip->i_e2fs_nlink--;
501 ip->i_flag |= IN_CHANGE;
502 }
503 out:
504 if (dvp == vp)
505 vrele(vp);
506 else
507 vput(vp);
508 vput(dvp);
509 return (error);
510 }
511
512 /*
513 * link vnode call
514 */
515 int
516 ext2fs_link(v)
517 void *v;
518 {
519 struct vop_link_args /* {
520 struct vnode *a_dvp;
521 struct vnode *a_vp;
522 struct componentname *a_cnp;
523 } */ *ap = v;
524 struct vnode *dvp = ap->a_dvp;
525 struct vnode *vp = ap->a_vp;
526 struct componentname *cnp = ap->a_cnp;
527 struct inode *ip;
528 int error;
529
530 #ifdef DIAGNOSTIC
531 if ((cnp->cn_flags & HASBUF) == 0)
532 panic("ext2fs_link: no name");
533 #endif
534 if (vp->v_type == VDIR) {
535 VOP_ABORTOP(dvp, cnp);
536 error = EISDIR;
537 goto out2;
538 }
539 if (dvp->v_mount != vp->v_mount) {
540 VOP_ABORTOP(dvp, cnp);
541 error = EXDEV;
542 goto out2;
543 }
544 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
545 VOP_ABORTOP(dvp, cnp);
546 goto out2;
547 }
548 ip = VTOI(vp);
549 if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
550 VOP_ABORTOP(dvp, cnp);
551 error = EMLINK;
552 goto out1;
553 }
554 if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
555 VOP_ABORTOP(dvp, cnp);
556 error = EPERM;
557 goto out1;
558 }
559 ip->i_e2fs_nlink++;
560 ip->i_flag |= IN_CHANGE;
561 error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
562 if (!error)
563 error = ext2fs_direnter(ip, dvp, cnp);
564 if (error) {
565 ip->i_e2fs_nlink--;
566 ip->i_flag |= IN_CHANGE;
567 }
568 FREE(cnp->cn_pnbuf, M_NAMEI);
569 out1:
570 if (dvp != vp)
571 VOP_UNLOCK(vp, 0);
572 out2:
573 vput(dvp);
574 return (error);
575 }
576
577 /*
578 * Rename system call.
579 * rename("foo", "bar");
580 * is essentially
581 * unlink("bar");
582 * link("foo", "bar");
583 * unlink("foo");
584 * but ``atomically''. Can't do full commit without saving state in the
585 * inode on disk which isn't feasible at this time. Best we can do is
586 * always guarantee the target exists.
587 *
588 * Basic algorithm is:
589 *
590 * 1) Bump link count on source while we're linking it to the
591 * target. This also ensure the inode won't be deleted out
592 * from underneath us while we work (it may be truncated by
593 * a concurrent `trunc' or `open' for creation).
594 * 2) Link source to destination. If destination already exists,
595 * delete it first.
596 * 3) Unlink source reference to inode if still around. If a
597 * directory was moved and the parent of the destination
598 * is different from the source, patch the ".." entry in the
599 * directory.
600 */
601 int
602 ext2fs_rename(v)
603 void *v;
604 {
605 struct vop_rename_args /* {
606 struct vnode *a_fdvp;
607 struct vnode *a_fvp;
608 struct componentname *a_fcnp;
609 struct vnode *a_tdvp;
610 struct vnode *a_tvp;
611 struct componentname *a_tcnp;
612 } */ *ap = v;
613 struct vnode *tvp = ap->a_tvp;
614 struct vnode *tdvp = ap->a_tdvp;
615 struct vnode *fvp = ap->a_fvp;
616 struct vnode *fdvp = ap->a_fdvp;
617 struct componentname *tcnp = ap->a_tcnp;
618 struct componentname *fcnp = ap->a_fcnp;
619 struct inode *ip, *xp, *dp;
620 struct ext2fs_dirtemplate dirbuf;
621 int doingdirectory = 0, oldparent = 0, newparent = 0;
622 int error = 0;
623 u_char namlen;
624
625 #ifdef DIAGNOSTIC
626 if ((tcnp->cn_flags & HASBUF) == 0 ||
627 (fcnp->cn_flags & HASBUF) == 0)
628 panic("ext2fs_rename: no name");
629 #endif
630 /*
631 * Check for cross-device rename.
632 */
633 if ((fvp->v_mount != tdvp->v_mount) ||
634 (tvp && (fvp->v_mount != tvp->v_mount))) {
635 error = EXDEV;
636 abortit:
637 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
638 if (tdvp == tvp)
639 vrele(tdvp);
640 else
641 vput(tdvp);
642 if (tvp)
643 vput(tvp);
644 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
645 vrele(fdvp);
646 vrele(fvp);
647 return (error);
648 }
649
650 /*
651 * Check if just deleting a link name.
652 */
653 if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
654 (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
655 error = EPERM;
656 goto abortit;
657 }
658 if (fvp == tvp) {
659 if (fvp->v_type == VDIR) {
660 error = EINVAL;
661 goto abortit;
662 }
663
664 /* Release destination completely. */
665 VOP_ABORTOP(tdvp, tcnp);
666 vput(tdvp);
667 vput(tvp);
668
669 /* Delete source. */
670 vrele(fdvp);
671 vrele(fvp);
672 fcnp->cn_flags &= ~MODMASK;
673 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
674 if ((fcnp->cn_flags & SAVESTART) == 0)
675 panic("ext2fs_rename: lost from startdir");
676 fcnp->cn_nameiop = DELETE;
677 (void) relookup(fdvp, &fvp, fcnp);
678 return (VOP_REMOVE(fdvp, fvp, fcnp));
679 }
680 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
681 goto abortit;
682 dp = VTOI(fdvp);
683 ip = VTOI(fvp);
684 if ((nlink_t) ip->i_e2fs_nlink >= LINK_MAX) {
685 VOP_UNLOCK(fvp, 0);
686 error = EMLINK;
687 goto abortit;
688 }
689 if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
690 (dp->i_e2fs_flags & EXT2_APPEND)) {
691 VOP_UNLOCK(fvp, 0);
692 error = EPERM;
693 goto abortit;
694 }
695 if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
696 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
697 if (!error && tvp)
698 error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred,
699 tcnp->cn_proc);
700 if (error) {
701 VOP_UNLOCK(fvp, 0);
702 error = EACCES;
703 goto abortit;
704 }
705 /*
706 * Avoid ".", "..", and aliases of "." for obvious reasons.
707 */
708 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
709 dp == ip ||
710 (fcnp->cn_flags&ISDOTDOT) ||
711 (tcnp->cn_flags & ISDOTDOT) ||
712 (ip->i_flag & IN_RENAME)) {
713 VOP_UNLOCK(fvp, 0);
714 error = EINVAL;
715 goto abortit;
716 }
717 ip->i_flag |= IN_RENAME;
718 oldparent = dp->i_number;
719 doingdirectory++;
720 }
721 vrele(fdvp);
722
723 /*
724 * When the target exists, both the directory
725 * and target vnodes are returned locked.
726 */
727 dp = VTOI(tdvp);
728 xp = NULL;
729 if (tvp)
730 xp = VTOI(tvp);
731
732 /*
733 * 1) Bump link count while we're moving stuff
734 * around. If we crash somewhere before
735 * completing our work, the link count
736 * may be wrong, but correctable.
737 */
738 ip->i_e2fs_nlink++;
739 ip->i_flag |= IN_CHANGE;
740 if ((error = VOP_UPDATE(fvp, NULL, NULL, UPDATE_WAIT)) != 0) {
741 VOP_UNLOCK(fvp, 0);
742 goto bad;
743 }
744
745 /*
746 * If ".." must be changed (ie the directory gets a new
747 * parent) then the source directory must not be in the
748 * directory heirarchy above the target, as this would
749 * orphan everything below the source directory. Also
750 * the user must have write permission in the source so
751 * as to be able to change "..". We must repeat the call
752 * to namei, as the parent directory is unlocked by the
753 * call to checkpath().
754 */
755 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
756 VOP_UNLOCK(fvp, 0);
757 if (oldparent != dp->i_number)
758 newparent = dp->i_number;
759 if (doingdirectory && newparent) {
760 if (error) /* write access check above */
761 goto bad;
762 if (xp != NULL)
763 vput(tvp);
764 error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
765 if (error != 0)
766 goto out;
767 if ((tcnp->cn_flags & SAVESTART) == 0)
768 panic("ext2fs_rename: lost to startdir");
769 if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
770 goto out;
771 dp = VTOI(tdvp);
772 xp = NULL;
773 if (tvp)
774 xp = VTOI(tvp);
775 }
776 /*
777 * 2) If target doesn't exist, link the target
778 * to the source and unlink the source.
779 * Otherwise, rewrite the target directory
780 * entry to reference the source inode and
781 * expunge the original entry's existence.
782 */
783 if (xp == NULL) {
784 if (dp->i_dev != ip->i_dev)
785 panic("rename: EXDEV");
786 /*
787 * Account for ".." in new directory.
788 * When source and destination have the same
789 * parent we don't fool with the link count.
790 */
791 if (doingdirectory && newparent) {
792 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
793 error = EMLINK;
794 goto bad;
795 }
796 dp->i_e2fs_nlink++;
797 dp->i_flag |= IN_CHANGE;
798 if ((error = VOP_UPDATE(tdvp, NULL, NULL, UPDATE_WAIT))
799 != 0)
800 goto bad;
801 }
802 error = ext2fs_direnter(ip, tdvp, tcnp);
803 if (error != 0) {
804 if (doingdirectory && newparent) {
805 dp->i_e2fs_nlink--;
806 dp->i_flag |= IN_CHANGE;
807 (void)VOP_UPDATE(tdvp, NULL, NULL, UPDATE_WAIT);
808 }
809 goto bad;
810 }
811 vput(tdvp);
812 } else {
813 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
814 panic("rename: EXDEV");
815 /*
816 * Short circuit rename(foo, foo).
817 */
818 if (xp->i_number == ip->i_number)
819 panic("rename: same file");
820 /*
821 * If the parent directory is "sticky", then the user must
822 * own the parent directory, or the destination of the rename,
823 * otherwise the destination may not be changed (except by
824 * root). This implements append-only directories.
825 */
826 if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
827 tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
828 xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
829 error = EPERM;
830 goto bad;
831 }
832 /*
833 * Target must be empty if a directory and have no links
834 * to it. Also, ensure source and target are compatible
835 * (both directories, or both not directories).
836 */
837 if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
838 if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
839 xp->i_e2fs_nlink > 2) {
840 error = ENOTEMPTY;
841 goto bad;
842 }
843 if (!doingdirectory) {
844 error = ENOTDIR;
845 goto bad;
846 }
847 cache_purge(tdvp);
848 } else if (doingdirectory) {
849 error = EISDIR;
850 goto bad;
851 }
852 error = ext2fs_dirrewrite(dp, ip, tcnp);
853 if (error != 0)
854 goto bad;
855 /*
856 * If the target directory is in the same
857 * directory as the source directory,
858 * decrement the link count on the parent
859 * of the target directory.
860 */
861 if (doingdirectory && !newparent) {
862 dp->i_e2fs_nlink--;
863 dp->i_flag |= IN_CHANGE;
864 }
865 vput(tdvp);
866 /*
867 * Adjust the link count of the target to
868 * reflect the dirrewrite above. If this is
869 * a directory it is empty and there are
870 * no links to it, so we can squash the inode and
871 * any space associated with it. We disallowed
872 * renaming over top of a directory with links to
873 * it above, as the remaining link would point to
874 * a directory without "." or ".." entries.
875 */
876 xp->i_e2fs_nlink--;
877 if (doingdirectory) {
878 if (--xp->i_e2fs_nlink != 0)
879 panic("rename: linked directory");
880 error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
881 tcnp->cn_cred, tcnp->cn_proc);
882 }
883 xp->i_flag |= IN_CHANGE;
884 vput(tvp);
885 xp = NULL;
886 }
887
888 /*
889 * 3) Unlink the source.
890 */
891 fcnp->cn_flags &= ~MODMASK;
892 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
893 if ((fcnp->cn_flags & SAVESTART) == 0)
894 panic("ext2fs_rename: lost from startdir");
895 (void) relookup(fdvp, &fvp, fcnp);
896 if (fvp != NULL) {
897 xp = VTOI(fvp);
898 dp = VTOI(fdvp);
899 } else {
900 /*
901 * From name has disappeared.
902 */
903 if (doingdirectory)
904 panic("ext2fs_rename: lost dir entry");
905 vrele(ap->a_fvp);
906 return (0);
907 }
908 /*
909 * Ensure that the directory entry still exists and has not
910 * changed while the new name has been entered. If the source is
911 * a file then the entry may have been unlinked or renamed. In
912 * either case there is no further work to be done. If the source
913 * is a directory then it cannot have been rmdir'ed; its link
914 * count of three would cause a rmdir to fail with ENOTEMPTY.
915 * The IRENAME flag ensures that it cannot be moved by another
916 * rename.
917 */
918 if (xp != ip) {
919 if (doingdirectory)
920 panic("ext2fs_rename: lost dir entry");
921 } else {
922 /*
923 * If the source is a directory with a
924 * new parent, the link count of the old
925 * parent directory must be decremented
926 * and ".." set to point to the new parent.
927 */
928 if (doingdirectory && newparent) {
929 dp->i_e2fs_nlink--;
930 dp->i_flag |= IN_CHANGE;
931 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
932 sizeof (struct ext2fs_dirtemplate), (off_t)0,
933 UIO_SYSSPACE, IO_NODELOCKED,
934 tcnp->cn_cred, (size_t *)0, (struct proc *)0);
935 if (error == 0) {
936 namlen = dirbuf.dotdot_namlen;
937 if (namlen != 2 ||
938 dirbuf.dotdot_name[0] != '.' ||
939 dirbuf.dotdot_name[1] != '.') {
940 ufs_dirbad(xp, (doff_t)12,
941 "ext2fs_rename: mangled dir");
942 } else {
943 dirbuf.dotdot_ino = h2fs32(newparent);
944 (void) vn_rdwr(UIO_WRITE, fvp,
945 (caddr_t)&dirbuf,
946 sizeof (struct dirtemplate),
947 (off_t)0, UIO_SYSSPACE,
948 IO_NODELOCKED|IO_SYNC,
949 tcnp->cn_cred, (size_t *)0,
950 (struct proc *)0);
951 cache_purge(fdvp);
952 }
953 }
954 }
955 error = ext2fs_dirremove(fdvp, fcnp);
956 if (!error) {
957 xp->i_e2fs_nlink--;
958 xp->i_flag |= IN_CHANGE;
959 }
960 xp->i_flag &= ~IN_RENAME;
961 }
962 if (dp)
963 vput(fdvp);
964 if (xp)
965 vput(fvp);
966 vrele(ap->a_fvp);
967 return (error);
968
969 bad:
970 if (xp)
971 vput(ITOV(xp));
972 vput(ITOV(dp));
973 out:
974 if (doingdirectory)
975 ip->i_flag &= ~IN_RENAME;
976 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
977 ip->i_e2fs_nlink--;
978 ip->i_flag |= IN_CHANGE;
979 vput(fvp);
980 } else
981 vrele(fvp);
982 return (error);
983 }
984
985 /*
986 * Mkdir system call
987 */
988 int
989 ext2fs_mkdir(v)
990 void *v;
991 {
992 struct vop_mkdir_args /* {
993 struct vnode *a_dvp;
994 struct vnode **a_vpp;
995 struct componentname *a_cnp;
996 struct vattr *a_vap;
997 } */ *ap = v;
998 struct vnode *dvp = ap->a_dvp;
999 struct vattr *vap = ap->a_vap;
1000 struct componentname *cnp = ap->a_cnp;
1001 struct inode *ip, *dp;
1002 struct vnode *tvp;
1003 struct ext2fs_dirtemplate dirtemplate;
1004 int error, dmode;
1005
1006 #ifdef DIAGNOSTIC
1007 if ((cnp->cn_flags & HASBUF) == 0)
1008 panic("ext2fs_mkdir: no name");
1009 #endif
1010 dp = VTOI(dvp);
1011 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
1012 error = EMLINK;
1013 goto out;
1014 }
1015 dmode = vap->va_mode & ACCESSPERMS;
1016 dmode |= IFDIR;
1017 /*
1018 * Must simulate part of ext2fs_makeinode here to acquire the inode,
1019 * but not have it entered in the parent directory. The entry is
1020 * made later after writing "." and ".." entries.
1021 */
1022 if ((error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
1023 goto out;
1024 ip = VTOI(tvp);
1025 ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1026 ip->i_e2fs_gid = dp->i_e2fs_gid;
1027 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1028 ip->i_e2fs_mode = dmode;
1029 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1030 ip->i_e2fs_nlink = 2;
1031 error = VOP_UPDATE(tvp, NULL, NULL, UPDATE_WAIT);
1032
1033 /*
1034 * Bump link count in parent directory
1035 * to reflect work done below. Should
1036 * be done before reference is created
1037 * so reparation is possible if we crash.
1038 */
1039 dp->i_e2fs_nlink++;
1040 dp->i_flag |= IN_CHANGE;
1041 if ((error = VOP_UPDATE(dvp, NULL, NULL, UPDATE_WAIT)) != 0)
1042 goto bad;
1043
1044 /* Initialize directory with "." and ".." from static template. */
1045 memset(&dirtemplate, 0, sizeof(dirtemplate));
1046 dirtemplate.dot_ino = h2fs32(ip->i_number);
1047 dirtemplate.dot_reclen = h2fs16(12);
1048 dirtemplate.dot_namlen = 1;
1049 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
1050 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
1051 dirtemplate.dot_type = EXT2_FT_DIR;
1052 }
1053 dirtemplate.dot_name[0] = '.';
1054 dirtemplate.dotdot_ino = h2fs32(dp->i_number);
1055 dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
1056 dirtemplate.dotdot_namlen = 2;
1057 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
1058 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
1059 dirtemplate.dotdot_type = EXT2_FT_DIR;
1060 }
1061 dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
1062 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1063 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1064 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, (struct proc *)0);
1065 if (error) {
1066 dp->i_e2fs_nlink--;
1067 dp->i_flag |= IN_CHANGE;
1068 goto bad;
1069 }
1070 if (VTOI(dvp)->i_e2fs->e2fs_bsize >
1071 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1072 panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
1073 else {
1074 ip->i_e2fs_size = VTOI(dvp)->i_e2fs->e2fs_bsize;
1075 ip->i_flag |= IN_CHANGE;
1076 }
1077
1078 /* Directory set up, now install it's entry in the parent directory. */
1079 error = ext2fs_direnter(ip, dvp, cnp);
1080 if (error != 0) {
1081 dp->i_e2fs_nlink--;
1082 dp->i_flag |= IN_CHANGE;
1083 }
1084 bad:
1085 /*
1086 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1087 * for us because we set the link count to 0.
1088 */
1089 if (error) {
1090 ip->i_e2fs_nlink = 0;
1091 ip->i_flag |= IN_CHANGE;
1092 vput(tvp);
1093 } else
1094 *ap->a_vpp = tvp;
1095 out:
1096 FREE(cnp->cn_pnbuf, M_NAMEI);
1097 vput(dvp);
1098 return (error);
1099 }
1100
1101 /*
1102 * Rmdir system call.
1103 */
1104 int
1105 ext2fs_rmdir(v)
1106 void *v;
1107 {
1108 struct vop_rmdir_args /* {
1109 struct vnode *a_dvp;
1110 struct vnode *a_vp;
1111 struct componentname *a_cnp;
1112 } */ *ap = v;
1113 struct vnode *vp = ap->a_vp;
1114 struct vnode *dvp = ap->a_dvp;
1115 struct componentname *cnp = ap->a_cnp;
1116 struct inode *ip, *dp;
1117 int error;
1118
1119 ip = VTOI(vp);
1120 dp = VTOI(dvp);
1121 /*
1122 * No rmdir "." please.
1123 */
1124 if (dp == ip) {
1125 vrele(dvp);
1126 vput(vp);
1127 return (EINVAL);
1128 }
1129 /*
1130 * Verify the directory is empty (and valid).
1131 * (Rmdir ".." won't be valid since
1132 * ".." will contain a reference to
1133 * the current directory and thus be
1134 * non-empty.)
1135 */
1136 error = 0;
1137 if (ip->i_e2fs_nlink != 2 ||
1138 !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1139 error = ENOTEMPTY;
1140 goto out;
1141 }
1142 if ((dp->i_e2fs_flags & EXT2_APPEND) ||
1143 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
1144 error = EPERM;
1145 goto out;
1146 }
1147 /*
1148 * Delete reference to directory before purging
1149 * inode. If we crash in between, the directory
1150 * will be reattached to lost+found,
1151 */
1152 error = ext2fs_dirremove(dvp, cnp);
1153 if (error != 0)
1154 goto out;
1155 dp->i_e2fs_nlink--;
1156 dp->i_flag |= IN_CHANGE;
1157 cache_purge(dvp);
1158 vput(dvp);
1159 dvp = NULL;
1160 /*
1161 * Truncate inode. The only stuff left
1162 * in the directory is "." and "..". The
1163 * "." reference is inconsequential since
1164 * we're quashing it. The ".." reference
1165 * has already been adjusted above. We've
1166 * removed the "." reference and the reference
1167 * in the parent directory, but there may be
1168 * other hard links so decrement by 2 and
1169 * worry about them later.
1170 */
1171 ip->i_e2fs_nlink -= 2;
1172 error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1173 cnp->cn_proc);
1174 cache_purge(ITOV(ip));
1175 out:
1176 if (dvp)
1177 vput(dvp);
1178 vput(vp);
1179 return (error);
1180 }
1181
1182 /*
1183 * symlink -- make a symbolic link
1184 */
1185 int
1186 ext2fs_symlink(v)
1187 void *v;
1188 {
1189 struct vop_symlink_args /* {
1190 struct vnode *a_dvp;
1191 struct vnode **a_vpp;
1192 struct componentname *a_cnp;
1193 struct vattr *a_vap;
1194 char *a_target;
1195 } */ *ap = v;
1196 struct vnode *vp, **vpp = ap->a_vpp;
1197 struct inode *ip;
1198 int len, error;
1199
1200 error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1201 vpp, ap->a_cnp);
1202 if (error)
1203 return (error);
1204 vp = *vpp;
1205 len = strlen(ap->a_target);
1206 if (len < vp->v_mount->mnt_maxsymlinklen) {
1207 ip = VTOI(vp);
1208 memcpy((char *)ip->i_din.e2fs_din.e2di_shortlink, ap->a_target, len);
1209 ip->i_e2fs_size = len;
1210 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1211 } else
1212 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1213 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1214 (size_t *)0, (struct proc *)0);
1215 vput(vp);
1216 return (error);
1217 }
1218
1219 /*
1220 * Return target name of a symbolic link
1221 */
1222 int
1223 ext2fs_readlink(v)
1224 void *v;
1225 {
1226 struct vop_readlink_args /* {
1227 struct vnode *a_vp;
1228 struct uio *a_uio;
1229 struct ucred *a_cred;
1230 } */ *ap = v;
1231 struct vnode *vp = ap->a_vp;
1232 struct inode *ip = VTOI(vp);
1233 int isize;
1234
1235 isize = ip->i_e2fs_size;
1236 if (isize < vp->v_mount->mnt_maxsymlinklen ||
1237 (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
1238 uiomove((char *)ip->i_din.e2fs_din.e2di_shortlink, isize, ap->a_uio);
1239 return (0);
1240 }
1241 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1242 }
1243
1244 /*
1245 * Advisory record locking support
1246 */
1247 int
1248 ext2fs_advlock(v)
1249 void *v;
1250 {
1251 struct vop_advlock_args /* {
1252 struct vnode *a_vp;
1253 caddr_t a_id;
1254 int a_op;
1255 struct flock *a_fl;
1256 int a_flags;
1257 } */ *ap = v;
1258 struct inode *ip = VTOI(ap->a_vp);
1259
1260 return (lf_advlock(&ip->i_lockf, ip->i_e2fs_size, ap->a_id, ap->a_op,
1261 ap->a_fl, ap->a_flags));
1262 }
1263
1264 /*
1265 * Initialize the vnode associated with a new inode, handle aliased
1266 * vnodes.
1267 */
1268 int
1269 ext2fs_vinit(mntp, specops, fifoops, vpp)
1270 struct mount *mntp;
1271 int (**specops) __P((void *));
1272 int (**fifoops) __P((void *));
1273 struct vnode **vpp;
1274 {
1275 struct inode *ip;
1276 struct vnode *vp, *nvp;
1277
1278 vp = *vpp;
1279 ip = VTOI(vp);
1280 switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
1281 case VCHR:
1282 case VBLK:
1283 vp->v_op = specops;
1284 if ((nvp = checkalias(vp,
1285 fs2h32(ip->i_din.e2fs_din.e2di_rdev), mntp)) != NULL) {
1286 /*
1287 * Discard unneeded vnode, but save its inode.
1288 */
1289 nvp->v_data = vp->v_data;
1290 vp->v_data = NULL;
1291 vp->v_op = spec_vnodeop_p;
1292 vput(vp);
1293 vgone(vp);
1294 lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
1295 /*
1296 * Reinitialize aliased inode.
1297 */
1298 vp = nvp;
1299 ip->i_vnode = vp;
1300 }
1301 break;
1302 case VFIFO:
1303 vp->v_op = fifoops;
1304 break;
1305 case VNON:
1306 case VBAD:
1307 case VSOCK:
1308 case VLNK:
1309 case VDIR:
1310 case VREG:
1311 break;
1312 }
1313 if (ip->i_number == ROOTINO)
1314 vp->v_flag |= VROOT;
1315 /*
1316 * Initialize modrev times
1317 */
1318 SETHIGH(ip->i_modrev, mono_time.tv_sec);
1319 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
1320 *vpp = vp;
1321 return (0);
1322 }
1323
1324 /*
1325 * Allocate a new inode.
1326 */
1327 int
1328 ext2fs_makeinode(mode, dvp, vpp, cnp)
1329 int mode;
1330 struct vnode *dvp;
1331 struct vnode **vpp;
1332 struct componentname *cnp;
1333 {
1334 struct inode *ip, *pdir;
1335 struct vnode *tvp;
1336 int error;
1337
1338 pdir = VTOI(dvp);
1339 #ifdef DIAGNOSTIC
1340 if ((cnp->cn_flags & HASBUF) == 0)
1341 panic("ext2fs_makeinode: no name");
1342 #endif
1343 *vpp = NULL;
1344 if ((mode & IFMT) == 0)
1345 mode |= IFREG;
1346
1347 if ((error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
1348 free(cnp->cn_pnbuf, M_NAMEI);
1349 vput(dvp);
1350 return (error);
1351 }
1352 ip = VTOI(tvp);
1353 ip->i_e2fs_gid = pdir->i_e2fs_gid;
1354 ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1355 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1356 ip->i_e2fs_mode = mode;
1357 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1358 ip->i_e2fs_nlink = 1;
1359 if ((ip->i_e2fs_mode & ISGID) &&
1360 !groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1361 suser(cnp->cn_cred, NULL))
1362 ip->i_e2fs_mode &= ~ISGID;
1363
1364 /*
1365 * Make sure inode goes to disk before directory entry.
1366 */
1367 if ((error = VOP_UPDATE(tvp, NULL, NULL, UPDATE_WAIT)) != 0)
1368 goto bad;
1369 error = ext2fs_direnter(ip, dvp, cnp);
1370 if (error != 0)
1371 goto bad;
1372 if ((cnp->cn_flags & SAVESTART) == 0)
1373 FREE(cnp->cn_pnbuf, M_NAMEI);
1374 vput(dvp);
1375 *vpp = tvp;
1376 return (0);
1377
1378 bad:
1379 /*
1380 * Write error occurred trying to update the inode
1381 * or the directory so must deallocate the inode.
1382 */
1383 free(cnp->cn_pnbuf, M_NAMEI);
1384 vput(dvp);
1385 ip->i_e2fs_nlink = 0;
1386 ip->i_flag |= IN_CHANGE;
1387 vput(tvp);
1388 return (error);
1389 }
1390
1391 /*
1392 * Reclaim an inode so that it can be used for other purposes.
1393 */
1394 int
1395 ext2fs_reclaim(v)
1396 void *v;
1397 {
1398 struct vop_reclaim_args /* {
1399 struct vnode *a_vp;
1400 } */ *ap = v;
1401 struct vnode *vp = ap->a_vp;
1402 struct inode *ip;
1403 extern int prtactive;
1404
1405 if (prtactive && vp->v_usecount != 0)
1406 vprint("ext2fs_reclaim: pushing active", vp);
1407 /*
1408 * Remove the inode from its hash chain.
1409 */
1410 ip = VTOI(vp);
1411 ufs_ihashrem(ip);
1412 /*
1413 * Purge old data structures associated with the inode.
1414 */
1415 cache_purge(vp);
1416 if (ip->i_devvp) {
1417 vrele(ip->i_devvp);
1418 ip->i_devvp = 0;
1419 }
1420
1421 pool_put(&ext2fs_inode_pool, vp->v_data);
1422 vp->v_data = NULL;
1423 return (0);
1424 }
1425
1426 /* Global vfs data structures for ext2fs. */
1427 int (**ext2fs_vnodeop_p) __P((void *));
1428 struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1429 { &vop_default_desc, vn_default_error },
1430 { &vop_lookup_desc, ext2fs_lookup }, /* lookup */
1431 { &vop_create_desc, ext2fs_create }, /* create */
1432 { &vop_mknod_desc, ext2fs_mknod }, /* mknod */
1433 { &vop_open_desc, ext2fs_open }, /* open */
1434 { &vop_close_desc, ufs_close }, /* close */
1435 { &vop_access_desc, ext2fs_access }, /* access */
1436 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1437 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1438 { &vop_read_desc, ext2fs_read }, /* read */
1439 { &vop_write_desc, ext2fs_write }, /* write */
1440 { &vop_lease_desc, ufs_lease_check }, /* lease */
1441 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
1442 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1443 { &vop_poll_desc, ufs_poll }, /* poll */
1444 { &vop_revoke_desc, ufs_revoke }, /* revoke */
1445 { &vop_mmap_desc, ufs_mmap }, /* mmap */
1446 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1447 { &vop_seek_desc, ufs_seek }, /* seek */
1448 { &vop_remove_desc, ext2fs_remove }, /* remove */
1449 { &vop_link_desc, ext2fs_link }, /* link */
1450 { &vop_rename_desc, ext2fs_rename }, /* rename */
1451 { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */
1452 { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */
1453 { &vop_symlink_desc, ext2fs_symlink }, /* symlink */
1454 { &vop_readdir_desc, ext2fs_readdir }, /* readdir */
1455 { &vop_readlink_desc, ext2fs_readlink }, /* readlink */
1456 { &vop_abortop_desc, ufs_abortop }, /* abortop */
1457 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1458 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1459 { &vop_lock_desc, ufs_lock }, /* lock */
1460 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1461 { &vop_bmap_desc, ext2fs_bmap }, /* bmap */
1462 { &vop_strategy_desc, ufs_strategy }, /* strategy */
1463 { &vop_print_desc, ufs_print }, /* print */
1464 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1465 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
1466 { &vop_advlock_desc, ext2fs_advlock }, /* advlock */
1467 { &vop_blkatoff_desc, ext2fs_blkatoff }, /* blkatoff */
1468 { &vop_valloc_desc, ext2fs_valloc }, /* valloc */
1469 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1470 { &vop_truncate_desc, ext2fs_truncate }, /* truncate */
1471 { &vop_update_desc, ext2fs_update }, /* update */
1472 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1473 { (struct vnodeop_desc*)NULL, (int(*) __P((void*)))NULL }
1474 };
1475 struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
1476 { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
1477
1478 int (**ext2fs_specop_p) __P((void *));
1479 struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1480 { &vop_default_desc, vn_default_error },
1481 { &vop_lookup_desc, spec_lookup }, /* lookup */
1482 { &vop_create_desc, spec_create }, /* create */
1483 { &vop_mknod_desc, spec_mknod }, /* mknod */
1484 { &vop_open_desc, spec_open }, /* open */
1485 { &vop_close_desc, ufsspec_close }, /* close */
1486 { &vop_access_desc, ext2fs_access }, /* access */
1487 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1488 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1489 { &vop_read_desc, ufsspec_read }, /* read */
1490 { &vop_write_desc, ufsspec_write }, /* write */
1491 { &vop_lease_desc, spec_lease_check }, /* lease */
1492 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
1493 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1494 { &vop_poll_desc, spec_poll }, /* poll */
1495 { &vop_revoke_desc, spec_revoke }, /* revoke */
1496 { &vop_mmap_desc, spec_mmap }, /* mmap */
1497 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1498 { &vop_seek_desc, spec_seek }, /* seek */
1499 { &vop_remove_desc, spec_remove }, /* remove */
1500 { &vop_link_desc, spec_link }, /* link */
1501 { &vop_rename_desc, spec_rename }, /* rename */
1502 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
1503 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
1504 { &vop_symlink_desc, spec_symlink }, /* symlink */
1505 { &vop_readdir_desc, spec_readdir }, /* readdir */
1506 { &vop_readlink_desc, spec_readlink }, /* readlink */
1507 { &vop_abortop_desc, spec_abortop }, /* abortop */
1508 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1509 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1510 { &vop_lock_desc, ufs_lock }, /* lock */
1511 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1512 { &vop_bmap_desc, spec_bmap }, /* bmap */
1513 { &vop_strategy_desc, spec_strategy }, /* strategy */
1514 { &vop_print_desc, ufs_print }, /* print */
1515 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1516 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
1517 { &vop_advlock_desc, spec_advlock }, /* advlock */
1518 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
1519 { &vop_valloc_desc, spec_valloc }, /* valloc */
1520 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1521 { &vop_truncate_desc, spec_truncate }, /* truncate */
1522 { &vop_update_desc, ext2fs_update }, /* update */
1523 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1524 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1525 };
1526 struct vnodeopv_desc ext2fs_specop_opv_desc =
1527 { &ext2fs_specop_p, ext2fs_specop_entries };
1528
1529 int (**ext2fs_fifoop_p) __P((void *));
1530 struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1531 { &vop_default_desc, vn_default_error },
1532 { &vop_lookup_desc, fifo_lookup }, /* lookup */
1533 { &vop_create_desc, fifo_create }, /* create */
1534 { &vop_mknod_desc, fifo_mknod }, /* mknod */
1535 { &vop_open_desc, fifo_open }, /* open */
1536 { &vop_close_desc, ufsfifo_close }, /* close */
1537 { &vop_access_desc, ext2fs_access }, /* access */
1538 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1539 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1540 { &vop_read_desc, ufsfifo_read }, /* read */
1541 { &vop_write_desc, ufsfifo_write }, /* write */
1542 { &vop_lease_desc, fifo_lease_check }, /* lease */
1543 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
1544 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1545 { &vop_poll_desc, fifo_poll }, /* poll */
1546 { &vop_revoke_desc, fifo_revoke }, /* revoke */
1547 { &vop_mmap_desc, fifo_mmap }, /* mmap */
1548 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1549 { &vop_seek_desc, fifo_seek }, /* seek */
1550 { &vop_remove_desc, fifo_remove }, /* remove */
1551 { &vop_link_desc, fifo_link }, /* link */
1552 { &vop_rename_desc, fifo_rename }, /* rename */
1553 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
1554 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
1555 { &vop_symlink_desc, fifo_symlink }, /* symlink */
1556 { &vop_readdir_desc, fifo_readdir }, /* readdir */
1557 { &vop_readlink_desc, fifo_readlink }, /* readlink */
1558 { &vop_abortop_desc, fifo_abortop }, /* abortop */
1559 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1560 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1561 { &vop_lock_desc, ufs_lock }, /* lock */
1562 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1563 { &vop_bmap_desc, fifo_bmap }, /* bmap */
1564 { &vop_strategy_desc, fifo_strategy }, /* strategy */
1565 { &vop_print_desc, ufs_print }, /* print */
1566 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1567 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
1568 { &vop_advlock_desc, fifo_advlock }, /* advlock */
1569 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
1570 { &vop_valloc_desc, fifo_valloc }, /* valloc */
1571 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1572 { &vop_truncate_desc, fifo_truncate }, /* truncate */
1573 { &vop_update_desc, ext2fs_update }, /* update */
1574 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1575 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1576 };
1577 struct vnodeopv_desc ext2fs_fifoop_opv_desc =
1578 { &ext2fs_fifoop_p, ext2fs_fifoop_entries };
1579