blob: 75d02eaa1279f7abf42505e6318dce6adb753f04 [file] [log] [blame]
Sage Weil16725b92009-10-06 11:31:07 -07001
2#include "ceph_debug.h"
3
4#include <linux/backing-dev.h>
5#include <linux/fs.h>
6#include <linux/inet.h>
7#include <linux/in6.h>
8#include <linux/module.h>
9#include <linux/mount.h>
10#include <linux/parser.h>
11#include <linux/rwsem.h>
12#include <linux/sched.h>
13#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Sage Weil16725b92009-10-06 11:31:07 -070015#include <linux/statfs.h>
16#include <linux/string.h>
17#include <linux/version.h>
18#include <linux/vmalloc.h>
19
Sage Weil16725b92009-10-06 11:31:07 -070020#include "decode.h"
21#include "super.h"
22#include "mon_client.h"
Sage Weil07433042009-11-18 16:50:41 -080023#include "auth.h"
Sage Weil16725b92009-10-06 11:31:07 -070024
25/*
26 * Ceph superblock operations
27 *
28 * Handle the basics of mounting, unmounting.
29 */
30
31
32/*
33 * find filename portion of a path (/foo/bar/baz -> baz)
34 */
35const char *ceph_file_part(const char *s, int len)
36{
37 const char *e = s + len;
38
39 while (e != s && *(e-1) != '/')
40 e--;
41 return e;
42}
43
44
45/*
46 * super ops
47 */
48static void ceph_put_super(struct super_block *s)
49{
50 struct ceph_client *cl = ceph_client(s);
51
52 dout("put_super\n");
53 ceph_mdsc_close_sessions(&cl->mdsc);
54 return;
55}
56
57static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
58{
59 struct ceph_client *client = ceph_inode_to_client(dentry->d_inode);
60 struct ceph_monmap *monmap = client->monc.monmap;
61 struct ceph_statfs st;
62 u64 fsid;
63 int err;
64
65 dout("statfs\n");
66 err = ceph_monc_do_statfs(&client->monc, &st);
67 if (err < 0)
68 return err;
69
70 /* fill in kstatfs */
71 buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
72
73 /*
74 * express utilization in terms of large blocks to avoid
75 * overflow on 32-bit machines.
76 */
77 buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
78 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
79 buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >>
80 (CEPH_BLOCK_SHIFT-10);
81 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
82
83 buf->f_files = le64_to_cpu(st.num_objects);
84 buf->f_ffree = -1;
85 buf->f_namelen = PATH_MAX;
86 buf->f_frsize = PAGE_CACHE_SIZE;
87
88 /* leave fsid little-endian, regardless of host endianness */
89 fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
90 buf->f_fsid.val[0] = fsid & 0xffffffff;
91 buf->f_fsid.val[1] = fsid >> 32;
92
93 return 0;
94}
95
96
97static int ceph_syncfs(struct super_block *sb, int wait)
98{
99 dout("sync_fs %d\n", wait);
100 ceph_osdc_sync(&ceph_client(sb)->osdc);
101 ceph_mdsc_sync(&ceph_client(sb)->mdsc);
Sage Weilf2cf4182009-10-14 14:09:07 -0700102 dout("sync_fs %d done\n", wait);
Sage Weil16725b92009-10-06 11:31:07 -0700103 return 0;
104}
105
106
107/**
108 * ceph_show_options - Show mount options in /proc/mounts
109 * @m: seq_file to write to
110 * @mnt: mount descriptor
111 */
112static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt)
113{
114 struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb);
Sage Weil6b805182009-10-27 11:50:50 -0700115 struct ceph_mount_args *args = client->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700116
117 if (args->flags & CEPH_OPT_FSID)
118 seq_printf(m, ",fsidmajor=%llu,fsidminor%llu",
119 le64_to_cpu(*(__le64 *)&args->fsid.fsid[0]),
120 le64_to_cpu(*(__le64 *)&args->fsid.fsid[8]));
121 if (args->flags & CEPH_OPT_NOSHARE)
122 seq_puts(m, ",noshare");
123 if (args->flags & CEPH_OPT_DIRSTAT)
124 seq_puts(m, ",dirstat");
125 if ((args->flags & CEPH_OPT_RBYTES) == 0)
126 seq_puts(m, ",norbytes");
127 if (args->flags & CEPH_OPT_NOCRC)
128 seq_puts(m, ",nocrc");
129 if (args->flags & CEPH_OPT_NOASYNCREADDIR)
130 seq_puts(m, ",noasyncreaddir");
131 if (strcmp(args->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
132 seq_printf(m, ",snapdirname=%s", args->snapdir_name);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800133 if (args->name)
134 seq_printf(m, ",name=%s", args->name);
Sage Weil16725b92009-10-06 11:31:07 -0700135 if (args->secret)
136 seq_puts(m, ",secret=<hidden>");
137 return 0;
138}
139
140/*
141 * caches
142 */
143struct kmem_cache *ceph_inode_cachep;
144struct kmem_cache *ceph_cap_cachep;
145struct kmem_cache *ceph_dentry_cachep;
146struct kmem_cache *ceph_file_cachep;
147
148static void ceph_inode_init_once(void *foo)
149{
150 struct ceph_inode_info *ci = foo;
151 inode_init_once(&ci->vfs_inode);
152}
153
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800154static int default_congestion_kb(void)
155{
156 int congestion_kb;
157
158 /*
159 * Copied from NFS
160 *
161 * congestion size, scale with available memory.
162 *
163 * 64MB: 8192k
164 * 128MB: 11585k
165 * 256MB: 16384k
166 * 512MB: 23170k
167 * 1GB: 32768k
168 * 2GB: 46340k
169 * 4GB: 65536k
170 * 8GB: 92681k
171 * 16GB: 131072k
172 *
173 * This allows larger machines to have larger/more transfers.
174 * Limit the default to 256M
175 */
176 congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
177 if (congestion_kb > 256*1024)
178 congestion_kb = 256*1024;
179
180 return congestion_kb;
181}
182
Sage Weil16725b92009-10-06 11:31:07 -0700183static int __init init_caches(void)
184{
185 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
186 sizeof(struct ceph_inode_info),
187 __alignof__(struct ceph_inode_info),
188 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
189 ceph_inode_init_once);
190 if (ceph_inode_cachep == NULL)
191 return -ENOMEM;
192
193 ceph_cap_cachep = KMEM_CACHE(ceph_cap,
194 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
195 if (ceph_cap_cachep == NULL)
196 goto bad_cap;
197
198 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
199 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
200 if (ceph_dentry_cachep == NULL)
201 goto bad_dentry;
202
203 ceph_file_cachep = KMEM_CACHE(ceph_file_info,
204 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
205 if (ceph_file_cachep == NULL)
206 goto bad_file;
207
208 return 0;
209
210bad_file:
211 kmem_cache_destroy(ceph_dentry_cachep);
212bad_dentry:
213 kmem_cache_destroy(ceph_cap_cachep);
214bad_cap:
215 kmem_cache_destroy(ceph_inode_cachep);
216 return -ENOMEM;
217}
218
219static void destroy_caches(void)
220{
221 kmem_cache_destroy(ceph_inode_cachep);
222 kmem_cache_destroy(ceph_cap_cachep);
223 kmem_cache_destroy(ceph_dentry_cachep);
224 kmem_cache_destroy(ceph_file_cachep);
225}
226
227
228/*
229 * ceph_umount_begin - initiate forced umount. Tear down down the
230 * mount, skipping steps that may hang while waiting for server(s).
231 */
232static void ceph_umount_begin(struct super_block *sb)
233{
234 struct ceph_client *client = ceph_sb_to_client(sb);
235
236 dout("ceph_umount_begin - starting forced umount\n");
237 if (!client)
238 return;
239 client->mount_state = CEPH_MOUNT_SHUTDOWN;
240 return;
241}
242
243static const struct super_operations ceph_super_ops = {
244 .alloc_inode = ceph_alloc_inode,
245 .destroy_inode = ceph_destroy_inode,
246 .write_inode = ceph_write_inode,
247 .sync_fs = ceph_syncfs,
248 .put_super = ceph_put_super,
249 .show_options = ceph_show_options,
250 .statfs = ceph_statfs,
251 .umount_begin = ceph_umount_begin,
252};
253
254
255const char *ceph_msg_type_name(int type)
256{
257 switch (type) {
258 case CEPH_MSG_SHUTDOWN: return "shutdown";
259 case CEPH_MSG_PING: return "ping";
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800260 case CEPH_MSG_AUTH: return "auth";
261 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700262 case CEPH_MSG_MON_MAP: return "mon_map";
263 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
264 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
265 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
Sage Weil16725b92009-10-06 11:31:07 -0700266 case CEPH_MSG_STATFS: return "statfs";
267 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700268 case CEPH_MSG_MDS_MAP: return "mds_map";
269 case CEPH_MSG_CLIENT_SESSION: return "client_session";
270 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
271 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
272 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
273 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
274 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
275 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
276 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
277 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
Sage Weil16725b92009-10-06 11:31:07 -0700278 case CEPH_MSG_OSD_MAP: return "osd_map";
279 case CEPH_MSG_OSD_OP: return "osd_op";
280 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
281 default: return "unknown";
282 }
283}
284
285
286/*
287 * mount options
288 */
289enum {
290 Opt_fsidmajor,
291 Opt_fsidminor,
292 Opt_monport,
293 Opt_wsize,
294 Opt_rsize,
295 Opt_osdtimeout,
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800296 Opt_osdkeepalivetimeout,
Sage Weil16725b92009-10-06 11:31:07 -0700297 Opt_mount_timeout,
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800298 Opt_osd_idle_ttl,
Sage Weil16725b92009-10-06 11:31:07 -0700299 Opt_caps_wanted_delay_min,
300 Opt_caps_wanted_delay_max,
301 Opt_readdir_max_entries,
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800302 Opt_congestion_kb,
Sage Weile53c2fe2009-10-27 10:19:28 -0700303 Opt_last_int,
Sage Weil16725b92009-10-06 11:31:07 -0700304 /* int args above */
305 Opt_snapdirname,
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800306 Opt_name,
Sage Weil16725b92009-10-06 11:31:07 -0700307 Opt_secret,
Sage Weile53c2fe2009-10-27 10:19:28 -0700308 Opt_last_string,
Sage Weil16725b92009-10-06 11:31:07 -0700309 /* string args above */
310 Opt_ip,
311 Opt_noshare,
312 Opt_dirstat,
313 Opt_nodirstat,
314 Opt_rbytes,
315 Opt_norbytes,
316 Opt_nocrc,
317 Opt_noasyncreaddir,
318};
319
320static match_table_t arg_tokens = {
321 {Opt_fsidmajor, "fsidmajor=%ld"},
322 {Opt_fsidminor, "fsidminor=%ld"},
323 {Opt_monport, "monport=%d"},
324 {Opt_wsize, "wsize=%d"},
325 {Opt_rsize, "rsize=%d"},
326 {Opt_osdtimeout, "osdtimeout=%d"},
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800327 {Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
Sage Weil16725b92009-10-06 11:31:07 -0700328 {Opt_mount_timeout, "mount_timeout=%d"},
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800329 {Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
Sage Weil16725b92009-10-06 11:31:07 -0700330 {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
331 {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
332 {Opt_readdir_max_entries, "readdir_max_entries=%d"},
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800333 {Opt_congestion_kb, "write_congestion_kb=%d"},
Sage Weil16725b92009-10-06 11:31:07 -0700334 /* int args above */
335 {Opt_snapdirname, "snapdirname=%s"},
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800336 {Opt_name, "name=%s"},
Sage Weil16725b92009-10-06 11:31:07 -0700337 {Opt_secret, "secret=%s"},
338 /* string args above */
339 {Opt_ip, "ip=%s"},
340 {Opt_noshare, "noshare"},
341 {Opt_dirstat, "dirstat"},
342 {Opt_nodirstat, "nodirstat"},
343 {Opt_rbytes, "rbytes"},
344 {Opt_norbytes, "norbytes"},
345 {Opt_nocrc, "nocrc"},
346 {Opt_noasyncreaddir, "noasyncreaddir"},
347 {-1, NULL}
348};
349
350
Sage Weil6b805182009-10-27 11:50:50 -0700351static struct ceph_mount_args *parse_mount_args(int flags, char *options,
352 const char *dev_name,
353 const char **path)
Sage Weil16725b92009-10-06 11:31:07 -0700354{
Sage Weil6b805182009-10-27 11:50:50 -0700355 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700356 const char *c;
Sage Weil6b805182009-10-27 11:50:50 -0700357 int err = -ENOMEM;
Sage Weil16725b92009-10-06 11:31:07 -0700358 substring_t argstr[MAX_OPT_ARGS];
Sage Weil16725b92009-10-06 11:31:07 -0700359
Sage Weil6b805182009-10-27 11:50:50 -0700360 args = kzalloc(sizeof(*args), GFP_KERNEL);
361 if (!args)
362 return ERR_PTR(-ENOMEM);
363 args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
364 GFP_KERNEL);
365 if (!args->mon_addr)
366 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700367
Sage Weil6b805182009-10-27 11:50:50 -0700368 dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700369
Sage Weil16725b92009-10-06 11:31:07 -0700370 /* start with defaults */
371 args->sb_flags = flags;
372 args->flags = CEPH_OPT_DEFAULT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800373 args->osd_timeout = CEPH_OSD_TIMEOUT_DEFAULT;
374 args->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
Sage Weil16725b92009-10-06 11:31:07 -0700375 args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800376 args->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT; /* seconds */
Sage Weil16725b92009-10-06 11:31:07 -0700377 args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
378 args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
Sage Weil8fa97652009-10-16 14:44:35 -0700379 args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
Sage Weil16725b92009-10-06 11:31:07 -0700380 args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
381 args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
382 args->max_readdir = 1024;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800383 args->congestion_kb = default_congestion_kb();
Sage Weil16725b92009-10-06 11:31:07 -0700384
385 /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
Sage Weil7b813c462009-10-26 22:07:53 -0700386 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700387 if (!dev_name)
Sage Weil7b813c462009-10-26 22:07:53 -0700388 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700389 *path = strstr(dev_name, ":/");
390 if (*path == NULL) {
391 pr_err("device name is missing path (no :/ in %s)\n",
392 dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700393 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700394 }
395
396 /* get mon ip(s) */
Sage Weil6b805182009-10-27 11:50:50 -0700397 err = ceph_parse_ips(dev_name, *path, args->mon_addr,
398 CEPH_MAX_MON, &args->num_mon);
Sage Weil16725b92009-10-06 11:31:07 -0700399 if (err < 0)
Sage Weil7b813c462009-10-26 22:07:53 -0700400 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700401
Sage Weil16725b92009-10-06 11:31:07 -0700402 /* path on server */
403 *path += 2;
404 dout("server path '%s'\n", *path);
405
406 /* parse mount options */
407 while ((c = strsep(&options, ",")) != NULL) {
408 int token, intval, ret;
409 if (!*c)
410 continue;
Sage Weil7b813c462009-10-26 22:07:53 -0700411 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700412 token = match_token((char *)c, arg_tokens, argstr);
413 if (token < 0) {
414 pr_err("bad mount option at '%s'\n", c);
Sage Weil7b813c462009-10-26 22:07:53 -0700415 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700416 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700417 if (token < Opt_last_int) {
Sage Weil16725b92009-10-06 11:31:07 -0700418 ret = match_int(&argstr[0], &intval);
419 if (ret < 0) {
420 pr_err("bad mount option arg (not int) "
421 "at '%s'\n", c);
422 continue;
423 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700424 dout("got int token %d val %d\n", token, intval);
425 } else if (token > Opt_last_int && token < Opt_last_string) {
426 dout("got string token %d val %s\n", token,
427 argstr[0].from);
428 } else {
429 dout("got token %d\n", token);
Sage Weil16725b92009-10-06 11:31:07 -0700430 }
431 switch (token) {
432 case Opt_fsidmajor:
433 *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
434 break;
435 case Opt_fsidminor:
436 *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
437 break;
438 case Opt_ip:
439 err = ceph_parse_ips(argstr[0].from,
440 argstr[0].to,
441 &args->my_addr,
442 1, NULL);
443 if (err < 0)
Sage Weil6b805182009-10-27 11:50:50 -0700444 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700445 args->flags |= CEPH_OPT_MYIP;
446 break;
447
448 case Opt_snapdirname:
449 kfree(args->snapdir_name);
450 args->snapdir_name = kstrndup(argstr[0].from,
451 argstr[0].to-argstr[0].from,
452 GFP_KERNEL);
453 break;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800454 case Opt_name:
455 args->name = kstrndup(argstr[0].from,
456 argstr[0].to-argstr[0].from,
457 GFP_KERNEL);
458 break;
Sage Weil16725b92009-10-06 11:31:07 -0700459 case Opt_secret:
460 args->secret = kstrndup(argstr[0].from,
461 argstr[0].to-argstr[0].from,
462 GFP_KERNEL);
463 break;
464
465 /* misc */
466 case Opt_wsize:
467 args->wsize = intval;
468 break;
469 case Opt_rsize:
470 args->rsize = intval;
471 break;
472 case Opt_osdtimeout:
473 args->osd_timeout = intval;
474 break;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800475 case Opt_osdkeepalivetimeout:
476 args->osd_keepalive_timeout = intval;
477 break;
Sage Weil16725b92009-10-06 11:31:07 -0700478 case Opt_mount_timeout:
479 args->mount_timeout = intval;
480 break;
481 case Opt_caps_wanted_delay_min:
482 args->caps_wanted_delay_min = intval;
483 break;
484 case Opt_caps_wanted_delay_max:
485 args->caps_wanted_delay_max = intval;
486 break;
487 case Opt_readdir_max_entries:
488 args->max_readdir = intval;
489 break;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800490 case Opt_congestion_kb:
491 args->congestion_kb = intval;
492 break;
Sage Weil16725b92009-10-06 11:31:07 -0700493
494 case Opt_noshare:
495 args->flags |= CEPH_OPT_NOSHARE;
496 break;
497
498 case Opt_dirstat:
499 args->flags |= CEPH_OPT_DIRSTAT;
500 break;
501 case Opt_nodirstat:
502 args->flags &= ~CEPH_OPT_DIRSTAT;
503 break;
504 case Opt_rbytes:
505 args->flags |= CEPH_OPT_RBYTES;
506 break;
507 case Opt_norbytes:
508 args->flags &= ~CEPH_OPT_RBYTES;
509 break;
510 case Opt_nocrc:
511 args->flags |= CEPH_OPT_NOCRC;
512 break;
513 case Opt_noasyncreaddir:
514 args->flags |= CEPH_OPT_NOASYNCREADDIR;
515 break;
516
517 default:
518 BUG_ON(token);
519 }
520 }
Sage Weil6b805182009-10-27 11:50:50 -0700521 return args;
Sage Weil16725b92009-10-06 11:31:07 -0700522
Sage Weil7b813c462009-10-26 22:07:53 -0700523out:
Sage Weil6b805182009-10-27 11:50:50 -0700524 kfree(args->mon_addr);
525 kfree(args);
526 return ERR_PTR(err);
Sage Weil16725b92009-10-06 11:31:07 -0700527}
528
Sage Weil6b805182009-10-27 11:50:50 -0700529static void destroy_mount_args(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700530{
Sage Weil6b805182009-10-27 11:50:50 -0700531 dout("destroy_mount_args %p\n", args);
Sage Weil16725b92009-10-06 11:31:07 -0700532 kfree(args->snapdir_name);
533 args->snapdir_name = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800534 kfree(args->name);
535 args->name = NULL;
Sage Weil16725b92009-10-06 11:31:07 -0700536 kfree(args->secret);
537 args->secret = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700538 kfree(args);
Sage Weil16725b92009-10-06 11:31:07 -0700539}
540
541/*
542 * create a fresh client instance
543 */
Sage Weil6b805182009-10-27 11:50:50 -0700544static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700545{
546 struct ceph_client *client;
547 int err = -ENOMEM;
548
549 client = kzalloc(sizeof(*client), GFP_KERNEL);
550 if (client == NULL)
551 return ERR_PTR(-ENOMEM);
552
553 mutex_init(&client->mount_mutex);
554
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800555 init_waitqueue_head(&client->auth_wq);
Sage Weil16725b92009-10-06 11:31:07 -0700556
557 client->sb = NULL;
558 client->mount_state = CEPH_MOUNT_MOUNTING;
Sage Weil6b805182009-10-27 11:50:50 -0700559 client->mount_args = args;
Sage Weil16725b92009-10-06 11:31:07 -0700560
561 client->msgr = NULL;
562
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800563 client->auth_err = 0;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800564 atomic_long_set(&client->writeback_count, 0);
Sage Weil16725b92009-10-06 11:31:07 -0700565
Sage Weil859e7b12009-11-02 09:32:47 -0800566 err = bdi_init(&client->backing_dev_info);
567 if (err < 0)
568 goto fail;
569
Sage Weil16725b92009-10-06 11:31:07 -0700570 err = -ENOMEM;
571 client->wb_wq = create_workqueue("ceph-writeback");
572 if (client->wb_wq == NULL)
Sage Weil859e7b12009-11-02 09:32:47 -0800573 goto fail_bdi;
Sage Weil16725b92009-10-06 11:31:07 -0700574 client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
575 if (client->pg_inv_wq == NULL)
576 goto fail_wb_wq;
577 client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
578 if (client->trunc_wq == NULL)
579 goto fail_pg_inv_wq;
580
Sage Weilb9bfb932009-11-18 15:08:44 -0800581 /* set up mempools */
582 err = -ENOMEM;
583 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
584 client->mount_args->wsize >> PAGE_CACHE_SHIFT);
585 if (!client->wb_pagevec_pool)
586 goto fail_trunc_wq;
587
Sage Weil85ccce42010-02-17 10:02:43 -0800588 /* caps */
589 client->min_caps = args->max_readdir;
590 ceph_adjust_min_caps(client->min_caps);
Sage Weilb9bfb932009-11-18 15:08:44 -0800591
Sage Weil16725b92009-10-06 11:31:07 -0700592 /* subsystems */
593 err = ceph_monc_init(&client->monc, client);
594 if (err < 0)
Sage Weilb9bfb932009-11-18 15:08:44 -0800595 goto fail_mempool;
Sage Weil16725b92009-10-06 11:31:07 -0700596 err = ceph_osdc_init(&client->osdc, client);
597 if (err < 0)
598 goto fail_monc;
Sage Weil5f44f142009-11-18 14:52:18 -0800599 err = ceph_mdsc_init(&client->mdsc, client);
600 if (err < 0)
601 goto fail_osdc;
Sage Weil16725b92009-10-06 11:31:07 -0700602 return client;
603
Sage Weil5f44f142009-11-18 14:52:18 -0800604fail_osdc:
605 ceph_osdc_stop(&client->osdc);
Sage Weil16725b92009-10-06 11:31:07 -0700606fail_monc:
607 ceph_monc_stop(&client->monc);
Sage Weilb9bfb932009-11-18 15:08:44 -0800608fail_mempool:
609 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700610fail_trunc_wq:
611 destroy_workqueue(client->trunc_wq);
612fail_pg_inv_wq:
613 destroy_workqueue(client->pg_inv_wq);
614fail_wb_wq:
615 destroy_workqueue(client->wb_wq);
Sage Weil859e7b12009-11-02 09:32:47 -0800616fail_bdi:
617 bdi_destroy(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700618fail:
619 kfree(client);
620 return ERR_PTR(err);
621}
622
623static void ceph_destroy_client(struct ceph_client *client)
624{
625 dout("destroy_client %p\n", client);
626
627 /* unmount */
628 ceph_mdsc_stop(&client->mdsc);
629 ceph_monc_stop(&client->monc);
630 ceph_osdc_stop(&client->osdc);
631
Sage Weil85ccce42010-02-17 10:02:43 -0800632 ceph_adjust_min_caps(-client->min_caps);
633
Sage Weil16725b92009-10-06 11:31:07 -0700634 ceph_debugfs_client_cleanup(client);
635 destroy_workqueue(client->wb_wq);
636 destroy_workqueue(client->pg_inv_wq);
637 destroy_workqueue(client->trunc_wq);
638
639 if (client->msgr)
640 ceph_messenger_destroy(client->msgr);
Sage Weilb9bfb932009-11-18 15:08:44 -0800641 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700642
Sage Weil6b805182009-10-27 11:50:50 -0700643 destroy_mount_args(client->mount_args);
Sage Weil16725b92009-10-06 11:31:07 -0700644
645 kfree(client);
646 dout("destroy_client %p done\n", client);
647}
648
649/*
Sage Weil07433042009-11-18 16:50:41 -0800650 * Initially learn our fsid, or verify an fsid matches.
651 */
652int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
653{
654 if (client->have_fsid) {
655 if (ceph_fsid_compare(&client->fsid, fsid)) {
Sage Weil9ec7cab2009-12-14 15:13:47 -0800656 pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT,
657 PR_FSID(&client->fsid), PR_FSID(fsid));
Sage Weil07433042009-11-18 16:50:41 -0800658 return -1;
659 }
660 } else {
661 pr_info("client%lld fsid " FSID_FORMAT "\n",
662 client->monc.auth->global_id, PR_FSID(fsid));
663 memcpy(&client->fsid, fsid, sizeof(*fsid));
664 ceph_debugfs_client_init(client);
665 client->have_fsid = true;
666 }
667 return 0;
668}
669
670/*
Sage Weil16725b92009-10-06 11:31:07 -0700671 * true if we have the mon map (and have thus joined the cluster)
672 */
673static int have_mon_map(struct ceph_client *client)
674{
675 return client->monc.monmap && client->monc.monmap->epoch;
676}
677
678/*
679 * Bootstrap mount by opening the root directory. Note the mount
680 * @started time from caller, and time out if this takes too long.
681 */
682static struct dentry *open_root_dentry(struct ceph_client *client,
683 const char *path,
684 unsigned long started)
685{
686 struct ceph_mds_client *mdsc = &client->mdsc;
687 struct ceph_mds_request *req = NULL;
688 int err;
689 struct dentry *root;
690
691 /* open dir */
692 dout("open_root_inode opening '%s'\n", path);
693 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
694 if (IS_ERR(req))
695 return ERR_PTR(PTR_ERR(req));
696 req->r_path1 = kstrdup(path, GFP_NOFS);
697 req->r_ino1.ino = CEPH_INO_ROOT;
698 req->r_ino1.snap = CEPH_NOSNAP;
699 req->r_started = started;
Sage Weil6b805182009-10-27 11:50:50 -0700700 req->r_timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700701 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
702 req->r_num_caps = 2;
703 err = ceph_mdsc_do_request(mdsc, NULL, req);
704 if (err == 0) {
705 dout("open_root_inode success\n");
706 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
707 client->sb->s_root == NULL)
708 root = d_alloc_root(req->r_target_inode);
709 else
710 root = d_obtain_alias(req->r_target_inode);
711 req->r_target_inode = NULL;
712 dout("open_root_inode success, root dentry is %p\n", root);
713 } else {
714 root = ERR_PTR(err);
715 }
716 ceph_mdsc_put_request(req);
717 return root;
718}
719
720/*
721 * mount: join the ceph cluster, and open root directory.
722 */
723static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
724 const char *path)
725{
726 struct ceph_entity_addr *myaddr = NULL;
727 int err;
Sage Weil6b805182009-10-27 11:50:50 -0700728 unsigned long timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700729 unsigned long started = jiffies; /* note the start time */
730 struct dentry *root;
731
732 dout("mount start\n");
733 mutex_lock(&client->mount_mutex);
734
735 /* initialize the messenger */
736 if (client->msgr == NULL) {
737 if (ceph_test_opt(client, MYIP))
Sage Weil6b805182009-10-27 11:50:50 -0700738 myaddr = &client->mount_args->my_addr;
Sage Weil16725b92009-10-06 11:31:07 -0700739 client->msgr = ceph_messenger_create(myaddr);
740 if (IS_ERR(client->msgr)) {
741 err = PTR_ERR(client->msgr);
742 client->msgr = NULL;
743 goto out;
744 }
745 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
746 }
747
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800748 /* open session, and wait for mon, mds, and osd maps */
749 err = ceph_monc_open_session(&client->monc);
Sage Weil16725b92009-10-06 11:31:07 -0700750 if (err < 0)
751 goto out;
752
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800753 while (!have_mon_map(client)) {
Sage Weil16725b92009-10-06 11:31:07 -0700754 err = -EIO;
755 if (timeout && time_after_eq(jiffies, started + timeout))
756 goto out;
757
758 /* wait */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800759 dout("mount waiting for mon_map\n");
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800760 err = wait_event_interruptible_timeout(client->auth_wq,
761 have_mon_map(client) || (client->auth_err < 0),
Sage Weil16725b92009-10-06 11:31:07 -0700762 timeout);
763 if (err == -EINTR || err == -ERESTARTSYS)
764 goto out;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800765 if (client->auth_err < 0) {
766 err = client->auth_err;
Yehuda Sadehdc146572009-11-20 13:59:13 -0800767 goto out;
768 }
Sage Weil16725b92009-10-06 11:31:07 -0700769 }
770
771 dout("mount opening root\n");
772 root = open_root_dentry(client, "", started);
773 if (IS_ERR(root)) {
774 err = PTR_ERR(root);
775 goto out;
776 }
777 if (client->sb->s_root)
778 dput(root);
779 else
780 client->sb->s_root = root;
781
782 if (path[0] == 0) {
783 dget(root);
784 } else {
785 dout("mount opening base mountpoint\n");
786 root = open_root_dentry(client, path, started);
787 if (IS_ERR(root)) {
788 err = PTR_ERR(root);
789 dput(client->sb->s_root);
790 client->sb->s_root = NULL;
791 goto out;
792 }
793 }
794
795 mnt->mnt_root = root;
796 mnt->mnt_sb = client->sb;
797
798 client->mount_state = CEPH_MOUNT_MOUNTED;
799 dout("mount success\n");
800 err = 0;
801
802out:
803 mutex_unlock(&client->mount_mutex);
804 return err;
805}
806
807static int ceph_set_super(struct super_block *s, void *data)
808{
809 struct ceph_client *client = data;
810 int ret;
811
812 dout("set_super %p data %p\n", s, data);
813
Sage Weil6b805182009-10-27 11:50:50 -0700814 s->s_flags = client->mount_args->sb_flags;
Sage Weil16725b92009-10-06 11:31:07 -0700815 s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
816
817 s->s_fs_info = client;
818 client->sb = s;
819
820 s->s_op = &ceph_super_ops;
821 s->s_export_op = &ceph_export_ops;
822
823 s->s_time_gran = 1000; /* 1000 ns == 1 us */
824
825 ret = set_anon_super(s, NULL); /* what is that second arg for? */
826 if (ret != 0)
827 goto fail;
828
829 return ret;
830
831fail:
832 s->s_fs_info = NULL;
833 client->sb = NULL;
834 return ret;
835}
836
837/*
838 * share superblock if same fs AND options
839 */
840static int ceph_compare_super(struct super_block *sb, void *data)
841{
842 struct ceph_client *new = data;
Sage Weil6b805182009-10-27 11:50:50 -0700843 struct ceph_mount_args *args = new->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700844 struct ceph_client *other = ceph_sb_to_client(sb);
845 int i;
846
847 dout("ceph_compare_super %p\n", sb);
848 if (args->flags & CEPH_OPT_FSID) {
849 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
850 dout("fsid doesn't match\n");
851 return 0;
852 }
853 } else {
854 /* do we share (a) monitor? */
855 for (i = 0; i < new->monc.monmap->num_mon; i++)
856 if (ceph_monmap_contains(other->monc.monmap,
857 &new->monc.monmap->mon_inst[i].addr))
858 break;
859 if (i == new->monc.monmap->num_mon) {
860 dout("mon ip not part of monmap\n");
861 return 0;
862 }
863 dout("mon ip matches existing sb %p\n", sb);
864 }
Sage Weil6b805182009-10-27 11:50:50 -0700865 if (args->sb_flags != other->mount_args->sb_flags) {
Sage Weil16725b92009-10-06 11:31:07 -0700866 dout("flags differ\n");
867 return 0;
868 }
869 return 1;
870}
871
872/*
873 * construct our own bdi so we can control readahead, etc.
874 */
Sage Weil859e7b12009-11-02 09:32:47 -0800875static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
Sage Weil16725b92009-10-06 11:31:07 -0700876{
877 int err;
878
Sage Weilf2cf4182009-10-14 14:09:07 -0700879 sb->s_bdi = &client->backing_dev_info;
Sage Weil16725b92009-10-06 11:31:07 -0700880
881 /* set ra_pages based on rsize mount option? */
Sage Weil6b805182009-10-27 11:50:50 -0700882 if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
Sage Weil16725b92009-10-06 11:31:07 -0700883 client->backing_dev_info.ra_pages =
Sage Weil6b805182009-10-27 11:50:50 -0700884 (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
Sage Weil16725b92009-10-06 11:31:07 -0700885 >> PAGE_SHIFT;
Sage Weil16725b92009-10-06 11:31:07 -0700886 err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
887 return err;
888}
889
890static int ceph_get_sb(struct file_system_type *fs_type,
891 int flags, const char *dev_name, void *data,
892 struct vfsmount *mnt)
893{
894 struct super_block *sb;
895 struct ceph_client *client;
896 int err;
897 int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
Sage Weil6a18be12009-11-04 11:40:05 -0800898 const char *path = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700899 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700900
901 dout("ceph_get_sb\n");
Sage Weil6b805182009-10-27 11:50:50 -0700902 args = parse_mount_args(flags, data, dev_name, &path);
903 if (IS_ERR(args)) {
904 err = PTR_ERR(args);
905 goto out_final;
906 }
Sage Weil16725b92009-10-06 11:31:07 -0700907
908 /* create client (which we may/may not use) */
Sage Weil6b805182009-10-27 11:50:50 -0700909 client = ceph_create_client(args);
910 if (IS_ERR(client)) {
911 err = PTR_ERR(client);
912 goto out_final;
913 }
Sage Weil16725b92009-10-06 11:31:07 -0700914
Sage Weil6b805182009-10-27 11:50:50 -0700915 if (client->mount_args->flags & CEPH_OPT_NOSHARE)
Sage Weil16725b92009-10-06 11:31:07 -0700916 compare_super = NULL;
917 sb = sget(fs_type, compare_super, ceph_set_super, client);
918 if (IS_ERR(sb)) {
919 err = PTR_ERR(sb);
920 goto out;
921 }
922
923 if (ceph_client(sb) != client) {
924 ceph_destroy_client(client);
925 client = ceph_client(sb);
926 dout("get_sb got existing client %p\n", client);
927 } else {
928 dout("get_sb using new client %p\n", client);
Sage Weil859e7b12009-11-02 09:32:47 -0800929 err = ceph_register_bdi(sb, client);
Sage Weil16725b92009-10-06 11:31:07 -0700930 if (err < 0)
931 goto out_splat;
932 }
933
934 err = ceph_mount(client, mnt, path);
935 if (err < 0)
936 goto out_splat;
937 dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
938 mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
939 return 0;
940
941out_splat:
942 ceph_mdsc_close_sessions(&client->mdsc);
943 up_write(&sb->s_umount);
944 deactivate_super(sb);
945 goto out_final;
946
947out:
948 ceph_destroy_client(client);
949out_final:
950 dout("ceph_get_sb fail %d\n", err);
951 return err;
952}
953
954static void ceph_kill_sb(struct super_block *s)
955{
956 struct ceph_client *client = ceph_sb_to_client(s);
957 dout("kill_sb %p\n", s);
958 ceph_mdsc_pre_umount(&client->mdsc);
Sage Weil16725b92009-10-06 11:31:07 -0700959 kill_anon_super(s); /* will call put_super after sb is r/o */
Sage Weile0e32712009-12-21 21:04:26 -0800960 if (s->s_bdi == &client->backing_dev_info)
961 bdi_unregister(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700962 bdi_destroy(&client->backing_dev_info);
963 ceph_destroy_client(client);
964}
965
966static struct file_system_type ceph_fs_type = {
967 .owner = THIS_MODULE,
968 .name = "ceph",
969 .get_sb = ceph_get_sb,
970 .kill_sb = ceph_kill_sb,
971 .fs_flags = FS_RENAME_DOES_D_MOVE,
972};
973
974#define _STRINGIFY(x) #x
975#define STRINGIFY(x) _STRINGIFY(x)
976
977static int __init init_ceph(void)
978{
979 int ret = 0;
980
981 ret = ceph_debugfs_init();
982 if (ret < 0)
983 goto out;
984
985 ret = ceph_msgr_init();
986 if (ret < 0)
987 goto out_debugfs;
988
989 ret = init_caches();
990 if (ret)
991 goto out_msgr;
992
993 ceph_caps_init();
994
995 ret = register_filesystem(&ceph_fs_type);
996 if (ret)
997 goto out_icache;
998
Sage Weilfa0b72e2009-10-07 10:59:10 -0700999 pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
1000 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
1001 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
Sage Weil16725b92009-10-06 11:31:07 -07001002 return 0;
1003
1004out_icache:
1005 destroy_caches();
1006out_msgr:
1007 ceph_msgr_exit();
1008out_debugfs:
1009 ceph_debugfs_cleanup();
1010out:
1011 return ret;
1012}
1013
1014static void __exit exit_ceph(void)
1015{
1016 dout("exit_ceph\n");
1017 unregister_filesystem(&ceph_fs_type);
1018 ceph_caps_finalize();
1019 destroy_caches();
1020 ceph_msgr_exit();
1021 ceph_debugfs_cleanup();
1022}
1023
1024module_init(init_ceph);
1025module_exit(exit_ceph);
1026
1027MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1028MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1029MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1030MODULE_DESCRIPTION("Ceph filesystem for Linux");
1031MODULE_LICENSE("GPL");