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>
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 *cache_dir = NULL;
54 struct dentry *cache_dentry = NULL;
55 struct dentry *cache_parent = NULL;
57 struct hook_msg msg = {
64 cache_dir = I2CI(dir);
66 LASSERT(cache_dir->i_op->create);
69 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
70 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
71 if (!cache_dentry || !cache_parent) {
76 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
82 SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
84 pre_smfs_inode(dir, cache_dir);
86 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
87 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
89 rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
92 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino, dir, 0);
94 d_instantiate(dentry, inode);
99 SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc);
101 post_smfs_inode(dir, cache_dir);
102 smfs_trans_commit(dir, handle, 0);
106 post_smfs_dentry(cache_dentry);
107 post_smfs_dentry(cache_parent);
111 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
112 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
114 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
115 struct nameidata *nd)
118 struct inode *cache_dir;
119 struct inode *inode = NULL;
120 struct inode * cache_inode = NULL;
121 struct dentry *cache_dentry = NULL;
122 struct dentry *cache_parent = NULL;
123 struct dentry *rdentry = NULL;
125 struct hook_msg msg = {
131 cache_dir = I2CI(dir);
133 RETURN(ERR_PTR(-ENOENT));
135 LASSERT(cache_dir->i_op->lookup);
137 /* preparing artificial backing fs dentries. */
138 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
139 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
140 if (!cache_dentry || !cache_parent)
141 RETURN(ERR_PTR(-ENOMEM));
143 SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
145 /* perform lookup in backing fs. */
146 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
147 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry);
149 rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
153 rc = PTR_ERR(rdentry);
155 cache_inode = rdentry->d_inode;
159 cache_inode = cache_dentry->d_inode;
163 inode = smfs_get_inode(dir->i_sb, cache_inode->i_ino, dir,0);
169 d_add(dentry, inode);
171 SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
173 post_smfs_dentry(cache_dentry);
174 post_smfs_dentry(cache_parent);
178 static int smfs_link(struct dentry *old_dentry,
179 struct inode *dir, struct dentry *dentry)
181 struct inode *cache_old_inode = NULL;
182 struct inode *cache_dir = NULL;
183 struct inode *old_inode = NULL;
184 struct dentry *cache_dentry = NULL;
185 struct dentry *cache_old_dentry = NULL;
186 struct dentry *cache_parent = NULL;
189 struct hook_msg msg = {
190 .dentry = old_dentry,
195 cache_dir = I2CI(dir);
199 old_inode = old_dentry->d_inode;
200 cache_old_inode = I2CI(old_inode);
201 if (!cache_old_inode)
204 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
205 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
206 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
207 if (!cache_old_dentry || !cache_dentry || !cache_parent) {
212 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
213 if (IS_ERR(handle)) {
218 pre_smfs_inode(dir, cache_dir);
219 pre_smfs_inode(old_inode, cache_old_inode);
222 SMFS_PRE_HOOK(dir, HOOK_LINK, &msg);
224 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
226 atomic_inc(&old_inode->i_count);
227 d_instantiate(dentry, old_inode);
230 SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc);
232 post_smfs_inode(old_inode, cache_old_inode);
233 post_smfs_inode(dir, cache_dir);
235 smfs_trans_commit(dir, handle, 0);
239 post_smfs_dentry(cache_dentry);
240 post_smfs_dentry(cache_parent);
241 post_smfs_dentry(cache_old_dentry);
246 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
248 struct inode *cache_dir = I2CI(dir);
249 struct inode *cache_inode = I2CI(dentry->d_inode);
250 struct dentry *cache_dentry;
251 struct dentry *cache_parent;
255 struct hook_unlink_msg msg = {
262 if (!cache_dir || !cache_inode || !cache_dir->i_op->unlink)
265 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
266 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
267 if (!cache_dentry || !cache_parent) {
273 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
274 if (IS_ERR(handle)) {
279 pre_smfs_inode(dir, cache_dir);
280 pre_smfs_inode(dentry->d_inode, cache_inode);
282 SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg);
284 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
286 SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc);
288 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
289 post_smfs_inode(dir, cache_dir);
292 smfs_trans_commit(dir, handle, 0);
294 post_smfs_dentry(cache_dentry);
295 post_smfs_dentry(cache_parent);
299 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
302 struct inode *cache_dir = I2CI(dir);
303 struct inode *inode = NULL;
304 struct dentry *cache_dentry;
305 struct dentry *cache_parent;
308 struct hook_symlink_msg msg = {
310 .tgt_len = strlen(symname) + 1,
311 .symname = (char*)symname
316 if (!cache_dir || !cache_dir->i_op->symlink)
319 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
320 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
321 if (!cache_parent || !cache_dentry) {
326 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
327 if (IS_ERR(handle)) {
333 pre_smfs_inode(dir, cache_dir);
335 SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg);
337 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
339 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
342 d_instantiate(dentry, inode);
347 SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
349 post_smfs_inode(dir, cache_dir);
350 smfs_trans_commit(dir, handle, 0);
354 post_smfs_dentry(cache_dentry);
355 post_smfs_dentry(cache_parent);
359 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
362 struct inode *cache_dir = I2CI(dir);
363 struct inode *inode = NULL;
364 struct dentry *cache_dentry;
365 struct dentry *cache_parent;
368 struct hook_msg msg = {
377 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
378 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
380 if (!cache_parent || !cache_dentry) {
385 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
386 if (IS_ERR(handle)) {
391 pre_smfs_inode(dir, cache_dir);
392 SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg);
394 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
396 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
399 d_instantiate(dentry, inode);
404 SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc);
405 post_smfs_inode(dir, cache_dir);
406 smfs_trans_commit(dir, handle, 0);
409 post_smfs_dentry(cache_dentry);
410 post_smfs_dentry(cache_parent);
414 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
416 struct inode *cache_dir = I2CI(dir);
417 struct inode *cache_inode = I2CI(dentry->d_inode);
418 struct dentry *cache_dentry = NULL;
419 struct dentry *cache_parent = NULL;
422 struct hook_unlink_msg msg = {
429 if (!cache_dir || !cache_dir->i_op->rmdir)
432 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
433 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
434 if (!cache_parent || !cache_dentry) {
439 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
440 if (IS_ERR(handle) ) {
445 pre_smfs_inode(dir, cache_dir);
446 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
448 SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg);
450 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
452 SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc);
454 post_smfs_inode(dir, cache_dir);
455 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
457 smfs_trans_commit(dir, handle, 0);
460 post_smfs_dentry(cache_dentry);
461 post_smfs_dentry(cache_parent);
465 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
466 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
469 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
470 int mode, dev_t rdev)
473 struct inode *cache_dir = I2CI(dir);
474 struct inode *inode = NULL;
475 struct dentry *cache_dentry = NULL;
476 struct dentry *cache_parent = NULL;
479 struct hook_msg msg = {
485 if (!cache_dir || !cache_dir->i_op->mknod)
488 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
489 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
490 if (!cache_parent || !cache_dentry) {
495 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
496 if (IS_ERR(handle)) {
501 pre_smfs_inode(dir, cache_dir);
502 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
504 SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg);
506 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
508 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
511 d_instantiate(dentry, inode);
516 SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc);
518 post_smfs_inode(dir, cache_dir);
519 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
520 smfs_trans_commit(dir, handle, 0);
523 post_smfs_dentry(cache_dentry);
524 post_smfs_dentry(cache_parent);
528 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
529 struct inode *new_dir,struct dentry *new_dentry)
531 struct inode *cache_old_dir = I2CI(old_dir);
532 struct inode *cache_new_dir = I2CI(new_dir);
533 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
534 struct inode *cache_new_inode = NULL;
535 struct dentry *cache_old_dentry = NULL;
536 struct dentry *cache_new_dentry = NULL;
537 struct dentry *cache_new_parent = NULL;
538 struct dentry *cache_old_parent = NULL;
541 struct hook_rename_msg msg = {
542 .dentry = old_dentry,
544 .new_dentry = new_dentry
549 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
552 if (new_dentry->d_inode) {
553 cache_new_inode = I2CI(new_dentry->d_inode);
554 if (!cache_new_inode)
558 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
559 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
561 if (!cache_old_parent || !cache_old_dentry) {
566 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
567 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
569 if (!cache_new_parent || !cache_new_dentry) {
574 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
575 if (IS_ERR(handle)) {
580 pre_smfs_inode(old_dir, cache_old_dir);
581 pre_smfs_inode(new_dir, cache_new_dir);
582 if (new_dentry->d_inode)
583 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
585 SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg);
587 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
588 cache_new_dir, cache_new_dentry);
590 SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc);
592 post_smfs_inode(old_dir, cache_old_dir);
593 post_smfs_inode(new_dir, cache_new_dir);
594 if (new_dentry->d_inode)
595 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
597 smfs_trans_commit(old_dir, handle, 0);
600 post_smfs_dentry(cache_old_dentry);
601 post_smfs_dentry(cache_old_parent);
602 post_smfs_dentry(cache_new_dentry);
603 post_smfs_dentry(cache_new_parent);
607 struct inode_operations smfs_dir_iops = {
610 link: smfs_link, /* BKL held */
611 unlink: smfs_unlink, /* BKL held */
612 symlink: smfs_symlink, /* BKL held */
613 mkdir: smfs_mkdir, /* BKL held */
614 rmdir: smfs_rmdir, /* BKL held */
615 mknod: smfs_mknod, /* BKL held */
616 rename: smfs_rename, /* BKL held */
617 setxattr: smfs_setxattr, /* BKL held */
618 getxattr: smfs_getxattr, /* BKL held */
619 listxattr: smfs_listxattr, /* BKL held */
620 removexattr: smfs_removexattr, /* BKL held */
623 static ssize_t smfs_read_dir(struct file *filp, char *buf,
624 size_t size, loff_t *ppos)
626 struct dentry *dentry = filp->f_dentry;
627 struct inode *cache_inode = NULL;
628 struct smfs_file_info *sfi = NULL;
630 loff_t *cache_ppos = NULL;
635 cache_inode = I2CI(dentry->d_inode);
637 if (!cache_inode || !cache_inode->i_fop->read)
641 if (sfi->magic != SMFS_FILE_MAGIC)
644 if (ppos != &(filp->f_pos))
645 cache_ppos = &tmp_ppos;
647 cache_ppos = &sfi->c_file->f_pos;
651 rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
657 duplicate_file(filp, sfi->c_file);
662 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
664 struct dentry *dentry = filp->f_dentry;
665 struct inode *cache_inode = NULL;
666 struct smfs_file_info *sfi = NULL;
668 struct hook_readdir_msg msg = {
677 cache_inode = I2CI(dentry->d_inode);
678 if (!cache_inode || !cache_inode->i_fop->readdir)
682 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
684 SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg);
686 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
688 SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
689 duplicate_file(filp, sfi->c_file);
698 struct file_operations smfs_dir_fops = {
699 .read = smfs_read_dir,
700 .readdir = smfs_readdir, /* BKL held */
701 .ioctl = smfs_ioctl, /* BKL held */
702 .fsync = smfs_fsync, /* BKL held */
704 .release = smfs_release,