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