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