1 /*-
2 * modified for EXT2FS support in Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7 /*-
8 * SPDX-License-Identifier: BSD-3-Clause
9 *
10 * Copyright (c) 1982, 1986, 1989, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
43 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
44 * $FreeBSD: releng/12.0/sys/fs/ext2fs/ext2_vnops.c 338150 2018-08-21 18:39:02Z fsu $
45 */
46
47 #include "opt_suiddir.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/stat.h>
55 #include <sys/bio.h>
56 #include <sys/buf.h>
57 #include <sys/endian.h>
58 #include <sys/priv.h>
59 #include <sys/rwlock.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
62 #include <sys/time.h>
63 #include <sys/vnode.h>
64 #include <sys/namei.h>
65 #include <sys/lockf.h>
66 #include <sys/event.h>
67 #include <sys/conf.h>
68 #include <sys/file.h>
69 #include <sys/extattr.h>
70 #include <sys/vmmeter.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
79
80 #include "opt_directio.h"
81
82 #include <ufs/ufs/dir.h>
83
84 #include <fs/ext2fs/fs.h>
85 #include <fs/ext2fs/inode.h>
86 #include <fs/ext2fs/ext2_acl.h>
87 #include <fs/ext2fs/ext2fs.h>
88 #include <fs/ext2fs/ext2_extern.h>
89 #include <fs/ext2fs/ext2_dinode.h>
90 #include <fs/ext2fs/ext2_dir.h>
91 #include <fs/ext2fs/ext2_mount.h>
92 #include <fs/ext2fs/ext2_extattr.h>
93 #include <fs/ext2fs/ext2_extents.h>
94
95 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
96 static void ext2_itimes_locked(struct vnode *);
97
98 static vop_access_t ext2_access;
99 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
100 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
101 struct thread *);
102 static vop_close_t ext2_close;
103 static vop_create_t ext2_create;
104 static vop_fsync_t ext2_fsync;
105 static vop_getattr_t ext2_getattr;
106 static vop_ioctl_t ext2_ioctl;
107 static vop_link_t ext2_link;
108 static vop_mkdir_t ext2_mkdir;
109 static vop_mknod_t ext2_mknod;
110 static vop_open_t ext2_open;
111 static vop_pathconf_t ext2_pathconf;
112 static vop_print_t ext2_print;
113 static vop_read_t ext2_read;
114 static vop_readlink_t ext2_readlink;
115 static vop_remove_t ext2_remove;
116 static vop_rename_t ext2_rename;
117 static vop_rmdir_t ext2_rmdir;
118 static vop_setattr_t ext2_setattr;
119 static vop_strategy_t ext2_strategy;
120 static vop_symlink_t ext2_symlink;
121 static vop_write_t ext2_write;
122 static vop_deleteextattr_t ext2_deleteextattr;
123 static vop_getextattr_t ext2_getextattr;
124 static vop_listextattr_t ext2_listextattr;
125 static vop_setextattr_t ext2_setextattr;
126 static vop_vptofh_t ext2_vptofh;
127 static vop_close_t ext2fifo_close;
128 static vop_kqfilter_t ext2fifo_kqfilter;
129
130 /* Global vfs data structures for ext2. */
131 struct vop_vector ext2_vnodeops = {
132 .vop_default = &default_vnodeops,
133 .vop_access = ext2_access,
134 .vop_bmap = ext2_bmap,
135 .vop_cachedlookup = ext2_lookup,
136 .vop_close = ext2_close,
137 .vop_create = ext2_create,
138 .vop_fsync = ext2_fsync,
139 .vop_getpages = vnode_pager_local_getpages,
140 .vop_getpages_async = vnode_pager_local_getpages_async,
141 .vop_getattr = ext2_getattr,
142 .vop_inactive = ext2_inactive,
143 .vop_ioctl = ext2_ioctl,
144 .vop_link = ext2_link,
145 .vop_lookup = vfs_cache_lookup,
146 .vop_mkdir = ext2_mkdir,
147 .vop_mknod = ext2_mknod,
148 .vop_open = ext2_open,
149 .vop_pathconf = ext2_pathconf,
150 .vop_poll = vop_stdpoll,
151 .vop_print = ext2_print,
152 .vop_read = ext2_read,
153 .vop_readdir = ext2_readdir,
154 .vop_readlink = ext2_readlink,
155 .vop_reallocblks = ext2_reallocblks,
156 .vop_reclaim = ext2_reclaim,
157 .vop_remove = ext2_remove,
158 .vop_rename = ext2_rename,
159 .vop_rmdir = ext2_rmdir,
160 .vop_setattr = ext2_setattr,
161 .vop_strategy = ext2_strategy,
162 .vop_symlink = ext2_symlink,
163 .vop_write = ext2_write,
164 .vop_deleteextattr = ext2_deleteextattr,
165 .vop_getextattr = ext2_getextattr,
166 .vop_listextattr = ext2_listextattr,
167 .vop_setextattr = ext2_setextattr,
168 #ifdef UFS_ACL
169 .vop_getacl = ext2_getacl,
170 .vop_setacl = ext2_setacl,
171 .vop_aclcheck = ext2_aclcheck,
172 #endif /* UFS_ACL */
173 .vop_vptofh = ext2_vptofh,
174 };
175
176 struct vop_vector ext2_fifoops = {
177 .vop_default = &fifo_specops,
178 .vop_access = ext2_access,
179 .vop_close = ext2fifo_close,
180 .vop_fsync = ext2_fsync,
181 .vop_getattr = ext2_getattr,
182 .vop_inactive = ext2_inactive,
183 .vop_kqfilter = ext2fifo_kqfilter,
184 .vop_pathconf = ext2_pathconf,
185 .vop_print = ext2_print,
186 .vop_read = VOP_PANIC,
187 .vop_reclaim = ext2_reclaim,
188 .vop_setattr = ext2_setattr,
189 .vop_write = VOP_PANIC,
190 .vop_vptofh = ext2_vptofh,
191 };
192
193 /*
194 * A virgin directory (no blushing please).
195 * Note that the type and namlen fields are reversed relative to ext2.
196 * Also, we don't use `struct odirtemplate', since it would just cause
197 * endianness problems.
198 */
199 static struct dirtemplate mastertemplate = {
200 0, 12, 1, EXT2_FT_DIR, ".",
201 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
202 };
203 static struct dirtemplate omastertemplate = {
204 0, 12, 1, EXT2_FT_UNKNOWN, ".",
205 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
206 };
207
208 static void
209 ext2_itimes_locked(struct vnode *vp)
210 {
211 struct inode *ip;
212 struct timespec ts;
213
214 ASSERT_VI_LOCKED(vp, __func__);
215
216 ip = VTOI(vp);
217 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
218 return;
219 if ((vp->v_type == VBLK || vp->v_type == VCHR))
220 ip->i_flag |= IN_LAZYMOD;
221 else
222 ip->i_flag |= IN_MODIFIED;
223 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
224 vfs_timestamp(&ts);
225 if (ip->i_flag & IN_ACCESS) {
226 ip->i_atime = ts.tv_sec;
227 ip->i_atimensec = ts.tv_nsec;
228 }
229 if (ip->i_flag & IN_UPDATE) {
230 ip->i_mtime = ts.tv_sec;
231 ip->i_mtimensec = ts.tv_nsec;
232 ip->i_modrev++;
233 }
234 if (ip->i_flag & IN_CHANGE) {
235 ip->i_ctime = ts.tv_sec;
236 ip->i_ctimensec = ts.tv_nsec;
237 }
238 }
239 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
240 }
241
242 void
243 ext2_itimes(struct vnode *vp)
244 {
245
246 VI_LOCK(vp);
247 ext2_itimes_locked(vp);
248 VI_UNLOCK(vp);
249 }
250
251 /*
252 * Create a regular file
253 */
254 static int
255 ext2_create(struct vop_create_args *ap)
256 {
257 int error;
258
259 error =
260 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
261 ap->a_dvp, ap->a_vpp, ap->a_cnp);
262 if (error != 0)
263 return (error);
264 if ((ap->a_cnp->cn_flags & MAKEENTRY) != 0)
265 cache_enter(ap->a_dvp, *ap->a_vpp, ap->a_cnp);
266 return (0);
267 }
268
269 static int
270 ext2_open(struct vop_open_args *ap)
271 {
272
273 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
274 return (EOPNOTSUPP);
275
276 /*
277 * Files marked append-only must be opened for appending.
278 */
279 if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
280 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
281 return (EPERM);
282
283 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
284
285 return (0);
286 }
287
288 /*
289 * Close called.
290 *
291 * Update the times on the inode.
292 */
293 static int
294 ext2_close(struct vop_close_args *ap)
295 {
296 struct vnode *vp = ap->a_vp;
297
298 VI_LOCK(vp);
299 if (vp->v_usecount > 1)
300 ext2_itimes_locked(vp);
301 VI_UNLOCK(vp);
302 return (0);
303 }
304
305 static int
306 ext2_access(struct vop_access_args *ap)
307 {
308 struct vnode *vp = ap->a_vp;
309 struct inode *ip = VTOI(vp);
310 accmode_t accmode = ap->a_accmode;
311 int error;
312
313 if (vp->v_type == VBLK || vp->v_type == VCHR)
314 return (EOPNOTSUPP);
315
316 /*
317 * Disallow write attempts on read-only file systems;
318 * unless the file is a socket, fifo, or a block or
319 * character device resident on the file system.
320 */
321 if (accmode & VWRITE) {
322 switch (vp->v_type) {
323 case VDIR:
324 case VLNK:
325 case VREG:
326 if (vp->v_mount->mnt_flag & MNT_RDONLY)
327 return (EROFS);
328 break;
329 default:
330 break;
331 }
332 }
333
334 /* If immutable bit set, nobody gets to write it. */
335 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
336 return (EPERM);
337
338 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
339 ap->a_accmode, ap->a_cred, NULL);
340 return (error);
341 }
342
343 static int
344 ext2_getattr(struct vop_getattr_args *ap)
345 {
346 struct vnode *vp = ap->a_vp;
347 struct inode *ip = VTOI(vp);
348 struct vattr *vap = ap->a_vap;
349
350 ext2_itimes(vp);
351 /*
352 * Copy from inode table
353 */
354 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
355 vap->va_fileid = ip->i_number;
356 vap->va_mode = ip->i_mode & ~IFMT;
357 vap->va_nlink = ip->i_nlink;
358 vap->va_uid = ip->i_uid;
359 vap->va_gid = ip->i_gid;
360 vap->va_rdev = ip->i_rdev;
361 vap->va_size = ip->i_size;
362 vap->va_atime.tv_sec = ip->i_atime;
363 vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
364 vap->va_mtime.tv_sec = ip->i_mtime;
365 vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
366 vap->va_ctime.tv_sec = ip->i_ctime;
367 vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
368 if E2DI_HAS_XTIME(ip) {
369 vap->va_birthtime.tv_sec = ip->i_birthtime;
370 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
371 }
372 vap->va_flags = ip->i_flags;
373 vap->va_gen = ip->i_gen;
374 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
375 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
376 vap->va_type = IFTOVT(ip->i_mode);
377 vap->va_filerev = ip->i_modrev;
378 return (0);
379 }
380
381 /*
382 * Set attribute vnode op. called from several syscalls
383 */
384 static int
385 ext2_setattr(struct vop_setattr_args *ap)
386 {
387 struct vattr *vap = ap->a_vap;
388 struct vnode *vp = ap->a_vp;
389 struct inode *ip = VTOI(vp);
390 struct ucred *cred = ap->a_cred;
391 struct thread *td = curthread;
392 int error;
393
394 /*
395 * Check for unsettable attributes.
396 */
397 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
398 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
399 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
400 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
401 return (EINVAL);
402 }
403 if (vap->va_flags != VNOVAL) {
404 /* Disallow flags not supported by ext2fs. */
405 if (vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
406 return (EOPNOTSUPP);
407
408 if (vp->v_mount->mnt_flag & MNT_RDONLY)
409 return (EROFS);
410 /*
411 * Callers may only modify the file flags on objects they
412 * have VADMIN rights for.
413 */
414 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
415 return (error);
416 /*
417 * Unprivileged processes and privileged processes in
418 * jail() are not permitted to unset system flags, or
419 * modify flags if any system flags are set.
420 * Privileged non-jail processes may not modify system flags
421 * if securelevel > 0 and any existing system flags are set.
422 */
423 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
424 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) {
425 error = securelevel_gt(cred, 0);
426 if (error)
427 return (error);
428 }
429 } else {
430 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) ||
431 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE))
432 return (EPERM);
433 }
434 ip->i_flags = vap->va_flags;
435 ip->i_flag |= IN_CHANGE;
436 if (ip->i_flags & (IMMUTABLE | APPEND))
437 return (0);
438 }
439 if (ip->i_flags & (IMMUTABLE | APPEND))
440 return (EPERM);
441 /*
442 * Go through the fields and update iff not VNOVAL.
443 */
444 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
445 if (vp->v_mount->mnt_flag & MNT_RDONLY)
446 return (EROFS);
447 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
448 td)) != 0)
449 return (error);
450 }
451 if (vap->va_size != VNOVAL) {
452 /*
453 * Disallow write attempts on read-only file systems;
454 * unless the file is a socket, fifo, or a block or
455 * character device resident on the file system.
456 */
457 switch (vp->v_type) {
458 case VDIR:
459 return (EISDIR);
460 case VLNK:
461 case VREG:
462 if (vp->v_mount->mnt_flag & MNT_RDONLY)
463 return (EROFS);
464 break;
465 default:
466 break;
467 }
468 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
469 return (error);
470 }
471 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
472 if (vp->v_mount->mnt_flag & MNT_RDONLY)
473 return (EROFS);
474 /*
475 * From utimes(2):
476 * If times is NULL, ... The caller must be the owner of
477 * the file, have permission to write the file, or be the
478 * super-user.
479 * If times is non-NULL, ... The caller must be the owner of
480 * the file or be the super-user.
481 */
482 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
483 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
484 (error = VOP_ACCESS(vp, VWRITE, cred, td))))
485 return (error);
486 ip->i_flag |= IN_CHANGE | IN_MODIFIED;
487 if (vap->va_atime.tv_sec != VNOVAL) {
488 ip->i_flag &= ~IN_ACCESS;
489 ip->i_atime = vap->va_atime.tv_sec;
490 ip->i_atimensec = vap->va_atime.tv_nsec;
491 }
492 if (vap->va_mtime.tv_sec != VNOVAL) {
493 ip->i_flag &= ~IN_UPDATE;
494 ip->i_mtime = vap->va_mtime.tv_sec;
495 ip->i_mtimensec = vap->va_mtime.tv_nsec;
496 }
497 ip->i_birthtime = vap->va_birthtime.tv_sec;
498 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
499 error = ext2_update(vp, 0);
500 if (error)
501 return (error);
502 }
503 error = 0;
504 if (vap->va_mode != (mode_t)VNOVAL) {
505 if (vp->v_mount->mnt_flag & MNT_RDONLY)
506 return (EROFS);
507 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
508 }
509 return (error);
510 }
511
512 /*
513 * Change the mode on a file.
514 * Inode must be locked before calling.
515 */
516 static int
517 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
518 {
519 struct inode *ip = VTOI(vp);
520 int error;
521
522 /*
523 * To modify the permissions on a file, must possess VADMIN
524 * for that file.
525 */
526 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
527 return (error);
528 /*
529 * Privileged processes may set the sticky bit on non-directories,
530 * as well as set the setgid bit on a file with a group that the
531 * process is not a member of.
532 */
533 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
534 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
535 if (error)
536 return (EFTYPE);
537 }
538 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
539 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
540 if (error)
541 return (error);
542 }
543 ip->i_mode &= ~ALLPERMS;
544 ip->i_mode |= (mode & ALLPERMS);
545 ip->i_flag |= IN_CHANGE;
546 return (0);
547 }
548
549 /*
550 * Perform chown operation on inode ip;
551 * inode must be locked prior to call.
552 */
553 static int
554 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
555 struct thread *td)
556 {
557 struct inode *ip = VTOI(vp);
558 uid_t ouid;
559 gid_t ogid;
560 int error = 0;
561
562 if (uid == (uid_t)VNOVAL)
563 uid = ip->i_uid;
564 if (gid == (gid_t)VNOVAL)
565 gid = ip->i_gid;
566 /*
567 * To modify the ownership of a file, must possess VADMIN
568 * for that file.
569 */
570 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
571 return (error);
572 /*
573 * To change the owner of a file, or change the group of a file
574 * to a group of which we are not a member, the caller must
575 * have privilege.
576 */
577 if (uid != ip->i_uid || (gid != ip->i_gid &&
578 !groupmember(gid, cred))) {
579 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
580 if (error)
581 return (error);
582 }
583 ogid = ip->i_gid;
584 ouid = ip->i_uid;
585 ip->i_gid = gid;
586 ip->i_uid = uid;
587 ip->i_flag |= IN_CHANGE;
588 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
589 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
590 ip->i_mode &= ~(ISUID | ISGID);
591 }
592 return (0);
593 }
594
595 /*
596 * Synch an open file.
597 */
598 /* ARGSUSED */
599 static int
600 ext2_fsync(struct vop_fsync_args *ap)
601 {
602 /*
603 * Flush all dirty buffers associated with a vnode.
604 */
605
606 vop_stdfsync(ap);
607
608 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
609 }
610
611 /*
612 * Mknod vnode call
613 */
614 /* ARGSUSED */
615 static int
616 ext2_mknod(struct vop_mknod_args *ap)
617 {
618 struct vattr *vap = ap->a_vap;
619 struct vnode **vpp = ap->a_vpp;
620 struct inode *ip;
621 ino_t ino;
622 int error;
623
624 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
625 ap->a_dvp, vpp, ap->a_cnp);
626 if (error)
627 return (error);
628 ip = VTOI(*vpp);
629 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
630 if (vap->va_rdev != VNOVAL) {
631 /*
632 * Want to be able to use this to make badblock
633 * inodes, so don't truncate the dev number.
634 */
635 if (!(ip->i_flag & IN_E4EXTENTS))
636 ip->i_rdev = vap->va_rdev;
637 }
638 /*
639 * Remove inode, then reload it through VFS_VGET so it is
640 * checked to see if it is an alias of an existing entry in
641 * the inode cache. XXX I don't believe this is necessary now.
642 */
643 (*vpp)->v_type = VNON;
644 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
645 vgone(*vpp);
646 vput(*vpp);
647 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
648 if (error) {
649 *vpp = NULL;
650 return (error);
651 }
652 return (0);
653 }
654
655 static int
656 ext2_remove(struct vop_remove_args *ap)
657 {
658 struct inode *ip;
659 struct vnode *vp = ap->a_vp;
660 struct vnode *dvp = ap->a_dvp;
661 int error;
662
663 ip = VTOI(vp);
664 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
665 (VTOI(dvp)->i_flags & APPEND)) {
666 error = EPERM;
667 goto out;
668 }
669 error = ext2_dirremove(dvp, ap->a_cnp);
670 if (error == 0) {
671 ip->i_nlink--;
672 ip->i_flag |= IN_CHANGE;
673 }
674 out:
675 return (error);
676 }
677
678 /*
679 * link vnode call
680 */
681 static int
682 ext2_link(struct vop_link_args *ap)
683 {
684 struct vnode *vp = ap->a_vp;
685 struct vnode *tdvp = ap->a_tdvp;
686 struct componentname *cnp = ap->a_cnp;
687 struct inode *ip;
688 int error;
689
690 #ifdef INVARIANTS
691 if ((cnp->cn_flags & HASBUF) == 0)
692 panic("ext2_link: no name");
693 #endif
694 ip = VTOI(vp);
695 if ((nlink_t)ip->i_nlink >= EXT4_LINK_MAX) {
696 error = EMLINK;
697 goto out;
698 }
699 if (ip->i_flags & (IMMUTABLE | APPEND)) {
700 error = EPERM;
701 goto out;
702 }
703 ip->i_nlink++;
704 ip->i_flag |= IN_CHANGE;
705 error = ext2_update(vp, !DOINGASYNC(vp));
706 if (!error)
707 error = ext2_direnter(ip, tdvp, cnp);
708 if (error) {
709 ip->i_nlink--;
710 ip->i_flag |= IN_CHANGE;
711 }
712 out:
713 return (error);
714 }
715
716 static int
717 ext2_inc_nlink(struct inode *ip)
718 {
719
720 ip->i_nlink++;
721
722 if (S_ISDIR(ip->i_mode) &&
723 EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK) &&
724 ip->i_nlink > 1) {
725 if (ip->i_nlink >= EXT4_LINK_MAX || ip->i_nlink == 2)
726 ip->i_nlink = 1;
727 } else if (ip->i_nlink > EXT4_LINK_MAX) {
728 ip->i_nlink--;
729 return (EMLINK);
730 }
731
732 return (0);
733 }
734
735 static void
736 ext2_dec_nlink(struct inode *ip)
737 {
738
739 if (!S_ISDIR(ip->i_mode) || ip->i_nlink > 2)
740 ip->i_nlink--;
741 }
742
743 /*
744 * Rename system call.
745 * rename("foo", "bar");
746 * is essentially
747 * unlink("bar");
748 * link("foo", "bar");
749 * unlink("foo");
750 * but ``atomically''. Can't do full commit without saving state in the
751 * inode on disk which isn't feasible at this time. Best we can do is
752 * always guarantee the target exists.
753 *
754 * Basic algorithm is:
755 *
756 * 1) Bump link count on source while we're linking it to the
757 * target. This also ensure the inode won't be deleted out
758 * from underneath us while we work (it may be truncated by
759 * a concurrent `trunc' or `open' for creation).
760 * 2) Link source to destination. If destination already exists,
761 * delete it first.
762 * 3) Unlink source reference to inode if still around. If a
763 * directory was moved and the parent of the destination
764 * is different from the source, patch the ".." entry in the
765 * directory.
766 */
767 static int
768 ext2_rename(struct vop_rename_args *ap)
769 {
770 struct vnode *tvp = ap->a_tvp;
771 struct vnode *tdvp = ap->a_tdvp;
772 struct vnode *fvp = ap->a_fvp;
773 struct vnode *fdvp = ap->a_fdvp;
774 struct componentname *tcnp = ap->a_tcnp;
775 struct componentname *fcnp = ap->a_fcnp;
776 struct inode *ip, *xp, *dp;
777 struct dirtemplate *dirbuf;
778 int doingdirectory = 0, oldparent = 0, newparent = 0;
779 int error = 0;
780 u_char namlen;
781
782 #ifdef INVARIANTS
783 if ((tcnp->cn_flags & HASBUF) == 0 ||
784 (fcnp->cn_flags & HASBUF) == 0)
785 panic("ext2_rename: no name");
786 #endif
787 /*
788 * Check for cross-device rename.
789 */
790 if ((fvp->v_mount != tdvp->v_mount) ||
791 (tvp && (fvp->v_mount != tvp->v_mount))) {
792 error = EXDEV;
793 abortit:
794 if (tdvp == tvp)
795 vrele(tdvp);
796 else
797 vput(tdvp);
798 if (tvp)
799 vput(tvp);
800 vrele(fdvp);
801 vrele(fvp);
802 return (error);
803 }
804
805 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
806 (VTOI(tdvp)->i_flags & APPEND))) {
807 error = EPERM;
808 goto abortit;
809 }
810
811 /*
812 * Renaming a file to itself has no effect. The upper layers should
813 * not call us in that case. Temporarily just warn if they do.
814 */
815 if (fvp == tvp) {
816 printf("ext2_rename: fvp == tvp (can't happen)\n");
817 error = 0;
818 goto abortit;
819 }
820
821 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
822 goto abortit;
823 dp = VTOI(fdvp);
824 ip = VTOI(fvp);
825 if (ip->i_nlink >= EXT4_LINK_MAX &&
826 !EXT2_HAS_RO_COMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
827 VOP_UNLOCK(fvp, 0);
828 error = EMLINK;
829 goto abortit;
830 }
831 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
832 || (dp->i_flags & APPEND)) {
833 VOP_UNLOCK(fvp, 0);
834 error = EPERM;
835 goto abortit;
836 }
837 if ((ip->i_mode & IFMT) == IFDIR) {
838 /*
839 * Avoid ".", "..", and aliases of "." for obvious reasons.
840 */
841 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
842 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
843 (ip->i_flag & IN_RENAME)) {
844 VOP_UNLOCK(fvp, 0);
845 error = EINVAL;
846 goto abortit;
847 }
848 ip->i_flag |= IN_RENAME;
849 oldparent = dp->i_number;
850 doingdirectory++;
851 }
852 vrele(fdvp);
853
854 /*
855 * When the target exists, both the directory
856 * and target vnodes are returned locked.
857 */
858 dp = VTOI(tdvp);
859 xp = NULL;
860 if (tvp)
861 xp = VTOI(tvp);
862
863 /*
864 * 1) Bump link count while we're moving stuff
865 * around. If we crash somewhere before
866 * completing our work, the link count
867 * may be wrong, but correctable.
868 */
869 ext2_inc_nlink(ip);
870 ip->i_flag |= IN_CHANGE;
871 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
872 VOP_UNLOCK(fvp, 0);
873 goto bad;
874 }
875
876 /*
877 * If ".." must be changed (ie the directory gets a new
878 * parent) then the source directory must not be in the
879 * directory hierarchy above the target, as this would
880 * orphan everything below the source directory. Also
881 * the user must have write permission in the source so
882 * as to be able to change "..". We must repeat the call
883 * to namei, as the parent directory is unlocked by the
884 * call to checkpath().
885 */
886 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
887 VOP_UNLOCK(fvp, 0);
888 if (oldparent != dp->i_number)
889 newparent = dp->i_number;
890 if (doingdirectory && newparent) {
891 if (error) /* write access check above */
892 goto bad;
893 if (xp != NULL)
894 vput(tvp);
895 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
896 if (error)
897 goto out;
898 VREF(tdvp);
899 error = relookup(tdvp, &tvp, tcnp);
900 if (error)
901 goto out;
902 vrele(tdvp);
903 dp = VTOI(tdvp);
904 xp = NULL;
905 if (tvp)
906 xp = VTOI(tvp);
907 }
908 /*
909 * 2) If target doesn't exist, link the target
910 * to the source and unlink the source.
911 * Otherwise, rewrite the target directory
912 * entry to reference the source inode and
913 * expunge the original entry's existence.
914 */
915 if (xp == NULL) {
916 if (dp->i_devvp != ip->i_devvp)
917 panic("ext2_rename: EXDEV");
918 /*
919 * Account for ".." in new directory.
920 * When source and destination have the same
921 * parent we don't fool with the link count.
922 */
923 if (doingdirectory && newparent) {
924 error = ext2_inc_nlink(dp);
925 if (error)
926 goto bad;
927
928 dp->i_flag |= IN_CHANGE;
929 error = ext2_update(tdvp, !DOINGASYNC(tdvp));
930 if (error)
931 goto bad;
932 }
933 error = ext2_direnter(ip, tdvp, tcnp);
934 if (error) {
935 if (doingdirectory && newparent) {
936 ext2_dec_nlink(dp);
937 dp->i_flag |= IN_CHANGE;
938 (void)ext2_update(tdvp, 1);
939 }
940 goto bad;
941 }
942 vput(tdvp);
943 } else {
944 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
945 panic("ext2_rename: EXDEV");
946 /*
947 * Short circuit rename(foo, foo).
948 */
949 if (xp->i_number == ip->i_number)
950 panic("ext2_rename: same file");
951 /*
952 * If the parent directory is "sticky", then the user must
953 * own the parent directory, or the destination of the rename,
954 * otherwise the destination may not be changed (except by
955 * root). This implements append-only directories.
956 */
957 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
958 tcnp->cn_cred->cr_uid != dp->i_uid &&
959 xp->i_uid != tcnp->cn_cred->cr_uid) {
960 error = EPERM;
961 goto bad;
962 }
963 /*
964 * Target must be empty if a directory and have no links
965 * to it. Also, ensure source and target are compatible
966 * (both directories, or both not directories).
967 */
968 if ((xp->i_mode & IFMT) == IFDIR) {
969 if (!ext2_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
970 error = ENOTEMPTY;
971 goto bad;
972 }
973 if (!doingdirectory) {
974 error = ENOTDIR;
975 goto bad;
976 }
977 cache_purge(tdvp);
978 } else if (doingdirectory) {
979 error = EISDIR;
980 goto bad;
981 }
982 error = ext2_dirrewrite(dp, ip, tcnp);
983 if (error)
984 goto bad;
985 /*
986 * If the target directory is in the same
987 * directory as the source directory,
988 * decrement the link count on the parent
989 * of the target directory.
990 */
991 if (doingdirectory && !newparent) {
992 ext2_dec_nlink(dp);
993 dp->i_flag |= IN_CHANGE;
994 }
995 vput(tdvp);
996 /*
997 * Adjust the link count of the target to
998 * reflect the dirrewrite above. If this is
999 * a directory it is empty and there are
1000 * no links to it, so we can squash the inode and
1001 * any space associated with it. We disallowed
1002 * renaming over top of a directory with links to
1003 * it above, as the remaining link would point to
1004 * a directory without "." or ".." entries.
1005 */
1006 ext2_dec_nlink(xp);
1007 if (doingdirectory) {
1008 if (--xp->i_nlink != 0)
1009 panic("ext2_rename: linked directory");
1010 error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
1011 tcnp->cn_cred, tcnp->cn_thread);
1012 }
1013 xp->i_flag |= IN_CHANGE;
1014 vput(tvp);
1015 xp = NULL;
1016 }
1017
1018 /*
1019 * 3) Unlink the source.
1020 */
1021 fcnp->cn_flags &= ~MODMASK;
1022 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1023 VREF(fdvp);
1024 error = relookup(fdvp, &fvp, fcnp);
1025 if (error == 0)
1026 vrele(fdvp);
1027 if (fvp != NULL) {
1028 xp = VTOI(fvp);
1029 dp = VTOI(fdvp);
1030 } else {
1031 /*
1032 * From name has disappeared. IN_RENAME is not sufficient
1033 * to protect against directory races due to timing windows,
1034 * so we can't panic here.
1035 */
1036 vrele(ap->a_fvp);
1037 return (0);
1038 }
1039 /*
1040 * Ensure that the directory entry still exists and has not
1041 * changed while the new name has been entered. If the source is
1042 * a file then the entry may have been unlinked or renamed. In
1043 * either case there is no further work to be done. If the source
1044 * is a directory then it cannot have been rmdir'ed; its link
1045 * count of three would cause a rmdir to fail with ENOTEMPTY.
1046 * The IN_RENAME flag ensures that it cannot be moved by another
1047 * rename.
1048 */
1049 if (xp != ip) {
1050 /*
1051 * From name resolves to a different inode. IN_RENAME is
1052 * not sufficient protection against timing window races
1053 * so we can't panic here.
1054 */
1055 } else {
1056 /*
1057 * If the source is a directory with a
1058 * new parent, the link count of the old
1059 * parent directory must be decremented
1060 * and ".." set to point to the new parent.
1061 */
1062 if (doingdirectory && newparent) {
1063 ext2_dec_nlink(dp);
1064 dp->i_flag |= IN_CHANGE;
1065 dirbuf = malloc(dp->i_e2fs->e2fs_bsize, M_TEMP, M_WAITOK | M_ZERO);
1066 if (!dirbuf) {
1067 error = ENOMEM;
1068 goto bad;
1069 }
1070 error = vn_rdwr(UIO_READ, fvp, (caddr_t)dirbuf,
1071 ip->i_e2fs->e2fs_bsize, (off_t)0,
1072 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1073 tcnp->cn_cred, NOCRED, NULL, NULL);
1074 if (error == 0) {
1075 /* Like ufs little-endian: */
1076 namlen = dirbuf->dotdot_type;
1077 if (namlen != 2 ||
1078 dirbuf->dotdot_name[0] != '.' ||
1079 dirbuf->dotdot_name[1] != '.') {
1080 ext2_dirbad(xp, (doff_t)12,
1081 "rename: mangled dir");
1082 } else {
1083 dirbuf->dotdot_ino = newparent;
1084 /*
1085 * dirblock 0 could be htree root,
1086 * try both csum update functions.
1087 */
1088 ext2_dirent_csum_set(ip,
1089 (struct ext2fs_direct_2 *)dirbuf);
1090 ext2_dx_csum_set(ip,
1091 (struct ext2fs_direct_2 *)dirbuf);
1092 (void)vn_rdwr(UIO_WRITE, fvp,
1093 (caddr_t)dirbuf,
1094 ip->i_e2fs->e2fs_bsize,
1095 (off_t)0, UIO_SYSSPACE,
1096 IO_NODELOCKED | IO_SYNC |
1097 IO_NOMACCHECK, tcnp->cn_cred,
1098 NOCRED, NULL, NULL);
1099 cache_purge(fdvp);
1100 }
1101 }
1102 free(dirbuf, M_TEMP);
1103 }
1104 error = ext2_dirremove(fdvp, fcnp);
1105 if (!error) {
1106 ext2_dec_nlink(xp);
1107 xp->i_flag |= IN_CHANGE;
1108 }
1109 xp->i_flag &= ~IN_RENAME;
1110 }
1111 if (dp)
1112 vput(fdvp);
1113 if (xp)
1114 vput(fvp);
1115 vrele(ap->a_fvp);
1116 return (error);
1117
1118 bad:
1119 if (xp)
1120 vput(ITOV(xp));
1121 vput(ITOV(dp));
1122 out:
1123 if (doingdirectory)
1124 ip->i_flag &= ~IN_RENAME;
1125 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1126 ext2_dec_nlink(ip);
1127 ip->i_flag |= IN_CHANGE;
1128 ip->i_flag &= ~IN_RENAME;
1129 vput(fvp);
1130 } else
1131 vrele(fvp);
1132 return (error);
1133 }
1134
1135 #ifdef UFS_ACL
1136 static int
1137 ext2_do_posix1e_acl_inheritance_dir(struct vnode *dvp, struct vnode *tvp,
1138 mode_t dmode, struct ucred *cred, struct thread *td)
1139 {
1140 int error;
1141 struct inode *ip = VTOI(tvp);
1142 struct acl *dacl, *acl;
1143
1144 acl = acl_alloc(M_WAITOK);
1145 dacl = acl_alloc(M_WAITOK);
1146
1147 /*
1148 * Retrieve default ACL from parent, if any.
1149 */
1150 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1151 switch (error) {
1152 case 0:
1153 /*
1154 * Retrieved a default ACL, so merge mode and ACL if
1155 * necessary. If the ACL is empty, fall through to
1156 * the "not defined or available" case.
1157 */
1158 if (acl->acl_cnt != 0) {
1159 dmode = acl_posix1e_newfilemode(dmode, acl);
1160 ip->i_mode = dmode;
1161 *dacl = *acl;
1162 ext2_sync_acl_from_inode(ip, acl);
1163 break;
1164 }
1165 /* FALLTHROUGH */
1166
1167 case EOPNOTSUPP:
1168 /*
1169 * Just use the mode as-is.
1170 */
1171 ip->i_mode = dmode;
1172 error = 0;
1173 goto out;
1174
1175 default:
1176 goto out;
1177 }
1178
1179 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1180 if (error == 0)
1181 error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl, cred, td);
1182 switch (error) {
1183 case 0:
1184 break;
1185
1186 case EOPNOTSUPP:
1187 /*
1188 * XXX: This should not happen, as EOPNOTSUPP above
1189 * was supposed to free acl.
1190 */
1191 #ifdef DEBUG
1192 printf("ext2_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
1193 #endif /* DEBUG */
1194 break;
1195
1196 default:
1197 goto out;
1198 }
1199
1200 out:
1201 acl_free(acl);
1202 acl_free(dacl);
1203
1204 return (error);
1205 }
1206
1207 static int
1208 ext2_do_posix1e_acl_inheritance_file(struct vnode *dvp, struct vnode *tvp,
1209 mode_t mode, struct ucred *cred, struct thread *td)
1210 {
1211 int error;
1212 struct inode *ip = VTOI(tvp);
1213 struct acl *acl;
1214
1215 acl = acl_alloc(M_WAITOK);
1216
1217 /*
1218 * Retrieve default ACL for parent, if any.
1219 */
1220 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cred, td);
1221 switch (error) {
1222 case 0:
1223 /*
1224 * Retrieved a default ACL, so merge mode and ACL if
1225 * necessary.
1226 */
1227 if (acl->acl_cnt != 0) {
1228 /*
1229 * Two possible ways for default ACL to not
1230 * be present. First, the EA can be
1231 * undefined, or second, the default ACL can
1232 * be blank. If it's blank, fall through to
1233 * the it's not defined case.
1234 */
1235 mode = acl_posix1e_newfilemode(mode, acl);
1236 ip->i_mode = mode;
1237 ext2_sync_acl_from_inode(ip, acl);
1238 break;
1239 }
1240 /* FALLTHROUGH */
1241
1242 case EOPNOTSUPP:
1243 /*
1244 * Just use the mode as-is.
1245 */
1246 ip->i_mode = mode;
1247 error = 0;
1248 goto out;
1249
1250 default:
1251 goto out;
1252 }
1253
1254 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cred, td);
1255 switch (error) {
1256 case 0:
1257 break;
1258
1259 case EOPNOTSUPP:
1260 /*
1261 * XXX: This should not happen, as EOPNOTSUPP above was
1262 * supposed to free acl.
1263 */
1264 printf("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1265 "but no VOP_SETACL()\n");
1266 /* panic("ufs_do_posix1e_acl_inheritance_file: VOP_GETACL() "
1267 "but no VOP_SETACL()"); */
1268 break;
1269
1270 default:
1271 goto out;
1272 }
1273
1274 out:
1275 acl_free(acl);
1276
1277 return (error);
1278 }
1279
1280 #endif /* UFS_ACL */
1281
1282 /*
1283 * Mkdir system call
1284 */
1285 static int
1286 ext2_mkdir(struct vop_mkdir_args *ap)
1287 {
1288 struct m_ext2fs *fs;
1289 struct vnode *dvp = ap->a_dvp;
1290 struct vattr *vap = ap->a_vap;
1291 struct componentname *cnp = ap->a_cnp;
1292 struct inode *ip, *dp;
1293 struct vnode *tvp;
1294 struct dirtemplate dirtemplate, *dtp;
1295 char *buf = NULL;
1296 int error, dmode;
1297
1298 #ifdef INVARIANTS
1299 if ((cnp->cn_flags & HASBUF) == 0)
1300 panic("ext2_mkdir: no name");
1301 #endif
1302 dp = VTOI(dvp);
1303 if ((nlink_t)dp->i_nlink >= EXT4_LINK_MAX &&
1304 !EXT2_HAS_RO_COMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
1305 error = EMLINK;
1306 goto out;
1307 }
1308 dmode = vap->va_mode & 0777;
1309 dmode |= IFDIR;
1310 /*
1311 * Must simulate part of ext2_makeinode here to acquire the inode,
1312 * but not have it entered in the parent directory. The entry is
1313 * made later after writing "." and ".." entries.
1314 */
1315 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
1316 if (error)
1317 goto out;
1318 ip = VTOI(tvp);
1319 fs = ip->i_e2fs;
1320 ip->i_gid = dp->i_gid;
1321 #ifdef SUIDDIR
1322 {
1323 /*
1324 * if we are hacking owners here, (only do this where told to)
1325 * and we are not giving it TOO root, (would subvert quotas)
1326 * then go ahead and give it to the other user.
1327 * The new directory also inherits the SUID bit.
1328 * If user's UID and dir UID are the same,
1329 * 'give it away' so that the SUID is still forced on.
1330 */
1331 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1332 (dp->i_mode & ISUID) && dp->i_uid) {
1333 dmode |= ISUID;
1334 ip->i_uid = dp->i_uid;
1335 } else {
1336 ip->i_uid = cnp->cn_cred->cr_uid;
1337 }
1338 }
1339 #else
1340 ip->i_uid = cnp->cn_cred->cr_uid;
1341 #endif
1342 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1343 ip->i_mode = dmode;
1344 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
1345 ip->i_nlink = 2;
1346 if (cnp->cn_flags & ISWHITEOUT)
1347 ip->i_flags |= UF_OPAQUE;
1348 error = ext2_update(tvp, 1);
1349
1350 /*
1351 * Bump link count in parent directory
1352 * to reflect work done below. Should
1353 * be done before reference is created
1354 * so reparation is possible if we crash.
1355 */
1356 ext2_inc_nlink(dp);
1357 dp->i_flag |= IN_CHANGE;
1358 error = ext2_update(dvp, !DOINGASYNC(dvp));
1359 if (error)
1360 goto bad;
1361
1362 /* Initialize directory with "." and ".." from static template. */
1363 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1364 EXT2F_INCOMPAT_FTYPE))
1365 dtp = &mastertemplate;
1366 else
1367 dtp = &omastertemplate;
1368 dirtemplate = *dtp;
1369 dirtemplate.dot_ino = ip->i_number;
1370 dirtemplate.dotdot_ino = dp->i_number;
1371 /*
1372 * note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE so let's
1373 * just redefine it - for this function only
1374 */
1375 #undef DIRBLKSIZ
1376 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize
1377 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
1378 buf = malloc(DIRBLKSIZ, M_TEMP, M_WAITOK | M_ZERO);
1379 if (!buf) {
1380 error = ENOMEM;
1381 ext2_dec_nlink(dp);
1382 dp->i_flag |= IN_CHANGE;
1383 goto bad;
1384 }
1385 if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
1386 dirtemplate.dotdot_reclen -= sizeof(struct ext2fs_direct_tail);
1387 ext2_init_dirent_tail(EXT2_DIRENT_TAIL(buf, DIRBLKSIZ));
1388 }
1389 memcpy(buf, &dirtemplate, sizeof(dirtemplate));
1390 ext2_dirent_csum_set(ip, (struct ext2fs_direct_2 *)buf);
1391 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)buf,
1392 DIRBLKSIZ, (off_t)0, UIO_SYSSPACE,
1393 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
1394 NULL, NULL);
1395 if (error) {
1396 ext2_dec_nlink(dp);
1397 dp->i_flag |= IN_CHANGE;
1398 goto bad;
1399 }
1400 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
1401 /* XXX should grow with balloc() */
1402 panic("ext2_mkdir: blksize");
1403 else {
1404 ip->i_size = DIRBLKSIZ;
1405 ip->i_flag |= IN_CHANGE;
1406 }
1407
1408 #ifdef UFS_ACL
1409 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
1410 error = ext2_do_posix1e_acl_inheritance_dir(dvp, tvp, dmode,
1411 cnp->cn_cred, cnp->cn_thread);
1412 if (error)
1413 goto bad;
1414 }
1415
1416 #endif /* UFS_ACL */
1417
1418 /* Directory set up, now install its entry in the parent directory. */
1419 error = ext2_direnter(ip, dvp, cnp);
1420 if (error) {
1421 ext2_dec_nlink(dp);
1422 dp->i_flag |= IN_CHANGE;
1423 }
1424 bad:
1425 /*
1426 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
1427 * for us because we set the link count to 0.
1428 */
1429 if (error) {
1430 ip->i_nlink = 0;
1431 ip->i_flag |= IN_CHANGE;
1432 vput(tvp);
1433 } else
1434 *ap->a_vpp = tvp;
1435 out:
1436 free(buf, M_TEMP);
1437 return (error);
1438 #undef DIRBLKSIZ
1439 #define DIRBLKSIZ DEV_BSIZE
1440 }
1441
1442 /*
1443 * Rmdir system call.
1444 */
1445 static int
1446 ext2_rmdir(struct vop_rmdir_args *ap)
1447 {
1448 struct vnode *vp = ap->a_vp;
1449 struct vnode *dvp = ap->a_dvp;
1450 struct componentname *cnp = ap->a_cnp;
1451 struct inode *ip, *dp;
1452 int error;
1453
1454 ip = VTOI(vp);
1455 dp = VTOI(dvp);
1456
1457 /*
1458 * Verify the directory is empty (and valid).
1459 * (Rmdir ".." won't be valid since
1460 * ".." will contain a reference to
1461 * the current directory and thus be
1462 * non-empty.)
1463 */
1464 if (!ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1465 error = ENOTEMPTY;
1466 goto out;
1467 }
1468 if ((dp->i_flags & APPEND)
1469 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1470 error = EPERM;
1471 goto out;
1472 }
1473 /*
1474 * Delete reference to directory before purging
1475 * inode. If we crash in between, the directory
1476 * will be reattached to lost+found,
1477 */
1478 error = ext2_dirremove(dvp, cnp);
1479 if (error)
1480 goto out;
1481 ext2_dec_nlink(dp);
1482 dp->i_flag |= IN_CHANGE;
1483 cache_purge(dvp);
1484 VOP_UNLOCK(dvp, 0);
1485 /*
1486 * Truncate inode. The only stuff left
1487 * in the directory is "." and "..".
1488 */
1489 ip->i_nlink = 0;
1490 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
1491 cnp->cn_thread);
1492 cache_purge(ITOV(ip));
1493 if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1494 VOP_UNLOCK(vp, 0);
1495 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1496 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1497 }
1498 out:
1499 return (error);
1500 }
1501
1502 /*
1503 * symlink -- make a symbolic link
1504 */
1505 static int
1506 ext2_symlink(struct vop_symlink_args *ap)
1507 {
1508 struct vnode *vp, **vpp = ap->a_vpp;
1509 struct inode *ip;
1510 int len, error;
1511
1512 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1513 vpp, ap->a_cnp);
1514 if (error)
1515 return (error);
1516 vp = *vpp;
1517 len = strlen(ap->a_target);
1518 if (len < vp->v_mount->mnt_maxsymlinklen) {
1519 ip = VTOI(vp);
1520 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1521 ip->i_size = len;
1522 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1523 } else
1524 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
1525 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1526 ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
1527 if (error)
1528 vput(vp);
1529 return (error);
1530 }
1531
1532 /*
1533 * Return target name of a symbolic link
1534 */
1535 static int
1536 ext2_readlink(struct vop_readlink_args *ap)
1537 {
1538 struct vnode *vp = ap->a_vp;
1539 struct inode *ip = VTOI(vp);
1540 int isize;
1541
1542 isize = ip->i_size;
1543 if (isize < vp->v_mount->mnt_maxsymlinklen) {
1544 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1545 return (0);
1546 }
1547 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1548 }
1549
1550 /*
1551 * Calculate the logical to physical mapping if not done already,
1552 * then call the device strategy routine.
1553 *
1554 * In order to be able to swap to a file, the ext2_bmaparray() operation may not
1555 * deadlock on memory. See ext2_bmap() for details.
1556 */
1557 static int
1558 ext2_strategy(struct vop_strategy_args *ap)
1559 {
1560 struct buf *bp = ap->a_bp;
1561 struct vnode *vp = ap->a_vp;
1562 struct bufobj *bo;
1563 daddr_t blkno;
1564 int error;
1565
1566 if (vp->v_type == VBLK || vp->v_type == VCHR)
1567 panic("ext2_strategy: spec");
1568 if (bp->b_blkno == bp->b_lblkno) {
1569
1570 if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
1571 error = ext4_bmapext(vp, bp->b_lblkno, &blkno, NULL, NULL);
1572 else
1573 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
1574
1575 bp->b_blkno = blkno;
1576 if (error) {
1577 bp->b_error = error;
1578 bp->b_ioflags |= BIO_ERROR;
1579 bufdone(bp);
1580 return (0);
1581 }
1582 if ((long)bp->b_blkno == -1)
1583 vfs_bio_clrbuf(bp);
1584 }
1585 if ((long)bp->b_blkno == -1) {
1586 bufdone(bp);
1587 return (0);
1588 }
1589 bp->b_iooffset = dbtob(bp->b_blkno);
1590 bo = VFSTOEXT2(vp->v_mount)->um_bo;
1591 BO_STRATEGY(bo, bp);
1592 return (0);
1593 }
1594
1595 /*
1596 * Print out the contents of an inode.
1597 */
1598 static int
1599 ext2_print(struct vop_print_args *ap)
1600 {
1601 struct vnode *vp = ap->a_vp;
1602 struct inode *ip = VTOI(vp);
1603
1604 vn_printf(ip->i_devvp, "\tino %ju", (uintmax_t)ip->i_number);
1605 if (vp->v_type == VFIFO)
1606 fifo_printinfo(vp);
1607 printf("\n");
1608 return (0);
1609 }
1610
1611 /*
1612 * Close wrapper for fifos.
1613 *
1614 * Update the times on the inode then do device close.
1615 */
1616 static int
1617 ext2fifo_close(struct vop_close_args *ap)
1618 {
1619 struct vnode *vp = ap->a_vp;
1620
1621 VI_LOCK(vp);
1622 if (vp->v_usecount > 1)
1623 ext2_itimes_locked(vp);
1624 VI_UNLOCK(vp);
1625 return (fifo_specops.vop_close(ap));
1626 }
1627
1628 /*
1629 * Kqfilter wrapper for fifos.
1630 *
1631 * Fall through to ext2 kqfilter routines if needed
1632 */
1633 static int
1634 ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1635 {
1636 int error;
1637
1638 error = fifo_specops.vop_kqfilter(ap);
1639 if (error)
1640 error = vfs_kqfilter(ap);
1641 return (error);
1642 }
1643
1644 /*
1645 * Return POSIX pathconf information applicable to ext2 filesystems.
1646 */
1647 static int
1648 ext2_pathconf(struct vop_pathconf_args *ap)
1649 {
1650 int error = 0;
1651
1652 switch (ap->a_name) {
1653 case _PC_LINK_MAX:
1654 if (EXT2_HAS_RO_COMPAT_FEATURE(VTOI(ap->a_vp)->i_e2fs,
1655 EXT2F_ROCOMPAT_DIR_NLINK))
1656 *ap->a_retval = INT_MAX;
1657 else
1658 *ap->a_retval = EXT4_LINK_MAX;
1659 break;
1660 case _PC_NAME_MAX:
1661 *ap->a_retval = NAME_MAX;
1662 break;
1663 case _PC_PIPE_BUF:
1664 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
1665 *ap->a_retval = PIPE_BUF;
1666 else
1667 error = EINVAL;
1668 break;
1669 case _PC_CHOWN_RESTRICTED:
1670 *ap->a_retval = 1;
1671 break;
1672 case _PC_NO_TRUNC:
1673 *ap->a_retval = 1;
1674 break;
1675
1676 #ifdef UFS_ACL
1677 case _PC_ACL_EXTENDED:
1678 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1679 *ap->a_retval = 1;
1680 else
1681 *ap->a_retval = 0;
1682 break;
1683 case _PC_ACL_PATH_MAX:
1684 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
1685 *ap->a_retval = ACL_MAX_ENTRIES;
1686 else
1687 *ap->a_retval = 3;
1688 break;
1689 #endif /* UFS_ACL */
1690
1691 case _PC_MIN_HOLE_SIZE:
1692 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1693 break;
1694 case _PC_PRIO_IO:
1695 *ap->a_retval = 0;
1696 break;
1697 case _PC_SYNC_IO:
1698 *ap->a_retval = 0;
1699 break;
1700 case _PC_ALLOC_SIZE_MIN:
1701 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
1702 break;
1703 case _PC_FILESIZEBITS:
1704 *ap->a_retval = 64;
1705 break;
1706 case _PC_REC_INCR_XFER_SIZE:
1707 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1708 break;
1709 case _PC_REC_MAX_XFER_SIZE:
1710 *ap->a_retval = -1; /* means ``unlimited'' */
1711 break;
1712 case _PC_REC_MIN_XFER_SIZE:
1713 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
1714 break;
1715 case _PC_REC_XFER_ALIGN:
1716 *ap->a_retval = PAGE_SIZE;
1717 break;
1718 case _PC_SYMLINK_MAX:
1719 *ap->a_retval = MAXPATHLEN;
1720 break;
1721
1722 default:
1723 error = vop_stdpathconf(ap);
1724 break;
1725 }
1726 return (error);
1727 }
1728
1729 /*
1730 * Vnode operation to remove a named attribute.
1731 */
1732 static int
1733 ext2_deleteextattr(struct vop_deleteextattr_args *ap)
1734 {
1735 struct inode *ip;
1736 struct m_ext2fs *fs;
1737 int error;
1738
1739 ip = VTOI(ap->a_vp);
1740 fs = ip->i_e2fs;
1741
1742 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1743 return (EOPNOTSUPP);
1744
1745 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1746 return (EOPNOTSUPP);
1747
1748 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1749 ap->a_cred, ap->a_td, VWRITE);
1750 if (error)
1751 return (error);
1752
1753 error = ENOATTR;
1754
1755 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1756 error = ext2_extattr_inode_delete(ip, ap->a_attrnamespace, ap->a_name);
1757 if (error != ENOATTR)
1758 return (error);
1759 }
1760
1761 if (ip->i_facl)
1762 error = ext2_extattr_block_delete(ip, ap->a_attrnamespace, ap->a_name);
1763
1764 return (error);
1765 }
1766
1767 /*
1768 * Vnode operation to retrieve a named extended attribute.
1769 */
1770 static int
1771 ext2_getextattr(struct vop_getextattr_args *ap)
1772 {
1773 struct inode *ip;
1774 struct m_ext2fs *fs;
1775 int error;
1776
1777 ip = VTOI(ap->a_vp);
1778 fs = ip->i_e2fs;
1779
1780 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1781 return (EOPNOTSUPP);
1782
1783 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1784 return (EOPNOTSUPP);
1785
1786 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1787 ap->a_cred, ap->a_td, VREAD);
1788 if (error)
1789 return (error);
1790
1791 if (ap->a_size != NULL)
1792 *ap->a_size = 0;
1793
1794 error = ENOATTR;
1795
1796 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1797 error = ext2_extattr_inode_get(ip, ap->a_attrnamespace,
1798 ap->a_name, ap->a_uio, ap->a_size);
1799 if (error != ENOATTR)
1800 return (error);
1801 }
1802
1803 if (ip->i_facl)
1804 error = ext2_extattr_block_get(ip, ap->a_attrnamespace,
1805 ap->a_name, ap->a_uio, ap->a_size);
1806
1807 return (error);
1808 }
1809
1810 /*
1811 * Vnode operation to retrieve extended attributes on a vnode.
1812 */
1813 static int
1814 ext2_listextattr(struct vop_listextattr_args *ap)
1815 {
1816 struct inode *ip;
1817 struct m_ext2fs *fs;
1818 int error;
1819
1820 ip = VTOI(ap->a_vp);
1821 fs = ip->i_e2fs;
1822
1823 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1824 return (EOPNOTSUPP);
1825
1826 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1827 return (EOPNOTSUPP);
1828
1829 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1830 ap->a_cred, ap->a_td, VREAD);
1831 if (error)
1832 return (error);
1833
1834 if (ap->a_size != NULL)
1835 *ap->a_size = 0;
1836
1837 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1838 error = ext2_extattr_inode_list(ip, ap->a_attrnamespace,
1839 ap->a_uio, ap->a_size);
1840 if (error)
1841 return (error);
1842 }
1843
1844 if (ip->i_facl)
1845 error = ext2_extattr_block_list(ip, ap->a_attrnamespace,
1846 ap->a_uio, ap->a_size);
1847
1848 return (error);
1849 }
1850
1851 /*
1852 * Vnode operation to set a named attribute.
1853 */
1854 static int
1855 ext2_setextattr(struct vop_setextattr_args *ap)
1856 {
1857 struct inode *ip;
1858 struct m_ext2fs *fs;
1859 int error;
1860
1861 ip = VTOI(ap->a_vp);
1862 fs = ip->i_e2fs;
1863
1864 if (!EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_EXT_ATTR))
1865 return (EOPNOTSUPP);
1866
1867 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1868 return (EOPNOTSUPP);
1869
1870 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1871 ap->a_cred, ap->a_td, VWRITE);
1872 if (error)
1873 return (error);
1874
1875 error = ext2_extattr_valid_attrname(ap->a_attrnamespace, ap->a_name);
1876 if (error)
1877 return (error);
1878
1879 if (EXT2_INODE_SIZE(fs) != E2FS_REV0_INODE_SIZE) {
1880 error = ext2_extattr_inode_set(ip, ap->a_attrnamespace,
1881 ap->a_name, ap->a_uio);
1882 if (error != ENOSPC)
1883 return (error);
1884 }
1885
1886 error = ext2_extattr_block_set(ip, ap->a_attrnamespace,
1887 ap->a_name, ap->a_uio);
1888
1889 return (error);
1890 }
1891
1892 /*
1893 * Vnode pointer to File handle
1894 */
1895 /* ARGSUSED */
1896 static int
1897 ext2_vptofh(struct vop_vptofh_args *ap)
1898 {
1899 struct inode *ip;
1900 struct ufid *ufhp;
1901
1902 ip = VTOI(ap->a_vp);
1903 ufhp = (struct ufid *)ap->a_fhp;
1904 ufhp->ufid_len = sizeof(struct ufid);
1905 ufhp->ufid_ino = ip->i_number;
1906 ufhp->ufid_gen = ip->i_gen;
1907 return (0);
1908 }
1909
1910 /*
1911 * Initialize the vnode associated with a new inode, handle aliased
1912 * vnodes.
1913 */
1914 int
1915 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp)
1916 {
1917 struct inode *ip;
1918 struct vnode *vp;
1919
1920 vp = *vpp;
1921 ip = VTOI(vp);
1922 vp->v_type = IFTOVT(ip->i_mode);
1923 if (vp->v_type == VFIFO)
1924 vp->v_op = fifoops;
1925
1926 if (ip->i_number == EXT2_ROOTINO)
1927 vp->v_vflag |= VV_ROOT;
1928 ip->i_modrev = init_va_filerev();
1929 *vpp = vp;
1930 return (0);
1931 }
1932
1933 /*
1934 * Allocate a new inode.
1935 */
1936 static int
1937 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1938 struct componentname *cnp)
1939 {
1940 struct inode *ip, *pdir;
1941 struct vnode *tvp;
1942 int error;
1943
1944 pdir = VTOI(dvp);
1945 #ifdef INVARIANTS
1946 if ((cnp->cn_flags & HASBUF) == 0)
1947 panic("ext2_makeinode: no name");
1948 #endif
1949 *vpp = NULL;
1950 if ((mode & IFMT) == 0)
1951 mode |= IFREG;
1952
1953 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
1954 if (error) {
1955 return (error);
1956 }
1957 ip = VTOI(tvp);
1958 ip->i_gid = pdir->i_gid;
1959 #ifdef SUIDDIR
1960 {
1961 /*
1962 * if we are
1963 * not the owner of the directory,
1964 * and we are hacking owners here, (only do this where told to)
1965 * and we are not giving it TOO root, (would subvert quotas)
1966 * then go ahead and give it to the other user.
1967 * Note that this drops off the execute bits for security.
1968 */
1969 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1970 (pdir->i_mode & ISUID) &&
1971 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1972 ip->i_uid = pdir->i_uid;
1973 mode &= ~07111;
1974 } else {
1975 ip->i_uid = cnp->cn_cred->cr_uid;
1976 }
1977 }
1978 #else
1979 ip->i_uid = cnp->cn_cred->cr_uid;
1980 #endif
1981 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1982 ip->i_mode = mode;
1983 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1984 ip->i_nlink = 1;
1985 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
1986 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
1987 ip->i_mode &= ~ISGID;
1988 }
1989
1990 if (cnp->cn_flags & ISWHITEOUT)
1991 ip->i_flags |= UF_OPAQUE;
1992
1993 /*
1994 * Make sure inode goes to disk before directory entry.
1995 */
1996 error = ext2_update(tvp, !DOINGASYNC(tvp));
1997 if (error)
1998 goto bad;
1999
2000 #ifdef UFS_ACL
2001 if (dvp->v_mount->mnt_flag & MNT_ACLS) {
2002 error = ext2_do_posix1e_acl_inheritance_file(dvp, tvp, mode,
2003 cnp->cn_cred, cnp->cn_thread);
2004 if (error)
2005 goto bad;
2006 }
2007 #endif /* UFS_ACL */
2008
2009 error = ext2_direnter(ip, dvp, cnp);
2010 if (error)
2011 goto bad;
2012
2013 *vpp = tvp;
2014 return (0);
2015
2016 bad:
2017 /*
2018 * Write error occurred trying to update the inode
2019 * or the directory so must deallocate the inode.
2020 */
2021 ip->i_nlink = 0;
2022 ip->i_flag |= IN_CHANGE;
2023 vput(tvp);
2024 return (error);
2025 }
2026
2027 /*
2028 * Vnode op for reading.
2029 */
2030 static int
2031 ext2_read(struct vop_read_args *ap)
2032 {
2033 struct vnode *vp;
2034 struct inode *ip;
2035 struct uio *uio;
2036 struct m_ext2fs *fs;
2037 struct buf *bp;
2038 daddr_t lbn, nextlbn;
2039 off_t bytesinfile;
2040 long size, xfersize, blkoffset;
2041 int error, orig_resid, seqcount;
2042 int ioflag;
2043
2044 vp = ap->a_vp;
2045 uio = ap->a_uio;
2046 ioflag = ap->a_ioflag;
2047
2048 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
2049 ip = VTOI(vp);
2050
2051 #ifdef INVARIANTS
2052 if (uio->uio_rw != UIO_READ)
2053 panic("%s: mode", "ext2_read");
2054
2055 if (vp->v_type == VLNK) {
2056 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
2057 panic("%s: short symlink", "ext2_read");
2058 } else if (vp->v_type != VREG && vp->v_type != VDIR)
2059 panic("%s: type %d", "ext2_read", vp->v_type);
2060 #endif
2061 orig_resid = uio->uio_resid;
2062 KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
2063 if (orig_resid == 0)
2064 return (0);
2065 KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
2066 fs = ip->i_e2fs;
2067 if (uio->uio_offset < ip->i_size &&
2068 uio->uio_offset >= fs->e2fs_maxfilesize)
2069 return (EOVERFLOW);
2070
2071 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
2072 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
2073 break;
2074 lbn = lblkno(fs, uio->uio_offset);
2075 nextlbn = lbn + 1;
2076 size = blksize(fs, ip, lbn);
2077 blkoffset = blkoff(fs, uio->uio_offset);
2078
2079 xfersize = fs->e2fs_fsize - blkoffset;
2080 if (uio->uio_resid < xfersize)
2081 xfersize = uio->uio_resid;
2082 if (bytesinfile < xfersize)
2083 xfersize = bytesinfile;
2084
2085 if (lblktosize(fs, nextlbn) >= ip->i_size)
2086 error = bread(vp, lbn, size, NOCRED, &bp);
2087 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
2088 error = cluster_read(vp, ip->i_size, lbn, size,
2089 NOCRED, blkoffset + uio->uio_resid, seqcount,
2090 0, &bp);
2091 } else if (seqcount > 1) {
2092 u_int nextsize = blksize(fs, ip, nextlbn);
2093
2094 error = breadn(vp, lbn,
2095 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
2096 } else
2097 error = bread(vp, lbn, size, NOCRED, &bp);
2098 if (error) {
2099 brelse(bp);
2100 bp = NULL;
2101 break;
2102 }
2103
2104 /*
2105 * We should only get non-zero b_resid when an I/O error
2106 * has occurred, which should cause us to break above.
2107 * However, if the short read did not cause an error,
2108 * then we want to ensure that we do not uiomove bad
2109 * or uninitialized data.
2110 */
2111 size -= bp->b_resid;
2112 if (size < xfersize) {
2113 if (size == 0)
2114 break;
2115 xfersize = size;
2116 }
2117 error = uiomove((char *)bp->b_data + blkoffset,
2118 (int)xfersize, uio);
2119 if (error)
2120 break;
2121 vfs_bio_brelse(bp, ioflag);
2122 }
2123
2124 /*
2125 * This can only happen in the case of an error because the loop
2126 * above resets bp to NULL on each iteration and on normal
2127 * completion has not set a new value into it. so it must have come
2128 * from a 'break' statement
2129 */
2130 if (bp != NULL)
2131 vfs_bio_brelse(bp, ioflag);
2132
2133 if ((error == 0 || uio->uio_resid != orig_resid) &&
2134 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
2135 ip->i_flag |= IN_ACCESS;
2136 return (error);
2137 }
2138
2139 static int
2140 ext2_ioctl(struct vop_ioctl_args *ap)
2141 {
2142
2143 switch (ap->a_command) {
2144 case FIOSEEKDATA:
2145 case FIOSEEKHOLE:
2146 return (vn_bmap_seekhole(ap->a_vp, ap->a_command,
2147 (off_t *)ap->a_data, ap->a_cred));
2148 default:
2149 return (ENOTTY);
2150 }
2151 }
2152
2153 /*
2154 * Vnode op for writing.
2155 */
2156 static int
2157 ext2_write(struct vop_write_args *ap)
2158 {
2159 struct vnode *vp;
2160 struct uio *uio;
2161 struct inode *ip;
2162 struct m_ext2fs *fs;
2163 struct buf *bp;
2164 daddr_t lbn;
2165 off_t osize;
2166 int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
2167
2168 ioflag = ap->a_ioflag;
2169 uio = ap->a_uio;
2170 vp = ap->a_vp;
2171
2172 seqcount = ioflag >> IO_SEQSHIFT;
2173 ip = VTOI(vp);
2174
2175 #ifdef INVARIANTS
2176 if (uio->uio_rw != UIO_WRITE)
2177 panic("%s: mode", "ext2_write");
2178 #endif
2179
2180 switch (vp->v_type) {
2181 case VREG:
2182 if (ioflag & IO_APPEND)
2183 uio->uio_offset = ip->i_size;
2184 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
2185 return (EPERM);
2186 /* FALLTHROUGH */
2187 case VLNK:
2188 break;
2189 case VDIR:
2190 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
2191 if ((ioflag & IO_SYNC) == 0)
2192 panic("ext2_write: nonsync dir write");
2193 break;
2194 default:
2195 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
2196 vp->v_type, (intmax_t)uio->uio_offset,
2197 (intmax_t)uio->uio_resid);
2198 }
2199
2200 KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
2201 KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
2202 fs = ip->i_e2fs;
2203 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
2204 return (EFBIG);
2205 /*
2206 * Maybe this should be above the vnode op call, but so long as
2207 * file servers have no limits, I don't think it matters.
2208 */
2209 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
2210 return (EFBIG);
2211
2212 resid = uio->uio_resid;
2213 osize = ip->i_size;
2214 if (seqcount > BA_SEQMAX)
2215 flags = BA_SEQMAX << BA_SEQSHIFT;
2216 else
2217 flags = seqcount << BA_SEQSHIFT;
2218 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
2219 flags |= IO_SYNC;
2220
2221 for (error = 0; uio->uio_resid > 0;) {
2222 lbn = lblkno(fs, uio->uio_offset);
2223 blkoffset = blkoff(fs, uio->uio_offset);
2224 xfersize = fs->e2fs_fsize - blkoffset;
2225 if (uio->uio_resid < xfersize)
2226 xfersize = uio->uio_resid;
2227 if (uio->uio_offset + xfersize > ip->i_size)
2228 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
2229
2230 /*
2231 * We must perform a read-before-write if the transfer size
2232 * does not cover the entire buffer.
2233 */
2234 if (fs->e2fs_bsize > xfersize)
2235 flags |= BA_CLRBUF;
2236 else
2237 flags &= ~BA_CLRBUF;
2238 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
2239 ap->a_cred, &bp, flags);
2240 if (error != 0)
2241 break;
2242
2243 if ((ioflag & (IO_SYNC | IO_INVAL)) == (IO_SYNC | IO_INVAL))
2244 bp->b_flags |= B_NOCACHE;
2245 if (uio->uio_offset + xfersize > ip->i_size)
2246 ip->i_size = uio->uio_offset + xfersize;
2247 size = blksize(fs, ip, lbn) - bp->b_resid;
2248 if (size < xfersize)
2249 xfersize = size;
2250
2251 error =
2252 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
2253 /*
2254 * If the buffer is not already filled and we encounter an
2255 * error while trying to fill it, we have to clear out any
2256 * garbage data from the pages instantiated for the buffer.
2257 * If we do not, a failed uiomove() during a write can leave
2258 * the prior contents of the pages exposed to a userland mmap.
2259 *
2260 * Note that we need only clear buffers with a transfer size
2261 * equal to the block size because buffers with a shorter
2262 * transfer size were cleared above by the call to ext2_balloc()
2263 * with the BA_CLRBUF flag set.
2264 *
2265 * If the source region for uiomove identically mmaps the
2266 * buffer, uiomove() performed the NOP copy, and the buffer
2267 * content remains valid because the page fault handler
2268 * validated the pages.
2269 */
2270 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
2271 fs->e2fs_bsize == xfersize)
2272 vfs_bio_clrbuf(bp);
2273
2274 vfs_bio_set_flags(bp, ioflag);
2275
2276 /*
2277 * If IO_SYNC each buffer is written synchronously. Otherwise
2278 * if we have a severe page deficiency write the buffer
2279 * asynchronously. Otherwise try to cluster, and if that
2280 * doesn't do it then either do an async write (if O_DIRECT),
2281 * or a delayed write (if not).
2282 */
2283 if (ioflag & IO_SYNC) {
2284 (void)bwrite(bp);
2285 } else if (vm_page_count_severe() ||
2286 buf_dirty_count_severe() ||
2287 (ioflag & IO_ASYNC)) {
2288 bp->b_flags |= B_CLUSTEROK;
2289 bawrite(bp);
2290 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
2291 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
2292 bp->b_flags |= B_CLUSTEROK;
2293 cluster_write(vp, bp, ip->i_size, seqcount, 0);
2294 } else {
2295 bawrite(bp);
2296 }
2297 } else if (ioflag & IO_DIRECT) {
2298 bp->b_flags |= B_CLUSTEROK;
2299 bawrite(bp);
2300 } else {
2301 bp->b_flags |= B_CLUSTEROK;
2302 bdwrite(bp);
2303 }
2304 if (error || xfersize == 0)
2305 break;
2306 }
2307 /*
2308 * If we successfully wrote any data, and we are not the superuser
2309 * we clear the setuid and setgid bits as a precaution against
2310 * tampering.
2311 */
2312 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
2313 ap->a_cred) {
2314 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
2315 ip->i_mode &= ~(ISUID | ISGID);
2316 }
2317 if (error) {
2318 if (ioflag & IO_UNIT) {
2319 (void)ext2_truncate(vp, osize,
2320 ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
2321 uio->uio_offset -= resid - uio->uio_resid;
2322 uio->uio_resid = resid;
2323 }
2324 }
2325 if (uio->uio_resid != resid) {
2326 ip->i_flag |= IN_CHANGE | IN_UPDATE;
2327 if (ioflag & IO_SYNC)
2328 error = ext2_update(vp, 1);
2329 }
2330 return (error);
2331 }
Cache object: 952979918ecf3306364ace80832ff89f
|