ioctl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Authors: Zoltan Sogor
  21. * Artem Bityutskiy (Битюцкий Артём)
  22. * Adrian Hunter
  23. */
  24. /* This file implements EXT2-compatible extended attribute ioctl() calls */
  25. #include <linux/compat.h>
  26. #include <linux/mount.h>
  27. #include "ubifs.h"
  28. /**
  29. * ubifs_set_inode_flags - set VFS inode flags.
  30. * @inode: VFS inode to set flags for
  31. *
  32. * This function propagates flags from UBIFS inode object to VFS inode object.
  33. */
  34. void ubifs_set_inode_flags(struct inode *inode)
  35. {
  36. unsigned int flags = ubifs_inode(inode)->flags;
  37. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
  38. S_ENCRYPTED);
  39. if (flags & UBIFS_SYNC_FL)
  40. inode->i_flags |= S_SYNC;
  41. if (flags & UBIFS_APPEND_FL)
  42. inode->i_flags |= S_APPEND;
  43. if (flags & UBIFS_IMMUTABLE_FL)
  44. inode->i_flags |= S_IMMUTABLE;
  45. if (flags & UBIFS_DIRSYNC_FL)
  46. inode->i_flags |= S_DIRSYNC;
  47. if (flags & UBIFS_CRYPT_FL)
  48. inode->i_flags |= S_ENCRYPTED;
  49. }
  50. /*
  51. * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
  52. * @ioctl_flags: flags to convert
  53. *
  54. * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
  55. * (@UBIFS_COMPR_FL, etc).
  56. */
  57. static int ioctl2ubifs(int ioctl_flags)
  58. {
  59. int ubifs_flags = 0;
  60. if (ioctl_flags & FS_COMPR_FL)
  61. ubifs_flags |= UBIFS_COMPR_FL;
  62. if (ioctl_flags & FS_SYNC_FL)
  63. ubifs_flags |= UBIFS_SYNC_FL;
  64. if (ioctl_flags & FS_APPEND_FL)
  65. ubifs_flags |= UBIFS_APPEND_FL;
  66. if (ioctl_flags & FS_IMMUTABLE_FL)
  67. ubifs_flags |= UBIFS_IMMUTABLE_FL;
  68. if (ioctl_flags & FS_DIRSYNC_FL)
  69. ubifs_flags |= UBIFS_DIRSYNC_FL;
  70. return ubifs_flags;
  71. }
  72. /*
  73. * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
  74. * @ubifs_flags: flags to convert
  75. *
  76. * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
  77. * flags (@FS_COMPR_FL, etc).
  78. */
  79. static int ubifs2ioctl(int ubifs_flags)
  80. {
  81. int ioctl_flags = 0;
  82. if (ubifs_flags & UBIFS_COMPR_FL)
  83. ioctl_flags |= FS_COMPR_FL;
  84. if (ubifs_flags & UBIFS_SYNC_FL)
  85. ioctl_flags |= FS_SYNC_FL;
  86. if (ubifs_flags & UBIFS_APPEND_FL)
  87. ioctl_flags |= FS_APPEND_FL;
  88. if (ubifs_flags & UBIFS_IMMUTABLE_FL)
  89. ioctl_flags |= FS_IMMUTABLE_FL;
  90. if (ubifs_flags & UBIFS_DIRSYNC_FL)
  91. ioctl_flags |= FS_DIRSYNC_FL;
  92. return ioctl_flags;
  93. }
  94. static int setflags(struct inode *inode, int flags)
  95. {
  96. int oldflags, err, release;
  97. struct ubifs_inode *ui = ubifs_inode(inode);
  98. struct ubifs_info *c = inode->i_sb->s_fs_info;
  99. struct ubifs_budget_req req = { .dirtied_ino = 1,
  100. .dirtied_ino_d = ui->data_len };
  101. err = ubifs_budget_space(c, &req);
  102. if (err)
  103. return err;
  104. /*
  105. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  106. * the relevant capability.
  107. */
  108. mutex_lock(&ui->ui_mutex);
  109. oldflags = ubifs2ioctl(ui->flags);
  110. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  111. if (!capable(CAP_LINUX_IMMUTABLE)) {
  112. err = -EPERM;
  113. goto out_unlock;
  114. }
  115. }
  116. ui->flags = ioctl2ubifs(flags);
  117. ubifs_set_inode_flags(inode);
  118. inode->i_ctime = current_time(inode);
  119. release = ui->dirty;
  120. mark_inode_dirty_sync(inode);
  121. mutex_unlock(&ui->ui_mutex);
  122. if (release)
  123. ubifs_release_budget(c, &req);
  124. if (IS_SYNC(inode))
  125. err = write_inode_now(inode, 1);
  126. return err;
  127. out_unlock:
  128. ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
  129. mutex_unlock(&ui->ui_mutex);
  130. ubifs_release_budget(c, &req);
  131. return err;
  132. }
  133. long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  134. {
  135. int flags, err;
  136. struct inode *inode = file_inode(file);
  137. switch (cmd) {
  138. case FS_IOC_GETFLAGS:
  139. flags = ubifs2ioctl(ubifs_inode(inode)->flags);
  140. dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
  141. return put_user(flags, (int __user *) arg);
  142. case FS_IOC_SETFLAGS: {
  143. if (IS_RDONLY(inode))
  144. return -EROFS;
  145. if (!inode_owner_or_capable(inode))
  146. return -EACCES;
  147. if (get_user(flags, (int __user *) arg))
  148. return -EFAULT;
  149. if (!S_ISDIR(inode->i_mode))
  150. flags &= ~FS_DIRSYNC_FL;
  151. /*
  152. * Make sure the file-system is read-write and make sure it
  153. * will not become read-only while we are changing the flags.
  154. */
  155. err = mnt_want_write_file(file);
  156. if (err)
  157. return err;
  158. dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
  159. err = setflags(inode, flags);
  160. mnt_drop_write_file(file);
  161. return err;
  162. }
  163. case FS_IOC_SET_ENCRYPTION_POLICY: {
  164. #ifdef CONFIG_UBIFS_FS_ENCRYPTION
  165. struct ubifs_info *c = inode->i_sb->s_fs_info;
  166. err = ubifs_enable_encryption(c);
  167. if (err)
  168. return err;
  169. return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  170. #else
  171. return -EOPNOTSUPP;
  172. #endif
  173. }
  174. case FS_IOC_GET_ENCRYPTION_POLICY: {
  175. #ifdef CONFIG_UBIFS_FS_ENCRYPTION
  176. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  177. #else
  178. return -EOPNOTSUPP;
  179. #endif
  180. }
  181. default:
  182. return -ENOTTY;
  183. }
  184. }
  185. #ifdef CONFIG_COMPAT
  186. long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  187. {
  188. switch (cmd) {
  189. case FS_IOC32_GETFLAGS:
  190. cmd = FS_IOC_GETFLAGS;
  191. break;
  192. case FS_IOC32_SETFLAGS:
  193. cmd = FS_IOC_SETFLAGS;
  194. break;
  195. case FS_IOC_SET_ENCRYPTION_POLICY:
  196. case FS_IOC_GET_ENCRYPTION_POLICY:
  197. break;
  198. default:
  199. return -ENOIOCTLCMD;
  200. }
  201. return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  202. }
  203. #endif