ext2fs_vnops.c revision 1.98 1 /* $NetBSD: ext2fs_vnops.c,v 1.98 2011/04/24 21:35:30 rmind 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.98 2011/04/24 21:35:30 rmind 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);
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 ext2fs_set_inode_guid(ip);
507 ip->i_flag |= IN_CHANGE;
508 }
509 if (ouid != uid && kauth_authorize_generic(cred,
510 KAUTH_GENERIC_ISSUSER, NULL) != 0)
511 ip->i_e2fs_mode &= ~ISUID;
512 if (ogid != gid && kauth_authorize_generic(cred,
513 KAUTH_GENERIC_ISSUSER, NULL) != 0)
514 ip->i_e2fs_mode &= ~ISGID;
515 return (0);
516 }
517
518 int
519 ext2fs_remove(void *v)
520 {
521 struct vop_remove_args /* {
522 struct vnode *a_dvp;
523 struct vnode *a_vp;
524 struct componentname *a_cnp;
525 } */ *ap = v;
526 struct inode *ip;
527 struct vnode *vp = ap->a_vp;
528 struct vnode *dvp = ap->a_dvp;
529 int error;
530
531 ip = VTOI(vp);
532 if (vp->v_type == VDIR ||
533 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
534 (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
535 error = EPERM;
536 } else {
537 error = ext2fs_dirremove(dvp, ap->a_cnp);
538 if (error == 0) {
539 ip->i_e2fs_nlink--;
540 ip->i_flag |= IN_CHANGE;
541 }
542 }
543
544 VN_KNOTE(vp, NOTE_DELETE);
545 VN_KNOTE(dvp, NOTE_WRITE);
546 if (dvp == vp)
547 vrele(vp);
548 else
549 vput(vp);
550 vput(dvp);
551 return (error);
552 }
553
554 /*
555 * ext2fs_link: create hard link.
556 */
557 int
558 ext2fs_link(void *v)
559 {
560 struct vop_link_args /* {
561 struct vnode *a_dvp;
562 struct vnode *a_vp;
563 struct componentname *a_cnp;
564 } */ *ap = v;
565 struct vnode *dvp = ap->a_dvp;
566 struct vnode *vp = ap->a_vp;
567 struct componentname *cnp = ap->a_cnp;
568 struct inode *ip;
569 int error;
570
571 KASSERT(dvp != vp);
572 KASSERT(vp->v_type != VDIR);
573 KASSERT(dvp->v_mount == vp->v_mount);
574
575 error = vn_lock(vp, LK_EXCLUSIVE);
576 if (error) {
577 VOP_ABORTOP(dvp, cnp);
578 goto out2;
579 }
580 ip = VTOI(vp);
581 if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) {
582 VOP_ABORTOP(dvp, cnp);
583 error = EMLINK;
584 goto out1;
585 }
586 if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
587 VOP_ABORTOP(dvp, cnp);
588 error = EPERM;
589 goto out1;
590 }
591 ip->i_e2fs_nlink++;
592 ip->i_flag |= IN_CHANGE;
593 error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
594 if (!error)
595 error = ext2fs_direnter(ip, dvp, cnp);
596 if (error) {
597 ip->i_e2fs_nlink--;
598 ip->i_flag |= IN_CHANGE;
599 }
600 out1:
601 VOP_UNLOCK(vp);
602 out2:
603 VN_KNOTE(vp, NOTE_LINK);
604 VN_KNOTE(dvp, NOTE_WRITE);
605 vput(dvp);
606 return (error);
607 }
608
609 /*
610 * Rename system call.
611 * rename("foo", "bar");
612 * is essentially
613 * unlink("bar");
614 * link("foo", "bar");
615 * unlink("foo");
616 * but ``atomically''. Can't do full commit without saving state in the
617 * inode on disk which isn't feasible at this time. Best we can do is
618 * always guarantee the target exists.
619 *
620 * Basic algorithm is:
621 *
622 * 1) Bump link count on source while we're linking it to the
623 * target. This also ensure the inode won't be deleted out
624 * from underneath us while we work (it may be truncated by
625 * a concurrent `trunc' or `open' for creation).
626 * 2) Link source to destination. If destination already exists,
627 * delete it first.
628 * 3) Unlink source reference to inode if still around. If a
629 * directory was moved and the parent of the destination
630 * is different from the source, patch the ".." entry in the
631 * directory.
632 */
633 int
634 ext2fs_rename(void *v)
635 {
636 struct vop_rename_args /* {
637 struct vnode *a_fdvp;
638 struct vnode *a_fvp;
639 struct componentname *a_fcnp;
640 struct vnode *a_tdvp;
641 struct vnode *a_tvp;
642 struct componentname *a_tcnp;
643 } */ *ap = v;
644 struct vnode *tvp = ap->a_tvp;
645 struct vnode *tdvp = ap->a_tdvp;
646 struct vnode *fvp = ap->a_fvp;
647 struct vnode *fdvp = ap->a_fdvp;
648 struct componentname *tcnp = ap->a_tcnp;
649 struct componentname *fcnp = ap->a_fcnp;
650 struct inode *ip, *xp, *dp;
651 struct ext2fs_dirtemplate dirbuf;
652 int doingdirectory = 0, oldparent = 0, newparent = 0;
653 int error = 0;
654 u_char namlen;
655
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);
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, 0))) {
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);
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);
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);
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);
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);
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);
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 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
797 if ((error = relookup(tdvp, &tvp, tcnp, 0)) != 0) {
798 vput(tdvp);
799 goto out;
800 }
801 dp = VTOI(tdvp);
802 xp = NULL;
803 if (tvp)
804 xp = VTOI(tvp);
805 }
806 /*
807 * 2) If target doesn't exist, link the target
808 * to the source and unlink the source.
809 * Otherwise, rewrite the target directory
810 * entry to reference the source inode and
811 * expunge the original entry's existence.
812 */
813 if (xp == NULL) {
814 if (dp->i_dev != ip->i_dev)
815 panic("rename: EXDEV");
816 /*
817 * Account for ".." in new directory.
818 * When source and destination have the same
819 * parent we don't fool with the link count.
820 */
821 if (doingdirectory && newparent) {
822 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
823 error = EMLINK;
824 goto bad;
825 }
826 dp->i_e2fs_nlink++;
827 dp->i_flag |= IN_CHANGE;
828 if ((error = ext2fs_update(tdvp, NULL, NULL,
829 UPDATE_WAIT)) != 0)
830 goto bad;
831 }
832 error = ext2fs_direnter(ip, tdvp, tcnp);
833 if (error != 0) {
834 if (doingdirectory && newparent) {
835 dp->i_e2fs_nlink--;
836 dp->i_flag |= IN_CHANGE;
837 (void)ext2fs_update(tdvp, NULL, NULL,
838 UPDATE_WAIT);
839 }
840 goto bad;
841 }
842 VN_KNOTE(tdvp, NOTE_WRITE);
843 vput(tdvp);
844 } else {
845 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
846 panic("rename: EXDEV");
847 /*
848 * Short circuit rename(foo, foo).
849 */
850 if (xp->i_number == ip->i_number)
851 panic("rename: same file");
852 /*
853 * If the parent directory is "sticky", then the user must
854 * own the parent directory, or the destination of the rename,
855 * otherwise the destination may not be changed (except by
856 * root). This implements append-only directories.
857 */
858 if ((dp->i_e2fs_mode & S_ISTXT) &&
859 kauth_authorize_generic(tcnp->cn_cred,
860 KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
861 kauth_cred_geteuid(tcnp->cn_cred) != dp->i_uid &&
862 xp->i_uid != kauth_cred_geteuid(tcnp->cn_cred)) {
863 error = EPERM;
864 goto bad;
865 }
866 /*
867 * Target must be empty if a directory and have no links
868 * to it. Also, ensure source and target are compatible
869 * (both directories, or both not directories).
870 */
871 if ((xp->i_e2fs_mode & IFMT) == IFDIR) {
872 if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
873 xp->i_e2fs_nlink > 2) {
874 error = ENOTEMPTY;
875 goto bad;
876 }
877 if (!doingdirectory) {
878 error = ENOTDIR;
879 goto bad;
880 }
881 cache_purge(tdvp);
882 } else if (doingdirectory) {
883 error = EISDIR;
884 goto bad;
885 }
886 error = ext2fs_dirrewrite(dp, ip, tcnp);
887 if (error != 0)
888 goto bad;
889 /*
890 * If the target directory is in the same
891 * directory as the source directory,
892 * decrement the link count on the parent
893 * of the target directory.
894 */
895 if (doingdirectory && !newparent) {
896 dp->i_e2fs_nlink--;
897 dp->i_flag |= IN_CHANGE;
898 }
899 /*
900 * Adjust the link count of the target to
901 * reflect the dirrewrite above. If this is
902 * a directory it is empty and there are
903 * no links to it, so we can squash the inode and
904 * any space associated with it. We disallowed
905 * renaming over top of a directory with links to
906 * it above, as the remaining link would point to
907 * a directory without "." or ".." entries.
908 */
909 xp->i_e2fs_nlink--;
910 if (doingdirectory) {
911 if (--xp->i_e2fs_nlink != 0)
912 panic("rename: linked directory");
913 error = ext2fs_truncate(tvp, (off_t)0, IO_SYNC,
914 tcnp->cn_cred);
915 }
916 xp->i_flag |= IN_CHANGE;
917 VN_KNOTE(tdvp, NOTE_WRITE);
918 vput(tdvp);
919 VN_KNOTE(tvp, NOTE_DELETE);
920 vput(tvp);
921 xp = NULL;
922 }
923
924 /*
925 * 3) Unlink the source.
926 */
927 fcnp->cn_flags &= ~(MODMASK);
928 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
929 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
930 if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
931 vput(fdvp);
932 vrele(ap->a_fvp);
933 return (error);
934 }
935 if (fvp != NULL) {
936 xp = VTOI(fvp);
937 dp = VTOI(fdvp);
938 } else {
939 /*
940 * From name has disappeared.
941 */
942 if (doingdirectory)
943 panic("ext2fs_rename: lost dir entry");
944 vrele(ap->a_fvp);
945 return (0);
946 }
947 /*
948 * Ensure that the directory entry still exists and has not
949 * changed while the new name has been entered. If the source is
950 * a file then the entry may have been unlinked or renamed. In
951 * either case there is no further work to be done. If the source
952 * is a directory then it cannot have been rmdir'ed; its link
953 * count of three would cause a rmdir to fail with ENOTEMPTY.
954 * The IRENAME flag ensures that it cannot be moved by another
955 * rename.
956 */
957 if (xp != ip) {
958 if (doingdirectory)
959 panic("ext2fs_rename: lost dir entry");
960 } else {
961 /*
962 * If the source is a directory with a
963 * new parent, the link count of the old
964 * parent directory must be decremented
965 * and ".." set to point to the new parent.
966 */
967 if (doingdirectory && newparent) {
968 KASSERT(dp != NULL);
969 dp->i_e2fs_nlink--;
970 dp->i_flag |= IN_CHANGE;
971 error = vn_rdwr(UIO_READ, fvp, (void *)&dirbuf,
972 sizeof (struct ext2fs_dirtemplate), (off_t)0,
973 UIO_SYSSPACE, IO_NODELOCKED,
974 tcnp->cn_cred, (size_t *)0, NULL);
975 if (error == 0) {
976 namlen = dirbuf.dotdot_namlen;
977 if (namlen != 2 ||
978 dirbuf.dotdot_name[0] != '.' ||
979 dirbuf.dotdot_name[1] != '.') {
980 ufs_dirbad(xp, (doff_t)12,
981 "ext2fs_rename: mangled dir");
982 } else {
983 dirbuf.dotdot_ino = h2fs32(newparent);
984 (void) vn_rdwr(UIO_WRITE, fvp,
985 (void *)&dirbuf,
986 sizeof (struct dirtemplate),
987 (off_t)0, UIO_SYSSPACE,
988 IO_NODELOCKED|IO_SYNC,
989 tcnp->cn_cred, (size_t *)0,
990 NULL);
991 cache_purge(fdvp);
992 }
993 }
994 }
995 error = ext2fs_dirremove(fdvp, fcnp);
996 if (!error) {
997 xp->i_e2fs_nlink--;
998 xp->i_flag |= IN_CHANGE;
999 }
1000 xp->i_flag &= ~IN_RENAME;
1001 }
1002 VN_KNOTE(fvp, NOTE_RENAME);
1003 if (dp)
1004 vput(fdvp);
1005 if (xp)
1006 vput(fvp);
1007 vrele(ap->a_fvp);
1008 return (error);
1009
1010 bad:
1011 if (xp)
1012 vput(ITOV(xp));
1013 vput(ITOV(dp));
1014 out:
1015 if (doingdirectory)
1016 ip->i_flag &= ~IN_RENAME;
1017 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1018 ip->i_e2fs_nlink--;
1019 ip->i_flag |= IN_CHANGE;
1020 vput(fvp);
1021 } else
1022 vrele(fvp);
1023 vrele(fdvp);
1024 return (error);
1025 }
1026
1027 /*
1028 * Mkdir system call
1029 */
1030 int
1031 ext2fs_mkdir(void *v)
1032 {
1033 struct vop_mkdir_args /* {
1034 struct vnode *a_dvp;
1035 struct vnode **a_vpp;
1036 struct componentname *a_cnp;
1037 struct vattr *a_vap;
1038 } */ *ap = v;
1039 struct vnode *dvp = ap->a_dvp;
1040 struct vattr *vap = ap->a_vap;
1041 struct componentname *cnp = ap->a_cnp;
1042 struct inode *ip, *dp = VTOI(dvp);
1043 struct vnode *tvp;
1044 struct ext2fs_dirtemplate dirtemplate;
1045 int error, dmode;
1046
1047 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) {
1048 error = EMLINK;
1049 goto out;
1050 }
1051 dmode = vap->va_mode & ACCESSPERMS;
1052 dmode |= IFDIR;
1053 /*
1054 * Must simulate part of ext2fs_makeinode here to acquire the inode,
1055 * but not have it entered in the parent directory. The entry is
1056 * made later after writing "." and ".." entries.
1057 */
1058 if ((error = ext2fs_valloc(dvp, dmode, cnp->cn_cred, &tvp)) != 0)
1059 goto out;
1060 ip = VTOI(tvp);
1061 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
1062 ip->i_e2fs_uid = ip->i_uid & 0xffff;
1063 ip->i_e2fs_gid = dp->i_e2fs_gid;
1064 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
1065 ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
1066 ip->i_e2fs_gid_high = dp->i_e2fs_gid_high;
1067 } else {
1068 ip->i_e2fs_uid_high = 0;
1069 ip->i_e2fs_gid_high = 0;
1070 }
1071 ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
1072 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1073 ip->i_e2fs_mode = dmode;
1074 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1075 ip->i_e2fs_nlink = 2;
1076
1077 /*
1078 * Bump link count in parent directory
1079 * to reflect work done below. Should
1080 * be done before reference is created
1081 * so reparation is possible if we crash.
1082 */
1083 dp->i_e2fs_nlink++;
1084 dp->i_flag |= IN_CHANGE;
1085 if ((error = ext2fs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
1086 goto bad;
1087
1088 /* Initialize directory with "." and ".." from static template. */
1089 memset(&dirtemplate, 0, sizeof(dirtemplate));
1090 dirtemplate.dot_ino = h2fs32(ip->i_number);
1091 dirtemplate.dot_reclen = h2fs16(12);
1092 dirtemplate.dot_namlen = 1;
1093 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
1094 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
1095 dirtemplate.dot_type = EXT2_FT_DIR;
1096 }
1097 dirtemplate.dot_name[0] = '.';
1098 dirtemplate.dotdot_ino = h2fs32(dp->i_number);
1099 dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
1100 dirtemplate.dotdot_namlen = 2;
1101 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
1102 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
1103 dirtemplate.dotdot_type = EXT2_FT_DIR;
1104 }
1105 dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
1106 error = vn_rdwr(UIO_WRITE, tvp, (void *)&dirtemplate,
1107 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
1108 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (size_t *)0, NULL);
1109 if (error) {
1110 dp->i_e2fs_nlink--;
1111 dp->i_flag |= IN_CHANGE;
1112 goto bad;
1113 }
1114 if (VTOI(dvp)->i_e2fs->e2fs_bsize > dvp->v_mount->mnt_stat.f_bsize)
1115 panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
1116 else {
1117 error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
1118 if (error) {
1119 dp->i_e2fs_nlink--;
1120 dp->i_flag |= IN_CHANGE;
1121 goto bad;
1122 }
1123 ip->i_flag |= IN_CHANGE;
1124 uvm_vnp_setsize(tvp, ext2fs_size(ip));
1125 }
1126
1127 /* Directory set up, now install it's entry in the parent directory. */
1128 error = ext2fs_direnter(ip, dvp, cnp);
1129 if (error != 0) {
1130 dp->i_e2fs_nlink--;
1131 dp->i_flag |= IN_CHANGE;
1132 }
1133 bad:
1134 /*
1135 * No need to do an explicit ext2fs_truncate here, vrele will do this
1136 * for us because we set the link count to 0.
1137 */
1138 if (error) {
1139 ip->i_e2fs_nlink = 0;
1140 ip->i_flag |= IN_CHANGE;
1141 vput(tvp);
1142 } else {
1143 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1144 *ap->a_vpp = tvp;
1145 }
1146 out:
1147 vput(dvp);
1148 return (error);
1149 }
1150
1151 /*
1152 * Rmdir system call.
1153 */
1154 int
1155 ext2fs_rmdir(void *v)
1156 {
1157 struct vop_rmdir_args /* {
1158 struct vnode *a_dvp;
1159 struct vnode *a_vp;
1160 struct componentname *a_cnp;
1161 } */ *ap = v;
1162 struct vnode *vp = ap->a_vp;
1163 struct vnode *dvp = ap->a_dvp;
1164 struct componentname *cnp = ap->a_cnp;
1165 struct inode *ip, *dp;
1166 int error;
1167
1168 ip = VTOI(vp);
1169 dp = VTOI(dvp);
1170 /*
1171 * No rmdir "." please.
1172 */
1173 if (dp == ip) {
1174 vrele(dvp);
1175 vput(vp);
1176 return (EINVAL);
1177 }
1178 /*
1179 * Verify the directory is empty (and valid).
1180 * (Rmdir ".." won't be valid since
1181 * ".." will contain a reference to
1182 * the current directory and thus be
1183 * non-empty.)
1184 */
1185 error = 0;
1186 if (ip->i_e2fs_nlink != 2 ||
1187 !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1188 error = ENOTEMPTY;
1189 goto out;
1190 }
1191 if ((dp->i_e2fs_flags & EXT2_APPEND) ||
1192 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
1193 error = EPERM;
1194 goto out;
1195 }
1196 /*
1197 * Delete reference to directory before purging
1198 * inode. If we crash in between, the directory
1199 * will be reattached to lost+found,
1200 */
1201 error = ext2fs_dirremove(dvp, cnp);
1202 if (error != 0)
1203 goto out;
1204 dp->i_e2fs_nlink--;
1205 dp->i_flag |= IN_CHANGE;
1206 VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
1207 cache_purge(dvp);
1208 vput(dvp);
1209 dvp = NULL;
1210 /*
1211 * Truncate inode. The only stuff left
1212 * in the directory is "." and "..". The
1213 * "." reference is inconsequential since
1214 * we're quashing it. The ".." reference
1215 * has already been adjusted above. We've
1216 * removed the "." reference and the reference
1217 * in the parent directory, but there may be
1218 * other hard links so decrement by 2 and
1219 * worry about them later.
1220 */
1221 ip->i_e2fs_nlink -= 2;
1222 error = ext2fs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
1223 cache_purge(ITOV(ip));
1224 out:
1225 VN_KNOTE(vp, NOTE_DELETE);
1226 if (dvp)
1227 vput(dvp);
1228 vput(vp);
1229 return (error);
1230 }
1231
1232 /*
1233 * symlink -- make a symbolic link
1234 */
1235 int
1236 ext2fs_symlink(void *v)
1237 {
1238 struct vop_symlink_args /* {
1239 struct vnode *a_dvp;
1240 struct vnode **a_vpp;
1241 struct componentname *a_cnp;
1242 struct vattr *a_vap;
1243 char *a_target;
1244 } */ *ap = v;
1245 struct vnode *vp, **vpp;
1246 struct inode *ip;
1247 int len, error;
1248
1249 vpp = ap->a_vpp;
1250 error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1251 vpp, ap->a_cnp);
1252 if (error)
1253 return (error);
1254 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
1255 vp = *vpp;
1256 len = strlen(ap->a_target);
1257 ip = VTOI(vp);
1258 if (len < ip->i_ump->um_maxsymlinklen) {
1259 memcpy((char *)ip->i_din.e2fs_din->e2di_shortlink, ap->a_target, len);
1260 error = ext2fs_setsize(ip, len);
1261 if (error)
1262 goto bad;
1263 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1264 uvm_vnp_setsize(vp, len);
1265 } else
1266 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1267 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred,
1268 (size_t *)0, NULL);
1269 bad:
1270 if (error)
1271 vput(vp);
1272 return (error);
1273 }
1274
1275 /*
1276 * Return target name of a symbolic link
1277 */
1278 int
1279 ext2fs_readlink(void *v)
1280 {
1281 struct vop_readlink_args /* {
1282 struct vnode *a_vp;
1283 struct uio *a_uio;
1284 kauth_cred_t a_cred;
1285 } */ *ap = v;
1286 struct vnode *vp = ap->a_vp;
1287 struct inode *ip = VTOI(vp);
1288 struct ufsmount *ump = ip->i_ump;
1289 int isize;
1290
1291 isize = ext2fs_size(ip);
1292 if (isize < ump->um_maxsymlinklen ||
1293 (ump->um_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) {
1294 uiomove((char *)ip->i_din.e2fs_din->e2di_shortlink, isize, ap->a_uio);
1295 return (0);
1296 }
1297 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1298 }
1299
1300 /*
1301 * Advisory record locking support
1302 */
1303 int
1304 ext2fs_advlock(void *v)
1305 {
1306 struct vop_advlock_args /* {
1307 struct vnode *a_vp;
1308 void * a_id;
1309 int a_op;
1310 struct flock *a_fl;
1311 int a_flags;
1312 } */ *ap = v;
1313 struct inode *ip = VTOI(ap->a_vp);
1314
1315 return lf_advlock(ap, &ip->i_lockf, ext2fs_size(ip));
1316 }
1317
1318 int
1319 ext2fs_fsync(void *v)
1320 {
1321 struct vop_fsync_args /* {
1322 struct vnode *a_vp;
1323 kauth_cred_t a_cred;
1324 int a_flags;
1325 off_t offlo;
1326 off_t offhi;
1327 struct proc *a_p;
1328 } */ *ap = v;
1329 struct vnode *vp = ap->a_vp;
1330 int wait;
1331 int error;
1332
1333 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1334
1335 if (vp->v_type == VBLK)
1336 spec_fsync(v);
1337 else
1338 vflushbuf(vp, wait);
1339 if ((ap->a_flags & FSYNC_DATAONLY) != 0)
1340 error = 0;
1341 else
1342 error = ext2fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
1343
1344 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
1345 int l = 0;
1346 error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
1347 curlwp->l_cred);
1348 }
1349
1350 return error;
1351 }
1352
1353 /*
1354 * Initialize the vnode associated with a new inode, handle aliased
1355 * vnodes.
1356 */
1357 int
1358 ext2fs_vinit(struct mount *mntp, int (**specops)(void *),
1359 int (**fifoops)(void *), struct vnode **vpp)
1360 {
1361 struct timeval tv;
1362 struct inode *ip;
1363 struct vnode *vp;
1364
1365 vp = *vpp;
1366 ip = VTOI(vp);
1367 switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
1368 case VCHR:
1369 case VBLK:
1370 vp->v_op = specops;
1371 spec_node_init(vp, fs2h32(ip->i_din.e2fs_din->e2di_rdev));
1372 break;
1373 case VFIFO:
1374 vp->v_op = fifoops;
1375 break;
1376 case VNON:
1377 case VBAD:
1378 case VSOCK:
1379 case VLNK:
1380 case VDIR:
1381 case VREG:
1382 break;
1383 }
1384 if (ip->i_number == ROOTINO)
1385 vp->v_vflag |= VV_ROOT;
1386 /*
1387 * Initialize modrev times
1388 */
1389 getmicrouptime(&tv);
1390 SETHIGH(ip->i_modrev, tv.tv_sec);
1391 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1392 *vpp = vp;
1393 return (0);
1394 }
1395
1396 /*
1397 * Allocate a new inode.
1398 */
1399 int
1400 ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1401 struct componentname *cnp)
1402 {
1403 struct inode *ip, *pdir;
1404 struct vnode *tvp;
1405 int error, ismember = 0;
1406
1407 pdir = VTOI(dvp);
1408 *vpp = NULL;
1409 if ((mode & IFMT) == 0)
1410 mode |= IFREG;
1411
1412 if ((error = ext2fs_valloc(dvp, mode, cnp->cn_cred, &tvp)) != 0) {
1413 vput(dvp);
1414 return (error);
1415 }
1416 ip = VTOI(tvp);
1417 ip->i_uid = kauth_cred_geteuid(cnp->cn_cred);
1418 ip->i_e2fs_uid = ip->i_uid & 0xffff;
1419 ip->i_e2fs_gid = pdir->i_e2fs_gid;
1420 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
1421 ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff;
1422 ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high;
1423 } else {
1424 ip->i_e2fs_uid_high = 0;
1425 ip->i_e2fs_gid_high = 0;
1426 }
1427 ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16);
1428 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1429 ip->i_e2fs_mode = mode;
1430 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1431 ip->i_e2fs_nlink = 1;
1432 if ((ip->i_e2fs_mode & ISGID) && (kauth_cred_ismember_gid(cnp->cn_cred,
1433 ip->i_gid, &ismember) != 0 || !ismember) &&
1434 kauth_authorize_generic(cnp->cn_cred, KAUTH_GENERIC_ISSUSER, NULL))
1435 ip->i_e2fs_mode &= ~ISGID;
1436
1437 /*
1438 * Make sure inode goes to disk before directory entry.
1439 */
1440 if ((error = ext2fs_update(tvp, NULL, NULL, UPDATE_WAIT)) != 0)
1441 goto bad;
1442 error = ext2fs_direnter(ip, dvp, cnp);
1443 if (error != 0)
1444 goto bad;
1445 vput(dvp);
1446 *vpp = tvp;
1447 return (0);
1448
1449 bad:
1450 /*
1451 * Write error occurred trying to update the inode
1452 * or the directory so must deallocate the inode.
1453 */
1454 tvp->v_type = VNON; /* Stop explosion if VBLK */
1455 ip->i_e2fs_nlink = 0;
1456 ip->i_flag |= IN_CHANGE;
1457 vput(tvp);
1458 vput(dvp);
1459 return (error);
1460 }
1461
1462 /*
1463 * Reclaim an inode so that it can be used for other purposes.
1464 */
1465 int
1466 ext2fs_reclaim(void *v)
1467 {
1468 struct vop_reclaim_args /* {
1469 struct vnode *a_vp;
1470 } */ *ap = v;
1471 struct vnode *vp = ap->a_vp;
1472 struct inode *ip = VTOI(vp);
1473 int error;
1474
1475 /*
1476 * The inode must be freed and updated before being removed
1477 * from its hash chain. Other threads trying to gain a hold
1478 * on the inode will be stalled because it is locked (VI_XLOCK).
1479 */
1480 if (ip->i_omode == 1 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1481 ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode);
1482 if ((error = ufs_reclaim(vp)) != 0)
1483 return (error);
1484 if (ip->i_din.e2fs_din != NULL)
1485 pool_put(&ext2fs_dinode_pool, ip->i_din.e2fs_din);
1486 genfs_node_destroy(vp);
1487 pool_put(&ext2fs_inode_pool, vp->v_data);
1488 vp->v_data = NULL;
1489 return (0);
1490 }
1491
1492 /* Global vfs data structures for ext2fs. */
1493 int (**ext2fs_vnodeop_p)(void *);
1494 const struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1495 { &vop_default_desc, vn_default_error },
1496 { &vop_lookup_desc, ext2fs_lookup }, /* lookup */
1497 { &vop_create_desc, ext2fs_create }, /* create */
1498 { &vop_mknod_desc, ext2fs_mknod }, /* mknod */
1499 { &vop_open_desc, ext2fs_open }, /* open */
1500 { &vop_close_desc, ufs_close }, /* close */
1501 { &vop_access_desc, ext2fs_access }, /* access */
1502 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1503 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1504 { &vop_read_desc, ext2fs_read }, /* read */
1505 { &vop_write_desc, ext2fs_write }, /* write */
1506 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
1507 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1508 { &vop_poll_desc, ufs_poll }, /* poll */
1509 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1510 { &vop_revoke_desc, ufs_revoke }, /* revoke */
1511 { &vop_mmap_desc, ufs_mmap }, /* mmap */
1512 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1513 { &vop_seek_desc, ufs_seek }, /* seek */
1514 { &vop_remove_desc, ext2fs_remove }, /* remove */
1515 { &vop_link_desc, ext2fs_link }, /* link */
1516 { &vop_rename_desc, ext2fs_rename }, /* rename */
1517 { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */
1518 { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */
1519 { &vop_symlink_desc, ext2fs_symlink }, /* symlink */
1520 { &vop_readdir_desc, ext2fs_readdir }, /* readdir */
1521 { &vop_readlink_desc, ext2fs_readlink }, /* readlink */
1522 { &vop_abortop_desc, ufs_abortop }, /* abortop */
1523 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1524 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1525 { &vop_lock_desc, ufs_lock }, /* lock */
1526 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1527 { &vop_bmap_desc, ext2fs_bmap }, /* bmap */
1528 { &vop_strategy_desc, ufs_strategy }, /* strategy */
1529 { &vop_print_desc, ufs_print }, /* print */
1530 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1531 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
1532 { &vop_advlock_desc, ext2fs_advlock }, /* advlock */
1533 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1534 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1535 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1536 { NULL, NULL }
1537 };
1538 const struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
1539 { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
1540
1541 int (**ext2fs_specop_p)(void *);
1542 const struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1543 { &vop_default_desc, vn_default_error },
1544 { &vop_lookup_desc, spec_lookup }, /* lookup */
1545 { &vop_create_desc, spec_create }, /* create */
1546 { &vop_mknod_desc, spec_mknod }, /* mknod */
1547 { &vop_open_desc, spec_open }, /* open */
1548 { &vop_close_desc, ufsspec_close }, /* close */
1549 { &vop_access_desc, ext2fs_access }, /* access */
1550 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1551 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1552 { &vop_read_desc, ufsspec_read }, /* read */
1553 { &vop_write_desc, ufsspec_write }, /* write */
1554 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
1555 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1556 { &vop_poll_desc, spec_poll }, /* poll */
1557 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
1558 { &vop_revoke_desc, spec_revoke }, /* revoke */
1559 { &vop_mmap_desc, spec_mmap }, /* mmap */
1560 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1561 { &vop_seek_desc, spec_seek }, /* seek */
1562 { &vop_remove_desc, spec_remove }, /* remove */
1563 { &vop_link_desc, spec_link }, /* link */
1564 { &vop_rename_desc, spec_rename }, /* rename */
1565 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
1566 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
1567 { &vop_symlink_desc, spec_symlink }, /* symlink */
1568 { &vop_readdir_desc, spec_readdir }, /* readdir */
1569 { &vop_readlink_desc, spec_readlink }, /* readlink */
1570 { &vop_abortop_desc, spec_abortop }, /* abortop */
1571 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1572 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1573 { &vop_lock_desc, ufs_lock }, /* lock */
1574 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1575 { &vop_bmap_desc, spec_bmap }, /* bmap */
1576 { &vop_strategy_desc, spec_strategy }, /* strategy */
1577 { &vop_print_desc, ufs_print }, /* print */
1578 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1579 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
1580 { &vop_advlock_desc, spec_advlock }, /* advlock */
1581 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1582 { &vop_getpages_desc, spec_getpages }, /* getpages */
1583 { &vop_putpages_desc, spec_putpages }, /* putpages */
1584 { NULL, NULL }
1585 };
1586 const struct vnodeopv_desc ext2fs_specop_opv_desc =
1587 { &ext2fs_specop_p, ext2fs_specop_entries };
1588
1589 int (**ext2fs_fifoop_p)(void *);
1590 const struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1591 { &vop_default_desc, vn_default_error },
1592 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
1593 { &vop_create_desc, vn_fifo_bypass }, /* create */
1594 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
1595 { &vop_open_desc, vn_fifo_bypass }, /* open */
1596 { &vop_close_desc, ufsfifo_close }, /* close */
1597 { &vop_access_desc, ext2fs_access }, /* access */
1598 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1599 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1600 { &vop_read_desc, ufsfifo_read }, /* read */
1601 { &vop_write_desc, ufsfifo_write }, /* write */
1602 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
1603 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
1604 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
1605 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
1606 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
1607 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
1608 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1609 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
1610 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
1611 { &vop_link_desc, vn_fifo_bypass }, /* link */
1612 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
1613 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
1614 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
1615 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
1616 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
1617 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
1618 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
1619 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1620 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1621 { &vop_lock_desc, ufs_lock }, /* lock */
1622 { &vop_unlock_desc, ufs_unlock }, /* unlock */
1623 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
1624 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
1625 { &vop_print_desc, ufs_print }, /* print */
1626 { &vop_islocked_desc, ufs_islocked }, /* islocked */
1627 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
1628 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
1629 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1630 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
1631 { NULL, NULL }
1632 };
1633 const struct vnodeopv_desc ext2fs_fifoop_opv_desc =
1634 { &ext2fs_fifoop_p, ext2fs_fifoop_entries };
1635