xattr.c 27 KB

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