xattr.c 12 KB

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