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