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