ioctl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. * Copyright (C) 2006, 2007 University of Szeged, Hungary
  7. *
  8. * Authors: Zoltan Sogor
  9. * Artem Bityutskiy (Битюцкий Артём)
  10. * Adrian Hunter
  11. */
  12. /* This file implements EXT2-compatible extended attribute ioctl() calls */
  13. #include <linux/compat.h>
  14. #include <linux/mount.h>
  15. #include "ubifs.h"
  16. /* Need to be kept consistent with checked flags in ioctl2ubifs() */
  17. #define UBIFS_SETTABLE_IOCTL_FLAGS \
  18. (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
  19. FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
  20. /* Need to be kept consistent with checked flags in ubifs2ioctl() */
  21. #define UBIFS_GETTABLE_IOCTL_FLAGS \
  22. (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL)
  23. /**
  24. * ubifs_set_inode_flags - set VFS inode flags.
  25. * @inode: VFS inode to set flags for
  26. *
  27. * This function propagates flags from UBIFS inode object to VFS inode object.
  28. */
  29. void ubifs_set_inode_flags(struct inode *inode)
  30. {
  31. unsigned int flags = ubifs_inode(inode)->flags;
  32. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
  33. S_ENCRYPTED);
  34. if (flags & UBIFS_SYNC_FL)
  35. inode->i_flags |= S_SYNC;
  36. if (flags & UBIFS_APPEND_FL)
  37. inode->i_flags |= S_APPEND;
  38. if (flags & UBIFS_IMMUTABLE_FL)
  39. inode->i_flags |= S_IMMUTABLE;
  40. if (flags & UBIFS_DIRSYNC_FL)
  41. inode->i_flags |= S_DIRSYNC;
  42. if (flags & UBIFS_CRYPT_FL)
  43. inode->i_flags |= S_ENCRYPTED;
  44. }
  45. /*
  46. * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
  47. * @ioctl_flags: flags to convert
  48. *
  49. * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
  50. * (@UBIFS_COMPR_FL, etc).
  51. */
  52. static int ioctl2ubifs(int ioctl_flags)
  53. {
  54. int ubifs_flags = 0;
  55. if (ioctl_flags & FS_COMPR_FL)
  56. ubifs_flags |= UBIFS_COMPR_FL;
  57. if (ioctl_flags & FS_SYNC_FL)
  58. ubifs_flags |= UBIFS_SYNC_FL;
  59. if (ioctl_flags & FS_APPEND_FL)
  60. ubifs_flags |= UBIFS_APPEND_FL;
  61. if (ioctl_flags & FS_IMMUTABLE_FL)
  62. ubifs_flags |= UBIFS_IMMUTABLE_FL;
  63. if (ioctl_flags & FS_DIRSYNC_FL)
  64. ubifs_flags |= UBIFS_DIRSYNC_FL;
  65. return ubifs_flags;
  66. }
  67. /*
  68. * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
  69. * @ubifs_flags: flags to convert
  70. *
  71. * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
  72. * flags (@FS_COMPR_FL, etc).
  73. */
  74. static int ubifs2ioctl(int ubifs_flags)
  75. {
  76. int ioctl_flags = 0;
  77. if (ubifs_flags & UBIFS_COMPR_FL)
  78. ioctl_flags |= FS_COMPR_FL;
  79. if (ubifs_flags & UBIFS_SYNC_FL)
  80. ioctl_flags |= FS_SYNC_FL;
  81. if (ubifs_flags & UBIFS_APPEND_FL)
  82. ioctl_flags |= FS_APPEND_FL;
  83. if (ubifs_flags & UBIFS_IMMUTABLE_FL)
  84. ioctl_flags |= FS_IMMUTABLE_FL;
  85. if (ubifs_flags & UBIFS_DIRSYNC_FL)
  86. ioctl_flags |= FS_DIRSYNC_FL;
  87. if (ubifs_flags & UBIFS_CRYPT_FL)
  88. ioctl_flags |= FS_ENCRYPT_FL;
  89. return ioctl_flags;
  90. }
  91. static int setflags(struct inode *inode, int flags)
  92. {
  93. int oldflags, err, release;
  94. struct ubifs_inode *ui = ubifs_inode(inode);
  95. struct ubifs_info *c = inode->i_sb->s_fs_info;
  96. struct ubifs_budget_req req = { .dirtied_ino = 1,
  97. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  98. err = ubifs_budget_space(c, &req);
  99. if (err)
  100. return err;
  101. mutex_lock(&ui->ui_mutex);
  102. oldflags = ubifs2ioctl(ui->flags);
  103. err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
  104. if (err)
  105. goto out_unlock;
  106. ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS);
  107. ui->flags |= ioctl2ubifs(flags);
  108. ubifs_set_inode_flags(inode);
  109. inode->i_ctime = current_time(inode);
  110. release = ui->dirty;
  111. mark_inode_dirty_sync(inode);
  112. mutex_unlock(&ui->ui_mutex);
  113. if (release)
  114. ubifs_release_budget(c, &req);
  115. if (IS_SYNC(inode))
  116. err = write_inode_now(inode, 1);
  117. return err;
  118. out_unlock:
  119. mutex_unlock(&ui->ui_mutex);
  120. ubifs_release_budget(c, &req);
  121. return err;
  122. }
  123. long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  124. {
  125. int flags, err;
  126. struct inode *inode = file_inode(file);
  127. switch (cmd) {
  128. case FS_IOC_GETFLAGS:
  129. flags = ubifs2ioctl(ubifs_inode(inode)->flags);
  130. dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
  131. return put_user(flags, (int __user *) arg);
  132. case FS_IOC_SETFLAGS: {
  133. if (IS_RDONLY(inode))
  134. return -EROFS;
  135. if (!inode_owner_or_capable(inode))
  136. return -EACCES;
  137. if (get_user(flags, (int __user *) arg))
  138. return -EFAULT;
  139. if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS)
  140. return -EOPNOTSUPP;
  141. flags &= UBIFS_SETTABLE_IOCTL_FLAGS;
  142. if (!S_ISDIR(inode->i_mode))
  143. flags &= ~FS_DIRSYNC_FL;
  144. /*
  145. * Make sure the file-system is read-write and make sure it
  146. * will not become read-only while we are changing the flags.
  147. */
  148. err = mnt_want_write_file(file);
  149. if (err)
  150. return err;
  151. dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
  152. err = setflags(inode, flags);
  153. mnt_drop_write_file(file);
  154. return err;
  155. }
  156. case FS_IOC_SET_ENCRYPTION_POLICY: {
  157. struct ubifs_info *c = inode->i_sb->s_fs_info;
  158. err = ubifs_enable_encryption(c);
  159. if (err)
  160. return err;
  161. return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  162. }
  163. case FS_IOC_GET_ENCRYPTION_POLICY:
  164. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  165. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  166. return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
  167. case FS_IOC_ADD_ENCRYPTION_KEY:
  168. return fscrypt_ioctl_add_key(file, (void __user *)arg);
  169. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  170. return fscrypt_ioctl_remove_key(file, (void __user *)arg);
  171. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  172. return fscrypt_ioctl_remove_key_all_users(file,
  173. (void __user *)arg);
  174. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  175. return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
  176. case FS_IOC_GET_ENCRYPTION_NONCE:
  177. return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
  178. default:
  179. return -ENOTTY;
  180. }
  181. }
  182. #ifdef CONFIG_COMPAT
  183. long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  184. {
  185. switch (cmd) {
  186. case FS_IOC32_GETFLAGS:
  187. cmd = FS_IOC_GETFLAGS;
  188. break;
  189. case FS_IOC32_SETFLAGS:
  190. cmd = FS_IOC_SETFLAGS;
  191. break;
  192. case FS_IOC_SET_ENCRYPTION_POLICY:
  193. case FS_IOC_GET_ENCRYPTION_POLICY:
  194. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  195. case FS_IOC_ADD_ENCRYPTION_KEY:
  196. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  197. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  198. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  199. case FS_IOC_GET_ENCRYPTION_NONCE:
  200. break;
  201. default:
  202. return -ENOIOCTLCMD;
  203. }
  204. return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  205. }
  206. #endif