ext2fs_vnops.c revision 1.19.4.2 1 /* $NetBSD: ext2fs_vnops.c,v 1.19.4.2 1999/08/02 22:56:41 thorpej 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 register struct vattr *vap = ap->a_vap;
135 register struct vnode **vpp = ap->a_vpp;
136 register 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 register struct vnode *vp = ap->a_vp;
242 register struct inode *ip = VTOI(vp);
243 register 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 & (EXT2_APPEND | EXT2_IMMUTABLE)) &&
324 securelevel > 0)
325 return (EPERM);
326 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
327 ip->i_e2fs_flags |= (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
328 (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
329 } else {
330 return (EPERM);
331 }
332 #else
333 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE);
334 ip->i_e2fs_flags |= (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
335 (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0;
336 #endif
337 ip->i_flag |= IN_CHANGE;
338 if (vap->va_flags & (IMMUTABLE | APPEND))
339 return (0);
340 }
341 if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
342 return (EPERM);
343 /*
344 * Go through the fields and update iff not VNOVAL.
345 */
346 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
347 if (vp->v_mount->mnt_flag & MNT_RDONLY)
348 return (EROFS);
349 error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p);
350 if (error)
351 return (error);
352 }
353 if (vap->va_size != VNOVAL) {
354 /*
355 * Disallow write attempts on read-only file systems;
356 * unless the file is a socket, fifo, or a block or
357 * character device resident on the file system.
358 */
359 switch (vp->v_type) {
360 case VDIR:
361 return (EISDIR);
362 case VLNK:
363 case VREG:
364 if (vp->v_mount->mnt_flag & MNT_RDONLY)
365 return (EROFS);
366 default:
367 break;
368 }
369 error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p);
370 if (error)
371 return (error);
372 }
373 ip = VTOI(vp);
374 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
375 if (vp->v_mount->mnt_flag & MNT_RDONLY)
376 return (EROFS);
377 if (cred->cr_uid != ip->i_e2fs_uid &&
378 (error = suser(cred, &p->p_acflag)) &&
379 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
380 (error = VOP_ACCESS(vp, VWRITE, cred, p))))
381 return (error);
382 if (vap->va_atime.tv_sec != VNOVAL)
383 if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
384 ip->i_flag |= IN_ACCESS;
385 if (vap->va_mtime.tv_sec != VNOVAL)
386 ip->i_flag |= IN_CHANGE | IN_UPDATE;
387 error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1);
388 if (error)
389 return (error);
390 }
391 error = 0;
392 if (vap->va_mode != (mode_t)VNOVAL) {
393 if (vp->v_mount->mnt_flag & MNT_RDONLY)
394 return (EROFS);
395 error = ext2fs_chmod(vp, (int)vap->va_mode, cred, p);
396 }
397 return (error);
398 }
399
400 /*
401 * Change the mode on a file.
402 * Inode must be locked before calling.
403 */
404 static int
405 ext2fs_chmod(vp, mode, cred, p)
406 register struct vnode *vp;
407 register int mode;
408 register struct ucred *cred;
409 struct proc *p;
410 {
411 register struct inode *ip = VTOI(vp);
412 int error;
413
414 if (cred->cr_uid != ip->i_e2fs_uid &&
415 (error = suser(cred, &p->p_acflag)))
416 return (error);
417 if (cred->cr_uid) {
418 if (vp->v_type != VDIR && (mode & S_ISTXT))
419 return (EFTYPE);
420 if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID))
421 return (EPERM);
422 }
423 ip->i_e2fs_mode &= ~ALLPERMS;
424 ip->i_e2fs_mode |= (mode & ALLPERMS);
425 ip->i_flag |= IN_CHANGE;
426 return (0);
427 }
428
429 /*
430 * Perform chown operation on inode ip;
431 * inode must be locked prior to call.
432 */
433 static int
434 ext2fs_chown(vp, uid, gid, cred, p)
435 register struct vnode *vp;
436 uid_t uid;
437 gid_t gid;
438 struct ucred *cred;
439 struct proc *p;
440 {
441 register struct inode *ip = VTOI(vp);
442 uid_t ouid;
443 gid_t ogid;
444 int error = 0;
445
446 if (uid == (uid_t)VNOVAL)
447 uid = ip->i_e2fs_uid;
448 if (gid == (gid_t)VNOVAL)
449 gid = ip->i_e2fs_gid;
450 /*
451 * If we don't own the file, are trying to change the owner
452 * of the file, or are not a member of the target group,
453 * the caller must be superuser or the call fails.
454 */
455 if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid ||
456 (gid != ip->i_e2fs_gid && !groupmember((gid_t)gid, cred))) &&
457 (error = suser(cred, &p->p_acflag)))
458 return (error);
459 ogid = ip->i_e2fs_gid;
460 ouid = ip->i_e2fs_uid;
461
462 ip->i_e2fs_gid = gid;
463 ip->i_e2fs_uid = uid;
464 if (ouid != uid || ogid != gid)
465 ip->i_flag |= IN_CHANGE;
466 if (ouid != uid && cred->cr_uid != 0)
467 ip->i_e2fs_mode &= ~ISUID;
468 if (ogid != gid && cred->cr_uid != 0)
469 ip->i_e2fs_mode &= ~ISGID;
470 return (0);
471 }
472
473 int
474 ext2fs_remove(v)
475 void *v;
476 {
477 struct vop_remove_args /* {
478 struct vnode *a_dvp;
479 struct vnode *a_vp;
480 struct componentname *a_cnp;
481 } */ *ap = v;
482 struct inode *ip;
483 struct vnode *vp = ap->a_vp;
484 struct vnode *dvp = ap->a_dvp;
485 int error;
486
487 ip = VTOI(vp);
488 if (vp->v_type == VDIR ||
489 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
490 (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
491 error = EPERM;
492 goto out;
493 }
494 error = ext2fs_dirremove(dvp, ap->a_cnp);
495 if (error == 0) {
496 ip->i_e2fs_nlink--;
497 ip->i_flag |= IN_CHANGE;
498 }
499 out:
500 if (dvp == vp)
501 vrele(vp);
502 else
503 vput(vp);
504 vput(dvp);
505 return (error);
506 }
507
508 /*
509 * link vnode call
510 */
511 int
512 ext2fs_link(v)
513 void *v;
514 {
515 struct vop_link_args /* {
516 struct vnode *a_dvp;
517 struct vnode *a_vp;
518 struct componentname *a_cnp;
519 } */ *ap = v;
520 struct vnode *dvp = ap->a_dvp;
521 struct vnode *vp = ap->a_vp;
522 struct componentname *cnp = ap->a_cnp;
523 struct inode *ip;
524 int error;
525
526 #ifdef DIAGNOSTIC
527 if ((cnp->cn_flags & HASBUF) == 0)
528 panic("ext2fs_link: no name");
529 #endif
530 if (vp->v_type == VDIR) {
531 VOP_ABORTOP(dvp, cnp);
532 error = EISDIR;
533 goto out2;
534 }
535 if (dvp->v_mount != vp->v_mount) {
536 VOP_ABORTOP(dvp, cnp);
537 error = EXDEV;
538 goto out2;
539 }
540 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
541 VOP_ABORTOP(dvp, cnp);
542 goto out2;
543 }
544 ip = VTOI(vp);
545 if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
546 VOP_ABORTOP(dvp, cnp);
547 error = EMLINK;
548 goto out1;
549 }
550 if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
551 VOP_ABORTOP(dvp, cnp);
552 error = EPERM;
553 goto out1;
554 }
555 ip->i_e2fs_nlink++;
556 ip->i_flag |= IN_CHANGE;
557 error = VOP_UPDATE(vp, NULL, NULL, 1);
558 if (!error)
559 error = ext2fs_direnter(ip, dvp, cnp);
560 if (error) {
561 ip->i_e2fs_nlink--;
562 ip->i_flag |= IN_CHANGE;
563 }
564 FREE(cnp->cn_pnbuf, M_NAMEI);
565 out1:
566 if (dvp != vp)
567 VOP_UNLOCK(vp, 0);
568 out2:
569 vput(dvp);
570 return (error);
571 }
572
573 /*
574 * Rename system call.
575 * rename("foo", "bar");
576 * is essentially
577 * unlink("bar");
578 * link("foo", "bar");
579 * unlink("foo");
580 * but ``atomically''. Can't do full commit without saving state in the
581 * inode on disk which isn't feasible at this time. Best we can do is
582 * always guarantee the target exists.
583 *
584 * Basic algorithm is:
585 *
586 * 1) Bump link count on source while we're linking it to the
587 * target. This also ensure the inode won't be deleted out
588 * from underneath us while we work (it may be truncated by
589 * a concurrent `trunc' or `open' for creation).
590 * 2) Link source to destination. If destination already exists,
591 * delete it first.
592 * 3) Unlink source reference to inode if still around. If a
593 * directory was moved and the parent of the destination
594 * is different from the source, patch the ".." entry in the
595 * directory.
596 */
597 int
598 ext2fs_rename(v)
599 void *v;
600 {
601 struct vop_rename_args /* {
602 struct vnode *a_fdvp;
603 struct vnode *a_fvp;
604 struct componentname *a_fcnp;
605 struct vnode *a_tdvp;
606 struct vnode *a_tvp;
607 struct componentname *a_tcnp;
608 } */ *ap = v;
609 struct vnode *tvp = ap->a_tvp;
610 register struct vnode *tdvp = ap->a_tdvp;
611 struct vnode *fvp = ap->a_fvp;
612 struct vnode *fdvp = ap->a_fdvp;
613 struct componentname *tcnp = ap->a_tcnp;
614 struct componentname *fcnp = ap->a_fcnp;
615 struct inode *ip, *xp, *dp;
616 struct ext2fs_dirtemplate dirbuf;
617 int doingdirectory = 0, oldparent = 0, newparent = 0;
618 int error = 0;
619 u_char namlen;
620
621 #ifdef DIAGNOSTIC
622 if ((tcnp->cn_flags & HASBUF) == 0 ||
623 (fcnp->cn_flags & HASBUF) == 0)
624 panic("ext2fs_rename: no name");
625 #endif
626 /*
627 * Check for cross-device rename.
628 */
629 if ((fvp->v_mount != tdvp->v_mount) ||
630 (tvp && (fvp->v_mount != tvp->v_mount))) {
631 error = EXDEV;
632 abortit:
633 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
634 if (tdvp == tvp)
635 vrele(tdvp);
636 else
637 vput(tdvp);
638 if (tvp)
639 vput(tvp);
640 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
641 vrele(fdvp);
642 vrele(fvp);
643 return (error);
644 }
645
646 /*
647 * Check if just deleting a link name.
648 */
649 if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
650 (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) {
651 error = EPERM;
652 goto abortit;
653 }
654 if (fvp == tvp) {
655 if (fvp->v_type == VDIR) {
656 error = EINVAL;
657 goto abortit;
658 }
659
660 /* Release destination completely. */
661 VOP_ABORTOP(tdvp, tcnp);
662 vput(tdvp);
663 vput(tvp);
664
665 /* Delete source. */
666 vrele(fdvp);
667 vrele(fvp);
668 fcnp->cn_flags &= ~MODMASK;
669 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
670 if ((fcnp->cn_flags & SAVESTART) == 0)
671 panic("ext2fs_rename: lost from startdir");
672 fcnp->cn_nameiop = DELETE;
673 (void) relookup(fdvp, &fvp, fcnp);
674 return (VOP_REMOVE(fdvp, fvp, fcnp));
675 }
676 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
677 goto abortit;
678 dp = VTOI(fdvp);
679 ip = VTOI(fvp);
680 if ((nlink_t) ip->i_e2fs_nlink >= LINK_MAX) {
681 VOP_UNLOCK(fvp, 0);
682 error = EMLINK;
683 goto abortit;
684 }
685 if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
686 (dp->i_e2fs_flags & EXT2_APPEND)) {
687 VOP_UNLOCK(fvp, 0);
688 error = EPERM;
689 goto abortit;
690 }
691 if ((ip->i_e2fs_mode & IFMT) == IFDIR) {
692 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
693 if (!error && tvp)
694 error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred,
695 tcnp->cn_proc);
696 if (error) {
697 VOP_UNLOCK(fvp, 0);
698 error = EACCES;
699 goto abortit;
700 }
701 /*
702 * Avoid ".", "..", and aliases of "." for obvious reasons.
703 */
704 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
705 dp == ip ||
706 (fcnp->cn_flags&ISDOTDOT) ||
707 (tcnp->cn_flags & ISDOTDOT) ||
708 (ip->i_flag & IN_RENAME)) {
709 VOP_UNLOCK(fvp, 0);
710 error = EINVAL;
711 goto abortit;
712 }
713 ip->i_flag |= IN_RENAME;
714 oldparent = dp->i_number;
715 doingdirectory++;
716 }
717 vrele(fdvp);
718
719 /*
720 * When the target exists, both the directory
721 * and target vnodes are returned locked.
722 */
723 dp = VTOI(tdvp);
724 xp = NULL;
725 if (tvp)
726 xp = VTOI(tvp);
727
728 /*
729 * 1) Bump link count while we're moving stuff
730 * around. If we crash somewhere before
731 * completing our work, the link count
732 * may be wrong, but correctable.
733 */
734 ip->i_e2fs_nlink++;
735 ip->i_flag |= IN_CHANGE;
736 if ((error = VOP_UPDATE(fvp, NULL, NULL, 1)) != 0) {
737 VOP_UNLOCK(fvp, 0);
738 goto bad;
739 }
740
741 /*
742 * If ".." must be changed (ie the directory gets a new
743 * parent) then the source directory must not be in the
744 * directory heirarchy above the target, as this would
745 * orphan everything below the source directory. Also
746 * the user must have write permission in the source so
747 * as to be able to change "..". We must repeat the call
748 * to namei, as the parent directory is unlocked by the
749 * call to checkpath().
750 */
751 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc);
752 VOP_UNLOCK(fvp, 0);
753 if (oldparent != dp->i_number)
754 newparent = dp->i_number;
755 if (doingdirectory && newparent) {
756 if (error) /* write access check above */
757 goto bad;
758 if (xp != NULL)
759 vput(tvp);
760 error = ext2fs_checkpath(ip, dp, tcnp->cn_cred);
761 if (error != 0)
762 goto out;
763 if ((tcnp->cn_flags & SAVESTART) == 0)
764 panic("ext2fs_rename: lost to startdir");
765 if ((error = relookup(tdvp, &tvp, tcnp)) != 0)
766 goto out;
767 dp = VTOI(tdvp);
768 xp = NULL;
769 if (tvp)
770 xp = VTOI(tvp);
771 }
772 /*
773 * 2) If target doesn't exist, link the target
774 * to the source and unlink the source.
775 * Otherwise, rewrite the target directory
776 * entry to reference the source inode and
777 * expunge the original entry's existence.
778 */
779 if (xp == NULL) {
780 if (dp->i_dev != ip->i_dev)
781 panic("rename: EXDEV");
782 /*
783 * Account for ".." in new directory.
784 * When source and destination have the same
785 * parent we don't fool with the link count.
786 */
787 if (doingdirectory && newparent) {
788 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
789 error = EMLINK;
790 goto bad;
791 }
792 dp->i_e2fs_nlink++;
793 dp->i_flag |= IN_CHANGE;
794 if ((error = VOP_UPDATE(tdvp, NULL, NULL, 1)) != 0)
795 goto bad;
796 }
797 error = ext2fs_direnter(ip, tdvp, tcnp);
798 if (error != 0) {
799 if (doingdirectory && newparent) {
800 dp->i_e2fs_nlink--;
801 dp->i_flag |= IN_CHANGE;
802 (void)VOP_UPDATE(tdvp, NULL, NULL, 1);
803 }
804 goto bad;
805 }
806 vput(tdvp);
807 } else {
808 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
809 panic("rename: EXDEV");
810 /*
811 * Short circuit rename(foo, foo).
812 */
813 if (xp->i_number == ip->i_number)
814 panic("rename: same file");
815 /*
816 * If the parent directory is "sticky", then the user must
817 * own the parent directory, or the destination of the rename,
818 * otherwise the destination may not be changed (except by
819 * root). This implements append-only directories.
820 */
821 if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
822 tcnp->cn_cred->cr_uid != dp->i_e2fs_uid &&
823 xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) {
824 error = EPERM;
825 goto bad;
826 }
827 /*
828 * Target must be empty if a directory and have no links
829 * to it. Also, ensure source and target are compatible
830 * (both directories, or both not directories).
831 */
832 if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
833 if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
834 xp->i_e2fs_nlink > 2) {
835 error = ENOTEMPTY;
836 goto bad;
837 }
838 if (!doingdirectory) {
839 error = ENOTDIR;
840 goto bad;
841 }
842 cache_purge(tdvp);
843 } else if (doingdirectory) {
844 error = EISDIR;
845 goto bad;
846 }
847 error = ext2fs_dirrewrite(dp, ip, tcnp);
848 if (error != 0)
849 goto bad;
850 /*
851 * If the target directory is in the same
852 * directory as the source directory,
853 * decrement the link count on the parent
854 * of the target directory.
855 */
856 if (doingdirectory && !newparent) {
857 dp->i_e2fs_nlink--;
858 dp->i_flag |= IN_CHANGE;
859 }
860 vput(tdvp);
861 /*
862 * Adjust the link count of the target to
863 * reflect the dirrewrite above. If this is
864 * a directory it is empty and there are
865 * no links to it, so we can squash the inode and
866 * any space associated with it. We disallowed
867 * renaming over top of a directory with links to
868 * it above, as the remaining link would point to
869 * a directory without "." or ".." entries.
870 */
871 xp->i_e2fs_nlink--;
872 if (doingdirectory) {
873 if (--xp->i_e2fs_nlink != 0)
874 panic("rename: linked directory");
875 error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC,
876 tcnp->cn_cred, tcnp->cn_proc);
877 }
878 xp->i_flag |= IN_CHANGE;
879 vput(tvp);
880 xp = NULL;
881 }
882
883 /*
884 * 3) Unlink the source.
885 */
886 fcnp->cn_flags &= ~MODMASK;
887 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
888 if ((fcnp->cn_flags & SAVESTART) == 0)
889 panic("ext2fs_rename: lost from startdir");
890 (void) relookup(fdvp, &fvp, fcnp);
891 if (fvp != NULL) {
892 xp = VTOI(fvp);
893 dp = VTOI(fdvp);
894 } else {
895 /*
896 * From name has disappeared.
897 */
898 if (doingdirectory)
899 panic("ext2fs_rename: lost dir entry");
900 vrele(ap->a_fvp);
901 return (0);
902 }
903 /*
904 * Ensure that the directory entry still exists and has not
905 * changed while the new name has been entered. If the source is
906 * a file then the entry may have been unlinked or renamed. In
907 * either case there is no further work to be done. If the source
908 * is a directory then it cannot have been rmdir'ed; its link
909 * count of three would cause a rmdir to fail with ENOTEMPTY.
910 * The IRENAME flag ensures that it cannot be moved by another
911 * rename.
912 */
913 if (xp != ip) {
914 if (doingdirectory)
915 panic("ext2fs_rename: lost dir entry");
916 } else {
917 /*
918 * If the source is a directory with a
919 * new parent, the link count of the old
920 * parent directory must be decremented
921 * and ".." set to point to the new parent.
922 */
923 if (doingdirectory && newparent) {
924 dp->i_e2fs_nlink--;
925 dp->i_flag |= IN_CHANGE;
926 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
927 sizeof (struct ext2fs_dirtemplate), (off_t)0,
928 UIO_SYSSPACE, IO_NODELOCKED,
929 tcnp->cn_cred, (size_t *)0, (struct proc *)0);
930 if (error == 0) {
931 namlen = fs2h16(dirbuf.dotdot_namlen);
932 if (namlen != 2 ||
933 dirbuf.dotdot_name[0] != '.' ||
934 dirbuf.dotdot_name[1] != '.') {
935 ufs_dirbad(xp, (doff_t)12,
936 "ext2fs_rename: mangled dir");
937 } else {
938 dirbuf.dotdot_ino = h2fs32(newparent);
939 (void) vn_rdwr(UIO_WRITE, fvp,
940 (caddr_t)&dirbuf,
941 sizeof (struct dirtemplate),
942 (off_t)0, UIO_SYSSPACE,
943 IO_NODELOCKED|IO_SYNC,
944 tcnp->cn_cred, (size_t *)0,
945 (struct proc *)0);
946 cache_purge(fdvp);
947 }
948 }
949 }
950 error = ext2fs_dirremove(fdvp, fcnp);
951 if (!error) {
952 xp->i_e2fs_nlink--;
953 xp->i_flag |= IN_CHANGE;
954 }
955 xp->i_flag &= ~IN_RENAME;
956 }
957 if (dp)
958 vput(fdvp);
959 if (xp)
960 vput(fvp);
961 vrele(ap->a_fvp);
962 return (error);
963
964 bad:
965 if (xp)
966 vput(ITOV(xp));
967 vput(ITOV(dp));
968 out:
969 if (doingdirectory)
970 ip->i_flag &= ~IN_RENAME;
971 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
972 ip->i_e2fs_nlink--;
973 ip->i_flag |= IN_CHANGE;
974 vput(fvp);
975 } else
976 vrele(fvp);
977 return (error);
978 }
979
980 /*
981 * Mkdir system call
982 */
983 int
984 ext2fs_mkdir(v)
985 void *v;
986 {
987 struct vop_mkdir_args /* {
988 struct vnode *a_dvp;
989 struct vnode **a_vpp;
990 struct componentname *a_cnp;
991 struct vattr *a_vap;
992 } */ *ap = v;
993 register struct vnode *dvp = ap->a_dvp;
994 register struct vattr *vap = ap->a_vap;
995 register struct componentname *cnp = ap->a_cnp;
996 register struct inode *ip, *dp;
997 struct vnode *tvp;
998 struct ext2fs_dirtemplate dirtemplate;
999 int error, dmode;
1000
1001 #ifdef DIAGNOSTIC
1002 if ((cnp->cn_flags & HASBUF) == 0)
1003 panic("ext2fs_mkdir: no name");
1004 #endif
1005 dp = VTOI(dvp);
1006 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
1007 error = EMLINK;
1008 goto out;
1009 }
1010 dmode = vap->va_mode & ACCESSPERMS;
1011 dmode |= IFDIR;
1012 /*
1013 * Must simulate part of ext2fs_makeinode here to acquire the inode,
1014 * but not have it entered in the parent directory. The entry is
1015 * made later after writing "." and ".." entries.
1016 */
1017 if ((error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
1018 goto out;
1019 ip = VTOI(tvp);
1020 ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1021 ip->i_e2fs_gid = dp->i_e2fs_gid;
1022 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1023 ip->i_e2fs_mode = dmode;
1024 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1025 ip->i_e2fs_nlink = 2;
1026 error = VOP_UPDATE(tvp, NULL, NULL, 1);
1027
1028 /*
1029 * Bump link count in parent directory
1030 * to reflect work done below. Should
1031 * be done before reference is created
1032 * so reparation is possible if we crash.
1033 */
1034 dp->i_e2fs_nlink++;
1035 dp->i_flag |= IN_CHANGE;
1036 if ((error = VOP_UPDATE(dvp, NULL, NULL, 1)) != 0)
1037 goto bad;
1038
1039 /* Initialize directory with "." and ".." from static template. */
1040 memset(&dirtemplate, 0, sizeof(dirtemplate));
1041 dirtemplate.dot_ino = h2fs32(ip->i_number);
1042 dirtemplate.dot_reclen = h2fs16(12);
1043 dirtemplate.dot_namlen = h2fs16(1);
1044 dirtemplate.dot_name[0] = '.';
1045 dirtemplate.dotdot_ino = h2fs32(dp->i_number);
1046 dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
1047 dirtemplate.dotdot_namlen = h2fs16(2);
1048 dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
1049 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
1050 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1051 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, (struct proc *)0);
1052 if (error) {
1053 dp->i_e2fs_nlink--;
1054 dp->i_flag |= IN_CHANGE;
1055 goto bad;
1056 }
1057 if (VTOI(dvp)->i_e2fs->e2fs_bsize >
1058 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1059 panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
1060 else {
1061 ip->i_e2fs_size = VTOI(dvp)->i_e2fs->e2fs_bsize;
1062 ip->i_flag |= IN_CHANGE;
1063 }
1064
1065 /* Directory set up, now install it's entry in the parent directory. */
1066 error = ext2fs_direnter(ip, dvp, cnp);
1067 if (error != 0) {
1068 dp->i_e2fs_nlink--;
1069 dp->i_flag |= IN_CHANGE;
1070 }
1071 bad:
1072 /*
1073 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1074 * for us because we set the link count to 0.
1075 */
1076 if (error) {
1077 ip->i_e2fs_nlink = 0;
1078 ip->i_flag |= IN_CHANGE;
1079 vput(tvp);
1080 } else
1081 *ap->a_vpp = tvp;
1082 out:
1083 FREE(cnp->cn_pnbuf, M_NAMEI);
1084 vput(dvp);
1085 return (error);
1086 }
1087
1088 /*
1089 * Rmdir system call.
1090 */
1091 int
1092 ext2fs_rmdir(v)
1093 void *v;
1094 {
1095 struct vop_rmdir_args /* {
1096 struct vnode *a_dvp;
1097 struct vnode *a_vp;
1098 struct componentname *a_cnp;
1099 } */ *ap = v;
1100 register struct vnode *vp = ap->a_vp;
1101 register struct vnode *dvp = ap->a_dvp;
1102 register struct componentname *cnp = ap->a_cnp;
1103 register struct inode *ip, *dp;
1104 int error;
1105
1106 ip = VTOI(vp);
1107 dp = VTOI(dvp);
1108 /*
1109 * No rmdir "." please.
1110 */
1111 if (dp == ip) {
1112 vrele(dvp);
1113 vput(vp);
1114 return (EINVAL);
1115 }
1116 /*
1117 * Verify the directory is empty (and valid).
1118 * (Rmdir ".." won't be valid since
1119 * ".." will contain a reference to
1120 * the current directory and thus be
1121 * non-empty.)
1122 */
1123 error = 0;
1124 if (ip->i_e2fs_nlink != 2 ||
1125 !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1126 error = ENOTEMPTY;
1127 goto out;
1128 }
1129 if ((dp->i_e2fs_flags & EXT2_APPEND) ||
1130 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
1131 error = EPERM;
1132 goto out;
1133 }
1134 /*
1135 * Delete reference to directory before purging
1136 * inode. If we crash in between, the directory
1137 * will be reattached to lost+found,
1138 */
1139 error = ext2fs_dirremove(dvp, cnp);
1140 if (error != 0)
1141 goto out;
1142 dp->i_e2fs_nlink--;
1143 dp->i_flag |= IN_CHANGE;
1144 cache_purge(dvp);
1145 vput(dvp);
1146 dvp = NULL;
1147 /*
1148 * Truncate inode. The only stuff left
1149 * in the directory is "." and "..". The
1150 * "." reference is inconsequential since
1151 * we're quashing it. The ".." reference
1152 * has already been adjusted above. We've
1153 * removed the "." reference and the reference
1154 * in the parent directory, but there may be
1155 * other hard links so decrement by 2 and
1156 * worry about them later.
1157 */
1158 ip->i_e2fs_nlink -= 2;
1159 error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1160 cnp->cn_proc);
1161 cache_purge(ITOV(ip));
1162 out:
1163 if (dvp)
1164 vput(dvp);
1165 vput(vp);
1166 return (error);
1167 }
1168
1169 /*
1170 * symlink -- make a symbolic link
1171 */
1172 int
1173 ext2fs_symlink(v)
1174 void *v;
1175 {
1176 struct vop_symlink_args /* {
1177 struct vnode *a_dvp;
1178 struct vnode **a_vpp;
1179 struct componentname *a_cnp;
1180 struct vattr *a_vap;
1181 char *a_target;
1182 } */ *ap = v;
1183 register struct vnode *vp, **vpp = ap->a_vpp;
1184 register struct inode *ip;
1185 int len, error;
1186
1187 error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1188 vpp, ap->a_cnp);
1189 if (error)
1190 return (error);
1191 vp = *vpp;
1192 len = strlen(ap->a_target);
1193 if (len < vp->v_mount->mnt_maxsymlinklen) {
1194 ip = VTOI(vp);
1195 memcpy((char *)ip->i_din.e2fs_din.e2di_shortlink, ap->a_target, len);
1196 ip->i_e2fs_size = len;
1197 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1198 } else
1199 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1200 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1201 (size_t *)0, (struct proc *)0);
1202 vput(vp);
1203 return (error);
1204 }
1205
1206 /*
1207 * Return target name of a symbolic link
1208 */
1209 int
1210 ext2fs_readlink(v)
1211 void *v;
1212 {
1213 struct vop_readlink_args /* {
1214 struct vnode *a_vp;
1215 struct uio *a_uio;
1216 struct ucred *a_cred;
1217 } */ *ap = v;
1218 register struct vnode *vp = ap->a_vp;
1219 register struct inode *ip = VTOI(vp);
1220 int isize;
1221
1222 isize = ip->i_e2fs_size;
1223 if (isize < vp->v_mount->mnt_maxsymlinklen ||
1224 (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
1225 uiomove((char *)ip->i_din.e2fs_din.e2di_shortlink, isize, ap->a_uio);
1226 return (0);
1227 }
1228 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1229 }
1230
1231 /*
1232 * Advisory record locking support
1233 */
1234 int
1235 ext2fs_advlock(v)
1236 void *v;
1237 {
1238 struct vop_advlock_args /* {
1239 struct vnode *a_vp;
1240 caddr_t a_id;
1241 int a_op;
1242 struct flock *a_fl;
1243 int a_flags;
1244 } */ *ap = v;
1245 register struct inode *ip = VTOI(ap->a_vp);
1246
1247 return (lf_advlock(&ip->i_lockf, ip->i_e2fs_size, ap->a_id, ap->a_op,
1248 ap->a_fl, ap->a_flags));
1249 }
1250
1251 /*
1252 * Initialize the vnode associated with a new inode, handle aliased
1253 * vnodes.
1254 */
1255 int
1256 ext2fs_vinit(mntp, specops, fifoops, vpp)
1257 struct mount *mntp;
1258 int (**specops) __P((void *));
1259 int (**fifoops) __P((void *));
1260 struct vnode **vpp;
1261 {
1262 struct inode *ip;
1263 struct vnode *vp, *nvp;
1264
1265 vp = *vpp;
1266 ip = VTOI(vp);
1267 switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
1268 case VCHR:
1269 case VBLK:
1270 vp->v_op = specops;
1271 if ((nvp = checkalias(vp,
1272 fs2h32(ip->i_din.e2fs_din.e2di_rdev), mntp)) != NULL) {
1273 /*
1274 * Discard unneeded vnode, but save its inode.
1275 */
1276 nvp->v_data = vp->v_data;
1277 vp->v_data = NULL;
1278 vp->v_op = spec_vnodeop_p;
1279 vput(vp);
1280 vgone(vp);
1281 lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
1282 /*
1283 * Reinitialize aliased inode.
1284 */
1285 vp = nvp;
1286 ip->i_vnode = vp;
1287 }
1288 break;
1289 case VFIFO:
1290 vp->v_op = fifoops;
1291 break;
1292 case VNON:
1293 case VBAD:
1294 case VSOCK:
1295 case VLNK:
1296 case VDIR:
1297 case VREG:
1298 break;
1299 }
1300 if (ip->i_number == ROOTINO)
1301 vp->v_flag |= VROOT;
1302 /*
1303 * Initialize modrev times
1304 */
1305 SETHIGH(ip->i_modrev, mono_time.tv_sec);
1306 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294);
1307 *vpp = vp;
1308 return (0);
1309 }
1310
1311 /*
1312 * Allocate a new inode.
1313 */
1314 int
1315 ext2fs_makeinode(mode, dvp, vpp, cnp)
1316 int mode;
1317 struct vnode *dvp;
1318 struct vnode **vpp;
1319 struct componentname *cnp;
1320 {
1321 register struct inode *ip, *pdir;
1322 struct vnode *tvp;
1323 int error;
1324
1325 pdir = VTOI(dvp);
1326 #ifdef DIAGNOSTIC
1327 if ((cnp->cn_flags & HASBUF) == 0)
1328 panic("ext2fs_makeinode: no name");
1329 #endif
1330 *vpp = NULL;
1331 if ((mode & IFMT) == 0)
1332 mode |= IFREG;
1333
1334 if ((error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
1335 free(cnp->cn_pnbuf, M_NAMEI);
1336 vput(dvp);
1337 return (error);
1338 }
1339 ip = VTOI(tvp);
1340 ip->i_e2fs_gid = pdir->i_e2fs_gid;
1341 ip->i_e2fs_uid = cnp->cn_cred->cr_uid;
1342 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1343 ip->i_e2fs_mode = mode;
1344 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1345 ip->i_e2fs_nlink = 1;
1346 if ((ip->i_e2fs_mode & ISGID) &&
1347 !groupmember(ip->i_e2fs_gid, cnp->cn_cred) &&
1348 suser(cnp->cn_cred, NULL))
1349 ip->i_e2fs_mode &= ~ISGID;
1350
1351 /*
1352 * Make sure inode goes to disk before directory entry.
1353 */
1354 if ((error = VOP_UPDATE(tvp, NULL, NULL, 1)) != 0)
1355 goto bad;
1356 error = ext2fs_direnter(ip, dvp, cnp);
1357 if (error != 0)
1358 goto bad;
1359 if ((cnp->cn_flags & SAVESTART) == 0)
1360 FREE(cnp->cn_pnbuf, M_NAMEI);
1361 vput(dvp);
1362 *vpp = tvp;
1363 return (0);
1364
1365 bad:
1366 /*
1367 * Write error occurred trying to update the inode
1368 * or the directory so must deallocate the inode.
1369 */
1370 free(cnp->cn_pnbuf, M_NAMEI);
1371 vput(dvp);
1372 ip->i_e2fs_nlink = 0;
1373 ip->i_flag |= IN_CHANGE;
1374 vput(tvp);
1375 return (error);
1376 }
1377
1378 /*
1379 * Reclaim an inode so that it can be used for other purposes.
1380 */
1381 int
1382 ext2fs_reclaim(v)
1383 void *v;
1384 {
1385 struct vop_reclaim_args /* {
1386 struct vnode *a_vp;
1387 } */ *ap = v;
1388 register struct vnode *vp = ap->a_vp;
1389 struct inode *ip;
1390 extern int prtactive;
1391
1392 if (prtactive && vp->v_usecount != 0)
1393 vprint("ext2fs_reclaim: pushing active", vp);
1394 /*
1395 * Remove the inode from its hash chain.
1396 */
1397 ip = VTOI(vp);
1398 ufs_ihashrem(ip);
1399 /*
1400 * Purge old data structures associated with the inode.
1401 */
1402 cache_purge(vp);
1403 if (ip->i_devvp) {
1404 vrele(ip->i_devvp);
1405 ip->i_devvp = 0;
1406 }
1407
1408 pool_put(&ext2fs_inode_pool, vp->v_data);
1409 vp->v_data = NULL;
1410 return (0);
1411 }
1412
1413 /* Global vfs data structures for ext2fs. */
1414 int (**ext2fs_vnodeop_p) __P((void *));
1415 struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1416 { &vop_default_desc, vn_default_error },
1417 { &vop_lookup_desc, ext2fs_lookup }, /* lookup */
1418 { &vop_create_desc, ext2fs_create }, /* create */
1419 { &vop_mknod_desc, ext2fs_mknod }, /* mknod */
1420 { &vop_open_desc, ext2fs_open }, /* open */
1421 { &vop_close_desc, ufs_close }, /* close */
1422 { &vop_access_desc, ext2fs_access }, /* access */
1423 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1424 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1425 { &vop_read_desc, ext2fs_read }, /* read */
1426 { &vop_write_desc, ext2fs_write }, /* write */
1427 { &vop_lease_desc, ufs_lease_check }, /* lease */
1428 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
1429 { &vop_poll_desc, ufs_poll }, /* poll */
1430 { &vop_revoke_desc, ufs_revoke }, /* revoke */
1431 { &vop_mmap_desc, ufs_mmap }, /* mmap */
1432 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1433 { &vop_seek_desc, ufs_seek }, /* seek */
1434 { &vop_remove_desc, ext2fs_remove }, /* remove */
1435 { &vop_link_desc, ext2fs_link }, /* link */
1436 { &vop_rename_desc, ext2fs_rename }, /* rename */
1437 { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */
1438 { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */
1439 { &vop_symlink_desc, ext2fs_symlink }, /* symlink */
1440 { &vop_readdir_desc, ext2fs_readdir }, /* readdir */
1441 { &vop_readlink_desc, ext2fs_readlink }, /* readlink */
1442 { &vop_abortop_desc, ufs_abortop }, /* abortop */
1443 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1444 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1445 { &vop_lock_desc, ufs_lock }, /* lock */
1446 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1447 { &vop_bmap_desc, ext2fs_bmap }, /* bmap */
1448 { &vop_strategy_desc, ufs_strategy }, /* strategy */
1449 { &vop_print_desc, ufs_print }, /* print */
1450 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1451 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
1452 { &vop_advlock_desc, ext2fs_advlock }, /* advlock */
1453 { &vop_blkatoff_desc, ext2fs_blkatoff }, /* blkatoff */
1454 { &vop_valloc_desc, ext2fs_valloc }, /* valloc */
1455 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1456 { &vop_truncate_desc, ext2fs_truncate }, /* truncate */
1457 { &vop_update_desc, ext2fs_update }, /* update */
1458 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1459 { (struct vnodeop_desc*)NULL, (int(*) __P((void*)))NULL }
1460 };
1461 struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
1462 { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
1463
1464 int (**ext2fs_specop_p) __P((void *));
1465 struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1466 { &vop_default_desc, vn_default_error },
1467 { &vop_lookup_desc, spec_lookup }, /* lookup */
1468 { &vop_create_desc, spec_create }, /* create */
1469 { &vop_mknod_desc, spec_mknod }, /* mknod */
1470 { &vop_open_desc, spec_open }, /* open */
1471 { &vop_close_desc, ufsspec_close }, /* close */
1472 { &vop_access_desc, ext2fs_access }, /* access */
1473 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1474 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1475 { &vop_read_desc, ufsspec_read }, /* read */
1476 { &vop_write_desc, ufsspec_write }, /* write */
1477 { &vop_lease_desc, spec_lease_check }, /* lease */
1478 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
1479 { &vop_poll_desc, spec_poll }, /* poll */
1480 { &vop_revoke_desc, spec_revoke }, /* revoke */
1481 { &vop_mmap_desc, spec_mmap }, /* mmap */
1482 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1483 { &vop_seek_desc, spec_seek }, /* seek */
1484 { &vop_remove_desc, spec_remove }, /* remove */
1485 { &vop_link_desc, spec_link }, /* link */
1486 { &vop_rename_desc, spec_rename }, /* rename */
1487 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
1488 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
1489 { &vop_symlink_desc, spec_symlink }, /* symlink */
1490 { &vop_readdir_desc, spec_readdir }, /* readdir */
1491 { &vop_readlink_desc, spec_readlink }, /* readlink */
1492 { &vop_abortop_desc, spec_abortop }, /* abortop */
1493 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1494 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1495 { &vop_lock_desc, ufs_lock }, /* lock */
1496 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1497 { &vop_bmap_desc, spec_bmap }, /* bmap */
1498 { &vop_strategy_desc, spec_strategy }, /* strategy */
1499 { &vop_print_desc, ufs_print }, /* print */
1500 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1501 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
1502 { &vop_advlock_desc, spec_advlock }, /* advlock */
1503 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
1504 { &vop_valloc_desc, spec_valloc }, /* valloc */
1505 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1506 { &vop_truncate_desc, spec_truncate }, /* truncate */
1507 { &vop_update_desc, ext2fs_update }, /* update */
1508 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1509 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1510 };
1511 struct vnodeopv_desc ext2fs_specop_opv_desc =
1512 { &ext2fs_specop_p, ext2fs_specop_entries };
1513
1514 int (**ext2fs_fifoop_p) __P((void *));
1515 struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1516 { &vop_default_desc, vn_default_error },
1517 { &vop_lookup_desc, fifo_lookup }, /* lookup */
1518 { &vop_create_desc, fifo_create }, /* create */
1519 { &vop_mknod_desc, fifo_mknod }, /* mknod */
1520 { &vop_open_desc, fifo_open }, /* open */
1521 { &vop_close_desc, ufsfifo_close }, /* close */
1522 { &vop_access_desc, ext2fs_access }, /* access */
1523 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1524 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1525 { &vop_read_desc, ufsfifo_read }, /* read */
1526 { &vop_write_desc, ufsfifo_write }, /* write */
1527 { &vop_lease_desc, fifo_lease_check }, /* lease */
1528 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */
1529 { &vop_poll_desc, fifo_poll }, /* poll */
1530 { &vop_revoke_desc, fifo_revoke }, /* revoke */
1531 { &vop_mmap_desc, fifo_mmap }, /* mmap */
1532 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1533 { &vop_seek_desc, fifo_seek }, /* seek */
1534 { &vop_remove_desc, fifo_remove }, /* remove */
1535 { &vop_link_desc, fifo_link }, /* link */
1536 { &vop_rename_desc, fifo_rename }, /* rename */
1537 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
1538 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
1539 { &vop_symlink_desc, fifo_symlink }, /* symlink */
1540 { &vop_readdir_desc, fifo_readdir }, /* readdir */
1541 { &vop_readlink_desc, fifo_readlink }, /* readlink */
1542 { &vop_abortop_desc, fifo_abortop }, /* abortop */
1543 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1544 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1545 { &vop_lock_desc, ufs_lock }, /* lock */
1546 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1547 { &vop_bmap_desc, fifo_bmap }, /* bmap */
1548 { &vop_strategy_desc, fifo_strategy }, /* strategy */
1549 { &vop_print_desc, ufs_print }, /* print */
1550 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1551 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
1552 { &vop_advlock_desc, fifo_advlock }, /* advlock */
1553 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */
1554 { &vop_valloc_desc, fifo_valloc }, /* valloc */
1555 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */
1556 { &vop_truncate_desc, fifo_truncate }, /* truncate */
1557 { &vop_update_desc, ext2fs_update }, /* update */
1558 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1559 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
1560 };
1561 struct vnodeopv_desc ext2fs_fifoop_opv_desc =
1562 { &ext2fs_fifoop_p, ext2fs_fifoop_entries };
1563