utimes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if (times && times[0].tv_nsec == UTIME_NOW &&
  85. times[1].tv_nsec == UTIME_NOW)
  86. times = NULL;
  87. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  88. if (times) {
  89. error = -EPERM;
  90. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  91. goto mnt_drop_write_and_out;
  92. if (times[0].tv_nsec == UTIME_OMIT)
  93. newattrs.ia_valid &= ~ATTR_ATIME;
  94. else if (times[0].tv_nsec != UTIME_NOW) {
  95. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  96. newattrs.ia_atime.tv_nsec = times[0].tv_nsec;
  97. newattrs.ia_valid |= ATTR_ATIME_SET;
  98. }
  99. if (times[1].tv_nsec == UTIME_OMIT)
  100. newattrs.ia_valid &= ~ATTR_MTIME;
  101. else if (times[1].tv_nsec != UTIME_NOW) {
  102. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  103. newattrs.ia_mtime.tv_nsec = times[1].tv_nsec;
  104. newattrs.ia_valid |= ATTR_MTIME_SET;
  105. }
  106. /*
  107. * Tell inode_change_ok(), that this is an explicit time
  108. * update, even if neither ATTR_ATIME_SET nor ATTR_MTIME_SET
  109. * were used.
  110. */
  111. newattrs.ia_valid |= ATTR_TIMES_SET;
  112. } else {
  113. /*
  114. * If times is NULL (or both times are UTIME_NOW),
  115. * then we need to check permissions, because
  116. * inode_change_ok() won't do it.
  117. */
  118. error = -EACCES;
  119. if (IS_IMMUTABLE(inode))
  120. goto mnt_drop_write_and_out;
  121. if (!is_owner_or_cap(inode)) {
  122. error = permission(inode, MAY_WRITE, NULL);
  123. if (error)
  124. goto mnt_drop_write_and_out;
  125. }
  126. }
  127. mutex_lock(&inode->i_mutex);
  128. error = notify_change(dentry, &newattrs);
  129. mutex_unlock(&inode->i_mutex);
  130. mnt_drop_write_and_out:
  131. mnt_drop_write(mnt);
  132. dput_and_out:
  133. if (f)
  134. fput(f);
  135. else
  136. path_put(&nd.path);
  137. out:
  138. return error;
  139. }
  140. asmlinkage long sys_utimensat(int dfd, char __user *filename, struct timespec __user *utimes, int flags)
  141. {
  142. struct timespec tstimes[2];
  143. if (utimes) {
  144. if (copy_from_user(&tstimes, utimes, sizeof(tstimes)))
  145. return -EFAULT;
  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. }