xattr.c 21 KB

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