xattr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. #include <linux/ceph/ceph_debug.h>
  2. #include "super.h"
  3. #include "mds_client.h"
  4. #include <linux/ceph/decode.h>
  5. #include <linux/xattr.h>
  6. #include <linux/slab.h>
  7. #define XATTR_CEPH_PREFIX "ceph."
  8. #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
  9. static bool ceph_is_valid_xattr(const char *name)
  10. {
  11. return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
  12. !strncmp(name, XATTR_SECURITY_PREFIX,
  13. XATTR_SECURITY_PREFIX_LEN) ||
  14. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  15. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  16. }
  17. /*
  18. * These define virtual xattrs exposing the recursive directory
  19. * statistics and layout metadata.
  20. */
  21. struct ceph_vxattr {
  22. char *name;
  23. size_t name_size; /* strlen(name) + 1 (for '\0') */
  24. size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
  25. size_t size);
  26. bool readonly, hidden;
  27. };
  28. /* directories */
  29. static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
  30. size_t size)
  31. {
  32. return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
  33. }
  34. static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
  35. size_t size)
  36. {
  37. return snprintf(val, size, "%lld", ci->i_files);
  38. }
  39. static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
  40. size_t size)
  41. {
  42. return snprintf(val, size, "%lld", ci->i_subdirs);
  43. }
  44. static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
  45. size_t size)
  46. {
  47. return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
  48. }
  49. static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
  50. size_t size)
  51. {
  52. return snprintf(val, size, "%lld", ci->i_rfiles);
  53. }
  54. static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
  55. size_t size)
  56. {
  57. return snprintf(val, size, "%lld", ci->i_rsubdirs);
  58. }
  59. static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
  60. size_t size)
  61. {
  62. return snprintf(val, size, "%lld", ci->i_rbytes);
  63. }
  64. static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
  65. size_t size)
  66. {
  67. return snprintf(val, size, "%ld.09%ld", (long)ci->i_rctime.tv_sec,
  68. (long)ci->i_rctime.tv_nsec);
  69. }
  70. #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
  71. #define XATTR_NAME_CEPH(_type, _name) \
  72. { \
  73. .name = CEPH_XATTR_NAME(_type, _name), \
  74. .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
  75. .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
  76. .readonly = true, \
  77. .hidden = false, \
  78. }
  79. static struct ceph_vxattr ceph_dir_vxattrs[] = {
  80. XATTR_NAME_CEPH(dir, entries),
  81. XATTR_NAME_CEPH(dir, files),
  82. XATTR_NAME_CEPH(dir, subdirs),
  83. XATTR_NAME_CEPH(dir, rentries),
  84. XATTR_NAME_CEPH(dir, rfiles),
  85. XATTR_NAME_CEPH(dir, rsubdirs),
  86. XATTR_NAME_CEPH(dir, rbytes),
  87. XATTR_NAME_CEPH(dir, rctime),
  88. { 0 } /* Required table terminator */
  89. };
  90. static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
  91. /* files */
  92. static size_t ceph_vxattrcb_file_layout(struct ceph_inode_info *ci, char *val,
  93. size_t size)
  94. {
  95. int ret;
  96. ret = snprintf(val, size,
  97. "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n",
  98. (unsigned long long)ceph_file_layout_su(ci->i_layout),
  99. (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout),
  100. (unsigned long long)ceph_file_layout_object_size(ci->i_layout));
  101. return ret;
  102. }
  103. static struct ceph_vxattr ceph_file_vxattrs[] = {
  104. XATTR_NAME_CEPH(file, layout),
  105. { 0 } /* Required table terminator */
  106. };
  107. static size_t ceph_file_vxattrs_name_size; /* total size of all names */
  108. static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
  109. {
  110. if (S_ISDIR(inode->i_mode))
  111. return ceph_dir_vxattrs;
  112. else if (S_ISREG(inode->i_mode))
  113. return ceph_file_vxattrs;
  114. return NULL;
  115. }
  116. static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
  117. {
  118. if (vxattrs == ceph_dir_vxattrs)
  119. return ceph_dir_vxattrs_name_size;
  120. if (vxattrs == ceph_file_vxattrs)
  121. return ceph_file_vxattrs_name_size;
  122. BUG();
  123. return 0;
  124. }
  125. /*
  126. * Compute the aggregate size (including terminating '\0') of all
  127. * virtual extended attribute names in the given vxattr table.
  128. */
  129. static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
  130. {
  131. struct ceph_vxattr *vxattr;
  132. size_t size = 0;
  133. for (vxattr = vxattrs; vxattr->name; vxattr++)
  134. if (!vxattr->hidden)
  135. size += vxattr->name_size;
  136. return size;
  137. }
  138. /* Routines called at initialization and exit time */
  139. void __init ceph_xattr_init(void)
  140. {
  141. ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
  142. ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
  143. }
  144. void ceph_xattr_exit(void)
  145. {
  146. ceph_dir_vxattrs_name_size = 0;
  147. ceph_file_vxattrs_name_size = 0;
  148. }
  149. static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
  150. const char *name)
  151. {
  152. struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
  153. if (vxattr) {
  154. while (vxattr->name) {
  155. if (!strcmp(vxattr->name, name))
  156. return vxattr;
  157. vxattr++;
  158. }
  159. }
  160. return NULL;
  161. }
  162. static int __set_xattr(struct ceph_inode_info *ci,
  163. const char *name, int name_len,
  164. const char *val, int val_len,
  165. int dirty,
  166. int should_free_name, int should_free_val,
  167. struct ceph_inode_xattr **newxattr)
  168. {
  169. struct rb_node **p;
  170. struct rb_node *parent = NULL;
  171. struct ceph_inode_xattr *xattr = NULL;
  172. int c;
  173. int new = 0;
  174. p = &ci->i_xattrs.index.rb_node;
  175. while (*p) {
  176. parent = *p;
  177. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  178. c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
  179. if (c < 0)
  180. p = &(*p)->rb_left;
  181. else if (c > 0)
  182. p = &(*p)->rb_right;
  183. else {
  184. if (name_len == xattr->name_len)
  185. break;
  186. else if (name_len < xattr->name_len)
  187. p = &(*p)->rb_left;
  188. else
  189. p = &(*p)->rb_right;
  190. }
  191. xattr = NULL;
  192. }
  193. if (!xattr) {
  194. new = 1;
  195. xattr = *newxattr;
  196. xattr->name = name;
  197. xattr->name_len = name_len;
  198. xattr->should_free_name = should_free_name;
  199. ci->i_xattrs.count++;
  200. dout("__set_xattr count=%d\n", ci->i_xattrs.count);
  201. } else {
  202. kfree(*newxattr);
  203. *newxattr = NULL;
  204. if (xattr->should_free_val)
  205. kfree((void *)xattr->val);
  206. if (should_free_name) {
  207. kfree((void *)name);
  208. name = xattr->name;
  209. }
  210. ci->i_xattrs.names_size -= xattr->name_len;
  211. ci->i_xattrs.vals_size -= xattr->val_len;
  212. }
  213. ci->i_xattrs.names_size += name_len;
  214. ci->i_xattrs.vals_size += val_len;
  215. if (val)
  216. xattr->val = val;
  217. else
  218. xattr->val = "";
  219. xattr->val_len = val_len;
  220. xattr->dirty = dirty;
  221. xattr->should_free_val = (val && should_free_val);
  222. if (new) {
  223. rb_link_node(&xattr->node, parent, p);
  224. rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  225. dout("__set_xattr_val p=%p\n", p);
  226. }
  227. dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
  228. ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
  229. return 0;
  230. }
  231. static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  232. const char *name)
  233. {
  234. struct rb_node **p;
  235. struct rb_node *parent = NULL;
  236. struct ceph_inode_xattr *xattr = NULL;
  237. int name_len = strlen(name);
  238. int c;
  239. p = &ci->i_xattrs.index.rb_node;
  240. while (*p) {
  241. parent = *p;
  242. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  243. c = strncmp(name, xattr->name, xattr->name_len);
  244. if (c == 0 && name_len > xattr->name_len)
  245. c = 1;
  246. if (c < 0)
  247. p = &(*p)->rb_left;
  248. else if (c > 0)
  249. p = &(*p)->rb_right;
  250. else {
  251. dout("__get_xattr %s: found %.*s\n", name,
  252. xattr->val_len, xattr->val);
  253. return xattr;
  254. }
  255. }
  256. dout("__get_xattr %s: not found\n", name);
  257. return NULL;
  258. }
  259. static void __free_xattr(struct ceph_inode_xattr *xattr)
  260. {
  261. BUG_ON(!xattr);
  262. if (xattr->should_free_name)
  263. kfree((void *)xattr->name);
  264. if (xattr->should_free_val)
  265. kfree((void *)xattr->val);
  266. kfree(xattr);
  267. }
  268. static int __remove_xattr(struct ceph_inode_info *ci,
  269. struct ceph_inode_xattr *xattr)
  270. {
  271. if (!xattr)
  272. return -EOPNOTSUPP;
  273. rb_erase(&xattr->node, &ci->i_xattrs.index);
  274. if (xattr->should_free_name)
  275. kfree((void *)xattr->name);
  276. if (xattr->should_free_val)
  277. kfree((void *)xattr->val);
  278. ci->i_xattrs.names_size -= xattr->name_len;
  279. ci->i_xattrs.vals_size -= xattr->val_len;
  280. ci->i_xattrs.count--;
  281. kfree(xattr);
  282. return 0;
  283. }
  284. static int __remove_xattr_by_name(struct ceph_inode_info *ci,
  285. const char *name)
  286. {
  287. struct rb_node **p;
  288. struct ceph_inode_xattr *xattr;
  289. int err;
  290. p = &ci->i_xattrs.index.rb_node;
  291. xattr = __get_xattr(ci, name);
  292. err = __remove_xattr(ci, xattr);
  293. return err;
  294. }
  295. static char *__copy_xattr_names(struct ceph_inode_info *ci,
  296. char *dest)
  297. {
  298. struct rb_node *p;
  299. struct ceph_inode_xattr *xattr = NULL;
  300. p = rb_first(&ci->i_xattrs.index);
  301. dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
  302. while (p) {
  303. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  304. memcpy(dest, xattr->name, xattr->name_len);
  305. dest[xattr->name_len] = '\0';
  306. dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
  307. xattr->name_len, ci->i_xattrs.names_size);
  308. dest += xattr->name_len + 1;
  309. p = rb_next(p);
  310. }
  311. return dest;
  312. }
  313. void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  314. {
  315. struct rb_node *p, *tmp;
  316. struct ceph_inode_xattr *xattr = NULL;
  317. p = rb_first(&ci->i_xattrs.index);
  318. dout("__ceph_destroy_xattrs p=%p\n", p);
  319. while (p) {
  320. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  321. tmp = p;
  322. p = rb_next(tmp);
  323. dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
  324. xattr->name_len, xattr->name);
  325. rb_erase(tmp, &ci->i_xattrs.index);
  326. __free_xattr(xattr);
  327. }
  328. ci->i_xattrs.names_size = 0;
  329. ci->i_xattrs.vals_size = 0;
  330. ci->i_xattrs.index_version = 0;
  331. ci->i_xattrs.count = 0;
  332. ci->i_xattrs.index = RB_ROOT;
  333. }
  334. static int __build_xattrs(struct inode *inode)
  335. __releases(ci->i_ceph_lock)
  336. __acquires(ci->i_ceph_lock)
  337. {
  338. u32 namelen;
  339. u32 numattr = 0;
  340. void *p, *end;
  341. u32 len;
  342. const char *name, *val;
  343. struct ceph_inode_info *ci = ceph_inode(inode);
  344. int xattr_version;
  345. struct ceph_inode_xattr **xattrs = NULL;
  346. int err = 0;
  347. int i;
  348. dout("__build_xattrs() len=%d\n",
  349. ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  350. if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  351. return 0; /* already built */
  352. __ceph_destroy_xattrs(ci);
  353. start:
  354. /* updated internal xattr rb tree */
  355. if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  356. p = ci->i_xattrs.blob->vec.iov_base;
  357. end = p + ci->i_xattrs.blob->vec.iov_len;
  358. ceph_decode_32_safe(&p, end, numattr, bad);
  359. xattr_version = ci->i_xattrs.version;
  360. spin_unlock(&ci->i_ceph_lock);
  361. xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
  362. GFP_NOFS);
  363. err = -ENOMEM;
  364. if (!xattrs)
  365. goto bad_lock;
  366. memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
  367. for (i = 0; i < numattr; i++) {
  368. xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  369. GFP_NOFS);
  370. if (!xattrs[i])
  371. goto bad_lock;
  372. }
  373. spin_lock(&ci->i_ceph_lock);
  374. if (ci->i_xattrs.version != xattr_version) {
  375. /* lost a race, retry */
  376. for (i = 0; i < numattr; i++)
  377. kfree(xattrs[i]);
  378. kfree(xattrs);
  379. xattrs = NULL;
  380. goto start;
  381. }
  382. err = -EIO;
  383. while (numattr--) {
  384. ceph_decode_32_safe(&p, end, len, bad);
  385. namelen = len;
  386. name = p;
  387. p += len;
  388. ceph_decode_32_safe(&p, end, len, bad);
  389. val = p;
  390. p += len;
  391. err = __set_xattr(ci, name, namelen, val, len,
  392. 0, 0, 0, &xattrs[numattr]);
  393. if (err < 0)
  394. goto bad;
  395. }
  396. kfree(xattrs);
  397. }
  398. ci->i_xattrs.index_version = ci->i_xattrs.version;
  399. ci->i_xattrs.dirty = false;
  400. return err;
  401. bad_lock:
  402. spin_lock(&ci->i_ceph_lock);
  403. bad:
  404. if (xattrs) {
  405. for (i = 0; i < numattr; i++)
  406. kfree(xattrs[i]);
  407. kfree(xattrs);
  408. }
  409. ci->i_xattrs.names_size = 0;
  410. return err;
  411. }
  412. static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  413. int val_size)
  414. {
  415. /*
  416. * 4 bytes for the length, and additional 4 bytes per each xattr name,
  417. * 4 bytes per each value
  418. */
  419. int size = 4 + ci->i_xattrs.count*(4 + 4) +
  420. ci->i_xattrs.names_size +
  421. ci->i_xattrs.vals_size;
  422. dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
  423. ci->i_xattrs.count, ci->i_xattrs.names_size,
  424. ci->i_xattrs.vals_size);
  425. if (name_size)
  426. size += 4 + 4 + name_size + val_size;
  427. return size;
  428. }
  429. /*
  430. * If there are dirty xattrs, reencode xattrs into the prealloc_blob
  431. * and swap into place.
  432. */
  433. void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  434. {
  435. struct rb_node *p;
  436. struct ceph_inode_xattr *xattr = NULL;
  437. void *dest;
  438. dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
  439. if (ci->i_xattrs.dirty) {
  440. int need = __get_required_blob_size(ci, 0, 0);
  441. BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  442. p = rb_first(&ci->i_xattrs.index);
  443. dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  444. ceph_encode_32(&dest, ci->i_xattrs.count);
  445. while (p) {
  446. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  447. ceph_encode_32(&dest, xattr->name_len);
  448. memcpy(dest, xattr->name, xattr->name_len);
  449. dest += xattr->name_len;
  450. ceph_encode_32(&dest, xattr->val_len);
  451. memcpy(dest, xattr->val, xattr->val_len);
  452. dest += xattr->val_len;
  453. p = rb_next(p);
  454. }
  455. /* adjust buffer len; it may be larger than we need */
  456. ci->i_xattrs.prealloc_blob->vec.iov_len =
  457. dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
  458. if (ci->i_xattrs.blob)
  459. ceph_buffer_put(ci->i_xattrs.blob);
  460. ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  461. ci->i_xattrs.prealloc_blob = NULL;
  462. ci->i_xattrs.dirty = false;
  463. ci->i_xattrs.version++;
  464. }
  465. }
  466. ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
  467. size_t size)
  468. {
  469. struct inode *inode = dentry->d_inode;
  470. struct ceph_inode_info *ci = ceph_inode(inode);
  471. int err;
  472. struct ceph_inode_xattr *xattr;
  473. struct ceph_vxattr *vxattr = NULL;
  474. if (!ceph_is_valid_xattr(name))
  475. return -ENODATA;
  476. /* let's see if a virtual xattr was requested */
  477. vxattr = ceph_match_vxattr(inode, name);
  478. spin_lock(&ci->i_ceph_lock);
  479. dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
  480. ci->i_xattrs.version, ci->i_xattrs.index_version);
  481. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  482. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  483. goto get_xattr;
  484. } else {
  485. spin_unlock(&ci->i_ceph_lock);
  486. /* get xattrs from mds (if we don't already have them) */
  487. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  488. if (err)
  489. return err;
  490. }
  491. spin_lock(&ci->i_ceph_lock);
  492. if (vxattr && vxattr->readonly) {
  493. err = vxattr->getxattr_cb(ci, value, size);
  494. goto out;
  495. }
  496. err = __build_xattrs(inode);
  497. if (err < 0)
  498. goto out;
  499. get_xattr:
  500. err = -ENODATA; /* == ENOATTR */
  501. xattr = __get_xattr(ci, name);
  502. if (!xattr) {
  503. if (vxattr)
  504. err = vxattr->getxattr_cb(ci, value, size);
  505. goto out;
  506. }
  507. err = -ERANGE;
  508. if (size && size < xattr->val_len)
  509. goto out;
  510. err = xattr->val_len;
  511. if (size == 0)
  512. goto out;
  513. memcpy(value, xattr->val, xattr->val_len);
  514. out:
  515. spin_unlock(&ci->i_ceph_lock);
  516. return err;
  517. }
  518. ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  519. {
  520. struct inode *inode = dentry->d_inode;
  521. struct ceph_inode_info *ci = ceph_inode(inode);
  522. struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
  523. u32 vir_namelen = 0;
  524. u32 namelen;
  525. int err;
  526. u32 len;
  527. int i;
  528. spin_lock(&ci->i_ceph_lock);
  529. dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
  530. ci->i_xattrs.version, ci->i_xattrs.index_version);
  531. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  532. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  533. goto list_xattr;
  534. } else {
  535. spin_unlock(&ci->i_ceph_lock);
  536. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  537. if (err)
  538. return err;
  539. }
  540. spin_lock(&ci->i_ceph_lock);
  541. err = __build_xattrs(inode);
  542. if (err < 0)
  543. goto out;
  544. list_xattr:
  545. /*
  546. * Start with virtual dir xattr names (if any) (including
  547. * terminating '\0' characters for each).
  548. */
  549. vir_namelen = ceph_vxattrs_name_size(vxattrs);
  550. /* adding 1 byte per each variable due to the null termination */
  551. namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
  552. err = -ERANGE;
  553. if (size && namelen > size)
  554. goto out;
  555. err = namelen;
  556. if (size == 0)
  557. goto out;
  558. names = __copy_xattr_names(ci, names);
  559. /* virtual xattr names, too */
  560. if (vxattrs)
  561. for (i = 0; vxattrs[i].name; i++) {
  562. len = sprintf(names, "%s", vxattrs[i].name);
  563. names += len + 1;
  564. }
  565. out:
  566. spin_unlock(&ci->i_ceph_lock);
  567. return err;
  568. }
  569. static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
  570. const char *value, size_t size, int flags)
  571. {
  572. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  573. struct inode *inode = dentry->d_inode;
  574. struct ceph_inode_info *ci = ceph_inode(inode);
  575. struct inode *parent_inode;
  576. struct ceph_mds_request *req;
  577. struct ceph_mds_client *mdsc = fsc->mdsc;
  578. int err;
  579. int i, nr_pages;
  580. struct page **pages = NULL;
  581. void *kaddr;
  582. /* copy value into some pages */
  583. nr_pages = calc_pages_for(0, size);
  584. if (nr_pages) {
  585. pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
  586. if (!pages)
  587. return -ENOMEM;
  588. err = -ENOMEM;
  589. for (i = 0; i < nr_pages; i++) {
  590. pages[i] = __page_cache_alloc(GFP_NOFS);
  591. if (!pages[i]) {
  592. nr_pages = i;
  593. goto out;
  594. }
  595. kaddr = kmap(pages[i]);
  596. memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
  597. min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
  598. }
  599. }
  600. dout("setxattr value=%.*s\n", (int)size, value);
  601. /* do request */
  602. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
  603. USE_AUTH_MDS);
  604. if (IS_ERR(req)) {
  605. err = PTR_ERR(req);
  606. goto out;
  607. }
  608. req->r_inode = inode;
  609. ihold(inode);
  610. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  611. req->r_num_caps = 1;
  612. req->r_args.setxattr.flags = cpu_to_le32(flags);
  613. req->r_path2 = kstrdup(name, GFP_NOFS);
  614. req->r_pages = pages;
  615. req->r_num_pages = nr_pages;
  616. req->r_data_len = size;
  617. dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
  618. parent_inode = ceph_get_dentry_parent_inode(dentry);
  619. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  620. iput(parent_inode);
  621. ceph_mdsc_put_request(req);
  622. dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
  623. out:
  624. if (pages) {
  625. for (i = 0; i < nr_pages; i++)
  626. __free_page(pages[i]);
  627. kfree(pages);
  628. }
  629. return err;
  630. }
  631. int ceph_setxattr(struct dentry *dentry, const char *name,
  632. const void *value, size_t size, int flags)
  633. {
  634. struct inode *inode = dentry->d_inode;
  635. struct ceph_vxattr *vxattr;
  636. struct ceph_inode_info *ci = ceph_inode(inode);
  637. int issued;
  638. int err;
  639. int dirty;
  640. int name_len = strlen(name);
  641. int val_len = size;
  642. char *newname = NULL;
  643. char *newval = NULL;
  644. struct ceph_inode_xattr *xattr = NULL;
  645. int required_blob_size;
  646. if (ceph_snap(inode) != CEPH_NOSNAP)
  647. return -EROFS;
  648. if (!ceph_is_valid_xattr(name))
  649. return -EOPNOTSUPP;
  650. vxattr = ceph_match_vxattr(inode, name);
  651. if (vxattr && vxattr->readonly)
  652. return -EOPNOTSUPP;
  653. /* pass any unhandled ceph.* xattrs through to the MDS */
  654. if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
  655. goto do_sync_unlocked;
  656. /* preallocate memory for xattr name, value, index node */
  657. err = -ENOMEM;
  658. newname = kmemdup(name, name_len + 1, GFP_NOFS);
  659. if (!newname)
  660. goto out;
  661. if (val_len) {
  662. newval = kmemdup(value, val_len, GFP_NOFS);
  663. if (!newval)
  664. goto out;
  665. }
  666. xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  667. if (!xattr)
  668. goto out;
  669. spin_lock(&ci->i_ceph_lock);
  670. retry:
  671. issued = __ceph_caps_issued(ci, NULL);
  672. dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
  673. if (!(issued & CEPH_CAP_XATTR_EXCL))
  674. goto do_sync;
  675. __build_xattrs(inode);
  676. required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  677. if (!ci->i_xattrs.prealloc_blob ||
  678. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  679. struct ceph_buffer *blob;
  680. spin_unlock(&ci->i_ceph_lock);
  681. dout(" preaallocating new blob size=%d\n", required_blob_size);
  682. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  683. if (!blob)
  684. goto out;
  685. spin_lock(&ci->i_ceph_lock);
  686. if (ci->i_xattrs.prealloc_blob)
  687. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  688. ci->i_xattrs.prealloc_blob = blob;
  689. goto retry;
  690. }
  691. err = __set_xattr(ci, newname, name_len, newval,
  692. val_len, 1, 1, 1, &xattr);
  693. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  694. ci->i_xattrs.dirty = true;
  695. inode->i_ctime = CURRENT_TIME;
  696. spin_unlock(&ci->i_ceph_lock);
  697. if (dirty)
  698. __mark_inode_dirty(inode, dirty);
  699. return err;
  700. do_sync:
  701. spin_unlock(&ci->i_ceph_lock);
  702. do_sync_unlocked:
  703. err = ceph_sync_setxattr(dentry, name, value, size, flags);
  704. out:
  705. kfree(newname);
  706. kfree(newval);
  707. kfree(xattr);
  708. return err;
  709. }
  710. static int ceph_send_removexattr(struct dentry *dentry, const char *name)
  711. {
  712. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  713. struct ceph_mds_client *mdsc = fsc->mdsc;
  714. struct inode *inode = dentry->d_inode;
  715. struct inode *parent_inode;
  716. struct ceph_mds_request *req;
  717. int err;
  718. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
  719. USE_AUTH_MDS);
  720. if (IS_ERR(req))
  721. return PTR_ERR(req);
  722. req->r_inode = inode;
  723. ihold(inode);
  724. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  725. req->r_num_caps = 1;
  726. req->r_path2 = kstrdup(name, GFP_NOFS);
  727. parent_inode = ceph_get_dentry_parent_inode(dentry);
  728. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  729. iput(parent_inode);
  730. ceph_mdsc_put_request(req);
  731. return err;
  732. }
  733. int ceph_removexattr(struct dentry *dentry, const char *name)
  734. {
  735. struct inode *inode = dentry->d_inode;
  736. struct ceph_vxattr *vxattr;
  737. struct ceph_inode_info *ci = ceph_inode(inode);
  738. int issued;
  739. int err;
  740. int required_blob_size;
  741. int dirty;
  742. if (ceph_snap(inode) != CEPH_NOSNAP)
  743. return -EROFS;
  744. if (!ceph_is_valid_xattr(name))
  745. return -EOPNOTSUPP;
  746. vxattr = ceph_match_vxattr(inode, name);
  747. if (vxattr && vxattr->readonly)
  748. return -EOPNOTSUPP;
  749. err = -ENOMEM;
  750. spin_lock(&ci->i_ceph_lock);
  751. retry:
  752. issued = __ceph_caps_issued(ci, NULL);
  753. dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
  754. if (!(issued & CEPH_CAP_XATTR_EXCL))
  755. goto do_sync;
  756. __build_xattrs(inode);
  757. required_blob_size = __get_required_blob_size(ci, 0, 0);
  758. if (!ci->i_xattrs.prealloc_blob ||
  759. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  760. struct ceph_buffer *blob;
  761. spin_unlock(&ci->i_ceph_lock);
  762. dout(" preaallocating new blob size=%d\n", required_blob_size);
  763. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  764. if (!blob)
  765. goto out;
  766. spin_lock(&ci->i_ceph_lock);
  767. if (ci->i_xattrs.prealloc_blob)
  768. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  769. ci->i_xattrs.prealloc_blob = blob;
  770. goto retry;
  771. }
  772. err = __remove_xattr_by_name(ceph_inode(inode), name);
  773. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  774. ci->i_xattrs.dirty = true;
  775. inode->i_ctime = CURRENT_TIME;
  776. spin_unlock(&ci->i_ceph_lock);
  777. if (dirty)
  778. __mark_inode_dirty(inode, dirty);
  779. return err;
  780. do_sync:
  781. spin_unlock(&ci->i_ceph_lock);
  782. err = ceph_send_removexattr(dentry, name);
  783. out:
  784. return err;
  785. }