utimes.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 setattr_prepare(), 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. newattrs.ia_valid |= ATTR_TOUCH;
  79. }
  80. retry_deleg:
  81. inode_lock(inode);
  82. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  83. inode_unlock(inode);
  84. if (delegated_inode) {
  85. error = break_deleg_wait(&delegated_inode);
  86. if (!error)
  87. goto retry_deleg;
  88. }
  89. mnt_drop_write(path->mnt);
  90. out:
  91. return error;
  92. }
  93. /*
  94. * do_utimes - change times on filename or file descriptor
  95. * @dfd: open file descriptor, -1 or AT_FDCWD
  96. * @filename: path name or NULL
  97. * @times: new times or NULL
  98. * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
  99. *
  100. * If filename is NULL and dfd refers to an open file, then operate on
  101. * the file. Otherwise look up filename, possibly using dfd as a
  102. * starting point.
  103. *
  104. * If times==NULL, set access and modification to current time,
  105. * must be owner or have write permission.
  106. * Else, update from *times, must be owner or super user.
  107. */
  108. long do_utimes(int dfd, const char __user *filename, struct timespec *times,
  109. int flags)
  110. {
  111. int error = -EINVAL;
  112. if (times && (!nsec_valid(times[0].tv_nsec) ||
  113. !nsec_valid(times[1].tv_nsec))) {
  114. goto out;
  115. }
  116. if (flags & ~AT_SYMLINK_NOFOLLOW)
  117. goto out;
  118. if (filename == NULL && dfd != AT_FDCWD) {
  119. struct fd f;
  120. if (flags & AT_SYMLINK_NOFOLLOW)
  121. goto out;
  122. f = fdget(dfd);
  123. error = -EBADF;
  124. if (!f.file)
  125. goto out;
  126. error = utimes_common(&f.file->f_path, times);
  127. fdput(f);
  128. } else {
  129. struct path path;
  130. int lookup_flags = 0;
  131. if (!(flags & AT_SYMLINK_NOFOLLOW))
  132. lookup_flags |= LOOKUP_FOLLOW;
  133. retry:
  134. error = user_path_at(dfd, filename, lookup_flags, &path);
  135. if (error)
  136. goto out;
  137. error = utimes_common(&path, times);
  138. path_put(&path);
  139. if (retry_estale(error, lookup_flags)) {
  140. lookup_flags |= LOOKUP_REVAL;
  141. goto retry;
  142. }
  143. }
  144. out:
  145. return error;
  146. }
  147. SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
  148. struct timespec __user *, utimes, int, flags)
  149. {
  150. struct timespec tstimes[2];
  151. if (utimes) {
  152. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  153. return -EFAULT;
  154. /* Nothing to do, we must not even check the path. */
  155. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  156. tstimes[1].tv_nsec == UTIME_OMIT)
  157. return 0;
  158. }
  159. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  160. }
  161. SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
  162. struct timeval __user *, utimes)
  163. {
  164. struct timeval times[2];
  165. struct timespec tstimes[2];
  166. if (utimes) {
  167. if (copy_from_user(&times, utimes, sizeof(times)))
  168. return -EFAULT;
  169. /* This test is needed to catch all invalid values. If we
  170. would test only in do_utimes we would miss those invalid
  171. values truncated by the multiplication with 1000. Note
  172. that we also catch UTIME_{NOW,OMIT} here which are only
  173. valid for utimensat. */
  174. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  175. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  176. return -EINVAL;
  177. tstimes[0].tv_sec = times[0].tv_sec;
  178. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  179. tstimes[1].tv_sec = times[1].tv_sec;
  180. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  181. }
  182. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  183. }
  184. SYSCALL_DEFINE2(utimes, char __user *, filename,
  185. struct timeval __user *, utimes)
  186. {
  187. return sys_futimesat(AT_FDCWD, filename, utimes);
  188. }