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, rc);
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 SMFS_PRE_COW(dir, dentry, REINT_CREATE, "create", rc, exit);
81 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
82 if (cache_dir && cache_dir->i_op->create)
83 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
86 if (cache_dir && cache_dir->i_op->create)
87 rc = cache_dir->i_op->create(cache_dir, cache_dentry,
93 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
94 &I2SMI(dir)->smi_flags);
96 GOTO(exit, rc = -ENOMEM);
98 d_instantiate(dentry, inode);
99 sm_set_inode_ops(cache_dentry->d_inode, inode);
100 post_smfs_inode(dir, cache_dir);
104 SMFS_KML_POST(dir, dentry, NULL, NULL, REINT_CREATE,
106 SMFS_CACHE_HOOK_POST(CACHE_HOOK_CREATE, handle, dir, dentry,
107 NULL, NULL, rc, exit);
110 post_smfs_dentry(cache_dentry);
111 post_smfs_dentry(cache_parent);
112 smfs_trans_commit(dir, handle, 0);
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
117 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
119 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
120 struct nameidata *nd)
123 struct inode *cache_dir;
124 struct inode *cache_inode;
125 struct inode *inode = NULL;
126 struct dentry *cache_dentry = NULL;
127 struct dentry *cache_parent = NULL;
128 struct dentry *rc = NULL;
134 if (!(cache_dir = I2CI(dir)))
135 RETURN(ERR_PTR(-ENOENT));
137 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LOOKUP, handle, dir, rc2);
140 RETURN(ERR_PTR(rc2));
142 /* preparing artificial backing fs dentries. */
143 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
144 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
146 if (!cache_dentry || !cache_parent)
147 GOTO(exit, rc = ERR_PTR(-ENOMEM));
149 if (!cache_dir && cache_dir->i_op->lookup)
150 GOTO(exit, rc = ERR_PTR(-ENOENT));
152 /* perform lookup in backing fs. */
153 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
154 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
156 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
159 if (rc && IS_ERR(rc))
162 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
163 if (IS_ERR(cache_inode)) {
164 dentry->d_inode = cache_inode;
165 GOTO(exit, rc = NULL);
167 inode = iget4(dir->i_sb, cache_inode->i_ino, NULL,
168 &I2SMI(dir)->smi_flags);
174 d_add(dentry, inode);
177 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LOOKUP, handle, dir, dentry,
178 NULL, NULL, rc2, exit);
182 post_smfs_dentry(cache_dentry);
183 post_smfs_dentry(cache_parent);
184 smfs_trans_commit(dir, handle, 0);
188 static int smfs_link(struct dentry * old_dentry,
189 struct inode * dir, struct dentry *dentry)
191 struct inode *cache_old_inode = NULL;
192 struct inode *cache_dir = I2CI(dir);
193 struct inode *inode = NULL;
194 struct dentry *cache_dentry = NULL;
195 struct dentry *cache_old_dentry = NULL;
196 struct dentry *cache_parent = NULL;
200 inode = old_dentry->d_inode;
202 cache_old_inode = I2CI(inode);
204 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
208 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_LINK, handle, dir, rc);
210 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
211 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
214 if (!cache_parent || !cache_dentry)
215 GOTO(exit, rc = -ENOMEM);
217 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode,
219 if (!cache_old_dentry)
220 GOTO(exit, rc = -ENOMEM);
222 pre_smfs_inode(dir, cache_dir);
223 pre_smfs_inode(inode, cache_old_dentry->d_inode);
225 if (cache_dir->i_op->link)
226 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
231 atomic_inc(&inode->i_count);
232 post_smfs_inode(inode, cache_old_dentry->d_inode);
233 d_instantiate(dentry, inode);
234 post_smfs_inode(dir, cache_dir);
236 SMFS_KML_POST(dir, old_dentry, dentry, NULL,
237 REINT_LINK, "link", rc, exit);
239 SMFS_CACHE_HOOK_POST(CACHE_HOOK_LINK, handle,
240 dir, old_dentry, NULL, NULL, rc, exit);
243 post_smfs_dentry(cache_dentry);
244 post_smfs_dentry(cache_parent);
245 post_smfs_dentry(cache_old_dentry);
246 smfs_trans_commit(dir, handle, 0);
250 static int smfs_unlink(struct inode * dir,
251 struct dentry *dentry)
253 struct inode *cache_dir = I2CI(dir);
254 struct inode *cache_inode = I2CI(dentry->d_inode);
255 struct dentry *cache_dentry;
256 struct dentry *cache_parent;
261 if (!cache_dir || !cache_inode)
264 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
268 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_UNLINK, handle, dir, rc);
270 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
271 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
273 if (!cache_parent || !cache_dentry)
274 GOTO(exit, rc = -ENOMEM);
277 pre_smfs_inode(dir, cache_dir);
278 pre_smfs_inode(dentry->d_inode, cache_inode);
279 if (cache_dir->i_op->unlink)
280 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
281 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
282 post_smfs_inode(dir, cache_dir);
284 SMFS_KML_POST(dir, dentry, &mode, NULL, REINT_UNLINK,
287 SMFS_CACHE_HOOK_POST(CACHE_HOOK_UNLINK, handle, dir, dentry,
288 NULL, NULL, rc, exit);
290 post_smfs_dentry(cache_dentry);
291 post_smfs_dentry(cache_parent);
292 smfs_trans_commit(dir, handle, 0);
296 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
299 struct inode *cache_dir = I2CI(dir);
300 struct inode *inode = NULL;
301 struct dentry *cache_dentry;
302 struct dentry *cache_parent;
309 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
310 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
312 if (!cache_parent || !cache_dentry)
313 GOTO(exit, rc = -ENOMEM);
315 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
319 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_SYMLINK, handle, dir, rc);
321 pre_smfs_inode(dir, cache_dir);
323 if (cache_dir->i_op->symlink)
324 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
326 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
327 &I2SMI(dir)->smi_flags);
328 post_smfs_inode(dir, cache_dir);
330 d_instantiate(dentry, inode);
334 tgt_len = strlen(symname) + 1;
335 SMFS_KML_POST(dir, dentry, (char*)symname, &tgt_len, REINT_CREATE,
336 "symlink", rc, exit);
338 SMFS_CACHE_HOOK_POST(CACHE_HOOK_SYMLINK, handle, dir, dentry,
339 NULL, NULL, rc, exit);
342 post_smfs_dentry(cache_dentry);
343 post_smfs_dentry(cache_parent);
344 smfs_trans_commit(dir, handle, 0);
348 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
351 struct inode *cache_dir = I2CI(dir);
352 struct inode *inode = NULL;
353 struct dentry *cache_dentry;
354 struct dentry *cache_parent;
361 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
365 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKDIR, handle, dir, rc);
367 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
368 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
371 if (!cache_parent || !cache_dentry)
372 GOTO(exit, rc = -ENOMEM);
374 pre_smfs_inode(dir, cache_dir);
376 if (cache_dir->i_op->mkdir)
377 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
382 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
383 &I2SMI(dir)->smi_flags);
385 GOTO(exit, rc = -ENOENT);
387 d_instantiate(dentry, inode);
388 post_smfs_inode(dir, cache_dir);
390 SMFS_KML_POST(dir, dentry, NULL, NULL,
391 REINT_CREATE, "mkdir", rc, exit);
393 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKDIR, handle,
394 dir, dentry, NULL, NULL, rc, exit);
397 post_smfs_dentry(cache_dentry);
398 post_smfs_dentry(cache_parent);
399 smfs_trans_commit(dir, handle, 0);
403 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
405 struct inode *cache_dir = I2CI(dir);
406 struct inode *cache_inode = I2CI(dentry->d_inode);
407 struct dentry *cache_dentry = NULL;
408 struct dentry *cache_parent = NULL;
410 int rc = 0, mode = S_IFDIR;
415 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
416 if (IS_ERR(handle) ) {
417 CERROR("smfs_do_mkdir: no space for transaction\n");
421 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RMDIR, handle, dir, rc);
423 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
424 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
427 if (!cache_parent || !cache_dentry)
428 GOTO(exit, rc = -ENOMEM);
430 pre_smfs_inode(dir, cache_dir);
431 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
432 if (cache_dir->i_op->rmdir)
433 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
435 post_smfs_inode(dir, cache_dir);
436 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
439 SMFS_KML_POST(dir, dentry, &mode, NULL,
440 REINT_UNLINK, "rmdir", rc, exit);
442 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RMDIR, handle, dir, dentry,
443 NULL, NULL, rc, exit);
445 post_smfs_dentry(cache_dentry);
446 post_smfs_dentry(cache_parent);
447 smfs_trans_commit(dir, handle, 0);
451 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
452 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
455 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
456 int mode, dev_t rdev)
459 struct inode *cache_dir = I2CI(dir);
460 struct inode *inode = NULL;
461 struct dentry *cache_dentry = NULL;
462 struct dentry *cache_parent = NULL;
469 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
470 if (IS_ERR(handle)) {
471 CERROR("smfs_do_mkdir: no space for transaction\n");
474 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_MKNOD, handle, dir, rc);
476 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
477 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
479 if (!cache_parent || !cache_dentry)
480 GOTO(exit, rc = -ENOMEM);
482 pre_smfs_inode(dir, cache_dir);
483 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
485 if (!cache_dir->i_op->mknod)
488 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
492 inode = iget4(dir->i_sb, cache_dentry->d_inode->i_ino, NULL,
493 &I2SMI(dir)->smi_flags);
494 d_instantiate(dentry, inode);
496 pre_smfs_inode(dir, cache_dir);
497 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
499 SMFS_KML_POST(dir, dentry, NULL, NULL,
500 REINT_CREATE, "mknod", rc, exit);
502 SMFS_CACHE_HOOK_POST(CACHE_HOOK_MKNOD, handle, dir,
503 dentry, NULL, NULL, rc, exit);
506 post_smfs_dentry(cache_dentry);
507 post_smfs_dentry(cache_parent);
508 smfs_trans_commit(dir, handle, 0);
512 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
513 struct inode * new_dir,struct dentry *new_dentry)
515 struct inode *cache_old_dir = I2CI(old_dir);
516 struct inode *cache_new_dir = I2CI(new_dir);
517 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
519 struct inode *cache_new_inode = new_dentry->d_inode ?
520 I2CI(new_dentry->d_inode) : NULL;
522 struct dentry *cache_old_dentry = NULL;
523 struct dentry *cache_new_dentry = NULL;
524 struct dentry *cache_new_parent = NULL;
525 struct dentry *cache_old_parent = NULL;
529 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
532 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
533 if (IS_ERR(handle)) {
534 CERROR("smfs_do_mkdir: no space for transaction\n");
539 SMFS_CACHE_HOOK_PRE(CACHE_HOOK_RENAME, handle, old_dir, rc);
541 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
542 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
544 if (!cache_old_parent || !cache_old_dentry)
545 GOTO(exit, rc = -ENOMEM);
547 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
548 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
550 if (!cache_new_parent || !cache_new_dentry)
551 GOTO(exit, rc = -ENOMEM);
553 pre_smfs_inode(old_dir, cache_old_dir);
554 pre_smfs_inode(new_dir, cache_new_dir);
556 if (cache_old_dir->i_op->rename)
557 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
558 cache_new_dir, cache_new_dentry);
560 post_smfs_inode(old_dir, cache_old_dir);
561 post_smfs_inode(new_dir, cache_new_dir);
563 SMFS_KML_POST(old_dir, old_dentry, new_dir,
564 new_dentry, REINT_RENAME, "rename", rc, exit);
565 if (new_dentry->d_inode)
566 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
568 SMFS_CACHE_HOOK_POST(CACHE_HOOK_RENAME, handle, old_dir, old_dentry,
569 new_dir, new_dentry, rc, exit);
572 post_smfs_dentry(cache_old_dentry);
573 post_smfs_dentry(cache_old_parent);
574 post_smfs_dentry(cache_new_dentry);
575 post_smfs_dentry(cache_new_parent);
576 smfs_trans_commit(old_dir, handle, 0);
580 struct inode_operations smfs_dir_iops = {
583 link: smfs_link, /* BKL held */
584 unlink: smfs_unlink, /* BKL held */
585 symlink: smfs_symlink, /* BKL held */
586 mkdir: smfs_mkdir, /* BKL held */
587 rmdir: smfs_rmdir, /* BKL held */
588 mknod: smfs_mknod, /* BKL held */
589 rename: smfs_rename, /* BKL held */
590 setxattr: smfs_setxattr, /* BKL held */
591 getxattr: smfs_getxattr, /* BKL held */
592 listxattr: smfs_listxattr, /* BKL held */
593 removexattr: smfs_removexattr, /* BKL held */
596 static ssize_t smfs_read_dir(struct file *filp, char *buf,
597 size_t size, loff_t *ppos)
599 struct dentry *dentry = filp->f_dentry;
600 struct inode *cache_inode = NULL;
601 struct smfs_file_info *sfi = NULL;
606 cache_inode = I2CI(dentry->d_inode);
612 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
614 if (ppos != &(filp->f_pos))
615 cache_ppos = &tmp_ppos;
617 cache_ppos = &sfi->c_file->f_pos;
620 if (cache_inode->i_fop->read)
621 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
625 duplicate_file(filp, sfi->c_file);
629 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
631 struct dentry *dentry = filp->f_dentry;
632 struct inode *cache_inode = NULL;
633 struct smfs_file_info *sfi = NULL;
636 cache_inode = I2CI(dentry->d_inode);
641 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
643 if (cache_inode->i_fop->readdir)
644 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
646 duplicate_file(filp, sfi->c_file);
650 struct file_operations smfs_dir_fops = {
652 readdir: smfs_readdir, /* BKL held */
653 ioctl: smfs_ioctl, /* BKL held */
654 fsync: smfs_fsync, /* BKL held */
656 release: smfs_release,