xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. rc = -ENOMEM;
  60. FreeXid(xid);
  61. return rc;
  62. }
  63. if (ea_name == NULL) {
  64. cFYI(1, ("Null xattr names not supported"));
  65. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5)
  66. && (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4))) {
  67. cFYI(1,
  68. ("illegal xattr request %s (only user namespace supported)",
  69. ea_name));
  70. /* BB what if no namespace prefix? */
  71. /* Should we just pass them to server, except for
  72. system and perhaps security prefixes? */
  73. } else {
  74. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  75. goto remove_ea_exit;
  76. ea_name += 5; /* skip past user. prefix */
  77. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
  78. (__u16)0, cifs_sb->local_nls,
  79. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  80. }
  81. remove_ea_exit:
  82. kfree(full_path);
  83. FreeXid(xid);
  84. #endif
  85. return rc;
  86. }
  87. int cifs_setxattr(struct dentry *direntry, const char *ea_name,
  88. const void *ea_value, size_t value_size, int flags)
  89. {
  90. int rc = -EOPNOTSUPP;
  91. #ifdef CONFIG_CIFS_XATTR
  92. int xid;
  93. struct cifs_sb_info *cifs_sb;
  94. struct cifsTconInfo *pTcon;
  95. struct super_block *sb;
  96. char *full_path;
  97. if (direntry == NULL)
  98. return -EIO;
  99. if (direntry->d_inode == NULL)
  100. return -EIO;
  101. sb = direntry->d_inode->i_sb;
  102. if (sb == NULL)
  103. return -EIO;
  104. xid = GetXid();
  105. cifs_sb = CIFS_SB(sb);
  106. pTcon = cifs_sb->tcon;
  107. full_path = build_path_from_dentry(direntry);
  108. if (full_path == NULL) {
  109. rc = -ENOMEM;
  110. FreeXid(xid);
  111. return rc;
  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. ea_name += 5; /* skip past user. prefix */
  132. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
  133. (__u16)value_size, cifs_sb->local_nls,
  134. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  135. } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
  136. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  137. goto set_ea_exit;
  138. ea_name += 4; /* skip past os2. prefix */
  139. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
  140. (__u16)value_size, cifs_sb->local_nls,
  141. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  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. if (sb->s_flags & MS_POSIXACL)
  149. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  150. ea_value, (const int)value_size,
  151. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  152. cifs_sb->mnt_cifs_flags &
  153. CIFS_MOUNT_MAP_SPECIAL_CHR);
  154. cFYI(1, ("set POSIX ACL rc %d", rc));
  155. #else
  156. cFYI(1, ("set POSIX ACL not supported"));
  157. #endif
  158. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  159. 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"
  173. " supported)", ea_name));
  174. /* BB what if no namespace prefix? */
  175. /* Should we just pass them to server, except for
  176. system and perhaps security prefixes? */
  177. }
  178. }
  179. set_ea_exit:
  180. kfree(full_path);
  181. FreeXid(xid);
  182. #endif
  183. return rc;
  184. }
  185. ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
  186. void *ea_value, size_t buf_size)
  187. {
  188. ssize_t rc = -EOPNOTSUPP;
  189. #ifdef CONFIG_CIFS_XATTR
  190. int xid;
  191. struct cifs_sb_info *cifs_sb;
  192. struct cifsTconInfo *pTcon;
  193. struct super_block *sb;
  194. char *full_path;
  195. if (direntry == NULL)
  196. return -EIO;
  197. if (direntry->d_inode == NULL)
  198. return -EIO;
  199. sb = direntry->d_inode->i_sb;
  200. if (sb == NULL)
  201. return -EIO;
  202. xid = GetXid();
  203. cifs_sb = CIFS_SB(sb);
  204. pTcon = cifs_sb->tcon;
  205. full_path = build_path_from_dentry(direntry);
  206. if (full_path == NULL) {
  207. rc = -ENOMEM;
  208. FreeXid(xid);
  209. return rc;
  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. #ifdef CONFIG_CIFS_EXPERIMENTAL
  243. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  244. __u16 fid;
  245. int oplock = 0;
  246. struct cifs_ntsd *pacl = NULL;
  247. __u32 buflen = 0;
  248. if (experimEnabled)
  249. rc = CIFSSMBOpen(xid, pTcon, full_path,
  250. FILE_OPEN, GENERIC_READ, 0, &fid,
  251. &oplock, NULL, cifs_sb->local_nls,
  252. cifs_sb->mnt_cifs_flags &
  253. CIFS_MOUNT_MAP_SPECIAL_CHR);
  254. /* else rc is EOPNOTSUPP from above */
  255. if (rc == 0) {
  256. rc = CIFSSMBGetCIFSACL(xid, pTcon, fid, &pacl,
  257. &buflen);
  258. CIFSSMBClose(xid, pTcon, fid);
  259. }
  260. }
  261. #endif /* EXPERIMENTAL */
  262. #else
  263. cFYI(1, ("query POSIX ACL not supported yet"));
  264. #endif /* CONFIG_CIFS_POSIX */
  265. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  266. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  267. #ifdef CONFIG_CIFS_POSIX
  268. if (sb->s_flags & MS_POSIXACL)
  269. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  270. ea_value, buf_size, ACL_TYPE_DEFAULT,
  271. cifs_sb->local_nls,
  272. cifs_sb->mnt_cifs_flags &
  273. CIFS_MOUNT_MAP_SPECIAL_CHR);
  274. #else
  275. cFYI(1, ("query POSIX default ACL not supported yet"));
  276. #endif
  277. } else if (strncmp(ea_name,
  278. CIFS_XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
  279. cFYI(1, ("Trusted xattr namespace not supported yet"));
  280. } else if (strncmp(ea_name,
  281. CIFS_XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
  282. cFYI(1, ("Security xattr namespace not supported yet"));
  283. } else
  284. cFYI(1,
  285. ("illegal xattr request %s (only user namespace supported)",
  286. ea_name));
  287. /* We could add an additional check for streams ie
  288. if proc/fs/cifs/streamstoxattr is set then
  289. search server for EAs or streams to
  290. returns as xattrs */
  291. if (rc == -EINVAL)
  292. rc = -EOPNOTSUPP;
  293. get_ea_exit:
  294. kfree(full_path);
  295. FreeXid(xid);
  296. #endif
  297. return rc;
  298. }
  299. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  300. {
  301. ssize_t rc = -EOPNOTSUPP;
  302. #ifdef CONFIG_CIFS_XATTR
  303. int xid;
  304. struct cifs_sb_info *cifs_sb;
  305. struct cifsTconInfo *pTcon;
  306. struct super_block *sb;
  307. char *full_path;
  308. if (direntry == NULL)
  309. return -EIO;
  310. if (direntry->d_inode == NULL)
  311. return -EIO;
  312. sb = direntry->d_inode->i_sb;
  313. if (sb == NULL)
  314. return -EIO;
  315. cifs_sb = CIFS_SB(sb);
  316. pTcon = cifs_sb->tcon;
  317. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  318. return -EOPNOTSUPP;
  319. xid = GetXid();
  320. full_path = build_path_from_dentry(direntry);
  321. if (full_path == NULL) {
  322. rc = -ENOMEM;
  323. FreeXid(xid);
  324. return rc;
  325. }
  326. /* return dos attributes as pseudo xattr */
  327. /* return alt name if available as pseudo attr */
  328. /* if proc/fs/cifs/streamstoxattr is set then
  329. search server for EAs or streams to
  330. returns as xattrs */
  331. rc = CIFSSMBQAllEAs(xid, pTcon, full_path, data, buf_size,
  332. cifs_sb->local_nls,
  333. cifs_sb->mnt_cifs_flags &
  334. CIFS_MOUNT_MAP_SPECIAL_CHR);
  335. kfree(full_path);
  336. FreeXid(xid);
  337. #endif
  338. return rc;
  339. }