xattr.c 29 KB

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