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