xattr.c 15 KB

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