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