ioctl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 |= FS_IMMUTABLE_FL; /* EXT2_IMMUTABLE_FL */
  29. if (HFSPLUS_I(inode).rootflags & HFSPLUS_FLG_APPEND)
  30. flags |= FS_APPEND_FL; /* EXT2_APPEND_FL */
  31. if (HFSPLUS_I(inode).userflags & HFSPLUS_FLG_NODUMP)
  32. flags |= FS_NODUMP_FL; /* 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 & (FS_IMMUTABLE_FL|FS_APPEND_FL) ||
  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 & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL))
  48. return -EOPNOTSUPP;
  49. if (flags & FS_IMMUTABLE_FL) { /* EXT2_IMMUTABLE_FL */
  50. inode->i_flags |= S_IMMUTABLE;
  51. HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_IMMUTABLE;
  52. } else {
  53. inode->i_flags &= ~S_IMMUTABLE;
  54. HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
  55. }
  56. if (flags & FS_APPEND_FL) { /* EXT2_APPEND_FL */
  57. inode->i_flags |= S_APPEND;
  58. HFSPLUS_I(inode).rootflags |= HFSPLUS_FLG_APPEND;
  59. } else {
  60. inode->i_flags &= ~S_APPEND;
  61. HFSPLUS_I(inode).rootflags &= ~HFSPLUS_FLG_APPEND;
  62. }
  63. if (flags & FS_NODUMP_FL) /* EXT2_NODUMP_FL */
  64. HFSPLUS_I(inode).userflags |= HFSPLUS_FLG_NODUMP;
  65. else
  66. HFSPLUS_I(inode).userflags &= ~HFSPLUS_FLG_NODUMP;
  67. inode->i_ctime = CURRENT_TIME_SEC;
  68. mark_inode_dirty(inode);
  69. return 0;
  70. }
  71. default:
  72. return -ENOTTY;
  73. }
  74. }
  75. int hfsplus_setxattr(struct dentry *dentry, const char *name,
  76. const void *value, size_t size, int flags)
  77. {
  78. struct inode *inode = dentry->d_inode;
  79. struct hfs_find_data fd;
  80. hfsplus_cat_entry entry;
  81. struct hfsplus_cat_file *file;
  82. int res;
  83. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  84. return -EOPNOTSUPP;
  85. res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
  86. if (res)
  87. return res;
  88. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  89. if (res)
  90. goto out;
  91. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  92. sizeof(struct hfsplus_cat_file));
  93. file = &entry.file;
  94. if (!strcmp(name, "hfs.type")) {
  95. if (size == 4)
  96. memcpy(&file->user_info.fdType, value, 4);
  97. else
  98. res = -ERANGE;
  99. } else if (!strcmp(name, "hfs.creator")) {
  100. if (size == 4)
  101. memcpy(&file->user_info.fdCreator, value, 4);
  102. else
  103. res = -ERANGE;
  104. } else
  105. res = -EOPNOTSUPP;
  106. if (!res)
  107. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  108. sizeof(struct hfsplus_cat_file));
  109. out:
  110. hfs_find_exit(&fd);
  111. return res;
  112. }
  113. ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
  114. void *value, size_t size)
  115. {
  116. struct inode *inode = dentry->d_inode;
  117. struct hfs_find_data fd;
  118. hfsplus_cat_entry entry;
  119. struct hfsplus_cat_file *file;
  120. ssize_t res = 0;
  121. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  122. return -EOPNOTSUPP;
  123. if (size) {
  124. res = hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
  125. if (res)
  126. return res;
  127. res = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
  128. if (res)
  129. goto out;
  130. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  131. sizeof(struct hfsplus_cat_file));
  132. }
  133. file = &entry.file;
  134. if (!strcmp(name, "hfs.type")) {
  135. if (size >= 4) {
  136. memcpy(value, &file->user_info.fdType, 4);
  137. res = 4;
  138. } else
  139. res = size ? -ERANGE : 4;
  140. } else if (!strcmp(name, "hfs.creator")) {
  141. if (size >= 4) {
  142. memcpy(value, &file->user_info.fdCreator, 4);
  143. res = 4;
  144. } else
  145. res = size ? -ERANGE : 4;
  146. } else
  147. res = -ENODATA;
  148. out:
  149. if (size)
  150. hfs_find_exit(&fd);
  151. return res;
  152. }
  153. #define HFSPLUS_ATTRLIST_SIZE (sizeof("hfs.creator")+sizeof("hfs.type"))
  154. ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. if (!S_ISREG(inode->i_mode) || HFSPLUS_IS_RSRC(inode))
  158. return -EOPNOTSUPP;
  159. if (!buffer || !size)
  160. return HFSPLUS_ATTRLIST_SIZE;
  161. if (size < HFSPLUS_ATTRLIST_SIZE)
  162. return -ERANGE;
  163. strcpy(buffer, "hfs.type");
  164. strcpy(buffer + sizeof("hfs.type"), "hfs.creator");
  165. return HFSPLUS_ATTRLIST_SIZE;
  166. }