ioctl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <linux/xattr.h>
  19. #include <asm/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 = dentry->d_inode;
  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->f_path.dentry->d_inode;
  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->f_path.dentry->d_inode;
  67. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  68. unsigned int flags;
  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. mutex_lock(&inode->i_mutex);
  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. inode->i_flags |= S_IMMUTABLE;
  96. else
  97. inode->i_flags &= ~S_IMMUTABLE;
  98. if (flags & FS_APPEND_FL)
  99. inode->i_flags |= S_APPEND;
  100. else
  101. inode->i_flags &= ~S_APPEND;
  102. if (flags & FS_NODUMP_FL)
  103. hip->userflags |= HFSPLUS_FLG_NODUMP;
  104. else
  105. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  106. inode->i_ctime = CURRENT_TIME_SEC;
  107. mark_inode_dirty(inode);
  108. out_unlock_inode:
  109. mutex_unlock(&inode->i_mutex);
  110. out_drop_write:
  111. mnt_drop_write_file(file);
  112. out:
  113. return err;
  114. }
  115. long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  116. {
  117. void __user *argp = (void __user *)arg;
  118. switch (cmd) {
  119. case HFSPLUS_IOC_EXT2_GETFLAGS:
  120. return hfsplus_ioctl_getflags(file, argp);
  121. case HFSPLUS_IOC_EXT2_SETFLAGS:
  122. return hfsplus_ioctl_setflags(file, argp);
  123. case HFSPLUS_IOC_BLESS:
  124. return hfsplus_ioctl_bless(file, argp);
  125. default:
  126. return -ENOTTY;
  127. }
  128. }
  129. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  130. const void *value, size_t size, int flags)
  131. {
  132. struct inode *inode = dentry->d_inode;
  133. struct hfs_find_data fd;
  134. hfsplus_cat_entry entry;
  135. struct hfsplus_cat_file *file;
  136. int res;
  137. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  138. return -EOPNOTSUPP;
  139. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  140. if (res)
  141. return res;
  142. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  143. if (res)
  144. goto out;
  145. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  146. sizeof(struct hfsplus_cat_file));
  147. file = &entry.file;
  148. if (!strcmp(name, "hfs.type")) {
  149. if (size == 4)
  150. memcpy(&file->user_info.fdType, value, 4);
  151. else
  152. res = -ERANGE;
  153. } else if (!strcmp(name, "hfs.creator")) {
  154. if (size == 4)
  155. memcpy(&file->user_info.fdCreator, value, 4);
  156. else
  157. res = -ERANGE;
  158. } else
  159. res = -EOPNOTSUPP;
  160. if (!res) {
  161. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  162. sizeof(struct hfsplus_cat_file));
  163. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  164. }
  165. out:
  166. hfs_find_exit(&fd);
  167. return res;
  168. }
  169. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  170. void *value, size_t size)
  171. {
  172. struct inode *inode = dentry->d_inode;
  173. struct hfs_find_data fd;
  174. hfsplus_cat_entry entry;
  175. struct hfsplus_cat_file *file;
  176. ssize_t res = 0;
  177. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  178. return -EOPNOTSUPP;
  179. if (size) {
  180. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  181. if (res)
  182. return res;
  183. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  184. if (res)
  185. goto out;
  186. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  187. sizeof(struct hfsplus_cat_file));
  188. }
  189. file = &entry.file;
  190. if (!strcmp(name, "hfs.type")) {
  191. if (size >= 4) {
  192. memcpy(value, &file->user_info.fdType, 4);
  193. res = 4;
  194. } else
  195. res = size ? -ERANGE : 4;
  196. } else if (!strcmp(name, "hfs.creator")) {
  197. if (size >= 4) {
  198. memcpy(value, &file->user_info.fdCreator, 4);
  199. res = 4;
  200. } else
  201. res = size ? -ERANGE : 4;
  202. } else
  203. res = -EOPNOTSUPP;
  204. out:
  205. if (size)
  206. hfs_find_exit(&fd);
  207. return res;
  208. }
  209. #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
  210. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  211. {
  212. struct inode *inode = dentry->d_inode;
  213. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  214. return -EOPNOTSUPP;
  215. if (!buffer || !size)
  216. return HFSPLUS_ATTRLIST_SIZE;
  217. if (size < HFSPLUS_ATTRLIST_SIZE)
  218. return -ERANGE;
  219. strcpy(buffer, "hfs.type");
  220. strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
  221. return HFSPLUS_ATTRLIST_SIZE;
  222. }