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