xattr.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 c;
  192. p = &ci->i_xattrs.index.rb_node;
  193. while (*p) {
  194. parent = *p;
  195. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  196. c = strncmp(name, xattr->name, xattr->name_len);
  197. if (c < 0)
  198. p = &(*p)->rb_left;
  199. else if (c > 0)
  200. p = &(*p)->rb_right;
  201. else {
  202. dout("__get_xattr %s: found %.*s\n", name,
  203. xattr->val_len, xattr->val);
  204. return xattr;
  205. }
  206. }
  207. dout("__get_xattr %s: not found\n", name);
  208. return NULL;
  209. }
  210. static void __free_xattr(struct ceph_inode_xattr *xattr)
  211. {
  212. BUG_ON(!xattr);
  213. if (xattr->should_free_name)
  214. kfree((void *)xattr->name);
  215. if (xattr->should_free_val)
  216. kfree((void *)xattr->val);
  217. kfree(xattr);
  218. }
  219. static int __remove_xattr(struct ceph_inode_info *ci,
  220. struct ceph_inode_xattr *xattr)
  221. {
  222. if (!xattr)
  223. return -EOPNOTSUPP;
  224. rb_erase(&xattr->node, &ci->i_xattrs.index);
  225. if (xattr->should_free_name)
  226. kfree((void *)xattr->name);
  227. if (xattr->should_free_val)
  228. kfree((void *)xattr->val);
  229. ci->i_xattrs.names_size -= xattr->name_len;
  230. ci->i_xattrs.vals_size -= xattr->val_len;
  231. ci->i_xattrs.count--;
  232. kfree(xattr);
  233. return 0;
  234. }
  235. static int __remove_xattr_by_name(struct ceph_inode_info *ci,
  236. const char *name)
  237. {
  238. struct rb_node **p;
  239. struct ceph_inode_xattr *xattr;
  240. int err;
  241. p = &ci->i_xattrs.index.rb_node;
  242. xattr = __get_xattr(ci, name);
  243. err = __remove_xattr(ci, xattr);
  244. return err;
  245. }
  246. static char *__copy_xattr_names(struct ceph_inode_info *ci,
  247. char *dest)
  248. {
  249. struct rb_node *p;
  250. struct ceph_inode_xattr *xattr = NULL;
  251. p = rb_first(&ci->i_xattrs.index);
  252. dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
  253. while (p) {
  254. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  255. memcpy(dest, xattr->name, xattr->name_len);
  256. dest[xattr->name_len] = '\0';
  257. dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
  258. xattr->name_len, ci->i_xattrs.names_size);
  259. dest += xattr->name_len + 1;
  260. p = rb_next(p);
  261. }
  262. return dest;
  263. }
  264. void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  265. {
  266. struct rb_node *p, *tmp;
  267. struct ceph_inode_xattr *xattr = NULL;
  268. p = rb_first(&ci->i_xattrs.index);
  269. dout("__ceph_destroy_xattrs p=%p\n", p);
  270. while (p) {
  271. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  272. tmp = p;
  273. p = rb_next(tmp);
  274. dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
  275. xattr->name_len, xattr->name);
  276. rb_erase(tmp, &ci->i_xattrs.index);
  277. __free_xattr(xattr);
  278. }
  279. ci->i_xattrs.names_size = 0;
  280. ci->i_xattrs.vals_size = 0;
  281. ci->i_xattrs.index_version = 0;
  282. ci->i_xattrs.count = 0;
  283. ci->i_xattrs.index = RB_ROOT;
  284. }
  285. static int __build_xattrs(struct inode *inode)
  286. __releases(inode->i_lock)
  287. __acquires(inode->i_lock)
  288. {
  289. u32 namelen;
  290. u32 numattr = 0;
  291. void *p, *end;
  292. u32 len;
  293. const char *name, *val;
  294. struct ceph_inode_info *ci = ceph_inode(inode);
  295. int xattr_version;
  296. struct ceph_inode_xattr **xattrs = NULL;
  297. int err = 0;
  298. int i;
  299. dout("__build_xattrs() len=%d\n",
  300. ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  301. if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  302. return 0; /* already built */
  303. __ceph_destroy_xattrs(ci);
  304. start:
  305. /* updated internal xattr rb tree */
  306. if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  307. p = ci->i_xattrs.blob->vec.iov_base;
  308. end = p + ci->i_xattrs.blob->vec.iov_len;
  309. ceph_decode_32_safe(&p, end, numattr, bad);
  310. xattr_version = ci->i_xattrs.version;
  311. spin_unlock(&inode->i_lock);
  312. xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *),
  313. GFP_NOFS);
  314. err = -ENOMEM;
  315. if (!xattrs)
  316. goto bad_lock;
  317. memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *));
  318. for (i = 0; i < numattr; i++) {
  319. xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  320. GFP_NOFS);
  321. if (!xattrs[i])
  322. goto bad_lock;
  323. }
  324. spin_lock(&inode->i_lock);
  325. if (ci->i_xattrs.version != xattr_version) {
  326. /* lost a race, retry */
  327. for (i = 0; i < numattr; i++)
  328. kfree(xattrs[i]);
  329. kfree(xattrs);
  330. goto start;
  331. }
  332. err = -EIO;
  333. while (numattr--) {
  334. ceph_decode_32_safe(&p, end, len, bad);
  335. namelen = len;
  336. name = p;
  337. p += len;
  338. ceph_decode_32_safe(&p, end, len, bad);
  339. val = p;
  340. p += len;
  341. err = __set_xattr(ci, name, namelen, val, len,
  342. 0, 0, 0, &xattrs[numattr]);
  343. if (err < 0)
  344. goto bad;
  345. }
  346. kfree(xattrs);
  347. }
  348. ci->i_xattrs.index_version = ci->i_xattrs.version;
  349. ci->i_xattrs.dirty = false;
  350. return err;
  351. bad_lock:
  352. spin_lock(&inode->i_lock);
  353. bad:
  354. if (xattrs) {
  355. for (i = 0; i < numattr; i++)
  356. kfree(xattrs[i]);
  357. kfree(xattrs);
  358. }
  359. ci->i_xattrs.names_size = 0;
  360. return err;
  361. }
  362. static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  363. int val_size)
  364. {
  365. /*
  366. * 4 bytes for the length, and additional 4 bytes per each xattr name,
  367. * 4 bytes per each value
  368. */
  369. int size = 4 + ci->i_xattrs.count*(4 + 4) +
  370. ci->i_xattrs.names_size +
  371. ci->i_xattrs.vals_size;
  372. dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
  373. ci->i_xattrs.count, ci->i_xattrs.names_size,
  374. ci->i_xattrs.vals_size);
  375. if (name_size)
  376. size += 4 + 4 + name_size + val_size;
  377. return size;
  378. }
  379. /*
  380. * If there are dirty xattrs, reencode xattrs into the prealloc_blob
  381. * and swap into place.
  382. */
  383. void __ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  384. {
  385. struct rb_node *p;
  386. struct ceph_inode_xattr *xattr = NULL;
  387. void *dest;
  388. dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
  389. if (ci->i_xattrs.dirty) {
  390. int need = __get_required_blob_size(ci, 0, 0);
  391. BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  392. p = rb_first(&ci->i_xattrs.index);
  393. dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  394. ceph_encode_32(&dest, ci->i_xattrs.count);
  395. while (p) {
  396. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  397. ceph_encode_32(&dest, xattr->name_len);
  398. memcpy(dest, xattr->name, xattr->name_len);
  399. dest += xattr->name_len;
  400. ceph_encode_32(&dest, xattr->val_len);
  401. memcpy(dest, xattr->val, xattr->val_len);
  402. dest += xattr->val_len;
  403. p = rb_next(p);
  404. }
  405. /* adjust buffer len; it may be larger than we need */
  406. ci->i_xattrs.prealloc_blob->vec.iov_len =
  407. dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
  408. if (ci->i_xattrs.blob)
  409. ceph_buffer_put(ci->i_xattrs.blob);
  410. ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  411. ci->i_xattrs.prealloc_blob = NULL;
  412. ci->i_xattrs.dirty = false;
  413. ci->i_xattrs.version++;
  414. }
  415. }
  416. ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value,
  417. size_t size)
  418. {
  419. struct inode *inode = dentry->d_inode;
  420. struct ceph_inode_info *ci = ceph_inode(inode);
  421. struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  422. int err;
  423. struct ceph_inode_xattr *xattr;
  424. struct ceph_vxattr_cb *vxattr = NULL;
  425. if (!ceph_is_valid_xattr(name))
  426. return -ENODATA;
  427. /* let's see if a virtual xattr was requested */
  428. if (vxattrs)
  429. vxattr = ceph_match_vxattr(vxattrs, name);
  430. spin_lock(&inode->i_lock);
  431. dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
  432. ci->i_xattrs.version, ci->i_xattrs.index_version);
  433. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  434. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  435. goto get_xattr;
  436. } else {
  437. spin_unlock(&inode->i_lock);
  438. /* get xattrs from mds (if we don't already have them) */
  439. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  440. if (err)
  441. return err;
  442. }
  443. spin_lock(&inode->i_lock);
  444. if (vxattr && vxattr->readonly) {
  445. err = vxattr->getxattr_cb(ci, value, size);
  446. goto out;
  447. }
  448. err = __build_xattrs(inode);
  449. if (err < 0)
  450. goto out;
  451. get_xattr:
  452. err = -ENODATA; /* == ENOATTR */
  453. xattr = __get_xattr(ci, name);
  454. if (!xattr) {
  455. if (vxattr)
  456. err = vxattr->getxattr_cb(ci, value, size);
  457. goto out;
  458. }
  459. err = -ERANGE;
  460. if (size && size < xattr->val_len)
  461. goto out;
  462. err = xattr->val_len;
  463. if (size == 0)
  464. goto out;
  465. memcpy(value, xattr->val, xattr->val_len);
  466. out:
  467. spin_unlock(&inode->i_lock);
  468. return err;
  469. }
  470. ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  471. {
  472. struct inode *inode = dentry->d_inode;
  473. struct ceph_inode_info *ci = ceph_inode(inode);
  474. struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  475. u32 vir_namelen = 0;
  476. u32 namelen;
  477. int err;
  478. u32 len;
  479. int i;
  480. spin_lock(&inode->i_lock);
  481. dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
  482. ci->i_xattrs.version, ci->i_xattrs.index_version);
  483. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) &&
  484. (ci->i_xattrs.index_version >= ci->i_xattrs.version)) {
  485. goto list_xattr;
  486. } else {
  487. spin_unlock(&inode->i_lock);
  488. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR);
  489. if (err)
  490. return err;
  491. }
  492. spin_lock(&inode->i_lock);
  493. err = __build_xattrs(inode);
  494. if (err < 0)
  495. goto out;
  496. list_xattr:
  497. vir_namelen = 0;
  498. /* include virtual dir xattrs */
  499. if (vxattrs)
  500. for (i = 0; vxattrs[i].name; i++)
  501. vir_namelen += strlen(vxattrs[i].name) + 1;
  502. /* adding 1 byte per each variable due to the null termination */
  503. namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count;
  504. err = -ERANGE;
  505. if (size && namelen > size)
  506. goto out;
  507. err = namelen;
  508. if (size == 0)
  509. goto out;
  510. names = __copy_xattr_names(ci, names);
  511. /* virtual xattr names, too */
  512. if (vxattrs)
  513. for (i = 0; vxattrs[i].name; i++) {
  514. len = sprintf(names, "%s", vxattrs[i].name);
  515. names += len + 1;
  516. }
  517. out:
  518. spin_unlock(&inode->i_lock);
  519. return err;
  520. }
  521. static int ceph_sync_setxattr(struct dentry *dentry, const char *name,
  522. const char *value, size_t size, int flags)
  523. {
  524. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  525. struct inode *inode = dentry->d_inode;
  526. struct ceph_inode_info *ci = ceph_inode(inode);
  527. struct inode *parent_inode = dentry->d_parent->d_inode;
  528. struct ceph_mds_request *req;
  529. struct ceph_mds_client *mdsc = fsc->mdsc;
  530. int err;
  531. int i, nr_pages;
  532. struct page **pages = NULL;
  533. void *kaddr;
  534. /* copy value into some pages */
  535. nr_pages = calc_pages_for(0, size);
  536. if (nr_pages) {
  537. pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS);
  538. if (!pages)
  539. return -ENOMEM;
  540. err = -ENOMEM;
  541. for (i = 0; i < nr_pages; i++) {
  542. pages[i] = __page_cache_alloc(GFP_NOFS);
  543. if (!pages[i]) {
  544. nr_pages = i;
  545. goto out;
  546. }
  547. kaddr = kmap(pages[i]);
  548. memcpy(kaddr, value + i*PAGE_CACHE_SIZE,
  549. min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE));
  550. }
  551. }
  552. dout("setxattr value=%.*s\n", (int)size, value);
  553. /* do request */
  554. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR,
  555. USE_AUTH_MDS);
  556. if (IS_ERR(req)) {
  557. err = PTR_ERR(req);
  558. goto out;
  559. }
  560. req->r_inode = igrab(inode);
  561. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  562. req->r_num_caps = 1;
  563. req->r_args.setxattr.flags = cpu_to_le32(flags);
  564. req->r_path2 = kstrdup(name, GFP_NOFS);
  565. req->r_pages = pages;
  566. req->r_num_pages = nr_pages;
  567. req->r_data_len = size;
  568. dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
  569. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  570. ceph_mdsc_put_request(req);
  571. dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
  572. out:
  573. if (pages) {
  574. for (i = 0; i < nr_pages; i++)
  575. __free_page(pages[i]);
  576. kfree(pages);
  577. }
  578. return err;
  579. }
  580. int ceph_setxattr(struct dentry *dentry, const char *name,
  581. const void *value, size_t size, int flags)
  582. {
  583. struct inode *inode = dentry->d_inode;
  584. struct ceph_inode_info *ci = ceph_inode(inode);
  585. struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  586. int err;
  587. int name_len = strlen(name);
  588. int val_len = size;
  589. char *newname = NULL;
  590. char *newval = NULL;
  591. struct ceph_inode_xattr *xattr = NULL;
  592. int issued;
  593. int required_blob_size;
  594. if (ceph_snap(inode) != CEPH_NOSNAP)
  595. return -EROFS;
  596. if (!ceph_is_valid_xattr(name))
  597. return -EOPNOTSUPP;
  598. if (vxattrs) {
  599. struct ceph_vxattr_cb *vxattr =
  600. ceph_match_vxattr(vxattrs, name);
  601. if (vxattr && vxattr->readonly)
  602. return -EOPNOTSUPP;
  603. }
  604. /* preallocate memory for xattr name, value, index node */
  605. err = -ENOMEM;
  606. newname = kmemdup(name, name_len + 1, GFP_NOFS);
  607. if (!newname)
  608. goto out;
  609. if (val_len) {
  610. newval = kmalloc(val_len + 1, GFP_NOFS);
  611. if (!newval)
  612. goto out;
  613. memcpy(newval, value, val_len);
  614. newval[val_len] = '\0';
  615. }
  616. xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  617. if (!xattr)
  618. goto out;
  619. spin_lock(&inode->i_lock);
  620. retry:
  621. issued = __ceph_caps_issued(ci, NULL);
  622. if (!(issued & CEPH_CAP_XATTR_EXCL))
  623. goto do_sync;
  624. __build_xattrs(inode);
  625. required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  626. if (!ci->i_xattrs.prealloc_blob ||
  627. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  628. struct ceph_buffer *blob = NULL;
  629. spin_unlock(&inode->i_lock);
  630. dout(" preaallocating new blob size=%d\n", required_blob_size);
  631. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  632. if (!blob)
  633. goto out;
  634. spin_lock(&inode->i_lock);
  635. if (ci->i_xattrs.prealloc_blob)
  636. ceph_buffer_put(ci->i_xattrs.prealloc_blob);
  637. ci->i_xattrs.prealloc_blob = blob;
  638. goto retry;
  639. }
  640. dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
  641. err = __set_xattr(ci, newname, name_len, newval,
  642. val_len, 1, 1, 1, &xattr);
  643. __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  644. ci->i_xattrs.dirty = true;
  645. inode->i_ctime = CURRENT_TIME;
  646. spin_unlock(&inode->i_lock);
  647. return err;
  648. do_sync:
  649. spin_unlock(&inode->i_lock);
  650. err = ceph_sync_setxattr(dentry, name, value, size, flags);
  651. out:
  652. kfree(newname);
  653. kfree(newval);
  654. kfree(xattr);
  655. return err;
  656. }
  657. static int ceph_send_removexattr(struct dentry *dentry, const char *name)
  658. {
  659. struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
  660. struct ceph_mds_client *mdsc = fsc->mdsc;
  661. struct inode *inode = dentry->d_inode;
  662. struct inode *parent_inode = dentry->d_parent->d_inode;
  663. struct ceph_mds_request *req;
  664. int err;
  665. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR,
  666. USE_AUTH_MDS);
  667. if (IS_ERR(req))
  668. return PTR_ERR(req);
  669. req->r_inode = igrab(inode);
  670. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  671. req->r_num_caps = 1;
  672. req->r_path2 = kstrdup(name, GFP_NOFS);
  673. err = ceph_mdsc_do_request(mdsc, parent_inode, req);
  674. ceph_mdsc_put_request(req);
  675. return err;
  676. }
  677. int ceph_removexattr(struct dentry *dentry, const char *name)
  678. {
  679. struct inode *inode = dentry->d_inode;
  680. struct ceph_inode_info *ci = ceph_inode(inode);
  681. struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode);
  682. int issued;
  683. int err;
  684. if (ceph_snap(inode) != CEPH_NOSNAP)
  685. return -EROFS;
  686. if (!ceph_is_valid_xattr(name))
  687. return -EOPNOTSUPP;
  688. if (vxattrs) {
  689. struct ceph_vxattr_cb *vxattr =
  690. ceph_match_vxattr(vxattrs, name);
  691. if (vxattr && vxattr->readonly)
  692. return -EOPNOTSUPP;
  693. }
  694. spin_lock(&inode->i_lock);
  695. __build_xattrs(inode);
  696. issued = __ceph_caps_issued(ci, NULL);
  697. dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued));
  698. if (!(issued & CEPH_CAP_XATTR_EXCL))
  699. goto do_sync;
  700. err = __remove_xattr_by_name(ceph_inode(inode), name);
  701. __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL);
  702. ci->i_xattrs.dirty = true;
  703. inode->i_ctime = CURRENT_TIME;
  704. spin_unlock(&inode->i_lock);
  705. return err;
  706. do_sync:
  707. spin_unlock(&inode->i_lock);
  708. err = ceph_send_removexattr(dentry, name);
  709. return err;
  710. }