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