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