xattr.c 28 KB

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