xattr.c 12 KB

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