ioctl.c 5.2 KB

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