super.c 23 KB

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