xattr.c 27 KB

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