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