utimes.c 4.8 KB

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