xattr.c 22 KB

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