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