super.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. #include "ceph_debug.h"
  2. #include <linux/backing-dev.h>
  3. #include <linux/fs.h>
  4. #include <linux/inet.h>
  5. #include <linux/in6.h>
  6. #include <linux/module.h>
  7. #include <linux/mount.h>
  8. #include <linux/parser.h>
  9. #include <linux/rwsem.h>
  10. #include <linux/sched.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/statfs.h>
  13. #include <linux/string.h>
  14. #include <linux/version.h>
  15. #include <linux/vmalloc.h>
  16. #include "decode.h"
  17. #include "super.h"
  18. #include "mon_client.h"
  19. /*
  20. * Ceph superblock operations
  21. *
  22. * Handle the basics of mounting, unmounting.
  23. */
  24. /*
  25. * find filename portion of a path (/foo/bar/baz -> baz)
  26. */
  27. const char *ceph_file_part(const char *s, int len)
  28. {
  29. const char *e = s + len;
  30. while (e != s && *(e-1) != '/')
  31. e--;
  32. return e;
  33. }
  34. /*
  35. * super ops
  36. */
  37. static void ceph_put_super(struct super_block *s)
  38. {
  39. struct ceph_client *cl = ceph_client(s);
  40. dout("put_super\n");
  41. ceph_mdsc_close_sessions(&cl->mdsc);
  42. return;
  43. }
  44. static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
  45. {
  46. struct ceph_client *client = ceph_inode_to_client(dentry->d_inode);
  47. struct ceph_monmap *monmap = client->monc.monmap;
  48. struct ceph_statfs st;
  49. u64 fsid;
  50. int err;
  51. dout("statfs\n");
  52. err = ceph_monc_do_statfs(&client->monc, &st);
  53. if (err < 0)
  54. return err;
  55. /* fill in kstatfs */
  56. buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
  57. /*
  58. * express utilization in terms of large blocks to avoid
  59. * overflow on 32-bit machines.
  60. */
  61. buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
  62. buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
  63. buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >>
  64. (CEPH_BLOCK_SHIFT-10);
  65. buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  66. buf->f_files = le64_to_cpu(st.num_objects);
  67. buf->f_ffree = -1;
  68. buf->f_namelen = PATH_MAX;
  69. buf->f_frsize = PAGE_CACHE_SIZE;
  70. /* leave fsid little-endian, regardless of host endianness */
  71. fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
  72. buf->f_fsid.val[0] = fsid & 0xffffffff;
  73. buf->f_fsid.val[1] = fsid >> 32;
  74. return 0;
  75. }
  76. static int ceph_syncfs(struct super_block *sb, int wait)
  77. {
  78. dout("sync_fs %d\n", wait);
  79. ceph_osdc_sync(&ceph_client(sb)->osdc);
  80. ceph_mdsc_sync(&ceph_client(sb)->mdsc);
  81. dout("sync_fs %d done\n", wait);
  82. return 0;
  83. }
  84. /**
  85. * ceph_show_options - Show mount options in /proc/mounts
  86. * @m: seq_file to write to
  87. * @mnt: mount descriptor
  88. */
  89. static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt)
  90. {
  91. struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb);
  92. struct ceph_mount_args *args = client->mount_args;
  93. if (args->flags & CEPH_OPT_FSID)
  94. seq_printf(m, ",fsidmajor=%llu,fsidminor%llu",
  95. le64_to_cpu(*(__le64 *)&args->fsid.fsid[0]),
  96. le64_to_cpu(*(__le64 *)&args->fsid.fsid[8]));
  97. if (args->flags & CEPH_OPT_NOSHARE)
  98. seq_puts(m, ",noshare");
  99. if (args->flags & CEPH_OPT_DIRSTAT)
  100. seq_puts(m, ",dirstat");
  101. if ((args->flags & CEPH_OPT_RBYTES) == 0)
  102. seq_puts(m, ",norbytes");
  103. if (args->flags & CEPH_OPT_NOCRC)
  104. seq_puts(m, ",nocrc");
  105. if (args->flags & CEPH_OPT_NOASYNCREADDIR)
  106. seq_puts(m, ",noasyncreaddir");
  107. if (strcmp(args->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  108. seq_printf(m, ",snapdirname=%s", args->snapdir_name);
  109. if (args->secret)
  110. seq_puts(m, ",secret=<hidden>");
  111. return 0;
  112. }
  113. /*
  114. * caches
  115. */
  116. struct kmem_cache *ceph_inode_cachep;
  117. struct kmem_cache *ceph_cap_cachep;
  118. struct kmem_cache *ceph_dentry_cachep;
  119. struct kmem_cache *ceph_file_cachep;
  120. static void ceph_inode_init_once(void *foo)
  121. {
  122. struct ceph_inode_info *ci = foo;
  123. inode_init_once(&ci->vfs_inode);
  124. }
  125. static int __init init_caches(void)
  126. {
  127. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  128. sizeof(struct ceph_inode_info),
  129. __alignof__(struct ceph_inode_info),
  130. (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
  131. ceph_inode_init_once);
  132. if (ceph_inode_cachep == NULL)
  133. return -ENOMEM;
  134. ceph_cap_cachep = KMEM_CACHE(ceph_cap,
  135. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  136. if (ceph_cap_cachep == NULL)
  137. goto bad_cap;
  138. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  139. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  140. if (ceph_dentry_cachep == NULL)
  141. goto bad_dentry;
  142. ceph_file_cachep = KMEM_CACHE(ceph_file_info,
  143. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  144. if (ceph_file_cachep == NULL)
  145. goto bad_file;
  146. return 0;
  147. bad_file:
  148. kmem_cache_destroy(ceph_dentry_cachep);
  149. bad_dentry:
  150. kmem_cache_destroy(ceph_cap_cachep);
  151. bad_cap:
  152. kmem_cache_destroy(ceph_inode_cachep);
  153. return -ENOMEM;
  154. }
  155. static void destroy_caches(void)
  156. {
  157. kmem_cache_destroy(ceph_inode_cachep);
  158. kmem_cache_destroy(ceph_cap_cachep);
  159. kmem_cache_destroy(ceph_dentry_cachep);
  160. kmem_cache_destroy(ceph_file_cachep);
  161. }
  162. /*
  163. * ceph_umount_begin - initiate forced umount. Tear down down the
  164. * mount, skipping steps that may hang while waiting for server(s).
  165. */
  166. static void ceph_umount_begin(struct super_block *sb)
  167. {
  168. struct ceph_client *client = ceph_sb_to_client(sb);
  169. dout("ceph_umount_begin - starting forced umount\n");
  170. if (!client)
  171. return;
  172. client->mount_state = CEPH_MOUNT_SHUTDOWN;
  173. return;
  174. }
  175. static const struct super_operations ceph_super_ops = {
  176. .alloc_inode = ceph_alloc_inode,
  177. .destroy_inode = ceph_destroy_inode,
  178. .write_inode = ceph_write_inode,
  179. .sync_fs = ceph_syncfs,
  180. .put_super = ceph_put_super,
  181. .show_options = ceph_show_options,
  182. .statfs = ceph_statfs,
  183. .umount_begin = ceph_umount_begin,
  184. };
  185. const char *ceph_msg_type_name(int type)
  186. {
  187. switch (type) {
  188. case CEPH_MSG_SHUTDOWN: return "shutdown";
  189. case CEPH_MSG_PING: return "ping";
  190. case CEPH_MSG_MON_MAP: return "mon_map";
  191. case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
  192. case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
  193. case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
  194. case CEPH_MSG_CLIENT_MOUNT: return "client_mount";
  195. case CEPH_MSG_CLIENT_MOUNT_ACK: return "client_mount_ack";
  196. case CEPH_MSG_STATFS: return "statfs";
  197. case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
  198. case CEPH_MSG_MDS_MAP: return "mds_map";
  199. case CEPH_MSG_CLIENT_SESSION: return "client_session";
  200. case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
  201. case CEPH_MSG_CLIENT_REQUEST: return "client_request";
  202. case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
  203. case CEPH_MSG_CLIENT_REPLY: return "client_reply";
  204. case CEPH_MSG_CLIENT_CAPS: return "client_caps";
  205. case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
  206. case CEPH_MSG_CLIENT_SNAP: return "client_snap";
  207. case CEPH_MSG_CLIENT_LEASE: return "client_lease";
  208. case CEPH_MSG_OSD_MAP: return "osd_map";
  209. case CEPH_MSG_OSD_OP: return "osd_op";
  210. case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
  211. default: return "unknown";
  212. }
  213. }
  214. /*
  215. * mount options
  216. */
  217. enum {
  218. Opt_fsidmajor,
  219. Opt_fsidminor,
  220. Opt_monport,
  221. Opt_wsize,
  222. Opt_rsize,
  223. Opt_osdtimeout,
  224. Opt_mount_timeout,
  225. Opt_caps_wanted_delay_min,
  226. Opt_caps_wanted_delay_max,
  227. Opt_readdir_max_entries,
  228. Opt_last_int,
  229. /* int args above */
  230. Opt_snapdirname,
  231. Opt_secret,
  232. Opt_last_string,
  233. /* string args above */
  234. Opt_ip,
  235. Opt_noshare,
  236. Opt_dirstat,
  237. Opt_nodirstat,
  238. Opt_rbytes,
  239. Opt_norbytes,
  240. Opt_nocrc,
  241. Opt_noasyncreaddir,
  242. };
  243. static match_table_t arg_tokens = {
  244. {Opt_fsidmajor, "fsidmajor=%ld"},
  245. {Opt_fsidminor, "fsidminor=%ld"},
  246. {Opt_monport, "monport=%d"},
  247. {Opt_wsize, "wsize=%d"},
  248. {Opt_rsize, "rsize=%d"},
  249. {Opt_osdtimeout, "osdtimeout=%d"},
  250. {Opt_mount_timeout, "mount_timeout=%d"},
  251. {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
  252. {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
  253. {Opt_readdir_max_entries, "readdir_max_entries=%d"},
  254. /* int args above */
  255. {Opt_snapdirname, "snapdirname=%s"},
  256. {Opt_secret, "secret=%s"},
  257. /* string args above */
  258. {Opt_ip, "ip=%s"},
  259. {Opt_noshare, "noshare"},
  260. {Opt_dirstat, "dirstat"},
  261. {Opt_nodirstat, "nodirstat"},
  262. {Opt_rbytes, "rbytes"},
  263. {Opt_norbytes, "norbytes"},
  264. {Opt_nocrc, "nocrc"},
  265. {Opt_noasyncreaddir, "noasyncreaddir"},
  266. {-1, NULL}
  267. };
  268. static struct ceph_mount_args *parse_mount_args(int flags, char *options,
  269. const char *dev_name,
  270. const char **path)
  271. {
  272. struct ceph_mount_args *args;
  273. const char *c;
  274. int err = -ENOMEM;
  275. substring_t argstr[MAX_OPT_ARGS];
  276. args = kzalloc(sizeof(*args), GFP_KERNEL);
  277. if (!args)
  278. return ERR_PTR(-ENOMEM);
  279. args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
  280. GFP_KERNEL);
  281. if (!args->mon_addr)
  282. goto out;
  283. dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
  284. /* start with defaults */
  285. args->sb_flags = flags;
  286. args->flags = CEPH_OPT_DEFAULT;
  287. args->osd_timeout = 5; /* seconds */
  288. args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
  289. args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  290. args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  291. args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
  292. args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
  293. args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
  294. args->max_readdir = 1024;
  295. /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
  296. err = -EINVAL;
  297. if (!dev_name)
  298. goto out;
  299. *path = strstr(dev_name, ":/");
  300. if (*path == NULL) {
  301. pr_err("device name is missing path (no :/ in %s)\n",
  302. dev_name);
  303. goto out;
  304. }
  305. /* get mon ip(s) */
  306. err = ceph_parse_ips(dev_name, *path, args->mon_addr,
  307. CEPH_MAX_MON, &args->num_mon);
  308. if (err < 0)
  309. goto out;
  310. /* path on server */
  311. *path += 2;
  312. dout("server path '%s'\n", *path);
  313. /* parse mount options */
  314. while ((c = strsep(&options, ",")) != NULL) {
  315. int token, intval, ret;
  316. if (!*c)
  317. continue;
  318. err = -EINVAL;
  319. token = match_token((char *)c, arg_tokens, argstr);
  320. if (token < 0) {
  321. pr_err("bad mount option at '%s'\n", c);
  322. goto out;
  323. }
  324. if (token < Opt_last_int) {
  325. ret = match_int(&argstr[0], &intval);
  326. if (ret < 0) {
  327. pr_err("bad mount option arg (not int) "
  328. "at '%s'\n", c);
  329. continue;
  330. }
  331. dout("got int token %d val %d\n", token, intval);
  332. } else if (token > Opt_last_int && token < Opt_last_string) {
  333. dout("got string token %d val %s\n", token,
  334. argstr[0].from);
  335. } else {
  336. dout("got token %d\n", token);
  337. }
  338. switch (token) {
  339. case Opt_fsidmajor:
  340. *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
  341. break;
  342. case Opt_fsidminor:
  343. *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
  344. break;
  345. case Opt_ip:
  346. err = ceph_parse_ips(argstr[0].from,
  347. argstr[0].to,
  348. &args->my_addr,
  349. 1, NULL);
  350. if (err < 0)
  351. goto out;
  352. args->flags |= CEPH_OPT_MYIP;
  353. break;
  354. case Opt_snapdirname:
  355. kfree(args->snapdir_name);
  356. args->snapdir_name = kstrndup(argstr[0].from,
  357. argstr[0].to-argstr[0].from,
  358. GFP_KERNEL);
  359. break;
  360. case Opt_secret:
  361. args->secret = kstrndup(argstr[0].from,
  362. argstr[0].to-argstr[0].from,
  363. GFP_KERNEL);
  364. break;
  365. /* misc */
  366. case Opt_wsize:
  367. args->wsize = intval;
  368. break;
  369. case Opt_rsize:
  370. args->rsize = intval;
  371. break;
  372. case Opt_osdtimeout:
  373. args->osd_timeout = intval;
  374. break;
  375. case Opt_mount_timeout:
  376. args->mount_timeout = intval;
  377. break;
  378. case Opt_caps_wanted_delay_min:
  379. args->caps_wanted_delay_min = intval;
  380. break;
  381. case Opt_caps_wanted_delay_max:
  382. args->caps_wanted_delay_max = intval;
  383. break;
  384. case Opt_readdir_max_entries:
  385. args->max_readdir = intval;
  386. break;
  387. case Opt_noshare:
  388. args->flags |= CEPH_OPT_NOSHARE;
  389. break;
  390. case Opt_dirstat:
  391. args->flags |= CEPH_OPT_DIRSTAT;
  392. break;
  393. case Opt_nodirstat:
  394. args->flags &= ~CEPH_OPT_DIRSTAT;
  395. break;
  396. case Opt_rbytes:
  397. args->flags |= CEPH_OPT_RBYTES;
  398. break;
  399. case Opt_norbytes:
  400. args->flags &= ~CEPH_OPT_RBYTES;
  401. break;
  402. case Opt_nocrc:
  403. args->flags |= CEPH_OPT_NOCRC;
  404. break;
  405. case Opt_noasyncreaddir:
  406. args->flags |= CEPH_OPT_NOASYNCREADDIR;
  407. break;
  408. default:
  409. BUG_ON(token);
  410. }
  411. }
  412. return args;
  413. out:
  414. kfree(args->mon_addr);
  415. kfree(args);
  416. return ERR_PTR(err);
  417. }
  418. static void destroy_mount_args(struct ceph_mount_args *args)
  419. {
  420. dout("destroy_mount_args %p\n", args);
  421. kfree(args->snapdir_name);
  422. args->snapdir_name = NULL;
  423. kfree(args->secret);
  424. args->secret = NULL;
  425. kfree(args);
  426. }
  427. /*
  428. * create a fresh client instance
  429. */
  430. static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
  431. {
  432. struct ceph_client *client;
  433. int err = -ENOMEM;
  434. client = kzalloc(sizeof(*client), GFP_KERNEL);
  435. if (client == NULL)
  436. return ERR_PTR(-ENOMEM);
  437. mutex_init(&client->mount_mutex);
  438. init_waitqueue_head(&client->mount_wq);
  439. client->sb = NULL;
  440. client->mount_state = CEPH_MOUNT_MOUNTING;
  441. client->whoami = -1;
  442. client->mount_args = args;
  443. client->msgr = NULL;
  444. client->mount_err = 0;
  445. client->signed_ticket = NULL;
  446. client->signed_ticket_len = 0;
  447. err = bdi_init(&client->backing_dev_info);
  448. if (err < 0)
  449. goto fail;
  450. err = -ENOMEM;
  451. client->wb_wq = create_workqueue("ceph-writeback");
  452. if (client->wb_wq == NULL)
  453. goto fail_bdi;
  454. client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
  455. if (client->pg_inv_wq == NULL)
  456. goto fail_wb_wq;
  457. client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
  458. if (client->trunc_wq == NULL)
  459. goto fail_pg_inv_wq;
  460. /* subsystems */
  461. err = ceph_monc_init(&client->monc, client);
  462. if (err < 0)
  463. goto fail_trunc_wq;
  464. err = ceph_osdc_init(&client->osdc, client);
  465. if (err < 0)
  466. goto fail_monc;
  467. err = ceph_mdsc_init(&client->mdsc, client);
  468. if (err < 0)
  469. goto fail_osdc;
  470. return client;
  471. fail_osdc:
  472. ceph_osdc_stop(&client->osdc);
  473. fail_monc:
  474. ceph_monc_stop(&client->monc);
  475. fail_trunc_wq:
  476. destroy_workqueue(client->trunc_wq);
  477. fail_pg_inv_wq:
  478. destroy_workqueue(client->pg_inv_wq);
  479. fail_wb_wq:
  480. destroy_workqueue(client->wb_wq);
  481. fail_bdi:
  482. bdi_destroy(&client->backing_dev_info);
  483. fail:
  484. kfree(client);
  485. return ERR_PTR(err);
  486. }
  487. static void ceph_destroy_client(struct ceph_client *client)
  488. {
  489. dout("destroy_client %p\n", client);
  490. /* unmount */
  491. ceph_mdsc_stop(&client->mdsc);
  492. ceph_monc_stop(&client->monc);
  493. ceph_osdc_stop(&client->osdc);
  494. kfree(client->signed_ticket);
  495. ceph_debugfs_client_cleanup(client);
  496. destroy_workqueue(client->wb_wq);
  497. destroy_workqueue(client->pg_inv_wq);
  498. destroy_workqueue(client->trunc_wq);
  499. if (client->msgr)
  500. ceph_messenger_destroy(client->msgr);
  501. if (client->wb_pagevec_pool)
  502. mempool_destroy(client->wb_pagevec_pool);
  503. destroy_mount_args(client->mount_args);
  504. kfree(client);
  505. dout("destroy_client %p done\n", client);
  506. }
  507. /*
  508. * true if we have the mon map (and have thus joined the cluster)
  509. */
  510. static int have_mon_map(struct ceph_client *client)
  511. {
  512. return client->monc.monmap && client->monc.monmap->epoch;
  513. }
  514. /*
  515. * Bootstrap mount by opening the root directory. Note the mount
  516. * @started time from caller, and time out if this takes too long.
  517. */
  518. static struct dentry *open_root_dentry(struct ceph_client *client,
  519. const char *path,
  520. unsigned long started)
  521. {
  522. struct ceph_mds_client *mdsc = &client->mdsc;
  523. struct ceph_mds_request *req = NULL;
  524. int err;
  525. struct dentry *root;
  526. /* open dir */
  527. dout("open_root_inode opening '%s'\n", path);
  528. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  529. if (IS_ERR(req))
  530. return ERR_PTR(PTR_ERR(req));
  531. req->r_path1 = kstrdup(path, GFP_NOFS);
  532. req->r_ino1.ino = CEPH_INO_ROOT;
  533. req->r_ino1.snap = CEPH_NOSNAP;
  534. req->r_started = started;
  535. req->r_timeout = client->mount_args->mount_timeout * HZ;
  536. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  537. req->r_num_caps = 2;
  538. err = ceph_mdsc_do_request(mdsc, NULL, req);
  539. if (err == 0) {
  540. dout("open_root_inode success\n");
  541. if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
  542. client->sb->s_root == NULL)
  543. root = d_alloc_root(req->r_target_inode);
  544. else
  545. root = d_obtain_alias(req->r_target_inode);
  546. req->r_target_inode = NULL;
  547. dout("open_root_inode success, root dentry is %p\n", root);
  548. } else {
  549. root = ERR_PTR(err);
  550. }
  551. ceph_mdsc_put_request(req);
  552. return root;
  553. }
  554. /*
  555. * mount: join the ceph cluster, and open root directory.
  556. */
  557. static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
  558. const char *path)
  559. {
  560. struct ceph_entity_addr *myaddr = NULL;
  561. int err;
  562. unsigned long timeout = client->mount_args->mount_timeout * HZ;
  563. unsigned long started = jiffies; /* note the start time */
  564. struct dentry *root;
  565. dout("mount start\n");
  566. mutex_lock(&client->mount_mutex);
  567. /* initialize the messenger */
  568. if (client->msgr == NULL) {
  569. if (ceph_test_opt(client, MYIP))
  570. myaddr = &client->mount_args->my_addr;
  571. client->msgr = ceph_messenger_create(myaddr);
  572. if (IS_ERR(client->msgr)) {
  573. err = PTR_ERR(client->msgr);
  574. client->msgr = NULL;
  575. goto out;
  576. }
  577. client->msgr->nocrc = ceph_test_opt(client, NOCRC);
  578. }
  579. /* send mount request, and wait for mon, mds, and osd maps */
  580. err = ceph_monc_request_mount(&client->monc);
  581. if (err < 0)
  582. goto out;
  583. while (!have_mon_map(client) && !client->mount_err) {
  584. err = -EIO;
  585. if (timeout && time_after_eq(jiffies, started + timeout))
  586. goto out;
  587. /* wait */
  588. dout("mount waiting for mount\n");
  589. err = wait_event_interruptible_timeout(client->mount_wq,
  590. client->mount_err || have_mon_map(client),
  591. timeout);
  592. if (err == -EINTR || err == -ERESTARTSYS)
  593. goto out;
  594. if (client->mount_err) {
  595. err = client->mount_err;
  596. goto out;
  597. }
  598. }
  599. dout("mount opening root\n");
  600. root = open_root_dentry(client, "", started);
  601. if (IS_ERR(root)) {
  602. err = PTR_ERR(root);
  603. goto out;
  604. }
  605. if (client->sb->s_root)
  606. dput(root);
  607. else
  608. client->sb->s_root = root;
  609. if (path[0] == 0) {
  610. dget(root);
  611. } else {
  612. dout("mount opening base mountpoint\n");
  613. root = open_root_dentry(client, path, started);
  614. if (IS_ERR(root)) {
  615. err = PTR_ERR(root);
  616. dput(client->sb->s_root);
  617. client->sb->s_root = NULL;
  618. goto out;
  619. }
  620. }
  621. mnt->mnt_root = root;
  622. mnt->mnt_sb = client->sb;
  623. client->mount_state = CEPH_MOUNT_MOUNTED;
  624. dout("mount success\n");
  625. err = 0;
  626. out:
  627. mutex_unlock(&client->mount_mutex);
  628. return err;
  629. }
  630. static int ceph_set_super(struct super_block *s, void *data)
  631. {
  632. struct ceph_client *client = data;
  633. int ret;
  634. dout("set_super %p data %p\n", s, data);
  635. s->s_flags = client->mount_args->sb_flags;
  636. s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
  637. s->s_fs_info = client;
  638. client->sb = s;
  639. s->s_op = &ceph_super_ops;
  640. s->s_export_op = &ceph_export_ops;
  641. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  642. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  643. if (ret != 0)
  644. goto fail;
  645. return ret;
  646. fail:
  647. s->s_fs_info = NULL;
  648. client->sb = NULL;
  649. return ret;
  650. }
  651. /*
  652. * share superblock if same fs AND options
  653. */
  654. static int ceph_compare_super(struct super_block *sb, void *data)
  655. {
  656. struct ceph_client *new = data;
  657. struct ceph_mount_args *args = new->mount_args;
  658. struct ceph_client *other = ceph_sb_to_client(sb);
  659. int i;
  660. dout("ceph_compare_super %p\n", sb);
  661. if (args->flags & CEPH_OPT_FSID) {
  662. if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
  663. dout("fsid doesn't match\n");
  664. return 0;
  665. }
  666. } else {
  667. /* do we share (a) monitor? */
  668. for (i = 0; i < new->monc.monmap->num_mon; i++)
  669. if (ceph_monmap_contains(other->monc.monmap,
  670. &new->monc.monmap->mon_inst[i].addr))
  671. break;
  672. if (i == new->monc.monmap->num_mon) {
  673. dout("mon ip not part of monmap\n");
  674. return 0;
  675. }
  676. dout("mon ip matches existing sb %p\n", sb);
  677. }
  678. if (args->sb_flags != other->mount_args->sb_flags) {
  679. dout("flags differ\n");
  680. return 0;
  681. }
  682. return 1;
  683. }
  684. /*
  685. * construct our own bdi so we can control readahead, etc.
  686. */
  687. static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
  688. {
  689. int err;
  690. sb->s_bdi = &client->backing_dev_info;
  691. /* set ra_pages based on rsize mount option? */
  692. if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
  693. client->backing_dev_info.ra_pages =
  694. (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
  695. >> PAGE_SHIFT;
  696. err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
  697. return err;
  698. }
  699. static int ceph_get_sb(struct file_system_type *fs_type,
  700. int flags, const char *dev_name, void *data,
  701. struct vfsmount *mnt)
  702. {
  703. struct super_block *sb;
  704. struct ceph_client *client;
  705. int err;
  706. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  707. const char *path = NULL;
  708. struct ceph_mount_args *args;
  709. dout("ceph_get_sb\n");
  710. args = parse_mount_args(flags, data, dev_name, &path);
  711. if (IS_ERR(args)) {
  712. err = PTR_ERR(args);
  713. goto out_final;
  714. }
  715. /* create client (which we may/may not use) */
  716. client = ceph_create_client(args);
  717. if (IS_ERR(client)) {
  718. err = PTR_ERR(client);
  719. goto out_final;
  720. }
  721. if (client->mount_args->flags & CEPH_OPT_NOSHARE)
  722. compare_super = NULL;
  723. sb = sget(fs_type, compare_super, ceph_set_super, client);
  724. if (IS_ERR(sb)) {
  725. err = PTR_ERR(sb);
  726. goto out;
  727. }
  728. if (ceph_client(sb) != client) {
  729. ceph_destroy_client(client);
  730. client = ceph_client(sb);
  731. dout("get_sb got existing client %p\n", client);
  732. } else {
  733. dout("get_sb using new client %p\n", client);
  734. /* set up mempools */
  735. err = -ENOMEM;
  736. client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
  737. client->mount_args->wsize >> PAGE_CACHE_SHIFT);
  738. if (!client->wb_pagevec_pool)
  739. goto out_splat;
  740. err = ceph_register_bdi(sb, client);
  741. if (err < 0)
  742. goto out_splat;
  743. }
  744. err = ceph_mount(client, mnt, path);
  745. if (err < 0)
  746. goto out_splat;
  747. dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
  748. mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
  749. return 0;
  750. out_splat:
  751. ceph_mdsc_close_sessions(&client->mdsc);
  752. up_write(&sb->s_umount);
  753. deactivate_super(sb);
  754. goto out_final;
  755. out:
  756. ceph_destroy_client(client);
  757. out_final:
  758. dout("ceph_get_sb fail %d\n", err);
  759. return err;
  760. }
  761. static void ceph_kill_sb(struct super_block *s)
  762. {
  763. struct ceph_client *client = ceph_sb_to_client(s);
  764. dout("kill_sb %p\n", s);
  765. ceph_mdsc_pre_umount(&client->mdsc);
  766. kill_anon_super(s); /* will call put_super after sb is r/o */
  767. bdi_unregister(&client->backing_dev_info);
  768. bdi_destroy(&client->backing_dev_info);
  769. ceph_destroy_client(client);
  770. }
  771. static struct file_system_type ceph_fs_type = {
  772. .owner = THIS_MODULE,
  773. .name = "ceph",
  774. .get_sb = ceph_get_sb,
  775. .kill_sb = ceph_kill_sb,
  776. .fs_flags = FS_RENAME_DOES_D_MOVE,
  777. };
  778. #define _STRINGIFY(x) #x
  779. #define STRINGIFY(x) _STRINGIFY(x)
  780. static int __init init_ceph(void)
  781. {
  782. int ret = 0;
  783. ret = ceph_debugfs_init();
  784. if (ret < 0)
  785. goto out;
  786. ret = ceph_msgr_init();
  787. if (ret < 0)
  788. goto out_debugfs;
  789. ret = init_caches();
  790. if (ret)
  791. goto out_msgr;
  792. ceph_caps_init();
  793. ret = register_filesystem(&ceph_fs_type);
  794. if (ret)
  795. goto out_icache;
  796. pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
  797. CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
  798. CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
  799. return 0;
  800. out_icache:
  801. destroy_caches();
  802. out_msgr:
  803. ceph_msgr_exit();
  804. out_debugfs:
  805. ceph_debugfs_cleanup();
  806. out:
  807. return ret;
  808. }
  809. static void __exit exit_ceph(void)
  810. {
  811. dout("exit_ceph\n");
  812. unregister_filesystem(&ceph_fs_type);
  813. ceph_caps_finalize();
  814. destroy_caches();
  815. ceph_msgr_exit();
  816. ceph_debugfs_cleanup();
  817. }
  818. module_init(init_ceph);
  819. module_exit(exit_ceph);
  820. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  821. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  822. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  823. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  824. MODULE_LICENSE("GPL");