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