xattr.c 20 KB

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