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