xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * fs/cifs/xattr.c
  3. *
  4. * Copyright (c) International Business Machines Corp., 2003, 2007
  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. full_path = build_path_from_dentry(direntry);
  58. if (full_path == NULL) {
  59. FreeXid(xid);
  60. return -ENOMEM;
  61. }
  62. if (ea_name == NULL) {
  63. cFYI(1, ("Null xattr names not supported"));
  64. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5)
  65. && (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4))) {
  66. cFYI(1,
  67. ("illegal xattr request %s (only user namespace supported)",
  68. 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. full_path = build_path_from_dentry(direntry);
  107. if (full_path == NULL) {
  108. FreeXid(xid);
  109. return -ENOMEM;
  110. }
  111. /* return dos attributes as pseudo xattr */
  112. /* return alt name if available as pseudo attr */
  113. /* if proc/fs/cifs/streamstoxattr is set then
  114. search server for EAs or streams to
  115. returns as xattrs */
  116. if (value_size > MAX_EA_VALUE_SIZE) {
  117. cFYI(1, ("size of EA value too large"));
  118. kfree(full_path);
  119. FreeXid(xid);
  120. return -EOPNOTSUPP;
  121. }
  122. if (ea_name == NULL) {
  123. cFYI(1, ("Null xattr names not supported"));
  124. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
  125. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  126. goto set_ea_exit;
  127. if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
  128. cFYI(1, ("attempt to set cifs inode metadata"));
  129. }
  130. ea_name += 5; /* skip past user. prefix */
  131. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
  132. (__u16)value_size, cifs_sb->local_nls,
  133. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  134. } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
  135. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  136. goto set_ea_exit;
  137. ea_name += 4; /* skip past os2. prefix */
  138. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
  139. (__u16)value_size, cifs_sb->local_nls,
  140. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  141. } else {
  142. int temp;
  143. temp = strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
  144. strlen(POSIX_ACL_XATTR_ACCESS));
  145. if (temp == 0) {
  146. #ifdef CONFIG_CIFS_POSIX
  147. if (sb->s_flags & MS_POSIXACL)
  148. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  149. ea_value, (const int)value_size,
  150. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  151. cifs_sb->mnt_cifs_flags &
  152. CIFS_MOUNT_MAP_SPECIAL_CHR);
  153. cFYI(1, ("set POSIX ACL rc %d", rc));
  154. #else
  155. cFYI(1, ("set POSIX ACL not supported"));
  156. #endif
  157. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  158. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  159. #ifdef CONFIG_CIFS_POSIX
  160. if (sb->s_flags & MS_POSIXACL)
  161. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  162. ea_value, (const int)value_size,
  163. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  164. cifs_sb->mnt_cifs_flags &
  165. CIFS_MOUNT_MAP_SPECIAL_CHR);
  166. cFYI(1, ("set POSIX default ACL rc %d", rc));
  167. #else
  168. cFYI(1, ("set default POSIX ACL not supported"));
  169. #endif
  170. } else {
  171. cFYI(1, ("illegal xattr request %s (only user namespace"
  172. " 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. full_path = build_path_from_dentry(direntry);
  205. if (full_path == NULL) {
  206. FreeXid(xid);
  207. return -ENOMEM;
  208. }
  209. /* return dos attributes as pseudo xattr */
  210. /* return alt name if available as pseudo attr */
  211. if (ea_name == NULL) {
  212. cFYI(1, ("Null xattr names not supported"));
  213. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
  214. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  215. goto get_ea_exit;
  216. if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
  217. cFYI(1, ("attempt to query cifs inode metadata"));
  218. /* revalidate/getattr then populate from inode */
  219. } /* BB add else when above is implemented */
  220. ea_name += 5; /* skip past user. prefix */
  221. rc = CIFSSMBQueryEA(xid, pTcon, full_path, ea_name, ea_value,
  222. buf_size, cifs_sb->local_nls,
  223. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  224. } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
  225. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  226. goto get_ea_exit;
  227. ea_name += 4; /* skip past os2. prefix */
  228. rc = CIFSSMBQueryEA(xid, pTcon, full_path, ea_name, ea_value,
  229. buf_size, cifs_sb->local_nls,
  230. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  231. } else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
  232. strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
  233. #ifdef CONFIG_CIFS_POSIX
  234. if (sb->s_flags & MS_POSIXACL)
  235. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  236. ea_value, buf_size, ACL_TYPE_ACCESS,
  237. cifs_sb->local_nls,
  238. cifs_sb->mnt_cifs_flags &
  239. CIFS_MOUNT_MAP_SPECIAL_CHR);
  240. #ifdef CONFIG_CIFS_EXPERIMENTAL
  241. else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  242. __u16 fid;
  243. int oplock = FALSE;
  244. struct cifs_ntsd *pacl = NULL;
  245. __u32 buflen = 0;
  246. if (experimEnabled)
  247. rc = CIFSSMBOpen(xid, pTcon, full_path,
  248. FILE_OPEN, GENERIC_READ, 0, &fid,
  249. &oplock, NULL, cifs_sb->local_nls,
  250. cifs_sb->mnt_cifs_flags &
  251. CIFS_MOUNT_MAP_SPECIAL_CHR);
  252. /* else rc is EOPNOTSUPP from above */
  253. if (rc == 0) {
  254. rc = CIFSSMBGetCIFSACL(xid, pTcon, fid, &pacl,
  255. &buflen);
  256. CIFSSMBClose(xid, pTcon, fid);
  257. }
  258. }
  259. #endif /* EXPERIMENTAL */
  260. #else
  261. cFYI(1, ("query POSIX ACL not supported yet"));
  262. #endif /* CONFIG_CIFS_POSIX */
  263. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  264. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  265. #ifdef CONFIG_CIFS_POSIX
  266. if (sb->s_flags & MS_POSIXACL)
  267. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  268. ea_value, buf_size, ACL_TYPE_DEFAULT,
  269. cifs_sb->local_nls,
  270. cifs_sb->mnt_cifs_flags &
  271. CIFS_MOUNT_MAP_SPECIAL_CHR);
  272. #else
  273. cFYI(1, ("query POSIX default ACL not supported yet"));
  274. #endif
  275. } else if (strncmp(ea_name,
  276. CIFS_XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
  277. cFYI(1, ("Trusted xattr namespace not supported yet"));
  278. } else if (strncmp(ea_name,
  279. CIFS_XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
  280. cFYI(1, ("Security xattr namespace not supported yet"));
  281. } else {
  282. cFYI(1,
  283. ("illegal xattr request %s (only user namespace supported)",
  284. ea_name));
  285. }
  286. /* We could add an additional check for streams ie
  287. if proc/fs/cifs/streamstoxattr is set then
  288. search server for EAs or streams to
  289. returns as xattrs */
  290. if (rc == -EINVAL)
  291. rc = -EOPNOTSUPP;
  292. get_ea_exit:
  293. kfree(full_path);
  294. FreeXid(xid);
  295. #endif
  296. return rc;
  297. }
  298. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  299. {
  300. ssize_t rc = -EOPNOTSUPP;
  301. #ifdef CONFIG_CIFS_XATTR
  302. int xid;
  303. struct cifs_sb_info *cifs_sb;
  304. struct cifsTconInfo *pTcon;
  305. struct super_block *sb;
  306. char *full_path;
  307. if (direntry == NULL)
  308. return -EIO;
  309. if (direntry->d_inode == NULL)
  310. return -EIO;
  311. sb = direntry->d_inode->i_sb;
  312. if (sb == NULL)
  313. return -EIO;
  314. cifs_sb = CIFS_SB(sb);
  315. pTcon = cifs_sb->tcon;
  316. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  317. return -EOPNOTSUPP;
  318. xid = GetXid();
  319. full_path = build_path_from_dentry(direntry);
  320. if (full_path == NULL) {
  321. FreeXid(xid);
  322. return -ENOMEM;
  323. }
  324. /* return dos attributes as pseudo xattr */
  325. /* return alt name if available as pseudo attr */
  326. /* if proc/fs/cifs/streamstoxattr is set then
  327. search server for EAs or streams to
  328. returns as xattrs */
  329. rc = CIFSSMBQAllEAs(xid, pTcon, full_path, data, buf_size,
  330. cifs_sb->local_nls,
  331. cifs_sb->mnt_cifs_flags &
  332. CIFS_MOUNT_MAP_SPECIAL_CHR);
  333. kfree(full_path);
  334. FreeXid(xid);
  335. #endif
  336. return rc;
  337. }