super.c 25 KB

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