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