xattr.c 36 KB

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