1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre 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 * Lustre 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 Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_SM
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 #include <linux/unistd.h>
31 #include <linux/smp_lock.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd_support.h>
34 #include <linux/lustre_lib.h>
35 #include <linux/lustre_idl.h>
36 #include <linux/lustre_fsfilt.h>
37 #include <linux/lustre_smfs.h>
38 #include <linux/lustre_snap.h>
39 #include <linux/security.h>
40 #include "smfs_internal.h"
42 //#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 static int smfs_create(struct inode *dir, struct dentry *dentry,
48 static int smfs_create(struct inode *dir, struct dentry *dentry,
49 int mode, struct nameidata *nd)
52 struct inode *inode = NULL;
53 struct inode *parent = I2CI(dentry->d_parent->d_inode);
54 struct inode *cache_dir = I2CI(dir);
55 struct dentry *cache_dentry = NULL;
56 struct dentry *cache_parent = NULL;
58 struct hook_msg msg = {
66 LASSERT(cache_dir->i_op->create);
68 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
69 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
70 if (!cache_dentry || !cache_parent) {
75 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
81 SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
83 pre_smfs_inode(dir, cache_dir);
85 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
86 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
88 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
91 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
94 d_instantiate(dentry, inode);
100 SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc);
102 post_smfs_inode(dir, cache_dir);
103 smfs_trans_commit(dir, handle, 0);
106 post_smfs_dentry(cache_dentry);
107 post_smfs_dentry(cache_parent);
111 static struct dentry * iopen_connect_dentry(struct dentry * dentry,
112 struct inode *inode, int rehash)
114 struct dentry *tmp, *goal = NULL;
115 struct list_head *lp;
117 /* verify this dentry is really new */
118 LASSERT(dentry->d_inode == NULL);
119 LASSERT(list_empty(&dentry->d_alias)); /* d_instantiate */
121 LASSERT(d_unhashed(dentry)); /* d_rehash */
122 LASSERT(list_empty(&dentry->d_subdirs));
124 spin_lock(&dcache_lock);
128 /* preferrably return a connected dentry */
129 list_for_each(lp, &inode->i_dentry) {
130 tmp = list_entry(lp, struct dentry, d_alias);
131 if (tmp->d_flags & DCACHE_DISCONNECTED) {
132 LASSERT(tmp->d_alias.next == &inode->i_dentry);
133 LASSERT(tmp->d_alias.prev == &inode->i_dentry);
143 /* Move the goal to the de hash queue */
144 goal->d_flags &= ~ DCACHE_DISCONNECTED;
145 security_d_instantiate(goal, inode);
147 __d_move(goal, dentry);
148 spin_unlock(&dcache_lock);
153 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
155 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
156 dentry->d_inode = inode;
160 spin_unlock(&dcache_lock);
166 static int smfs_do_lookup (struct inode * dir,
167 struct dentry * dentry,
168 struct nameidata *nd,
169 struct inode **inode)
171 struct inode *cache_dir = I2CI(dir);
172 struct inode *parent = I2CI(dentry->d_parent->d_inode);
173 struct dentry *cache_dentry = NULL;
174 struct dentry *cache_parent = NULL;
175 struct dentry *rdentry = NULL, *tmp = NULL;
177 struct hook_msg msg = {
186 LASSERT(cache_dir->i_op->lookup);
188 /* preparing artificial backing fs dentries. */
189 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
190 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
191 if (!cache_dentry || !cache_parent) {
196 SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
198 /* perform lookup in backing fs. */
199 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
201 if (IS_ERR(rdentry)) {
202 rc = PTR_ERR(rdentry);
211 SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
214 //copy fields if DCACHE_CROSS_REF
215 smfs_update_dentry(dentry, tmp);
218 if (!tmp->d_inode->i_nlink)
219 CWARN("inode #%lu (%p) nlink is 0\n",
220 tmp->d_inode->i_ino, tmp->d_inode);
222 *inode = smfs_get_inode(dir->i_sb, tmp->d_inode->i_ino,
234 post_smfs_dentry(cache_dentry);
235 post_smfs_dentry(cache_parent);
240 static struct dentry * smfs_iopen_lookup(struct inode * dir,
241 struct dentry *dentry,
242 struct nameidata *nd)
244 struct dentry * alternate = NULL;
245 struct inode *inode = NULL;
249 rc = smfs_do_lookup(dir, dentry, nd, &inode);
254 /* preferrably return a connected dentry */
255 spin_lock(&dcache_lock);
256 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
257 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
260 list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
261 dget_locked(alternate);
262 spin_lock(&alternate->d_lock);
263 alternate->d_flags |= DCACHE_REFERENCED;
264 spin_unlock(&alternate->d_lock);
266 spin_unlock(&dcache_lock);
270 dentry->d_flags |= DCACHE_DISCONNECTED;
272 /* d_add(), but don't drop dcache_lock before adding dentry to inode */
273 list_add(&dentry->d_alias, &inode->i_dentry); /* d_instantiate */
274 dentry->d_inode = inode;
276 __d_rehash(dentry); /* d_rehash */
277 spin_unlock(&dcache_lock);
282 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
283 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
285 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
286 struct nameidata *nd)
289 struct dentry * rdentry = NULL;
290 struct inode * inode = NULL;
295 rc = smfs_do_lookup(dir, dentry, nd, &inode);
299 //lmv stuff. Special dentry that has no inode.
300 if (dentry->d_flags & DCACHE_CROSS_REF) {
304 //TODO: should flags be checked and copied before?
305 rdentry = iopen_connect_dentry(dentry, inode, 1);
310 static int smfs_lookup_raw(struct inode *dir, const char *name,
311 int len, ino_t *data)
313 struct inode *cache_dir = I2CI(dir);
319 if (cache_dir->i_op->lookup_raw) {
320 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
322 CWARN("do not have raw lookup ops in bottom fs\n");
328 static int smfs_link(struct dentry *old_dentry,
329 struct inode *dir, struct dentry *dentry)
331 struct inode *parent = I2CI(dentry->d_parent->d_inode);
332 struct inode *cache_dir = I2CI(dir);
333 struct inode *old_inode = old_dentry->d_inode;
334 struct inode *cache_old_inode = I2CI(old_inode);
335 struct dentry *cache_old_dentry = NULL;
336 struct dentry *cache_dentry = NULL;
337 struct dentry *cache_parent = NULL;
340 struct hook_link_msg msg = {
341 .dentry = old_dentry,
350 if (!cache_old_inode)
353 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
354 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
355 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
356 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
361 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
362 if (IS_ERR(handle)) {
367 pre_smfs_inode(dir, cache_dir);
368 pre_smfs_inode(old_inode, cache_old_inode);
371 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
373 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
375 atomic_inc(&old_inode->i_count);
376 dput(iopen_connect_dentry(dentry, old_inode, 0));
379 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
381 post_smfs_inode(old_inode, cache_old_inode);
382 post_smfs_inode(dir, cache_dir);
384 smfs_trans_commit(dir, handle, 0);
388 post_smfs_dentry(cache_dentry);
389 post_smfs_dentry(cache_parent);
390 post_smfs_dentry(cache_old_dentry);
395 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
397 struct inode *cache_dir = I2CI(dir);
398 struct inode *cache_inode = I2CI(dentry->d_inode);
399 struct inode *parent = I2CI(dentry->d_parent->d_inode);
400 struct dentry *cache_dentry = NULL;
401 struct dentry *cache_parent = NULL;
405 struct hook_unlink_msg msg = {
407 .mode = dentry->d_inode->i_mode
413 LASSERT(cache_inode);
414 LASSERT(cache_dir->i_op->unlink);
417 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
418 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
419 if (!cache_dentry || !cache_parent) {
424 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
425 if (IS_ERR(handle)) {
430 pre_smfs_inode(dir, cache_dir);
431 pre_smfs_inode(dentry->d_inode, cache_inode);
433 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
435 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
437 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
439 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
440 post_smfs_inode(dir, cache_dir);
442 smfs_trans_commit(dir, handle, 0);
444 post_smfs_dentry(cache_dentry);
445 post_smfs_dentry(cache_parent);
449 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
452 struct inode *cache_dir = I2CI(dir);
453 struct inode *inode = NULL;
454 struct inode *parent = I2CI(dentry->d_parent->d_inode);
455 struct dentry *cache_dentry = NULL;
456 struct dentry *cache_parent = NULL;
459 struct hook_symlink_msg msg = {
461 .tgt_len = strlen(symname) + 1,
462 .symname = (char*)symname
468 LASSERT(cache_dir->i_op->symlink);
471 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
472 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
473 if (!cache_parent || !cache_dentry) {
478 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
479 if (IS_ERR(handle)) {
484 pre_smfs_inode(dir, cache_dir);
486 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
488 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
490 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
493 d_instantiate(dentry, inode);
499 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
501 post_smfs_inode(dir, cache_dir);
502 smfs_trans_commit(dir, handle, 0);
505 post_smfs_dentry(cache_dentry);
506 post_smfs_dentry(cache_parent);
510 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
512 struct inode *cache_dir = I2CI(dir);
513 struct inode *parent = I2CI(dentry->d_parent->d_inode);
514 struct inode *inode = NULL;
515 struct dentry *cache_dentry = NULL;
516 struct dentry *cache_parent = NULL;
519 struct hook_msg msg = {
528 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
529 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
530 if (!cache_parent || !cache_dentry) {
535 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
536 if (IS_ERR(handle)) {
541 pre_smfs_inode(dir, cache_dir);
542 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
544 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
546 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
549 //smsf_update_dentry(dentry, cache_dentry);
550 d_instantiate(dentry, inode);
556 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
557 post_smfs_inode(dir, cache_dir);
558 smfs_trans_commit(dir, handle, 0);
561 post_smfs_dentry(cache_dentry);
562 post_smfs_dentry(cache_parent);
566 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
568 struct inode *cache_dir = I2CI(dir);
569 struct inode *cache_inode = I2CI(dentry->d_inode);
570 struct inode *parent = I2CI(dentry->d_parent->d_inode);
571 struct dentry *cache_dentry = NULL;
572 struct dentry *cache_parent = NULL;
575 struct hook_unlink_msg msg = {
583 LASSERT(cache_dir->i_op->rmdir);
586 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
587 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
588 if (!cache_parent || !cache_dentry) {
593 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
594 if (IS_ERR(handle) ) {
599 pre_smfs_inode(dir, cache_dir);
600 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
602 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
604 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
606 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
608 post_smfs_inode(dir, cache_dir);
609 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
610 //like vfs_rmdir is doing with inode
612 cache_dentry->d_inode->i_flags |= S_DEAD;
614 smfs_trans_commit(dir, handle, 0);
617 post_smfs_dentry(cache_dentry);
618 post_smfs_dentry(cache_parent);
622 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
623 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
626 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
627 int mode, dev_t rdev)
630 struct inode *cache_dir = I2CI(dir);
631 struct inode *inode = NULL;
632 struct inode *parent = I2CI(dentry->d_parent->d_inode);
633 struct dentry *cache_dentry = NULL;
634 struct dentry *cache_parent = NULL;
637 struct hook_msg msg = {
645 LASSERT(cache_dir->i_op->mknod);
647 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
648 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
649 if (!cache_parent || !cache_dentry) {
654 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
655 if (IS_ERR(handle)) {
660 pre_smfs_inode(dir, cache_dir);
662 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
664 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
666 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
669 //smsf_update_dentry(dentry, cache_dentry);
670 d_instantiate(dentry, inode);
676 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
678 post_smfs_inode(dir, cache_dir);
680 smfs_trans_commit(dir, handle, 0);
683 post_smfs_dentry(cache_dentry);
684 post_smfs_dentry(cache_parent);
688 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
689 struct inode *new_dir,struct dentry *new_dentry)
691 struct inode *cache_old_dir = I2CI(old_dir);
692 struct inode *cache_new_dir = I2CI(new_dir);
693 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
694 struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
695 struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
696 struct inode *cache_new_inode = NULL;
697 struct dentry *cache_old_dentry = NULL;
698 struct dentry *cache_new_dentry = NULL;
699 struct dentry *cache_new_parent = NULL;
700 struct dentry *cache_old_parent = NULL;
703 struct hook_rename_msg msg = {
704 .dentry = old_dentry,
706 .new_dentry = new_dentry
711 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
714 if (new_dentry->d_inode) {
715 cache_new_inode = I2CI(new_dentry->d_inode);
716 if (!cache_new_inode)
720 cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
721 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
723 if (!cache_old_parent || !cache_old_dentry) {
728 cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
729 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
731 if (!cache_new_parent || !cache_new_dentry) {
736 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
737 if (IS_ERR(handle)) {
742 pre_smfs_inode(old_dir, cache_old_dir);
743 pre_smfs_inode(new_dir, cache_new_dir);
744 if (new_dentry->d_inode)
745 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
747 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
749 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
750 cache_new_dir, cache_new_dentry);
752 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
754 post_smfs_inode(old_dir, cache_old_dir);
755 post_smfs_inode(new_dir, cache_new_dir);
756 if (new_dentry->d_inode)
757 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
759 smfs_trans_commit(old_dir, handle, 0);
762 post_smfs_dentry(cache_old_dentry);
763 post_smfs_dentry(cache_old_parent);
764 post_smfs_dentry(cache_new_dentry);
765 post_smfs_dentry(cache_new_parent);
769 struct inode_operations smfs_dir_iops = {
772 lookup_raw: smfs_lookup_raw,
773 link: smfs_link, /* BKL held */
774 unlink: smfs_unlink, /* BKL held */
775 symlink: smfs_symlink, /* BKL held */
776 mkdir: smfs_mkdir, /* BKL held */
777 rmdir: smfs_rmdir, /* BKL held */
778 mknod: smfs_mknod, /* BKL held */
779 rename: smfs_rename, /* BKL held */
780 setxattr: smfs_setxattr, /* BKL held */
781 getxattr: smfs_getxattr, /* BKL held */
782 listxattr: smfs_listxattr, /* BKL held */
783 removexattr: smfs_removexattr, /* BKL held */
786 struct inode_operations smfs_iopen_iops = {
787 lookup: smfs_iopen_lookup,
790 static ssize_t smfs_read_dir(struct file *filp, char *buf,
791 size_t size, loff_t *ppos)
793 struct dentry *dentry = filp->f_dentry;
794 struct inode *cache_inode = NULL;
795 struct smfs_file_info *sfi = NULL;
797 loff_t *cache_ppos = NULL;
802 cache_inode = I2CI(dentry->d_inode);
804 if (!cache_inode || !cache_inode->i_fop->read)
808 if (sfi->magic != SMFS_FILE_MAGIC)
811 if (ppos != &(filp->f_pos))
812 cache_ppos = &tmp_ppos;
814 cache_ppos = &sfi->c_file->f_pos;
818 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
824 duplicate_file(filp, sfi->c_file);
829 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
831 struct dentry *dentry = filp->f_dentry;
832 struct inode *cache_inode = NULL;
833 struct smfs_file_info *sfi = NULL;
835 struct hook_readdir_msg msg = {
844 cache_inode = I2CI(dentry->d_inode);
845 if (!cache_inode || !cache_inode->i_fop->readdir)
849 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
851 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
853 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
855 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
856 duplicate_file(filp, sfi->c_file);
861 struct file_operations smfs_dir_fops = {
862 .read = smfs_read_dir,
863 .readdir = smfs_readdir, /* BKL held */
864 .ioctl = smfs_ioctl, /* BKL held */
865 .fsync = smfs_fsync, /* BKL held */
867 .release = smfs_release,
870 struct file_operations smfs_iopen_fops = {
871 .read = smfs_read_dir,