ioctl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (flags & UBIFS_SYNC_FL)
  39. inode->i_flags |= S_SYNC;
  40. if (flags & UBIFS_APPEND_FL)
  41. inode->i_flags |= S_APPEND;
  42. if (flags & UBIFS_IMMUTABLE_FL)
  43. inode->i_flags |= S_IMMUTABLE;
  44. if (flags & UBIFS_DIRSYNC_FL)
  45. inode->i_flags |= S_DIRSYNC;
  46. }
  47. /*
  48. * ioctl2ubifs - convert ioctl inode flags to UBIFS inode flags.
  49. * @ioctl_flags: flags to convert
  50. *
  51. * This function convert ioctl flags (@FS_COMPR_FL, etc) to UBIFS inode flags
  52. * (@UBIFS_COMPR_FL, etc).
  53. */
  54. static int ioctl2ubifs(int ioctl_flags)
  55. {
  56. int ubifs_flags = 0;
  57. if (ioctl_flags & FS_COMPR_FL)
  58. ubifs_flags |= UBIFS_COMPR_FL;
  59. if (ioctl_flags & FS_SYNC_FL)
  60. ubifs_flags |= UBIFS_SYNC_FL;
  61. if (ioctl_flags & FS_APPEND_FL)
  62. ubifs_flags |= UBIFS_APPEND_FL;
  63. if (ioctl_flags & FS_IMMUTABLE_FL)
  64. ubifs_flags |= UBIFS_IMMUTABLE_FL;
  65. if (ioctl_flags & FS_DIRSYNC_FL)
  66. ubifs_flags |= UBIFS_DIRSYNC_FL;
  67. return ubifs_flags;
  68. }
  69. /*
  70. * ubifs2ioctl - convert UBIFS inode flags to ioctl inode flags.
  71. * @ubifs_flags: flags to convert
  72. *
  73. * This function convert UBIFS (@UBIFS_COMPR_FL, etc) to ioctl flags
  74. * (@FS_COMPR_FL, etc).
  75. */
  76. static int ubifs2ioctl(int ubifs_flags)
  77. {
  78. int ioctl_flags = 0;
  79. if (ubifs_flags & UBIFS_COMPR_FL)
  80. ioctl_flags |= FS_COMPR_FL;
  81. if (ubifs_flags & UBIFS_SYNC_FL)
  82. ioctl_flags |= FS_SYNC_FL;
  83. if (ubifs_flags & UBIFS_APPEND_FL)
  84. ioctl_flags |= FS_APPEND_FL;
  85. if (ubifs_flags & UBIFS_IMMUTABLE_FL)
  86. ioctl_flags |= FS_IMMUTABLE_FL;
  87. if (ubifs_flags & UBIFS_DIRSYNC_FL)
  88. ioctl_flags |= FS_DIRSYNC_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 = ui->data_len };
  98. err = ubifs_budget_space(c, &req);
  99. if (err)
  100. return err;
  101. /*
  102. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  103. * the relevant capability.
  104. */
  105. mutex_lock(&ui->ui_mutex);
  106. oldflags = ubifs2ioctl(ui->flags);
  107. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  108. if (!capable(CAP_LINUX_IMMUTABLE)) {
  109. err = -EPERM;
  110. goto out_unlock;
  111. }
  112. }
  113. ui->flags = ioctl2ubifs(flags);
  114. ubifs_set_inode_flags(inode);
  115. inode->i_ctime = ubifs_current_time(inode);
  116. release = ui->dirty;
  117. mark_inode_dirty_sync(inode);
  118. mutex_unlock(&ui->ui_mutex);
  119. if (release)
  120. ubifs_release_budget(c, &req);
  121. if (IS_SYNC(inode))
  122. err = write_inode_now(inode, 1);
  123. return err;
  124. out_unlock:
  125. ubifs_err(c, "can't modify inode %lu attributes", inode->i_ino);
  126. mutex_unlock(&ui->ui_mutex);
  127. ubifs_release_budget(c, &req);
  128. return err;
  129. }
  130. long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  131. {
  132. int flags, err;
  133. struct inode *inode = file_inode(file);
  134. switch (cmd) {
  135. case FS_IOC_GETFLAGS:
  136. flags = ubifs2ioctl(ubifs_inode(inode)->flags);
  137. dbg_gen("get flags: %#x, i_flags %#x", flags, inode->i_flags);
  138. return put_user(flags, (int __user *) arg);
  139. case FS_IOC_SETFLAGS: {
  140. if (IS_RDONLY(inode))
  141. return -EROFS;
  142. if (!inode_owner_or_capable(inode))
  143. return -EACCES;
  144. if (get_user(flags, (int __user *) arg))
  145. return -EFAULT;
  146. if (!S_ISDIR(inode->i_mode))
  147. flags &= ~FS_DIRSYNC_FL;
  148. /*
  149. * Make sure the file-system is read-write and make sure it
  150. * will not become read-only while we are changing the flags.
  151. */
  152. err = mnt_want_write_file(file);
  153. if (err)
  154. return err;
  155. dbg_gen("set flags: %#x, i_flags %#x", flags, inode->i_flags);
  156. err = setflags(inode, flags);
  157. mnt_drop_write_file(file);
  158. return err;
  159. }
  160. default:
  161. return -ENOTTY;
  162. }
  163. }
  164. #ifdef CONFIG_COMPAT
  165. long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  166. {
  167. switch (cmd) {
  168. case FS_IOC32_GETFLAGS:
  169. cmd = FS_IOC_GETFLAGS;
  170. break;
  171. case FS_IOC32_SETFLAGS:
  172. cmd = FS_IOC_SETFLAGS;
  173. break;
  174. default:
  175. return -ENOIOCTLCMD;
  176. }
  177. return ubifs_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  178. }
  179. #endif