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