utimes.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. asmlinkage long sys_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_special(long nsec)
  38. {
  39. return nsec == UTIME_OMIT || nsec == UTIME_NOW;
  40. }
  41. static bool nsec_valid(long nsec)
  42. {
  43. if (nsec_special(nsec))
  44. return true;
  45. return nsec >= 0 && nsec <= 999999999;
  46. }
  47. /* If times==NULL, set access and modification to current time,
  48. * must be owner or have write permission.
  49. * Else, update from *times, must be owner or super user.
  50. */
  51. long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
  52. {
  53. int error;
  54. struct nameidata nd;
  55. struct dentry *dentry;
  56. struct inode *inode;
  57. struct iattr newattrs;
  58. struct file *f = NULL;
  59. struct vfsmount *mnt;
  60. error = -EINVAL;
  61. if (times && (!nsec_valid(times[0].tv_nsec) ||
  62. !nsec_valid(times[1].tv_nsec))) {
  63. goto out;
  64. }
  65. if (flags & ~AT_SYMLINK_NOFOLLOW)
  66. goto out;
  67. if (filename == NULL && dfd != AT_FDCWD) {
  68. error = -EINVAL;
  69. if (flags & AT_SYMLINK_NOFOLLOW)
  70. goto out;
  71. error = -EBADF;
  72. f = fget(dfd);
  73. if (!f)
  74. goto out;
  75. dentry = f->f_path.dentry;
  76. mnt = f->f_path.mnt;
  77. } else {
  78. error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
  79. if (error)
  80. goto out;
  81. dentry = nd.path.dentry;
  82. mnt = nd.path.mnt;
  83. }
  84. inode = dentry->d_inode;
  85. error = mnt_want_write(mnt);
  86. if (error)
  87. goto dput_and_out;
  88. /* Don't worry, the checks are done in inode_change_ok() */
  89. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  90. if (times) {
  91. error = -EPERM;
  92. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  93. goto mnt_drop_write_and_out;
  94. if (times[0].tv_nsec == UTIME_OMIT)
  95. newattrs.ia_valid &= ~ATTR_ATIME;
  96. else if (times[0].tv_nsec != UTIME_NOW) {
  97. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  98. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  99. newattrs.ia_valid |= ATTR_ATIME_SET;
  100. }
  101. if (times[1].tv_nsec == UTIME_OMIT)
  102. newattrs.ia_valid &= ~ATTR_MTIME;
  103. else if (times[1].tv_nsec != UTIME_NOW) {
  104. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  105. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  106. newattrs.ia_valid |= ATTR_MTIME_SET;
  107. }
  108. }
  109. /*
  110. * If times is NULL or both times are either UTIME_OMIT or
  111. * UTIME_NOW, then need to check permissions, because
  112. * inode_change_ok() won't do it.
  113. */
  114. if (!times || (nsec_special(times[0].tv_nsec) &&
  115. nsec_special(times[1].tv_nsec))) {
  116. error = -EACCES;
  117. if (IS_IMMUTABLE(inode))
  118. goto mnt_drop_write_and_out;
  119. if (!is_owner_or_cap(inode)) {
  120. if (f) {
  121. if (!(f->f_mode & FMODE_WRITE))
  122. goto mnt_drop_write_and_out;
  123. } else {
  124. error = vfs_permission(&nd, MAY_WRITE);
  125. if (error)
  126. goto mnt_drop_write_and_out;
  127. }
  128. }
  129. }
  130. mutex_lock(&inode->i_mutex);
  131. error = notify_change(dentry, &newattrs);
  132. mutex_unlock(&inode->i_mutex);
  133. mnt_drop_write_and_out:
  134. mnt_drop_write(mnt);
  135. dput_and_out:
  136. if (f)
  137. fput(f);
  138. else
  139. path_put(&nd.path);
  140. out:
  141. return error;
  142. }
  143. asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
  144. {
  145. struct timespec tstimes[2];
  146. if (utimes) {
  147. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  148. return -EFAULT;
  149. if ((tstimes[0].tv_nsec == UTIME_OMIT ||
  150. tstimes[0].tv_nsec == UTIME_NOW) &&
  151. tstimes[0].tv_sec != 0)
  152. return -EINVAL;
  153. if ((tstimes[1].tv_nsec == UTIME_OMIT ||
  154. tstimes[1].tv_nsec == UTIME_NOW) &&
  155. tstimes[1].tv_sec != 0)
  156. return -EINVAL;
  157. /* Nothing to do, we must not even check the path. */
  158. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  159. tstimes[1].tv_nsec == UTIME_OMIT)
  160. return 0;
  161. }
  162. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  163. }
  164. asmlinkage long sys_futimesat(int dfd, char __user *filename, 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. asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
  187. {
  188. return sys_futimesat(AT_FDCWD, filename, utimes);
  189. }