super.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. #include <linux/ceph/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 "super.h"
  16. #include "mds_client.h"
  17. #include "cache.h"
  18. #include <linux/ceph/ceph_features.h>
  19. #include <linux/ceph/decode.h>
  20. #include <linux/ceph/mon_client.h>
  21. #include <linux/ceph/auth.h>
  22. #include <linux/ceph/debugfs.h>
  23. /*
  24. * Ceph superblock operations
  25. *
  26. * Handle the basics of mounting, unmounting.
  27. */
  28. /*
  29. * super ops
  30. */
  31. static void ceph_put_super(struct super_block *s)
  32. {
  33. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  34. dout("put_super\n");
  35. ceph_mdsc_close_sessions(fsc->mdsc);
  36. }
  37. static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
  38. {
  39. struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
  40. struct ceph_mon_client *monc = &fsc->client->monc;
  41. struct ceph_statfs st;
  42. u64 fsid;
  43. int err;
  44. u64 data_pool;
  45. if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
  46. data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
  47. } else {
  48. data_pool = CEPH_NOPOOL;
  49. }
  50. dout("statfs\n");
  51. err = ceph_monc_do_statfs(monc, data_pool, &st);
  52. if (err < 0)
  53. return err;
  54. /* fill in kstatfs */
  55. buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
  56. /*
  57. * express utilization in terms of large blocks to avoid
  58. * overflow on 32-bit machines.
  59. *
  60. * NOTE: for the time being, we make bsize == frsize to humor
  61. * not-yet-ancient versions of glibc that are broken.
  62. * Someday, we will probably want to report a real block
  63. * size... whatever that may mean for a network file system!
  64. */
  65. buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
  66. buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
  67. /*
  68. * By default use root quota for stats; fallback to overall filesystem
  69. * usage if using 'noquotadf' mount option or if the root dir doesn't
  70. * have max_bytes quota set.
  71. */
  72. if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
  73. !ceph_quota_update_statfs(fsc, buf)) {
  74. buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
  75. buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  76. buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
  77. }
  78. buf->f_files = le64_to_cpu(st.num_objects);
  79. buf->f_ffree = -1;
  80. buf->f_namelen = NAME_MAX;
  81. /* Must convert the fsid, for consistent values across arches */
  82. mutex_lock(&monc->mutex);
  83. fsid = le64_to_cpu(*(__le64 *)(&monc->monmap->fsid)) ^
  84. le64_to_cpu(*((__le64 *)&monc->monmap->fsid + 1));
  85. mutex_unlock(&monc->mutex);
  86. buf->f_fsid.val[0] = fsid & 0xffffffff;
  87. buf->f_fsid.val[1] = fsid >> 32;
  88. return 0;
  89. }
  90. static int ceph_sync_fs(struct super_block *sb, int wait)
  91. {
  92. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  93. if (!wait) {
  94. dout("sync_fs (non-blocking)\n");
  95. ceph_flush_dirty_caps(fsc->mdsc);
  96. dout("sync_fs (non-blocking) done\n");
  97. return 0;
  98. }
  99. dout("sync_fs (blocking)\n");
  100. ceph_osdc_sync(&fsc->client->osdc);
  101. ceph_mdsc_sync(fsc->mdsc);
  102. dout("sync_fs (blocking) done\n");
  103. return 0;
  104. }
  105. /*
  106. * mount options
  107. */
  108. enum {
  109. Opt_wsize,
  110. Opt_rsize,
  111. Opt_rasize,
  112. Opt_caps_wanted_delay_min,
  113. Opt_caps_wanted_delay_max,
  114. Opt_readdir_max_entries,
  115. Opt_readdir_max_bytes,
  116. Opt_congestion_kb,
  117. Opt_last_int,
  118. /* int args above */
  119. Opt_snapdirname,
  120. Opt_mds_namespace,
  121. Opt_fscache_uniq,
  122. Opt_last_string,
  123. /* string args above */
  124. Opt_dirstat,
  125. Opt_nodirstat,
  126. Opt_rbytes,
  127. Opt_norbytes,
  128. Opt_asyncreaddir,
  129. Opt_noasyncreaddir,
  130. Opt_dcache,
  131. Opt_nodcache,
  132. Opt_ino32,
  133. Opt_noino32,
  134. Opt_fscache,
  135. Opt_nofscache,
  136. Opt_poolperm,
  137. Opt_nopoolperm,
  138. Opt_require_active_mds,
  139. Opt_norequire_active_mds,
  140. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  141. Opt_acl,
  142. #endif
  143. Opt_noacl,
  144. Opt_quotadf,
  145. Opt_noquotadf,
  146. Opt_copyfrom,
  147. Opt_nocopyfrom,
  148. };
  149. static match_table_t fsopt_tokens = {
  150. {Opt_wsize, "wsize=%d"},
  151. {Opt_rsize, "rsize=%d"},
  152. {Opt_rasize, "rasize=%d"},
  153. {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
  154. {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
  155. {Opt_readdir_max_entries, "readdir_max_entries=%d"},
  156. {Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
  157. {Opt_congestion_kb, "write_congestion_kb=%d"},
  158. /* int args above */
  159. {Opt_snapdirname, "snapdirname=%s"},
  160. {Opt_mds_namespace, "mds_namespace=%s"},
  161. {Opt_fscache_uniq, "fsc=%s"},
  162. /* string args above */
  163. {Opt_dirstat, "dirstat"},
  164. {Opt_nodirstat, "nodirstat"},
  165. {Opt_rbytes, "rbytes"},
  166. {Opt_norbytes, "norbytes"},
  167. {Opt_asyncreaddir, "asyncreaddir"},
  168. {Opt_noasyncreaddir, "noasyncreaddir"},
  169. {Opt_dcache, "dcache"},
  170. {Opt_nodcache, "nodcache"},
  171. {Opt_ino32, "ino32"},
  172. {Opt_noino32, "noino32"},
  173. {Opt_fscache, "fsc"},
  174. {Opt_nofscache, "nofsc"},
  175. {Opt_poolperm, "poolperm"},
  176. {Opt_nopoolperm, "nopoolperm"},
  177. {Opt_require_active_mds, "require_active_mds"},
  178. {Opt_norequire_active_mds, "norequire_active_mds"},
  179. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  180. {Opt_acl, "acl"},
  181. #endif
  182. {Opt_noacl, "noacl"},
  183. {Opt_quotadf, "quotadf"},
  184. {Opt_noquotadf, "noquotadf"},
  185. {Opt_copyfrom, "copyfrom"},
  186. {Opt_nocopyfrom, "nocopyfrom"},
  187. {-1, NULL}
  188. };
  189. static int parse_fsopt_token(char *c, void *private)
  190. {
  191. struct ceph_mount_options *fsopt = private;
  192. substring_t argstr[MAX_OPT_ARGS];
  193. int token, intval, ret;
  194. token = match_token((char *)c, fsopt_tokens, argstr);
  195. if (token < 0)
  196. return -EINVAL;
  197. if (token < Opt_last_int) {
  198. ret = match_int(&argstr[0], &intval);
  199. if (ret < 0) {
  200. pr_err("bad option arg (not int) at '%s'\n", c);
  201. return ret;
  202. }
  203. dout("got int token %d val %d\n", token, intval);
  204. } else if (token > Opt_last_int && token < Opt_last_string) {
  205. dout("got string token %d val %s\n", token,
  206. argstr[0].from);
  207. } else {
  208. dout("got token %d\n", token);
  209. }
  210. switch (token) {
  211. case Opt_snapdirname:
  212. kfree(fsopt->snapdir_name);
  213. fsopt->snapdir_name = kstrndup(argstr[0].from,
  214. argstr[0].to-argstr[0].from,
  215. GFP_KERNEL);
  216. if (!fsopt->snapdir_name)
  217. return -ENOMEM;
  218. break;
  219. case Opt_mds_namespace:
  220. kfree(fsopt->mds_namespace);
  221. fsopt->mds_namespace = kstrndup(argstr[0].from,
  222. argstr[0].to-argstr[0].from,
  223. GFP_KERNEL);
  224. if (!fsopt->mds_namespace)
  225. return -ENOMEM;
  226. break;
  227. case Opt_fscache_uniq:
  228. kfree(fsopt->fscache_uniq);
  229. fsopt->fscache_uniq = kstrndup(argstr[0].from,
  230. argstr[0].to-argstr[0].from,
  231. GFP_KERNEL);
  232. if (!fsopt->fscache_uniq)
  233. return -ENOMEM;
  234. fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
  235. break;
  236. /* misc */
  237. case Opt_wsize:
  238. if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_WRITE_SIZE)
  239. return -EINVAL;
  240. fsopt->wsize = ALIGN(intval, PAGE_SIZE);
  241. break;
  242. case Opt_rsize:
  243. if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_READ_SIZE)
  244. return -EINVAL;
  245. fsopt->rsize = ALIGN(intval, PAGE_SIZE);
  246. break;
  247. case Opt_rasize:
  248. if (intval < 0)
  249. return -EINVAL;
  250. fsopt->rasize = ALIGN(intval, PAGE_SIZE);
  251. break;
  252. case Opt_caps_wanted_delay_min:
  253. if (intval < 1)
  254. return -EINVAL;
  255. fsopt->caps_wanted_delay_min = intval;
  256. break;
  257. case Opt_caps_wanted_delay_max:
  258. if (intval < 1)
  259. return -EINVAL;
  260. fsopt->caps_wanted_delay_max = intval;
  261. break;
  262. case Opt_readdir_max_entries:
  263. if (intval < 1)
  264. return -EINVAL;
  265. fsopt->max_readdir = intval;
  266. break;
  267. case Opt_readdir_max_bytes:
  268. if (intval < (int)PAGE_SIZE && intval != 0)
  269. return -EINVAL;
  270. fsopt->max_readdir_bytes = intval;
  271. break;
  272. case Opt_congestion_kb:
  273. if (intval < 1024) /* at least 1M */
  274. return -EINVAL;
  275. fsopt->congestion_kb = intval;
  276. break;
  277. case Opt_dirstat:
  278. fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
  279. break;
  280. case Opt_nodirstat:
  281. fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
  282. break;
  283. case Opt_rbytes:
  284. fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
  285. break;
  286. case Opt_norbytes:
  287. fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
  288. break;
  289. case Opt_asyncreaddir:
  290. fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
  291. break;
  292. case Opt_noasyncreaddir:
  293. fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
  294. break;
  295. case Opt_dcache:
  296. fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
  297. break;
  298. case Opt_nodcache:
  299. fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
  300. break;
  301. case Opt_ino32:
  302. fsopt->flags |= CEPH_MOUNT_OPT_INO32;
  303. break;
  304. case Opt_noino32:
  305. fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
  306. break;
  307. case Opt_fscache:
  308. fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
  309. kfree(fsopt->fscache_uniq);
  310. fsopt->fscache_uniq = NULL;
  311. break;
  312. case Opt_nofscache:
  313. fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
  314. kfree(fsopt->fscache_uniq);
  315. fsopt->fscache_uniq = NULL;
  316. break;
  317. case Opt_poolperm:
  318. fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
  319. break;
  320. case Opt_nopoolperm:
  321. fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
  322. break;
  323. case Opt_require_active_mds:
  324. fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
  325. break;
  326. case Opt_norequire_active_mds:
  327. fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
  328. break;
  329. case Opt_quotadf:
  330. fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
  331. break;
  332. case Opt_noquotadf:
  333. fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
  334. break;
  335. case Opt_copyfrom:
  336. fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
  337. break;
  338. case Opt_nocopyfrom:
  339. fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
  340. break;
  341. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  342. case Opt_acl:
  343. fsopt->sb_flags |= SB_POSIXACL;
  344. break;
  345. #endif
  346. case Opt_noacl:
  347. fsopt->sb_flags &= ~SB_POSIXACL;
  348. break;
  349. default:
  350. BUG_ON(token);
  351. }
  352. return 0;
  353. }
  354. static void destroy_mount_options(struct ceph_mount_options *args)
  355. {
  356. dout("destroy_mount_options %p\n", args);
  357. kfree(args->snapdir_name);
  358. kfree(args->mds_namespace);
  359. kfree(args->server_path);
  360. kfree(args->fscache_uniq);
  361. kfree(args);
  362. }
  363. static int strcmp_null(const char *s1, const char *s2)
  364. {
  365. if (!s1 && !s2)
  366. return 0;
  367. if (s1 && !s2)
  368. return -1;
  369. if (!s1 && s2)
  370. return 1;
  371. return strcmp(s1, s2);
  372. }
  373. static int compare_mount_options(struct ceph_mount_options *new_fsopt,
  374. struct ceph_options *new_opt,
  375. struct ceph_fs_client *fsc)
  376. {
  377. struct ceph_mount_options *fsopt1 = new_fsopt;
  378. struct ceph_mount_options *fsopt2 = fsc->mount_options;
  379. int ofs = offsetof(struct ceph_mount_options, snapdir_name);
  380. int ret;
  381. ret = memcmp(fsopt1, fsopt2, ofs);
  382. if (ret)
  383. return ret;
  384. ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
  385. if (ret)
  386. return ret;
  387. ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
  388. if (ret)
  389. return ret;
  390. ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
  391. if (ret)
  392. return ret;
  393. ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
  394. if (ret)
  395. return ret;
  396. return ceph_compare_options(new_opt, fsc->client);
  397. }
  398. static int parse_mount_options(struct ceph_mount_options **pfsopt,
  399. struct ceph_options **popt,
  400. int flags, char *options,
  401. const char *dev_name)
  402. {
  403. struct ceph_mount_options *fsopt;
  404. const char *dev_name_end;
  405. int err;
  406. if (!dev_name || !*dev_name)
  407. return -EINVAL;
  408. fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
  409. if (!fsopt)
  410. return -ENOMEM;
  411. dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
  412. fsopt->sb_flags = flags;
  413. fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
  414. fsopt->wsize = CEPH_MAX_WRITE_SIZE;
  415. fsopt->rsize = CEPH_MAX_READ_SIZE;
  416. fsopt->rasize = CEPH_RASIZE_DEFAULT;
  417. fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
  418. if (!fsopt->snapdir_name) {
  419. err = -ENOMEM;
  420. goto out;
  421. }
  422. fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
  423. fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
  424. fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
  425. fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
  426. fsopt->congestion_kb = default_congestion_kb();
  427. /*
  428. * Distinguish the server list from the path in "dev_name".
  429. * Internally we do not include the leading '/' in the path.
  430. *
  431. * "dev_name" will look like:
  432. * <server_spec>[,<server_spec>...]:[<path>]
  433. * where
  434. * <server_spec> is <ip>[:<port>]
  435. * <path> is optional, but if present must begin with '/'
  436. */
  437. dev_name_end = strchr(dev_name, '/');
  438. if (dev_name_end) {
  439. if (strlen(dev_name_end) > 1) {
  440. fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
  441. if (!fsopt->server_path) {
  442. err = -ENOMEM;
  443. goto out;
  444. }
  445. }
  446. } else {
  447. dev_name_end = dev_name + strlen(dev_name);
  448. }
  449. err = -EINVAL;
  450. dev_name_end--; /* back up to ':' separator */
  451. if (dev_name_end < dev_name || *dev_name_end != ':') {
  452. pr_err("device name is missing path (no : separator in %s)\n",
  453. dev_name);
  454. goto out;
  455. }
  456. dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
  457. if (fsopt->server_path)
  458. dout("server path '%s'\n", fsopt->server_path);
  459. *popt = ceph_parse_options(options, dev_name, dev_name_end,
  460. parse_fsopt_token, (void *)fsopt);
  461. if (IS_ERR(*popt)) {
  462. err = PTR_ERR(*popt);
  463. goto out;
  464. }
  465. /* success */
  466. *pfsopt = fsopt;
  467. return 0;
  468. out:
  469. destroy_mount_options(fsopt);
  470. return err;
  471. }
  472. /**
  473. * ceph_show_options - Show mount options in /proc/mounts
  474. * @m: seq_file to write to
  475. * @root: root of that (sub)tree
  476. */
  477. static int ceph_show_options(struct seq_file *m, struct dentry *root)
  478. {
  479. struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
  480. struct ceph_mount_options *fsopt = fsc->mount_options;
  481. size_t pos;
  482. int ret;
  483. /* a comma between MNT/MS and client options */
  484. seq_putc(m, ',');
  485. pos = m->count;
  486. ret = ceph_print_client_options(m, fsc->client);
  487. if (ret)
  488. return ret;
  489. /* retract our comma if no client options */
  490. if (m->count == pos)
  491. m->count--;
  492. if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
  493. seq_puts(m, ",dirstat");
  494. if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
  495. seq_puts(m, ",rbytes");
  496. if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
  497. seq_puts(m, ",noasyncreaddir");
  498. if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
  499. seq_puts(m, ",nodcache");
  500. if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
  501. seq_puts(m, ",ino32");
  502. if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
  503. seq_show_option(m, "fsc", fsopt->fscache_uniq);
  504. }
  505. if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
  506. seq_puts(m, ",nopoolperm");
  507. if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
  508. seq_puts(m, ",noquotadf");
  509. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  510. if (fsopt->sb_flags & SB_POSIXACL)
  511. seq_puts(m, ",acl");
  512. else
  513. seq_puts(m, ",noacl");
  514. #endif
  515. if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
  516. seq_puts(m, ",copyfrom");
  517. if (fsopt->mds_namespace)
  518. seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
  519. if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
  520. seq_printf(m, ",wsize=%d", fsopt->wsize);
  521. if (fsopt->rsize != CEPH_MAX_READ_SIZE)
  522. seq_printf(m, ",rsize=%d", fsopt->rsize);
  523. if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
  524. seq_printf(m, ",rasize=%d", fsopt->rasize);
  525. if (fsopt->congestion_kb != default_congestion_kb())
  526. seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
  527. if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
  528. seq_printf(m, ",caps_wanted_delay_min=%d",
  529. fsopt->caps_wanted_delay_min);
  530. if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
  531. seq_printf(m, ",caps_wanted_delay_max=%d",
  532. fsopt->caps_wanted_delay_max);
  533. if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
  534. seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
  535. if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
  536. seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
  537. if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
  538. seq_show_option(m, "snapdirname", fsopt->snapdir_name);
  539. return 0;
  540. }
  541. /*
  542. * handle any mon messages the standard library doesn't understand.
  543. * return error if we don't either.
  544. */
  545. static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
  546. {
  547. struct ceph_fs_client *fsc = client->private;
  548. int type = le16_to_cpu(msg->hdr.type);
  549. switch (type) {
  550. case CEPH_MSG_MDS_MAP:
  551. ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
  552. return 0;
  553. case CEPH_MSG_FS_MAP_USER:
  554. ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
  555. return 0;
  556. default:
  557. return -1;
  558. }
  559. }
  560. /*
  561. * create a new fs client
  562. *
  563. * Success or not, this function consumes @fsopt and @opt.
  564. */
  565. static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
  566. struct ceph_options *opt)
  567. {
  568. struct ceph_fs_client *fsc;
  569. int page_count;
  570. size_t size;
  571. int err;
  572. fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
  573. if (!fsc) {
  574. err = -ENOMEM;
  575. goto fail;
  576. }
  577. fsc->client = ceph_create_client(opt, fsc);
  578. if (IS_ERR(fsc->client)) {
  579. err = PTR_ERR(fsc->client);
  580. goto fail;
  581. }
  582. opt = NULL; /* fsc->client now owns this */
  583. fsc->client->extra_mon_dispatch = extra_mon_dispatch;
  584. fsc->client->osdc.abort_on_full = true;
  585. if (!fsopt->mds_namespace) {
  586. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
  587. 0, true);
  588. } else {
  589. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
  590. 0, false);
  591. }
  592. fsc->mount_options = fsopt;
  593. fsc->sb = NULL;
  594. fsc->mount_state = CEPH_MOUNT_MOUNTING;
  595. atomic_long_set(&fsc->writeback_count, 0);
  596. err = -ENOMEM;
  597. /*
  598. * The number of concurrent works can be high but they don't need
  599. * to be processed in parallel, limit concurrency.
  600. */
  601. fsc->wb_wq = alloc_workqueue("ceph-writeback", 0, 1);
  602. if (!fsc->wb_wq)
  603. goto fail_client;
  604. fsc->pg_inv_wq = alloc_workqueue("ceph-pg-invalid", 0, 1);
  605. if (!fsc->pg_inv_wq)
  606. goto fail_wb_wq;
  607. fsc->trunc_wq = alloc_workqueue("ceph-trunc", 0, 1);
  608. if (!fsc->trunc_wq)
  609. goto fail_pg_inv_wq;
  610. /* set up mempools */
  611. err = -ENOMEM;
  612. page_count = fsc->mount_options->wsize >> PAGE_SHIFT;
  613. size = sizeof (struct page *) * (page_count ? page_count : 1);
  614. fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
  615. if (!fsc->wb_pagevec_pool)
  616. goto fail_trunc_wq;
  617. /* caps */
  618. fsc->min_caps = fsopt->max_readdir;
  619. return fsc;
  620. fail_trunc_wq:
  621. destroy_workqueue(fsc->trunc_wq);
  622. fail_pg_inv_wq:
  623. destroy_workqueue(fsc->pg_inv_wq);
  624. fail_wb_wq:
  625. destroy_workqueue(fsc->wb_wq);
  626. fail_client:
  627. ceph_destroy_client(fsc->client);
  628. fail:
  629. kfree(fsc);
  630. if (opt)
  631. ceph_destroy_options(opt);
  632. destroy_mount_options(fsopt);
  633. return ERR_PTR(err);
  634. }
  635. static void flush_fs_workqueues(struct ceph_fs_client *fsc)
  636. {
  637. flush_workqueue(fsc->wb_wq);
  638. flush_workqueue(fsc->pg_inv_wq);
  639. flush_workqueue(fsc->trunc_wq);
  640. }
  641. static void destroy_fs_client(struct ceph_fs_client *fsc)
  642. {
  643. dout("destroy_fs_client %p\n", fsc);
  644. destroy_workqueue(fsc->wb_wq);
  645. destroy_workqueue(fsc->pg_inv_wq);
  646. destroy_workqueue(fsc->trunc_wq);
  647. mempool_destroy(fsc->wb_pagevec_pool);
  648. destroy_mount_options(fsc->mount_options);
  649. ceph_destroy_client(fsc->client);
  650. kfree(fsc);
  651. dout("destroy_fs_client %p done\n", fsc);
  652. }
  653. /*
  654. * caches
  655. */
  656. struct kmem_cache *ceph_inode_cachep;
  657. struct kmem_cache *ceph_cap_cachep;
  658. struct kmem_cache *ceph_cap_flush_cachep;
  659. struct kmem_cache *ceph_dentry_cachep;
  660. struct kmem_cache *ceph_file_cachep;
  661. struct kmem_cache *ceph_dir_file_cachep;
  662. static void ceph_inode_init_once(void *foo)
  663. {
  664. struct ceph_inode_info *ci = foo;
  665. inode_init_once(&ci->vfs_inode);
  666. }
  667. static int __init init_caches(void)
  668. {
  669. int error = -ENOMEM;
  670. ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
  671. sizeof(struct ceph_inode_info),
  672. __alignof__(struct ceph_inode_info),
  673. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
  674. SLAB_ACCOUNT, ceph_inode_init_once);
  675. if (!ceph_inode_cachep)
  676. return -ENOMEM;
  677. ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
  678. if (!ceph_cap_cachep)
  679. goto bad_cap;
  680. ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
  681. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  682. if (!ceph_cap_flush_cachep)
  683. goto bad_cap_flush;
  684. ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
  685. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
  686. if (!ceph_dentry_cachep)
  687. goto bad_dentry;
  688. ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
  689. if (!ceph_file_cachep)
  690. goto bad_file;
  691. ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
  692. if (!ceph_dir_file_cachep)
  693. goto bad_dir_file;
  694. error = ceph_fscache_register();
  695. if (error)
  696. goto bad_fscache;
  697. return 0;
  698. bad_fscache:
  699. kmem_cache_destroy(ceph_dir_file_cachep);
  700. bad_dir_file:
  701. kmem_cache_destroy(ceph_file_cachep);
  702. bad_file:
  703. kmem_cache_destroy(ceph_dentry_cachep);
  704. bad_dentry:
  705. kmem_cache_destroy(ceph_cap_flush_cachep);
  706. bad_cap_flush:
  707. kmem_cache_destroy(ceph_cap_cachep);
  708. bad_cap:
  709. kmem_cache_destroy(ceph_inode_cachep);
  710. return error;
  711. }
  712. static void destroy_caches(void)
  713. {
  714. /*
  715. * Make sure all delayed rcu free inodes are flushed before we
  716. * destroy cache.
  717. */
  718. rcu_barrier();
  719. kmem_cache_destroy(ceph_inode_cachep);
  720. kmem_cache_destroy(ceph_cap_cachep);
  721. kmem_cache_destroy(ceph_cap_flush_cachep);
  722. kmem_cache_destroy(ceph_dentry_cachep);
  723. kmem_cache_destroy(ceph_file_cachep);
  724. kmem_cache_destroy(ceph_dir_file_cachep);
  725. ceph_fscache_unregister();
  726. }
  727. /*
  728. * ceph_umount_begin - initiate forced umount. Tear down down the
  729. * mount, skipping steps that may hang while waiting for server(s).
  730. */
  731. static void ceph_umount_begin(struct super_block *sb)
  732. {
  733. struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
  734. dout("ceph_umount_begin - starting forced umount\n");
  735. if (!fsc)
  736. return;
  737. fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
  738. ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
  739. ceph_mdsc_force_umount(fsc->mdsc);
  740. return;
  741. }
  742. static const struct super_operations ceph_super_ops = {
  743. .alloc_inode = ceph_alloc_inode,
  744. .destroy_inode = ceph_destroy_inode,
  745. .write_inode = ceph_write_inode,
  746. .drop_inode = ceph_drop_inode,
  747. .sync_fs = ceph_sync_fs,
  748. .put_super = ceph_put_super,
  749. .show_options = ceph_show_options,
  750. .statfs = ceph_statfs,
  751. .umount_begin = ceph_umount_begin,
  752. };
  753. /*
  754. * Bootstrap mount by opening the root directory. Note the mount
  755. * @started time from caller, and time out if this takes too long.
  756. */
  757. static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
  758. const char *path,
  759. unsigned long started)
  760. {
  761. struct ceph_mds_client *mdsc = fsc->mdsc;
  762. struct ceph_mds_request *req = NULL;
  763. int err;
  764. struct dentry *root;
  765. /* open dir */
  766. dout("open_root_inode opening '%s'\n", path);
  767. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
  768. if (IS_ERR(req))
  769. return ERR_CAST(req);
  770. req->r_path1 = kstrdup(path, GFP_NOFS);
  771. if (!req->r_path1) {
  772. root = ERR_PTR(-ENOMEM);
  773. goto out;
  774. }
  775. req->r_ino1.ino = CEPH_INO_ROOT;
  776. req->r_ino1.snap = CEPH_NOSNAP;
  777. req->r_started = started;
  778. req->r_timeout = fsc->client->options->mount_timeout;
  779. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
  780. req->r_num_caps = 2;
  781. err = ceph_mdsc_do_request(mdsc, NULL, req);
  782. if (err == 0) {
  783. struct inode *inode = req->r_target_inode;
  784. req->r_target_inode = NULL;
  785. dout("open_root_inode success\n");
  786. root = d_make_root(inode);
  787. if (!root) {
  788. root = ERR_PTR(-ENOMEM);
  789. goto out;
  790. }
  791. dout("open_root_inode success, root dentry is %p\n", root);
  792. } else {
  793. root = ERR_PTR(err);
  794. }
  795. out:
  796. ceph_mdsc_put_request(req);
  797. return root;
  798. }
  799. /*
  800. * mount: join the ceph cluster, and open root directory.
  801. */
  802. static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc)
  803. {
  804. int err;
  805. unsigned long started = jiffies; /* note the start time */
  806. struct dentry *root;
  807. dout("mount start %p\n", fsc);
  808. mutex_lock(&fsc->client->mount_mutex);
  809. if (!fsc->sb->s_root) {
  810. const char *path;
  811. err = __ceph_open_session(fsc->client, started);
  812. if (err < 0)
  813. goto out;
  814. /* setup fscache */
  815. if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
  816. err = ceph_fscache_register_fs(fsc);
  817. if (err < 0)
  818. goto out;
  819. }
  820. if (!fsc->mount_options->server_path) {
  821. path = "";
  822. dout("mount opening path \\t\n");
  823. } else {
  824. path = fsc->mount_options->server_path + 1;
  825. dout("mount opening path %s\n", path);
  826. }
  827. err = ceph_fs_debugfs_init(fsc);
  828. if (err < 0)
  829. goto out;
  830. root = open_root_dentry(fsc, path, started);
  831. if (IS_ERR(root)) {
  832. err = PTR_ERR(root);
  833. goto out;
  834. }
  835. fsc->sb->s_root = dget(root);
  836. } else {
  837. root = dget(fsc->sb->s_root);
  838. }
  839. fsc->mount_state = CEPH_MOUNT_MOUNTED;
  840. dout("mount success\n");
  841. mutex_unlock(&fsc->client->mount_mutex);
  842. return root;
  843. out:
  844. mutex_unlock(&fsc->client->mount_mutex);
  845. return ERR_PTR(err);
  846. }
  847. static int ceph_set_super(struct super_block *s, void *data)
  848. {
  849. struct ceph_fs_client *fsc = data;
  850. int ret;
  851. dout("set_super %p data %p\n", s, data);
  852. s->s_flags = fsc->mount_options->sb_flags;
  853. s->s_maxbytes = MAX_LFS_FILESIZE;
  854. s->s_xattr = ceph_xattr_handlers;
  855. s->s_fs_info = fsc;
  856. fsc->sb = s;
  857. fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
  858. s->s_op = &ceph_super_ops;
  859. s->s_d_op = &ceph_dentry_ops;
  860. s->s_export_op = &ceph_export_ops;
  861. s->s_time_gran = 1000; /* 1000 ns == 1 us */
  862. ret = set_anon_super(s, NULL); /* what is that second arg for? */
  863. if (ret != 0)
  864. goto fail;
  865. return ret;
  866. fail:
  867. s->s_fs_info = NULL;
  868. fsc->sb = NULL;
  869. return ret;
  870. }
  871. /*
  872. * share superblock if same fs AND options
  873. */
  874. static int ceph_compare_super(struct super_block *sb, void *data)
  875. {
  876. struct ceph_fs_client *new = data;
  877. struct ceph_mount_options *fsopt = new->mount_options;
  878. struct ceph_options *opt = new->client->options;
  879. struct ceph_fs_client *other = ceph_sb_to_client(sb);
  880. dout("ceph_compare_super %p\n", sb);
  881. if (compare_mount_options(fsopt, opt, other)) {
  882. dout("monitor(s)/mount options don't match\n");
  883. return 0;
  884. }
  885. if ((opt->flags & CEPH_OPT_FSID) &&
  886. ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
  887. dout("fsid doesn't match\n");
  888. return 0;
  889. }
  890. if (fsopt->sb_flags != other->mount_options->sb_flags) {
  891. dout("flags differ\n");
  892. return 0;
  893. }
  894. return 1;
  895. }
  896. /*
  897. * construct our own bdi so we can control readahead, etc.
  898. */
  899. static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
  900. static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
  901. {
  902. int err;
  903. err = super_setup_bdi_name(sb, "ceph-%ld",
  904. atomic_long_inc_return(&bdi_seq));
  905. if (err)
  906. return err;
  907. /* set ra_pages based on rasize mount option? */
  908. sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
  909. /* set io_pages based on max osd read size */
  910. sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
  911. return 0;
  912. }
  913. static struct dentry *ceph_mount(struct file_system_type *fs_type,
  914. int flags, const char *dev_name, void *data)
  915. {
  916. struct super_block *sb;
  917. struct ceph_fs_client *fsc;
  918. struct dentry *res;
  919. int err;
  920. int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
  921. struct ceph_mount_options *fsopt = NULL;
  922. struct ceph_options *opt = NULL;
  923. dout("ceph_mount\n");
  924. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  925. flags |= SB_POSIXACL;
  926. #endif
  927. err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
  928. if (err < 0) {
  929. res = ERR_PTR(err);
  930. goto out_final;
  931. }
  932. /* create client (which we may/may not use) */
  933. fsc = create_fs_client(fsopt, opt);
  934. if (IS_ERR(fsc)) {
  935. res = ERR_CAST(fsc);
  936. goto out_final;
  937. }
  938. err = ceph_mdsc_init(fsc);
  939. if (err < 0) {
  940. res = ERR_PTR(err);
  941. goto out;
  942. }
  943. if (ceph_test_opt(fsc->client, NOSHARE))
  944. compare_super = NULL;
  945. sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
  946. if (IS_ERR(sb)) {
  947. res = ERR_CAST(sb);
  948. goto out;
  949. }
  950. if (ceph_sb_to_client(sb) != fsc) {
  951. ceph_mdsc_destroy(fsc);
  952. destroy_fs_client(fsc);
  953. fsc = ceph_sb_to_client(sb);
  954. dout("get_sb got existing client %p\n", fsc);
  955. } else {
  956. dout("get_sb using new client %p\n", fsc);
  957. err = ceph_setup_bdi(sb, fsc);
  958. if (err < 0) {
  959. res = ERR_PTR(err);
  960. goto out_splat;
  961. }
  962. }
  963. res = ceph_real_mount(fsc);
  964. if (IS_ERR(res))
  965. goto out_splat;
  966. dout("root %p inode %p ino %llx.%llx\n", res,
  967. d_inode(res), ceph_vinop(d_inode(res)));
  968. return res;
  969. out_splat:
  970. ceph_mdsc_close_sessions(fsc->mdsc);
  971. deactivate_locked_super(sb);
  972. goto out_final;
  973. out:
  974. ceph_mdsc_destroy(fsc);
  975. destroy_fs_client(fsc);
  976. out_final:
  977. dout("ceph_mount fail %ld\n", PTR_ERR(res));
  978. return res;
  979. }
  980. static void ceph_kill_sb(struct super_block *s)
  981. {
  982. struct ceph_fs_client *fsc = ceph_sb_to_client(s);
  983. dev_t dev = s->s_dev;
  984. dout("kill_sb %p\n", s);
  985. ceph_mdsc_pre_umount(fsc->mdsc);
  986. flush_fs_workqueues(fsc);
  987. generic_shutdown_super(s);
  988. fsc->client->extra_mon_dispatch = NULL;
  989. ceph_fs_debugfs_cleanup(fsc);
  990. ceph_fscache_unregister_fs(fsc);
  991. ceph_mdsc_destroy(fsc);
  992. destroy_fs_client(fsc);
  993. free_anon_bdev(dev);
  994. }
  995. static struct file_system_type ceph_fs_type = {
  996. .owner = THIS_MODULE,
  997. .name = "ceph",
  998. .mount = ceph_mount,
  999. .kill_sb = ceph_kill_sb,
  1000. .fs_flags = FS_RENAME_DOES_D_MOVE,
  1001. };
  1002. MODULE_ALIAS_FS("ceph");
  1003. static int __init init_ceph(void)
  1004. {
  1005. int ret = init_caches();
  1006. if (ret)
  1007. goto out;
  1008. ceph_flock_init();
  1009. ceph_xattr_init();
  1010. ret = register_filesystem(&ceph_fs_type);
  1011. if (ret)
  1012. goto out_xattr;
  1013. pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
  1014. return 0;
  1015. out_xattr:
  1016. ceph_xattr_exit();
  1017. destroy_caches();
  1018. out:
  1019. return ret;
  1020. }
  1021. static void __exit exit_ceph(void)
  1022. {
  1023. dout("exit_ceph\n");
  1024. unregister_filesystem(&ceph_fs_type);
  1025. ceph_xattr_exit();
  1026. destroy_caches();
  1027. }
  1028. module_init(init_ceph);
  1029. module_exit(exit_ceph);
  1030. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  1031. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  1032. MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
  1033. MODULE_DESCRIPTION("Ceph filesystem for Linux");
  1034. MODULE_LICENSE("GPL");