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