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(dentry->d_inode);
  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. fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  312. fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  313. fsopt->cap_release_safety = CEPH_CAP_RELEASE_SAFETY_DEFAULT;
  314. fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
  315. fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
  316. fsopt->congestion_kb = default_congestion_kb();
  317. /*
  318. * Distinguish the server list from the path in "dev_name".
  319. * Internally we do not include the leading '/' in the path.
  320. *
  321. * "dev_name" will look like:
  322. * <server_spec>[,<server_spec>...]:[<path>]
  323. * where
  324. * <server_spec> is <ip>[:<port>]
  325. * <path> is optional, but if present must begin with '/'
  326. */
  327. dev_name_end = strchr(dev_name, '/');
  328. if (dev_name_end) {
  329. /* skip over leading '/' for path */
  330. *path = dev_name_end + 1;
  331. } else {
  332. /* path is empty */
  333. dev_name_end = dev_name + strlen(dev_name);
  334. *path = dev_name_end;
  335. }
  336. err = -EINVAL;
  337. dev_name_end--; /* back up to ':' separator */
  338. if (dev_name_end < dev_name || *dev_name_end != ':') {
  339. pr_err("device name is missing path (no : separator in %s)\n",
  340. dev_name);
  341. goto out;
  342. }
  343. dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
  344. dout("server path '%s'\n", *path);
  345. *popt = ceph_parse_options(options, dev_name, dev_name_end,
  346. parse_fsopt_token, (void *)fsopt);
  347. if (IS_ERR(*popt)) {
  348. err = PTR_ERR(*popt);
  349. goto out;
  350. }
  351. /* success */
  352. *pfsopt = fsopt;
  353. return 0;
  354. out:
  355. destroy_mount_options(fsopt);
  356. return err;
  357. }
  358. /**
  359. * ceph_show_options - Show mount options in /proc/mounts
  360. * @m: seq_file to write to
  361. * @root: root of that (sub)tree
  362. */
  363. static int ceph_show_options(struct seq_file *m, struct dentry *root)
  364. {
  365. struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
  366. struct ceph_mount_options *fsopt = fsc->mount_options;
  367. struct ceph_options *opt = fsc->client->options;
  368. if (opt->flags & CEPH_OPT_FSID)
  369. seq_printf(m, ",fsid=%pU", &opt->fsid);
  370. if (opt->flags & CEPH_OPT_NOSHARE)
  371. seq_puts(m, ",noshare");
  372. if (opt->flags & CEPH_OPT_NOCRC)
  373. seq_puts(m, ",nocrc");
  374. if (opt->name)
  375. seq_printf(m, ",name=%s", opt->name);
  376. if (opt->key)
  377. seq_puts(m, ",secret=<hidden>");
  378. if (opt->mount_timeout != CEPH_MOUNT_TIMEOUT_DEFAULT)
  379. seq_printf(m, ",mount_timeout=%d", opt->mount_timeout);
  380. if (opt->osd_idle_ttl != CEPH_OSD_IDLE_TTL_DEFAULT)
  381. seq_printf(m, ",osd_idle_ttl=%d", opt->osd_idle_ttl);
  382. if (opt->osd_keepalive_timeout != CEPH_OSD_KEEPALIVE_DEFAULT)
  383. seq_printf(m, ",osdkeepalivetimeout=%d",
  384. opt->osd_keepalive_timeout);
  385. if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
  386. seq_puts(m, ",dirstat");
  387. if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES) == 0)
  388. seq_puts(m, ",norbytes");
  389. if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
  390. seq_puts(m, ",noasyncreaddir");
  391. if (fsopt->flags & CEPH_MOUNT_OPT_DCACHE)
  392. seq_puts(m, ",dcache");
  393. else
  394. seq_puts(m, ",nodcache");
  395. if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE)
  396. seq_puts(m, ",fsc");
  397. else
  398. seq_puts(m, ",nofsc");
  399. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  400. if (fsopt->sb_flags & MS_POSIXACL)
  401. seq_puts(m, ",acl");
  402. else
  403. seq_puts(m, ",noacl");
  404. #endif
  405. if (fsopt->wsize)
  406. seq_printf(m, ",wsize=%d", fsopt->wsize);
  407. if (fsopt->rsize != CEPH_RSIZE_DEFAULT)
  408. seq_printf(m, ",rsize=%d", fsopt->rsize);
  409. if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
  410. seq_printf(m, ",rasize=%d", fsopt->rasize);
  411. if (fsopt->congestion_kb != default_congestion_kb())
  412. seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
  413. if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
  414. seq_printf(m, ",caps_wanted_delay_min=%d",
  415. fsopt->caps_wanted_delay_min);
  416. if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
  417. seq_printf(m, ",caps_wanted_delay_max=%d",
  418. fsopt->caps_wanted_delay_max);
  419. if (fsopt->cap_release_safety != CEPH_CAP_RELEASE_SAFETY_DEFAULT)
  420. seq_printf(m, ",cap_release_safety=%d",
  421. fsopt->cap_release_safety);
  422. if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
  423. seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
  424. if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
  425. seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
  426. if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  427. seq_printf(m, ",snapdirname=%s", fsopt->snapdir_name);
  428. return 0;
  429. }
  430. /*
  431. * handle any mon messages the standard library doesn't understand.
  432. * return error if we don't either.
  433. */
  434. static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
  435. {
  436. struct ceph_fs_client *fsc = client->private;
  437. int type = le16_to_cpu(msg->hdr.type);
  438. switch (type) {
  439. case CEPH_MSG_MDS_MAP:
  440. ceph_mdsc_handle_map(fsc->mdsc, msg);
  441. return 0;
  442. default:
  443. return -1;
  444. }
  445. }
  446. /*
  447. * create a new fs client
  448. */
  449. static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
  450. struct ceph_options *opt)
  451. {
  452. struct ceph_fs_client *fsc;
  453. const u64 supported_features =
  454. CEPH_FEATURE_FLOCK |
  455. CEPH_FEATURE_DIRLAYOUTHASH |
  456. CEPH_FEATURE_MDS_INLINE_DATA;
  457. const u64 required_features = 0;
  458. int page_count;
  459. size_t size;
  460. int err = -ENOMEM;
  461. fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
  462. if (!fsc)
  463. return ERR_PTR(-ENOMEM);
  464. fsc->client = ceph_create_client(opt, fsc, supported_features,
  465. required_features);
  466. if (IS_ERR(fsc->client)) {
  467. err = PTR_ERR(fsc->client);
  468. goto fail;
  469. }
  470. fsc->client->extra_mon_dispatch = extra_mon_dispatch;
  471. fsc->client->monc.want_mdsmap = 1;
  472. fsc->mount_options = fsopt;
  473. fsc->sb = NULL;
  474. fsc->mount_state = CEPH_MOUNT_MOUNTING;
  475. atomic_long_set(&fsc->writeback_count, 0);
  476. err = bdi_init(&fsc->backing_dev_info);
  477. if (err < 0)
  478. goto fail_client;
  479. err = -ENOMEM;
  480. /*
  481. * The number of concurrent works can be high but they don't need
  482. * to be processed in parallel, limit concurrency.
  483. */
  484. fsc->wb_wq = alloc_workqueue("ceph-writeback", 0, 1);
  485. if (fsc->wb_wq == NULL)
  486. goto fail_bdi;
  487. fsc->pg_inv_wq = alloc_workqueue("ceph-pg-invalid", 0, 1);
  488. if (fsc->pg_inv_wq == NULL)
  489. goto fail_wb_wq;
  490. fsc->trunc_wq = alloc_workqueue("ceph-trunc", 0, 1);
  491. if (fsc->trunc_wq == NULL)
  492. goto fail_pg_inv_wq;
  493. /* set up mempools */
  494. err = -ENOMEM;
  495. page_count = fsc->mount_options->wsize >> PAGE_CACHE_SHIFT;
  496. size = sizeof (struct page *) * (page_count ? page_count : 1);
  497. fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
  498. if (!fsc->wb_pagevec_pool)
  499. goto fail_trunc_wq;
  500. /* setup fscache */
  501. if ((fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) &&
  502. (ceph_fscache_register_fs(fsc) != 0))
  503. goto fail_fscache;
  504. /* caps */
  505. fsc->min_caps = fsopt->max_readdir;
  506. return fsc;
  507. fail_fscache:
  508. ceph_fscache_unregister_fs(fsc);
  509. fail_trunc_wq:
  510. destroy_workqueue(fsc->trunc_wq);
  511. fail_pg_inv_wq:
  512. destroy_workqueue(fsc->pg_inv_wq);
  513. fail_wb_wq:
  514. destroy_workqueue(fsc->wb_wq);
  515. fail_bdi:
  516. bdi_destroy(&fsc->backing_dev_info);
  517. fail_client:
  518. ceph_destroy_client(fsc->client);
  519. fail:
  520. kfree(fsc);
  521. return ERR_PTR(err);
  522. }
  523. static void destroy_fs_client(struct ceph_fs_client *fsc)
  524. {
  525. dout("destroy_fs_client %p\n", fsc);
  526. ceph_fscache_unregister_fs(fsc);
  527. destroy_workqueue(fsc->wb_wq);
  528. destroy_workqueue(fsc->pg_inv_wq);
  529. destroy_workqueue(fsc->trunc_wq);
  530. bdi_destroy(&fsc->backing_dev_info);
  531. mempool_destroy(fsc->wb_pagevec_pool);
  532. destroy_mount_options(fsc->mount_options);
  533. ceph_fs_debugfs_cleanup(fsc);
  534. ceph_destroy_client(fsc->client);
  535. kfree(fsc);
  536. dout("destroy_fs_client %p done\n", fsc);
  537. }
  538. /*
  539. * caches
  540. */
  541. struct kmem_cache *ceph_inode_cachep;
  542. struct kmem_cache *ceph_cap_cachep;
  543. struct kmem_cache *ceph_dentry_cachep;
  544. struct kmem_cache *ceph_file_cachep;
  545. static void ceph_inode_init_once(void *foo)
  546. {
  547. struct ceph_inode_info *ci = foo;
  548. inode_init_once(&ci->vfs_inode);
  549. }
  550. static int __init init_caches(void)
  551. {
  552. int error = -ENOMEM;
  553. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  554. sizeof(struct ceph_inode_info),
  555. __alignof__(struct ceph_inode_info),
  556. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  557. ceph_inode_init_once);
  558. if (ceph_inode_cachep == NULL)
  559. return -ENOMEM;
  560. ceph_cap_cachep = KMEM_CACHE(ceph_cap,
  561. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  562. if (ceph_cap_cachep == NULL)
  563. goto bad_cap;
  564. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  565. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  566. if (ceph_dentry_cachep == NULL)
  567. goto bad_dentry;
  568. ceph_file_cachep = KMEM_CACHE(ceph_file_info,
  569. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  570. if (ceph_file_cachep == NULL)
  571. goto bad_file;
  572. if ((error = ceph_fscache_register()))
  573. goto bad_file;
  574. return 0;
  575. bad_file:
  576. kmem_cache_destroy(ceph_dentry_cachep);
  577. bad_dentry:
  578. kmem_cache_destroy(ceph_cap_cachep);
  579. bad_cap:
  580. kmem_cache_destroy(ceph_inode_cachep);
  581. return error;
  582. }
  583. static void destroy_caches(void)
  584. {
  585. /*
  586. * Make sure all delayed rcu free inodes are flushed before we
  587. * destroy cache.
  588. */
  589. rcu_barrier();
  590. kmem_cache_destroy(ceph_inode_cachep);
  591. kmem_cache_destroy(ceph_cap_cachep);
  592. kmem_cache_destroy(ceph_dentry_cachep);
  593. kmem_cache_destroy(ceph_file_cachep);
  594. ceph_fscache_unregister();
  595. }
  596. /*
  597. * ceph_umount_begin - initiate forced umount. Tear down down the
  598. * mount, skipping steps that may hang while waiting for server(s).
  599. */
  600. static void ceph_umount_begin(struct super_block *sb)
  601. {
  602. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  603. dout("ceph_umount_begin - starting forced umount\n");
  604. if (!fsc)
  605. return;
  606. fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
  607. return;
  608. }
  609. static const struct super_operations ceph_super_ops = {
  610. .alloc_inode = ceph_alloc_inode,
  611. .destroy_inode = ceph_destroy_inode,
  612. .write_inode = ceph_write_inode,
  613. .drop_inode = ceph_drop_inode,
  614. .sync_fs = ceph_sync_fs,
  615. .put_super = ceph_put_super,
  616. .show_options = ceph_show_options,
  617. .statfs = ceph_statfs,
  618. .umount_begin = ceph_umount_begin,
  619. };
  620. /*
  621. * Bootstrap mount by opening the root directory. Note the mount
  622. * @started time from caller, and time out if this takes too long.
  623. */
  624. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  625. const char *path,
  626. unsigned long started)
  627. {
  628. struct ceph_mds_client *mdsc = fsc->mdsc;
  629. struct ceph_mds_request *req = NULL;
  630. int err;
  631. struct dentry *root;
  632. /* open dir */
  633. dout("open_root_inode opening '%s'\n", path);
  634. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  635. if (IS_ERR(req))
  636. return ERR_CAST(req);
  637. req->r_path1 = kstrdup(path, GFP_NOFS);
  638. req->r_ino1.ino = CEPH_INO_ROOT;
  639. req->r_ino1.snap = CEPH_NOSNAP;
  640. req->r_started = started;
  641. req->r_timeout = fsc->client->options->mount_timeout * HZ;
  642. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  643. req->r_num_caps = 2;
  644. err = ceph_mdsc_do_request(mdsc, NULL, req);
  645. if (err == 0) {
  646. struct inode *inode = req->r_target_inode;
  647. req->r_target_inode = NULL;
  648. dout("open_root_inode success\n");
  649. if (ceph_ino(inode) == CEPH_INO_ROOT &&
  650. fsc->sb->s_root == NULL) {
  651. root = d_make_root(inode);
  652. if (!root) {
  653. root = ERR_PTR(-ENOMEM);
  654. goto out;
  655. }
  656. } else {
  657. root = d_obtain_root(inode);
  658. }
  659. ceph_init_dentry(root);
  660. dout("open_root_inode success, root dentry is %p\n", root);
  661. } else {
  662. root = ERR_PTR(err);
  663. }
  664. out:
  665. ceph_mdsc_put_request(req);
  666. return root;
  667. }
  668. /*
  669. * mount: join the ceph cluster, and open root directory.
  670. */
  671. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
  672. const char *path)
  673. {
  674. int err;
  675. unsigned long started = jiffies; /* note the start time */
  676. struct dentry *root;
  677. int first = 0; /* first vfsmount for this super_block */
  678. dout("mount start\n");
  679. mutex_lock(&fsc->client->mount_mutex);
  680. err = __ceph_open_session(fsc->client, started);
  681. if (err < 0)
  682. goto out;
  683. dout("mount opening root\n");
  684. root = open_root_dentry(fsc, "", started);
  685. if (IS_ERR(root)) {
  686. err = PTR_ERR(root);
  687. goto out;
  688. }
  689. if (fsc->sb->s_root) {
  690. dput(root);
  691. } else {
  692. fsc->sb->s_root = root;
  693. first = 1;
  694. err = ceph_fs_debugfs_init(fsc);
  695. if (err < 0)
  696. goto fail;
  697. }
  698. if (path[0] == 0) {
  699. dget(root);
  700. } else {
  701. dout("mount opening base mountpoint\n");
  702. root = open_root_dentry(fsc, path, started);
  703. if (IS_ERR(root)) {
  704. err = PTR_ERR(root);
  705. goto fail;
  706. }
  707. }
  708. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  709. dout("mount success\n");
  710. mutex_unlock(&fsc->client->mount_mutex);
  711. return root;
  712. out:
  713. mutex_unlock(&fsc->client->mount_mutex);
  714. return ERR_PTR(err);
  715. fail:
  716. if (first) {
  717. dput(fsc->sb->s_root);
  718. fsc->sb->s_root = NULL;
  719. }
  720. goto out;
  721. }
  722. static int ceph_set_super(struct super_block *s, void *data)
  723. {
  724. struct ceph_fs_client *fsc = data;
  725. int ret;
  726. dout("set_super %p data %p\n", s, data);
  727. s->s_flags = fsc->mount_options->sb_flags;
  728. s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
  729. s->s_xattr = ceph_xattr_handlers;
  730. s->s_fs_info = fsc;
  731. fsc->sb = s;
  732. s->s_op = &ceph_super_ops;
  733. s->s_export_op = &ceph_export_ops;
  734. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  735. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  736. if (ret != 0)
  737. goto fail;
  738. return ret;
  739. fail:
  740. s->s_fs_info = NULL;
  741. fsc->sb = NULL;
  742. return ret;
  743. }
  744. /*
  745. * share superblock if same fs AND options
  746. */
  747. static int ceph_compare_super(struct super_block *sb, void *data)
  748. {
  749. struct ceph_fs_client *new = data;
  750. struct ceph_mount_options *fsopt = new->mount_options;
  751. struct ceph_options *opt = new->client->options;
  752. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  753. dout("ceph_compare_super %p\n", sb);
  754. if (compare_mount_options(fsopt, opt, other)) {
  755. dout("monitor(s)/mount options don't match\n");
  756. return 0;
  757. }
  758. if ((opt->flags & CEPH_OPT_FSID) &&
  759. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  760. dout("fsid doesn't match\n");
  761. return 0;
  762. }
  763. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  764. dout("flags differ\n");
  765. return 0;
  766. }
  767. return 1;
  768. }
  769. /*
  770. * construct our own bdi so we can control readahead, etc.
  771. */
  772. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  773. static int ceph_register_bdi(struct super_block *sb,
  774. struct ceph_fs_client *fsc)
  775. {
  776. int err;
  777. /* set ra_pages based on rasize mount option? */
  778. if (fsc->mount_options->rasize >= PAGE_CACHE_SIZE)
  779. fsc->backing_dev_info.ra_pages =
  780. (fsc->mount_options->rasize + PAGE_CACHE_SIZE - 1)
  781. >> PAGE_SHIFT;
  782. else
  783. fsc->backing_dev_info.ra_pages =
  784. VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE;
  785. err = bdi_register(&fsc->backing_dev_info, NULL, "ceph-%ld",
  786. atomic_long_inc_return(&bdi_seq));
  787. if (!err)
  788. sb->s_bdi = &fsc->backing_dev_info;
  789. return err;
  790. }
  791. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  792. int flags, const char *dev_name, void *data)
  793. {
  794. struct super_block *sb;
  795. struct ceph_fs_client *fsc;
  796. struct dentry *res;
  797. int err;
  798. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  799. const char *path = NULL;
  800. struct ceph_mount_options *fsopt = NULL;
  801. struct ceph_options *opt = NULL;
  802. dout("ceph_mount\n");
  803. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  804. flags |= MS_POSIXACL;
  805. #endif
  806. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name, &path);
  807. if (err < 0) {
  808. res = ERR_PTR(err);
  809. goto out_final;
  810. }
  811. /* create client (which we may/may not use) */
  812. fsc = create_fs_client(fsopt, opt);
  813. if (IS_ERR(fsc)) {
  814. res = ERR_CAST(fsc);
  815. destroy_mount_options(fsopt);
  816. ceph_destroy_options(opt);
  817. goto out_final;
  818. }
  819. err = ceph_mdsc_init(fsc);
  820. if (err < 0) {
  821. res = ERR_PTR(err);
  822. goto out;
  823. }
  824. if (ceph_test_opt(fsc->client, NOSHARE))
  825. compare_super = NULL;
  826. sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
  827. if (IS_ERR(sb)) {
  828. res = ERR_CAST(sb);
  829. goto out;
  830. }
  831. if (ceph_sb_to_client(sb) != fsc) {
  832. ceph_mdsc_destroy(fsc);
  833. destroy_fs_client(fsc);
  834. fsc = ceph_sb_to_client(sb);
  835. dout("get_sb got existing client %p\n", fsc);
  836. } else {
  837. dout("get_sb using new client %p\n", fsc);
  838. err = ceph_register_bdi(sb, fsc);
  839. if (err < 0) {
  840. res = ERR_PTR(err);
  841. goto out_splat;
  842. }
  843. }
  844. res = ceph_real_mount(fsc, path);
  845. if (IS_ERR(res))
  846. goto out_splat;
  847. dout("root %p inode %p ino %llx.%llx\n", res,
  848. res->d_inode, ceph_vinop(res->d_inode));
  849. return res;
  850. out_splat:
  851. ceph_mdsc_close_sessions(fsc->mdsc);
  852. deactivate_locked_super(sb);
  853. goto out_final;
  854. out:
  855. ceph_mdsc_destroy(fsc);
  856. destroy_fs_client(fsc);
  857. out_final:
  858. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  859. return res;
  860. }
  861. static void ceph_kill_sb(struct super_block *s)
  862. {
  863. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  864. dev_t dev = s->s_dev;
  865. dout("kill_sb %p\n", s);
  866. ceph_mdsc_pre_umount(fsc->mdsc);
  867. generic_shutdown_super(s);
  868. ceph_mdsc_destroy(fsc);
  869. destroy_fs_client(fsc);
  870. free_anon_bdev(dev);
  871. }
  872. static struct file_system_type ceph_fs_type = {
  873. .owner = THIS_MODULE,
  874. .name = "ceph",
  875. .mount = ceph_mount,
  876. .kill_sb = ceph_kill_sb,
  877. .fs_flags = FS_RENAME_DOES_D_MOVE,
  878. };
  879. MODULE_ALIAS_FS("ceph");
  880. static int __init init_ceph(void)
  881. {
  882. int ret = init_caches();
  883. if (ret)
  884. goto out;
  885. ceph_flock_init();
  886. ceph_xattr_init();
  887. ret = ceph_snap_init();
  888. if (ret)
  889. goto out_xattr;
  890. ret = register_filesystem(&ceph_fs_type);
  891. if (ret)
  892. goto out_snap;
  893. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  894. return 0;
  895. out_snap:
  896. ceph_snap_exit();
  897. out_xattr:
  898. ceph_xattr_exit();
  899. destroy_caches();
  900. out:
  901. return ret;
  902. }
  903. static void __exit exit_ceph(void)
  904. {
  905. dout("exit_ceph\n");
  906. unregister_filesystem(&ceph_fs_type);
  907. ceph_snap_exit();
  908. ceph_xattr_exit();
  909. destroy_caches();
  910. }
  911. module_init(init_ceph);
  912. module_exit(exit_ceph);
  913. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  914. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  915. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  916. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  917. MODULE_LICENSE("GPL");