ioctl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 = dentry->d_inode;
  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;
  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. inode->i_flags |= S_IMMUTABLE;
  95. else
  96. inode->i_flags &= ~S_IMMUTABLE;
  97. if (flags & FS_APPEND_FL)
  98. inode->i_flags |= S_APPEND;
  99. else
  100. inode->i_flags &= ~S_APPEND;
  101. if (flags & FS_NODUMP_FL)
  102. hip->userflags |= HFSPLUS_FLG_NODUMP;
  103. else
  104. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  105. inode->i_ctime = CURRENT_TIME_SEC;
  106. mark_inode_dirty(inode);
  107. out_unlock_inode:
  108. mutex_unlock(&inode->i_mutex);
  109. out_drop_write:
  110. mnt_drop_write_file(file);
  111. out:
  112. return err;
  113. }
  114. long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  115. {
  116. void __user *argp = (void __user *)arg;
  117. switch (cmd) {
  118. case HFSPLUS_IOC_EXT2_GETFLAGS:
  119. return hfsplus_ioctl_getflags(file, argp);
  120. case HFSPLUS_IOC_EXT2_SETFLAGS:
  121. return hfsplus_ioctl_setflags(file, argp);
  122. case HFSPLUS_IOC_BLESS:
  123. return hfsplus_ioctl_bless(file, argp);
  124. default:
  125. return -ENOTTY;
  126. }
  127. }