xattr.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/ceph/pagelist.h>
  4. #include "super.h"
  5. #include "mds_client.h"
  6. #include <linux/ceph/decode.h>
  7. #include <linux/xattr.h>
  8. #include <linux/posix_acl_xattr.h>
  9. #include <linux/slab.h>
  10. #define XATTR_CEPH_PREFIX "ceph."
  11. #define XATTR_CEPH_PREFIX_LEN (sizeof (XATTR_CEPH_PREFIX) - 1)
  12. static int __remove_xattr(struct ceph_inode_info *ci,
  13. struct ceph_inode_xattr *xattr);
  14. static const struct xattr_handler ceph_other_xattr_handler;
  15. /*
  16. * List of handlers for synthetic system.* attributes. Other
  17. * attributes are handled directly.
  18. */
  19. const struct xattr_handler *ceph_xattr_handlers[] = {
  20. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  21. &posix_acl_access_xattr_handler,
  22. &posix_acl_default_xattr_handler,
  23. #endif
  24. &ceph_other_xattr_handler,
  25. NULL,
  26. };
  27. static bool ceph_is_valid_xattr(const char *name)
  28. {
  29. return !strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN) ||
  30. !strncmp(name, XATTR_SECURITY_PREFIX,
  31. XATTR_SECURITY_PREFIX_LEN) ||
  32. !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
  33. !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  34. }
  35. /*
  36. * These define virtual xattrs exposing the recursive directory
  37. * statistics and layout metadata.
  38. */
  39. struct ceph_vxattr {
  40. char *name;
  41. size_t name_size; /* strlen(name) + 1 (for '\0') */
  42. size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val,
  43. size_t size);
  44. bool (*exists_cb)(struct ceph_inode_info *ci);
  45. unsigned int flags;
  46. };
  47. #define VXATTR_FLAG_READONLY (1<<0)
  48. #define VXATTR_FLAG_HIDDEN (1<<1)
  49. #define VXATTR_FLAG_RSTAT (1<<2)
  50. /* layouts */
  51. static bool ceph_vxattrcb_layout_exists(struct ceph_inode_info *ci)
  52. {
  53. struct ceph_file_layout *fl = &ci->i_layout;
  54. return (fl->stripe_unit > 0 || fl->stripe_count > 0 ||
  55. fl->object_size > 0 || fl->pool_id >= 0 ||
  56. rcu_dereference_raw(fl->pool_ns) != NULL);
  57. }
  58. static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val,
  59. size_t size)
  60. {
  61. struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  62. struct ceph_osd_client *osdc = &fsc->client->osdc;
  63. struct ceph_string *pool_ns;
  64. s64 pool = ci->i_layout.pool_id;
  65. const char *pool_name;
  66. const char *ns_field = " pool_namespace=";
  67. char buf[128];
  68. size_t len, total_len = 0;
  69. ssize_t ret;
  70. pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
  71. dout("ceph_vxattrcb_layout %p\n", &ci->vfs_inode);
  72. down_read(&osdc->lock);
  73. pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
  74. if (pool_name) {
  75. len = snprintf(buf, sizeof(buf),
  76. "stripe_unit=%u stripe_count=%u object_size=%u pool=",
  77. ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
  78. ci->i_layout.object_size);
  79. total_len = len + strlen(pool_name);
  80. } else {
  81. len = snprintf(buf, sizeof(buf),
  82. "stripe_unit=%u stripe_count=%u object_size=%u pool=%lld",
  83. ci->i_layout.stripe_unit, ci->i_layout.stripe_count,
  84. ci->i_layout.object_size, (unsigned long long)pool);
  85. total_len = len;
  86. }
  87. if (pool_ns)
  88. total_len += strlen(ns_field) + pool_ns->len;
  89. ret = total_len;
  90. if (size >= total_len) {
  91. memcpy(val, buf, len);
  92. ret = len;
  93. if (pool_name) {
  94. len = strlen(pool_name);
  95. memcpy(val + ret, pool_name, len);
  96. ret += len;
  97. }
  98. if (pool_ns) {
  99. len = strlen(ns_field);
  100. memcpy(val + ret, ns_field, len);
  101. ret += len;
  102. memcpy(val + ret, pool_ns->str, pool_ns->len);
  103. ret += pool_ns->len;
  104. }
  105. }
  106. up_read(&osdc->lock);
  107. ceph_put_string(pool_ns);
  108. return ret;
  109. }
  110. static size_t ceph_vxattrcb_layout_stripe_unit(struct ceph_inode_info *ci,
  111. char *val, size_t size)
  112. {
  113. return snprintf(val, size, "%u", ci->i_layout.stripe_unit);
  114. }
  115. static size_t ceph_vxattrcb_layout_stripe_count(struct ceph_inode_info *ci,
  116. char *val, size_t size)
  117. {
  118. return snprintf(val, size, "%u", ci->i_layout.stripe_count);
  119. }
  120. static size_t ceph_vxattrcb_layout_object_size(struct ceph_inode_info *ci,
  121. char *val, size_t size)
  122. {
  123. return snprintf(val, size, "%u", ci->i_layout.object_size);
  124. }
  125. static size_t ceph_vxattrcb_layout_pool(struct ceph_inode_info *ci,
  126. char *val, size_t size)
  127. {
  128. int ret;
  129. struct ceph_fs_client *fsc = ceph_sb_to_client(ci->vfs_inode.i_sb);
  130. struct ceph_osd_client *osdc = &fsc->client->osdc;
  131. s64 pool = ci->i_layout.pool_id;
  132. const char *pool_name;
  133. down_read(&osdc->lock);
  134. pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, pool);
  135. if (pool_name)
  136. ret = snprintf(val, size, "%s", pool_name);
  137. else
  138. ret = snprintf(val, size, "%lld", (unsigned long long)pool);
  139. up_read(&osdc->lock);
  140. return ret;
  141. }
  142. static size_t ceph_vxattrcb_layout_pool_namespace(struct ceph_inode_info *ci,
  143. char *val, size_t size)
  144. {
  145. int ret = 0;
  146. struct ceph_string *ns = ceph_try_get_string(ci->i_layout.pool_ns);
  147. if (ns) {
  148. ret = snprintf(val, size, "%.*s", (int)ns->len, ns->str);
  149. ceph_put_string(ns);
  150. }
  151. return ret;
  152. }
  153. /* directories */
  154. static size_t ceph_vxattrcb_dir_entries(struct ceph_inode_info *ci, char *val,
  155. size_t size)
  156. {
  157. return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs);
  158. }
  159. static size_t ceph_vxattrcb_dir_files(struct ceph_inode_info *ci, char *val,
  160. size_t size)
  161. {
  162. return snprintf(val, size, "%lld", ci->i_files);
  163. }
  164. static size_t ceph_vxattrcb_dir_subdirs(struct ceph_inode_info *ci, char *val,
  165. size_t size)
  166. {
  167. return snprintf(val, size, "%lld", ci->i_subdirs);
  168. }
  169. static size_t ceph_vxattrcb_dir_rentries(struct ceph_inode_info *ci, char *val,
  170. size_t size)
  171. {
  172. return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs);
  173. }
  174. static size_t ceph_vxattrcb_dir_rfiles(struct ceph_inode_info *ci, char *val,
  175. size_t size)
  176. {
  177. return snprintf(val, size, "%lld", ci->i_rfiles);
  178. }
  179. static size_t ceph_vxattrcb_dir_rsubdirs(struct ceph_inode_info *ci, char *val,
  180. size_t size)
  181. {
  182. return snprintf(val, size, "%lld", ci->i_rsubdirs);
  183. }
  184. static size_t ceph_vxattrcb_dir_rbytes(struct ceph_inode_info *ci, char *val,
  185. size_t size)
  186. {
  187. return snprintf(val, size, "%lld", ci->i_rbytes);
  188. }
  189. static size_t ceph_vxattrcb_dir_rctime(struct ceph_inode_info *ci, char *val,
  190. size_t size)
  191. {
  192. return snprintf(val, size, "%lld.%09ld", ci->i_rctime.tv_sec,
  193. ci->i_rctime.tv_nsec);
  194. }
  195. /* quotas */
  196. static bool ceph_vxattrcb_quota_exists(struct ceph_inode_info *ci)
  197. {
  198. bool ret = false;
  199. spin_lock(&ci->i_ceph_lock);
  200. if ((ci->i_max_files || ci->i_max_bytes) &&
  201. ci->i_vino.snap == CEPH_NOSNAP &&
  202. ci->i_snap_realm &&
  203. ci->i_snap_realm->ino == ci->i_vino.ino)
  204. ret = true;
  205. spin_unlock(&ci->i_ceph_lock);
  206. return ret;
  207. }
  208. static size_t ceph_vxattrcb_quota(struct ceph_inode_info *ci, char *val,
  209. size_t size)
  210. {
  211. return snprintf(val, size, "max_bytes=%llu max_files=%llu",
  212. ci->i_max_bytes, ci->i_max_files);
  213. }
  214. static size_t ceph_vxattrcb_quota_max_bytes(struct ceph_inode_info *ci,
  215. char *val, size_t size)
  216. {
  217. return snprintf(val, size, "%llu", ci->i_max_bytes);
  218. }
  219. static size_t ceph_vxattrcb_quota_max_files(struct ceph_inode_info *ci,
  220. char *val, size_t size)
  221. {
  222. return snprintf(val, size, "%llu", ci->i_max_files);
  223. }
  224. #define CEPH_XATTR_NAME(_type, _name) XATTR_CEPH_PREFIX #_type "." #_name
  225. #define CEPH_XATTR_NAME2(_type, _name, _name2) \
  226. XATTR_CEPH_PREFIX #_type "." #_name "." #_name2
  227. #define XATTR_NAME_CEPH(_type, _name, _flags) \
  228. { \
  229. .name = CEPH_XATTR_NAME(_type, _name), \
  230. .name_size = sizeof (CEPH_XATTR_NAME(_type, _name)), \
  231. .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
  232. .exists_cb = NULL, \
  233. .flags = (VXATTR_FLAG_READONLY | _flags), \
  234. }
  235. #define XATTR_RSTAT_FIELD(_type, _name) \
  236. XATTR_NAME_CEPH(_type, _name, VXATTR_FLAG_RSTAT)
  237. #define XATTR_LAYOUT_FIELD(_type, _name, _field) \
  238. { \
  239. .name = CEPH_XATTR_NAME2(_type, _name, _field), \
  240. .name_size = sizeof (CEPH_XATTR_NAME2(_type, _name, _field)), \
  241. .getxattr_cb = ceph_vxattrcb_ ## _name ## _ ## _field, \
  242. .exists_cb = ceph_vxattrcb_layout_exists, \
  243. .flags = VXATTR_FLAG_HIDDEN, \
  244. }
  245. #define XATTR_QUOTA_FIELD(_type, _name) \
  246. { \
  247. .name = CEPH_XATTR_NAME(_type, _name), \
  248. .name_size = sizeof(CEPH_XATTR_NAME(_type, _name)), \
  249. .getxattr_cb = ceph_vxattrcb_ ## _type ## _ ## _name, \
  250. .exists_cb = ceph_vxattrcb_quota_exists, \
  251. .flags = VXATTR_FLAG_HIDDEN, \
  252. }
  253. static struct ceph_vxattr ceph_dir_vxattrs[] = {
  254. {
  255. .name = "ceph.dir.layout",
  256. .name_size = sizeof("ceph.dir.layout"),
  257. .getxattr_cb = ceph_vxattrcb_layout,
  258. .exists_cb = ceph_vxattrcb_layout_exists,
  259. .flags = VXATTR_FLAG_HIDDEN,
  260. },
  261. XATTR_LAYOUT_FIELD(dir, layout, stripe_unit),
  262. XATTR_LAYOUT_FIELD(dir, layout, stripe_count),
  263. XATTR_LAYOUT_FIELD(dir, layout, object_size),
  264. XATTR_LAYOUT_FIELD(dir, layout, pool),
  265. XATTR_LAYOUT_FIELD(dir, layout, pool_namespace),
  266. XATTR_NAME_CEPH(dir, entries, 0),
  267. XATTR_NAME_CEPH(dir, files, 0),
  268. XATTR_NAME_CEPH(dir, subdirs, 0),
  269. XATTR_RSTAT_FIELD(dir, rentries),
  270. XATTR_RSTAT_FIELD(dir, rfiles),
  271. XATTR_RSTAT_FIELD(dir, rsubdirs),
  272. XATTR_RSTAT_FIELD(dir, rbytes),
  273. XATTR_RSTAT_FIELD(dir, rctime),
  274. {
  275. .name = "ceph.quota",
  276. .name_size = sizeof("ceph.quota"),
  277. .getxattr_cb = ceph_vxattrcb_quota,
  278. .exists_cb = ceph_vxattrcb_quota_exists,
  279. .flags = VXATTR_FLAG_HIDDEN,
  280. },
  281. XATTR_QUOTA_FIELD(quota, max_bytes),
  282. XATTR_QUOTA_FIELD(quota, max_files),
  283. { .name = NULL, 0 } /* Required table terminator */
  284. };
  285. static size_t ceph_dir_vxattrs_name_size; /* total size of all names */
  286. /* files */
  287. static struct ceph_vxattr ceph_file_vxattrs[] = {
  288. {
  289. .name = "ceph.file.layout",
  290. .name_size = sizeof("ceph.file.layout"),
  291. .getxattr_cb = ceph_vxattrcb_layout,
  292. .exists_cb = ceph_vxattrcb_layout_exists,
  293. .flags = VXATTR_FLAG_HIDDEN,
  294. },
  295. XATTR_LAYOUT_FIELD(file, layout, stripe_unit),
  296. XATTR_LAYOUT_FIELD(file, layout, stripe_count),
  297. XATTR_LAYOUT_FIELD(file, layout, object_size),
  298. XATTR_LAYOUT_FIELD(file, layout, pool),
  299. XATTR_LAYOUT_FIELD(file, layout, pool_namespace),
  300. { .name = NULL, 0 } /* Required table terminator */
  301. };
  302. static size_t ceph_file_vxattrs_name_size; /* total size of all names */
  303. static struct ceph_vxattr *ceph_inode_vxattrs(struct inode *inode)
  304. {
  305. if (S_ISDIR(inode->i_mode))
  306. return ceph_dir_vxattrs;
  307. else if (S_ISREG(inode->i_mode))
  308. return ceph_file_vxattrs;
  309. return NULL;
  310. }
  311. static size_t ceph_vxattrs_name_size(struct ceph_vxattr *vxattrs)
  312. {
  313. if (vxattrs == ceph_dir_vxattrs)
  314. return ceph_dir_vxattrs_name_size;
  315. if (vxattrs == ceph_file_vxattrs)
  316. return ceph_file_vxattrs_name_size;
  317. BUG_ON(vxattrs);
  318. return 0;
  319. }
  320. /*
  321. * Compute the aggregate size (including terminating '\0') of all
  322. * virtual extended attribute names in the given vxattr table.
  323. */
  324. static size_t __init vxattrs_name_size(struct ceph_vxattr *vxattrs)
  325. {
  326. struct ceph_vxattr *vxattr;
  327. size_t size = 0;
  328. for (vxattr = vxattrs; vxattr->name; vxattr++) {
  329. if (!(vxattr->flags & VXATTR_FLAG_HIDDEN))
  330. size += vxattr->name_size;
  331. }
  332. return size;
  333. }
  334. /* Routines called at initialization and exit time */
  335. void __init ceph_xattr_init(void)
  336. {
  337. ceph_dir_vxattrs_name_size = vxattrs_name_size(ceph_dir_vxattrs);
  338. ceph_file_vxattrs_name_size = vxattrs_name_size(ceph_file_vxattrs);
  339. }
  340. void ceph_xattr_exit(void)
  341. {
  342. ceph_dir_vxattrs_name_size = 0;
  343. ceph_file_vxattrs_name_size = 0;
  344. }
  345. static struct ceph_vxattr *ceph_match_vxattr(struct inode *inode,
  346. const char *name)
  347. {
  348. struct ceph_vxattr *vxattr = ceph_inode_vxattrs(inode);
  349. if (vxattr) {
  350. while (vxattr->name) {
  351. if (!strcmp(vxattr->name, name))
  352. return vxattr;
  353. vxattr++;
  354. }
  355. }
  356. return NULL;
  357. }
  358. static int __set_xattr(struct ceph_inode_info *ci,
  359. const char *name, int name_len,
  360. const char *val, int val_len,
  361. int flags, int update_xattr,
  362. struct ceph_inode_xattr **newxattr)
  363. {
  364. struct rb_node **p;
  365. struct rb_node *parent = NULL;
  366. struct ceph_inode_xattr *xattr = NULL;
  367. int c;
  368. int new = 0;
  369. p = &ci->i_xattrs.index.rb_node;
  370. while (*p) {
  371. parent = *p;
  372. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  373. c = strncmp(name, xattr->name, min(name_len, xattr->name_len));
  374. if (c < 0)
  375. p = &(*p)->rb_left;
  376. else if (c > 0)
  377. p = &(*p)->rb_right;
  378. else {
  379. if (name_len == xattr->name_len)
  380. break;
  381. else if (name_len < xattr->name_len)
  382. p = &(*p)->rb_left;
  383. else
  384. p = &(*p)->rb_right;
  385. }
  386. xattr = NULL;
  387. }
  388. if (update_xattr) {
  389. int err = 0;
  390. if (xattr && (flags & XATTR_CREATE))
  391. err = -EEXIST;
  392. else if (!xattr && (flags & XATTR_REPLACE))
  393. err = -ENODATA;
  394. if (err) {
  395. kfree(name);
  396. kfree(val);
  397. kfree(*newxattr);
  398. return err;
  399. }
  400. if (update_xattr < 0) {
  401. if (xattr)
  402. __remove_xattr(ci, xattr);
  403. kfree(name);
  404. kfree(*newxattr);
  405. return 0;
  406. }
  407. }
  408. if (!xattr) {
  409. new = 1;
  410. xattr = *newxattr;
  411. xattr->name = name;
  412. xattr->name_len = name_len;
  413. xattr->should_free_name = update_xattr;
  414. ci->i_xattrs.count++;
  415. dout("__set_xattr count=%d\n", ci->i_xattrs.count);
  416. } else {
  417. kfree(*newxattr);
  418. *newxattr = NULL;
  419. if (xattr->should_free_val)
  420. kfree((void *)xattr->val);
  421. if (update_xattr) {
  422. kfree((void *)name);
  423. name = xattr->name;
  424. }
  425. ci->i_xattrs.names_size -= xattr->name_len;
  426. ci->i_xattrs.vals_size -= xattr->val_len;
  427. }
  428. ci->i_xattrs.names_size += name_len;
  429. ci->i_xattrs.vals_size += val_len;
  430. if (val)
  431. xattr->val = val;
  432. else
  433. xattr->val = "";
  434. xattr->val_len = val_len;
  435. xattr->dirty = update_xattr;
  436. xattr->should_free_val = (val && update_xattr);
  437. if (new) {
  438. rb_link_node(&xattr->node, parent, p);
  439. rb_insert_color(&xattr->node, &ci->i_xattrs.index);
  440. dout("__set_xattr_val p=%p\n", p);
  441. }
  442. dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n",
  443. ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val);
  444. return 0;
  445. }
  446. static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci,
  447. const char *name)
  448. {
  449. struct rb_node **p;
  450. struct rb_node *parent = NULL;
  451. struct ceph_inode_xattr *xattr = NULL;
  452. int name_len = strlen(name);
  453. int c;
  454. p = &ci->i_xattrs.index.rb_node;
  455. while (*p) {
  456. parent = *p;
  457. xattr = rb_entry(parent, struct ceph_inode_xattr, node);
  458. c = strncmp(name, xattr->name, xattr->name_len);
  459. if (c == 0 && name_len > xattr->name_len)
  460. c = 1;
  461. if (c < 0)
  462. p = &(*p)->rb_left;
  463. else if (c > 0)
  464. p = &(*p)->rb_right;
  465. else {
  466. dout("__get_xattr %s: found %.*s\n", name,
  467. xattr->val_len, xattr->val);
  468. return xattr;
  469. }
  470. }
  471. dout("__get_xattr %s: not found\n", name);
  472. return NULL;
  473. }
  474. static void __free_xattr(struct ceph_inode_xattr *xattr)
  475. {
  476. BUG_ON(!xattr);
  477. if (xattr->should_free_name)
  478. kfree((void *)xattr->name);
  479. if (xattr->should_free_val)
  480. kfree((void *)xattr->val);
  481. kfree(xattr);
  482. }
  483. static int __remove_xattr(struct ceph_inode_info *ci,
  484. struct ceph_inode_xattr *xattr)
  485. {
  486. if (!xattr)
  487. return -ENODATA;
  488. rb_erase(&xattr->node, &ci->i_xattrs.index);
  489. if (xattr->should_free_name)
  490. kfree((void *)xattr->name);
  491. if (xattr->should_free_val)
  492. kfree((void *)xattr->val);
  493. ci->i_xattrs.names_size -= xattr->name_len;
  494. ci->i_xattrs.vals_size -= xattr->val_len;
  495. ci->i_xattrs.count--;
  496. kfree(xattr);
  497. return 0;
  498. }
  499. static char *__copy_xattr_names(struct ceph_inode_info *ci,
  500. char *dest)
  501. {
  502. struct rb_node *p;
  503. struct ceph_inode_xattr *xattr = NULL;
  504. p = rb_first(&ci->i_xattrs.index);
  505. dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count);
  506. while (p) {
  507. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  508. memcpy(dest, xattr->name, xattr->name_len);
  509. dest[xattr->name_len] = '\0';
  510. dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name,
  511. xattr->name_len, ci->i_xattrs.names_size);
  512. dest += xattr->name_len + 1;
  513. p = rb_next(p);
  514. }
  515. return dest;
  516. }
  517. void __ceph_destroy_xattrs(struct ceph_inode_info *ci)
  518. {
  519. struct rb_node *p, *tmp;
  520. struct ceph_inode_xattr *xattr = NULL;
  521. p = rb_first(&ci->i_xattrs.index);
  522. dout("__ceph_destroy_xattrs p=%p\n", p);
  523. while (p) {
  524. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  525. tmp = p;
  526. p = rb_next(tmp);
  527. dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p,
  528. xattr->name_len, xattr->name);
  529. rb_erase(tmp, &ci->i_xattrs.index);
  530. __free_xattr(xattr);
  531. }
  532. ci->i_xattrs.names_size = 0;
  533. ci->i_xattrs.vals_size = 0;
  534. ci->i_xattrs.index_version = 0;
  535. ci->i_xattrs.count = 0;
  536. ci->i_xattrs.index = RB_ROOT;
  537. }
  538. static int __build_xattrs(struct inode *inode)
  539. __releases(ci->i_ceph_lock)
  540. __acquires(ci->i_ceph_lock)
  541. {
  542. u32 namelen;
  543. u32 numattr = 0;
  544. void *p, *end;
  545. u32 len;
  546. const char *name, *val;
  547. struct ceph_inode_info *ci = ceph_inode(inode);
  548. int xattr_version;
  549. struct ceph_inode_xattr **xattrs = NULL;
  550. int err = 0;
  551. int i;
  552. dout("__build_xattrs() len=%d\n",
  553. ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0);
  554. if (ci->i_xattrs.index_version >= ci->i_xattrs.version)
  555. return 0; /* already built */
  556. __ceph_destroy_xattrs(ci);
  557. start:
  558. /* updated internal xattr rb tree */
  559. if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) {
  560. p = ci->i_xattrs.blob->vec.iov_base;
  561. end = p + ci->i_xattrs.blob->vec.iov_len;
  562. ceph_decode_32_safe(&p, end, numattr, bad);
  563. xattr_version = ci->i_xattrs.version;
  564. spin_unlock(&ci->i_ceph_lock);
  565. xattrs = kcalloc(numattr, sizeof(struct ceph_inode_xattr *),
  566. GFP_NOFS);
  567. err = -ENOMEM;
  568. if (!xattrs)
  569. goto bad_lock;
  570. for (i = 0; i < numattr; i++) {
  571. xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr),
  572. GFP_NOFS);
  573. if (!xattrs[i])
  574. goto bad_lock;
  575. }
  576. spin_lock(&ci->i_ceph_lock);
  577. if (ci->i_xattrs.version != xattr_version) {
  578. /* lost a race, retry */
  579. for (i = 0; i < numattr; i++)
  580. kfree(xattrs[i]);
  581. kfree(xattrs);
  582. xattrs = NULL;
  583. goto start;
  584. }
  585. err = -EIO;
  586. while (numattr--) {
  587. ceph_decode_32_safe(&p, end, len, bad);
  588. namelen = len;
  589. name = p;
  590. p += len;
  591. ceph_decode_32_safe(&p, end, len, bad);
  592. val = p;
  593. p += len;
  594. err = __set_xattr(ci, name, namelen, val, len,
  595. 0, 0, &xattrs[numattr]);
  596. if (err < 0)
  597. goto bad;
  598. }
  599. kfree(xattrs);
  600. }
  601. ci->i_xattrs.index_version = ci->i_xattrs.version;
  602. ci->i_xattrs.dirty = false;
  603. return err;
  604. bad_lock:
  605. spin_lock(&ci->i_ceph_lock);
  606. bad:
  607. if (xattrs) {
  608. for (i = 0; i < numattr; i++)
  609. kfree(xattrs[i]);
  610. kfree(xattrs);
  611. }
  612. ci->i_xattrs.names_size = 0;
  613. return err;
  614. }
  615. static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size,
  616. int val_size)
  617. {
  618. /*
  619. * 4 bytes for the length, and additional 4 bytes per each xattr name,
  620. * 4 bytes per each value
  621. */
  622. int size = 4 + ci->i_xattrs.count*(4 + 4) +
  623. ci->i_xattrs.names_size +
  624. ci->i_xattrs.vals_size;
  625. dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n",
  626. ci->i_xattrs.count, ci->i_xattrs.names_size,
  627. ci->i_xattrs.vals_size);
  628. if (name_size)
  629. size += 4 + 4 + name_size + val_size;
  630. return size;
  631. }
  632. /*
  633. * If there are dirty xattrs, reencode xattrs into the prealloc_blob
  634. * and swap into place. It returns the old i_xattrs.blob (or NULL) so
  635. * that it can be freed by the caller as the i_ceph_lock is likely to be
  636. * held.
  637. */
  638. struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci)
  639. {
  640. struct rb_node *p;
  641. struct ceph_inode_xattr *xattr = NULL;
  642. struct ceph_buffer *old_blob = NULL;
  643. void *dest;
  644. dout("__build_xattrs_blob %p\n", &ci->vfs_inode);
  645. if (ci->i_xattrs.dirty) {
  646. int need = __get_required_blob_size(ci, 0, 0);
  647. BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len);
  648. p = rb_first(&ci->i_xattrs.index);
  649. dest = ci->i_xattrs.prealloc_blob->vec.iov_base;
  650. ceph_encode_32(&dest, ci->i_xattrs.count);
  651. while (p) {
  652. xattr = rb_entry(p, struct ceph_inode_xattr, node);
  653. ceph_encode_32(&dest, xattr->name_len);
  654. memcpy(dest, xattr->name, xattr->name_len);
  655. dest += xattr->name_len;
  656. ceph_encode_32(&dest, xattr->val_len);
  657. memcpy(dest, xattr->val, xattr->val_len);
  658. dest += xattr->val_len;
  659. p = rb_next(p);
  660. }
  661. /* adjust buffer len; it may be larger than we need */
  662. ci->i_xattrs.prealloc_blob->vec.iov_len =
  663. dest - ci->i_xattrs.prealloc_blob->vec.iov_base;
  664. if (ci->i_xattrs.blob)
  665. old_blob = ci->i_xattrs.blob;
  666. ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob;
  667. ci->i_xattrs.prealloc_blob = NULL;
  668. ci->i_xattrs.dirty = false;
  669. ci->i_xattrs.version++;
  670. }
  671. return old_blob;
  672. }
  673. static inline int __get_request_mask(struct inode *in) {
  674. struct ceph_mds_request *req = current->journal_info;
  675. int mask = 0;
  676. if (req && req->r_target_inode == in) {
  677. if (req->r_op == CEPH_MDS_OP_LOOKUP ||
  678. req->r_op == CEPH_MDS_OP_LOOKUPINO ||
  679. req->r_op == CEPH_MDS_OP_LOOKUPPARENT ||
  680. req->r_op == CEPH_MDS_OP_GETATTR) {
  681. mask = le32_to_cpu(req->r_args.getattr.mask);
  682. } else if (req->r_op == CEPH_MDS_OP_OPEN ||
  683. req->r_op == CEPH_MDS_OP_CREATE) {
  684. mask = le32_to_cpu(req->r_args.open.mask);
  685. }
  686. }
  687. return mask;
  688. }
  689. ssize_t __ceph_getxattr(struct inode *inode, const char *name, void *value,
  690. size_t size)
  691. {
  692. struct ceph_inode_info *ci = ceph_inode(inode);
  693. struct ceph_inode_xattr *xattr;
  694. struct ceph_vxattr *vxattr = NULL;
  695. int req_mask;
  696. int err;
  697. /* let's see if a virtual xattr was requested */
  698. vxattr = ceph_match_vxattr(inode, name);
  699. if (vxattr) {
  700. int mask = 0;
  701. if (vxattr->flags & VXATTR_FLAG_RSTAT)
  702. mask |= CEPH_STAT_RSTAT;
  703. err = ceph_do_getattr(inode, mask, true);
  704. if (err)
  705. return err;
  706. err = -ENODATA;
  707. if (!(vxattr->exists_cb && !vxattr->exists_cb(ci))) {
  708. err = vxattr->getxattr_cb(ci, value, size);
  709. if (size && size < err)
  710. err = -ERANGE;
  711. }
  712. return err;
  713. }
  714. req_mask = __get_request_mask(inode);
  715. spin_lock(&ci->i_ceph_lock);
  716. dout("getxattr %p ver=%lld index_ver=%lld\n", inode,
  717. ci->i_xattrs.version, ci->i_xattrs.index_version);
  718. if (ci->i_xattrs.version == 0 ||
  719. !((req_mask & CEPH_CAP_XATTR_SHARED) ||
  720. __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1))) {
  721. spin_unlock(&ci->i_ceph_lock);
  722. /* security module gets xattr while filling trace */
  723. if (current->journal_info) {
  724. pr_warn_ratelimited("sync getxattr %p "
  725. "during filling trace\n", inode);
  726. return -EBUSY;
  727. }
  728. /* get xattrs from mds (if we don't already have them) */
  729. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
  730. if (err)
  731. return err;
  732. spin_lock(&ci->i_ceph_lock);
  733. }
  734. err = __build_xattrs(inode);
  735. if (err < 0)
  736. goto out;
  737. err = -ENODATA; /* == ENOATTR */
  738. xattr = __get_xattr(ci, name);
  739. if (!xattr)
  740. goto out;
  741. err = -ERANGE;
  742. if (size && size < xattr->val_len)
  743. goto out;
  744. err = xattr->val_len;
  745. if (size == 0)
  746. goto out;
  747. memcpy(value, xattr->val, xattr->val_len);
  748. if (current->journal_info &&
  749. !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
  750. ci->i_ceph_flags |= CEPH_I_SEC_INITED;
  751. out:
  752. spin_unlock(&ci->i_ceph_lock);
  753. return err;
  754. }
  755. ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size)
  756. {
  757. struct inode *inode = d_inode(dentry);
  758. struct ceph_inode_info *ci = ceph_inode(inode);
  759. struct ceph_vxattr *vxattrs = ceph_inode_vxattrs(inode);
  760. u32 vir_namelen = 0;
  761. u32 namelen;
  762. int err;
  763. u32 len;
  764. int i;
  765. spin_lock(&ci->i_ceph_lock);
  766. dout("listxattr %p ver=%lld index_ver=%lld\n", inode,
  767. ci->i_xattrs.version, ci->i_xattrs.index_version);
  768. if (ci->i_xattrs.version == 0 ||
  769. !__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1)) {
  770. spin_unlock(&ci->i_ceph_lock);
  771. err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR, true);
  772. if (err)
  773. return err;
  774. spin_lock(&ci->i_ceph_lock);
  775. }
  776. err = __build_xattrs(inode);
  777. if (err < 0)
  778. goto out;
  779. /*
  780. * Start with virtual dir xattr names (if any) (including
  781. * terminating '\0' characters for each).
  782. */
  783. vir_namelen = ceph_vxattrs_name_size(vxattrs);
  784. /* adding 1 byte per each variable due to the null termination */
  785. namelen = ci->i_xattrs.names_size + ci->i_xattrs.count;
  786. err = -ERANGE;
  787. if (size && vir_namelen + namelen > size)
  788. goto out;
  789. err = namelen + vir_namelen;
  790. if (size == 0)
  791. goto out;
  792. names = __copy_xattr_names(ci, names);
  793. /* virtual xattr names, too */
  794. err = namelen;
  795. if (vxattrs) {
  796. for (i = 0; vxattrs[i].name; i++) {
  797. if (!(vxattrs[i].flags & VXATTR_FLAG_HIDDEN) &&
  798. !(vxattrs[i].exists_cb &&
  799. !vxattrs[i].exists_cb(ci))) {
  800. len = sprintf(names, "%s", vxattrs[i].name);
  801. names += len + 1;
  802. err += len + 1;
  803. }
  804. }
  805. }
  806. out:
  807. spin_unlock(&ci->i_ceph_lock);
  808. return err;
  809. }
  810. static int ceph_sync_setxattr(struct inode *inode, const char *name,
  811. const char *value, size_t size, int flags)
  812. {
  813. struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
  814. struct ceph_inode_info *ci = ceph_inode(inode);
  815. struct ceph_mds_request *req;
  816. struct ceph_mds_client *mdsc = fsc->mdsc;
  817. struct ceph_pagelist *pagelist = NULL;
  818. int op = CEPH_MDS_OP_SETXATTR;
  819. int err;
  820. if (size > 0) {
  821. /* copy value into pagelist */
  822. pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
  823. if (!pagelist)
  824. return -ENOMEM;
  825. ceph_pagelist_init(pagelist);
  826. err = ceph_pagelist_append(pagelist, value, size);
  827. if (err)
  828. goto out;
  829. } else if (!value) {
  830. if (flags & CEPH_XATTR_REPLACE)
  831. op = CEPH_MDS_OP_RMXATTR;
  832. else
  833. flags |= CEPH_XATTR_REMOVE;
  834. }
  835. dout("setxattr value=%.*s\n", (int)size, value);
  836. /* do request */
  837. req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
  838. if (IS_ERR(req)) {
  839. err = PTR_ERR(req);
  840. goto out;
  841. }
  842. req->r_path2 = kstrdup(name, GFP_NOFS);
  843. if (!req->r_path2) {
  844. ceph_mdsc_put_request(req);
  845. err = -ENOMEM;
  846. goto out;
  847. }
  848. if (op == CEPH_MDS_OP_SETXATTR) {
  849. req->r_args.setxattr.flags = cpu_to_le32(flags);
  850. req->r_pagelist = pagelist;
  851. pagelist = NULL;
  852. }
  853. req->r_inode = inode;
  854. ihold(inode);
  855. req->r_num_caps = 1;
  856. req->r_inode_drop = CEPH_CAP_XATTR_SHARED;
  857. dout("xattr.ver (before): %lld\n", ci->i_xattrs.version);
  858. err = ceph_mdsc_do_request(mdsc, NULL, req);
  859. ceph_mdsc_put_request(req);
  860. dout("xattr.ver (after): %lld\n", ci->i_xattrs.version);
  861. out:
  862. if (pagelist)
  863. ceph_pagelist_release(pagelist);
  864. return err;
  865. }
  866. int __ceph_setxattr(struct inode *inode, const char *name,
  867. const void *value, size_t size, int flags)
  868. {
  869. struct ceph_vxattr *vxattr;
  870. struct ceph_inode_info *ci = ceph_inode(inode);
  871. struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
  872. struct ceph_cap_flush *prealloc_cf = NULL;
  873. struct ceph_buffer *old_blob = NULL;
  874. int issued;
  875. int err;
  876. int dirty = 0;
  877. int name_len = strlen(name);
  878. int val_len = size;
  879. char *newname = NULL;
  880. char *newval = NULL;
  881. struct ceph_inode_xattr *xattr = NULL;
  882. int required_blob_size;
  883. bool check_realm = false;
  884. bool lock_snap_rwsem = false;
  885. if (ceph_snap(inode) != CEPH_NOSNAP)
  886. return -EROFS;
  887. vxattr = ceph_match_vxattr(inode, name);
  888. if (vxattr) {
  889. if (vxattr->flags & VXATTR_FLAG_READONLY)
  890. return -EOPNOTSUPP;
  891. if (value && !strncmp(vxattr->name, "ceph.quota", 10))
  892. check_realm = true;
  893. }
  894. /* pass any unhandled ceph.* xattrs through to the MDS */
  895. if (!strncmp(name, XATTR_CEPH_PREFIX, XATTR_CEPH_PREFIX_LEN))
  896. goto do_sync_unlocked;
  897. /* preallocate memory for xattr name, value, index node */
  898. err = -ENOMEM;
  899. newname = kmemdup(name, name_len + 1, GFP_NOFS);
  900. if (!newname)
  901. goto out;
  902. if (val_len) {
  903. newval = kmemdup(value, val_len, GFP_NOFS);
  904. if (!newval)
  905. goto out;
  906. }
  907. xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS);
  908. if (!xattr)
  909. goto out;
  910. prealloc_cf = ceph_alloc_cap_flush();
  911. if (!prealloc_cf)
  912. goto out;
  913. spin_lock(&ci->i_ceph_lock);
  914. retry:
  915. issued = __ceph_caps_issued(ci, NULL);
  916. if (ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL))
  917. goto do_sync;
  918. if (!lock_snap_rwsem && !ci->i_head_snapc) {
  919. lock_snap_rwsem = true;
  920. if (!down_read_trylock(&mdsc->snap_rwsem)) {
  921. spin_unlock(&ci->i_ceph_lock);
  922. down_read(&mdsc->snap_rwsem);
  923. spin_lock(&ci->i_ceph_lock);
  924. goto retry;
  925. }
  926. }
  927. dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued));
  928. __build_xattrs(inode);
  929. required_blob_size = __get_required_blob_size(ci, name_len, val_len);
  930. if (!ci->i_xattrs.prealloc_blob ||
  931. required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) {
  932. struct ceph_buffer *blob;
  933. spin_unlock(&ci->i_ceph_lock);
  934. ceph_buffer_put(old_blob); /* Shouldn't be required */
  935. dout(" pre-allocating new blob size=%d\n", required_blob_size);
  936. blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
  937. if (!blob)
  938. goto do_sync_unlocked;
  939. spin_lock(&ci->i_ceph_lock);
  940. /* prealloc_blob can't be released while holding i_ceph_lock */
  941. if (ci->i_xattrs.prealloc_blob)
  942. old_blob = ci->i_xattrs.prealloc_blob;
  943. ci->i_xattrs.prealloc_blob = blob;
  944. goto retry;
  945. }
  946. err = __set_xattr(ci, newname, name_len, newval, val_len,
  947. flags, value ? 1 : -1, &xattr);
  948. if (!err) {
  949. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL,
  950. &prealloc_cf);
  951. ci->i_xattrs.dirty = true;
  952. inode->i_ctime = current_time(inode);
  953. }
  954. spin_unlock(&ci->i_ceph_lock);
  955. ceph_buffer_put(old_blob);
  956. if (lock_snap_rwsem)
  957. up_read(&mdsc->snap_rwsem);
  958. if (dirty)
  959. __mark_inode_dirty(inode, dirty);
  960. ceph_free_cap_flush(prealloc_cf);
  961. return err;
  962. do_sync:
  963. spin_unlock(&ci->i_ceph_lock);
  964. do_sync_unlocked:
  965. if (lock_snap_rwsem)
  966. up_read(&mdsc->snap_rwsem);
  967. /* security module set xattr while filling trace */
  968. if (current->journal_info) {
  969. pr_warn_ratelimited("sync setxattr %p "
  970. "during filling trace\n", inode);
  971. err = -EBUSY;
  972. } else {
  973. err = ceph_sync_setxattr(inode, name, value, size, flags);
  974. if (err >= 0 && check_realm) {
  975. /* check if snaprealm was created for quota inode */
  976. spin_lock(&ci->i_ceph_lock);
  977. if ((ci->i_max_files || ci->i_max_bytes) &&
  978. !(ci->i_snap_realm &&
  979. ci->i_snap_realm->ino == ci->i_vino.ino))
  980. err = -EOPNOTSUPP;
  981. spin_unlock(&ci->i_ceph_lock);
  982. }
  983. }
  984. out:
  985. ceph_free_cap_flush(prealloc_cf);
  986. kfree(newname);
  987. kfree(newval);
  988. kfree(xattr);
  989. return err;
  990. }
  991. static int ceph_get_xattr_handler(const struct xattr_handler *handler,
  992. struct dentry *dentry, struct inode *inode,
  993. const char *name, void *value, size_t size)
  994. {
  995. if (!ceph_is_valid_xattr(name))
  996. return -EOPNOTSUPP;
  997. return __ceph_getxattr(inode, name, value, size);
  998. }
  999. static int ceph_set_xattr_handler(const struct xattr_handler *handler,
  1000. struct dentry *unused, struct inode *inode,
  1001. const char *name, const void *value,
  1002. size_t size, int flags)
  1003. {
  1004. if (!ceph_is_valid_xattr(name))
  1005. return -EOPNOTSUPP;
  1006. return __ceph_setxattr(inode, name, value, size, flags);
  1007. }
  1008. static const struct xattr_handler ceph_other_xattr_handler = {
  1009. .prefix = "", /* match any name => handlers called with full name */
  1010. .get = ceph_get_xattr_handler,
  1011. .set = ceph_set_xattr_handler,
  1012. };
  1013. #ifdef CONFIG_SECURITY
  1014. bool ceph_security_xattr_wanted(struct inode *in)
  1015. {
  1016. return in->i_security != NULL;
  1017. }
  1018. bool ceph_security_xattr_deadlock(struct inode *in)
  1019. {
  1020. struct ceph_inode_info *ci;
  1021. bool ret;
  1022. if (!in->i_security)
  1023. return false;
  1024. ci = ceph_inode(in);
  1025. spin_lock(&ci->i_ceph_lock);
  1026. ret = !(ci->i_ceph_flags & CEPH_I_SEC_INITED) &&
  1027. !(ci->i_xattrs.version > 0 &&
  1028. __ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0));
  1029. spin_unlock(&ci->i_ceph_lock);
  1030. return ret;
  1031. }
  1032. #endif