ioctl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. 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. /* Bootloader */
  39. vh->finder_info[1] = bvh->finder_info[1] = cpu_to_be32(inode->i_ino);
  40. /* Per spec, the OS X system folder - same as finder_info[0] here */
  41. vh->finder_info[5] = bvh->finder_info[5] =
  42. cpu_to_be32(parent_ino(dentry));
  43. mutex_unlock(&sbi->vh_mutex);
  44. return 0;
  45. }
  46. static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
  47. {
  48. struct inode *inode = file->f_path.dentry->d_inode;
  49. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  50. unsigned int flags = 0;
  51. if (inode->i_flags & S_IMMUTABLE)
  52. flags |= FS_IMMUTABLE_FL;
  53. if (inode->i_flags & S_APPEND)
  54. flags |= FS_APPEND_FL;
  55. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  56. flags |= FS_NODUMP_FL;
  57. return put_user(flags, user_flags);
  58. }
  59. static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
  60. {
  61. struct inode *inode = file->f_path.dentry->d_inode;
  62. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  63. unsigned int flags;
  64. int err = 0;
  65. err = mnt_want_write_file(file);
  66. if (err)
  67. goto out;
  68. if (!inode_owner_or_capable(inode)) {
  69. err = -EACCES;
  70. goto out_drop_write;
  71. }
  72. if (get_user(flags, user_flags)) {
  73. err = -EFAULT;
  74. goto out_drop_write;
  75. }
  76. mutex_lock(&inode->i_mutex);
  77. if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
  78. inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  79. if (!capable(CAP_LINUX_IMMUTABLE)) {
  80. err = -EPERM;
  81. goto out_unlock_inode;
  82. }
  83. }
  84. /* don't silently ignore unsupported ext2 flags */
  85. if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
  86. err = -EOPNOTSUPP;
  87. goto out_unlock_inode;
  88. }
  89. if (flags & FS_IMMUTABLE_FL)
  90. inode->i_flags |= S_IMMUTABLE;
  91. else
  92. inode->i_flags &= ~S_IMMUTABLE;
  93. if (flags & FS_APPEND_FL)
  94. inode->i_flags |= S_APPEND;
  95. else
  96. inode->i_flags &= ~S_APPEND;
  97. if (flags & FS_NODUMP_FL)
  98. hip->userflags |= HFSPLUS_FLG_NODUMP;
  99. else
  100. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  101. inode->i_ctime = CURRENT_TIME_SEC;
  102. mark_inode_dirty(inode);
  103. out_unlock_inode:
  104. mutex_unlock(&inode->i_mutex);
  105. out_drop_write:
  106. mnt_drop_write_file(file);
  107. out:
  108. return err;
  109. }
  110. long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  111. {
  112. void __user *argp = (void __user *)arg;
  113. switch (cmd) {
  114. case HFSPLUS_IOC_EXT2_GETFLAGS:
  115. return hfsplus_ioctl_getflags(file, argp);
  116. case HFSPLUS_IOC_EXT2_SETFLAGS:
  117. return hfsplus_ioctl_setflags(file, argp);
  118. case HFSPLUS_IOC_BLESS:
  119. return hfsplus_ioctl_bless(file, argp);
  120. default:
  121. return -ENOTTY;
  122. }
  123. }
  124. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  125. const void *value, size_t size, int flags)
  126. {
  127. struct inode *inode = dentry->d_inode;
  128. struct hfs_find_data fd;
  129. hfsplus_cat_entry entry;
  130. struct hfsplus_cat_file *file;
  131. int res;
  132. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  133. return -EOPNOTSUPP;
  134. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  135. if (res)
  136. return res;
  137. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  138. if (res)
  139. goto out;
  140. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  141. sizeof(struct hfsplus_cat_file));
  142. file = &entry.file;
  143. if (!strcmp(name, "hfs.type")) {
  144. if (size == 4)
  145. memcpy(&file->user_info.fdType, value, 4);
  146. else
  147. res = -ERANGE;
  148. } else if (!strcmp(name, "hfs.creator")) {
  149. if (size == 4)
  150. memcpy(&file->user_info.fdCreator, value, 4);
  151. else
  152. res = -ERANGE;
  153. } else
  154. res = -EOPNOTSUPP;
  155. if (!res) {
  156. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  157. sizeof(struct hfsplus_cat_file));
  158. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  159. }
  160. out:
  161. hfs_find_exit(&fd);
  162. return res;
  163. }
  164. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  165. void *value, size_t size)
  166. {
  167. struct inode *inode = dentry->d_inode;
  168. struct hfs_find_data fd;
  169. hfsplus_cat_entry entry;
  170. struct hfsplus_cat_file *file;
  171. ssize_t res = 0;
  172. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  173. return -EOPNOTSUPP;
  174. if (size) {
  175. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  176. if (res)
  177. return res;
  178. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  179. if (res)
  180. goto out;
  181. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  182. sizeof(struct hfsplus_cat_file));
  183. }
  184. file = &entry.file;
  185. if (!strcmp(name, "hfs.type")) {
  186. if (size >= 4) {
  187. memcpy(value, &file->user_info.fdType, 4);
  188. res = 4;
  189. } else
  190. res = size ? -ERANGE : 4;
  191. } else if (!strcmp(name, "hfs.creator")) {
  192. if (size >= 4) {
  193. memcpy(value, &file->user_info.fdCreator, 4);
  194. res = 4;
  195. } else
  196. res = size ? -ERANGE : 4;
  197. } else
  198. res = -EOPNOTSUPP;
  199. out:
  200. if (size)
  201. hfs_find_exit(&fd);
  202. return res;
  203. }
  204. #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
  205. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  206. {
  207. struct inode *inode = dentry->d_inode;
  208. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  209. return -EOPNOTSUPP;
  210. if (!buffer || !size)
  211. return HFSPLUS_ATTRLIST_SIZE;
  212. if (size < HFSPLUS_ATTRLIST_SIZE)
  213. return -ERANGE;
  214. strcpy(buffer, "hfs.type");
  215. strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
  216. return HFSPLUS_ATTRLIST_SIZE;
  217. }