Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/ufs Factor out post-read/write inode updates in UFS.



details:   https://anonhg.NetBSD.org/src/rev/c81f0ab42d27
branches:  trunk
changeset: 336967:c81f0ab42d27
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sat Mar 28 17:06:15 2015 +0000

description:
Factor out post-read/write inode updates in UFS.

diffstat:

 sys/ufs/ext2fs/ext2fs_readwrite.c |   91 +++++++++++++++--------------
 sys/ufs/lfs/ulfs_readwrite.c      |  101 +++++++++++++++-----------------
 sys/ufs/ufs/ufs_readwrite.c       |  116 +++++++++++++++++--------------------
 3 files changed, 147 insertions(+), 161 deletions(-)

diffs (truncated from 536 to 300 lines):

diff -r 06615d7feef1 -r c81f0ab42d27 sys/ufs/ext2fs/ext2fs_readwrite.c
--- a/sys/ufs/ext2fs/ext2fs_readwrite.c Sat Mar 28 16:57:23 2015 +0000
+++ b/sys/ufs/ext2fs/ext2fs_readwrite.c Sat Mar 28 17:06:15 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ext2fs_readwrite.c,v 1.70 2015/03/28 03:53:36 riastradh Exp $  */
+/*     $NetBSD: ext2fs_readwrite.c,v 1.71 2015/03/28 17:06:15 riastradh Exp $  */
 
 /*-
  * Copyright (c) 1993
@@ -60,7 +60,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.70 2015/03/28 03:53:36 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.71 2015/03/28 17:06:15 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -81,6 +81,10 @@
 #include <ufs/ext2fs/ext2fs.h>
 #include <ufs/ext2fs/ext2fs_extern.h>
 
+static int     ext2fs_post_read_update(struct vnode *, int, int);
+static int     ext2fs_post_write_update(struct vnode *, struct uio *, int,
+                   kauth_cred_t, off_t, int, int, int);
+
 /*
  * Vnode op for reading.
  */
@@ -137,11 +141,7 @@
        }
 
 out:
-       if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
-               ip->i_flag |= IN_ACCESS;
-               if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
-                       error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
-       }
+       error = ext2fs_post_read_update(vp, ap->a_ioflag, error);
        return (error);
 }
 
@@ -227,12 +227,22 @@
                brelse(bp, 0);
 
 out:
+       error = ext2fs_post_read_update(vp, ioflag, error);
+       return (error);
+}
+
+static int
+ext2fs_post_read_update(struct vnode *vp, int ioflag, int error)
+{
+       struct inode *ip = VTOI(vp);
+
        if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
                ip->i_flag |= IN_ACCESS;
                if ((ioflag & IO_SYNC) == IO_SYNC)
                        error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
        }
-       return (error);
+
+       return error;
 }
 
 /*
@@ -335,36 +345,8 @@
                    PGO_CLEANIT | PGO_SYNCIO);
        }
 
-       /*
-        * If we successfully wrote any data, and we are not the superuser
-        * we clear the setuid and setgid bits as a precaution against
-        * tampering.
-        */
-       ip->i_flag |= IN_CHANGE | IN_UPDATE;
-       if (vp->v_mount->mnt_flag & MNT_RELATIME)
-               ip->i_flag |= IN_ACCESS;
-       if (resid > uio->uio_resid && ap->a_cred) {
-               if (ip->i_e2fs_mode & ISUID) {
-                       if (kauth_authorize_vnode(ap->a_cred,
-                           KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
-                               ip->i_e2fs_mode &= ISUID;
-               }
-
-               if (ip->i_e2fs_mode & ISGID) {
-                       if (kauth_authorize_vnode(ap->a_cred,
-                           KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
-                               ip->i_e2fs_mode &= ~ISGID;
-               }
-       }
-       if (resid > uio->uio_resid)
-               VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
-       if (error) {
-               (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
-               uio->uio_offset -= resid - uio->uio_resid;
-               uio->uio_resid = resid;
-       } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
-               error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
-       KASSERT(vp->v_size == ext2fs_size(ip));
+       error = ext2fs_post_write_update(vp, uio, ioflag, ap->a_cred, osize,
+           resid, extended, error);
        return (error);
 }
 
@@ -445,14 +427,27 @@
                        break;
        }
 
+       error = ext2fs_post_write_update(vp, uio, ioflag, cred, osize, resid,
+           extended, error);
+       return (error);
+}
+
+static int
+ext2fs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
+    kauth_cred_t cred, off_t osize, int resid, int extended, int error)
+{
+       struct inode *ip = VTOI(vp);
+
+       /* Trigger ctime and mtime updates, and atime if MNT_RELATIME.  */
+       ip->i_flag |= IN_CHANGE | IN_UPDATE;
+       if (vp->v_mount->mnt_flag & MNT_RELATIME)
+               ip->i_flag |= IN_ACCESS;
+
        /*
-        * If we successfully wrote any data, and we are not the superuser
+        * If we successfully wrote any data and we are not the superuser,
         * we clear the setuid and setgid bits as a precaution against
         * tampering.
         */
-       ip->i_flag |= IN_CHANGE | IN_UPDATE;
-       if (vp->v_mount->mnt_flag & MNT_RELATIME)
-               ip->i_flag |= IN_ACCESS;
        if (resid > uio->uio_resid && cred) {
                if (ip->i_e2fs_mode & ISUID) {
                        if (kauth_authorize_vnode(cred,
@@ -466,14 +461,24 @@
                                ip->i_e2fs_mode &= ~ISGID;
                }
        }
+
+       /* If we successfully wrote anything, notify kevent listeners.  */
        if (resid > uio->uio_resid)
                VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
+
+       /*
+        * Update the size on disk: truncate back to original size on
+        * error, or reflect the new size on success.
+        */
        if (error) {
                (void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
                uio->uio_offset -= resid - uio->uio_resid;
                uio->uio_resid = resid;
        } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
                error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
+
+       /* Make sure the vnode uvm size matches the inode file size.  */
        KASSERT(vp->v_size == ext2fs_size(ip));
-       return (error);
+
+       return error;
 }
diff -r 06615d7feef1 -r c81f0ab42d27 sys/ufs/lfs/ulfs_readwrite.c
--- a/sys/ufs/lfs/ulfs_readwrite.c      Sat Mar 28 16:57:23 2015 +0000
+++ b/sys/ufs/lfs/ulfs_readwrite.c      Sat Mar 28 17:06:15 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ulfs_readwrite.c,v 1.10 2015/03/28 03:53:36 riastradh Exp $    */
+/*     $NetBSD: ulfs_readwrite.c,v 1.11 2015/03/28 17:06:15 riastradh Exp $    */
 /*  from NetBSD: ufs_readwrite.c,v 1.105 2013/01/22 09:39:18 dholland Exp  */
 
 /*-
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.10 2015/03/28 03:53:36 riastradh Exp $");
+__KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.11 2015/03/28 17:06:15 riastradh Exp $");
 
 #ifdef LFS_READWRITE
 #define        FS                      struct lfs
@@ -57,6 +57,10 @@
 #define        BUFWR                   ffs_bufwr
 #endif
 
+static int     ulfs_post_read_update(struct vnode *, int, int);
+static int     ulfs_post_write_update(struct vnode *, struct uio *, int,
+                   kauth_cred_t, off_t, int, int, int);
+
 /*
  * Vnode op for reading.
  */
@@ -126,13 +130,7 @@
        }
 
  out:
-       if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
-               ip->i_flag |= IN_ACCESS;
-               if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
-                       error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
-               }
-       }
-
+       error = ulfs_post_read_update(vp, ap->a_ioflag, error);
        fstrans_done(vp->v_mount);
        return (error);
 }
@@ -223,6 +221,16 @@
                brelse(bp, 0);
 
  out:
+       error = ulfs_post_read_update(vp, ioflag, error);
+       fstrans_done(vp->v_mount);
+       return (error);
+}
+
+static int
+ulfs_post_read_update(struct vnode *vp, int ioflag, int error)
+{
+       struct inode *ip = VTOI(vp);
+
        if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
                ip->i_flag |= IN_ACCESS;
                if ((ioflag & IO_SYNC) == IO_SYNC) {
@@ -230,8 +238,7 @@
                }
        }
 
-       fstrans_done(vp->v_mount);
-       return (error);
+       return error;
 }
 
 /*
@@ -436,44 +443,9 @@
                    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
        }
 
-       /*
-        * If we successfully wrote any data, and we are not the superuser
-        * we clear the setuid and setgid bits as a precaution against
-        * tampering.
-        */
 out:
-       ip->i_flag |= IN_CHANGE | IN_UPDATE;
-       if (vp->v_mount->mnt_flag & MNT_RELATIME)
-               ip->i_flag |= IN_ACCESS;
-       if (resid > uio->uio_resid && ap->a_cred) {
-               if (ip->i_mode & ISUID) {
-                       if (kauth_authorize_vnode(ap->a_cred,
-                           KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
-                               ip->i_mode &= ~ISUID;
-                               DIP_ASSIGN(ip, mode, ip->i_mode);
-                       }
-               }
-
-               if (ip->i_mode & ISGID) {
-                       if (kauth_authorize_vnode(ap->a_cred,
-                           KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
-                               ip->i_mode &= ~ISGID;
-                               DIP_ASSIGN(ip, mode, ip->i_mode);
-                       }
-               }
-       }
-       if (resid > uio->uio_resid)
-               VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
-       if (error) {
-               (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
-               uio->uio_offset -= resid - uio->uio_resid;
-               uio->uio_resid = resid;
-       } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
-               error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
-       } else {
-               /* nothing */
-       }
-       KASSERT(vp->v_size == ip->i_size);
+       error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
+           extended, error);
        fstrans_done(vp->v_mount);
 
        return (error);
@@ -603,14 +575,29 @@
        }
 #endif
 
+       error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
+           extended, error);
+       fstrans_done(vp->v_mount);
+
+       return (error);
+}
+
+static int
+ulfs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
+    kauth_cred_t cred, off_t osize, int resid, int extended, int error)
+{
+       struct inode *ip = VTOI(vp);
+
+       /* Trigger ctime and mtime updates, and atime if MNT_RELATIME.  */
+       ip->i_flag |= IN_CHANGE | IN_UPDATE;



Home | Main Index | Thread Index | Old Index