xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. mutex_lock(&sb->s_vfs_rename_mutex);
  58. full_path = build_path_from_dentry(direntry);
  59. mutex_unlock(&sb->s_vfs_rename_mutex);
  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. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  79. }
  80. remove_ea_exit:
  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. mutex_lock(&sb->s_vfs_rename_mutex);
  107. full_path = build_path_from_dentry(direntry);
  108. mutex_unlock(&sb->s_vfs_rename_mutex);
  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. kfree(full_path);
  121. FreeXid(xid);
  122. return -EOPNOTSUPP;
  123. }
  124. if(ea_name == NULL) {
  125. cFYI(1,("Null xattr names not supported"));
  126. } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
  127. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  128. goto set_ea_exit;
  129. if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
  130. cFYI(1,("attempt to set cifs inode metadata"));
  131. }
  132. ea_name += 5; /* skip past user. prefix */
  133. rc = CIFSSMBSetEA(xid,pTcon,full_path,ea_name,ea_value,
  134. (__u16)value_size, cifs_sb->local_nls,
  135. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  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. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  143. } else {
  144. int temp;
  145. temp = strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,
  146. strlen(POSIX_ACL_XATTR_ACCESS));
  147. if (temp == 0) {
  148. #ifdef CONFIG_CIFS_POSIX
  149. if(sb->s_flags & MS_POSIXACL)
  150. rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,
  151. ea_value, (const int)value_size,
  152. ACL_TYPE_ACCESS,cifs_sb->local_nls,
  153. cifs_sb->mnt_cifs_flags &
  154. CIFS_MOUNT_MAP_SPECIAL_CHR);
  155. cFYI(1,("set POSIX ACL rc %d",rc));
  156. #else
  157. cFYI(1,("set POSIX ACL not supported"));
  158. #endif
  159. } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  160. #ifdef CONFIG_CIFS_POSIX
  161. if(sb->s_flags & MS_POSIXACL)
  162. rc = CIFSSMBSetPosixACL(xid, pTcon,full_path,
  163. ea_value, (const int)value_size,
  164. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  165. cifs_sb->mnt_cifs_flags &
  166. CIFS_MOUNT_MAP_SPECIAL_CHR);
  167. cFYI(1,("set POSIX default ACL rc %d",rc));
  168. #else
  169. cFYI(1,("set default POSIX ACL not supported"));
  170. #endif
  171. } else {
  172. cFYI(1,("illegal xattr request %s (only user namespace supported)",ea_name));
  173. /* BB what if no namespace prefix? */
  174. /* Should we just pass them to server, except for
  175. system and perhaps security prefixes? */
  176. }
  177. }
  178. set_ea_exit:
  179. kfree(full_path);
  180. FreeXid(xid);
  181. #endif
  182. return rc;
  183. }
  184. ssize_t cifs_getxattr(struct dentry * direntry, const char * ea_name,
  185. void * ea_value, size_t buf_size)
  186. {
  187. ssize_t rc = -EOPNOTSUPP;
  188. #ifdef CONFIG_CIFS_XATTR
  189. int xid;
  190. struct cifs_sb_info *cifs_sb;
  191. struct cifsTconInfo *pTcon;
  192. struct super_block * sb;
  193. char * full_path;
  194. if(direntry == NULL)
  195. return -EIO;
  196. if(direntry->d_inode == NULL)
  197. return -EIO;
  198. sb = direntry->d_inode->i_sb;
  199. if(sb == NULL)
  200. return -EIO;
  201. xid = GetXid();
  202. cifs_sb = CIFS_SB(sb);
  203. pTcon = cifs_sb->tcon;
  204. mutex_lock(&sb->s_vfs_rename_mutex);
  205. full_path = build_path_from_dentry(direntry);
  206. mutex_unlock(&sb->s_vfs_rename_mutex);
  207. if(full_path == NULL) {
  208. FreeXid(xid);
  209. return -ENOMEM;
  210. }
  211. /* return dos attributes as pseudo xattr */
  212. /* return alt name if available as pseudo attr */
  213. if(ea_name == NULL) {
  214. cFYI(1,("Null xattr names not supported"));
  215. } else if(strncmp(ea_name,CIFS_XATTR_USER_PREFIX,5) == 0) {
  216. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  217. goto get_ea_exit;
  218. if(strncmp(ea_name,CIFS_XATTR_DOS_ATTRIB,14) == 0) {
  219. cFYI(1,("attempt to query cifs inode metadata"));
  220. /* revalidate/getattr then populate from inode */
  221. } /* BB add else when above is implemented */
  222. ea_name += 5; /* skip past user. prefix */
  223. rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
  224. buf_size, cifs_sb->local_nls,
  225. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  226. } else if(strncmp(ea_name, CIFS_XATTR_OS2_PREFIX,4) == 0) {
  227. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  228. goto get_ea_exit;
  229. ea_name += 4; /* skip past os2. prefix */
  230. rc = CIFSSMBQueryEA(xid,pTcon,full_path,ea_name,ea_value,
  231. buf_size, cifs_sb->local_nls,
  232. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  233. } else if(strncmp(ea_name,POSIX_ACL_XATTR_ACCESS,
  234. strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
  235. #ifdef CONFIG_CIFS_POSIX
  236. if(sb->s_flags & MS_POSIXACL)
  237. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  238. ea_value, buf_size, ACL_TYPE_ACCESS,
  239. cifs_sb->local_nls,
  240. cifs_sb->mnt_cifs_flags &
  241. CIFS_MOUNT_MAP_SPECIAL_CHR);
  242. /* else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  243. __u16 fid;
  244. int oplock = FALSE;
  245. rc = CIFSSMBOpen(xid, pTcon, full_path,
  246. FILE_OPEN, GENERIC_READ, 0, &fid,
  247. &oplock, NULL, cifs_sb->local_nls,
  248. cifs_sb->mnt_cifs_flags &
  249. CIFS_MOUNT_MAP_SPECIAL_CHR);
  250. if(rc == 0) {
  251. rc = CIFSSMBGetCIFSACL(xid, pTcon, fid,
  252. ea_value, buf_size,
  253. ACL_TYPE_ACCESS);
  254. CIFSSMBClose(xid, pTcon, fid)
  255. }
  256. } */ /* BB enable after fixing up return data */
  257. #else
  258. cFYI(1,("query POSIX ACL not supported yet"));
  259. #endif /* CONFIG_CIFS_POSIX */
  260. } else if(strncmp(ea_name,POSIX_ACL_XATTR_DEFAULT,
  261. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  262. #ifdef CONFIG_CIFS_POSIX
  263. if(sb->s_flags & MS_POSIXACL)
  264. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  265. ea_value, buf_size, ACL_TYPE_DEFAULT,
  266. cifs_sb->local_nls,
  267. cifs_sb->mnt_cifs_flags &
  268. CIFS_MOUNT_MAP_SPECIAL_CHR);
  269. #else
  270. cFYI(1,("query POSIX default ACL not supported yet"));
  271. #endif
  272. } else if(strncmp(ea_name,
  273. CIFS_XATTR_TRUSTED_PREFIX,XATTR_TRUSTED_PREFIX_LEN) == 0) {
  274. cFYI(1,("Trusted xattr namespace not supported yet"));
  275. } else if(strncmp(ea_name,
  276. CIFS_XATTR_SECURITY_PREFIX,XATTR_SECURITY_PREFIX_LEN) == 0) {
  277. cFYI(1,("Security xattr namespace not supported yet"));
  278. } else {
  279. cFYI(1,("illegal xattr name request %s (only user namespace supported)",ea_name));
  280. }
  281. /* We could add an additional check for streams ie
  282. if proc/fs/cifs/streamstoxattr is set then
  283. search server for EAs or streams to
  284. returns as xattrs */
  285. if(rc == -EINVAL)
  286. rc = -EOPNOTSUPP;
  287. get_ea_exit:
  288. kfree(full_path);
  289. FreeXid(xid);
  290. #endif
  291. return rc;
  292. }
  293. ssize_t cifs_listxattr(struct dentry * direntry, char * data, size_t buf_size)
  294. {
  295. ssize_t rc = -EOPNOTSUPP;
  296. #ifdef CONFIG_CIFS_XATTR
  297. int xid;
  298. struct cifs_sb_info *cifs_sb;
  299. struct cifsTconInfo *pTcon;
  300. struct super_block * sb;
  301. char * full_path;
  302. if(direntry == NULL)
  303. return -EIO;
  304. if(direntry->d_inode == NULL)
  305. return -EIO;
  306. sb = direntry->d_inode->i_sb;
  307. if(sb == NULL)
  308. return -EIO;
  309. xid = GetXid();
  310. cifs_sb = CIFS_SB(sb);
  311. pTcon = cifs_sb->tcon;
  312. mutex_lock(&sb->s_vfs_rename_mutex);
  313. full_path = build_path_from_dentry(direntry);
  314. mutex_unlock(&sb->s_vfs_rename_mutex);
  315. if(full_path == NULL) {
  316. FreeXid(xid);
  317. return -ENOMEM;
  318. }
  319. /* return dos attributes as pseudo xattr */
  320. /* return alt name if available as pseudo attr */
  321. /* if proc/fs/cifs/streamstoxattr is set then
  322. search server for EAs or streams to
  323. returns as xattrs */
  324. rc = CIFSSMBQAllEAs(xid,pTcon,full_path,data,buf_size,
  325. cifs_sb->local_nls,
  326. cifs_sb->mnt_cifs_flags &
  327. CIFS_MOUNT_MAP_SPECIAL_CHR);
  328. kfree(full_path);
  329. FreeXid(xid);
  330. #endif
  331. return rc;
  332. }