~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ceph/super.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.11 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.84 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.154 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.201 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.201 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.77 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp