super.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/ctype.h>
  4. #include <linux/fs.h>
  5. #include <linux/inet.h>
  6. #include <linux/in6.h>
  7. #include <linux/module.h>
  8. #include <linux/mount.h>
  9. #include <linux/parser.h>
  10. #include <linux/sched.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/statfs.h>
  14. #include <linux/string.h>
  15. #include "super.h"
  16. #include "mds_client.h"
  17. #include "cache.h"
  18. #include <linux/ceph/ceph_features.h>
  19. #include <linux/ceph/decode.h>
  20. #include <linux/ceph/mon_client.h>
  21. #include <linux/ceph/auth.h>
  22. #include <linux/ceph/debugfs.h>
  23. /*
  24. * Ceph superblock operations
  25. *
  26. * Handle the basics of mounting, unmounting.
  27. */
  28. /*
  29. * super ops
  30. */
  31. static void ceph_put_super(struct super_block *s)
  32. {
  33. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  34. dout("put_super\n");
  35. ceph_mdsc_close_sessions(fsc->mdsc);
  36. }
  37. static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
  38. {
  39. struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
  40. struct ceph_mon_client *monc = &fsc->client->monc;
  41. struct ceph_statfs st;
  42. u64 fsid;
  43. int err;
  44. u64 data_pool;
  45. if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
  46. data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
  47. } else {
  48. data_pool = CEPH_NOPOOL;
  49. }
  50. dout("statfs\n");
  51. err = ceph_monc_do_statfs(monc, data_pool, &st);
  52. if (err < 0)
  53. return err;
  54. /* fill in kstatfs */
  55. buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
  56. /*
  57. * express utilization in terms of large blocks to avoid
  58. * overflow on 32-bit machines.
  59. *
  60. * NOTE: for the time being, we make bsize == frsize to humor
  61. * not-yet-ancient versions of glibc that are broken.
  62. * Someday, we will probably want to report a real block
  63. * size... whatever that may mean for a network file system!
  64. */
  65. buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
  66. buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
  67. /*
  68. * By default use root quota for stats; fallback to overall filesystem
  69. * usage if using 'noquotadf' mount option or if the root dir doesn't
  70. * have max_bytes quota set.
  71. */
  72. if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
  73. !ceph_quota_update_statfs(fsc, buf)) {
  74. buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
  75. buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  76. buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  77. }
  78. buf->f_files = le64_to_cpu(st.num_objects);
  79. buf->f_ffree = -1;
  80. buf->f_namelen = NAME_MAX;
  81. /* Must convert the fsid, for consistent values across arches */
  82. mutex_lock(&monc->mutex);
  83. fsid = le64_to_cpu(*(__le64 *)(&monc->monmap->fsid)) ^
  84. le64_to_cpu(*((__le64 *)&monc->monmap->fsid + 1));
  85. mutex_unlock(&monc->mutex);
  86. buf->f_fsid.val[0] = fsid & 0xffffffff;
  87. buf->f_fsid.val[1] = fsid >> 32;
  88. return 0;
  89. }
  90. static int ceph_sync_fs(struct super_block *sb, int wait)
  91. {
  92. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  93. if (!wait) {
  94. dout("sync_fs (non-blocking)\n");
  95. ceph_flush_dirty_caps(fsc->mdsc);
  96. dout("sync_fs (non-blocking) done\n");
  97. return 0;
  98. }
  99. dout("sync_fs (blocking)\n");
  100. ceph_osdc_sync(&fsc->client->osdc);
  101. ceph_mdsc_sync(fsc->mdsc);
  102. dout("sync_fs (blocking) done\n");
  103. return 0;
  104. }
  105. /*
  106. * mount options
  107. */
  108. enum {
  109. Opt_wsize,
  110. Opt_rsize,
  111. Opt_rasize,
  112. Opt_caps_wanted_delay_min,
  113. Opt_caps_wanted_delay_max,
  114. Opt_readdir_max_entries,
  115. Opt_readdir_max_bytes,
  116. Opt_congestion_kb,
  117. Opt_last_int,
  118. /* int args above */
  119. Opt_snapdirname,
  120. Opt_mds_namespace,
  121. Opt_fscache_uniq,
  122. Opt_last_string,
  123. /* string args above */
  124. Opt_dirstat,
  125. Opt_nodirstat,
  126. Opt_rbytes,
  127. Opt_norbytes,
  128. Opt_asyncreaddir,
  129. Opt_noasyncreaddir,
  130. Opt_dcache,
  131. Opt_nodcache,
  132. Opt_ino32,
  133. Opt_noino32,
  134. Opt_fscache,
  135. Opt_nofscache,
  136. Opt_poolperm,
  137. Opt_nopoolperm,
  138. Opt_require_active_mds,
  139. Opt_norequire_active_mds,
  140. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  141. Opt_acl,
  142. #endif
  143. Opt_noacl,
  144. Opt_quotadf,
  145. Opt_noquotadf,
  146. };
  147. static match_table_t fsopt_tokens = {
  148. {Opt_wsize, "wsize=%d"},
  149. {Opt_rsize, "rsize=%d"},
  150. {Opt_rasize, "rasize=%d"},
  151. {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
  152. {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
  153. {Opt_readdir_max_entries, "readdir_max_entries=%d"},
  154. {Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
  155. {Opt_congestion_kb, "write_congestion_kb=%d"},
  156. /* int args above */
  157. {Opt_snapdirname, "snapdirname=%s"},
  158. {Opt_mds_namespace, "mds_namespace=%s"},
  159. {Opt_fscache_uniq, "fsc=%s"},
  160. /* string args above */
  161. {Opt_dirstat, "dirstat"},
  162. {Opt_nodirstat, "nodirstat"},
  163. {Opt_rbytes, "rbytes"},
  164. {Opt_norbytes, "norbytes"},
  165. {Opt_asyncreaddir, "asyncreaddir"},
  166. {Opt_noasyncreaddir, "noasyncreaddir"},
  167. {Opt_dcache, "dcache"},
  168. {Opt_nodcache, "nodcache"},
  169. {Opt_ino32, "ino32"},
  170. {Opt_noino32, "noino32"},
  171. {Opt_fscache, "fsc"},
  172. {Opt_nofscache, "nofsc"},
  173. {Opt_poolperm, "poolperm"},
  174. {Opt_nopoolperm, "nopoolperm"},
  175. {Opt_require_active_mds, "require_active_mds"},
  176. {Opt_norequire_active_mds, "norequire_active_mds"},
  177. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  178. {Opt_acl, "acl"},
  179. #endif
  180. {Opt_noacl, "noacl"},
  181. {Opt_quotadf, "quotadf"},
  182. {Opt_noquotadf, "noquotadf"},
  183. {-1, NULL}
  184. };
  185. /*
  186. * Remove adjacent slashes and then the trailing slash, unless it is
  187. * the only remaining character.
  188. *
  189. * E.g. "//dir1////dir2///" --> "/dir1/dir2", "///" --> "/".
  190. */
  191. static void canonicalize_path(char *path)
  192. {
  193. int i, j = 0;
  194. for (i = 0; path[i] != '\0'; i++) {
  195. if (path[i] != '/' || j < 1 || path[j - 1] != '/')
  196. path[j++] = path[i];
  197. }
  198. if (j > 1 && path[j - 1] == '/')
  199. j--;
  200. path[j] = '\0';
  201. }
  202. static int parse_fsopt_token(char *c, void *private)
  203. {
  204. struct ceph_mount_options *fsopt = private;
  205. substring_t argstr[MAX_OPT_ARGS];
  206. int token, intval, ret;
  207. token = match_token((char *)c, fsopt_tokens, argstr);
  208. if (token < 0)
  209. return -EINVAL;
  210. if (token < Opt_last_int) {
  211. ret = match_int(&argstr[0], &intval);
  212. if (ret < 0) {
  213. pr_err("bad option arg (not int) at '%s'\n", c);
  214. return ret;
  215. }
  216. dout("got int token %d val %d\n", token, intval);
  217. } else if (token > Opt_last_int && token < Opt_last_string) {
  218. dout("got string token %d val %s\n", token,
  219. argstr[0].from);
  220. } else {
  221. dout("got token %d\n", token);
  222. }
  223. switch (token) {
  224. case Opt_snapdirname:
  225. kfree(fsopt->snapdir_name);
  226. fsopt->snapdir_name = kstrndup(argstr[0].from,
  227. argstr[0].to-argstr[0].from,
  228. GFP_KERNEL);
  229. if (!fsopt->snapdir_name)
  230. return -ENOMEM;
  231. break;
  232. case Opt_mds_namespace:
  233. kfree(fsopt->mds_namespace);
  234. fsopt->mds_namespace = kstrndup(argstr[0].from,
  235. argstr[0].to-argstr[0].from,
  236. GFP_KERNEL);
  237. if (!fsopt->mds_namespace)
  238. return -ENOMEM;
  239. break;
  240. case Opt_fscache_uniq:
  241. #ifdef CONFIG_CEPH_FSCACHE
  242. kfree(fsopt->fscache_uniq);
  243. fsopt->fscache_uniq = kstrndup(argstr[0].from,
  244. argstr[0].to-argstr[0].from,
  245. GFP_KERNEL);
  246. if (!fsopt->fscache_uniq)
  247. return -ENOMEM;
  248. fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
  249. break;
  250. #else
  251. pr_err("fscache support is disabled\n");
  252. return -EINVAL;
  253. #endif
  254. case Opt_wsize:
  255. if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_WRITE_SIZE)
  256. return -EINVAL;
  257. fsopt->wsize = ALIGN(intval, PAGE_SIZE);
  258. break;
  259. case Opt_rsize:
  260. if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_READ_SIZE)
  261. return -EINVAL;
  262. fsopt->rsize = ALIGN(intval, PAGE_SIZE);
  263. break;
  264. case Opt_rasize:
  265. if (intval < 0)
  266. return -EINVAL;
  267. fsopt->rasize = ALIGN(intval, PAGE_SIZE);
  268. break;
  269. case Opt_caps_wanted_delay_min:
  270. if (intval < 1)
  271. return -EINVAL;
  272. fsopt->caps_wanted_delay_min = intval;
  273. break;
  274. case Opt_caps_wanted_delay_max:
  275. if (intval < 1)
  276. return -EINVAL;
  277. fsopt->caps_wanted_delay_max = intval;
  278. break;
  279. case Opt_readdir_max_entries:
  280. if (intval < 1)
  281. return -EINVAL;
  282. fsopt->max_readdir = intval;
  283. break;
  284. case Opt_readdir_max_bytes:
  285. if (intval < (int)PAGE_SIZE && intval != 0)
  286. return -EINVAL;
  287. fsopt->max_readdir_bytes = intval;
  288. break;
  289. case Opt_congestion_kb:
  290. if (intval < 1024) /* at least 1M */
  291. return -EINVAL;
  292. fsopt->congestion_kb = intval;
  293. break;
  294. case Opt_dirstat:
  295. fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
  296. break;
  297. case Opt_nodirstat:
  298. fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
  299. break;
  300. case Opt_rbytes:
  301. fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
  302. break;
  303. case Opt_norbytes:
  304. fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
  305. break;
  306. case Opt_asyncreaddir:
  307. fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
  308. break;
  309. case Opt_noasyncreaddir:
  310. fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
  311. break;
  312. case Opt_dcache:
  313. fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
  314. break;
  315. case Opt_nodcache:
  316. fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
  317. break;
  318. case Opt_ino32:
  319. fsopt->flags |= CEPH_MOUNT_OPT_INO32;
  320. break;
  321. case Opt_noino32:
  322. fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
  323. break;
  324. case Opt_fscache:
  325. #ifdef CONFIG_CEPH_FSCACHE
  326. fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
  327. kfree(fsopt->fscache_uniq);
  328. fsopt->fscache_uniq = NULL;
  329. break;
  330. #else
  331. pr_err("fscache support is disabled\n");
  332. return -EINVAL;
  333. #endif
  334. case Opt_nofscache:
  335. fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
  336. kfree(fsopt->fscache_uniq);
  337. fsopt->fscache_uniq = NULL;
  338. break;
  339. case Opt_poolperm:
  340. fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
  341. break;
  342. case Opt_nopoolperm:
  343. fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
  344. break;
  345. case Opt_require_active_mds:
  346. fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
  347. break;
  348. case Opt_norequire_active_mds:
  349. fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
  350. break;
  351. case Opt_quotadf:
  352. fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
  353. break;
  354. case Opt_noquotadf:
  355. fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
  356. break;
  357. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  358. case Opt_acl:
  359. fsopt->sb_flags |= SB_POSIXACL;
  360. break;
  361. #endif
  362. case Opt_noacl:
  363. fsopt->sb_flags &= ~SB_POSIXACL;
  364. break;
  365. default:
  366. BUG_ON(token);
  367. }
  368. return 0;
  369. }
  370. static void destroy_mount_options(struct ceph_mount_options *args)
  371. {
  372. dout("destroy_mount_options %p\n", args);
  373. kfree(args->snapdir_name);
  374. kfree(args->mds_namespace);
  375. kfree(args->server_path);
  376. kfree(args->fscache_uniq);
  377. kfree(args);
  378. }
  379. static int strcmp_null(const char *s1, const char *s2)
  380. {
  381. if (!s1 && !s2)
  382. return 0;
  383. if (s1 && !s2)
  384. return -1;
  385. if (!s1 && s2)
  386. return 1;
  387. return strcmp(s1, s2);
  388. }
  389. static int compare_mount_options(struct ceph_mount_options *new_fsopt,
  390. struct ceph_options *new_opt,
  391. struct ceph_fs_client *fsc)
  392. {
  393. struct ceph_mount_options *fsopt1 = new_fsopt;
  394. struct ceph_mount_options *fsopt2 = fsc->mount_options;
  395. int ofs = offsetof(struct ceph_mount_options, snapdir_name);
  396. int ret;
  397. ret = memcmp(fsopt1, fsopt2, ofs);
  398. if (ret)
  399. return ret;
  400. ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
  401. if (ret)
  402. return ret;
  403. ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
  404. if (ret)
  405. return ret;
  406. ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
  407. if (ret)
  408. return ret;
  409. ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
  410. if (ret)
  411. return ret;
  412. return ceph_compare_options(new_opt, fsc->client);
  413. }
  414. static int parse_mount_options(struct ceph_mount_options **pfsopt,
  415. struct ceph_options **popt,
  416. int flags, char *options,
  417. const char *dev_name)
  418. {
  419. struct ceph_mount_options *fsopt;
  420. const char *dev_name_end;
  421. int err;
  422. if (!dev_name || !*dev_name)
  423. return -EINVAL;
  424. fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
  425. if (!fsopt)
  426. return -ENOMEM;
  427. dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
  428. fsopt->sb_flags = flags;
  429. fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
  430. fsopt->wsize = CEPH_MAX_WRITE_SIZE;
  431. fsopt->rsize = CEPH_MAX_READ_SIZE;
  432. fsopt->rasize = CEPH_RASIZE_DEFAULT;
  433. fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
  434. if (!fsopt->snapdir_name) {
  435. err = -ENOMEM;
  436. goto out;
  437. }
  438. fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  439. fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  440. fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
  441. fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
  442. fsopt->congestion_kb = default_congestion_kb();
  443. /*
  444. * Distinguish the server list from the path in "dev_name".
  445. * Internally we do not include the leading '/' in the path.
  446. *
  447. * "dev_name" will look like:
  448. * <server_spec>[,<server_spec>...]:[<path>]
  449. * where
  450. * <server_spec> is <ip>[:<port>]
  451. * <path> is optional, but if present must begin with '/'
  452. */
  453. dev_name_end = strchr(dev_name, '/');
  454. if (dev_name_end) {
  455. /*
  456. * The server_path will include the whole chars from userland
  457. * including the leading '/'.
  458. */
  459. fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
  460. if (!fsopt->server_path) {
  461. err = -ENOMEM;
  462. goto out;
  463. }
  464. canonicalize_path(fsopt->server_path);
  465. } else {
  466. dev_name_end = dev_name + strlen(dev_name);
  467. }
  468. err = -EINVAL;
  469. dev_name_end--; /* back up to ':' separator */
  470. if (dev_name_end < dev_name || *dev_name_end != ':') {
  471. pr_err("device name is missing path (no : separator in %s)\n",
  472. dev_name);
  473. goto out;
  474. }
  475. dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
  476. if (fsopt->server_path)
  477. dout("server path '%s'\n", fsopt->server_path);
  478. *popt = ceph_parse_options(options, dev_name, dev_name_end,
  479. parse_fsopt_token, (void *)fsopt);
  480. if (IS_ERR(*popt)) {
  481. err = PTR_ERR(*popt);
  482. goto out;
  483. }
  484. /* success */
  485. *pfsopt = fsopt;
  486. return 0;
  487. out:
  488. destroy_mount_options(fsopt);
  489. return err;
  490. }
  491. /**
  492. * ceph_show_options - Show mount options in /proc/mounts
  493. * @m: seq_file to write to
  494. * @root: root of that (sub)tree
  495. */
  496. static int ceph_show_options(struct seq_file *m, struct dentry *root)
  497. {
  498. struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
  499. struct ceph_mount_options *fsopt = fsc->mount_options;
  500. size_t pos;
  501. int ret;
  502. /* a comma between MNT/MS and client options */
  503. seq_putc(m, ',');
  504. pos = m->count;
  505. ret = ceph_print_client_options(m, fsc->client);
  506. if (ret)
  507. return ret;
  508. /* retract our comma if no client options */
  509. if (m->count == pos)
  510. m->count--;
  511. if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
  512. seq_puts(m, ",dirstat");
  513. if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
  514. seq_puts(m, ",rbytes");
  515. if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
  516. seq_puts(m, ",noasyncreaddir");
  517. if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
  518. seq_puts(m, ",nodcache");
  519. if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
  520. seq_puts(m, ",ino32");
  521. if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
  522. seq_show_option(m, "fsc", fsopt->fscache_uniq);
  523. }
  524. if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
  525. seq_puts(m, ",nopoolperm");
  526. if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
  527. seq_puts(m, ",noquotadf");
  528. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  529. if (fsopt->sb_flags & SB_POSIXACL)
  530. seq_puts(m, ",acl");
  531. else
  532. seq_puts(m, ",noacl");
  533. #endif
  534. if (fsopt->mds_namespace)
  535. seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
  536. if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
  537. seq_printf(m, ",wsize=%d", fsopt->wsize);
  538. if (fsopt->rsize != CEPH_MAX_READ_SIZE)
  539. seq_printf(m, ",rsize=%d", fsopt->rsize);
  540. if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
  541. seq_printf(m, ",rasize=%d", fsopt->rasize);
  542. if (fsopt->congestion_kb != default_congestion_kb())
  543. seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
  544. if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
  545. seq_printf(m, ",caps_wanted_delay_min=%d",
  546. fsopt->caps_wanted_delay_min);
  547. if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
  548. seq_printf(m, ",caps_wanted_delay_max=%d",
  549. fsopt->caps_wanted_delay_max);
  550. if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
  551. seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
  552. if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
  553. seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
  554. if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  555. seq_show_option(m, "snapdirname", fsopt->snapdir_name);
  556. return 0;
  557. }
  558. /*
  559. * handle any mon messages the standard library doesn't understand.
  560. * return error if we don't either.
  561. */
  562. static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
  563. {
  564. struct ceph_fs_client *fsc = client->private;
  565. int type = le16_to_cpu(msg->hdr.type);
  566. switch (type) {
  567. case CEPH_MSG_MDS_MAP:
  568. ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
  569. return 0;
  570. case CEPH_MSG_FS_MAP_USER:
  571. ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
  572. return 0;
  573. default:
  574. return -1;
  575. }
  576. }
  577. /*
  578. * create a new fs client
  579. *
  580. * Success or not, this function consumes @fsopt and @opt.
  581. */
  582. static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
  583. struct ceph_options *opt)
  584. {
  585. struct ceph_fs_client *fsc;
  586. int page_count;
  587. size_t size;
  588. int err;
  589. fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
  590. if (!fsc) {
  591. err = -ENOMEM;
  592. goto fail;
  593. }
  594. fsc->client = ceph_create_client(opt, fsc);
  595. if (IS_ERR(fsc->client)) {
  596. err = PTR_ERR(fsc->client);
  597. goto fail;
  598. }
  599. opt = NULL; /* fsc->client now owns this */
  600. fsc->client->extra_mon_dispatch = extra_mon_dispatch;
  601. fsc->client->osdc.abort_on_full = true;
  602. if (!fsopt->mds_namespace) {
  603. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
  604. 0, true);
  605. } else {
  606. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
  607. 0, false);
  608. }
  609. fsc->mount_options = fsopt;
  610. fsc->sb = NULL;
  611. fsc->mount_state = CEPH_MOUNT_MOUNTING;
  612. atomic_long_set(&fsc->writeback_count, 0);
  613. err = -ENOMEM;
  614. /*
  615. * The number of concurrent works can be high but they don't need
  616. * to be processed in parallel, limit concurrency.
  617. */
  618. fsc->wb_wq = alloc_workqueue("ceph-writeback", 0, 1);
  619. if (!fsc->wb_wq)
  620. goto fail_client;
  621. fsc->pg_inv_wq = alloc_workqueue("ceph-pg-invalid", 0, 1);
  622. if (!fsc->pg_inv_wq)
  623. goto fail_wb_wq;
  624. fsc->trunc_wq = alloc_workqueue("ceph-trunc", 0, 1);
  625. if (!fsc->trunc_wq)
  626. goto fail_pg_inv_wq;
  627. /* set up mempools */
  628. err = -ENOMEM;
  629. page_count = fsc->mount_options->wsize >> PAGE_SHIFT;
  630. size = sizeof (struct page *) * (page_count ? page_count : 1);
  631. fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
  632. if (!fsc->wb_pagevec_pool)
  633. goto fail_trunc_wq;
  634. /* caps */
  635. fsc->min_caps = fsopt->max_readdir;
  636. return fsc;
  637. fail_trunc_wq:
  638. destroy_workqueue(fsc->trunc_wq);
  639. fail_pg_inv_wq:
  640. destroy_workqueue(fsc->pg_inv_wq);
  641. fail_wb_wq:
  642. destroy_workqueue(fsc->wb_wq);
  643. fail_client:
  644. ceph_destroy_client(fsc->client);
  645. fail:
  646. kfree(fsc);
  647. if (opt)
  648. ceph_destroy_options(opt);
  649. destroy_mount_options(fsopt);
  650. return ERR_PTR(err);
  651. }
  652. static void flush_fs_workqueues(struct ceph_fs_client *fsc)
  653. {
  654. flush_workqueue(fsc->wb_wq);
  655. flush_workqueue(fsc->pg_inv_wq);
  656. flush_workqueue(fsc->trunc_wq);
  657. }
  658. static void destroy_fs_client(struct ceph_fs_client *fsc)
  659. {
  660. dout("destroy_fs_client %p\n", fsc);
  661. destroy_workqueue(fsc->wb_wq);
  662. destroy_workqueue(fsc->pg_inv_wq);
  663. destroy_workqueue(fsc->trunc_wq);
  664. mempool_destroy(fsc->wb_pagevec_pool);
  665. destroy_mount_options(fsc->mount_options);
  666. ceph_destroy_client(fsc->client);
  667. kfree(fsc);
  668. dout("destroy_fs_client %p done\n", fsc);
  669. }
  670. /*
  671. * caches
  672. */
  673. struct kmem_cache *ceph_inode_cachep;
  674. struct kmem_cache *ceph_cap_cachep;
  675. struct kmem_cache *ceph_cap_flush_cachep;
  676. struct kmem_cache *ceph_dentry_cachep;
  677. struct kmem_cache *ceph_file_cachep;
  678. struct kmem_cache *ceph_dir_file_cachep;
  679. static void ceph_inode_init_once(void *foo)
  680. {
  681. struct ceph_inode_info *ci = foo;
  682. inode_init_once(&ci->vfs_inode);
  683. }
  684. static int __init init_caches(void)
  685. {
  686. int error = -ENOMEM;
  687. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  688. sizeof(struct ceph_inode_info),
  689. __alignof__(struct ceph_inode_info),
  690. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
  691. SLAB_ACCOUNT, ceph_inode_init_once);
  692. if (!ceph_inode_cachep)
  693. return -ENOMEM;
  694. ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
  695. if (!ceph_cap_cachep)
  696. goto bad_cap;
  697. ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
  698. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  699. if (!ceph_cap_flush_cachep)
  700. goto bad_cap_flush;
  701. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  702. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  703. if (!ceph_dentry_cachep)
  704. goto bad_dentry;
  705. ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
  706. if (!ceph_file_cachep)
  707. goto bad_file;
  708. ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
  709. if (!ceph_dir_file_cachep)
  710. goto bad_dir_file;
  711. error = ceph_fscache_register();
  712. if (error)
  713. goto bad_fscache;
  714. return 0;
  715. bad_fscache:
  716. kmem_cache_destroy(ceph_dir_file_cachep);
  717. bad_dir_file:
  718. kmem_cache_destroy(ceph_file_cachep);
  719. bad_file:
  720. kmem_cache_destroy(ceph_dentry_cachep);
  721. bad_dentry:
  722. kmem_cache_destroy(ceph_cap_flush_cachep);
  723. bad_cap_flush:
  724. kmem_cache_destroy(ceph_cap_cachep);
  725. bad_cap:
  726. kmem_cache_destroy(ceph_inode_cachep);
  727. return error;
  728. }
  729. static void destroy_caches(void)
  730. {
  731. /*
  732. * Make sure all delayed rcu free inodes are flushed before we
  733. * destroy cache.
  734. */
  735. rcu_barrier();
  736. kmem_cache_destroy(ceph_inode_cachep);
  737. kmem_cache_destroy(ceph_cap_cachep);
  738. kmem_cache_destroy(ceph_cap_flush_cachep);
  739. kmem_cache_destroy(ceph_dentry_cachep);
  740. kmem_cache_destroy(ceph_file_cachep);
  741. kmem_cache_destroy(ceph_dir_file_cachep);
  742. ceph_fscache_unregister();
  743. }
  744. /*
  745. * ceph_umount_begin - initiate forced umount. Tear down down the
  746. * mount, skipping steps that may hang while waiting for server(s).
  747. */
  748. static void ceph_umount_begin(struct super_block *sb)
  749. {
  750. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  751. dout("ceph_umount_begin - starting forced umount\n");
  752. if (!fsc)
  753. return;
  754. fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
  755. ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
  756. ceph_mdsc_force_umount(fsc->mdsc);
  757. return;
  758. }
  759. static int ceph_remount(struct super_block *sb, int *flags, char *data)
  760. {
  761. sync_filesystem(sb);
  762. return 0;
  763. }
  764. static const struct super_operations ceph_super_ops = {
  765. .alloc_inode = ceph_alloc_inode,
  766. .destroy_inode = ceph_destroy_inode,
  767. .write_inode = ceph_write_inode,
  768. .drop_inode = ceph_drop_inode,
  769. .evict_inode = ceph_evict_inode,
  770. .sync_fs = ceph_sync_fs,
  771. .put_super = ceph_put_super,
  772. .remount_fs = ceph_remount,
  773. .show_options = ceph_show_options,
  774. .statfs = ceph_statfs,
  775. .umount_begin = ceph_umount_begin,
  776. };
  777. /*
  778. * Bootstrap mount by opening the root directory. Note the mount
  779. * @started time from caller, and time out if this takes too long.
  780. */
  781. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  782. const char *path,
  783. unsigned long started)
  784. {
  785. struct ceph_mds_client *mdsc = fsc->mdsc;
  786. struct ceph_mds_request *req = NULL;
  787. int err;
  788. struct dentry *root;
  789. /* open dir */
  790. dout("open_root_inode opening '%s'\n", path);
  791. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  792. if (IS_ERR(req))
  793. return ERR_CAST(req);
  794. req->r_path1 = kstrdup(path, GFP_NOFS);
  795. if (!req->r_path1) {
  796. root = ERR_PTR(-ENOMEM);
  797. goto out;
  798. }
  799. req->r_ino1.ino = CEPH_INO_ROOT;
  800. req->r_ino1.snap = CEPH_NOSNAP;
  801. req->r_started = started;
  802. req->r_timeout = fsc->client->options->mount_timeout;
  803. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  804. req->r_num_caps = 2;
  805. err = ceph_mdsc_do_request(mdsc, NULL, req);
  806. if (err == 0) {
  807. struct inode *inode = req->r_target_inode;
  808. req->r_target_inode = NULL;
  809. dout("open_root_inode success\n");
  810. root = d_make_root(inode);
  811. if (!root) {
  812. root = ERR_PTR(-ENOMEM);
  813. goto out;
  814. }
  815. dout("open_root_inode success, root dentry is %p\n", root);
  816. } else {
  817. root = ERR_PTR(err);
  818. }
  819. out:
  820. ceph_mdsc_put_request(req);
  821. return root;
  822. }
  823. /*
  824. * mount: join the ceph cluster, and open root directory.
  825. */
  826. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc)
  827. {
  828. int err;
  829. unsigned long started = jiffies; /* note the start time */
  830. struct dentry *root;
  831. dout("mount start %p\n", fsc);
  832. mutex_lock(&fsc->client->mount_mutex);
  833. if (!fsc->sb->s_root) {
  834. const char *path = fsc->mount_options->server_path ?
  835. fsc->mount_options->server_path + 1 : "";
  836. err = __ceph_open_session(fsc->client, started);
  837. if (err < 0)
  838. goto out;
  839. /* setup fscache */
  840. if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
  841. err = ceph_fscache_register_fs(fsc);
  842. if (err < 0)
  843. goto out;
  844. }
  845. dout("mount opening path '%s'\n", path);
  846. err = ceph_fs_debugfs_init(fsc);
  847. if (err < 0)
  848. goto out;
  849. root = open_root_dentry(fsc, path, started);
  850. if (IS_ERR(root)) {
  851. err = PTR_ERR(root);
  852. goto out;
  853. }
  854. fsc->sb->s_root = dget(root);
  855. } else {
  856. root = dget(fsc->sb->s_root);
  857. }
  858. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  859. dout("mount success\n");
  860. mutex_unlock(&fsc->client->mount_mutex);
  861. return root;
  862. out:
  863. mutex_unlock(&fsc->client->mount_mutex);
  864. return ERR_PTR(err);
  865. }
  866. static int ceph_set_super(struct super_block *s, void *data)
  867. {
  868. struct ceph_fs_client *fsc = data;
  869. int ret;
  870. dout("set_super %p data %p\n", s, data);
  871. s->s_flags = fsc->mount_options->sb_flags;
  872. s->s_maxbytes = MAX_LFS_FILESIZE;
  873. s->s_xattr = ceph_xattr_handlers;
  874. s->s_fs_info = fsc;
  875. fsc->sb = s;
  876. fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
  877. s->s_op = &ceph_super_ops;
  878. s->s_d_op = &ceph_dentry_ops;
  879. s->s_export_op = &ceph_export_ops;
  880. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  881. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  882. if (ret != 0)
  883. goto fail;
  884. return ret;
  885. fail:
  886. s->s_fs_info = NULL;
  887. fsc->sb = NULL;
  888. return ret;
  889. }
  890. /*
  891. * share superblock if same fs AND options
  892. */
  893. static int ceph_compare_super(struct super_block *sb, void *data)
  894. {
  895. struct ceph_fs_client *new = data;
  896. struct ceph_mount_options *fsopt = new->mount_options;
  897. struct ceph_options *opt = new->client->options;
  898. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  899. dout("ceph_compare_super %p\n", sb);
  900. if (compare_mount_options(fsopt, opt, other)) {
  901. dout("monitor(s)/mount options don't match\n");
  902. return 0;
  903. }
  904. if ((opt->flags & CEPH_OPT_FSID) &&
  905. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  906. dout("fsid doesn't match\n");
  907. return 0;
  908. }
  909. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  910. dout("flags differ\n");
  911. return 0;
  912. }
  913. return 1;
  914. }
  915. /*
  916. * construct our own bdi so we can control readahead, etc.
  917. */
  918. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  919. static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
  920. {
  921. int err;
  922. err = super_setup_bdi_name(sb, "ceph-%ld",
  923. atomic_long_inc_return(&bdi_seq));
  924. if (err)
  925. return err;
  926. /* set ra_pages based on rasize mount option? */
  927. sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
  928. /* set io_pages based on max osd read size */
  929. sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
  930. return 0;
  931. }
  932. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  933. int flags, const char *dev_name, void *data)
  934. {
  935. struct super_block *sb;
  936. struct ceph_fs_client *fsc;
  937. struct dentry *res;
  938. int err;
  939. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  940. struct ceph_mount_options *fsopt = NULL;
  941. struct ceph_options *opt = NULL;
  942. dout("ceph_mount\n");
  943. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  944. flags |= SB_POSIXACL;
  945. #endif
  946. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
  947. if (err < 0) {
  948. res = ERR_PTR(err);
  949. goto out_final;
  950. }
  951. /* create client (which we may/may not use) */
  952. fsc = create_fs_client(fsopt, opt);
  953. if (IS_ERR(fsc)) {
  954. res = ERR_CAST(fsc);
  955. goto out_final;
  956. }
  957. err = ceph_mdsc_init(fsc);
  958. if (err < 0) {
  959. res = ERR_PTR(err);
  960. goto out;
  961. }
  962. if (ceph_test_opt(fsc->client, NOSHARE))
  963. compare_super = NULL;
  964. sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
  965. if (IS_ERR(sb)) {
  966. res = ERR_CAST(sb);
  967. goto out;
  968. }
  969. if (ceph_sb_to_client(sb) != fsc) {
  970. ceph_mdsc_destroy(fsc);
  971. destroy_fs_client(fsc);
  972. fsc = ceph_sb_to_client(sb);
  973. dout("get_sb got existing client %p\n", fsc);
  974. } else {
  975. dout("get_sb using new client %p\n", fsc);
  976. err = ceph_setup_bdi(sb, fsc);
  977. if (err < 0) {
  978. res = ERR_PTR(err);
  979. goto out_splat;
  980. }
  981. }
  982. res = ceph_real_mount(fsc);
  983. if (IS_ERR(res))
  984. goto out_splat;
  985. dout("root %p inode %p ino %llx.%llx\n", res,
  986. d_inode(res), ceph_vinop(d_inode(res)));
  987. return res;
  988. out_splat:
  989. if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
  990. pr_info("No mds server is up or the cluster is laggy\n");
  991. err = -EHOSTUNREACH;
  992. }
  993. ceph_mdsc_close_sessions(fsc->mdsc);
  994. deactivate_locked_super(sb);
  995. goto out_final;
  996. out:
  997. ceph_mdsc_destroy(fsc);
  998. destroy_fs_client(fsc);
  999. out_final:
  1000. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  1001. return res;
  1002. }
  1003. static void ceph_kill_sb(struct super_block *s)
  1004. {
  1005. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  1006. dev_t dev = s->s_dev;
  1007. dout("kill_sb %p\n", s);
  1008. ceph_mdsc_pre_umount(fsc->mdsc);
  1009. flush_fs_workqueues(fsc);
  1010. generic_shutdown_super(s);
  1011. fsc->client->extra_mon_dispatch = NULL;
  1012. ceph_fs_debugfs_cleanup(fsc);
  1013. ceph_fscache_unregister_fs(fsc);
  1014. ceph_mdsc_destroy(fsc);
  1015. destroy_fs_client(fsc);
  1016. free_anon_bdev(dev);
  1017. }
  1018. static struct file_system_type ceph_fs_type = {
  1019. .owner = THIS_MODULE,
  1020. .name = "ceph",
  1021. .mount = ceph_mount,
  1022. .kill_sb = ceph_kill_sb,
  1023. .fs_flags = FS_RENAME_DOES_D_MOVE,
  1024. };
  1025. MODULE_ALIAS_FS("ceph");
  1026. static int __init init_ceph(void)
  1027. {
  1028. int ret = init_caches();
  1029. if (ret)
  1030. goto out;
  1031. ceph_flock_init();
  1032. ceph_xattr_init();
  1033. ret = register_filesystem(&ceph_fs_type);
  1034. if (ret)
  1035. goto out_xattr;
  1036. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  1037. return 0;
  1038. out_xattr:
  1039. ceph_xattr_exit();
  1040. destroy_caches();
  1041. out:
  1042. return ret;
  1043. }
  1044. static void __exit exit_ceph(void)
  1045. {
  1046. dout("exit_ceph\n");
  1047. unregister_filesystem(&ceph_fs_type);
  1048. ceph_xattr_exit();
  1049. destroy_caches();
  1050. }
  1051. module_init(init_ceph);
  1052. module_exit(exit_ceph);
  1053. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  1054. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  1055. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  1056. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  1057. MODULE_LICENSE("GPL");