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>
39 #include "smfs_internal.h"
41 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
43 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
44 static int smfs_create(struct inode *dir, struct dentry *dentry,
47 static int smfs_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
51 struct inode *inode = NULL;
52 struct inode *cache_dir = NULL;
53 struct dentry *cache_dentry = NULL;
54 struct dentry *cache_parent = NULL;
60 cache_dir = I2CI(dir);
64 handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
68 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_CREATE, handle, dir);
70 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
71 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
74 if (!cache_dentry || !cache_parent)
75 GOTO(exit, rc = -ENOMEM);
77 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 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
92 &I2SMI(dir)->smi_flags);
94 GOTO(exit, rc = -ENOMEM);
96 d_instantiate(dentry, inode);
97 sm_set_inode_ops(cache_dentry->d_inode, inode);
98 post_smfs_inode(dir, cache_dir);
101 SMFS_KML_POST(dir, dentry, NULL, NULL, REINT_CREATE,
103 SMFS_CACHE_HOOK_POST(CACHE_HOOK_CREATE, handle, dir, dentry,
104 NULL, NULL, rc, exit);
107 post_smfs_dentry(cache_dentry);
108 post_smfs_dentry(cache_parent);
109 smfs_trans_commit(dir, handle, 0);
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
114 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
116 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
117 struct nameidata *nd)
120 struct inode *cache_dir;
121 struct inode *cache_inode;
122 struct inode *inode = NULL;
123 struct dentry *cache_dentry = NULL;
124 struct dentry *cache_parent = NULL;
125 struct dentry *rc = NULL;
131 if (!(cache_dir = I2CI(dir)))
132 RETURN(ERR_PTR(-ENOENT));
134 handle = smfs_trans_start(dir, KML_CACHE_NOOP, NULL);
136 RETURN(ERR_PTR(-ENOSPC));
138 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir);
140 /* preparing artificial backing fs dentries. */
141 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
142 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
144 if (!cache_dentry || !cache_parent)
145 GOTO(exit, rc = ERR_PTR(-ENOMEM));
147 if (!cache_dir && cache_dir->i_op->lookup)
148 GOTO(exit, rc = ERR_PTR(-ENOENT));
150 /* perform lookup in backing fs. */
151 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
152 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
154 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
157 if (rc && IS_ERR(rc))
160 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
161 if (IS_ERR(cache_inode)) {
162 dentry->d_inode = cache_inode;
163 GOTO(exit, rc = NULL);
165 inode = iget4(dir->i_sb, cache_inode->i_ino, NULL,
166 &I2SMI(dir)->smi_flags);
172 d_add(dentry, inode);
175 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LOOKUP, handle, dir, dentry,
176 NULL, NULL, rc2, exit);
180 post_smfs_dentry(cache_dentry);
181 post_smfs_dentry(cache_parent);
182 smfs_trans_commit(dir, handle, 0);
186 static int smfs_link(struct dentry * old_dentry,
187 struct inode * dir, struct dentry *dentry)
189 struct inode *cache_old_inode = NULL;
190 struct inode *cache_dir = I2CI(dir);
191 struct inode *inode = NULL;
192 struct dentry *cache_dentry = NULL;
193 struct dentry *cache_old_dentry = NULL;
194 struct dentry *cache_parent = NULL;
198 inode = old_dentry->d_inode;
200 cache_old_inode = I2CI(inode);
202 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
206 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir);
208 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
209 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
212 if (!cache_parent || !cache_dentry)
213 GOTO(exit, rc = -ENOMEM);
215 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
217 if (!cache_old_dentry)
218 GOTO(exit, rc = -ENOMEM);
220 pre_smfs_inode(dir, cache_dir);
221 pre_smfs_inode(inode, cache_old_dentry->d_inode);
223 if (cache_dir->i_op->link)
224 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
229 atomic_inc(&inode->i_count);
230 post_smfs_inode(inode, cache_old_dentry->d_inode);
231 d_instantiate(dentry, inode);
232 post_smfs_inode(dir, cache_dir);
234 SMFS_KML_POST(dir, old_dentry, dentry, NULL,
235 REINT_LINK, "link", rc, exit);
237 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LINK, handle,
238 dir, old_dentry, NULL, NULL, rc, exit);
241 post_smfs_dentry(cache_dentry);
242 post_smfs_dentry(cache_parent);
243 post_smfs_dentry(cache_old_dentry);
244 smfs_trans_commit(dir, handle, 0);
248 static int smfs_unlink(struct inode * dir,
249 struct dentry *dentry)
251 struct inode *cache_dir = I2CI(dir);
252 struct inode *cache_inode = I2CI(dentry->d_inode);
253 struct dentry *cache_dentry;
254 struct dentry *cache_parent;
259 if (!cache_dir || !cache_inode)
262 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
266 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir);
268 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
269 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
271 if (!cache_parent || !cache_dentry)
272 GOTO(exit, rc = -ENOMEM);
275 pre_smfs_inode(dir, cache_dir);
276 pre_smfs_inode(dentry->d_inode, cache_inode);
277 if (cache_dir->i_op->unlink)
278 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
279 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
280 post_smfs_inode(dir, cache_dir);
282 SMFS_KML_POST(dir, dentry, &mode, NULL, REINT_UNLINK,
285 SMFS_CACHE_HOOK_POST(CACHE_HOOK_UNLINK, handle, dir, dentry,
286 NULL, NULL, rc, exit);
288 post_smfs_dentry(cache_dentry);
289 post_smfs_dentry(cache_parent);
290 smfs_trans_commit(dir, handle, 0);
294 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
297 struct inode *cache_dir = I2CI(dir);
298 struct inode *inode = NULL;
299 struct dentry *cache_dentry;
300 struct dentry *cache_parent;
307 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
308 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
310 if (!cache_parent || !cache_dentry)
311 GOTO(exit, rc = -ENOMEM);
313 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
317 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir);
319 pre_smfs_inode(dir, cache_dir);
321 if (cache_dir->i_op->symlink)
322 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
324 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
325 &I2SMI(dir)->smi_flags);
326 post_smfs_inode(dir, cache_dir);
328 d_instantiate(dentry, inode);
332 tgt_len = strlen(symname) + 1;
333 SMFS_KML_POST(dir, dentry, (char*)symname, &tgt_len, REINT_CREATE,
334 "symlink", rc, exit);
336 SMFS_CACHE_HOOK_POST(CACHE_HOOK_SYMLINK, handle, dir, dentry,
337 NULL, NULL, rc, exit);
340 post_smfs_dentry(cache_dentry);
341 post_smfs_dentry(cache_parent);
342 smfs_trans_commit(dir, handle, 0);
346 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
349 struct inode *cache_dir = I2CI(dir);
350 struct inode *inode = NULL;
351 struct dentry *cache_dentry;
352 struct dentry *cache_parent;
359 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
363 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir);
365 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
366 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
369 if (!cache_parent || !cache_dentry)
370 GOTO(exit, rc = -ENOMEM);
372 pre_smfs_inode(dir, cache_dir);
374 if (cache_dir->i_op->mkdir)
375 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
380 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
381 &I2SMI(dir)->smi_flags);
383 GOTO(exit, rc = -ENOENT);
385 d_instantiate(dentry, inode);
386 post_smfs_inode(dir, cache_dir);
388 SMFS_KML_POST(dir, dentry, NULL, NULL,
389 REINT_CREATE, "mkdir", rc, exit);
391 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKDIR, handle,
392 dir, dentry, NULL, NULL, rc, exit);
395 post_smfs_dentry(cache_dentry);
396 post_smfs_dentry(cache_parent);
397 smfs_trans_commit(dir, handle, 0);
401 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
403 struct inode *cache_dir = I2CI(dir);
404 struct inode *cache_inode = I2CI(dentry->d_inode);
405 struct dentry *cache_dentry = NULL;
406 struct dentry *cache_parent = NULL;
408 int rc = 0, mode = S_IFDIR;
413 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
414 if (IS_ERR(handle) ) {
415 CERROR("smfs_do_mkdir: no space for transaction\n");
419 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir);
421 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
422 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
425 if (!cache_parent || !cache_dentry)
426 GOTO(exit, rc = -ENOMEM);
428 pre_smfs_inode(dir, cache_dir);
429 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
430 if (cache_dir->i_op->rmdir)
431 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
433 post_smfs_inode(dir, cache_dir);
434 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
437 SMFS_KML_POST(dir, dentry, &mode, NULL,
438 REINT_UNLINK, "rmdir", rc, exit);
440 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RMDIR, handle, dir, dentry,
441 NULL, NULL, rc, exit);
443 post_smfs_dentry(cache_dentry);
444 post_smfs_dentry(cache_parent);
445 smfs_trans_commit(dir, handle, 0);
449 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
450 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
453 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
454 int mode, dev_t rdev)
457 struct inode *cache_dir = I2CI(dir);
458 struct inode *inode = NULL;
459 struct dentry *cache_dentry = NULL;
460 struct dentry *cache_parent = NULL;
467 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
468 if (IS_ERR(handle)) {
469 CERROR("smfs_do_mkdir: no space for transaction\n");
472 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir);
474 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
475 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
477 if (!cache_parent || !cache_dentry)
478 GOTO(exit, rc = -ENOMEM);
480 pre_smfs_inode(dir, cache_dir);
481 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
483 if (!cache_dir->i_op->mknod)
486 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
490 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
491 &I2SMI(dir)->smi_flags);
492 d_instantiate(dentry, inode);
494 pre_smfs_inode(dir, cache_dir);
495 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
497 SMFS_KML_POST(dir, dentry, NULL, NULL,
498 REINT_CREATE, "mknod", rc, exit);
500 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKNOD, handle, dir,
501 dentry, NULL, NULL, rc, exit);
504 post_smfs_dentry(cache_dentry);
505 post_smfs_dentry(cache_parent);
506 smfs_trans_commit(dir, handle, 0);
510 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
511 struct inode * new_dir,struct dentry *new_dentry)
513 struct inode *cache_old_dir = I2CI(old_dir);
514 struct inode *cache_new_dir = I2CI(new_dir);
515 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
517 struct inode *cache_new_inode = new_dentry->d_inode ?
518 I2CI(new_dentry->d_inode) : NULL;
520 struct dentry *cache_old_dentry = NULL;
521 struct dentry *cache_new_dentry = NULL;
522 struct dentry *cache_new_parent = NULL;
523 struct dentry *cache_old_parent = NULL;
527 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
530 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
531 if (IS_ERR(handle)) {
532 CERROR("smfs_do_mkdir: no space for transaction\n");
537 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir);
539 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
540 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
542 if (!cache_old_parent || !cache_old_dentry)
543 GOTO(exit, rc = -ENOMEM);
545 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
546 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
548 if (!cache_new_parent || !cache_new_dentry)
549 GOTO(exit, rc = -ENOMEM);
551 pre_smfs_inode(old_dir, cache_old_dir);
552 pre_smfs_inode(new_dir, cache_new_dir);
554 if (cache_old_dir->i_op->rename)
555 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
556 cache_new_dir, cache_new_dentry);
558 post_smfs_inode(old_dir, cache_old_dir);
559 post_smfs_inode(new_dir, cache_new_dir);
561 SMFS_KML_POST(old_dir, old_dentry, new_dir,
562 new_dentry, REINT_RENAME, "rename", rc, exit);
563 if (new_dentry->d_inode)
564 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
566 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RENAME, handle, old_dir, old_dentry,
567 new_dir, new_dentry, rc, exit);
570 post_smfs_dentry(cache_old_dentry);
571 post_smfs_dentry(cache_old_parent);
572 post_smfs_dentry(cache_new_dentry);
573 post_smfs_dentry(cache_new_parent);
574 smfs_trans_commit(old_dir, handle, 0);
578 struct inode_operations smfs_dir_iops = {
581 link: smfs_link, /* BKL held */
582 unlink: smfs_unlink, /* BKL held */
583 symlink: smfs_symlink, /* BKL held */
584 mkdir: smfs_mkdir, /* BKL held */
585 rmdir: smfs_rmdir, /* BKL held */
586 mknod: smfs_mknod, /* BKL held */
587 rename: smfs_rename, /* BKL held */
588 setxattr: smfs_setxattr, /* BKL held */
589 getxattr: smfs_getxattr, /* BKL held */
590 listxattr: smfs_listxattr, /* BKL held */
591 removexattr: smfs_removexattr, /* BKL held */
594 static ssize_t smfs_read_dir(struct file *filp, char *buf,
595 size_t size, loff_t *ppos)
597 struct dentry *dentry = filp->f_dentry;
598 struct inode *cache_inode = NULL;
599 struct smfs_file_info *sfi = NULL;
604 cache_inode = I2CI(dentry->d_inode);
610 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
612 if (ppos != &(filp->f_pos))
613 cache_ppos = &tmp_ppos;
615 cache_ppos = &sfi->c_file->f_pos;
618 if (cache_inode->i_fop->read)
619 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
623 duplicate_file(filp, sfi->c_file);
627 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
629 struct dentry *dentry = filp->f_dentry;
630 struct inode *cache_inode = NULL;
631 struct smfs_file_info *sfi = NULL;
634 cache_inode = I2CI(dentry->d_inode);
639 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
641 if (cache_inode->i_fop->readdir)
642 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
644 duplicate_file(filp, sfi->c_file);
648 struct file_operations smfs_dir_fops = {
650 readdir: smfs_readdir, /* BKL held */
651 ioctl: smfs_ioctl, /* BKL held */
652 fsync: smfs_fsync, /* BKL held */
654 release: smfs_release,