xattr.c 24 KB

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