xattr.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. return err;
  352. }
  353. if (update_xattr < 0) {
  354. if (xattr)
  355. __remove_xattr(ci, xattr);
  356. kfree(name);
  357. return 0;
  358. }
  359. }
  360. if (!xattr) {
  361. new = 1;
  362. xattr = *newxattr;
  363. xattr->name = name;
  364. xattr->name_len = name_len;
  365. xattr->should_free_name = update_xattr;
  366. ci->i_xattrs.count++;
  367. dout("__set_xattr count=%d\n", ci->i_xattrs.count);
  368. } else {
  369. kfree(*newxattr);
  370. *newxattr = NULL;
  371. if (xattr->should_free_val)
  372. kfree((void *)xattr->val);
  373. if (update_xattr) {
  374. kfree((void *)name);
  375. name = xattr->name;
  376. }
  377. ci->i_xattrs.names_size -= xattr->name_len;
  378. ci->i_xattrs.vals_size -= xattr->val_len;
  379. }
  380. ci->i_xattrs.names_size += name_len;
  381. ci->i_xattrs.vals_size += val_len;
  382. if (val)
  383. xattr->val = val;
  384. else
  385. xattr->val = "";
  386. xattr->val_len = val_len;
  387. xattr->dirty = update_xattr;
  388. xattr->should_free_val = (val && update_xattr);
  389. if (new) {
  390. rb_link_node(&xattr->node, parent, p);
  391. rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  392. dout("__set_xattr_val p=%p\n", p);
  393. }
  394. dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
  395. ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
  396. return 0;
  397. }
  398. static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  399. const char *name)
  400. {
  401. struct rb_node **p;
  402. struct rb_node *parent = NULL;
  403. struct ceph_inode_xattr *xattr = NULL;
  404. int name_len = strlen(name);
  405. int c;
  406. p = &ci->i_xattrs.index.rb_node;
  407. while (*p) {
  408. parent = *p;
  409. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  410. c = strncmp(name, xattr->name, xattr->name_len);
  411. if (c == 0 && name_len > xattr->name_len)
  412. c = 1;
  413. if (c < 0)
  414. p = &(*p)->rb_left;
  415. else if (c > 0)
  416. p = &(*p)->rb_right;
  417. else {
  418. dout("__get_xattr %s: found %.*s\n", name,
  419. xattr->val_len, xattr->val);
  420. return xattr;
  421. }
  422. }
  423. dout("__get_xattr %s: not found\n", name);
  424. return NULL;
  425. }
  426. static void __free_xattr(struct ceph_inode_xattr *xattr)
  427. {
  428. BUG_ON(!xattr);
  429. if (xattr->should_free_name)
  430. kfree((void *)xattr->name);
  431. if (xattr->should_free_val)
  432. kfree((void *)xattr->val);
  433. kfree(xattr);
  434. }
  435. static int __remove_xattr(struct ceph_inode_info *ci,
  436. struct ceph_inode_xattr *xattr)
  437. {
  438. if (!xattr)
  439. return -ENODATA;
  440. rb_erase(&xattr->node, &ci->i_xattrs.index);
  441. if (xattr->should_free_name)
  442. kfree((void *)xattr->name);
  443. if (xattr->should_free_val)
  444. kfree((void *)xattr->val);
  445. ci->i_xattrs.names_size -= xattr->name_len;
  446. ci->i_xattrs.vals_size -= xattr->val_len;
  447. ci->i_xattrs.count--;
  448. kfree(xattr);
  449. return 0;
  450. }
  451. static char *__copy_xattr_names(struct ceph_inode_info *ci,
  452. char *dest)
  453. {
  454. struct rb_node *p;
  455. struct ceph_inode_xattr *xattr = NULL;
  456. p = rb_first(&ci->i_xattrs.index);
  457. dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
  458. while (p) {
  459. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  460. memcpy(dest, xattr->name, xattr->name_len);
  461. dest[xattr->name_len] = '\0';
  462. dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
  463. xattr->name_len, ci->i_xattrs.names_size);
  464. dest += xattr->name_len + 1;
  465. p = rb_next(p);
  466. }
  467. return dest;
  468. }
  469. void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  470. {
  471. struct rb_node *p, *tmp;
  472. struct ceph_inode_xattr *xattr = NULL;
  473. p = rb_first(&ci->i_xattrs.index);
  474. dout("__ceph_destroy_xattrs p=%p\n", p);
  475. while (p) {
  476. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  477. tmp = p;
  478. p = rb_next(tmp);
  479. dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
  480. xattr->name_len, xattr->name);
  481. rb_erase(tmp, &ci->i_xattrs.index);
  482. __free_xattr(xattr);
  483. }
  484. ci->i_xattrs.names_size = 0;
  485. ci->i_xattrs.vals_size = 0;
  486. ci->i_xattrs.index_version = 0;
  487. ci->i_xattrs.count = 0;
  488. ci->i_xattrs.index = RB_ROOT;
  489. }
  490. static int __build_xattrs(struct inode *inode)
  491. __releases(ci->i_ceph_lock)
  492. __acquires(ci->i_ceph_lock)
  493. {
  494. u32 namelen;
  495. u32 numattr = 0;
  496. void *p, *end;
  497. u32 len;
  498. const char *name, *val;
  499. struct ceph_inode_info *ci = ceph_inode(inode);
  500. int xattr_version;
  501. struct ceph_inode_xattr **xattrs = NULL;
  502. int err = 0;
  503. int i;
  504. dout("__build_xattrs() len=%d\n",
  505. ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  506. if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  507. return 0; /* already built */
  508. __ceph_destroy_xattrs(ci);
  509. start:
  510. /* updated internal xattr rb tree */
  511. if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  512. p = ci->i_xattrs.blob->vec.iov_base;
  513. end = p + ci->i_xattrs.blob->vec.iov_len;
  514. ceph_decode_32_safe(&p, end, numattr, bad);
  515. xattr_version = ci->i_xattrs.version;
  516. spin_unlock(&ci->i_ceph_lock);
  517. xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
  518. GFP_NOFS);
  519. err = -ENOMEM;
  520. if (!xattrs)
  521. goto bad_lock;
  522. for (i = 0; i < numattr; i++) {
  523. xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  524. GFP_NOFS);
  525. if (!xattrs[i])
  526. goto bad_lock;
  527. }
  528. spin_lock(&ci->i_ceph_lock);
  529. if (ci->i_xattrs.version != xattr_version) {
  530. /* lost a race, retry */
  531. for (i = 0; i < numattr; i++)
  532. kfree(xattrs[i]);
  533. kfree(xattrs);
  534. xattrs = NULL;
  535. goto start;
  536. }
  537. err = -EIO;
  538. while (numattr--) {
  539. ceph_decode_32_safe(&p, end, len, bad);
  540. namelen = len;
  541. name = p;
  542. p += len;
  543. ceph_decode_32_safe(&p, end, len, bad);
  544. val = p;
  545. p += len;
  546. err = __set_xattr(ci, name, namelen, val, len,
  547. 0, 0, &xattrs[numattr]);
  548. if (err < 0)
  549. goto bad;
  550. }
  551. kfree(xattrs);
  552. }
  553. ci->i_xattrs.index_version = ci->i_xattrs.version;
  554. ci->i_xattrs.dirty = false;
  555. return err;
  556. bad_lock:
  557. spin_lock(&ci->i_ceph_lock);
  558. bad:
  559. if (xattrs) {
  560. for (i = 0; i < numattr; i++)
  561. kfree(xattrs[i]);
  562. kfree(xattrs);
  563. }
  564. ci->i_xattrs.names_size = 0;
  565. return err;
  566. }
  567. static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  568. int val_size)
  569. {
  570. /*
  571. * 4 bytes for the length, and additional 4 bytes per each xattr name,
  572. * 4 bytes per each value
  573. */
  574. int size = 4 + ci->i_xattrs.count*(4 + 4) +
  575. ci->i_xattrs.names_size +
  576. ci->i_xattrs.vals_size;
  577. dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
  578. ci->i_xattrs.count, ci->i_xattrs.names_size,
  579. ci->i_xattrs.vals_size);
  580. if (name_size)
  581. size += 4 + 4 + name_size + val_size;
  582. return size;
  583. }
  584. /*
  585. * If there are dirty xattrs, reencode xattrs into the prealloc_blob
  586. * and swap into place.
  587. */
  588. void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  589. {
  590. struct rb_node *p;
  591. struct ceph_inode_xattr *xattr = NULL;
  592. void *dest;
  593. dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
  594. if (ci->i_xattrs.dirty) {
  595. int need = __get_required_blob_size(ci, 0, 0);
  596. BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  597. p = rb_first(&ci->i_xattrs.index);
  598. dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  599. ceph_encode_32(&dest, ci->i_xattrs.count);
  600. while (p) {
  601. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  602. ceph_encode_32(&dest, xattr->name_len);
  603. memcpy(dest, xattr->name, xattr->name_len);
  604. dest += xattr->name_len;
  605. ceph_encode_32(&dest, xattr->val_len);
  606. memcpy(dest, xattr->val, xattr->val_len);
  607. dest += xattr->val_len;
  608. p = rb_next(p);
  609. }
  610. /* adjust buffer len; it may be larger than we need */
  611. ci->i_xattrs.prealloc_blob->vec.iov_len =
  612. dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
  613. if (ci->i_xattrs.blob)
  614. ceph_buffer_put(ci->i_xattrs.blob);
  615. ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  616. ci->i_xattrs.prealloc_blob = NULL;
  617. ci->i_xattrs.dirty = false;
  618. ci->i_xattrs.version++;
  619. }
  620. }
  621. static inline int __get_request_mask(struct inode *in) {
  622. struct ceph_mds_request *req = current->journal_info;
  623. int mask = 0;
  624. if (req && req->r_target_inode == in) {
  625. if (req->r_op == CEPH_MDS_OP_LOOKUP ||
  626. req->r_op == CEPH_MDS_OP_LOOKUPINO ||
  627. req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
  628. req->r_op == CEPH_MDS_OP_GETATTR) {
  629. mask = le32_to_cpu(req->r_args.getattr.mask);
  630. } else if (req->r_op == CEPH_MDS_OP_OPEN ||
  631. req->r_op == CEPH_MDS_OP_CREATE) {
  632. mask = le32_to_cpu(req->r_args.open.mask);
  633. }
  634. }
  635. return mask;
  636. }
  637. ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
  638. size_t size)
  639. {
  640. struct ceph_inode_info *ci = ceph_inode(inode);
  641. struct ceph_inode_xattr *xattr;
  642. struct ceph_vxattr *vxattr = NULL;
  643. int req_mask;
  644. int err;
  645. /* let's see if a virtual xattr was requested */
  646. vxattr = ceph_match_vxattr(inode, name);
  647. if (vxattr) {
  648. err = -ENODATA;
  649. if (!(vxattr->exists_cb && !vxattr->exists_cb(ci)))
  650. err = vxattr->getxattr_cb(ci, value, size);
  651. return err;
  652. }
  653. req_mask = __get_request_mask(inode);
  654. spin_lock(&ci->i_ceph_lock);
  655. dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
  656. ci->i_xattrs.version, ci->i_xattrs.index_version);
  657. if (ci->i_xattrs.version == 0 ||
  658. !((req_mask & CEPH_CAP_XATTR_SHARED) ||
  659. __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
  660. spin_unlock(&ci->i_ceph_lock);
  661. /* security module gets xattr while filling trace */
  662. if (current->journal_info != NULL) {
  663. pr_warn_ratelimited("sync getxattr %p "
  664. "during filling trace\n", inode);
  665. return -EBUSY;
  666. }
  667. /* get xattrs from mds (if we don't already have them) */
  668. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
  669. if (err)
  670. return err;
  671. spin_lock(&ci->i_ceph_lock);
  672. }
  673. err = __build_xattrs(inode);
  674. if (err < 0)
  675. goto out;
  676. err = -ENODATA; /* == ENOATTR */
  677. xattr = __get_xattr(ci, name);
  678. if (!xattr)
  679. goto out;
  680. err = -ERANGE;
  681. if (size && size < xattr->val_len)
  682. goto out;
  683. err = xattr->val_len;
  684. if (size == 0)
  685. goto out;
  686. memcpy(value, xattr->val, xattr->val_len);
  687. if (current->journal_info != NULL &&
  688. !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
  689. ci->i_ceph_flags |= CEPH_I_SEC_INITED;
  690. out:
  691. spin_unlock(&ci->i_ceph_lock);
  692. return err;
  693. }
  694. ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  695. {
  696. struct inode *inode = d_inode(dentry);
  697. struct ceph_inode_info *ci = ceph_inode(inode);
  698. struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
  699. u32 vir_namelen = 0;
  700. u32 namelen;
  701. int err;
  702. u32 len;
  703. int i;
  704. spin_lock(&ci->i_ceph_lock);
  705. dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
  706. ci->i_xattrs.version, ci->i_xattrs.index_version);
  707. if (ci->i_xattrs.version == 0 ||
  708. !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
  709. spin_unlock(&ci->i_ceph_lock);
  710. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
  711. if (err)
  712. return err;
  713. spin_lock(&ci->i_ceph_lock);
  714. }
  715. err = __build_xattrs(inode);
  716. if (err < 0)
  717. goto out;
  718. /*
  719. * Start with virtual dir xattr names (if any) (including
  720. * terminating '\0' characters for each).
  721. */
  722. vir_namelen = ceph_vxattrs_name_size(vxattrs);
  723. /* adding 1 byte per each variable due to the null termination */
  724. namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
  725. err = -ERANGE;
  726. if (size && vir_namelen + namelen > size)
  727. goto out;
  728. err = namelen + vir_namelen;
  729. if (size == 0)
  730. goto out;
  731. names = __copy_xattr_names(ci, names);
  732. /* virtual xattr names, too */
  733. err = namelen;
  734. if (vxattrs) {
  735. for (i = 0; vxattrs[i].name; i++) {
  736. if (!vxattrs[i].hidden &&
  737. !(vxattrs[i].exists_cb &&
  738. !vxattrs[i].exists_cb(ci))) {
  739. len = sprintf(names, "%s", vxattrs[i].name);
  740. names += len + 1;
  741. err += len + 1;
  742. }
  743. }
  744. }
  745. out:
  746. spin_unlock(&ci->i_ceph_lock);
  747. return err;
  748. }
  749. static int ceph_sync_setxattr(struct inode *inode, const char *name,
  750. const char *value, size_t size, int flags)
  751. {
  752. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  753. struct ceph_inode_info *ci = ceph_inode(inode);
  754. struct ceph_mds_request *req;
  755. struct ceph_mds_client *mdsc = fsc->mdsc;
  756. struct ceph_pagelist *pagelist = NULL;
  757. int op = CEPH_MDS_OP_SETXATTR;
  758. int err;
  759. if (size > 0) {
  760. /* copy value into pagelist */
  761. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  762. if (!pagelist)
  763. return -ENOMEM;
  764. ceph_pagelist_init(pagelist);
  765. err = ceph_pagelist_append(pagelist, value, size);
  766. if (err)
  767. goto out;
  768. } else if (!value) {
  769. if (flags & CEPH_XATTR_REPLACE)
  770. op = CEPH_MDS_OP_RMXATTR;
  771. else
  772. flags |= CEPH_XATTR_REMOVE;
  773. }
  774. dout("setxattr value=%.*s\n", (int)size, value);
  775. /* do request */
  776. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  777. if (IS_ERR(req)) {
  778. err = PTR_ERR(req);
  779. goto out;
  780. }
  781. req->r_path2 = kstrdup(name, GFP_NOFS);
  782. if (!req->r_path2) {
  783. ceph_mdsc_put_request(req);
  784. err = -ENOMEM;
  785. goto out;
  786. }
  787. if (op == CEPH_MDS_OP_SETXATTR) {
  788. req->r_args.setxattr.flags = cpu_to_le32(flags);
  789. req->r_pagelist = pagelist;
  790. pagelist = NULL;
  791. }
  792. req->r_inode = inode;
  793. ihold(inode);
  794. req->r_num_caps = 1;
  795. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  796. dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
  797. err = ceph_mdsc_do_request(mdsc, NULL, req);
  798. ceph_mdsc_put_request(req);
  799. dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
  800. out:
  801. if (pagelist)
  802. ceph_pagelist_release(pagelist);
  803. return err;
  804. }
  805. int __ceph_setxattr(struct inode *inode, const char *name,
  806. const void *value, size_t size, int flags)
  807. {
  808. struct ceph_vxattr *vxattr;
  809. struct ceph_inode_info *ci = ceph_inode(inode);
  810. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  811. struct ceph_cap_flush *prealloc_cf = NULL;
  812. int issued;
  813. int err;
  814. int dirty = 0;
  815. int name_len = strlen(name);
  816. int val_len = size;
  817. char *newname = NULL;
  818. char *newval = NULL;
  819. struct ceph_inode_xattr *xattr = NULL;
  820. int required_blob_size;
  821. bool lock_snap_rwsem = false;
  822. if (ceph_snap(inode) != CEPH_NOSNAP)
  823. return -EROFS;
  824. vxattr = ceph_match_vxattr(inode, name);
  825. if (vxattr && vxattr->readonly)
  826. return -EOPNOTSUPP;
  827. /* pass any unhandled ceph.* xattrs through to the MDS */
  828. if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
  829. goto do_sync_unlocked;
  830. /* preallocate memory for xattr name, value, index node */
  831. err = -ENOMEM;
  832. newname = kmemdup(name, name_len + 1, GFP_NOFS);
  833. if (!newname)
  834. goto out;
  835. if (val_len) {
  836. newval = kmemdup(value, val_len, GFP_NOFS);
  837. if (!newval)
  838. goto out;
  839. }
  840. xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  841. if (!xattr)
  842. goto out;
  843. prealloc_cf = ceph_alloc_cap_flush();
  844. if (!prealloc_cf)
  845. goto out;
  846. spin_lock(&ci->i_ceph_lock);
  847. retry:
  848. issued = __ceph_caps_issued(ci, NULL);
  849. if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
  850. goto do_sync;
  851. if (!lock_snap_rwsem && !ci->i_head_snapc) {
  852. lock_snap_rwsem = true;
  853. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  854. spin_unlock(&ci->i_ceph_lock);
  855. down_read(&mdsc->snap_rwsem);
  856. spin_lock(&ci->i_ceph_lock);
  857. goto retry;
  858. }
  859. }
  860. dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
  861. __build_xattrs(inode);
  862. required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  863. if (!ci->i_xattrs.prealloc_blob ||
  864. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  865. struct ceph_buffer *blob;
  866. spin_unlock(&ci->i_ceph_lock);
  867. dout(" preaallocating new blob size=%d\n", required_blob_size);
  868. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  869. if (!blob)
  870. goto do_sync_unlocked;
  871. spin_lock(&ci->i_ceph_lock);
  872. if (ci->i_xattrs.prealloc_blob)
  873. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  874. ci->i_xattrs.prealloc_blob = blob;
  875. goto retry;
  876. }
  877. err = __set_xattr(ci, newname, name_len, newval, val_len,
  878. flags, value ? 1 : -1, &xattr);
  879. if (!err) {
  880. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
  881. &prealloc_cf);
  882. ci->i_xattrs.dirty = true;
  883. inode->i_ctime = current_time(inode);
  884. }
  885. spin_unlock(&ci->i_ceph_lock);
  886. if (lock_snap_rwsem)
  887. up_read(&mdsc->snap_rwsem);
  888. if (dirty)
  889. __mark_inode_dirty(inode, dirty);
  890. ceph_free_cap_flush(prealloc_cf);
  891. return err;
  892. do_sync:
  893. spin_unlock(&ci->i_ceph_lock);
  894. do_sync_unlocked:
  895. if (lock_snap_rwsem)
  896. up_read(&mdsc->snap_rwsem);
  897. /* security module set xattr while filling trace */
  898. if (current->journal_info != NULL) {
  899. pr_warn_ratelimited("sync setxattr %p "
  900. "during filling trace\n", inode);
  901. err = -EBUSY;
  902. } else {
  903. err = ceph_sync_setxattr(inode, name, value, size, flags);
  904. }
  905. out:
  906. ceph_free_cap_flush(prealloc_cf);
  907. kfree(newname);
  908. kfree(newval);
  909. kfree(xattr);
  910. return err;
  911. }
  912. static int ceph_get_xattr_handler(const struct xattr_handler *handler,
  913. struct dentry *dentry, struct inode *inode,
  914. const char *name, void *value, size_t size)
  915. {
  916. if (!ceph_is_valid_xattr(name))
  917. return -EOPNOTSUPP;
  918. return __ceph_getxattr(inode, name, value, size);
  919. }
  920. static int ceph_set_xattr_handler(const struct xattr_handler *handler,
  921. struct dentry *unused, struct inode *inode,
  922. const char *name, const void *value,
  923. size_t size, int flags)
  924. {
  925. if (!ceph_is_valid_xattr(name))
  926. return -EOPNOTSUPP;
  927. return __ceph_setxattr(inode, name, value, size, flags);
  928. }
  929. static const struct xattr_handler ceph_other_xattr_handler = {
  930. .prefix = "", /* match any name => handlers called with full name */
  931. .get = ceph_get_xattr_handler,
  932. .set = ceph_set_xattr_handler,
  933. };
  934. #ifdef CONFIG_SECURITY
  935. bool ceph_security_xattr_wanted(struct inode *in)
  936. {
  937. return in->i_security != NULL;
  938. }
  939. bool ceph_security_xattr_deadlock(struct inode *in)
  940. {
  941. struct ceph_inode_info *ci;
  942. bool ret;
  943. if (in->i_security == NULL)
  944. return false;
  945. ci = ceph_inode(in);
  946. spin_lock(&ci->i_ceph_lock);
  947. ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
  948. !(ci->i_xattrs.version > 0 &&
  949. __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
  950. spin_unlock(&ci->i_ceph_lock);
  951. return ret;
  952. }
  953. #endif