xattr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. /*
  7. * Linux VFS extended attribute operations.
  8. */
  9. #include "protocol.h"
  10. #include "orangefs-kernel.h"
  11. #include "orangefs-bufmap.h"
  12. #include <linux/posix_acl_xattr.h>
  13. #include <linux/xattr.h>
  14. #define SYSTEM_ORANGEFS_KEY "system.pvfs2."
  15. #define SYSTEM_ORANGEFS_KEY_LEN 13
  16. /*
  17. * this function returns
  18. * 0 if the key corresponding to name is not meant to be printed as part
  19. * of a listxattr.
  20. * 1 if the key corresponding to name is meant to be returned as part of
  21. * a listxattr.
  22. * The ones that start SYSTEM_ORANGEFS_KEY are the ones to avoid printing.
  23. */
  24. static int is_reserved_key(const char *key, size_t size)
  25. {
  26. if (size < SYSTEM_ORANGEFS_KEY_LEN)
  27. return 1;
  28. return strncmp(key, SYSTEM_ORANGEFS_KEY, SYSTEM_ORANGEFS_KEY_LEN) ? 1 : 0;
  29. }
  30. static inline int convert_to_internal_xattr_flags(int setxattr_flags)
  31. {
  32. int internal_flag = 0;
  33. if (setxattr_flags & XATTR_REPLACE) {
  34. /* Attribute must exist! */
  35. internal_flag = ORANGEFS_XATTR_REPLACE;
  36. } else if (setxattr_flags & XATTR_CREATE) {
  37. /* Attribute must not exist */
  38. internal_flag = ORANGEFS_XATTR_CREATE;
  39. }
  40. return internal_flag;
  41. }
  42. /*
  43. * Tries to get a specified key's attributes of a given
  44. * file into a user-specified buffer. Note that the getxattr
  45. * interface allows for the users to probe the size of an
  46. * extended attribute by passing in a value of 0 to size.
  47. * Thus our return value is always the size of the attribute
  48. * unless the key does not exist for the file and/or if
  49. * there were errors in fetching the attribute value.
  50. */
  51. ssize_t orangefs_inode_getxattr(struct inode *inode, const char *name,
  52. void *buffer, size_t size)
  53. {
  54. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  55. struct orangefs_kernel_op_s *new_op = NULL;
  56. ssize_t ret = -ENOMEM;
  57. ssize_t length = 0;
  58. int fsuid;
  59. int fsgid;
  60. gossip_debug(GOSSIP_XATTR_DEBUG,
  61. "%s: name %s, buffer_size %zd\n",
  62. __func__, name, size);
  63. if (S_ISLNK(inode->i_mode))
  64. return -EOPNOTSUPP;
  65. if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN) {
  66. gossip_err("Invalid key length (%d)\n",
  67. (int)strlen(name));
  68. return -EINVAL;
  69. }
  70. fsuid = from_kuid(&init_user_ns, current_fsuid());
  71. fsgid = from_kgid(&init_user_ns, current_fsgid());
  72. gossip_debug(GOSSIP_XATTR_DEBUG,
  73. "getxattr on inode %pU, name %s "
  74. "(uid %o, gid %o)\n",
  75. get_khandle_from_ino(inode),
  76. name,
  77. fsuid,
  78. fsgid);
  79. down_read(&orangefs_inode->xattr_sem);
  80. new_op = op_alloc(ORANGEFS_VFS_OP_GETXATTR);
  81. if (!new_op)
  82. goto out_unlock;
  83. new_op->upcall.req.getxattr.refn = orangefs_inode->refn;
  84. strcpy(new_op->upcall.req.getxattr.key, name);
  85. /*
  86. * NOTE: Although keys are meant to be NULL terminated textual
  87. * strings, I am going to explicitly pass the length just in case
  88. * we change this later on...
  89. */
  90. new_op->upcall.req.getxattr.key_sz = strlen(name) + 1;
  91. ret = service_operation(new_op, "orangefs_inode_getxattr",
  92. get_interruptible_flag(inode));
  93. if (ret != 0) {
  94. if (ret == -ENOENT) {
  95. ret = -ENODATA;
  96. gossip_debug(GOSSIP_XATTR_DEBUG,
  97. "orangefs_inode_getxattr: inode %pU key %s"
  98. " does not exist!\n",
  99. get_khandle_from_ino(inode),
  100. (char *)new_op->upcall.req.getxattr.key);
  101. }
  102. goto out_release_op;
  103. }
  104. /*
  105. * Length returned includes null terminator.
  106. */
  107. length = new_op->downcall.resp.getxattr.val_sz;
  108. /*
  109. * Just return the length of the queried attribute.
  110. */
  111. if (size == 0) {
  112. ret = length;
  113. goto out_release_op;
  114. }
  115. /*
  116. * Check to see if key length is > provided buffer size.
  117. */
  118. if (length > size) {
  119. ret = -ERANGE;
  120. goto out_release_op;
  121. }
  122. memcpy(buffer, new_op->downcall.resp.getxattr.val, length);
  123. memset(buffer + length, 0, size - length);
  124. gossip_debug(GOSSIP_XATTR_DEBUG,
  125. "orangefs_inode_getxattr: inode %pU "
  126. "key %s key_sz %d, val_len %d\n",
  127. get_khandle_from_ino(inode),
  128. (char *)new_op->
  129. upcall.req.getxattr.key,
  130. (int)new_op->
  131. upcall.req.getxattr.key_sz,
  132. (int)ret);
  133. ret = length;
  134. out_release_op:
  135. op_release(new_op);
  136. out_unlock:
  137. up_read(&orangefs_inode->xattr_sem);
  138. return ret;
  139. }
  140. static int orangefs_inode_removexattr(struct inode *inode, const char *name,
  141. int flags)
  142. {
  143. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  144. struct orangefs_kernel_op_s *new_op = NULL;
  145. int ret = -ENOMEM;
  146. down_write(&orangefs_inode->xattr_sem);
  147. new_op = op_alloc(ORANGEFS_VFS_OP_REMOVEXATTR);
  148. if (!new_op)
  149. goto out_unlock;
  150. new_op->upcall.req.removexattr.refn = orangefs_inode->refn;
  151. /*
  152. * NOTE: Although keys are meant to be NULL terminated
  153. * textual strings, I am going to explicitly pass the
  154. * length just in case we change this later on...
  155. */
  156. strcpy(new_op->upcall.req.removexattr.key, name);
  157. new_op->upcall.req.removexattr.key_sz = strlen(name) + 1;
  158. gossip_debug(GOSSIP_XATTR_DEBUG,
  159. "orangefs_inode_removexattr: key %s, key_sz %d\n",
  160. (char *)new_op->upcall.req.removexattr.key,
  161. (int)new_op->upcall.req.removexattr.key_sz);
  162. ret = service_operation(new_op,
  163. "orangefs_inode_removexattr",
  164. get_interruptible_flag(inode));
  165. if (ret == -ENOENT) {
  166. /*
  167. * Request to replace a non-existent attribute is an error.
  168. */
  169. if (flags & XATTR_REPLACE)
  170. ret = -ENODATA;
  171. else
  172. ret = 0;
  173. }
  174. gossip_debug(GOSSIP_XATTR_DEBUG,
  175. "orangefs_inode_removexattr: returning %d\n", ret);
  176. op_release(new_op);
  177. out_unlock:
  178. up_write(&orangefs_inode->xattr_sem);
  179. return ret;
  180. }
  181. /*
  182. * Tries to set an attribute for a given key on a file.
  183. *
  184. * Returns a -ve number on error and 0 on success. Key is text, but value
  185. * can be binary!
  186. */
  187. int orangefs_inode_setxattr(struct inode *inode, const char *name,
  188. const void *value, size_t size, int flags)
  189. {
  190. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  191. struct orangefs_kernel_op_s *new_op;
  192. int internal_flag = 0;
  193. int ret = -ENOMEM;
  194. gossip_debug(GOSSIP_XATTR_DEBUG,
  195. "%s: name %s, buffer_size %zd\n",
  196. __func__, name, size);
  197. if (size >= ORANGEFS_MAX_XATTR_VALUELEN ||
  198. flags < 0) {
  199. gossip_err("orangefs_inode_setxattr: bogus values of size(%d), flags(%d)\n",
  200. (int)size,
  201. flags);
  202. return -EINVAL;
  203. }
  204. internal_flag = convert_to_internal_xattr_flags(flags);
  205. if (strlen(name) >= ORANGEFS_MAX_XATTR_NAMELEN) {
  206. gossip_err
  207. ("orangefs_inode_setxattr: bogus key size (%d)\n",
  208. (int)(strlen(name)));
  209. return -EINVAL;
  210. }
  211. /* This is equivalent to a removexattr */
  212. if (size == 0 && value == NULL) {
  213. gossip_debug(GOSSIP_XATTR_DEBUG,
  214. "removing xattr (%s)\n",
  215. name);
  216. return orangefs_inode_removexattr(inode, name, flags);
  217. }
  218. gossip_debug(GOSSIP_XATTR_DEBUG,
  219. "setxattr on inode %pU, name %s\n",
  220. get_khandle_from_ino(inode),
  221. name);
  222. down_write(&orangefs_inode->xattr_sem);
  223. new_op = op_alloc(ORANGEFS_VFS_OP_SETXATTR);
  224. if (!new_op)
  225. goto out_unlock;
  226. new_op->upcall.req.setxattr.refn = orangefs_inode->refn;
  227. new_op->upcall.req.setxattr.flags = internal_flag;
  228. /*
  229. * NOTE: Although keys are meant to be NULL terminated textual
  230. * strings, I am going to explicitly pass the length just in
  231. * case we change this later on...
  232. */
  233. strcpy(new_op->upcall.req.setxattr.keyval.key, name);
  234. new_op->upcall.req.setxattr.keyval.key_sz = strlen(name) + 1;
  235. memcpy(new_op->upcall.req.setxattr.keyval.val, value, size);
  236. new_op->upcall.req.setxattr.keyval.val_sz = size;
  237. gossip_debug(GOSSIP_XATTR_DEBUG,
  238. "orangefs_inode_setxattr: key %s, key_sz %d "
  239. " value size %zd\n",
  240. (char *)new_op->upcall.req.setxattr.keyval.key,
  241. (int)new_op->upcall.req.setxattr.keyval.key_sz,
  242. size);
  243. ret = service_operation(new_op,
  244. "orangefs_inode_setxattr",
  245. get_interruptible_flag(inode));
  246. gossip_debug(GOSSIP_XATTR_DEBUG,
  247. "orangefs_inode_setxattr: returning %d\n",
  248. ret);
  249. /* when request is serviced properly, free req op struct */
  250. op_release(new_op);
  251. out_unlock:
  252. up_write(&orangefs_inode->xattr_sem);
  253. return ret;
  254. }
  255. /*
  256. * Tries to get a specified object's keys into a user-specified buffer of a
  257. * given size. Note that like the previous instances of xattr routines, this
  258. * also allows you to pass in a NULL pointer and 0 size to probe the size for
  259. * subsequent memory allocations. Thus our return value is always the size of
  260. * all the keys unless there were errors in fetching the keys!
  261. */
  262. ssize_t orangefs_listxattr(struct dentry *dentry, char *buffer, size_t size)
  263. {
  264. struct inode *inode = dentry->d_inode;
  265. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  266. struct orangefs_kernel_op_s *new_op;
  267. __u64 token = ORANGEFS_ITERATE_START;
  268. ssize_t ret = -ENOMEM;
  269. ssize_t total = 0;
  270. int count_keys = 0;
  271. int key_size;
  272. int i = 0;
  273. int returned_count = 0;
  274. if (size > 0 && buffer == NULL) {
  275. gossip_err("%s: bogus NULL pointers\n", __func__);
  276. return -EINVAL;
  277. }
  278. down_read(&orangefs_inode->xattr_sem);
  279. new_op = op_alloc(ORANGEFS_VFS_OP_LISTXATTR);
  280. if (!new_op)
  281. goto out_unlock;
  282. if (buffer && size > 0)
  283. memset(buffer, 0, size);
  284. try_again:
  285. key_size = 0;
  286. new_op->upcall.req.listxattr.refn = orangefs_inode->refn;
  287. new_op->upcall.req.listxattr.token = token;
  288. new_op->upcall.req.listxattr.requested_count =
  289. (size == 0) ? 0 : ORANGEFS_MAX_XATTR_LISTLEN;
  290. ret = service_operation(new_op, __func__,
  291. get_interruptible_flag(inode));
  292. if (ret != 0)
  293. goto done;
  294. if (size == 0) {
  295. /*
  296. * This is a bit of a big upper limit, but I did not want to
  297. * spend too much time getting this correct, since users end
  298. * up allocating memory rather than us...
  299. */
  300. total = new_op->downcall.resp.listxattr.returned_count *
  301. ORANGEFS_MAX_XATTR_NAMELEN;
  302. goto done;
  303. }
  304. returned_count = new_op->downcall.resp.listxattr.returned_count;
  305. if (returned_count < 0 ||
  306. returned_count >= ORANGEFS_MAX_XATTR_LISTLEN) {
  307. gossip_err("%s: impossible value for returned_count:%d:\n",
  308. __func__,
  309. returned_count);
  310. ret = -EIO;
  311. goto done;
  312. }
  313. /*
  314. * Check to see how much can be fit in the buffer. Fit only whole keys.
  315. */
  316. for (i = 0; i < returned_count; i++) {
  317. if (new_op->downcall.resp.listxattr.lengths[i] < 0 ||
  318. new_op->downcall.resp.listxattr.lengths[i] >
  319. ORANGEFS_MAX_XATTR_NAMELEN) {
  320. gossip_err("%s: impossible value for lengths[%d]\n",
  321. __func__,
  322. new_op->downcall.resp.listxattr.lengths[i]);
  323. ret = -EIO;
  324. goto done;
  325. }
  326. if (total + new_op->downcall.resp.listxattr.lengths[i] > size)
  327. goto done;
  328. /*
  329. * Since many dumb programs try to setxattr() on our reserved
  330. * xattrs this is a feeble attempt at defeating those by not
  331. * listing them in the output of listxattr.. sigh
  332. */
  333. if (is_reserved_key(new_op->downcall.resp.listxattr.key +
  334. key_size,
  335. new_op->downcall.resp.
  336. listxattr.lengths[i])) {
  337. gossip_debug(GOSSIP_XATTR_DEBUG, "Copying key %d -> %s\n",
  338. i, new_op->downcall.resp.listxattr.key +
  339. key_size);
  340. memcpy(buffer + total,
  341. new_op->downcall.resp.listxattr.key + key_size,
  342. new_op->downcall.resp.listxattr.lengths[i]);
  343. total += new_op->downcall.resp.listxattr.lengths[i];
  344. count_keys++;
  345. } else {
  346. gossip_debug(GOSSIP_XATTR_DEBUG, "[RESERVED] key %d -> %s\n",
  347. i, new_op->downcall.resp.listxattr.key +
  348. key_size);
  349. }
  350. key_size += new_op->downcall.resp.listxattr.lengths[i];
  351. }
  352. /*
  353. * Since the buffer was large enough, we might have to continue
  354. * fetching more keys!
  355. */
  356. token = new_op->downcall.resp.listxattr.token;
  357. if (token != ORANGEFS_ITERATE_END)
  358. goto try_again;
  359. done:
  360. gossip_debug(GOSSIP_XATTR_DEBUG, "%s: returning %d"
  361. " [size of buffer %ld] (filled in %d keys)\n",
  362. __func__,
  363. ret ? (int)ret : (int)total,
  364. (long)size,
  365. count_keys);
  366. op_release(new_op);
  367. if (ret == 0)
  368. ret = total;
  369. out_unlock:
  370. up_read(&orangefs_inode->xattr_sem);
  371. return ret;
  372. }
  373. static int orangefs_xattr_set_default(const struct xattr_handler *handler,
  374. struct dentry *unused,
  375. struct inode *inode,
  376. const char *name,
  377. const void *buffer,
  378. size_t size,
  379. int flags)
  380. {
  381. return orangefs_inode_setxattr(inode, name, buffer, size, flags);
  382. }
  383. static int orangefs_xattr_get_default(const struct xattr_handler *handler,
  384. struct dentry *unused,
  385. struct inode *inode,
  386. const char *name,
  387. void *buffer,
  388. size_t size)
  389. {
  390. return orangefs_inode_getxattr(inode, name, buffer, size);
  391. }
  392. static struct xattr_handler orangefs_xattr_default_handler = {
  393. .prefix = "", /* match any name => handlers called with full name */
  394. .get = orangefs_xattr_get_default,
  395. .set = orangefs_xattr_set_default,
  396. };
  397. const struct xattr_handler *orangefs_xattr_handlers[] = {
  398. &posix_acl_access_xattr_handler,
  399. &posix_acl_default_xattr_handler,
  400. &orangefs_xattr_default_handler,
  401. NULL
  402. };