xref: /netbsd/sys/ufs/lfs/lfs_itimes.c (revision 6550d01e)
1 /*	$NetBSD: lfs_itimes.c,v 1.12 2008/04/28 20:24:11 martin 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@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.12 2008/04/28 20:24:11 martin Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/mount.h>
37 #include <sys/buf.h>
38 
39 #include <ufs/ufs/inode.h>
40 
41 #ifndef _KERNEL
42 #include "bufcache.h"
43 #include "vnode.h"
44 #include "lfs_user.h"
45 #define vnode uvnode
46 #define buf ubuf
47 #define panic call_panic
48 #else
49 #include <ufs/lfs/lfs_extern.h>
50 #include <sys/kauth.h>
51 #endif
52 
53 #include <ufs/lfs/lfs.h>
54 
55 void
56 lfs_itimes(struct inode *ip, const struct timespec *acc,
57     const struct timespec *mod, const struct timespec *cre)
58 {
59 #ifdef _KERNEL
60 	struct timespec now;
61 
62 	KASSERT(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY));
63 
64 	vfs_timestamp(&now);
65 #endif
66 
67 	if (ip->i_flag & IN_ACCESS) {
68 #ifdef _KERNEL
69 		if (acc == NULL)
70 			acc = &now;
71 #endif
72 		ip->i_ffs1_atime = acc->tv_sec;
73 		ip->i_ffs1_atimensec = acc->tv_nsec;
74 		if (ip->i_lfs->lfs_version > 1) {
75 			struct lfs *fs = ip->i_lfs;
76 			struct buf *ibp;
77 			IFILE *ifp;
78 
79 			LFS_IENTRY(ifp, ip->i_lfs, ip->i_number, ibp);
80 			ifp->if_atime_sec = acc->tv_sec;
81 			ifp->if_atime_nsec = acc->tv_nsec;
82 			LFS_BWRITE_LOG(ibp);
83 			mutex_enter(&lfs_lock);
84 			fs->lfs_flags |= LFS_IFDIRTY;
85 			mutex_exit(&lfs_lock);
86 		} else {
87 			mutex_enter(&lfs_lock);
88 			LFS_SET_UINO(ip, IN_ACCESSED);
89 			mutex_exit(&lfs_lock);
90 		}
91 	}
92 	if (ip->i_flag & (IN_CHANGE | IN_UPDATE | IN_MODIFY)) {
93 		if (ip->i_flag & (IN_UPDATE | IN_MODIFY)) {
94 #ifdef _KERNEL
95 			if (mod == NULL)
96 				mod = &now;
97 #endif
98 			ip->i_ffs1_mtime = mod->tv_sec;
99 			ip->i_ffs1_mtimensec = mod->tv_nsec;
100 			ip->i_modrev++;
101 		}
102 		if (ip->i_flag & (IN_CHANGE | IN_MODIFY)) {
103 #ifdef _KERNEL
104 			if (cre == NULL)
105 				cre = &now;
106 #endif
107 			ip->i_ffs1_ctime = cre->tv_sec;
108 			ip->i_ffs1_ctimensec = cre->tv_nsec;
109 		}
110 		mutex_enter(&lfs_lock);
111 		if (ip->i_flag & (IN_CHANGE | IN_UPDATE))
112 			LFS_SET_UINO(ip, IN_MODIFIED);
113 		if (ip->i_flag & IN_MODIFY)
114 			LFS_SET_UINO(ip, IN_ACCESSED);
115 		mutex_exit(&lfs_lock);
116 	}
117 	ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
118 }
119