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)+1);
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__);
263 presto_d2d(dentry)->dd_fset = fset;
264 list_add(&fset->fset_list, &cache->cache_fset_list);
266 error = izo_init_kml_file(fset, &fset->fset_kml);
269 CDEBUG(D_JOURNAL, "Error init_kml %d\n", error);
273 error = izo_init_lml_file(fset, &fset->fset_lml);
277 rc = izo_log_close(&fset->fset_kml);
278 CDEBUG(D_JOURNAL, "Error init_lml %d, cleanup %d\n", error, rc);
282 /* init_last_rcvd_file could trigger a presto_file_write(), which
283 * requires that the lml structure be initialized. -phil */
284 error = izo_init_last_rcvd_file(fset, &fset->fset_rcvd);
288 rc = izo_log_close(&fset->fset_kml);
289 rc = izo_log_close(&fset->fset_lml);
290 CDEBUG(D_JOURNAL, "Error init_lastrcvd %d, cleanup %d\n", error, rc);
294 CDEBUG(D_PIOCTL, "-------> fset at %p, dentry at %p, mtpt %p,"
295 "fset %s, cache %p, presto_d2d(dentry)->dd_fset %p\n",
296 fset, dentry, fset->fset_dentry, fset->fset_name, cache,
297 presto_d2d(dentry)->dd_fset);
303 list_del(&fset->fset_list);
304 presto_d2d(dentry)->dd_fset = NULL;
307 mntput(fset->fset_mnt);
308 if (fset->fset_reint_buf != NULL)
309 PRESTO_FREE(fset->fset_reint_buf, 64 * 1024);
310 PRESTO_FREE(fset, sizeof(*fset));
317 static int izo_cleanup_fset(struct presto_file_set *fset)
320 struct presto_cache *cache;
324 CERROR("Cleaning up fset %s\n", fset->fset_name);
326 error = izo_log_close(&fset->fset_kml);
328 CERROR("InterMezzo: Closing kml for fset %s: %d\n",
329 fset->fset_name, error);
330 error = izo_log_close(&fset->fset_lml);
332 CERROR("InterMezzo: Closing lml for fset %s: %d\n",
333 fset->fset_name, error);
334 error = izo_log_close(&fset->fset_rcvd);
336 CERROR("InterMezzo: Closing last_rcvd for fset %s: %d\n",
337 fset->fset_name, error);
339 cache = fset->fset_cache;
341 list_del(&fset->fset_list);
343 presto_d2d(fset->fset_dentry)->dd_fset = NULL;
344 dput(fset->fset_dentry);
345 mntput(fset->fset_mnt);
347 izo_rep_cache_clean(fset);
348 PRESTO_FREE(fset->fset_name, strlen(fset->fset_name) + 1);
349 PRESTO_FREE(fset->fset_reint_buf, 64 * 1024);
350 PRESTO_FREE(fset, sizeof(*fset));
355 int izo_clear_fsetroot(struct dentry *dentry)
357 struct presto_file_set *fset;
361 fset = presto_dentry2fset(dentry);
367 izo_cleanup_fset(fset);
372 int izo_clear_all_fsetroots(struct presto_cache *cache)
374 struct presto_file_set *fset;
375 struct list_head *tmp,*tmpnext;
379 tmp = &cache->cache_fset_list;
381 while ( tmpnext != &cache->cache_fset_list) {
384 fset = list_entry(tmp, struct presto_file_set, fset_list);
386 error = izo_cleanup_fset(fset);
393 static struct vfsmount *izo_alloc_vfsmnt(void)
395 struct vfsmount *mnt;
396 PRESTO_ALLOC(mnt, sizeof(*mnt));
398 memset(mnt, 0, sizeof(struct vfsmount));
399 atomic_set(&mnt->mnt_count,1);
400 INIT_LIST_HEAD(&mnt->mnt_hash);
401 INIT_LIST_HEAD(&mnt->mnt_child);
402 INIT_LIST_HEAD(&mnt->mnt_mounts);
403 INIT_LIST_HEAD(&mnt->mnt_list);
409 static void izo_setup_ctxt(struct dentry *root, struct vfsmount *mnt,
410 struct run_ctxt *save)
414 mnt->mnt_root = root;
415 mnt->mnt_sb = root->d_inode->i_sb;
416 unlock_super(mnt->mnt_sb);
425 /* XXX where can we get the groups from? */
428 push_ctxt(save, &new);
431 static void izo_cleanup_ctxt(struct vfsmount *mnt, struct run_ctxt *save)
433 lock_super(mnt->mnt_sb);
437 static int izo_simple_mkdir(struct dentry *dir, char *name, int mode)
439 struct dentry *dchild;
443 dchild = lookup_one_len(name, dir, strlen(name));
444 if (IS_ERR(dchild)) {
446 return PTR_ERR(dchild);
449 if (dchild->d_inode) {
455 err = vfs_mkdir(dir->d_inode, dchild, mode);
462 static int izo_simple_symlink(struct dentry *dir, char *name, char *tgt)
464 struct dentry *dchild;
468 dchild = lookup_one_len(name, dir, strlen(name));
469 if (IS_ERR(dchild)) {
471 return PTR_ERR(dchild);
474 if (dchild->d_inode) {
480 err = vfs_symlink(dir->d_inode, dchild, tgt);
488 * run set_fsetroot in chroot environment
490 int presto_set_fsetroot_from_ioc(struct dentry *root, char *fsetname,
494 struct presto_cache *cache;
495 struct vfsmount *mnt;
496 struct run_ctxt save;
498 if (root != root->d_inode->i_sb->s_root) {
499 CERROR ("IOC_SET_FSET must be called on mount point\n");
503 cache = presto_get_cache(root->d_inode);
504 mnt = cache->cache_vfsmount;
510 izo_setup_ctxt(root, mnt, &save);
511 rc = presto_set_fsetroot(root, fsetname, flags);
512 izo_cleanup_ctxt(mnt, &save);
516 /* XXX: this function should detect if fsetname is already in use for
519 int izo_prepare_fileset(struct dentry *root, char *fsetname)
522 struct dentry *dotizo = NULL, *fsetdir = NULL, *dotiopen = NULL;
523 struct presto_cache *cache;
524 struct vfsmount *mnt;
525 struct run_ctxt save;
527 cache = presto_get_cache(root->d_inode);
528 mnt = cache->cache_vfsmount = izo_alloc_vfsmnt();
535 fsetname = "rootfset";
537 izo_setup_ctxt(root, mnt, &save);
539 err = izo_simple_mkdir(root, ".intermezzo", 0755);
540 CDEBUG(D_CACHE, "mkdir on .intermezzo err %d\n", err);
542 err = izo_simple_mkdir(root, "..iopen..", 0755);
543 CDEBUG(D_CACHE, "mkdir on ..iopen.. err %d\n", err);
545 dotiopen = lookup_one_len("..iopen..", root, strlen("..iopen.."));
546 if (IS_ERR(dotiopen)) {
550 dotiopen->d_inode->i_op = &presto_dir_iops;
554 dotizo = lookup_one_len(".intermezzo", root, strlen(".intermezzo"));
555 if (IS_ERR(dotizo)) {
561 err = izo_simple_mkdir(dotizo, fsetname, 0755);
562 CDEBUG(D_CACHE, "mkdir err %d\n", err);
564 /* XXX find the dentry of the root of the fileset (root for now) */
565 fsetdir = lookup_one_len(fsetname, dotizo, strlen(fsetname));
566 if (IS_ERR(fsetdir)) {
571 err = izo_simple_symlink(fsetdir, "ROOT", "../..");
573 /* XXX read flags from flags file */
574 err = presto_set_fsetroot(root, fsetname, 0);
575 CDEBUG(D_CACHE, "set_fsetroot err %d\n", err);
578 if (dotizo && !IS_ERR(dotizo))
580 if (fsetdir && !IS_ERR(fsetdir))
582 izo_cleanup_ctxt(mnt, &save);
586 int izo_set_fileid(struct file *dir, struct izo_ioctl_data *data)
589 struct presto_cache *cache;
590 struct vfsmount *mnt;
591 struct run_ctxt save;
593 struct dentry *dentry;
594 struct presto_dentry_data *dd;
601 root = dir->f_dentry;
603 /* actually, needs to be called on ROOT of fset, not mount point
604 if (root != root->d_inode->i_sb->s_root) {
605 CERROR ("IOC_SET_FSET must be called on mount point\n");
610 cache = presto_get_cache(root->d_inode);
611 mnt = cache->cache_vfsmount;
617 izo_setup_ctxt(root, mnt, &save);
619 PRESTO_ALLOC(buf, data->ioc_plen1);
625 if (copy_from_user(buf, data->ioc_pbuf1, data->ioc_plen1)) {
631 rc = presto_walk(buf, &nd);
633 CERROR("Unable to open: %s\n", buf);
639 CERROR("no dentry!\n");
644 dd = presto_d2d(dentry);
646 CERROR("no dentry_data!\n");
652 CDEBUG(D_FILE,"de:%p dd:%p\n", dentry, dd);
654 if (dd->remote_ino != 0) {
655 CERROR("remote_ino already set? %Lx:%Lx\n", dd->remote_ino,
656 dd->remote_generation);
663 CDEBUG(D_FILE,"setting %p %p, %s to %Lx:%Lx\n", dentry, dd,
665 data->ioc_generation);
666 dd->remote_ino = data->ioc_ino;
667 dd->remote_generation = data->ioc_generation;
674 PRESTO_FREE(buf, data->ioc_plen1);
675 izo_cleanup_ctxt(mnt, &save);