xattr.c 12 KB

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