super.c 25 KB

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