super.c 27 KB

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