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