ioctl.c 4.8 KB

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