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