xattr.c 29 KB

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