super.c 27 KB

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