super.c 25 KB

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