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