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;
61 cache_dir = I2CI(dir);
65 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
70 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, PRE_HOOK, rc,
72 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
73 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
75 if (!cache_dentry || !cache_parent)
76 GOTO(exit, rc = -ENOMEM);
78 pre_smfs_inode(dir, cache_dir);
79 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
80 if (cache_dir && cache_dir->i_op->create)
81 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
84 if (cache_dir && cache_dir->i_op->create)
85 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
91 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, 0, inode,
94 d_instantiate(dentry, inode);
95 post_smfs_inode(dir, cache_dir);
97 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, POST_HOOK, rc,
101 post_smfs_dentry(cache_dentry);
102 post_smfs_dentry(cache_parent);
103 smfs_trans_commit(dir, handle, 0);
107 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
108 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
110 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
111 struct nameidata *nd)
114 struct inode *cache_dir;
115 struct inode *cache_inode;
116 struct inode *inode = NULL;
117 struct dentry *cache_dentry = NULL;
118 struct dentry *cache_parent = NULL;
119 struct dentry *rc = NULL;
121 int rc2 = 0, index = 0;
125 if (!(cache_dir = I2CI(dir)))
126 RETURN(ERR_PTR(-ENOENT));
128 /* preparing artificial backing fs dentries. */
129 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent, NULL);
130 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, &index);
132 if (!cache_dentry || !cache_parent)
133 GOTO(exit, rc = ERR_PTR(-ENOMEM));
135 if (!cache_dir && cache_dir->i_op->lookup)
136 GOTO(exit, rc = ERR_PTR(-ENOENT));
138 SMFS_HOOK(dir, dentry, &index, NULL, HOOK_LOOKUP, handle,
139 PRE_HOOK, rc2, exit);
141 /* perform lookup in backing fs. */
142 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
143 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
145 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
147 if (rc && IS_ERR(rc))
150 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
151 if (IS_ERR(cache_inode)) {
152 dentry->d_inode = cache_inode;
153 GOTO(exit, rc = NULL);
155 SMFS_GET_INODE(dir->i_sb, cache_inode, dir, index, inode, rc2,
162 d_add(dentry, inode);
165 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, POST_HOOK, rc2,
170 post_smfs_dentry(cache_dentry);
171 post_smfs_dentry(cache_parent);
172 smfs_trans_commit(dir, handle, 0);
176 static int smfs_link(struct dentry * old_dentry,
177 struct inode * dir, struct dentry *dentry)
179 struct inode *cache_old_inode = NULL;
180 struct inode *cache_dir = I2CI(dir);
181 struct inode *inode = NULL;
182 struct dentry *cache_dentry = NULL;
183 struct dentry *cache_old_dentry = NULL;
184 struct dentry *cache_parent = NULL;
188 inode = old_dentry->d_inode;
190 cache_old_inode = I2CI(inode);
192 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
197 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle, PRE_HOOK, rc,
200 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
201 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
203 if (!cache_parent || !cache_dentry)
204 GOTO(exit, rc = -ENOMEM);
206 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
208 if (!cache_old_dentry)
209 GOTO(exit, rc = -ENOMEM);
211 pre_smfs_inode(dir, cache_dir);
212 pre_smfs_inode(inode, cache_old_dentry->d_inode);
214 if (cache_dir->i_op->link)
215 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
220 atomic_inc(&inode->i_count);
221 post_smfs_inode(inode, cache_old_dentry->d_inode);
222 d_instantiate(dentry, inode);
223 post_smfs_inode(dir, cache_dir);
225 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle, POST_HOOK,
229 post_smfs_dentry(cache_dentry);
230 post_smfs_dentry(cache_parent);
231 post_smfs_dentry(cache_old_dentry);
232 smfs_trans_commit(dir, handle, 0);
236 static int smfs_unlink(struct inode * dir,
237 struct dentry *dentry)
239 struct inode *cache_dir = I2CI(dir);
240 struct inode *cache_inode = I2CI(dentry->d_inode);
241 struct dentry *cache_dentry;
242 struct dentry *cache_parent;
247 if (!cache_dir || !cache_inode)
250 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
254 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
257 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
258 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry, NULL);
260 if (!cache_parent || !cache_dentry)
261 GOTO(exit, rc = -ENOMEM);
264 pre_smfs_inode(dir, cache_dir);
265 pre_smfs_inode(dentry->d_inode, cache_inode);
266 if (cache_dir->i_op->unlink)
267 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
268 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
269 post_smfs_inode(dir, cache_dir);
272 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
275 post_smfs_dentry(cache_dentry);
276 post_smfs_dentry(cache_parent);
277 smfs_trans_commit(dir, handle, 0);
281 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
284 struct inode *cache_dir = I2CI(dir);
285 struct inode *inode = NULL;
286 struct dentry *cache_dentry;
287 struct dentry *cache_parent;
294 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
299 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
302 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
303 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
305 if (!cache_parent || !cache_dentry)
306 GOTO(exit, rc = -ENOMEM);
308 pre_smfs_inode(dir, cache_dir);
309 if (cache_dir->i_op->symlink)
310 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
312 post_smfs_inode(dir, cache_dir);
314 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, 0, inode,
317 d_instantiate(dentry, inode);
321 tgt_len = strlen(symname) + 1;
323 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
324 POST_HOOK, rc, exit);
327 post_smfs_dentry(cache_dentry);
328 post_smfs_dentry(cache_parent);
329 smfs_trans_commit(dir, handle, 0);
333 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
336 struct inode *cache_dir = I2CI(dir);
337 struct inode *inode = NULL;
338 struct dentry *cache_dentry;
339 struct dentry *cache_parent;
346 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
352 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
355 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
356 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
358 if (!cache_parent || !cache_dentry)
359 GOTO(exit, rc = -ENOMEM);
361 pre_smfs_inode(dir, cache_dir);
363 if (cache_dir->i_op->mkdir)
364 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
369 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, 0, inode,
371 d_instantiate(dentry, inode);
372 post_smfs_inode(dir, cache_dir);
374 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, POST_HOOK, rc,
378 post_smfs_dentry(cache_dentry);
379 post_smfs_dentry(cache_parent);
380 smfs_trans_commit(dir, handle, 0);
384 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
386 struct inode *cache_dir = I2CI(dir);
387 struct inode *cache_inode = I2CI(dentry->d_inode);
388 struct dentry *cache_dentry = NULL;
389 struct dentry *cache_parent = NULL;
391 int rc = 0, mode = S_IFDIR;
396 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
397 if (IS_ERR(handle) ) {
398 CERROR("smfs_do_mkdir: no space for transaction\n");
404 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
407 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry, NULL);
408 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry, NULL);
410 if (!cache_parent || !cache_dentry)
411 GOTO(exit, rc = -ENOMEM);
413 pre_smfs_inode(dir, cache_dir);
414 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
415 if (cache_dir->i_op->rmdir)
416 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
418 post_smfs_inode(dir, cache_dir);
419 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
422 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
425 post_smfs_dentry(cache_dentry);
426 post_smfs_dentry(cache_parent);
427 smfs_trans_commit(dir, handle, 0);
431 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
432 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
435 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
436 int mode, dev_t rdev)
439 struct inode *cache_dir = I2CI(dir);
440 struct inode *inode = NULL;
441 struct dentry *cache_dentry = NULL;
442 struct dentry *cache_parent = NULL;
449 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
450 if (IS_ERR(handle)) {
451 CERROR("smfs_do_mkdir: no space for transaction\n");
456 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
459 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent, NULL);
460 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry, NULL);
461 if (!cache_parent || !cache_dentry)
462 GOTO(exit, rc = -ENOMEM);
464 pre_smfs_inode(dir, cache_dir);
465 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
467 if (!cache_dir->i_op->mknod)
470 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
474 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, 0, inode,
477 d_instantiate(dentry, inode);
479 pre_smfs_inode(dir, cache_dir);
480 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
482 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
487 post_smfs_dentry(cache_dentry);
488 post_smfs_dentry(cache_parent);
489 smfs_trans_commit(dir, handle, 0);
493 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
494 struct inode * new_dir,struct dentry *new_dentry)
496 struct inode *cache_old_dir = I2CI(old_dir);
497 struct inode *cache_new_dir = I2CI(new_dir);
498 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
500 struct inode *cache_new_inode = new_dentry->d_inode ?
501 I2CI(new_dentry->d_inode) : NULL;
503 struct dentry *cache_old_dentry = NULL;
504 struct dentry *cache_new_dentry = NULL;
505 struct dentry *cache_new_parent = NULL;
506 struct dentry *cache_old_parent = NULL;
510 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
513 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
514 if (IS_ERR(handle)) {
515 CERROR("smfs_do_mkdir: no space for transaction\n");
521 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
524 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry, NULL);
525 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
527 if (!cache_old_parent || !cache_old_dentry)
528 GOTO(exit, rc = -ENOMEM);
530 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry, NULL);
531 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
533 if (!cache_new_parent || !cache_new_dentry)
534 GOTO(exit, rc = -ENOMEM);
536 pre_smfs_inode(old_dir, cache_old_dir);
537 pre_smfs_inode(new_dir, cache_new_dir);
539 if (cache_old_dir->i_op->rename)
540 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
541 cache_new_dir, cache_new_dentry);
543 post_smfs_inode(old_dir, cache_old_dir);
544 post_smfs_inode(new_dir, cache_new_dir);
546 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
547 POST_HOOK, rc, exit);
549 if (new_dentry->d_inode)
550 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
553 post_smfs_dentry(cache_old_dentry);
554 post_smfs_dentry(cache_old_parent);
555 post_smfs_dentry(cache_new_dentry);
556 post_smfs_dentry(cache_new_parent);
557 smfs_trans_commit(old_dir, handle, 0);
561 struct inode_operations smfs_dir_iops = {
564 link: smfs_link, /* BKL held */
565 unlink: smfs_unlink, /* BKL held */
566 symlink: smfs_symlink, /* BKL held */
567 mkdir: smfs_mkdir, /* BKL held */
568 rmdir: smfs_rmdir, /* BKL held */
569 mknod: smfs_mknod, /* BKL held */
570 rename: smfs_rename, /* BKL held */
571 setxattr: smfs_setxattr, /* BKL held */
572 getxattr: smfs_getxattr, /* BKL held */
573 listxattr: smfs_listxattr, /* BKL held */
574 removexattr: smfs_removexattr, /* BKL held */
577 static ssize_t smfs_read_dir(struct file *filp, char *buf,
578 size_t size, loff_t *ppos)
580 struct dentry *dentry = filp->f_dentry;
581 struct inode *cache_inode = NULL;
582 struct smfs_file_info *sfi = NULL;
587 cache_inode = I2CI(dentry->d_inode);
593 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
595 if (ppos != &(filp->f_pos))
596 cache_ppos = &tmp_ppos;
598 cache_ppos = &sfi->c_file->f_pos;
601 if (cache_inode->i_fop->read)
602 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
605 duplicate_file(filp, sfi->c_file);
609 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
611 struct dentry *dentry = filp->f_dentry;
612 struct inode *cache_inode = NULL;
613 struct smfs_file_info *sfi = NULL;
616 cache_inode = I2CI(dentry->d_inode);
621 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
623 if (cache_inode->i_fop->readdir)
624 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
626 duplicate_file(filp, sfi->c_file);
630 struct file_operations smfs_dir_fops = {
632 readdir: smfs_readdir, /* BKL held */
633 ioctl: smfs_ioctl, /* BKL held */
634 fsync: smfs_fsync, /* BKL held */
636 release: smfs_release,