ioctl.c 3.6 KB

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