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 = iget(dir->i_sb, cache_dentry->d_inode->i_ino);
93 GOTO(exit, rc = -ENOMEM);
95 d_instantiate(dentry, inode);
96 sm_set_inode_ops(cache_dentry->d_inode, inode);
97 post_smfs_inode(dir, cache_dir);
99 /* Do KML post hook */
100 SMFS_KML_POST(dir, dentry, NULL, NULL, REINT_CREATE, "create", rc,
102 SMFS_CACHE_HOOK_POST(CACHE_HOOK_CREATE, handle, dir, dentry,
103 NULL, NULL, rc, exit);
106 post_smfs_dentry(cache_dentry);
107 smfs_trans_commit(dir, handle, 0);
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 *cache_inode;
120 struct inode *inode = NULL;
121 struct dentry *cache_dentry = NULL;
122 struct dentry *cache_parent = NULL;
123 struct dentry *rc = NULL;
129 if (!(cache_dir = I2CI(dir)))
130 RETURN(ERR_PTR(-ENOENT));
132 handle = smfs_trans_start(dir, KML_CACHE_NOOP, NULL);
134 RETURN(ERR_PTR(-ENOSPC));
136 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir);
138 /* preparing artificial backing fs dentries. */
139 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
140 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
142 if (!cache_dentry || !cache_parent)
143 GOTO(exit, rc = ERR_PTR(-ENOMEM));
145 if (!cache_dir && cache_dir->i_op->lookup)
146 GOTO(exit, rc = ERR_PTR(-ENOENT));
148 /* perform lookup in backing fs. */
149 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
150 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
152 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
155 if (rc && IS_ERR(rc))
158 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
159 if (IS_ERR(cache_inode)) {
160 dentry->d_inode = cache_inode;
161 GOTO(exit, rc = NULL);
163 inode = iget(dir->i_sb, cache_inode->i_ino);
169 d_add(dentry, inode);
172 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LOOKUP, handle, dir, dentry,
173 NULL, NULL, rc2, exit);
177 post_smfs_dentry(cache_dentry);
178 smfs_trans_commit(dir, handle, 0);
182 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) && !defined(CONFIG_RH_2_4_20))
183 static int smfs_lookup_raw(struct inode *dir, const char *name,
184 int len, ino_t *data)
186 struct inode *cache_dir;
189 cache_dir = I2CI(dir);
193 if (cache_dir->i_op->lookup_raw)
194 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
199 static int smfs_link(struct dentry *old_dentry, struct inode *dir,
200 struct dentry *dentry)
202 struct inode *cache_old_inode = NULL;
203 struct inode *cache_dir = I2CI(dir);
204 struct inode *inode = NULL;
205 struct dentry *cache_dentry = NULL;
206 struct dentry *cache_old_dentry = NULL;
207 struct dentry *cache_parent = NULL;
208 struct dentry *cache_old_parent = NULL;
212 inode = old_dentry->d_inode;
214 cache_old_inode = I2CI(inode);
216 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
220 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir);
222 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
223 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
226 if (!cache_parent || !cache_dentry)
227 GOTO(exit, rc = -ENOMEM);
229 cache_old_parent = pre_smfs_dentry(NULL, cache_dir, old_dentry);
230 cache_old_dentry = pre_smfs_dentry(cache_parent, cache_old_inode,
233 if (!cache_old_parent || !cache_old_dentry)
234 GOTO(exit, rc = -ENOMEM);
236 pre_smfs_inode(dir, cache_dir);
237 pre_smfs_inode(inode, cache_old_dentry->d_inode);
239 if (cache_dir->i_op->link)
240 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
245 atomic_inc(&inode->i_count);
246 post_smfs_inode(inode, cache_old_dentry->d_inode);
247 d_instantiate(dentry, inode);
248 post_smfs_inode(dir, cache_dir);
250 SMFS_KML_POST(dir, old_dentry, dentry, NULL,
251 REINT_LINK, "link", rc, exit);
253 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LINK, handle,
254 dir, old_dentry, NULL, NULL, rc, exit);
257 post_smfs_dentry(cache_dentry);
258 post_smfs_dentry(cache_old_dentry);
259 smfs_trans_commit(dir, handle, 0);
263 static int smfs_unlink(struct inode *dir, struct dentry *dentry)
265 struct inode *cache_dir = I2CI(dir);
266 struct inode *cache_inode = I2CI(dentry->d_inode);
267 struct dentry *cache_dentry;
268 struct dentry *cache_parent;
272 if (!cache_dir || !cache_inode)
275 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
279 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir);
281 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
282 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
284 if (!cache_parent || !cache_dentry)
285 GOTO(exit, rc = -ENOMEM);
287 if (smfs_do_rec(dir) && dentry->d_inode->i_nlink == 1) {
288 /*For unlink, we do not really unlink this
289 *inode and just remove it to a tmp dir for
290 *undo later, the inode will be deleted when
291 *the corresponding rec will be cancelled*/
293 SMFS_KML_POST(dir, dentry, &flag, NULL, REINT_UNLINK,
297 pre_smfs_inode(dir, cache_dir);
298 pre_smfs_inode(dentry->d_inode, cache_inode);
299 if (cache_dir->i_op->unlink)
300 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
301 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
302 post_smfs_inode(dir, cache_dir);
303 post_smfs_dentry(cache_dentry);
306 SMFS_KML_POST(dir, dentry, &flag, NULL, REINT_UNLINK,
307 "dec_link", rc, exit);
310 SMFS_CACHE_HOOK_POST(CACHE_HOOK_UNLINK, handle, dir, dentry,
311 NULL, NULL, rc, exit);
313 smfs_trans_commit(dir, handle, 0);
317 static int smfs_symlink (struct inode *dir, struct dentry *dentry,
320 struct inode *cache_dir = I2CI(dir);
321 struct inode *inode = NULL;
322 struct dentry *cache_dentry;
323 struct dentry *cache_parent;
330 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
331 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
333 if (!cache_parent || !cache_dentry)
334 GOTO(exit, rc = -ENOMEM);
336 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
340 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir);
342 pre_smfs_inode(dir, cache_dir);
344 if (cache_dir->i_op->symlink)
345 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
347 inode = iget(dir->i_sb, cache_dentry->d_inode->i_ino);
349 post_smfs_inode(dir, cache_dir);
351 d_instantiate(dentry, inode);
355 SMFS_KML_POST(dir, dentry, (char*)symname, NULL, REINT_CREATE,
356 "symlink", rc, exit);
358 SMFS_CACHE_HOOK_POST(CACHE_HOOK_SYMLINK, handle, dir, dentry,
359 NULL, NULL, rc, exit);
362 post_smfs_dentry(cache_dentry);
363 smfs_trans_commit(dir, handle, 0);
367 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
369 struct inode *cache_dir = I2CI(dir);
370 struct inode *inode = NULL;
371 struct dentry *cache_dentry;
372 struct dentry *cache_parent;
379 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
383 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir);
385 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
386 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
389 if (!cache_parent || !cache_dentry)
390 GOTO(exit, rc = -ENOMEM);
392 pre_smfs_inode(dir, cache_dir);
394 if (cache_dir->i_op->mkdir)
395 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
397 inode = iget(dir->i_sb, cache_dentry->d_inode->i_ino);
400 GOTO(exit, rc = -ENOENT);
402 d_instantiate(dentry, inode);
403 post_smfs_inode(dir, cache_dir);
405 SMFS_KML_POST(dir, dentry, NULL, NULL, REINT_CREATE, "mkdir", rc, exit);
407 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKDIR, handle,
408 dir, dentry, NULL, NULL, rc, exit);
411 post_smfs_dentry(cache_dentry);
412 smfs_trans_commit(dir, handle, 0);
416 static int smfs_rmdir(struct inode * dir, struct dentry *dentry)
418 struct inode *cache_dir = I2CI(dir);
419 struct inode *cache_inode = I2CI(dentry->d_inode);
420 struct dentry *cache_dentry = NULL;
421 struct dentry *cache_parent = NULL;
428 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
429 if (IS_ERR(handle)) {
430 CERROR("smfs_do_mkdir: no space for transaction\n");
434 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir);
436 if (smfs_do_rec(dir)) {
437 /*For rmdir, we do not really unlink this
438 *inode and just remove it to a tmp dir for
439 *undo later, the inode will be deleted when
440 *the corresponding rec will be cancelled*/
441 if (dentry->d_inode->i_nlink == 2) {
443 SMFS_KML_POST(dir, dentry, &flag, NULL,
444 REINT_UNLINK, "rmdir", rc, exit);
447 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
448 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode,
452 if (!cache_parent || !cache_dentry)
453 GOTO(exit, rc = -ENOMEM);
455 pre_smfs_inode(dir, cache_dir);
456 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
457 if (cache_dir->i_op->rmdir)
458 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
460 post_smfs_inode(dir, cache_dir);
461 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
464 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RMDIR, handle, dir, dentry,
465 NULL, NULL, rc, exit);
467 post_smfs_dentry(cache_dentry);
468 smfs_trans_commit(dir, handle, 0);
472 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
473 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
476 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
477 int mode, dev_t rdev)
480 struct inode *cache_dir = I2CI(dir);
481 struct inode *inode = NULL;
482 struct dentry *cache_dentry = NULL;
483 struct dentry *cache_parent = NULL;
490 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
491 if (IS_ERR(handle)) {
492 CERROR("smfs_do_mkdir: no space for transaction\n");
495 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir);
497 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
498 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
500 if (!cache_parent || !cache_dentry)
501 GOTO(exit, rc = -ENOMEM);
503 pre_smfs_inode(dir, cache_dir);
504 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
506 if (!cache_dir->i_op->mknod)
509 rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
513 inode = iget(dir->i_sb, cache_dentry->d_inode->i_ino);
514 d_instantiate(dentry, inode);
516 post_smfs_inode(dir, cache_dir);
517 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
519 SMFS_KML_POST(dir, dentry, NULL, NULL,
520 REINT_CREATE, "mknod", rc, exit);
522 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKNOD, handle, dir,
523 dentry, NULL, NULL, rc, exit);
526 post_smfs_dentry(cache_dentry);
527 smfs_trans_commit(dir, handle, 0);
531 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
532 struct inode * new_dir,struct dentry *new_dentry)
534 struct inode *cache_old_dir = I2CI(old_dir);
535 struct inode *cache_new_dir = I2CI(new_dir);
536 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
538 struct inode *cache_new_inode = new_dentry->d_inode ?
539 I2CI(new_dentry->d_inode) : NULL;
541 struct dentry *cache_old_dentry = NULL;
542 struct dentry *cache_new_dentry = NULL;
543 struct dentry *cache_new_parent = NULL;
544 struct dentry *cache_old_parent = NULL;
548 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
551 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
552 if (IS_ERR(handle)) {
553 CERROR("smfs_do_mkdir: no space for transaction\n");
558 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir);
560 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
561 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
563 if (!cache_old_parent || !cache_old_dentry)
564 GOTO(exit, rc = -ENOMEM);
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)
570 GOTO(exit, rc = -ENOMEM);
572 pre_smfs_inode(old_dir, cache_old_dir);
573 pre_smfs_inode(new_dir, cache_new_dir);
575 if (cache_old_dir->i_op->rename)
576 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
577 cache_new_dir, cache_new_dentry);
579 post_smfs_inode(old_dir, cache_old_dir);
580 post_smfs_inode(new_dir, cache_new_dir);
582 SMFS_KML_POST(old_dir, old_dentry, new_dir,
583 new_dentry, REINT_RENAME, "rename", rc, exit);
584 if (new_dentry->d_inode)
585 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
587 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RENAME, handle, old_dir, old_dentry,
588 new_dir, new_dentry, rc, exit);
591 post_smfs_dentry(cache_old_dentry);
592 post_smfs_dentry(cache_new_dentry);
593 smfs_trans_commit(old_dir, handle, 0);
597 struct inode_operations smfs_dir_iops = {
600 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
601 lookup_raw: smfs_lookup_raw, /* BKL held */
603 link: smfs_link, /* BKL held */
604 unlink: smfs_unlink, /* BKL held */
605 symlink: smfs_symlink, /* BKL held */
606 mkdir: smfs_mkdir, /* BKL held */
607 rmdir: smfs_rmdir, /* BKL held */
608 mknod: smfs_mknod, /* BKL held */
609 rename: smfs_rename, /* BKL held */
610 setxattr: smfs_setxattr, /* BKL held */
611 getxattr: smfs_getxattr, /* BKL held */
612 listxattr: smfs_listxattr, /* BKL held */
613 removexattr: smfs_removexattr, /* BKL held */
616 static ssize_t smfs_read_dir(struct file *filp, char *buf,
617 size_t size, loff_t *ppos)
619 struct dentry *dentry = filp->f_dentry;
620 struct inode *cache_inode = NULL;
621 struct smfs_file_info *sfi = NULL;
626 cache_inode = I2CI(dentry->d_inode);
632 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
634 if (ppos != &(filp->f_pos))
635 cache_ppos = &tmp_ppos;
637 cache_ppos = &sfi->c_file->f_pos;
640 if (cache_inode->i_fop->read)
641 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
645 duplicate_file(filp, sfi->c_file);
649 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
651 struct dentry *dentry = filp->f_dentry;
652 struct inode *cache_inode = NULL;
653 struct smfs_file_info *sfi = NULL;
656 cache_inode = I2CI(dentry->d_inode);
661 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
663 if (cache_inode->i_fop->readdir)
664 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
666 duplicate_file(filp, sfi->c_file);
670 struct file_operations smfs_dir_fops = {
672 readdir: smfs_readdir, /* BKL held */
673 ioctl: smfs_ioctl, /* BKL held */
674 fsync: smfs_fsync, /* BKL held */
676 release: smfs_release,