ioctl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * linux/fs/hfsplus/ioctl.c
  3. *
  4. * Copyright (C) 2003
  5. * Ethan Benson <erbenson@alaska.net>
  6. * partially derived from linux/fs/ext2/ioctl.c
  7. * Copyright (C) 1993, 1994, 1995
  8. * Remy Card (card@masi.ibp.fr)
  9. * Laboratoire MASI - Institut Blaise Pascal
  10. * Universite Pierre et Marie Curie (Paris VI)
  11. *
  12. * hfsplus ioctls
  13. */
  14. #include <linux/capability.h>
  15. #include <linux/fs.h>
  16. #include <linux/mount.h>
  17. #include <linux/sched.h>
  18. #include <asm/uaccess.h>
  19. #include "hfsplus_fs.h"
  20. /*
  21. * "Blessing" an HFS+ filesystem writes metadata to the superblock informing
  22. * the platform firmware which file to boot from
  23. */
  24. static int hfsplus_ioctl_bless(struct file *file, int __user *user_flags)
  25. {
  26. struct dentry *dentry = file->f_path.dentry;
  27. struct inode *inode = d_inode(dentry);
  28. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  29. struct hfsplus_vh *vh = sbi->s_vhdr;
  30. struct hfsplus_vh *bvh = sbi->s_backup_vhdr;
  31. u32 cnid = (unsigned long)dentry->d_fsdata;
  32. if (!capable(CAP_SYS_ADMIN))
  33. return -EPERM;
  34. mutex_lock(&sbi->vh_mutex);
  35. /* Directory containing the bootable system */
  36. vh->finder_info[0] = bvh->finder_info[0] =
  37. cpu_to_be32(parent_ino(dentry));
  38. /*
  39. * Bootloader. Just using the inode here breaks in the case of
  40. * hard links - the firmware wants the ID of the hard link file,
  41. * but the inode points at the indirect inode
  42. */
  43. vh->finder_info[1] = bvh->finder_info[1] = cpu_to_be32(cnid);
  44. /* Per spec, the OS X system folder - same as finder_info[0] here */
  45. vh->finder_info[5] = bvh->finder_info[5] =
  46. cpu_to_be32(parent_ino(dentry));
  47. mutex_unlock(&sbi->vh_mutex);
  48. return 0;
  49. }
  50. static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
  51. {
  52. struct inode *inode = file_inode(file);
  53. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  54. unsigned int flags = 0;
  55. if (inode->i_flags & S_IMMUTABLE)
  56. flags |= FS_IMMUTABLE_FL;
  57. if (inode->i_flags & S_APPEND)
  58. flags |= FS_APPEND_FL;
  59. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  60. flags |= FS_NODUMP_FL;
  61. return put_user(flags, user_flags);
  62. }
  63. static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
  64. {
  65. struct inode *inode = file_inode(file);
  66. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  67. unsigned int flags, new_fl = 0;
  68. int err = 0;
  69. err = mnt_want_write_file(file);
  70. if (err)
  71. goto out;
  72. if (!inode_owner_or_capable(inode)) {
  73. err = -EACCES;
  74. goto out_drop_write;
  75. }
  76. if (get_user(flags, user_flags)) {
  77. err = -EFAULT;
  78. goto out_drop_write;
  79. }
  80. mutex_lock(&inode->i_mutex);
  81. if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
  82. inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  83. if (!capable(CAP_LINUX_IMMUTABLE)) {
  84. err = -EPERM;
  85. goto out_unlock_inode;
  86. }
  87. }
  88. /* don't silently ignore unsupported ext2 flags */
  89. if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
  90. err = -EOPNOTSUPP;
  91. goto out_unlock_inode;
  92. }
  93. if (flags & FS_IMMUTABLE_FL)
  94. new_fl |= S_IMMUTABLE;
  95. if (flags & FS_APPEND_FL)
  96. new_fl |= S_APPEND;
  97. inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
  98. if (flags & FS_NODUMP_FL)
  99. hip->userflags |= HFSPLUS_FLG_NODUMP;
  100. else
  101. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  102. inode->i_ctime = CURRENT_TIME_SEC;
  103. mark_inode_dirty(inode);
  104. out_unlock_inode:
  105. mutex_unlock(&inode->i_mutex);
  106. out_drop_write:
  107. mnt_drop_write_file(file);
  108. out:
  109. return err;
  110. }
  111. long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  112. {
  113. void __user *argp = (void __user *)arg;
  114. switch (cmd) {
  115. case HFSPLUS_IOC_EXT2_GETFLAGS:
  116. return hfsplus_ioctl_getflags(file, argp);
  117. case HFSPLUS_IOC_EXT2_SETFLAGS:
  118. return hfsplus_ioctl_setflags(file, argp);
  119. case HFSPLUS_IOC_BLESS:
  120. return hfsplus_ioctl_bless(file, argp);
  121. default:
  122. return -ENOTTY;
  123. }
  124. }