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