utimes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. error = mnt_want_write(path->mnt);
  49. if (error)
  50. goto out;
  51. if (times && times[0].tv_nsec == UTIME_NOW &&
  52. times[1].tv_nsec == UTIME_NOW)
  53. times = NULL;
  54. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  55. if (times) {
  56. if (times[0].tv_nsec == UTIME_OMIT)
  57. newattrs.ia_valid &= ~ATTR_ATIME;
  58. else if (times[0].tv_nsec != UTIME_NOW) {
  59. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  60. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  61. newattrs.ia_valid |= ATTR_ATIME_SET;
  62. }
  63. if (times[1].tv_nsec == UTIME_OMIT)
  64. newattrs.ia_valid &= ~ATTR_MTIME;
  65. else if (times[1].tv_nsec != UTIME_NOW) {
  66. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  67. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  68. newattrs.ia_valid |= ATTR_MTIME_SET;
  69. }
  70. /*
  71. * Tell inode_change_ok(), that this is an explicit time
  72. * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
  73. * were used.
  74. */
  75. newattrs.ia_valid |= ATTR_TIMES_SET;
  76. } else {
  77. /*
  78. * If times is NULL (or both times are UTIME_NOW),
  79. * then we need to check permissions, because
  80. * inode_change_ok() won't do it.
  81. */
  82. error = -EACCES;
  83. if (IS_IMMUTABLE(inode))
  84. goto mnt_drop_write_and_out;
  85. if (!inode_owner_or_capable(inode)) {
  86. error = inode_permission2(path->mnt, inode, MAY_WRITE);
  87. if (error)
  88. goto mnt_drop_write_and_out;
  89. }
  90. }
  91. mutex_lock(&inode->i_mutex);
  92. error = notify_change2(path->mnt, path->dentry, &newattrs);
  93. mutex_unlock(&inode->i_mutex);
  94. mnt_drop_write_and_out:
  95. mnt_drop_write(path->mnt);
  96. out:
  97. return error;
  98. }
  99. /*
  100. * do_utimes - change times on filename or file descriptor
  101. * @dfd: open file descriptor, -1 or AT_FDCWD
  102. * @filename: path name or NULL
  103. * @times: new times or NULL
  104. * @flags: zero or more flags (only AT_SYMLINK_NOFOLLOW for the moment)
  105. *
  106. * If filename is NULL and dfd refers to an open file, then operate on
  107. * the file. Otherwise look up filename, possibly using dfd as a
  108. * starting point.
  109. *
  110. * If times==NULL, set access and modification to current time,
  111. * must be owner or have write permission.
  112. * Else, update from *times, must be owner or super user.
  113. */
  114. long do_utimes(int dfd, const char __user *filename, struct timespec *times,
  115. int flags)
  116. {
  117. int error = -EINVAL;
  118. if (times && (!nsec_valid(times[0].tv_nsec) ||
  119. !nsec_valid(times[1].tv_nsec))) {
  120. goto out;
  121. }
  122. if (flags & ~AT_SYMLINK_NOFOLLOW)
  123. goto out;
  124. if (filename == NULL && dfd != AT_FDCWD) {
  125. int fput_needed;
  126. struct file *file;
  127. if (flags & AT_SYMLINK_NOFOLLOW)
  128. goto out;
  129. file = fget_light(dfd, &fput_needed);
  130. error = -EBADF;
  131. if (!file)
  132. goto out;
  133. error = utimes_common(&file->f_path, times);
  134. fput_light(file, fput_needed);
  135. } else {
  136. struct path path;
  137. int lookup_flags = 0;
  138. if (!(flags & AT_SYMLINK_NOFOLLOW))
  139. lookup_flags |= LOOKUP_FOLLOW;
  140. error = user_path_at(dfd, filename, lookup_flags, &path);
  141. if (error)
  142. goto out;
  143. error = utimes_common(&path, times);
  144. path_put(&path);
  145. }
  146. out:
  147. return error;
  148. }
  149. SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
  150. struct timespec __user *, utimes, int, flags)
  151. {
  152. struct timespec tstimes[2];
  153. if (utimes) {
  154. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  155. return -EFAULT;
  156. /* Nothing to do, we must not even check the path. */
  157. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  158. tstimes[1].tv_nsec == UTIME_OMIT)
  159. return 0;
  160. }
  161. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  162. }
  163. SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
  164. struct timeval __user *, utimes)
  165. {
  166. struct timeval times[2];
  167. struct timespec tstimes[2];
  168. if (utimes) {
  169. if (copy_from_user(&times, utimes, sizeof(times)))
  170. return -EFAULT;
  171. /* This test is needed to catch all invalid values. If we
  172. would test only in do_utimes we would miss those invalid
  173. values truncated by the multiplication with 1000. Note
  174. that we also catch UTIME_{NOW,OMIT} here which are only
  175. valid for utimensat. */
  176. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  177. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  178. return -EINVAL;
  179. tstimes[0].tv_sec = times[0].tv_sec;
  180. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  181. tstimes[1].tv_sec = times[1].tv_sec;
  182. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  183. }
  184. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  185. }
  186. SYSCALL_DEFINE2(utimes, char __user *, filename,
  187. struct timeval __user *, utimes)
  188. {
  189. return sys_futimesat(AT_FDCWD, filename, utimes);
  190. }