super.c 23 KB

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