super.c 26 KB

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