xattr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * fs/cifs/xattr.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2003
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/posix_acl_xattr.h>
  23. #include "cifsfs.h"
  24. #include "cifspdu.h"
  25. #include "cifsglob.h"
  26. #include "cifsproto.h"
  27. #include "cifs_debug.h"
  28. #define MAX_EA_VALUE_SIZE 65535
  29. #define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
  30. #define CIFS_XATTR_USER_PREFIX "user."
  31. #define CIFS_XATTR_SYSTEM_PREFIX "system."
  32. #define CIFS_XATTR_OS2_PREFIX "os2."
  33. #define CIFS_XATTR_SECURITY_PREFIX ".security"
  34. #define CIFS_XATTR_TRUSTED_PREFIX "trusted."
  35. #define XATTR_TRUSTED_PREFIX_LEN 8
  36. #define XATTR_SECURITY_PREFIX_LEN 9
  37. /* BB need to add server (Samba e.g) support for security and trusted prefix */
  38. int cifs_removexattr(struct dentry * direntry, const char * ea_name)
  39. {
  40. int rc = -EOPNOTSUPP;
  41. #ifdef CONFIG_CIFS_XATTR
  42. int xid;
  43. struct cifs_sb_info *cifs_sb;
  44. struct cifsTconInfo *pTcon;
  45. struct super_block * sb;
  46. char * full_path;
  47. if(direntry == NULL)
  48. return -EIO;
  49. if(direntry->d_inode == NULL)
  50. return -EIO;
  51. sb = direntry->d_inode->i_sb;
  52. if(sb == NULL)
  53. return -EIO;
  54. xid = GetXid();
  55. cifs_sb = CIFS_SB(sb);
  56. pTcon = cifs_sb->tcon;
  57. down(&sb->s_vfs_rename_sem);
  58. full_path = build_path_from_dentry(direntry);
  59. up(&sb->s_vfs_rename_sem);
  60. if(full_path == NULL) {
  61. FreeXid(xid);
  62. return -ENOMEM;
  63. }
  64. if(ea_name == NULL) {
  65. cFYI(1,("Null xattr names not supported"));
  66. } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5)
  67. && (strncmp(ea_name,CIFS_XATTR_OS2_PREFIX,4))) {
  68. cFYI(1,("illegal xattr namespace %s (only user namespace supported)",ea_name));
  69. /* BB what if no namespace prefix? */
  70. /* Should we just pass them to server, except for
  71. system and perhaps security prefixes? */
  72. } else {
  73. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  74. goto remove_ea_exit;
  75. ea_name+=5; /* skip past user. prefix */
  76. rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,NULL,
  77. (__u16)0, cifs_sb->local_nls);
  78. }
  79. remove_ea_exit:
  80. if (full_path)
  81. kfree(full_path);
  82. FreeXid(xid);
  83. #endif
  84. return rc;
  85. }
  86. int cifs_setxattr(struct dentry * direntry, const char * ea_name,
  87. const void * ea_value, size_t value_size, int flags)
  88. {
  89. int rc = -EOPNOTSUPP;
  90. #ifdef CONFIG_CIFS_XATTR
  91. int xid;
  92. struct cifs_sb_info *cifs_sb;
  93. struct cifsTconInfo *pTcon;
  94. struct super_block * sb;
  95. char * full_path;
  96. if(direntry == NULL)
  97. return -EIO;
  98. if(direntry->d_inode == NULL)
  99. return -EIO;
  100. sb = direntry->d_inode->i_sb;
  101. if(sb == NULL)
  102. return -EIO;
  103. xid = GetXid();
  104. cifs_sb = CIFS_SB(sb);
  105. pTcon = cifs_sb->tcon;
  106. down(&sb->s_vfs_rename_sem);
  107. full_path = build_path_from_dentry(direntry);
  108. up(&sb->s_vfs_rename_sem);
  109. if(full_path == NULL) {
  110. FreeXid(xid);
  111. return -ENOMEM;
  112. }
  113. /* return dos attributes as pseudo xattr */
  114. /* return alt name if available as pseudo attr */
  115. /* if proc/fs/cifs/streamstoxattr is set then
  116. search server for EAs or streams to
  117. returns as xattrs */
  118. if(value_size > MAX_EA_VALUE_SIZE) {
  119. cFYI(1,("size of EA value too large"));
  120. if(full_path)
  121. kfree(full_path);
  122. FreeXid(xid);
  123. return -EOPNOTSUPP;
  124. }
  125. if(ea_name == NULL) {
  126. cFYI(1,("Null xattr names not supported"));
  127. } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
  128. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  129. goto set_ea_exit;
  130. if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
  131. cFYI(1,("attempt to set cifs inode metadata"));
  132. }
  133. ea_name += 5; /* skip past user. prefix */
  134. rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
  135. (__u16)value_size, cifs_sb->local_nls);
  136. } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
  137. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  138. goto set_ea_exit;
  139. ea_name += 4; /* skip past os2. prefix */
  140. rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
  141. (__u16)value_size, cifs_sb->local_nls);
  142. } else {
  143. int temp;
  144. temp = strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,
  145. strlen(POSIX_ACL_XATTR_ACCESS));
  146. if (temp == 0) {
  147. #ifdef CONFIG_CIFS_POSIX
  148. rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,ea_value,
  149. (const int)value_size, ACL_TYPE_ACCESS,
  150. cifs_sb->local_nls);
  151. cFYI(1,("set POSIX ACL rc %d",rc));
  152. #else
  153. cFYI(1,("set POSIX ACL not supported"));
  154. #endif
  155. } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  156. #ifdef CONFIG_CIFS_POSIX
  157. rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,ea_value,
  158. (const int)value_size, ACL_TYPE_DEFAULT,
  159. cifs_sb->local_nls);
  160. cFYI(1,("set POSIX default ACL rc %d",rc));
  161. #else
  162. cFYI(1,("set default POSIX ACL not supported"));
  163. #endif
  164. } else {
  165. cFYI(1,("illegal xattr request %s (only user namespace supported)",ea_name));
  166. /* BB what if no namespace prefix? */
  167. /* Should we just pass them to server, except for
  168. system and perhaps security prefixes? */
  169. }
  170. }
  171. set_ea_exit:
  172. if (full_path)
  173. kfree(full_path);
  174. FreeXid(xid);
  175. #endif
  176. return rc;
  177. }
  178. ssize_t cifs_getxattr(struct dentry * direntry, const char * ea_name,
  179. void * ea_value, size_t buf_size)
  180. {
  181. ssize_t rc = -EOPNOTSUPP;
  182. #ifdef CONFIG_CIFS_XATTR
  183. int xid;
  184. struct cifs_sb_info *cifs_sb;
  185. struct cifsTconInfo *pTcon;
  186. struct super_block * sb;
  187. char * full_path;
  188. if(direntry == NULL)
  189. return -EIO;
  190. if(direntry->d_inode == NULL)
  191. return -EIO;
  192. sb = direntry->d_inode->i_sb;
  193. if(sb == NULL)
  194. return -EIO;
  195. xid = GetXid();
  196. cifs_sb = CIFS_SB(sb);
  197. pTcon = cifs_sb->tcon;
  198. down(&sb->s_vfs_rename_sem);
  199. full_path = build_path_from_dentry(direntry);
  200. up(&sb->s_vfs_rename_sem);
  201. if(full_path == NULL) {
  202. FreeXid(xid);
  203. return -ENOMEM;
  204. }
  205. /* return dos attributes as pseudo xattr */
  206. /* return alt name if available as pseudo attr */
  207. if(ea_name == NULL) {
  208. cFYI(1,("Null xattr names not supported"));
  209. } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
  210. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  211. goto get_ea_exit;
  212. if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
  213. cFYI(1,("attempt to query cifs inode metadata"));
  214. /* revalidate/getattr then populate from inode */
  215. } /* BB add else when above is implemented */
  216. ea_name += 5; /* skip past user. prefix */
  217. rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
  218. buf_size, cifs_sb->local_nls);
  219. } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
  220. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  221. goto get_ea_exit;
  222. ea_name += 4; /* skip past os2. prefix */
  223. rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
  224. buf_size, cifs_sb->local_nls);
  225. } else if(strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
  226. #ifdef CONFIG_CIFS_POSIX
  227. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  228. ea_value, buf_size, ACL_TYPE_ACCESS,
  229. cifs_sb->local_nls);
  230. #else
  231. cFYI(1,("query POSIX ACL not supported yet"));
  232. #endif /* CONFIG_CIFS_POSIX */
  233. } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  234. #ifdef CONFIG_CIFS_POSIX
  235. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  236. ea_value, buf_size, ACL_TYPE_DEFAULT,
  237. cifs_sb->local_nls);
  238. #else
  239. cFYI(1,("query POSIX default ACL not supported yet"));
  240. #endif
  241. } else if(strncmp(ea_name,
  242. CIFS_XATTR_TRUSTED_PREFIX,XATTR_TRUSTED_PREFIX_LEN) == 0) {
  243. cFYI(1,("Trusted xattr namespace not supported yet"));
  244. } else if(strncmp(ea_name,
  245. CIFS_XATTR_SECURITY_PREFIX,XATTR_SECURITY_PREFIX_LEN) == 0) {
  246. cFYI(1,("Security xattr namespace not supported yet"));
  247. } else {
  248. cFYI(1,("illegal xattr name request %s (only user namespace supported)",ea_name));
  249. }
  250. /* We could add an additional check for streams ie
  251. if proc/fs/cifs/streamstoxattr is set then
  252. search server for EAs or streams to
  253. returns as xattrs */
  254. if(rc == -EINVAL)
  255. rc = -EOPNOTSUPP;
  256. get_ea_exit:
  257. if (full_path)
  258. kfree(full_path);
  259. FreeXid(xid);
  260. #endif
  261. return rc;
  262. }
  263. ssize_t cifs_listxattr(struct dentry * direntry, char * data, size_t buf_size)
  264. {
  265. ssize_t rc = -EOPNOTSUPP;
  266. #ifdef CONFIG_CIFS_XATTR
  267. int xid;
  268. struct cifs_sb_info *cifs_sb;
  269. struct cifsTconInfo *pTcon;
  270. struct super_block * sb;
  271. char * full_path;
  272. if(direntry == NULL)
  273. return -EIO;
  274. if(direntry->d_inode == NULL)
  275. return -EIO;
  276. sb = direntry->d_inode->i_sb;
  277. if(sb == NULL)
  278. return -EIO;
  279. xid = GetXid();
  280. cifs_sb = CIFS_SB(sb);
  281. pTcon = cifs_sb->tcon;
  282. down(&sb->s_vfs_rename_sem);
  283. full_path = build_path_from_dentry(direntry);
  284. up(&sb->s_vfs_rename_sem);
  285. if(full_path == NULL) {
  286. FreeXid(xid);
  287. return -ENOMEM;
  288. }
  289. /* return dos attributes as pseudo xattr */
  290. /* return alt name if available as pseudo attr */
  291. /* if proc/fs/cifs/streamstoxattr is set then
  292. search server for EAs or streams to
  293. returns as xattrs */
  294. rc = CIFSSMBQAllEAs(xid,pTcon,full_path,data,buf_size,
  295. cifs_sb->local_nls);
  296. if (full_path)
  297. kfree(full_path);
  298. FreeXid(xid);
  299. #endif
  300. return rc;
  301. }