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