utimes.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include <linux/compiler.h>
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/linkage.h>
  5. #include <linux/mount.h>
  6. #include <linux/namei.h>
  7. #include <linux/sched.h>
  8. #include <linux/stat.h>
  9. #include <linux/utime.h>
  10. #include <linux/syscalls.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/unistd.h>
  13. #ifdef __ARCH_WANT_SYS_UTIME
  14. /*
  15. * sys_utime() can be implemented in user-level using sys_utimes().
  16. * Is this for backwards compatibility? If so, why not move it
  17. * into the appropriate arch directory (for those architectures that
  18. * need it).
  19. */
  20. /* If times==NULL, set access and modification to current time,
  21. * must be owner or have write permission.
  22. * Else, update from *times, must be owner or super user.
  23. */
  24. SYSCALL_DEFINE2(utime, char __user *, filename, struct utimbuf __user *, times)
  25. {
  26. struct timespec tv[2];
  27. if (times) {
  28. if (get_user(tv[0].tv_sec, &times->actime) ||
  29. get_user(tv[1].tv_sec, &times->modtime))
  30. return -EFAULT;
  31. tv[0].tv_nsec = 0;
  32. tv[1].tv_nsec = 0;
  33. }
  34. return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
  35. }
  36. #endif
  37. static bool nsec_valid(long nsec)
  38. {
  39. if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
  40. return true;
  41. return nsec >= 0 && nsec <= 999999999;
  42. }
  43. static int utimes_common(struct path *path, struct timespec *times)
  44. {
  45. int error;
  46. struct iattr newattrs;
  47. struct inode *inode = path->dentry->d_inode;
  48. struct inode *delegated_inode = NULL;
  49. error = mnt_want_write(path->mnt);
  50. if (error)
  51. goto out;
  52. if (times && times[0].tv_nsec == UTIME_NOW &&
  53. times[1].tv_nsec == UTIME_NOW)
  54. times = NULL;
  55. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  56. if (times) {
  57. if (times[0].tv_nsec == UTIME_OMIT)
  58. newattrs.ia_valid &= ~ATTR_ATIME;
  59. else if (times[0].tv_nsec != UTIME_NOW) {
  60. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  61. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  62. newattrs.ia_valid |= ATTR_ATIME_SET;
  63. }
  64. if (times[1].tv_nsec == UTIME_OMIT)
  65. newattrs.ia_valid &= ~ATTR_MTIME;
  66. else if (times[1].tv_nsec != UTIME_NOW) {
  67. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  68. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  69. newattrs.ia_valid |= ATTR_MTIME_SET;
  70. }
  71. /*
  72. * Tell inode_change_ok(), that this is an explicit time
  73. * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
  74. * were used.
  75. */
  76. newattrs.ia_valid |= ATTR_TIMES_SET;
  77. } else {
  78. /*
  79. * If times is NULL (or both times are UTIME_NOW),
  80. * then we need to check permissions, because
  81. * inode_change_ok() won't do it.
  82. */
  83. error = -EACCES;
  84. if (IS_IMMUTABLE(inode))
  85. goto mnt_drop_write_and_out;
  86. if (!inode_owner_or_capable(inode)) {
  87. error = inode_permission(inode, MAY_WRITE);
  88. if (error)
  89. goto mnt_drop_write_and_out;
  90. }
  91. }
  92. retry_deleg:
  93. mutex_lock(&inode->i_mutex);
  94. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  95. mutex_unlock(&inode->i_mutex);
  96. if (delegated_inode) {
  97. error = break_deleg_wait(&delegated_inode);
  98. if (!error)
  99. goto retry_deleg;
  100. }
  101. mnt_drop_write_and_out:
  102. mnt_drop_write(path->mnt);
  103. out:
  104. return error;
  105. }
  106. /*
  107. * do_utimes - change times on filename or file descriptor
  108. * @dfd: open file descriptor, -1 or AT_FDCWD
  109. * @filename: path name or NULL
  110. * @times: new times or NULL
  111. * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
  112. *
  113. * If filename is NULL and dfd refers to an open file, then operate on
  114. * the file. Otherwise look up filename, possibly using dfd as a
  115. * starting point.
  116. *
  117. * If times==NULL, set access and modification to current time,
  118. * must be owner or have write permission.
  119. * Else, update from *times, must be owner or super user.
  120. */
  121. long do_utimes(int dfd, const char __user *filename, struct timespec *times,
  122. int flags)
  123. {
  124. int error = -EINVAL;
  125. if (times && (!nsec_valid(times[0].tv_nsec) ||
  126. !nsec_valid(times[1].tv_nsec))) {
  127. goto out;
  128. }
  129. if (flags & ~AT_SYMLINK_NOFOLLOW)
  130. goto out;
  131. if (filename == NULL && dfd != AT_FDCWD) {
  132. struct fd f;
  133. if (flags & AT_SYMLINK_NOFOLLOW)
  134. goto out;
  135. f = fdget(dfd);
  136. error = -EBADF;
  137. if (!f.file)
  138. goto out;
  139. error = utimes_common(&f.file->f_path, times);
  140. fdput(f);
  141. } else {
  142. struct path path;
  143. int lookup_flags = 0;
  144. if (!(flags & AT_SYMLINK_NOFOLLOW))
  145. lookup_flags |= LOOKUP_FOLLOW;
  146. retry:
  147. error = user_path_at(dfd, filename, lookup_flags, &path);
  148. if (error)
  149. goto out;
  150. error = utimes_common(&path, times);
  151. path_put(&path);
  152. if (retry_estale(error, lookup_flags)) {
  153. lookup_flags |= LOOKUP_REVAL;
  154. goto retry;
  155. }
  156. }
  157. out:
  158. return error;
  159. }
  160. SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
  161. struct timespec __user *, utimes, int, flags)
  162. {
  163. struct timespec tstimes[2];
  164. if (utimes) {
  165. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  166. return -EFAULT;
  167. /* Nothing to do, we must not even check the path. */
  168. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  169. tstimes[1].tv_nsec == UTIME_OMIT)
  170. return 0;
  171. }
  172. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  173. }
  174. SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
  175. struct timeval __user *, utimes)
  176. {
  177. struct timeval times[2];
  178. struct timespec tstimes[2];
  179. if (utimes) {
  180. if (copy_from_user(&times, utimes, sizeof(times)))
  181. return -EFAULT;
  182. /* This test is needed to catch all invalid values. If we
  183. would test only in do_utimes we would miss those invalid
  184. values truncated by the multiplication with 1000. Note
  185. that we also catch UTIME_{NOW,OMIT} here which are only
  186. valid for utimensat. */
  187. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  188. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  189. return -EINVAL;
  190. tstimes[0].tv_sec = times[0].tv_sec;
  191. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  192. tstimes[1].tv_sec = times[1].tv_sec;
  193. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  194. }
  195. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  196. }
  197. SYSCALL_DEFINE2(utimes, char __user *, filename,
  198. struct timeval __user *, utimes)
  199. {
  200. return sys_futimesat(AT_FDCWD, filename, utimes);
  201. }