super.c 25 KB

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