ioctl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /* Need to be kept consistent with checked flags in ioctl2ubifs() */
  29. #define UBIFS_SUPPORTED_IOCTL_FLAGS \
  30. (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
  31. FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
  32. /**
  33. * ubifs_set_inode_flags - set VFS inode flags.
  34. * @inode: VFS inode to set flags for
  35. *
  36. * This function propagates flags from UBIFS inode object to VFS inode object.
  37. */
  38. void ubifs_set_inode_flags(struct inode *inode)
  39. {
  40. unsigned int flags = ubifs_inode(inode)->flags;
  41. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
  42. S_ENCRYPTED);
  43. if (flags & UBIFS_SYNC_FL)
  44. inode->i_flags |= S_SYNC;
  45. if (flags & UBIFS_APPEND_FL)
  46. inode->i_flags |= S_APPEND;
  47. if (flags & UBIFS_IMMUTABLE_FL)
  48. inode->i_flags |= S_IMMUTABLE;
  49. if (flags & UBIFS_DIRSYNC_FL)
  50. inode->i_flags |= S_DIRSYNC;
  51. if (flags & UBIFS_CRYPT_FL)
  52. inode->i_flags |= S_ENCRYPTED;
  53. }
  54. /*
  55. * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
  56. * @ioctl_flags: flags to convert
  57. *
  58. * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
  59. * (@UBIFS_COMPR_FL, etc).
  60. */
  61. static int ioctl2ubifs(int ioctl_flags)
  62. {
  63. int ubifs_flags = 0;
  64. if (ioctl_flags & FS_COMPR_FL)
  65. ubifs_flags |= UBIFS_COMPR_FL;
  66. if (ioctl_flags & FS_SYNC_FL)
  67. ubifs_flags |= UBIFS_SYNC_FL;
  68. if (ioctl_flags & FS_APPEND_FL)
  69. ubifs_flags |= UBIFS_APPEND_FL;
  70. if (ioctl_flags & FS_IMMUTABLE_FL)
  71. ubifs_flags |= UBIFS_IMMUTABLE_FL;
  72. if (ioctl_flags & FS_DIRSYNC_FL)
  73. ubifs_flags |= UBIFS_DIRSYNC_FL;
  74. return ubifs_flags;
  75. }
  76. /*
  77. * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
  78. * @ubifs_flags: flags to convert
  79. *
  80. * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
  81. * flags (@FS_COMPR_FL, etc).
  82. */
  83. static int ubifs2ioctl(int ubifs_flags)
  84. {
  85. int ioctl_flags = 0;
  86. if (ubifs_flags & UBIFS_COMPR_FL)
  87. ioctl_flags |= FS_COMPR_FL;
  88. if (ubifs_flags & UBIFS_SYNC_FL)
  89. ioctl_flags |= FS_SYNC_FL;
  90. if (ubifs_flags & UBIFS_APPEND_FL)
  91. ioctl_flags |= FS_APPEND_FL;
  92. if (ubifs_flags & UBIFS_IMMUTABLE_FL)
  93. ioctl_flags |= FS_IMMUTABLE_FL;
  94. if (ubifs_flags & UBIFS_DIRSYNC_FL)
  95. ioctl_flags |= FS_DIRSYNC_FL;
  96. return ioctl_flags;
  97. }
  98. static int setflags(struct inode *inode, int flags)
  99. {
  100. int oldflags, err, release;
  101. struct ubifs_inode *ui = ubifs_inode(inode);
  102. struct ubifs_info *c = inode->i_sb->s_fs_info;
  103. struct ubifs_budget_req req = { .dirtied_ino = 1,
  104. .dirtied_ino_d = ui->data_len };
  105. err = ubifs_budget_space(c, &req);
  106. if (err)
  107. return err;
  108. /*
  109. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  110. * the relevant capability.
  111. */
  112. mutex_lock(&ui->ui_mutex);
  113. oldflags = ubifs2ioctl(ui->flags);
  114. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  115. if (!capable(CAP_LINUX_IMMUTABLE)) {
  116. err = -EPERM;
  117. goto out_unlock;
  118. }
  119. }
  120. ui->flags &= ~ioctl2ubifs(UBIFS_SUPPORTED_IOCTL_FLAGS);
  121. ui->flags |= ioctl2ubifs(flags);
  122. ubifs_set_inode_flags(inode);
  123. inode->i_ctime = current_time(inode);
  124. release = ui->dirty;
  125. mark_inode_dirty_sync(inode);
  126. mutex_unlock(&ui->ui_mutex);
  127. if (release)
  128. ubifs_release_budget(c, &req);
  129. if (IS_SYNC(inode))
  130. err = write_inode_now(inode, 1);
  131. return err;
  132. out_unlock:
  133. ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
  134. mutex_unlock(&ui->ui_mutex);
  135. ubifs_release_budget(c, &req);
  136. return err;
  137. }
  138. long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  139. {
  140. int flags, err;
  141. struct inode *inode = file_inode(file);
  142. switch (cmd) {
  143. case FS_IOC_GETFLAGS:
  144. flags = ubifs2ioctl(ubifs_inode(inode)->flags);
  145. dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
  146. return put_user(flags, (int __user *) arg);
  147. case FS_IOC_SETFLAGS: {
  148. if (IS_RDONLY(inode))
  149. return -EROFS;
  150. if (!inode_owner_or_capable(inode))
  151. return -EACCES;
  152. if (get_user(flags, (int __user *) arg))
  153. return -EFAULT;
  154. if (flags & ~UBIFS_SUPPORTED_IOCTL_FLAGS)
  155. return -EOPNOTSUPP;
  156. if (!S_ISDIR(inode->i_mode))
  157. flags &= ~FS_DIRSYNC_FL;
  158. /*
  159. * Make sure the file-system is read-write and make sure it
  160. * will not become read-only while we are changing the flags.
  161. */
  162. err = mnt_want_write_file(file);
  163. if (err)
  164. return err;
  165. dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
  166. err = setflags(inode, flags);
  167. mnt_drop_write_file(file);
  168. return err;
  169. }
  170. case FS_IOC_SET_ENCRYPTION_POLICY: {
  171. #ifdef CONFIG_UBIFS_FS_ENCRYPTION
  172. struct ubifs_info *c = inode->i_sb->s_fs_info;
  173. err = ubifs_enable_encryption(c);
  174. if (err)
  175. return err;
  176. return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  177. #else
  178. return -EOPNOTSUPP;
  179. #endif
  180. }
  181. case FS_IOC_GET_ENCRYPTION_POLICY: {
  182. #ifdef CONFIG_UBIFS_FS_ENCRYPTION
  183. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  184. #else
  185. return -EOPNOTSUPP;
  186. #endif
  187. }
  188. default:
  189. return -ENOTTY;
  190. }
  191. }
  192. #ifdef CONFIG_COMPAT
  193. long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  194. {
  195. switch (cmd) {
  196. case FS_IOC32_GETFLAGS:
  197. cmd = FS_IOC_GETFLAGS;
  198. break;
  199. case FS_IOC32_SETFLAGS:
  200. cmd = FS_IOC_SETFLAGS;
  201. break;
  202. case FS_IOC_SET_ENCRYPTION_POLICY:
  203. case FS_IOC_GET_ENCRYPTION_POLICY:
  204. break;
  205. default:
  206. return -ENOIOCTLCMD;
  207. }
  208. return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  209. }
  210. #endif