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