xattr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. #include <linux/ceph/ceph_debug.h>
  2. #include "super.h"
  3. #include "mds_client.h"
  4. #include <linux/ceph/decode.h>
  5. #include <linux/xattr.h>
  6. #include <linux/slab.h>
  7. #define XATTR_CEPH_PREFIX "ceph."
  8. #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
  9. static bool ceph_is_valid_xattr(const char *name)
  10. {
  11. return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
  12. !strncmp(name, XATTR_SECURITY_PREFIX,
  13. XATTR_SECURITY_PREFIX_LEN) ||
  14. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  15. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  16. }
  17. /*
  18. * These define virtual xattrs exposing the recursive directory
  19. * statistics and layout metadata.
  20. */
  21. struct ceph_vxattr {
  22. char *name;
  23. size_t name_size; /* strlen(name) + 1 (for '\0') */
  24. size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
  25. size_t size);
  26. bool readonly;
  27. };
  28. /* directories */
  29. static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
  30. size_t size)
  31. {
  32. return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
  33. }
  34. static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
  35. size_t size)
  36. {
  37. return snprintf(val, size, "%lld", ci->i_files);
  38. }
  39. static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
  40. size_t size)
  41. {
  42. return snprintf(val, size, "%lld", ci->i_subdirs);
  43. }
  44. static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
  45. size_t size)
  46. {
  47. return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
  48. }
  49. static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
  50. size_t size)
  51. {
  52. return snprintf(val, size, "%lld", ci->i_rfiles);
  53. }
  54. static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
  55. size_t size)
  56. {
  57. return snprintf(val, size, "%lld", ci->i_rsubdirs);
  58. }
  59. static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
  60. size_t size)
  61. {
  62. return snprintf(val, size, "%lld", ci->i_rbytes);
  63. }
  64. static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
  65. size_t size)
  66. {
  67. return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec,
  68. (long)ci->i_rctime.tv_nsec);
  69. }
  70. #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
  71. #define XATTR_NAME_CEPH(_type, _name) \
  72. { \
  73. .name = CEPH_XATTR_NAME(_type, _name), \
  74. .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
  75. .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
  76. .readonly = true, \
  77. }
  78. static struct ceph_vxattr ceph_dir_vxattrs[] = {
  79. XATTR_NAME_CEPH(dir, entries),
  80. XATTR_NAME_CEPH(dir, files),
  81. XATTR_NAME_CEPH(dir, subdirs),
  82. XATTR_NAME_CEPH(dir, rentries),
  83. XATTR_NAME_CEPH(dir, rfiles),
  84. XATTR_NAME_CEPH(dir, rsubdirs),
  85. XATTR_NAME_CEPH(dir, rbytes),
  86. XATTR_NAME_CEPH(dir, rctime),
  87. { 0 } /* Required table terminator */
  88. };
  89. static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
  90. /* files */
  91. static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
  92. size_t size)
  93. {
  94. int ret;
  95. ret = snprintf(val, size,
  96. "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
  97. (unsigned long long)ceph_file_layout_su(ci->i_layout),
  98. (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
  99. (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
  100. if (ceph_file_layout_pg_preferred(ci->i_layout))
  101. ret += snprintf(val + ret, size, "preferred_osd=%lld\n",
  102. (unsigned long long)ceph_file_layout_pg_preferred(
  103. ci->i_layout));
  104. return ret;
  105. }
  106. static struct ceph_vxattr ceph_file_vxattrs[] = {
  107. XATTR_NAME_CEPH(file, layout),
  108. /* The following extended attribute name is deprecated */
  109. {
  110. .name = XATTR_CEPH_PREFIX "layout",
  111. .name_size = sizeof (XATTR_CEPH_PREFIX "layout"),
  112. .getxattr_cb = ceph_vxattrcb_file_layout,
  113. .readonly = true,
  114. },
  115. { 0 } /* Required table terminator */
  116. };
  117. static size_t ceph_file_vxattrs_name_size; /* total size of all names */
  118. static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
  119. {
  120. if (S_ISDIR(inode->i_mode))
  121. return ceph_dir_vxattrs;
  122. else if (S_ISREG(inode->i_mode))
  123. return ceph_file_vxattrs;
  124. return NULL;
  125. }
  126. static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
  127. {
  128. if (vxattrs == ceph_dir_vxattrs)
  129. return ceph_dir_vxattrs_name_size;
  130. if (vxattrs == ceph_file_vxattrs)
  131. return ceph_file_vxattrs_name_size;
  132. BUG();
  133. return 0;
  134. }
  135. /*
  136. * Compute the aggregate size (including terminating '\0') of all
  137. * virtual extended attribute names in the given vxattr table.
  138. */
  139. static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
  140. {
  141. struct ceph_vxattr *vxattr;
  142. size_t size = 0;
  143. for (vxattr = vxattrs; vxattr->name; vxattr++)
  144. size += vxattr->name_size;
  145. return size;
  146. }
  147. /* Routines called at initialization and exit time */
  148. void __init ceph_xattr_init(void)
  149. {
  150. ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
  151. ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
  152. }
  153. void ceph_xattr_exit(void)
  154. {
  155. ceph_dir_vxattrs_name_size = 0;
  156. ceph_file_vxattrs_name_size = 0;
  157. }
  158. static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
  159. const char *name)
  160. {
  161. struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
  162. if (vxattr) {
  163. while (vxattr->name) {
  164. if (!strcmp(vxattr->name, name))
  165. return vxattr;
  166. vxattr++;
  167. }
  168. }
  169. return NULL;
  170. }
  171. static int __set_xattr(struct ceph_inode_info *ci,
  172. const char *name, int name_len,
  173. const char *val, int val_len,
  174. int dirty,
  175. int should_free_name, int should_free_val,
  176. struct ceph_inode_xattr **newxattr)
  177. {
  178. struct rb_node **p;
  179. struct rb_node *parent = NULL;
  180. struct ceph_inode_xattr *xattr = NULL;
  181. int c;
  182. int new = 0;
  183. p = &ci->i_xattrs.index.rb_node;
  184. while (*p) {
  185. parent = *p;
  186. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  187. c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
  188. if (c < 0)
  189. p = &(*p)->rb_left;
  190. else if (c > 0)
  191. p = &(*p)->rb_right;
  192. else {
  193. if (name_len == xattr->name_len)
  194. break;
  195. else if (name_len < xattr->name_len)
  196. p = &(*p)->rb_left;
  197. else
  198. p = &(*p)->rb_right;
  199. }
  200. xattr = NULL;
  201. }
  202. if (!xattr) {
  203. new = 1;
  204. xattr = *newxattr;
  205. xattr->name = name;
  206. xattr->name_len = name_len;
  207. xattr->should_free_name = should_free_name;
  208. ci->i_xattrs.count++;
  209. dout("__set_xattr count=%d\n", ci->i_xattrs.count);
  210. } else {
  211. kfree(*newxattr);
  212. *newxattr = NULL;
  213. if (xattr->should_free_val)
  214. kfree((void *)xattr->val);
  215. if (should_free_name) {
  216. kfree((void *)name);
  217. name = xattr->name;
  218. }
  219. ci->i_xattrs.names_size -= xattr->name_len;
  220. ci->i_xattrs.vals_size -= xattr->val_len;
  221. }
  222. ci->i_xattrs.names_size += name_len;
  223. ci->i_xattrs.vals_size += val_len;
  224. if (val)
  225. xattr->val = val;
  226. else
  227. xattr->val = "";
  228. xattr->val_len = val_len;
  229. xattr->dirty = dirty;
  230. xattr->should_free_val = (val && should_free_val);
  231. if (new) {
  232. rb_link_node(&xattr->node, parent, p);
  233. rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  234. dout("__set_xattr_val p=%p\n", p);
  235. }
  236. dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
  237. ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
  238. return 0;
  239. }
  240. static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  241. const char *name)
  242. {
  243. struct rb_node **p;
  244. struct rb_node *parent = NULL;
  245. struct ceph_inode_xattr *xattr = NULL;
  246. int name_len = strlen(name);
  247. int c;
  248. p = &ci->i_xattrs.index.rb_node;
  249. while (*p) {
  250. parent = *p;
  251. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  252. c = strncmp(name, xattr->name, xattr->name_len);
  253. if (c == 0 && name_len > xattr->name_len)
  254. c = 1;
  255. if (c < 0)
  256. p = &(*p)->rb_left;
  257. else if (c > 0)
  258. p = &(*p)->rb_right;
  259. else {
  260. dout("__get_xattr %s: found %.*s\n", name,
  261. xattr->val_len, xattr->val);
  262. return xattr;
  263. }
  264. }
  265. dout("__get_xattr %s: not found\n", name);
  266. return NULL;
  267. }
  268. static void __free_xattr(struct ceph_inode_xattr *xattr)
  269. {
  270. BUG_ON(!xattr);
  271. if (xattr->should_free_name)
  272. kfree((void *)xattr->name);
  273. if (xattr->should_free_val)
  274. kfree((void *)xattr->val);
  275. kfree(xattr);
  276. }
  277. static int __remove_xattr(struct ceph_inode_info *ci,
  278. struct ceph_inode_xattr *xattr)
  279. {
  280. if (!xattr)
  281. return -EOPNOTSUPP;
  282. rb_erase(&xattr->node, &ci->i_xattrs.index);
  283. if (xattr->should_free_name)
  284. kfree((void *)xattr->name);
  285. if (xattr->should_free_val)
  286. kfree((void *)xattr->val);
  287. ci->i_xattrs.names_size -= xattr->name_len;
  288. ci->i_xattrs.vals_size -= xattr->val_len;
  289. ci->i_xattrs.count--;
  290. kfree(xattr);
  291. return 0;
  292. }
  293. static int __remove_xattr_by_name(struct ceph_inode_info *ci,
  294. const char *name)
  295. {
  296. struct rb_node **p;
  297. struct ceph_inode_xattr *xattr;
  298. int err;
  299. p = &ci->i_xattrs.index.rb_node;
  300. xattr = __get_xattr(ci, name);
  301. err = __remove_xattr(ci, xattr);
  302. return err;
  303. }
  304. static char *__copy_xattr_names(struct ceph_inode_info *ci,
  305. char *dest)
  306. {
  307. struct rb_node *p;
  308. struct ceph_inode_xattr *xattr = NULL;
  309. p = rb_first(&ci->i_xattrs.index);
  310. dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
  311. while (p) {
  312. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  313. memcpy(dest, xattr->name, xattr->name_len);
  314. dest[xattr->name_len] = '\0';
  315. dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
  316. xattr->name_len, ci->i_xattrs.names_size);
  317. dest += xattr->name_len + 1;
  318. p = rb_next(p);
  319. }
  320. return dest;
  321. }
  322. void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  323. {
  324. struct rb_node *p, *tmp;
  325. struct ceph_inode_xattr *xattr = NULL;
  326. p = rb_first(&ci->i_xattrs.index);
  327. dout("__ceph_destroy_xattrs p=%p\n", p);
  328. while (p) {
  329. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  330. tmp = p;
  331. p = rb_next(tmp);
  332. dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
  333. xattr->name_len, xattr->name);
  334. rb_erase(tmp, &ci->i_xattrs.index);
  335. __free_xattr(xattr);
  336. }
  337. ci->i_xattrs.names_size = 0;
  338. ci->i_xattrs.vals_size = 0;
  339. ci->i_xattrs.index_version = 0;
  340. ci->i_xattrs.count = 0;
  341. ci->i_xattrs.index = RB_ROOT;
  342. }
  343. static int __build_xattrs(struct inode *inode)
  344. __releases(ci->i_ceph_lock)
  345. __acquires(ci->i_ceph_lock)
  346. {
  347. u32 namelen;
  348. u32 numattr = 0;
  349. void *p, *end;
  350. u32 len;
  351. const char *name, *val;
  352. struct ceph_inode_info *ci = ceph_inode(inode);
  353. int xattr_version;
  354. struct ceph_inode_xattr **xattrs = NULL;
  355. int err = 0;
  356. int i;
  357. dout("__build_xattrs() len=%d\n",
  358. ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  359. if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  360. return 0; /* already built */
  361. __ceph_destroy_xattrs(ci);
  362. start:
  363. /* updated internal xattr rb tree */
  364. if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  365. p = ci->i_xattrs.blob->vec.iov_base;
  366. end = p + ci->i_xattrs.blob->vec.iov_len;
  367. ceph_decode_32_safe(&p, end, numattr, bad);
  368. xattr_version = ci->i_xattrs.version;
  369. spin_unlock(&ci->i_ceph_lock);
  370. xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
  371. GFP_NOFS);
  372. err = -ENOMEM;
  373. if (!xattrs)
  374. goto bad_lock;
  375. memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
  376. for (i = 0; i < numattr; i++) {
  377. xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  378. GFP_NOFS);
  379. if (!xattrs[i])
  380. goto bad_lock;
  381. }
  382. spin_lock(&ci->i_ceph_lock);
  383. if (ci->i_xattrs.version != xattr_version) {
  384. /* lost a race, retry */
  385. for (i = 0; i < numattr; i++)
  386. kfree(xattrs[i]);
  387. kfree(xattrs);
  388. goto start;
  389. }
  390. err = -EIO;
  391. while (numattr--) {
  392. ceph_decode_32_safe(&p, end, len, bad);
  393. namelen = len;
  394. name = p;
  395. p += len;
  396. ceph_decode_32_safe(&p, end, len, bad);
  397. val = p;
  398. p += len;
  399. err = __set_xattr(ci, name, namelen, val, len,
  400. 0, 0, 0, &xattrs[numattr]);
  401. if (err < 0)
  402. goto bad;
  403. }
  404. kfree(xattrs);
  405. }
  406. ci->i_xattrs.index_version = ci->i_xattrs.version;
  407. ci->i_xattrs.dirty = false;
  408. return err;
  409. bad_lock:
  410. spin_lock(&ci->i_ceph_lock);
  411. bad:
  412. if (xattrs) {
  413. for (i = 0; i < numattr; i++)
  414. kfree(xattrs[i]);
  415. kfree(xattrs);
  416. }
  417. ci->i_xattrs.names_size = 0;
  418. return err;
  419. }
  420. static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  421. int val_size)
  422. {
  423. /*
  424. * 4 bytes for the length, and additional 4 bytes per each xattr name,
  425. * 4 bytes per each value
  426. */
  427. int size = 4 + ci->i_xattrs.count*(4 + 4) +
  428. ci->i_xattrs.names_size +
  429. ci->i_xattrs.vals_size;
  430. dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
  431. ci->i_xattrs.count, ci->i_xattrs.names_size,
  432. ci->i_xattrs.vals_size);
  433. if (name_size)
  434. size += 4 + 4 + name_size + val_size;
  435. return size;
  436. }
  437. /*
  438. * If there are dirty xattrs, reencode xattrs into the prealloc_blob
  439. * and swap into place.
  440. */
  441. void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  442. {
  443. struct rb_node *p;
  444. struct ceph_inode_xattr *xattr = NULL;
  445. void *dest;
  446. dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
  447. if (ci->i_xattrs.dirty) {
  448. int need = __get_required_blob_size(ci, 0, 0);
  449. BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  450. p = rb_first(&ci->i_xattrs.index);
  451. dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  452. ceph_encode_32(&dest, ci->i_xattrs.count);
  453. while (p) {
  454. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  455. ceph_encode_32(&dest, xattr->name_len);
  456. memcpy(dest, xattr->name, xattr->name_len);
  457. dest += xattr->name_len;
  458. ceph_encode_32(&dest, xattr->val_len);
  459. memcpy(dest, xattr->val, xattr->val_len);
  460. dest += xattr->val_len;
  461. p = rb_next(p);
  462. }
  463. /* adjust buffer len; it may be larger than we need */
  464. ci->i_xattrs.prealloc_blob->vec.iov_len =
  465. dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
  466. if (ci->i_xattrs.blob)
  467. ceph_buffer_put(ci->i_xattrs.blob);
  468. ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  469. ci->i_xattrs.prealloc_blob = NULL;
  470. ci->i_xattrs.dirty = false;
  471. ci->i_xattrs.version++;
  472. }
  473. }
  474. ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
  475. size_t size)
  476. {
  477. struct inode *inode = dentry->d_inode;
  478. struct ceph_inode_info *ci = ceph_inode(inode);
  479. int err;
  480. struct ceph_inode_xattr *xattr;
  481. struct ceph_vxattr *vxattr = NULL;
  482. if (!ceph_is_valid_xattr(name))
  483. return -ENODATA;
  484. /* let's see if a virtual xattr was requested */
  485. vxattr = ceph_match_vxattr(inode, name);
  486. spin_lock(&ci->i_ceph_lock);
  487. dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
  488. ci->i_xattrs.version, ci->i_xattrs.index_version);
  489. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  490. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  491. goto get_xattr;
  492. } else {
  493. spin_unlock(&ci->i_ceph_lock);
  494. /* get xattrs from mds (if we don't already have them) */
  495. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  496. if (err)
  497. return err;
  498. }
  499. spin_lock(&ci->i_ceph_lock);
  500. if (vxattr && vxattr->readonly) {
  501. err = vxattr->getxattr_cb(ci, value, size);
  502. goto out;
  503. }
  504. err = __build_xattrs(inode);
  505. if (err < 0)
  506. goto out;
  507. get_xattr:
  508. err = -ENODATA; /* == ENOATTR */
  509. xattr = __get_xattr(ci, name);
  510. if (!xattr) {
  511. if (vxattr)
  512. err = vxattr->getxattr_cb(ci, value, size);
  513. goto out;
  514. }
  515. err = -ERANGE;
  516. if (size && size < xattr->val_len)
  517. goto out;
  518. err = xattr->val_len;
  519. if (size == 0)
  520. goto out;
  521. memcpy(value, xattr->val, xattr->val_len);
  522. out:
  523. spin_unlock(&ci->i_ceph_lock);
  524. return err;
  525. }
  526. ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  527. {
  528. struct inode *inode = dentry->d_inode;
  529. struct ceph_inode_info *ci = ceph_inode(inode);
  530. struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
  531. u32 vir_namelen = 0;
  532. u32 namelen;
  533. int err;
  534. u32 len;
  535. int i;
  536. spin_lock(&ci->i_ceph_lock);
  537. dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
  538. ci->i_xattrs.version, ci->i_xattrs.index_version);
  539. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  540. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  541. goto list_xattr;
  542. } else {
  543. spin_unlock(&ci->i_ceph_lock);
  544. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  545. if (err)
  546. return err;
  547. }
  548. spin_lock(&ci->i_ceph_lock);
  549. err = __build_xattrs(inode);
  550. if (err < 0)
  551. goto out;
  552. list_xattr:
  553. /*
  554. * Start with virtual dir xattr names (if any) (including
  555. * terminating '\0' characters for each).
  556. */
  557. vir_namelen = ceph_vxattrs_name_size(vxattrs);
  558. /* adding 1 byte per each variable due to the null termination */
  559. namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
  560. err = -ERANGE;
  561. if (size && namelen > size)
  562. goto out;
  563. err = namelen;
  564. if (size == 0)
  565. goto out;
  566. names = __copy_xattr_names(ci, names);
  567. /* virtual xattr names, too */
  568. if (vxattrs)
  569. for (i = 0; vxattrs[i].name; i++) {
  570. len = sprintf(names, "%s", vxattrs[i].name);
  571. names += len + 1;
  572. }
  573. out:
  574. spin_unlock(&ci->i_ceph_lock);
  575. return err;
  576. }
  577. static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
  578. const char *value, size_t size, int flags)
  579. {
  580. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  581. struct inode *inode = dentry->d_inode;
  582. struct ceph_inode_info *ci = ceph_inode(inode);
  583. struct inode *parent_inode;
  584. struct ceph_mds_request *req;
  585. struct ceph_mds_client *mdsc = fsc->mdsc;
  586. int err;
  587. int i, nr_pages;
  588. struct page **pages = NULL;
  589. void *kaddr;
  590. /* copy value into some pages */
  591. nr_pages = calc_pages_for(0, size);
  592. if (nr_pages) {
  593. pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
  594. if (!pages)
  595. return -ENOMEM;
  596. err = -ENOMEM;
  597. for (i = 0; i < nr_pages; i++) {
  598. pages[i] = __page_cache_alloc(GFP_NOFS);
  599. if (!pages[i]) {
  600. nr_pages = i;
  601. goto out;
  602. }
  603. kaddr = kmap(pages[i]);
  604. memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
  605. min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
  606. }
  607. }
  608. dout("setxattr value=%.*s\n", (int)size, value);
  609. /* do request */
  610. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
  611. USE_AUTH_MDS);
  612. if (IS_ERR(req)) {
  613. err = PTR_ERR(req);
  614. goto out;
  615. }
  616. req->r_inode = inode;
  617. ihold(inode);
  618. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  619. req->r_num_caps = 1;
  620. req->r_args.setxattr.flags = cpu_to_le32(flags);
  621. req->r_path2 = kstrdup(name, GFP_NOFS);
  622. req->r_pages = pages;
  623. req->r_num_pages = nr_pages;
  624. req->r_data_len = size;
  625. dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
  626. parent_inode = ceph_get_dentry_parent_inode(dentry);
  627. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  628. iput(parent_inode);
  629. ceph_mdsc_put_request(req);
  630. dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
  631. out:
  632. if (pages) {
  633. for (i = 0; i < nr_pages; i++)
  634. __free_page(pages[i]);
  635. kfree(pages);
  636. }
  637. return err;
  638. }
  639. int ceph_setxattr(struct dentry *dentry, const char *name,
  640. const void *value, size_t size, int flags)
  641. {
  642. struct inode *inode = dentry->d_inode;
  643. struct ceph_vxattr *vxattr;
  644. struct ceph_inode_info *ci = ceph_inode(inode);
  645. int err;
  646. int name_len = strlen(name);
  647. int val_len = size;
  648. char *newname = NULL;
  649. char *newval = NULL;
  650. struct ceph_inode_xattr *xattr = NULL;
  651. int issued;
  652. int required_blob_size;
  653. int dirty;
  654. if (ceph_snap(inode) != CEPH_NOSNAP)
  655. return -EROFS;
  656. if (!ceph_is_valid_xattr(name))
  657. return -EOPNOTSUPP;
  658. vxattr = ceph_match_vxattr(inode, name);
  659. if (vxattr && vxattr->readonly)
  660. return -EOPNOTSUPP;
  661. /* preallocate memory for xattr name, value, index node */
  662. err = -ENOMEM;
  663. newname = kmemdup(name, name_len + 1, GFP_NOFS);
  664. if (!newname)
  665. goto out;
  666. if (val_len) {
  667. newval = kmemdup(value, val_len, GFP_NOFS);
  668. if (!newval)
  669. goto out;
  670. }
  671. xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  672. if (!xattr)
  673. goto out;
  674. spin_lock(&ci->i_ceph_lock);
  675. retry:
  676. issued = __ceph_caps_issued(ci, NULL);
  677. if (!(issued & CEPH_CAP_XATTR_EXCL))
  678. goto do_sync;
  679. __build_xattrs(inode);
  680. required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  681. if (!ci->i_xattrs.prealloc_blob ||
  682. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  683. struct ceph_buffer *blob = NULL;
  684. spin_unlock(&ci->i_ceph_lock);
  685. dout(" preaallocating new blob size=%d\n", required_blob_size);
  686. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  687. if (!blob)
  688. goto out;
  689. spin_lock(&ci->i_ceph_lock);
  690. if (ci->i_xattrs.prealloc_blob)
  691. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  692. ci->i_xattrs.prealloc_blob = blob;
  693. goto retry;
  694. }
  695. dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
  696. err = __set_xattr(ci, newname, name_len, newval,
  697. val_len, 1, 1, 1, &xattr);
  698. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  699. ci->i_xattrs.dirty = true;
  700. inode->i_ctime = CURRENT_TIME;
  701. spin_unlock(&ci->i_ceph_lock);
  702. if (dirty)
  703. __mark_inode_dirty(inode, dirty);
  704. return err;
  705. do_sync:
  706. spin_unlock(&ci->i_ceph_lock);
  707. err = ceph_sync_setxattr(dentry, name, value, size, flags);
  708. out:
  709. kfree(newname);
  710. kfree(newval);
  711. kfree(xattr);
  712. return err;
  713. }
  714. static int ceph_send_removexattr(struct dentry *dentry, const char *name)
  715. {
  716. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  717. struct ceph_mds_client *mdsc = fsc->mdsc;
  718. struct inode *inode = dentry->d_inode;
  719. struct inode *parent_inode;
  720. struct ceph_mds_request *req;
  721. int err;
  722. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
  723. USE_AUTH_MDS);
  724. if (IS_ERR(req))
  725. return PTR_ERR(req);
  726. req->r_inode = inode;
  727. ihold(inode);
  728. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  729. req->r_num_caps = 1;
  730. req->r_path2 = kstrdup(name, GFP_NOFS);
  731. parent_inode = ceph_get_dentry_parent_inode(dentry);
  732. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  733. iput(parent_inode);
  734. ceph_mdsc_put_request(req);
  735. return err;
  736. }
  737. int ceph_removexattr(struct dentry *dentry, const char *name)
  738. {
  739. struct inode *inode = dentry->d_inode;
  740. struct ceph_vxattr *vxattr;
  741. struct ceph_inode_info *ci = ceph_inode(inode);
  742. int issued;
  743. int err;
  744. int required_blob_size;
  745. int dirty;
  746. if (ceph_snap(inode) != CEPH_NOSNAP)
  747. return -EROFS;
  748. if (!ceph_is_valid_xattr(name))
  749. return -EOPNOTSUPP;
  750. vxattr = ceph_match_vxattr(inode, name);
  751. if (vxattr && vxattr->readonly)
  752. return -EOPNOTSUPP;
  753. err = -ENOMEM;
  754. spin_lock(&ci->i_ceph_lock);
  755. __build_xattrs(inode);
  756. retry:
  757. issued = __ceph_caps_issued(ci, NULL);
  758. dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
  759. if (!(issued & CEPH_CAP_XATTR_EXCL))
  760. goto do_sync;
  761. required_blob_size = __get_required_blob_size(ci, 0, 0);
  762. if (!ci->i_xattrs.prealloc_blob ||
  763. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  764. struct ceph_buffer *blob;
  765. spin_unlock(&ci->i_ceph_lock);
  766. dout(" preaallocating new blob size=%d\n", required_blob_size);
  767. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  768. if (!blob)
  769. goto out;
  770. spin_lock(&ci->i_ceph_lock);
  771. if (ci->i_xattrs.prealloc_blob)
  772. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  773. ci->i_xattrs.prealloc_blob = blob;
  774. goto retry;
  775. }
  776. err = __remove_xattr_by_name(ceph_inode(inode), name);
  777. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  778. ci->i_xattrs.dirty = true;
  779. inode->i_ctime = CURRENT_TIME;
  780. spin_unlock(&ci->i_ceph_lock);
  781. if (dirty)
  782. __mark_inode_dirty(inode, dirty);
  783. return err;
  784. do_sync:
  785. spin_unlock(&ci->i_ceph_lock);
  786. err = ceph_send_removexattr(dentry, name);
  787. out:
  788. return err;
  789. }