utimes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. struct file *file;
  126. if (flags & AT_SYMLINK_NOFOLLOW)
  127. goto out;
  128. file = fget(dfd);
  129. error = -EBADF;
  130. if (!file)
  131. goto out;
  132. error = utimes_common(&file->f_path, times);
  133. fput(file);
  134. } else {
  135. struct path path;
  136. int lookup_flags = 0;
  137. if (!(flags & AT_SYMLINK_NOFOLLOW))
  138. lookup_flags |= LOOKUP_FOLLOW;
  139. error = user_path_at(dfd, filename, lookup_flags, &path);
  140. if (error)
  141. goto out;
  142. error = utimes_common(&path, times);
  143. path_put(&path);
  144. }
  145. out:
  146. return error;
  147. }
  148. SYSCALL_DEFINE4(utimensat, int, dfd, const char __user *, filename,
  149. struct timespec __user *, utimes, int, flags)
  150. {
  151. struct timespec tstimes[2];
  152. if (utimes) {
  153. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  154. return -EFAULT;
  155. /* Nothing to do, we must not even check the path. */
  156. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  157. tstimes[1].tv_nsec == UTIME_OMIT)
  158. return 0;
  159. }
  160. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  161. }
  162. SYSCALL_DEFINE3(futimesat, int, dfd, const char __user *, filename,
  163. struct timeval __user *, utimes)
  164. {
  165. struct timeval times[2];
  166. struct timespec tstimes[2];
  167. if (utimes) {
  168. if (copy_from_user(&times, utimes, sizeof(times)))
  169. return -EFAULT;
  170. /* This test is needed to catch all invalid values. If we
  171. would test only in do_utimes we would miss those invalid
  172. values truncated by the multiplication with 1000. Note
  173. that we also catch UTIME_{NOW,OMIT} here which are only
  174. valid for utimensat. */
  175. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  176. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  177. return -EINVAL;
  178. tstimes[0].tv_sec = times[0].tv_sec;
  179. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  180. tstimes[1].tv_sec = times[1].tv_sec;
  181. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  182. }
  183. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  184. }
  185. SYSCALL_DEFINE2(utimes, char __user *, filename,
  186. struct timeval __user *, utimes)
  187. {
  188. return sys_futimesat(AT_FDCWD, filename, utimes);
  189. }