utimes.c 5.3 KB

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