ioctl.c 5.1 KB

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