super.c 25 KB

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