super.c 25 KB

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