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