utimes.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <linux/compiler.h>
  2. #include <linux/fs.h>
  3. #include <linux/linkage.h>
  4. #include <linux/namei.h>
  5. #include <linux/sched.h>
  6. #include <linux/utime.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/unistd.h>
  9. #ifdef __ARCH_WANT_SYS_UTIME
  10. /*
  11. * sys_utime() can be implemented in user-level using sys_utimes().
  12. * Is this for backwards compatibility? If so, why not move it
  13. * into the appropriate arch directory (for those architectures that
  14. * need it).
  15. */
  16. /* If times==NULL, set access and modification to current time,
  17. * must be owner or have write permission.
  18. * Else, update from *times, must be owner or super user.
  19. */
  20. asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
  21. {
  22. int error;
  23. struct nameidata nd;
  24. struct inode * inode;
  25. struct iattr newattrs;
  26. error = user_path_walk(filename, &nd);
  27. if (error)
  28. goto out;
  29. inode = nd.dentry->d_inode;
  30. error = -EROFS;
  31. if (IS_RDONLY(inode))
  32. goto dput_and_out;
  33. /* Don't worry, the checks are done in inode_change_ok() */
  34. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  35. if (times) {
  36. error = -EPERM;
  37. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  38. goto dput_and_out;
  39. error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
  40. newattrs.ia_atime.tv_nsec = 0;
  41. if (!error)
  42. error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
  43. newattrs.ia_mtime.tv_nsec = 0;
  44. if (error)
  45. goto dput_and_out;
  46. newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
  47. } else {
  48. error = -EACCES;
  49. if (IS_IMMUTABLE(inode))
  50. goto dput_and_out;
  51. if (current->fsuid != inode->i_uid &&
  52. (error = vfs_permission(&nd, MAY_WRITE)) != 0)
  53. goto dput_and_out;
  54. }
  55. mutex_lock(&inode->i_mutex);
  56. error = notify_change(nd.dentry, &newattrs);
  57. mutex_unlock(&inode->i_mutex);
  58. dput_and_out:
  59. path_release(&nd);
  60. out:
  61. return error;
  62. }
  63. #endif
  64. /* If times==NULL, set access and modification to current time,
  65. * must be owner or have write permission.
  66. * Else, update from *times, must be owner or super user.
  67. */
  68. long do_utimes(int dfd, char __user *filename, struct timeval *times)
  69. {
  70. int error;
  71. struct nameidata nd;
  72. struct inode * inode;
  73. struct iattr newattrs;
  74. error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  75. if (error)
  76. goto out;
  77. inode = nd.dentry->d_inode;
  78. error = -EROFS;
  79. if (IS_RDONLY(inode))
  80. goto dput_and_out;
  81. /* Don't worry, the checks are done in inode_change_ok() */
  82. newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
  83. if (times) {
  84. error = -EPERM;
  85. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  86. goto dput_and_out;
  87. newattrs.ia_atime.tv_sec = times[0].tv_sec;
  88. newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
  89. newattrs.ia_mtime.tv_sec = times[1].tv_sec;
  90. newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
  91. newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
  92. } else {
  93. error = -EACCES;
  94. if (IS_IMMUTABLE(inode))
  95. goto dput_and_out;
  96. if (current->fsuid != inode->i_uid &&
  97. (error = vfs_permission(&nd, MAY_WRITE)) != 0)
  98. goto dput_and_out;
  99. }
  100. mutex_lock(&inode->i_mutex);
  101. error = notify_change(nd.dentry, &newattrs);
  102. mutex_unlock(&inode->i_mutex);
  103. dput_and_out:
  104. path_release(&nd);
  105. out:
  106. return error;
  107. }
  108. asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
  109. {
  110. struct timeval times[2];
  111. if (utimes && copy_from_user(&times, utimes, sizeof(times)))
  112. return -EFAULT;
  113. return do_utimes(dfd, filename, utimes ? times : NULL);
  114. }
  115. asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
  116. {
  117. return sys_futimesat(AT_FDCWD, filename, utimes);
  118. }