xattr.c 38 KB

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