ioctl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
  22. {
  23. struct inode *inode = file->f_path.dentry->d_inode;
  24. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  25. unsigned int flags = 0;
  26. if (inode->i_flags & S_IMMUTABLE)
  27. flags |= FS_IMMUTABLE_FL;
  28. if (inode->i_flags & S_APPEND)
  29. flags |= FS_APPEND_FL;
  30. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  31. flags |= FS_NODUMP_FL;
  32. return put_user(flags, user_flags);
  33. }
  34. static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
  35. {
  36. struct inode *inode = file->f_path.dentry->d_inode;
  37. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  38. unsigned int flags;
  39. int err = 0;
  40. err = mnt_want_write(file->f_path.mnt);
  41. if (err)
  42. goto out;
  43. if (!inode_owner_or_capable(inode)) {
  44. err = -EACCES;
  45. goto out_drop_write;
  46. }
  47. if (get_user(flags, user_flags)) {
  48. err = -EFAULT;
  49. goto out_drop_write;
  50. }
  51. mutex_lock(&inode->i_mutex);
  52. if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
  53. inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  54. if (!capable(CAP_LINUX_IMMUTABLE)) {
  55. err = -EPERM;
  56. goto out_unlock_inode;
  57. }
  58. }
  59. /* don't silently ignore unsupported ext2 flags */
  60. if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
  61. err = -EOPNOTSUPP;
  62. goto out_unlock_inode;
  63. }
  64. if (flags & FS_IMMUTABLE_FL)
  65. inode->i_flags |= S_IMMUTABLE;
  66. else
  67. inode->i_flags &= ~S_IMMUTABLE;
  68. if (flags & FS_APPEND_FL)
  69. inode->i_flags |= S_APPEND;
  70. else
  71. inode->i_flags &= ~S_APPEND;
  72. if (flags & FS_NODUMP_FL)
  73. hip->userflags |= HFSPLUS_FLG_NODUMP;
  74. else
  75. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  76. inode->i_ctime = CURRENT_TIME_SEC;
  77. mark_inode_dirty(inode);
  78. out_unlock_inode:
  79. mutex_unlock(&inode->i_mutex);
  80. out_drop_write:
  81. mnt_drop_write(file->f_path.mnt);
  82. out:
  83. return err;
  84. }
  85. long hfsplus_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  86. {
  87. void __user *argp = (void __user *)arg;
  88. switch (cmd) {
  89. case HFSPLUS_IOC_EXT2_GETFLAGS:
  90. return hfsplus_ioctl_getflags(file, argp);
  91. case HFSPLUS_IOC_EXT2_SETFLAGS:
  92. return hfsplus_ioctl_setflags(file, argp);
  93. default:
  94. return -ENOTTY;
  95. }
  96. }
  97. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  98. const void *value, size_t size, int flags)
  99. {
  100. struct inode *inode = dentry->d_inode;
  101. struct hfs_find_data fd;
  102. hfsplus_cat_entry entry;
  103. struct hfsplus_cat_file *file;
  104. int res;
  105. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  106. return -EOPNOTSUPP;
  107. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  108. if (res)
  109. return res;
  110. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  111. if (res)
  112. goto out;
  113. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  114. sizeof(struct hfsplus_cat_file));
  115. file = &entry.file;
  116. if (!strcmp(name, "hfs.type")) {
  117. if (size == 4)
  118. memcpy(&file->user_info.fdType, value, 4);
  119. else
  120. res = -ERANGE;
  121. } else if (!strcmp(name, "hfs.creator")) {
  122. if (size == 4)
  123. memcpy(&file->user_info.fdCreator, value, 4);
  124. else
  125. res = -ERANGE;
  126. } else
  127. res = -EOPNOTSUPP;
  128. if (!res) {
  129. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  130. sizeof(struct hfsplus_cat_file));
  131. hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
  132. }
  133. out:
  134. hfs_find_exit(&fd);
  135. return res;
  136. }
  137. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  138. void *value, size_t size)
  139. {
  140. struct inode *inode = dentry->d_inode;
  141. struct hfs_find_data fd;
  142. hfsplus_cat_entry entry;
  143. struct hfsplus_cat_file *file;
  144. ssize_t res = 0;
  145. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  146. return -EOPNOTSUPP;
  147. if (size) {
  148. res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
  149. if (res)
  150. return res;
  151. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  152. if (res)
  153. goto out;
  154. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  155. sizeof(struct hfsplus_cat_file));
  156. }
  157. file = &entry.file;
  158. if (!strcmp(name, "hfs.type")) {
  159. if (size >= 4) {
  160. memcpy(value, &file->user_info.fdType, 4);
  161. res = 4;
  162. } else
  163. res = size ? -ERANGE : 4;
  164. } else if (!strcmp(name, "hfs.creator")) {
  165. if (size >= 4) {
  166. memcpy(value, &file->user_info.fdCreator, 4);
  167. res = 4;
  168. } else
  169. res = size ? -ERANGE : 4;
  170. } else
  171. res = -EOPNOTSUPP;
  172. out:
  173. if (size)
  174. hfs_find_exit(&fd);
  175. return res;
  176. }
  177. #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
  178. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  179. {
  180. struct inode *inode = dentry->d_inode;
  181. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  182. return -EOPNOTSUPP;
  183. if (!buffer || !size)
  184. return HFSPLUS_ATTRLIST_SIZE;
  185. if (size < HFSPLUS_ATTRLIST_SIZE)
  186. return -ERANGE;
  187. strcpy(buffer, "hfs.type");
  188. strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
  189. return HFSPLUS_ATTRLIST_SIZE;
  190. }