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