ioctl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <linux/fileattr.h>
  16. #include "ubifs.h"
  17. /* Need to be kept consistent with checked flags in ioctl2ubifs() */
  18. #define UBIFS_SETTABLE_IOCTL_FLAGS \
  19. (FS_COMPR_FL | FS_SYNC_FL | FS_APPEND_FL | \
  20. FS_IMMUTABLE_FL | FS_DIRSYNC_FL)
  21. /* Need to be kept consistent with checked flags in ubifs2ioctl() */
  22. #define UBIFS_GETTABLE_IOCTL_FLAGS \
  23. (UBIFS_SETTABLE_IOCTL_FLAGS | FS_ENCRYPT_FL)
  24. /**
  25. * ubifs_set_inode_flags - set VFS inode flags.
  26. * @inode: VFS inode to set flags for
  27. *
  28. * This function propagates flags from UBIFS inode object to VFS inode object.
  29. */
  30. void ubifs_set_inode_flags(struct inode *inode)
  31. {
  32. unsigned int flags = ubifs_inode(inode)->flags;
  33. inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC |
  34. S_ENCRYPTED);
  35. if (flags & UBIFS_SYNC_FL)
  36. inode->i_flags |= S_SYNC;
  37. if (flags & UBIFS_APPEND_FL)
  38. inode->i_flags |= S_APPEND;
  39. if (flags & UBIFS_IMMUTABLE_FL)
  40. inode->i_flags |= S_IMMUTABLE;
  41. if (flags & UBIFS_DIRSYNC_FL)
  42. inode->i_flags |= S_DIRSYNC;
  43. if (flags & UBIFS_CRYPT_FL)
  44. inode->i_flags |= S_ENCRYPTED;
  45. }
  46. /*
  47. * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
  48. * @ioctl_flags: flags to convert
  49. *
  50. * This function converts ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
  51. * (@UBIFS_COMPR_FL, etc).
  52. */
  53. static int ioctl2ubifs(int ioctl_flags)
  54. {
  55. int ubifs_flags = 0;
  56. if (ioctl_flags & FS_COMPR_FL)
  57. ubifs_flags |= UBIFS_COMPR_FL;
  58. if (ioctl_flags & FS_SYNC_FL)
  59. ubifs_flags |= UBIFS_SYNC_FL;
  60. if (ioctl_flags & FS_APPEND_FL)
  61. ubifs_flags |= UBIFS_APPEND_FL;
  62. if (ioctl_flags & FS_IMMUTABLE_FL)
  63. ubifs_flags |= UBIFS_IMMUTABLE_FL;
  64. if (ioctl_flags & FS_DIRSYNC_FL)
  65. ubifs_flags |= UBIFS_DIRSYNC_FL;
  66. return ubifs_flags;
  67. }
  68. /*
  69. * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
  70. * @ubifs_flags: flags to convert
  71. *
  72. * This function converts UBIFS inode flags (@UBIFS_COMPR_FL, etc) to ioctl
  73. * flags (@FS_COMPR_FL, etc).
  74. */
  75. static int ubifs2ioctl(int ubifs_flags)
  76. {
  77. int ioctl_flags = 0;
  78. if (ubifs_flags & UBIFS_COMPR_FL)
  79. ioctl_flags |= FS_COMPR_FL;
  80. if (ubifs_flags & UBIFS_SYNC_FL)
  81. ioctl_flags |= FS_SYNC_FL;
  82. if (ubifs_flags & UBIFS_APPEND_FL)
  83. ioctl_flags |= FS_APPEND_FL;
  84. if (ubifs_flags & UBIFS_IMMUTABLE_FL)
  85. ioctl_flags |= FS_IMMUTABLE_FL;
  86. if (ubifs_flags & UBIFS_DIRSYNC_FL)
  87. ioctl_flags |= FS_DIRSYNC_FL;
  88. if (ubifs_flags & UBIFS_CRYPT_FL)
  89. ioctl_flags |= FS_ENCRYPT_FL;
  90. return ioctl_flags;
  91. }
  92. static int setflags(struct inode *inode, int flags)
  93. {
  94. int err, release;
  95. struct ubifs_inode *ui = ubifs_inode(inode);
  96. struct ubifs_info *c = inode->i_sb->s_fs_info;
  97. struct ubifs_budget_req req = { .dirtied_ino = 1,
  98. .dirtied_ino_d = ALIGN(ui->data_len, 8) };
  99. err = ubifs_budget_space(c, &req);
  100. if (err)
  101. return err;
  102. mutex_lock(&ui->ui_mutex);
  103. ui->flags &= ~ioctl2ubifs(UBIFS_SETTABLE_IOCTL_FLAGS);
  104. ui->flags |= ioctl2ubifs(flags);
  105. ubifs_set_inode_flags(inode);
  106. inode->i_ctime = current_time(inode);
  107. release = ui->dirty;
  108. mark_inode_dirty_sync(inode);
  109. mutex_unlock(&ui->ui_mutex);
  110. if (release)
  111. ubifs_release_budget(c, &req);
  112. if (IS_SYNC(inode))
  113. err = write_inode_now(inode, 1);
  114. return err;
  115. }
  116. int ubifs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  117. {
  118. struct inode *inode = d_inode(dentry);
  119. int flags = ubifs2ioctl(ubifs_inode(inode)->flags);
  120. if (d_is_special(dentry))
  121. return -ENOTTY;
  122. dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
  123. fileattr_fill_flags(fa, flags);
  124. return 0;
  125. }
  126. int ubifs_fileattr_set(struct user_namespace *mnt_userns,
  127. struct dentry *dentry, struct fileattr *fa)
  128. {
  129. struct inode *inode = d_inode(dentry);
  130. int flags = fa->flags;
  131. if (d_is_special(dentry))
  132. return -ENOTTY;
  133. if (fileattr_has_fsx(fa))
  134. return -EOPNOTSUPP;
  135. if (flags & ~UBIFS_GETTABLE_IOCTL_FLAGS)
  136. return -EOPNOTSUPP;
  137. flags &= UBIFS_SETTABLE_IOCTL_FLAGS;
  138. if (!S_ISDIR(inode->i_mode))
  139. flags &= ~FS_DIRSYNC_FL;
  140. dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
  141. return setflags(inode, flags);
  142. }
  143. long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  144. {
  145. int err;
  146. struct inode *inode = file_inode(file);
  147. switch (cmd) {
  148. case FS_IOC_SET_ENCRYPTION_POLICY: {
  149. struct ubifs_info *c = inode->i_sb->s_fs_info;
  150. err = ubifs_enable_encryption(c);
  151. if (err)
  152. return err;
  153. return fscrypt_ioctl_set_policy(file, (const void __user *)arg);
  154. }
  155. case FS_IOC_GET_ENCRYPTION_POLICY:
  156. return fscrypt_ioctl_get_policy(file, (void __user *)arg);
  157. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  158. return fscrypt_ioctl_get_policy_ex(file, (void __user *)arg);
  159. case FS_IOC_ADD_ENCRYPTION_KEY:
  160. return fscrypt_ioctl_add_key(file, (void __user *)arg);
  161. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  162. return fscrypt_ioctl_remove_key(file, (void __user *)arg);
  163. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  164. return fscrypt_ioctl_remove_key_all_users(file,
  165. (void __user *)arg);
  166. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  167. return fscrypt_ioctl_get_key_status(file, (void __user *)arg);
  168. case FS_IOC_GET_ENCRYPTION_NONCE:
  169. return fscrypt_ioctl_get_nonce(file, (void __user *)arg);
  170. default:
  171. return -ENOTTY;
  172. }
  173. }
  174. #ifdef CONFIG_COMPAT
  175. long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  176. {
  177. switch (cmd) {
  178. case FS_IOC32_GETFLAGS:
  179. cmd = FS_IOC_GETFLAGS;
  180. break;
  181. case FS_IOC32_SETFLAGS:
  182. cmd = FS_IOC_SETFLAGS;
  183. break;
  184. case FS_IOC_SET_ENCRYPTION_POLICY:
  185. case FS_IOC_GET_ENCRYPTION_POLICY:
  186. case FS_IOC_GET_ENCRYPTION_POLICY_EX:
  187. case FS_IOC_ADD_ENCRYPTION_KEY:
  188. case FS_IOC_REMOVE_ENCRYPTION_KEY:
  189. case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
  190. case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
  191. case FS_IOC_GET_ENCRYPTION_NONCE:
  192. break;
  193. default:
  194. return -ENOIOCTLCMD;
  195. }
  196. return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  197. }
  198. #endif