1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
6 * This file is part of InterMezzo, http://www.inter-mezzo.org.
8 * InterMezzo is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * InterMezzo is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with InterMezzo; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define __NO_VERSION__
28 #include <asm/bitops.h>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
32 #include <linux/errno.h>
34 #include <linux/ext2_fs.h>
35 #include <linux/slab.h>
36 #include <linux/vmalloc.h>
37 #include <linux/sched.h>
38 #include <linux/stat.h>
39 #include <linux/string.h>
40 #include <linux/locks.h>
41 #include <linux/blkdev.h>
42 #include <linux/init.h>
43 #include <linux/module.h>
45 #include <linux/intermezzo_fs.h>
46 #include <linux/intermezzo_psdev.h>
48 static inline struct presto_file_set *presto_dentry2fset(struct dentry *dentry)
50 if (presto_d2d(dentry) == NULL) {
54 return presto_d2d(dentry)->dd_fset;
57 /* find the fileset dentry for this dentry */
58 struct presto_file_set *presto_fset(struct dentry *de)
63 /* FIXME: is this ok to be NULL? */
64 CDEBUG(D_INODE,"presto_fset: warning %*s has NULL inode.\n",
65 de->d_name.len, de->d_name.name);
67 for (fsde = de;; fsde = fsde->d_parent) {
68 if ( presto_dentry2fset(fsde) ) {
70 return presto_dentry2fset(fsde);
72 if (fsde->d_parent == fsde)
79 int presto_get_lastrecno(char *path, off_t *recno)
82 struct presto_file_set *fset;
83 struct dentry *dentry;
87 error = presto_walk(path, &nd);
96 if ( !presto_ispresto(dentry->d_inode) ) {
102 if ( ! presto_dentry2fset(dentry)) {
107 fset = presto_dentry2fset(dentry);
113 *recno = fset->fset_kml.fd_recno;
120 static char * _izo_make_path(char *fsetname, char *name)
125 len = strlen("/.intermezzo/") + strlen(fsetname)
126 + 1 + strlen(name) + 1;
128 PRESTO_ALLOC(path, len);
132 sprintf(path, "/.intermezzo/%s/%s", fsetname, name);
137 char * izo_make_path(struct presto_file_set *fset, char *name)
139 return _izo_make_path(fset->fset_name, name);
142 static struct file *_izo_fset_open(char *fsetname, char *name, int flags, int mode)
149 path = _izo_make_path(fsetname, name);
152 return ERR_PTR(-ENOMEM);
155 CDEBUG(D_INODE, "opening file %s\n", path);
156 f = filp_open(path, flags, mode);
159 CDEBUG(D_INODE, "Error %d\n", error);
162 PRESTO_FREE(path, strlen(path));
169 struct file *izo_fset_open(struct presto_file_set *fset, char *name, int flags, int mode)
171 return _izo_fset_open(fset->fset_name, name, flags, mode);
177 * note: this routine "pins" a dentry for a fileset root
179 int presto_set_fsetroot(struct dentry *ioctl_dentry, char *fsetname,
182 struct presto_file_set *fset = NULL;
183 struct presto_cache *cache;
185 struct file *fset_root;
186 struct dentry *dentry;
190 fset_root = _izo_fset_open(fsetname, "ROOT", O_RDONLY, 000);
191 if (IS_ERR(fset_root)) {
192 CERROR("Can't open %s/ROOT\n", fsetname);
194 error = PTR_ERR(fset_root);
197 dentry = dget(fset_root->f_dentry);
198 filp_close(fset_root, NULL);
200 dentry->d_inode->i_op = ioctl_dentry->d_inode->i_op;
201 dentry->d_inode->i_fop = ioctl_dentry->d_inode->i_fop;
202 dentry->d_op = ioctl_dentry->d_op;
203 fset = presto_dentry2fset(dentry);
204 if (fset && (fset->fset_dentry == dentry) ) {
205 CERROR("Fsetroot already set (inode %ld)\n",
206 dentry->d_inode->i_ino);
207 /* XXX: ignore because clear_fsetroot is broken */
216 cache = presto_get_cache(dentry->d_inode);
218 CERROR("No cache found for inode %ld\n",
219 dentry->d_inode->i_ino);
225 PRESTO_ALLOC(fset, sizeof(*fset));
227 CERROR("No memory allocating fset for %s\n", fsetname);
232 CDEBUG(D_INODE, "fset at %p\n", fset);
234 CDEBUG(D_INODE, "InterMezzo: fsetroot: inode %ld, fileset name %s\n",
235 dentry->d_inode->i_ino, fsetname);
237 fset->fset_mnt = mntget(current->fs->pwdmnt);
238 fset->fset_cache = cache;
239 fset->fset_dentry = dentry;
240 fset->fset_name = strdup(fsetname);
241 fset->fset_chunkbits = CHUNK_BITS;
242 fset->fset_flags = flags;
243 fset->fset_file_maxio = FSET_DEFAULT_MAX_FILEIO;
244 fset->fset_permit_lock = SPIN_LOCK_UNLOCKED;
245 PRESTO_ALLOC(fset->fset_reint_buf, 64 * 1024);
246 if (fset->fset_reint_buf == NULL) {
251 init_waitqueue_head(&fset->fset_permit_queue);
253 if (presto_d2d(dentry) == NULL) {
254 dentry->d_fsdata = izo_alloc_ddata();
256 if (presto_d2d(dentry) == NULL) {
257 CERROR("InterMezzo: %s: no memory\n", __FUNCTION__);
262 presto_d2d(dentry)->dd_fset = fset;
263 list_add(&fset->fset_list, &cache->cache_fset_list);
265 error = izo_init_kml_file(fset, &fset->fset_kml);
268 CDEBUG(D_JOURNAL, "Error init_kml %d\n", error);
272 error = izo_init_lml_file(fset, &fset->fset_lml);
276 rc = izo_log_close(&fset->fset_kml);
277 CDEBUG(D_JOURNAL, "Error init_lml %d, cleanup %d\n", error, rc);
281 /* init_last_rcvd_file could trigger a presto_file_write(), which
282 * requires that the lml structure be initialized. -phil */
283 error = izo_init_last_rcvd_file(fset, &fset->fset_rcvd);
287 rc = izo_log_close(&fset->fset_kml);
288 rc = izo_log_close(&fset->fset_lml);
289 CDEBUG(D_JOURNAL, "Error init_lastrcvd %d, cleanup %d\n", error, rc);
293 CDEBUG(D_PIOCTL, "-------> fset at %p, dentry at %p, mtpt %p,"
294 "fset %s, cache %p, presto_d2d(dentry)->dd_fset %p\n",
295 fset, dentry, fset->fset_dentry, fset->fset_name, cache,
296 presto_d2d(dentry)->dd_fset);
302 list_del(&fset->fset_list);
303 presto_d2d(dentry)->dd_fset = NULL;
306 mntput(fset->fset_mnt);
307 if (fset->fset_reint_buf != NULL)
308 PRESTO_FREE(fset->fset_reint_buf, 64 * 1024);
309 PRESTO_FREE(fset, sizeof(*fset));
316 static int izo_cleanup_fset(struct presto_file_set *fset)
319 struct presto_cache *cache;
323 CERROR("Cleaning up fset %s\n", fset->fset_name);
325 error = izo_log_close(&fset->fset_kml);
327 CERROR("InterMezzo: Closing kml for fset %s: %d\n",
328 fset->fset_name, error);
329 error = izo_log_close(&fset->fset_lml);
331 CERROR("InterMezzo: Closing lml for fset %s: %d\n",
332 fset->fset_name, error);
333 error = izo_log_close(&fset->fset_rcvd);
335 CERROR("InterMezzo: Closing last_rcvd for fset %s: %d\n",
336 fset->fset_name, error);
338 cache = fset->fset_cache;
340 list_del(&fset->fset_list);
342 presto_d2d(fset->fset_dentry)->dd_fset = NULL;
343 dput(fset->fset_dentry);
344 mntput(fset->fset_mnt);
346 PRESTO_FREE(fset->fset_name, strlen(fset->fset_name) + 1);
347 PRESTO_FREE(fset->fset_reint_buf, 64 * 1024);
348 PRESTO_FREE(fset, sizeof(*fset));
353 int izo_clear_fsetroot(struct dentry *dentry)
355 struct presto_file_set *fset;
359 fset = presto_dentry2fset(dentry);
365 izo_cleanup_fset(fset);
370 int izo_clear_all_fsetroots(struct presto_cache *cache)
372 struct presto_file_set *fset;
373 struct list_head *tmp,*tmpnext;
377 tmp = &cache->cache_fset_list;
379 while ( tmpnext != &cache->cache_fset_list) {
382 fset = list_entry(tmp, struct presto_file_set, fset_list);
384 error = izo_cleanup_fset(fset);
391 static struct vfsmount *izo_alloc_vfsmnt(void)
393 struct vfsmount *mnt;
394 PRESTO_ALLOC(mnt, sizeof(*mnt));
396 memset(mnt, 0, sizeof(struct vfsmount));
397 atomic_set(&mnt->mnt_count,1);
398 INIT_LIST_HEAD(&mnt->mnt_hash);
399 INIT_LIST_HEAD(&mnt->mnt_child);
400 INIT_LIST_HEAD(&mnt->mnt_mounts);
401 INIT_LIST_HEAD(&mnt->mnt_list);
407 static void izo_setup_ctxt(struct dentry *root, struct vfsmount *mnt,
408 struct run_ctxt *save)
412 mnt->mnt_root = root;
413 mnt->mnt_sb = root->d_inode->i_sb;
414 unlock_super(mnt->mnt_sb);
423 /* XXX where can we get the groups from? */
426 push_ctxt(save, &new);
429 static void izo_cleanup_ctxt(struct vfsmount *mnt, struct run_ctxt *save)
431 lock_super(mnt->mnt_sb);
435 static int izo_simple_mkdir(struct dentry *dir, char *name, int mode)
437 struct dentry *dchild;
441 dchild = lookup_one_len(name, dir, strlen(name));
442 if (IS_ERR(dchild)) {
444 return PTR_ERR(dchild);
447 if (dchild->d_inode) {
453 err = vfs_mkdir(dir->d_inode, dchild, mode);
460 static int izo_simple_symlink(struct dentry *dir, char *name, char *tgt)
462 struct dentry *dchild;
466 dchild = lookup_one_len(name, dir, strlen(name));
467 if (IS_ERR(dchild)) {
469 return PTR_ERR(dchild);
472 if (dchild->d_inode) {
478 err = vfs_symlink(dir->d_inode, dchild, tgt);
486 * run set_fsetroot in chroot environment
488 int presto_set_fsetroot_from_ioc(struct dentry *root, char *fsetname,
492 struct presto_cache *cache;
493 struct vfsmount *mnt;
494 struct run_ctxt save;
496 if (root != root->d_inode->i_sb->s_root) {
497 CERROR ("IOC_SET_FSET must be called on mount point\n");
501 cache = presto_get_cache(root->d_inode);
502 mnt = cache->cache_vfsmount;
508 izo_setup_ctxt(root, mnt, &save);
509 rc = presto_set_fsetroot(root, fsetname, flags);
510 izo_cleanup_ctxt(mnt, &save);
514 /* XXX: this function should detect if fsetname is already in use for
517 int izo_prepare_fileset(struct dentry *root, char *fsetname)
520 struct dentry *dotizo = NULL, *fsetdir = NULL, *dotiopen = NULL;
521 struct presto_cache *cache;
522 struct vfsmount *mnt;
523 struct run_ctxt save;
525 cache = presto_get_cache(root->d_inode);
526 mnt = cache->cache_vfsmount = izo_alloc_vfsmnt();
533 fsetname = "rootfset";
535 izo_setup_ctxt(root, mnt, &save);
537 err = izo_simple_mkdir(root, ".intermezzo", 0755);
538 CDEBUG(D_CACHE, "mkdir on .intermezzo err %d\n", err);
540 err = izo_simple_mkdir(root, "..iopen..", 0755);
541 CDEBUG(D_CACHE, "mkdir on ..iopen.. err %d\n", err);
543 dotiopen = lookup_one_len("..iopen..", root, strlen("..iopen.."));
544 if (IS_ERR(dotiopen)) {
548 dotiopen->d_inode->i_op = &presto_dir_iops;
552 dotizo = lookup_one_len(".intermezzo", root, strlen(".intermezzo"));
553 if (IS_ERR(dotizo)) {
559 err = izo_simple_mkdir(dotizo, fsetname, 0755);
560 CDEBUG(D_CACHE, "mkdir err %d\n", err);
562 /* XXX find the dentry of the root of the fileset (root for now) */
563 fsetdir = lookup_one_len(fsetname, dotizo, strlen(fsetname));
564 if (IS_ERR(fsetdir)) {
569 err = izo_simple_symlink(fsetdir, "ROOT", "../..");
571 /* XXX read flags from flags file */
572 err = presto_set_fsetroot(root, fsetname, 0);
573 CDEBUG(D_CACHE, "set_fsetroot err %d\n", err);
576 if (dotizo && !IS_ERR(dotizo))
578 if (fsetdir && !IS_ERR(fsetdir))
580 izo_cleanup_ctxt(mnt, &save);
584 int izo_set_fileid(struct file *dir, struct izo_ioctl_data *data)
587 struct presto_cache *cache;
588 struct vfsmount *mnt;
589 struct run_ctxt save;
591 struct dentry *dentry;
592 struct presto_dentry_data *dd;
599 root = dir->f_dentry;
601 /* actually, needs to be called on ROOT of fset, not mount point
602 if (root != root->d_inode->i_sb->s_root) {
603 CERROR ("IOC_SET_FSET must be called on mount point\n");
608 cache = presto_get_cache(root->d_inode);
609 mnt = cache->cache_vfsmount;
615 izo_setup_ctxt(root, mnt, &save);
617 PRESTO_ALLOC(buf, data->ioc_plen1);
623 if (copy_from_user(buf, data->ioc_pbuf1, data->ioc_plen1)) {
629 rc = presto_walk(buf, &nd);
631 CERROR("Unable to open: %s\n", buf);
637 CERROR("no dentry!\n");
642 dd = presto_d2d(dentry);
644 CERROR("no dentry_data!\n");
650 CDEBUG(D_FILE,"de:%p dd:%p\n", dentry, dd);
652 if (dd->remote_ino != 0) {
653 CERROR("remote_ino already set? %Lx:%Lx\n", dd->remote_ino,
654 dd->remote_generation);
661 CDEBUG(D_FILE,"setting %p %p, %s to %Lx:%Lx\n", dentry, dd,
663 data->ioc_generation);
664 dd->remote_ino = data->ioc_ino;
665 dd->remote_generation = data->ioc_generation;
672 PRESTO_FREE(buf, data->ioc_plen1);
673 izo_cleanup_ctxt(mnt, &save);