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,
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,
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);
311 static int smfs_lookup_raw(struct inode *dir, const char *name,
312 int len, ino_t *data)
314 struct inode *cache_dir = I2CI(dir);
320 if (cache_dir->i_op->lookup_raw) {
321 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
323 CWARN("do not have raw lookup ops in bottom fs\n");
330 static int smfs_link(struct dentry *old_dentry,
331 struct inode *dir, struct dentry *dentry)
333 struct inode *parent = I2CI(dentry->d_parent->d_inode);
334 struct inode *cache_dir = I2CI(dir);
335 struct inode *old_inode = old_dentry->d_inode;
336 struct inode *cache_old_inode = I2CI(old_inode);
337 struct dentry *cache_old_dentry = NULL;
338 struct dentry *cache_dentry = NULL;
339 struct dentry *cache_parent = NULL;
342 struct hook_link_msg msg = {
343 .dentry = old_dentry,
352 if (!cache_old_inode)
355 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
356 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
357 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
358 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
363 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
364 if (IS_ERR(handle)) {
369 pre_smfs_inode(dir, cache_dir);
370 pre_smfs_inode(old_inode, cache_old_inode);
373 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
375 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
377 atomic_inc(&old_inode->i_count);
378 dput(iopen_connect_dentry(dentry, old_inode, 0));
381 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
383 post_smfs_inode(old_inode, cache_old_inode);
384 post_smfs_inode(dir, cache_dir);
386 smfs_trans_commit(dir, handle, 0);
390 post_smfs_dentry(cache_dentry);
391 post_smfs_dentry(cache_parent);
392 post_smfs_dentry(cache_old_dentry);
397 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
399 struct inode *cache_dir = I2CI(dir);
400 struct inode *cache_inode = I2CI(dentry->d_inode);
401 struct inode *parent = I2CI(dentry->d_parent->d_inode);
402 struct dentry *cache_dentry = NULL;
403 struct dentry *cache_parent = NULL;
407 struct hook_unlink_msg msg = {
409 .mode = dentry->d_inode->i_mode
415 LASSERT(cache_inode);
416 LASSERT(cache_dir->i_op->unlink);
419 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
420 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
421 if (!cache_dentry || !cache_parent) {
426 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
427 if (IS_ERR(handle)) {
432 pre_smfs_inode(dir, cache_dir);
433 pre_smfs_inode(dentry->d_inode, cache_inode);
435 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
437 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
439 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
441 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
442 post_smfs_inode(dir, cache_dir);
444 smfs_trans_commit(dir, handle, 0);
446 post_smfs_dentry(cache_dentry);
447 post_smfs_dentry(cache_parent);
451 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
454 struct inode *cache_dir = I2CI(dir);
455 struct inode *inode = NULL;
456 struct inode *parent = I2CI(dentry->d_parent->d_inode);
457 struct dentry *cache_dentry = NULL;
458 struct dentry *cache_parent = NULL;
461 struct hook_symlink_msg msg = {
463 .tgt_len = strlen(symname) + 1,
464 .symname = (char*)symname
470 LASSERT(cache_dir->i_op->symlink);
473 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
474 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
475 if (!cache_parent || !cache_dentry) {
480 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
481 if (IS_ERR(handle)) {
486 pre_smfs_inode(dir, cache_dir);
488 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
490 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
492 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
495 d_instantiate(dentry, inode);
501 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
503 post_smfs_inode(dir, cache_dir);
504 smfs_trans_commit(dir, handle, 0);
507 post_smfs_dentry(cache_dentry);
508 post_smfs_dentry(cache_parent);
512 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
514 struct inode *cache_dir = I2CI(dir);
515 struct inode *parent = I2CI(dentry->d_parent->d_inode);
516 struct inode *inode = NULL;
517 struct dentry *cache_dentry = NULL;
518 struct dentry *cache_parent = NULL;
521 struct hook_msg msg = {
530 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
531 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
532 if (!cache_parent || !cache_dentry) {
537 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
538 if (IS_ERR(handle)) {
543 pre_smfs_inode(dir, cache_dir);
544 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
546 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
548 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
551 //smsf_update_dentry(dentry, cache_dentry);
552 d_instantiate(dentry, inode);
558 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
559 post_smfs_inode(dir, cache_dir);
560 smfs_trans_commit(dir, handle, 0);
563 post_smfs_dentry(cache_dentry);
564 post_smfs_dentry(cache_parent);
568 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
570 struct inode *cache_dir = I2CI(dir);
571 struct inode *cache_inode = I2CI(dentry->d_inode);
572 struct inode *parent = I2CI(dentry->d_parent->d_inode);
573 struct dentry *cache_dentry = NULL;
574 struct dentry *cache_parent = NULL;
577 struct hook_unlink_msg msg = {
585 LASSERT(cache_dir->i_op->rmdir);
588 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
589 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
590 if (!cache_parent || !cache_dentry) {
595 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
596 if (IS_ERR(handle) ) {
601 pre_smfs_inode(dir, cache_dir);
602 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
604 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
606 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
608 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
610 post_smfs_inode(dir, cache_dir);
611 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
612 //like vfs_rmdir is doing with inode
614 cache_dentry->d_inode->i_flags |= S_DEAD;
616 smfs_trans_commit(dir, handle, 0);
619 post_smfs_dentry(cache_dentry);
620 post_smfs_dentry(cache_parent);
624 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
625 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
628 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
629 int mode, dev_t rdev)
632 struct inode *cache_dir = I2CI(dir);
633 struct inode *inode = NULL;
634 struct inode *parent = I2CI(dentry->d_parent->d_inode);
635 struct dentry *cache_dentry = NULL;
636 struct dentry *cache_parent = NULL;
639 struct hook_msg msg = {
647 LASSERT(cache_dir->i_op->mknod);
649 cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
650 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
651 if (!cache_parent || !cache_dentry) {
656 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
657 if (IS_ERR(handle)) {
662 pre_smfs_inode(dir, cache_dir);
664 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
666 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
668 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
671 //smsf_update_dentry(dentry, cache_dentry);
672 d_instantiate(dentry, inode);
678 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
680 post_smfs_inode(dir, cache_dir);
682 smfs_trans_commit(dir, handle, 0);
685 post_smfs_dentry(cache_dentry);
686 post_smfs_dentry(cache_parent);
690 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
691 struct inode *new_dir,struct dentry *new_dentry)
693 struct inode *cache_old_dir = I2CI(old_dir);
694 struct inode *cache_new_dir = I2CI(new_dir);
695 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
696 struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
697 struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
698 struct inode *cache_new_inode = NULL;
699 struct dentry *cache_old_dentry = NULL;
700 struct dentry *cache_new_dentry = NULL;
701 struct dentry *cache_new_parent = NULL;
702 struct dentry *cache_old_parent = NULL;
705 struct hook_rename_msg msg = {
706 .dentry = old_dentry,
708 .new_dentry = new_dentry
713 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
716 if (new_dentry->d_inode) {
717 cache_new_inode = I2CI(new_dentry->d_inode);
718 if (!cache_new_inode)
722 cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
723 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
725 if (!cache_old_parent || !cache_old_dentry) {
730 cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
731 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
733 if (!cache_new_parent || !cache_new_dentry) {
738 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
739 if (IS_ERR(handle)) {
744 pre_smfs_inode(old_dir, cache_old_dir);
745 pre_smfs_inode(new_dir, cache_new_dir);
746 if (new_dentry->d_inode)
747 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
749 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
751 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
752 cache_new_dir, cache_new_dentry);
754 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
756 post_smfs_inode(old_dir, cache_old_dir);
757 post_smfs_inode(new_dir, cache_new_dir);
758 if (new_dentry->d_inode)
759 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
761 smfs_trans_commit(old_dir, handle, 0);
764 post_smfs_dentry(cache_old_dentry);
765 post_smfs_dentry(cache_old_parent);
766 post_smfs_dentry(cache_new_dentry);
767 post_smfs_dentry(cache_new_parent);
771 struct inode_operations smfs_dir_iops = {
775 lookup_raw: smfs_lookup_raw,
777 link: smfs_link, /* BKL held */
778 unlink: smfs_unlink, /* BKL held */
779 symlink: smfs_symlink, /* BKL held */
780 mkdir: smfs_mkdir, /* BKL held */
781 rmdir: smfs_rmdir, /* BKL held */
782 mknod: smfs_mknod, /* BKL held */
783 rename: smfs_rename, /* BKL held */
784 setxattr: smfs_setxattr, /* BKL held */
785 getxattr: smfs_getxattr, /* BKL held */
786 listxattr: smfs_listxattr, /* BKL held */
787 removexattr: smfs_removexattr, /* BKL held */
790 struct inode_operations smfs_iopen_iops = {
791 lookup: smfs_iopen_lookup,
794 static ssize_t smfs_read_dir(struct file *filp, char *buf,
795 size_t size, loff_t *ppos)
797 struct dentry *dentry = filp->f_dentry;
798 struct inode *cache_inode = NULL;
799 struct smfs_file_info *sfi = NULL;
801 loff_t *cache_ppos = NULL;
806 cache_inode = I2CI(dentry->d_inode);
808 if (!cache_inode || !cache_inode->i_fop->read)
812 if (sfi->magic != SMFS_FILE_MAGIC)
815 if (ppos != &(filp->f_pos))
816 cache_ppos = &tmp_ppos;
818 cache_ppos = &sfi->c_file->f_pos;
822 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
828 duplicate_file(filp, sfi->c_file);
833 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
835 struct dentry *dentry = filp->f_dentry;
836 struct inode *cache_inode = NULL;
837 struct smfs_file_info *sfi = NULL;
839 struct hook_readdir_msg msg = {
848 cache_inode = I2CI(dentry->d_inode);
849 if (!cache_inode || !cache_inode->i_fop->readdir)
853 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
855 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
857 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
859 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
860 duplicate_file(filp, sfi->c_file);
865 struct file_operations smfs_dir_fops = {
866 .read = smfs_read_dir,
867 .readdir = smfs_readdir, /* BKL held */
868 .ioctl = smfs_ioctl, /* BKL held */
869 .fsync = smfs_fsync, /* BKL held */
871 .release = smfs_release,
874 struct file_operations smfs_iopen_fops = {
875 .read = smfs_read_dir,