Home | History | Annotate | Line # | Download | only in lfs
      1 /*	$NetBSD: lfs_itimes.c,v 1.22 2026/01/05 05:02:47 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: lfs_itimes.c,v 1.22 2026/01/05 05:02:47 perseant Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/time.h>
     36 #include <sys/mount.h>
     37 #include <sys/buf.h>
     38 
     39 #ifndef _KERNEL
     40 #include "bufcache.h"
     41 #include "vnode.h"
     42 #include "lfs_user.h"
     43 #define vnode uvnode
     44 #define buf ubuf
     45 #define panic call_panic
     46 #else
     47 #include <ufs/lfs/ulfs_inode.h>
     48 #include <ufs/lfs/lfs_extern.h>
     49 #include <ufs/lfs/lfs_kernel.h>
     50 #include <sys/kauth.h>
     51 #endif
     52 
     53 #include <ufs/lfs/lfs.h>
     54 #include <ufs/lfs/lfs_accessors.h>
     55 #include <ufs/lfs/lfs_inode.h>
     56 
     57 void
     58 lfs_itimes(struct inode *ip, const struct timespec *acc,
     59     const struct timespec *mod, const struct timespec *cre)
     60 {
     61 	struct lfs *fs = ip->i_lfs;
     62 #ifdef _KERNEL
     63 	struct timespec now;
     64 
     65 	KASSERT(ip->i_state & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY));
     66 
     67 	vfs_timestamp(&now);
     68 #endif
     69 
     70 	if (ip->i_state & IN_ACCESS) {
     71 #ifdef _KERNEL
     72 		if (acc == NULL)
     73 			acc = &now;
     74 #endif
     75 		lfs_dino_setatime(fs, ip->i_din, acc->tv_sec);
     76 		lfs_dino_setatimensec(fs, ip->i_din, acc->tv_nsec);
     77 		if (fs->lfs_is64 || lfs_sb_getversion(fs) > 1) {
     78 			struct buf *ibp;
     79 			IFILE *ifp;
     80 
     81 #ifdef _KERNEL
     82 			if (!LFS_SEGLOCK_HELD(fs))
     83 				lfs_fraglock_enter(fs, RW_READER);
     84 #endif /* _KERNEL */
     85 			LFS_IENTRY(ifp, fs, ip->i_number, ibp);
     86 			lfs_if_setatime_sec(fs, ifp, acc->tv_sec);
     87 			lfs_if_setatime_nsec(fs, ifp, acc->tv_nsec);
     88 #ifdef _KERNEL
     89 			LFS_WRITEIENTRY(ifp, fs, ip->i_number, ibp);
     90 			if (!LFS_SEGLOCK_HELD(fs))
     91 				lfs_fraglock_exit(fs);
     92 #else /* ! _KERNEL */
     93 			LFS_BWRITE_LOG(ibp);
     94 #endif /* ! _KERNEL */
     95 			mutex_enter(&lfs_lock);
     96 			fs->lfs_flags |= LFS_IFDIRTY;
     97 			mutex_exit(&lfs_lock);
     98 		} else {
     99 			mutex_enter(&lfs_lock);
    100 			LFS_SET_UINO(ip, IN_ACCESSED);
    101 			mutex_exit(&lfs_lock);
    102 		}
    103 	}
    104 	if (ip->i_state & (IN_CHANGE | IN_UPDATE | IN_MODIFY)) {
    105 		if (ip->i_state & (IN_UPDATE | IN_MODIFY)) {
    106 #ifdef _KERNEL
    107 			if (mod == NULL)
    108 				mod = &now;
    109 #endif
    110 			lfs_dino_setmtime(fs, ip->i_din, mod->tv_sec);
    111 			lfs_dino_setmtimensec(fs, ip->i_din, mod->tv_nsec);
    112 			ip->i_modrev++;
    113 		}
    114 		if (ip->i_state & (IN_CHANGE | IN_MODIFY)) {
    115 #ifdef _KERNEL
    116 			if (cre == NULL)
    117 				cre = &now;
    118 #endif
    119 			lfs_dino_setctime(fs, ip->i_din, cre->tv_sec);
    120 			lfs_dino_setctimensec(fs, ip->i_din, cre->tv_nsec);
    121 		}
    122 		mutex_enter(&lfs_lock);
    123 		if (ip->i_state & (IN_CHANGE | IN_UPDATE))
    124 			LFS_SET_UINO(ip, IN_MODIFIED);
    125 		if (ip->i_state & IN_MODIFY)
    126 			LFS_SET_UINO(ip, IN_ACCESSED);
    127 		mutex_exit(&lfs_lock);
    128 	}
    129 	ip->i_state &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
    130 }
    131