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. switch (cmd) {
  27. case HFSPLUS_IOC_EXT2_GETFLAGS:
  28. flags = 0;
  29. if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_IMMUTABLE)
  30. flags |= FS_IMMUTABLE_FL; /* EXT2_IMMUTABLE_FL */
  31. if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_APPEND)
  32. flags |= FS_APPEND_FL; /* EXT2_APPEND_FL */
  33. if (HFSPLUS_I(inode).userflags & HFSPLUS_FLG_NODUMP)
  34. flags |= FS_NODUMP_FL; /* EXT2_NODUMP_FL */
  35. return put_user(flags, (int __user *)arg);
  36. case HFSPLUS_IOC_EXT2_SETFLAGS: {
  37. int err = 0;
  38. lock_kernel();
  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. return -ENOTTY;
  91. }
  92. }
  93. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  94. const void *value, size_t size, int flags)
  95. {
  96. struct inode *inode = dentry->d_inode;
  97. struct hfs_find_data fd;
  98. hfsplus_cat_entry entry;
  99. struct hfsplus_cat_file *file;
  100. int res;
  101. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  102. return -EOPNOTSUPP;
  103. res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
  104. if (res)
  105. return res;
  106. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  107. if (res)
  108. goto out;
  109. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  110. sizeof(struct hfsplus_cat_file));
  111. file = &entry.file;
  112. if (!strcmp(name, "hfs.type")) {
  113. if (size == 4)
  114. memcpy(&file->user_info.fdType, value, 4);
  115. else
  116. res = -ERANGE;
  117. } else if (!strcmp(name, "hfs.creator")) {
  118. if (size == 4)
  119. memcpy(&file->user_info.fdCreator, value, 4);
  120. else
  121. res = -ERANGE;
  122. } else
  123. res = -EOPNOTSUPP;
  124. if (!res)
  125. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  126. sizeof(struct hfsplus_cat_file));
  127. out:
  128. hfs_find_exit(&fd);
  129. return res;
  130. }
  131. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  132. void *value, size_t size)
  133. {
  134. struct inode *inode = dentry->d_inode;
  135. struct hfs_find_data fd;
  136. hfsplus_cat_entry entry;
  137. struct hfsplus_cat_file *file;
  138. ssize_t res = 0;
  139. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  140. return -EOPNOTSUPP;
  141. if (size) {
  142. res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
  143. if (res)
  144. return res;
  145. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  146. if (res)
  147. goto out;
  148. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  149. sizeof(struct hfsplus_cat_file));
  150. }
  151. file = &entry.file;
  152. if (!strcmp(name, "hfs.type")) {
  153. if (size >= 4) {
  154. memcpy(value, &file->user_info.fdType, 4);
  155. res = 4;
  156. } else
  157. res = size ? -ERANGE : 4;
  158. } else if (!strcmp(name, "hfs.creator")) {
  159. if (size >= 4) {
  160. memcpy(value, &file->user_info.fdCreator, 4);
  161. res = 4;
  162. } else
  163. res = size ? -ERANGE : 4;
  164. } else
  165. res = -ENODATA;
  166. out:
  167. if (size)
  168. hfs_find_exit(&fd);
  169. return res;
  170. }
  171. #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
  172. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  173. {
  174. struct inode *inode = dentry->d_inode;
  175. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  176. return -EOPNOTSUPP;
  177. if (!buffer || !size)
  178. return HFSPLUS_ATTRLIST_SIZE;
  179. if (size < HFSPLUS_ATTRLIST_SIZE)
  180. return -ERANGE;
  181. strcpy(buffer, "hfs.type");
  182. strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
  183. return HFSPLUS_ATTRLIST_SIZE;
  184. }