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