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