xattr.c 28 KB

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