ioctl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. #include <linux/capability.h>
  5. #include <linux/fs.h>
  6. #include <linux/mount.h>
  7. #include "reiserfs.h"
  8. #include <linux/time.h>
  9. #include <asm/uaccess.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/compat.h>
  12. /*
  13. * reiserfs_ioctl - handler for ioctl for inode
  14. * supported commands:
  15. * 1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
  16. * and prevent packing file (argument arg has to be non-zero)
  17. * 2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
  18. * 3) That's all for a while ...
  19. */
  20. long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  21. {
  22. struct inode *inode = filp->f_path.dentry->d_inode;
  23. unsigned int flags;
  24. int err = 0;
  25. reiserfs_write_lock(inode->i_sb);
  26. switch (cmd) {
  27. case REISERFS_IOC_UNPACK:
  28. if (S_ISREG(inode->i_mode)) {
  29. if (arg)
  30. err = reiserfs_unpack(inode, filp);
  31. } else
  32. err = -ENOTTY;
  33. break;
  34. /*
  35. * following two cases are taken from fs/ext2/ioctl.c by Remy
  36. * Card (card@masi.ibp.fr)
  37. */
  38. case REISERFS_IOC_GETFLAGS:
  39. if (!reiserfs_attrs(inode->i_sb)) {
  40. err = -ENOTTY;
  41. break;
  42. }
  43. flags = REISERFS_I(inode)->i_attrs;
  44. i_attrs_to_sd_attrs(inode, (__u16 *) & flags);
  45. err = put_user(flags, (int __user *)arg);
  46. break;
  47. case REISERFS_IOC_SETFLAGS:{
  48. if (!reiserfs_attrs(inode->i_sb)) {
  49. err = -ENOTTY;
  50. break;
  51. }
  52. err = mnt_want_write_file(filp);
  53. if (err)
  54. break;
  55. if (!inode_owner_or_capable(inode)) {
  56. err = -EPERM;
  57. goto setflags_out;
  58. }
  59. if (get_user(flags, (int __user *)arg)) {
  60. err = -EFAULT;
  61. goto setflags_out;
  62. }
  63. /*
  64. * Is it quota file? Do not allow user to mess with it
  65. */
  66. if (IS_NOQUOTA(inode)) {
  67. err = -EPERM;
  68. goto setflags_out;
  69. }
  70. if (((flags ^ REISERFS_I(inode)->
  71. i_attrs) & (REISERFS_IMMUTABLE_FL |
  72. REISERFS_APPEND_FL))
  73. && !capable(CAP_LINUX_IMMUTABLE)) {
  74. err = -EPERM;
  75. goto setflags_out;
  76. }
  77. if ((flags & REISERFS_NOTAIL_FL) &&
  78. S_ISREG(inode->i_mode)) {
  79. int result;
  80. result = reiserfs_unpack(inode, filp);
  81. if (result) {
  82. err = result;
  83. goto setflags_out;
  84. }
  85. }
  86. sd_attrs_to_i_attrs(flags, inode);
  87. REISERFS_I(inode)->i_attrs = flags;
  88. inode->i_ctime = CURRENT_TIME_SEC;
  89. mark_inode_dirty(inode);
  90. setflags_out:
  91. mnt_drop_write_file(filp);
  92. break;
  93. }
  94. case REISERFS_IOC_GETVERSION:
  95. err = put_user(inode->i_generation, (int __user *)arg);
  96. break;
  97. case REISERFS_IOC_SETVERSION:
  98. if (!inode_owner_or_capable(inode)) {
  99. err = -EPERM;
  100. break;
  101. }
  102. err = mnt_want_write_file(filp);
  103. if (err)
  104. break;
  105. if (get_user(inode->i_generation, (int __user *)arg)) {
  106. err = -EFAULT;
  107. goto setversion_out;
  108. }
  109. inode->i_ctime = CURRENT_TIME_SEC;
  110. mark_inode_dirty(inode);
  111. setversion_out:
  112. mnt_drop_write_file(filp);
  113. break;
  114. default:
  115. err = -ENOTTY;
  116. }
  117. reiserfs_write_unlock(inode->i_sb);
  118. return err;
  119. }
  120. #ifdef CONFIG_COMPAT
  121. long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
  122. unsigned long arg)
  123. {
  124. /* These are just misnamed, they actually get/put from/to user an int */
  125. switch (cmd) {
  126. case REISERFS_IOC32_UNPACK:
  127. cmd = REISERFS_IOC_UNPACK;
  128. break;
  129. case REISERFS_IOC32_GETFLAGS:
  130. cmd = REISERFS_IOC_GETFLAGS;
  131. break;
  132. case REISERFS_IOC32_SETFLAGS:
  133. cmd = REISERFS_IOC_SETFLAGS;
  134. break;
  135. case REISERFS_IOC32_GETVERSION:
  136. cmd = REISERFS_IOC_GETVERSION;
  137. break;
  138. case REISERFS_IOC32_SETVERSION:
  139. cmd = REISERFS_IOC_SETVERSION;
  140. break;
  141. default:
  142. return -ENOIOCTLCMD;
  143. }
  144. return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  145. }
  146. #endif
  147. int reiserfs_commit_write(struct file *f, struct page *page,
  148. unsigned from, unsigned to);
  149. /*
  150. ** reiserfs_unpack
  151. ** Function try to convert tail from direct item into indirect.
  152. ** It set up nopack attribute in the REISERFS_I(inode)->nopack
  153. */
  154. int reiserfs_unpack(struct inode *inode, struct file *filp)
  155. {
  156. int retval = 0;
  157. int depth;
  158. int index;
  159. struct page *page;
  160. struct address_space *mapping;
  161. unsigned long write_from;
  162. unsigned long blocksize = inode->i_sb->s_blocksize;
  163. if (inode->i_size == 0) {
  164. REISERFS_I(inode)->i_flags |= i_nopack_mask;
  165. return 0;
  166. }
  167. /* ioctl already done */
  168. if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
  169. return 0;
  170. }
  171. depth = reiserfs_write_lock_once(inode->i_sb);
  172. /* we need to make sure nobody is changing the file size beneath us */
  173. reiserfs_mutex_lock_safe(&inode->i_mutex, inode->i_sb);
  174. write_from = inode->i_size & (blocksize - 1);
  175. /* if we are on a block boundary, we are already unpacked. */
  176. if (write_from == 0) {
  177. REISERFS_I(inode)->i_flags |= i_nopack_mask;
  178. goto out;
  179. }
  180. /* we unpack by finding the page with the tail, and calling
  181. ** __reiserfs_write_begin on that page. This will force a
  182. ** reiserfs_get_block to unpack the tail for us.
  183. */
  184. index = inode->i_size >> PAGE_CACHE_SHIFT;
  185. mapping = inode->i_mapping;
  186. page = grab_cache_page(mapping, index);
  187. retval = -ENOMEM;
  188. if (!page) {
  189. goto out;
  190. }
  191. retval = __reiserfs_write_begin(page, write_from, 0);
  192. if (retval)
  193. goto out_unlock;
  194. /* conversion can change page contents, must flush */
  195. flush_dcache_page(page);
  196. retval = reiserfs_commit_write(NULL, page, write_from, write_from);
  197. REISERFS_I(inode)->i_flags |= i_nopack_mask;
  198. out_unlock:
  199. unlock_page(page);
  200. page_cache_release(page);
  201. out:
  202. mutex_unlock(&inode->i_mutex);
  203. reiserfs_write_unlock_once(inode->i_sb, depth);
  204. return retval;
  205. }