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