utimes.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <linux/compiler.h>
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/linkage.h>
  5. #include <linux/namei.h>
  6. #include <linux/sched.h>
  7. #include <linux/stat.h>
  8. #include <linux/utime.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/unistd.h>
  11. #ifdef __ARCH_WANT_SYS_UTIME
  12. /*
  13. * sys_utime() can be implemented in user-level using sys_utimes().
  14. * Is this for backwards compatibility? If so, why not move it
  15. * into the appropriate arch directory (for those architectures that
  16. * need it).
  17. */
  18. /* If times==NULL, set access and modification to current time,
  19. * must be owner or have write permission.
  20. * Else, update from *times, must be owner or super user.
  21. */
  22. asmlinkage long sys_utime(char __user *filename, struct utimbuf __user *times)
  23. {
  24. struct timespec tv[2];
  25. if (times) {
  26. if (get_user(tv[0].tv_sec, &times->actime) ||
  27. get_user(tv[1].tv_sec, &times->modtime))
  28. return -EFAULT;
  29. tv[0].tv_nsec = 0;
  30. tv[1].tv_nsec = 0;
  31. }
  32. return do_utimes(AT_FDCWD, filename, times ? tv : NULL, 0);
  33. }
  34. #endif
  35. static bool nsec_valid(long nsec)
  36. {
  37. if (nsec == UTIME_OMIT || nsec == UTIME_NOW)
  38. return true;
  39. return nsec >= 0 && nsec <= 999999999;
  40. }
  41. /* If times==NULL, set access and modification to current time,
  42. * must be owner or have write permission.
  43. * Else, update from *times, must be owner or super user.
  44. */
  45. long do_utimes(int dfd, char __user *filename, struct timespec *times, int flags)
  46. {
  47. int error;
  48. struct nameidata nd;
  49. struct dentry *dentry;
  50. struct inode *inode;
  51. struct iattr newattrs;
  52. struct file *f = NULL;
  53. error = -EINVAL;
  54. if (times && (!nsec_valid(times[0].tv_nsec) ||
  55. !nsec_valid(times[1].tv_nsec))) {
  56. goto out;
  57. }
  58. if (flags & ~AT_SYMLINK_NOFOLLOW)
  59. goto out;
  60. if (filename == NULL && dfd != AT_FDCWD) {
  61. error = -EINVAL;
  62. if (flags & AT_SYMLINK_NOFOLLOW)
  63. goto out;
  64. error = -EBADF;
  65. f = fget(dfd);
  66. if (!f)
  67. goto out;
  68. dentry = f->f_path.dentry;
  69. } else {
  70. error = __user_walk_fd(dfd, filename, (flags & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW, &nd);
  71. if (error)
  72. goto out;
  73. dentry = nd.dentry;
  74. }
  75. inode = dentry->d_inode;
  76. error = -EROFS;
  77. if (IS_RDONLY(inode))
  78. goto dput_and_out;
  79. /* Don't worry, the checks are done in inode_change_ok() */
  80. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  81. if (times) {
  82. error = -EPERM;
  83. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  84. goto dput_and_out;
  85. if (times[0].tv_nsec == UTIME_OMIT)
  86. newattrs.ia_valid &= ~ATTR_ATIME;
  87. else if (times[0].tv_nsec != UTIME_NOW) {
  88. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  89. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  90. newattrs.ia_valid |= ATTR_ATIME_SET;
  91. }
  92. if (times[1].tv_nsec == UTIME_OMIT)
  93. newattrs.ia_valid &= ~ATTR_MTIME;
  94. else if (times[1].tv_nsec != UTIME_NOW) {
  95. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  96. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  97. newattrs.ia_valid |= ATTR_MTIME_SET;
  98. }
  99. } else {
  100. error = -EACCES;
  101. if (IS_IMMUTABLE(inode))
  102. goto dput_and_out;
  103. if (!is_owner_or_cap(inode)) {
  104. if (f) {
  105. if (!(f->f_mode & FMODE_WRITE))
  106. goto dput_and_out;
  107. } else {
  108. error = vfs_permission(&nd, MAY_WRITE);
  109. if (error)
  110. goto dput_and_out;
  111. }
  112. }
  113. }
  114. mutex_lock(&inode->i_mutex);
  115. error = notify_change(dentry, &newattrs);
  116. mutex_unlock(&inode->i_mutex);
  117. dput_and_out:
  118. if (f)
  119. fput(f);
  120. else
  121. path_release(&nd);
  122. out:
  123. return error;
  124. }
  125. asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
  126. {
  127. struct timespec tstimes[2];
  128. if (utimes) {
  129. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  130. return -EFAULT;
  131. if ((tstimes[0].tv_nsec == UTIME_OMIT ||
  132. tstimes[0].tv_nsec == UTIME_NOW) &&
  133. tstimes[0].tv_sec != 0)
  134. return -EINVAL;
  135. if ((tstimes[1].tv_nsec == UTIME_OMIT ||
  136. tstimes[1].tv_nsec == UTIME_NOW) &&
  137. tstimes[1].tv_sec != 0)
  138. return -EINVAL;
  139. /* Nothing to do, we must not even check the path. */
  140. if (tstimes[0].tv_nsec == UTIME_OMIT &&
  141. tstimes[1].tv_nsec == UTIME_OMIT)
  142. return 0;
  143. }
  144. return do_utimes(dfd, filename, utimes ? tstimes : NULL, flags);
  145. }
  146. asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
  147. {
  148. struct timeval times[2];
  149. struct timespec tstimes[2];
  150. if (utimes) {
  151. if (copy_from_user(&times, utimes, sizeof(times)))
  152. return -EFAULT;
  153. /* This test is needed to catch all invalid values. If we
  154. would test only in do_utimes we would miss those invalid
  155. values truncated by the multiplication with 1000. Note
  156. that we also catch UTIME_{NOW,OMIT} here which are only
  157. valid for utimensat. */
  158. if (times[0].tv_usec >= 1000000 || times[0].tv_usec < 0 ||
  159. times[1].tv_usec >= 1000000 || times[1].tv_usec < 0)
  160. return -EINVAL;
  161. tstimes[0].tv_sec = times[0].tv_sec;
  162. tstimes[0].tv_nsec = 1000 * times[0].tv_usec;
  163. tstimes[1].tv_sec = times[1].tv_sec;
  164. tstimes[1].tv_nsec = 1000 * times[1].tv_usec;
  165. }
  166. return do_utimes(dfd, filename, utimes ? tstimes : NULL, 0);
  167. }
  168. asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
  169. {
  170. return sys_futimesat(AT_FDCWD, filename, utimes);
  171. }