xref: /openbmc/linux/fs/utimes.c (revision abf08576)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21c710c89SUlrich Drepper #include <linux/file.h>
374f9fdfaSDave Hansen #include <linux/mount.h>
482b0547cSAlexey Dobriyan #include <linux/namei.h>
582b0547cSAlexey Dobriyan #include <linux/utime.h>
612c2ab5eSAdrian Bunk #include <linux/syscalls.h>
77c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
81a060ba3SAl Viro #include <linux/compat.h>
982b0547cSAlexey Dobriyan #include <asm/unistd.h>
1082b0547cSAlexey Dobriyan #include <linux/filelock.h>
11043f46f6SMiklos Szeredi 
nsec_valid(long nsec)12043f46f6SMiklos Szeredi static bool nsec_valid(long nsec)
134cca9226SMichael Kerrisk {
14043f46f6SMiklos Szeredi 	if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
15043f46f6SMiklos Szeredi 		return true;
16043f46f6SMiklos Szeredi 
17043f46f6SMiklos Szeredi 	return nsec >= 0 && nsec <= 999999999;
18043f46f6SMiklos Szeredi }
19fd5ad30cSChristoph Hellwig 
vfs_utimes(const struct path * path,struct timespec64 * times)2082b0547cSAlexey Dobriyan int vfs_utimes(const struct path *path, struct timespec64 *times)
2182b0547cSAlexey Dobriyan {
2282b0547cSAlexey Dobriyan 	int error;
23e9b76fedSMiklos Szeredi 	struct iattr newattrs;
2427ac0ffeSJ. Bruce Fields 	struct inode *inode = path->dentry->d_inode;
2582b0547cSAlexey Dobriyan 	struct inode *delegated_inode = NULL;
2627eb11c9SChristoph Hellwig 
2727eb11c9SChristoph Hellwig 	if (times) {
2827eb11c9SChristoph Hellwig 		if (!nsec_valid(times[0].tv_nsec) ||
2927eb11c9SChristoph Hellwig 		    !nsec_valid(times[1].tv_nsec))
3027eb11c9SChristoph Hellwig 			return -EINVAL;
3127eb11c9SChristoph Hellwig 		if (times[0].tv_nsec == UTIME_NOW &&
3227eb11c9SChristoph Hellwig 		    times[1].tv_nsec == UTIME_NOW)
3327eb11c9SChristoph Hellwig 			times = NULL;
3427eb11c9SChristoph Hellwig 	}
35e9b76fedSMiklos Szeredi 
3682b0547cSAlexey Dobriyan 	error = mnt_want_write(path->mnt);
3782b0547cSAlexey Dobriyan 	if (error)
381c710c89SUlrich Drepper 		goto out;
3982b0547cSAlexey Dobriyan 
4082b0547cSAlexey Dobriyan 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
411c710c89SUlrich Drepper 	if (times) {
421c710c89SUlrich Drepper 		if (times[0].tv_nsec == UTIME_OMIT)
431c710c89SUlrich Drepper 			newattrs.ia_valid &= ~ATTR_ATIME;
44eb31e2f6SAmir Goldstein 		else if (times[0].tv_nsec != UTIME_NOW) {
451c710c89SUlrich Drepper 			newattrs.ia_atime = times[0];
461c710c89SUlrich Drepper 			newattrs.ia_valid |= ATTR_ATIME_SET;
471c710c89SUlrich Drepper 		}
481c710c89SUlrich Drepper 
491c710c89SUlrich Drepper 		if (times[1].tv_nsec == UTIME_OMIT)
501c710c89SUlrich Drepper 			newattrs.ia_valid &= ~ATTR_MTIME;
51eb31e2f6SAmir Goldstein 		else if (times[1].tv_nsec != UTIME_NOW) {
521c710c89SUlrich Drepper 			newattrs.ia_mtime = times[1];
531c710c89SUlrich Drepper 			newattrs.ia_valid |= ATTR_MTIME_SET;
5402c6be61SMiklos Szeredi 		}
5531051c85SJan Kara 		/*
569767d749SMiklos Szeredi 		 * Tell setattr_prepare(), that this is an explicit time
579767d749SMiklos Szeredi 		 * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
584cca9226SMichael Kerrisk 		 * were used.
599767d749SMiklos Szeredi 		 */
604cca9226SMichael Kerrisk 		newattrs.ia_valid |= ATTR_TIMES_SET;
61f2b20f6eSMiklos Szeredi 	} else {
621e5de283SLinus Torvalds 		newattrs.ia_valid |= ATTR_TOUCH;
6327ac0ffeSJ. Bruce Fields 	}
645955102cSAl Viro retry_deleg:
65*abf08576SChristian Brauner 	inode_lock(inode);
662f221d6fSChristian Brauner 	error = notify_change(mnt_idmap(path->mnt), path->dentry, &newattrs,
675955102cSAl Viro 			      &delegated_inode);
6827ac0ffeSJ. Bruce Fields 	inode_unlock(inode);
6927ac0ffeSJ. Bruce Fields 	if (delegated_inode) {
7027ac0ffeSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
7127ac0ffeSJ. Bruce Fields 		if (!error)
7227ac0ffeSJ. Bruce Fields 			goto retry_deleg;
73e9b76fedSMiklos Szeredi 	}
74e9b76fedSMiklos Szeredi 
75e9b76fedSMiklos Szeredi 	mnt_drop_write(path->mnt);
76e9b76fedSMiklos Szeredi out:
77e9b76fedSMiklos Szeredi 	return error;
78e9b76fedSMiklos Szeredi }
799d4b74aeSChristoph Hellwig 
do_utimes_path(int dfd,const char __user * filename,struct timespec64 * times,int flags)809d4b74aeSChristoph Hellwig static int do_utimes_path(int dfd, const char __user *filename,
819d4b74aeSChristoph Hellwig 		struct timespec64 *times, int flags)
829d4b74aeSChristoph Hellwig {
839d4b74aeSChristoph Hellwig 	struct path path;
849d4b74aeSChristoph Hellwig 	int lookup_flags = 0, error;
859d4b74aeSChristoph Hellwig 
869d4b74aeSChristoph Hellwig 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH))
879d4b74aeSChristoph Hellwig 		return -EINVAL;
889d4b74aeSChristoph Hellwig 
899d4b74aeSChristoph Hellwig 	if (!(flags & AT_SYMLINK_NOFOLLOW))
909d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_FOLLOW;
919d4b74aeSChristoph Hellwig 	if (flags & AT_EMPTY_PATH)
929d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_EMPTY;
939d4b74aeSChristoph Hellwig 
949d4b74aeSChristoph Hellwig retry:
959d4b74aeSChristoph Hellwig 	error = user_path_at(dfd, filename, lookup_flags, &path);
969d4b74aeSChristoph Hellwig 	if (error)
979d4b74aeSChristoph Hellwig 		return error;
98fd5ad30cSChristoph Hellwig 
999d4b74aeSChristoph Hellwig 	error = vfs_utimes(&path, times);
1009d4b74aeSChristoph Hellwig 	path_put(&path);
1019d4b74aeSChristoph Hellwig 	if (retry_estale(error, lookup_flags)) {
1029d4b74aeSChristoph Hellwig 		lookup_flags |= LOOKUP_REVAL;
1039d4b74aeSChristoph Hellwig 		goto retry;
1049d4b74aeSChristoph Hellwig 	}
1059d4b74aeSChristoph Hellwig 
1069d4b74aeSChristoph Hellwig 	return error;
1079d4b74aeSChristoph Hellwig }
1089d4b74aeSChristoph Hellwig 
do_utimes_fd(int fd,struct timespec64 * times,int flags)1099d4b74aeSChristoph Hellwig static int do_utimes_fd(int fd, struct timespec64 *times, int flags)
1109d4b74aeSChristoph Hellwig {
1119d4b74aeSChristoph Hellwig 	struct fd f;
1129d4b74aeSChristoph Hellwig 	int error;
1139d4b74aeSChristoph Hellwig 
1149d4b74aeSChristoph Hellwig 	if (flags)
1159d4b74aeSChristoph Hellwig 		return -EINVAL;
1169d4b74aeSChristoph Hellwig 
1179d4b74aeSChristoph Hellwig 	f = fdget(fd);
1189d4b74aeSChristoph Hellwig 	if (!f.file)
119fd5ad30cSChristoph Hellwig 		return -EBADF;
1209d4b74aeSChristoph Hellwig 	error = vfs_utimes(&f.file->f_path, times);
1219d4b74aeSChristoph Hellwig 	fdput(f);
1229d4b74aeSChristoph Hellwig 	return error;
1239d4b74aeSChristoph Hellwig }
124e9b76fedSMiklos Szeredi 
125e9b76fedSMiklos Szeredi /*
126e9b76fedSMiklos Szeredi  * do_utimes - change times on filename or file descriptor
127e9b76fedSMiklos Szeredi  * @dfd: open file descriptor, -1 or AT_FDCWD
128e9b76fedSMiklos Szeredi  * @filename: path name or NULL
129e9b76fedSMiklos Szeredi  * @times: new times or NULL
130e9b76fedSMiklos Szeredi  * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
131e9b76fedSMiklos Szeredi  *
132e9b76fedSMiklos Szeredi  * If filename is NULL and dfd refers to an open file, then operate on
133e9b76fedSMiklos Szeredi  * the file.  Otherwise look up filename, possibly using dfd as a
134e9b76fedSMiklos Szeredi  * starting point.
135e9b76fedSMiklos Szeredi  *
136e9b76fedSMiklos Szeredi  * If times==NULL, set access and modification to current time,
137e9b76fedSMiklos Szeredi  * must be owner or have write permission.
138e9b76fedSMiklos Szeredi  * Else, update from *times, must be owner or super user.
139aaed2dd8SDeepa Dinamani  */
do_utimes(int dfd,const char __user * filename,struct timespec64 * times,int flags)140c7887325SDavid Howells long do_utimes(int dfd, const char __user *filename, struct timespec64 *times,
141e9b76fedSMiklos Szeredi 	       int flags)
1429d4b74aeSChristoph Hellwig {
1439d4b74aeSChristoph Hellwig 	if (filename == NULL && dfd != AT_FDCWD)
1449d4b74aeSChristoph Hellwig 		return do_utimes_fd(dfd, times, flags);
14582b0547cSAlexey Dobriyan 	return do_utimes_path(dfd, filename, times, flags);
14682b0547cSAlexey Dobriyan }
147c7887325SDavid Howells 
SYSCALL_DEFINE4(utimensat,int,dfd,const char __user *,filename,struct __kernel_timespec __user *,utimes,int,flags)148a4f7a300SArnd Bergmann SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
1491c710c89SUlrich Drepper 		struct __kernel_timespec __user *, utimes, int, flags)
150aaed2dd8SDeepa Dinamani {
1511c710c89SUlrich Drepper 	struct timespec64 tstimes[2];
1521c710c89SUlrich Drepper 
153aaed2dd8SDeepa Dinamani 	if (utimes) {
154aaed2dd8SDeepa Dinamani 		if ((get_timespec64(&tstimes[0], &utimes[0]) ||
1551c710c89SUlrich Drepper 			get_timespec64(&tstimes[1], &utimes[1])))
1561c710c89SUlrich Drepper 			return -EFAULT;
1571c710c89SUlrich Drepper 
1581c710c89SUlrich Drepper 		/* Nothing to do, we must not even check the path.  */
1591c710c89SUlrich Drepper 		if (tstimes[0].tv_nsec == UTIME_OMIT &&
1601c710c89SUlrich Drepper 		    tstimes[1].tv_nsec == UTIME_OMIT)
1611c710c89SUlrich Drepper 			return 0;
1621c710c89SUlrich Drepper 	}
1631c710c89SUlrich Drepper 
1641c710c89SUlrich Drepper 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
1651c710c89SUlrich Drepper }
166185cfaf7SArnd Bergmann 
167185cfaf7SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME
168185cfaf7SArnd Bergmann /*
169185cfaf7SArnd Bergmann  * futimesat(), utimes() and utime() are older versions of utimensat()
170185cfaf7SArnd Bergmann  * that are provided for compatibility with traditional C libraries.
171185cfaf7SArnd Bergmann  * On modern architectures, we always use libc wrappers around
172185cfaf7SArnd Bergmann  * utimensat() instead.
173f1390358SDominik Brodowski  */
do_futimesat(int dfd,const char __user * filename,struct __kernel_old_timeval __user * utimes)17475d319c0SArnd Bergmann static long do_futimesat(int dfd, const char __user *filename,
17582b0547cSAlexey Dobriyan 			 struct __kernel_old_timeval __user *utimes)
17675d319c0SArnd Bergmann {
177aaed2dd8SDeepa Dinamani 	struct __kernel_old_timeval times[2];
17882b0547cSAlexey Dobriyan 	struct timespec64 tstimes[2];
1791c710c89SUlrich Drepper 
1801c710c89SUlrich Drepper 	if (utimes) {
18182b0547cSAlexey Dobriyan 		if (copy_from_user(&times, utimes, sizeof(times)))
1821c710c89SUlrich Drepper 			return -EFAULT;
1831c710c89SUlrich Drepper 
1841c710c89SUlrich Drepper 		/* This test is needed to catch all invalid values.  If we
1851c710c89SUlrich Drepper 		   would test only in do_utimes we would miss those invalid
1861c710c89SUlrich Drepper 		   values truncated by the multiplication with 1000.  Note
1871c710c89SUlrich Drepper 		   that we also catch UTIME_{NOW,OMIT} here which are only
1881c710c89SUlrich Drepper 		   valid for utimensat.  */
1891c710c89SUlrich Drepper 		if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
1901c710c89SUlrich Drepper 		    times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
1911c710c89SUlrich Drepper 			return -EINVAL;
1921c710c89SUlrich Drepper 
1931c710c89SUlrich Drepper 		tstimes[0].tv_sec = times[0].tv_sec;
1941c710c89SUlrich Drepper 		tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
1951c710c89SUlrich Drepper 		tstimes[1].tv_sec = times[1].tv_sec;
1961c710c89SUlrich Drepper 		tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
1971c710c89SUlrich Drepper 	}
1981c710c89SUlrich Drepper 
19982b0547cSAlexey Dobriyan 	return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
20082b0547cSAlexey Dobriyan }
201f1390358SDominik Brodowski 
202f1390358SDominik Brodowski 
SYSCALL_DEFINE3(futimesat,int,dfd,const char __user *,filename,struct __kernel_old_timeval __user *,utimes)20375d319c0SArnd Bergmann SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
204f1390358SDominik Brodowski 		struct __kernel_old_timeval __user *, utimes)
205f1390358SDominik Brodowski {
206f1390358SDominik Brodowski 	return do_futimesat(dfd, filename, utimes);
207f1390358SDominik Brodowski }
208003d7ab4SHeiko Carstens 
SYSCALL_DEFINE2(utimes,char __user *,filename,struct __kernel_old_timeval __user *,utimes)20975d319c0SArnd Bergmann SYSCALL_DEFINE2(utimes, char __user *, filename,
21082b0547cSAlexey Dobriyan 		struct __kernel_old_timeval __user *, utimes)
211f1390358SDominik Brodowski {
21282b0547cSAlexey Dobriyan 	return do_futimesat(AT_FDCWD, filename, utimes);
2131a060ba3SAl Viro }
214185cfaf7SArnd Bergmann 
SYSCALL_DEFINE2(utime,char __user *,filename,struct utimbuf __user *,times)215185cfaf7SArnd Bergmann SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
216185cfaf7SArnd Bergmann {
217185cfaf7SArnd Bergmann 	struct timespec64 tv[2];
218185cfaf7SArnd Bergmann 
219185cfaf7SArnd Bergmann 	if (times) {
220185cfaf7SArnd Bergmann 		if (get_user(tv[0].tv_sec, &times->actime) ||
221185cfaf7SArnd Bergmann 		    get_user(tv[1].tv_sec, &times->modtime))
222185cfaf7SArnd Bergmann 			return -EFAULT;
223185cfaf7SArnd Bergmann 		tv[0].tv_nsec = 0;
224185cfaf7SArnd Bergmann 		tv[1].tv_nsec = 0;
225185cfaf7SArnd Bergmann 	}
226185cfaf7SArnd Bergmann 	return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
227185cfaf7SArnd Bergmann }
228185cfaf7SArnd Bergmann #endif
2294faea239SArnd Bergmann 
2301a060ba3SAl Viro #ifdef CONFIG_COMPAT_32BIT_TIME
2311a060ba3SAl Viro /*
2321a060ba3SAl Viro  * Not all architectures have sys_utime, so implement this in terms
2331a060ba3SAl Viro  * of sys_utimes.
2344faea239SArnd Bergmann  */
2358dabe724SArnd Bergmann #ifdef __ARCH_WANT_SYS_UTIME32
SYSCALL_DEFINE2(utime32,const char __user *,filename,struct old_utimbuf32 __user *,t)2364faea239SArnd Bergmann SYSCALL_DEFINE2(utime32, const char __user *, filename,
2371a060ba3SAl Viro 		struct old_utimbuf32 __user *, t)
238aaed2dd8SDeepa Dinamani {
2391a060ba3SAl Viro 	struct timespec64 tv[2];
2401a060ba3SAl Viro 
2411a060ba3SAl Viro 	if (t) {
2421a060ba3SAl Viro 		if (get_user(tv[0].tv_sec, &t->actime) ||
2431a060ba3SAl Viro 		    get_user(tv[1].tv_sec, &t->modtime))
2441a060ba3SAl Viro 			return -EFAULT;
2451a060ba3SAl Viro 		tv[0].tv_nsec = 0;
2461a060ba3SAl Viro 		tv[1].tv_nsec = 0;
2471a060ba3SAl Viro 	}
2481a060ba3SAl Viro 	return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
2494faea239SArnd Bergmann }
2501a060ba3SAl Viro #endif
2518dabe724SArnd Bergmann 
SYSCALL_DEFINE4(utimensat_time32,unsigned int,dfd,const char __user *,filename,struct old_timespec32 __user *,t,int,flags)2521a060ba3SAl Viro SYSCALL_DEFINE4(utimensat_time32, unsigned int, dfd, const char __user *, filename, struct old_timespec32 __user *, t, int, flags)
253aaed2dd8SDeepa Dinamani {
2541a060ba3SAl Viro 	struct timespec64 tv[2];
2551a060ba3SAl Viro 
2569afc5eeeSArnd Bergmann 	if  (t) {
2579afc5eeeSArnd Bergmann 		if (get_old_timespec32(&tv[0], &t[0]) ||
2581a060ba3SAl Viro 		    get_old_timespec32(&tv[1], &t[1]))
2591a060ba3SAl Viro 			return -EFAULT;
2601a060ba3SAl Viro 
2611a060ba3SAl Viro 		if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
2621a060ba3SAl Viro 			return 0;
2631a060ba3SAl Viro 	}
2641a060ba3SAl Viro 	return do_utimes(dfd, filename, t ? tv : NULL, flags);
2651a060ba3SAl Viro }
2664faea239SArnd Bergmann 
267ab641afaSDominik Brodowski #ifdef __ARCH_WANT_SYS_UTIME32
do_compat_futimesat(unsigned int dfd,const char __user * filename,struct old_timeval32 __user * t)2689afc5eeeSArnd Bergmann static long do_compat_futimesat(unsigned int dfd, const char __user *filename,
2691a060ba3SAl Viro 				struct old_timeval32 __user *t)
270aaed2dd8SDeepa Dinamani {
2711a060ba3SAl Viro 	struct timespec64 tv[2];
2721a060ba3SAl Viro 
2731a060ba3SAl Viro 	if (t) {
2741a060ba3SAl Viro 		if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
2751a060ba3SAl Viro 		    get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
2761a060ba3SAl Viro 		    get_user(tv[1].tv_sec, &t[1].tv_sec) ||
2771a060ba3SAl Viro 		    get_user(tv[1].tv_nsec, &t[1].tv_usec))
2781a060ba3SAl Viro 			return -EFAULT;
2791a060ba3SAl Viro 		if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
2801a060ba3SAl Viro 		    tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
2811a060ba3SAl Viro 			return -EINVAL;
2821a060ba3SAl Viro 		tv[0].tv_nsec *= 1000;
2831a060ba3SAl Viro 		tv[1].tv_nsec *= 1000;
2841a060ba3SAl Viro 	}
2851a060ba3SAl Viro 	return do_utimes(dfd, filename, t ? tv : NULL, 0);
2861a060ba3SAl Viro }
2878dabe724SArnd Bergmann 
SYSCALL_DEFINE3(futimesat_time32,unsigned int,dfd,const char __user *,filename,struct old_timeval32 __user *,t)288ab641afaSDominik Brodowski SYSCALL_DEFINE3(futimesat_time32, unsigned int, dfd,
2899afc5eeeSArnd Bergmann 		       const char __user *, filename,
290ab641afaSDominik Brodowski 		       struct old_timeval32 __user *, t)
291ab641afaSDominik Brodowski {
292ab641afaSDominik Brodowski 	return do_compat_futimesat(dfd, filename, t);
293ab641afaSDominik Brodowski }
2948dabe724SArnd Bergmann 
SYSCALL_DEFINE2(utimes_time32,const char __user *,filename,struct old_timeval32 __user *,t)2951a060ba3SAl Viro SYSCALL_DEFINE2(utimes_time32, const char __user *, filename, struct old_timeval32 __user *, t)
296ab641afaSDominik Brodowski {
2971a060ba3SAl Viro 	return do_compat_futimesat(AT_FDCWD, filename, t);
2981a060ba3SAl Viro }
2994faea239SArnd Bergmann #endif
300 #endif
301