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