xattr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <linux/slab.h>
  24. #include "cifsfs.h"
  25. #include "cifspdu.h"
  26. #include "cifsglob.h"
  27. #include "cifsproto.h"
  28. #include "cifs_debug.h"
  29. #define MAX_EA_VALUE_SIZE 65535
  30. #define CIFS_XATTR_DOS_ATTRIB "user.DosAttrib"
  31. #define CIFS_XATTR_CIFS_ACL "system.cifs_acl"
  32. #define CIFS_XATTR_USER_PREFIX "user."
  33. #define CIFS_XATTR_SYSTEM_PREFIX "system."
  34. #define CIFS_XATTR_OS2_PREFIX "os2."
  35. #define CIFS_XATTR_SECURITY_PREFIX "security."
  36. #define CIFS_XATTR_TRUSTED_PREFIX "trusted."
  37. #define XATTR_TRUSTED_PREFIX_LEN 8
  38. #define XATTR_SECURITY_PREFIX_LEN 9
  39. /* BB need to add server (Samba e.g) support for security and trusted prefix */
  40. int cifs_removexattr(struct dentry *direntry, const char *ea_name)
  41. {
  42. int rc = -EOPNOTSUPP;
  43. #ifdef CONFIG_CIFS_XATTR
  44. int xid;
  45. struct cifs_sb_info *cifs_sb;
  46. struct tcon_link *tlink;
  47. struct cifsTconInfo *pTcon;
  48. struct super_block *sb;
  49. char *full_path = NULL;
  50. if (direntry == NULL)
  51. return -EIO;
  52. if (direntry->d_inode == NULL)
  53. return -EIO;
  54. sb = direntry->d_inode->i_sb;
  55. if (sb == NULL)
  56. return -EIO;
  57. cifs_sb = CIFS_SB(sb);
  58. tlink = cifs_sb_tlink(cifs_sb);
  59. if (IS_ERR(tlink))
  60. return PTR_ERR(tlink);
  61. pTcon = tlink_tcon(tlink);
  62. xid = GetXid();
  63. full_path = build_path_from_dentry(direntry);
  64. if (full_path == NULL) {
  65. rc = -ENOMEM;
  66. goto remove_ea_exit;
  67. }
  68. if (ea_name == NULL) {
  69. cFYI(1, "Null xattr names not supported");
  70. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5)
  71. && (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4))) {
  72. cFYI(1,
  73. "illegal xattr request %s (only user namespace supported)",
  74. ea_name);
  75. /* BB what if no namespace prefix? */
  76. /* Should we just pass them to server, except for
  77. system and perhaps security prefixes? */
  78. } else {
  79. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  80. goto remove_ea_exit;
  81. ea_name += 5; /* skip past user. prefix */
  82. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, NULL,
  83. (__u16)0, cifs_sb->local_nls,
  84. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  85. }
  86. remove_ea_exit:
  87. kfree(full_path);
  88. FreeXid(xid);
  89. cifs_put_tlink(tlink);
  90. #endif
  91. return rc;
  92. }
  93. int cifs_setxattr(struct dentry *direntry, const char *ea_name,
  94. const void *ea_value, size_t value_size, int flags)
  95. {
  96. int rc = -EOPNOTSUPP;
  97. #ifdef CONFIG_CIFS_XATTR
  98. int xid;
  99. struct cifs_sb_info *cifs_sb;
  100. struct tcon_link *tlink;
  101. struct cifsTconInfo *pTcon;
  102. struct super_block *sb;
  103. char *full_path;
  104. if (direntry == NULL)
  105. return -EIO;
  106. if (direntry->d_inode == NULL)
  107. return -EIO;
  108. sb = direntry->d_inode->i_sb;
  109. if (sb == NULL)
  110. return -EIO;
  111. cifs_sb = CIFS_SB(sb);
  112. tlink = cifs_sb_tlink(cifs_sb);
  113. if (IS_ERR(tlink))
  114. return PTR_ERR(tlink);
  115. pTcon = tlink_tcon(tlink);
  116. xid = GetXid();
  117. full_path = build_path_from_dentry(direntry);
  118. if (full_path == NULL) {
  119. rc = -ENOMEM;
  120. goto set_ea_exit;
  121. }
  122. /* return dos attributes as pseudo xattr */
  123. /* return alt name if available as pseudo attr */
  124. /* if proc/fs/cifs/streamstoxattr is set then
  125. search server for EAs or streams to
  126. returns as xattrs */
  127. if (value_size > MAX_EA_VALUE_SIZE) {
  128. cFYI(1, "size of EA value too large");
  129. rc = -EOPNOTSUPP;
  130. goto set_ea_exit;
  131. }
  132. if (ea_name == NULL) {
  133. cFYI(1, "Null xattr names not supported");
  134. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
  135. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  136. goto set_ea_exit;
  137. if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0)
  138. cFYI(1, "attempt to set cifs inode metadata");
  139. ea_name += 5; /* skip past user. 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 if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
  144. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  145. goto set_ea_exit;
  146. ea_name += 4; /* skip past os2. prefix */
  147. rc = CIFSSMBSetEA(xid, pTcon, full_path, ea_name, ea_value,
  148. (__u16)value_size, cifs_sb->local_nls,
  149. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  150. } else {
  151. int temp;
  152. temp = strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
  153. strlen(POSIX_ACL_XATTR_ACCESS));
  154. if (temp == 0) {
  155. #ifdef CONFIG_CIFS_POSIX
  156. if (sb->s_flags & MS_POSIXACL)
  157. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  158. ea_value, (const int)value_size,
  159. ACL_TYPE_ACCESS, cifs_sb->local_nls,
  160. cifs_sb->mnt_cifs_flags &
  161. CIFS_MOUNT_MAP_SPECIAL_CHR);
  162. cFYI(1, "set POSIX ACL rc %d", rc);
  163. #else
  164. cFYI(1, "set POSIX ACL not supported");
  165. #endif
  166. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  167. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  168. #ifdef CONFIG_CIFS_POSIX
  169. if (sb->s_flags & MS_POSIXACL)
  170. rc = CIFSSMBSetPosixACL(xid, pTcon, full_path,
  171. ea_value, (const int)value_size,
  172. ACL_TYPE_DEFAULT, cifs_sb->local_nls,
  173. cifs_sb->mnt_cifs_flags &
  174. CIFS_MOUNT_MAP_SPECIAL_CHR);
  175. cFYI(1, "set POSIX default ACL rc %d", rc);
  176. #else
  177. cFYI(1, "set default POSIX ACL not supported");
  178. #endif
  179. } else {
  180. cFYI(1, "illegal xattr request %s (only user namespace"
  181. " supported)", ea_name);
  182. /* BB what if no namespace prefix? */
  183. /* Should we just pass them to server, except for
  184. system and perhaps security prefixes? */
  185. }
  186. }
  187. set_ea_exit:
  188. kfree(full_path);
  189. FreeXid(xid);
  190. cifs_put_tlink(tlink);
  191. #endif
  192. return rc;
  193. }
  194. ssize_t cifs_getxattr(struct dentry *direntry, const char *ea_name,
  195. void *ea_value, size_t buf_size)
  196. {
  197. ssize_t rc = -EOPNOTSUPP;
  198. #ifdef CONFIG_CIFS_XATTR
  199. int xid;
  200. struct cifs_sb_info *cifs_sb;
  201. struct tcon_link *tlink;
  202. struct cifsTconInfo *pTcon;
  203. struct super_block *sb;
  204. char *full_path;
  205. if (direntry == NULL)
  206. return -EIO;
  207. if (direntry->d_inode == NULL)
  208. return -EIO;
  209. sb = direntry->d_inode->i_sb;
  210. if (sb == NULL)
  211. return -EIO;
  212. cifs_sb = CIFS_SB(sb);
  213. tlink = cifs_sb_tlink(cifs_sb);
  214. if (IS_ERR(tlink))
  215. return PTR_ERR(tlink);
  216. pTcon = tlink_tcon(tlink);
  217. xid = GetXid();
  218. full_path = build_path_from_dentry(direntry);
  219. if (full_path == NULL) {
  220. rc = -ENOMEM;
  221. goto get_ea_exit;
  222. }
  223. /* return dos attributes as pseudo xattr */
  224. /* return alt name if available as pseudo attr */
  225. if (ea_name == NULL) {
  226. cFYI(1, "Null xattr names not supported");
  227. } else if (strncmp(ea_name, CIFS_XATTR_USER_PREFIX, 5) == 0) {
  228. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  229. goto get_ea_exit;
  230. if (strncmp(ea_name, CIFS_XATTR_DOS_ATTRIB, 14) == 0) {
  231. cFYI(1, "attempt to query cifs inode metadata");
  232. /* revalidate/getattr then populate from inode */
  233. } /* BB add else when above is implemented */
  234. ea_name += 5; /* skip past user. prefix */
  235. rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
  236. buf_size, cifs_sb->local_nls,
  237. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  238. } else if (strncmp(ea_name, CIFS_XATTR_OS2_PREFIX, 4) == 0) {
  239. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  240. goto get_ea_exit;
  241. ea_name += 4; /* skip past os2. prefix */
  242. rc = CIFSSMBQAllEAs(xid, pTcon, full_path, ea_name, ea_value,
  243. buf_size, cifs_sb->local_nls,
  244. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  245. } else if (strncmp(ea_name, POSIX_ACL_XATTR_ACCESS,
  246. strlen(POSIX_ACL_XATTR_ACCESS)) == 0) {
  247. #ifdef CONFIG_CIFS_POSIX
  248. if (sb->s_flags & MS_POSIXACL)
  249. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  250. ea_value, buf_size, ACL_TYPE_ACCESS,
  251. cifs_sb->local_nls,
  252. cifs_sb->mnt_cifs_flags &
  253. CIFS_MOUNT_MAP_SPECIAL_CHR);
  254. #else
  255. cFYI(1, "Query POSIX ACL not supported yet");
  256. #endif /* CONFIG_CIFS_POSIX */
  257. } else if (strncmp(ea_name, POSIX_ACL_XATTR_DEFAULT,
  258. strlen(POSIX_ACL_XATTR_DEFAULT)) == 0) {
  259. #ifdef CONFIG_CIFS_POSIX
  260. if (sb->s_flags & MS_POSIXACL)
  261. rc = CIFSSMBGetPosixACL(xid, pTcon, full_path,
  262. ea_value, buf_size, ACL_TYPE_DEFAULT,
  263. cifs_sb->local_nls,
  264. cifs_sb->mnt_cifs_flags &
  265. CIFS_MOUNT_MAP_SPECIAL_CHR);
  266. #else
  267. cFYI(1, "Query POSIX default ACL not supported yet");
  268. #endif /* CONFIG_CIFS_POSIX */
  269. } else if (strncmp(ea_name, CIFS_XATTR_CIFS_ACL,
  270. strlen(CIFS_XATTR_CIFS_ACL)) == 0) {
  271. #ifdef CONFIG_CIFS_ACL
  272. u32 acllen;
  273. struct cifs_ntsd *pacl;
  274. pacl = get_cifs_acl(cifs_sb, direntry->d_inode,
  275. full_path, &acllen);
  276. if (IS_ERR(pacl)) {
  277. rc = PTR_ERR(pacl);
  278. cERROR(1, "%s: error %zd getting sec desc",
  279. __func__, rc);
  280. } else {
  281. if (ea_value) {
  282. if (acllen > buf_size)
  283. acllen = -ERANGE;
  284. else
  285. memcpy(ea_value, pacl, acllen);
  286. }
  287. rc = acllen;
  288. kfree(pacl);
  289. }
  290. #else
  291. cFYI(1, "Query CIFS ACL not supported yet");
  292. #endif /* CONFIG_CIFS_ACL */
  293. } else if (strncmp(ea_name,
  294. CIFS_XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) == 0) {
  295. cFYI(1, "Trusted xattr namespace not supported yet");
  296. } else if (strncmp(ea_name,
  297. CIFS_XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) == 0) {
  298. cFYI(1, "Security xattr namespace not supported yet");
  299. } else
  300. cFYI(1,
  301. "illegal xattr request %s (only user namespace supported)",
  302. ea_name);
  303. /* We could add an additional check for streams ie
  304. if proc/fs/cifs/streamstoxattr is set then
  305. search server for EAs or streams to
  306. returns as xattrs */
  307. if (rc == -EINVAL)
  308. rc = -EOPNOTSUPP;
  309. get_ea_exit:
  310. kfree(full_path);
  311. FreeXid(xid);
  312. cifs_put_tlink(tlink);
  313. #endif
  314. return rc;
  315. }
  316. ssize_t cifs_listxattr(struct dentry *direntry, char *data, size_t buf_size)
  317. {
  318. ssize_t rc = -EOPNOTSUPP;
  319. #ifdef CONFIG_CIFS_XATTR
  320. int xid;
  321. struct cifs_sb_info *cifs_sb;
  322. struct tcon_link *tlink;
  323. struct cifsTconInfo *pTcon;
  324. struct super_block *sb;
  325. char *full_path;
  326. if (direntry == NULL)
  327. return -EIO;
  328. if (direntry->d_inode == NULL)
  329. return -EIO;
  330. sb = direntry->d_inode->i_sb;
  331. if (sb == NULL)
  332. return -EIO;
  333. cifs_sb = CIFS_SB(sb);
  334. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_XATTR)
  335. return -EOPNOTSUPP;
  336. tlink = cifs_sb_tlink(cifs_sb);
  337. if (IS_ERR(tlink))
  338. return PTR_ERR(tlink);
  339. pTcon = tlink_tcon(tlink);
  340. xid = GetXid();
  341. full_path = build_path_from_dentry(direntry);
  342. if (full_path == NULL) {
  343. rc = -ENOMEM;
  344. goto list_ea_exit;
  345. }
  346. /* return dos attributes as pseudo xattr */
  347. /* return alt name if available as pseudo attr */
  348. /* if proc/fs/cifs/streamstoxattr is set then
  349. search server for EAs or streams to
  350. returns as xattrs */
  351. rc = CIFSSMBQAllEAs(xid, pTcon, full_path, NULL, data,
  352. buf_size, cifs_sb->local_nls,
  353. cifs_sb->mnt_cifs_flags &
  354. CIFS_MOUNT_MAP_SPECIAL_CHR);
  355. list_ea_exit:
  356. kfree(full_path);
  357. FreeXid(xid);
  358. cifs_put_tlink(tlink);
  359. #endif
  360. return rc;
  361. }