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