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);
73 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
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, inode, rc, exit);
93 d_instantiate(dentry, inode);
94 post_smfs_inode(dir, cache_dir);
96 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_CREATE, handle, POST_HOOK, rc,
100 post_smfs_dentry(cache_dentry);
101 post_smfs_dentry(cache_parent);
102 smfs_trans_commit(dir, handle, 0);
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
107 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
109 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
110 struct nameidata *nd)
113 struct inode *cache_dir;
114 struct inode *cache_inode;
115 struct inode *inode = NULL;
116 struct dentry *cache_dentry = NULL;
117 struct dentry *cache_parent = NULL;
118 struct dentry *rc = NULL;
124 if (!(cache_dir = I2CI(dir)))
125 RETURN(ERR_PTR(-ENOENT));
127 /* preparing artificial backing fs dentries. */
128 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
129 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
131 if (!cache_dentry || !cache_parent)
132 GOTO(exit, rc = ERR_PTR(-ENOMEM));
134 if (!cache_dir && cache_dir->i_op->lookup)
135 GOTO(exit, rc = ERR_PTR(-ENOENT));
137 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle,
138 PRE_HOOK, rc2, exit);
140 /* perform lookup in backing fs. */
141 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
142 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry);
144 rc = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
146 if (rc && IS_ERR(rc))
149 if ((cache_inode = rc ? rc->d_inode : cache_dentry->d_inode)) {
150 if (IS_ERR(cache_inode)) {
151 dentry->d_inode = cache_inode;
152 GOTO(exit, rc = NULL);
154 SMFS_GET_INODE(dir->i_sb, cache_inode, dir, inode, rc2, exit);
157 d_add(dentry, inode);
160 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_LOOKUP, handle, POST_HOOK, rc2,
165 post_smfs_dentry(cache_dentry);
166 post_smfs_dentry(cache_parent);
167 smfs_trans_commit(dir, handle, 0);
171 static int smfs_link(struct dentry * old_dentry,
172 struct inode * dir, struct dentry *dentry)
174 struct inode *cache_old_inode = NULL;
175 struct inode *cache_dir = I2CI(dir);
176 struct inode *inode = NULL;
177 struct dentry *cache_dentry = NULL;
178 struct dentry *cache_old_dentry = NULL;
179 struct dentry *cache_parent = NULL;
183 inode = old_dentry->d_inode;
185 cache_old_inode = I2CI(inode);
187 handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
192 SMFS_HOOK(dir, old_dentry, NULL, NULL, HOOK_LINK, handle, PRE_HOOK, rc,
195 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
196 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
198 if (!cache_parent || !cache_dentry)
199 GOTO(exit, rc = -ENOMEM);
201 cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
202 if (!cache_old_dentry)
203 GOTO(exit, rc = -ENOMEM);
205 pre_smfs_inode(dir, cache_dir);
206 pre_smfs_inode(inode, cache_old_dentry->d_inode);
208 if (cache_dir->i_op->link)
209 rc = cache_dir->i_op->link(cache_old_dentry, cache_dir,
214 atomic_inc(&inode->i_count);
215 post_smfs_inode(inode, cache_old_dentry->d_inode);
216 d_instantiate(dentry, inode);
217 post_smfs_inode(dir, cache_dir);
219 SMFS_HOOK(dir, old_dentry, dentry, NULL, HOOK_LINK, handle, POST_HOOK,
223 post_smfs_dentry(cache_dentry);
224 post_smfs_dentry(cache_parent);
225 post_smfs_dentry(cache_old_dentry);
226 smfs_trans_commit(dir, handle, 0);
230 static int smfs_unlink(struct inode * dir,
231 struct dentry *dentry)
233 struct inode *cache_dir = I2CI(dir);
234 struct inode *cache_inode = I2CI(dentry->d_inode);
235 struct dentry *cache_dentry;
236 struct dentry *cache_parent;
241 if (!cache_dir || !cache_inode)
244 handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
248 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_UNLINK, handle, PRE_HOOK, rc,
251 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
252 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
254 if (!cache_parent || !cache_dentry)
255 GOTO(exit, rc = -ENOMEM);
258 pre_smfs_inode(dir, cache_dir);
259 pre_smfs_inode(dentry->d_inode, cache_inode);
260 if (cache_dir->i_op->unlink)
261 rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
262 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
263 post_smfs_inode(dir, cache_dir);
266 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_UNLINK, handle, POST_HOOK,
269 post_smfs_dentry(cache_dentry);
270 post_smfs_dentry(cache_parent);
271 smfs_trans_commit(dir, handle, 0);
275 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
278 struct inode *cache_dir = I2CI(dir);
279 struct inode *inode = NULL;
280 struct dentry *cache_dentry;
281 struct dentry *cache_parent;
288 handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
293 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_SYMLINK, handle, PRE_HOOK, rc,
296 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
297 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
299 if (!cache_parent || !cache_dentry)
300 GOTO(exit, rc = -ENOMEM);
302 pre_smfs_inode(dir, cache_dir);
303 if (cache_dir->i_op->symlink)
304 rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
306 post_smfs_inode(dir, cache_dir);
308 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
310 d_instantiate(dentry, inode);
314 tgt_len = strlen(symname) + 1;
316 SMFS_HOOK(dir, dentry, (char *)symname, &tgt_len, HOOK_SYMLINK, handle,
317 POST_HOOK, rc, exit);
320 post_smfs_dentry(cache_dentry);
321 post_smfs_dentry(cache_parent);
322 smfs_trans_commit(dir, handle, 0);
326 static int smfs_mkdir(struct inode *dir, struct dentry *dentry,
329 struct inode *cache_dir = I2CI(dir);
330 struct inode *inode = NULL;
331 struct dentry *cache_dentry;
332 struct dentry *cache_parent;
339 handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
345 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, PRE_HOOK, rc,
348 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
349 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
351 if (!cache_parent || !cache_dentry)
352 GOTO(exit, rc = -ENOMEM);
354 pre_smfs_inode(dir, cache_dir);
356 if (cache_dir->i_op->mkdir)
357 rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
362 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
363 d_instantiate(dentry, inode);
364 post_smfs_inode(dir, cache_dir);
366 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKDIR, handle, POST_HOOK, rc,
370 post_smfs_dentry(cache_dentry);
371 post_smfs_dentry(cache_parent);
372 smfs_trans_commit(dir, handle, 0);
376 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
378 struct inode *cache_dir = I2CI(dir);
379 struct inode *cache_inode = I2CI(dentry->d_inode);
380 struct dentry *cache_dentry = NULL;
381 struct dentry *cache_parent = NULL;
383 int rc = 0, mode = S_IFDIR;
388 handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
389 if (IS_ERR(handle) ) {
390 CERROR("smfs_do_mkdir: no space for transaction\n");
396 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_RMDIR, handle, PRE_HOOK, rc,
399 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry);
400 cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
402 if (!cache_parent || !cache_dentry)
403 GOTO(exit, rc = -ENOMEM);
405 pre_smfs_inode(dir, cache_dir);
406 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
407 if (cache_dir->i_op->rmdir)
408 rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
410 post_smfs_inode(dir, cache_dir);
411 post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
414 SMFS_HOOK(dir, dentry, &mode, NULL, HOOK_RMDIR, handle, POST_HOOK,
417 post_smfs_dentry(cache_dentry);
418 post_smfs_dentry(cache_parent);
419 smfs_trans_commit(dir, handle, 0);
423 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
424 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
427 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
428 int mode, dev_t rdev)
431 struct inode *cache_dir = I2CI(dir);
432 struct inode *inode = NULL;
433 struct dentry *cache_dentry = NULL;
434 struct dentry *cache_parent = NULL;
441 handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
442 if (IS_ERR(handle)) {
443 CERROR("smfs_do_mkdir: no space for transaction\n");
448 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, PRE_HOOK, rc,
451 cache_parent = pre_smfs_dentry(NULL, cache_dir, dentry->d_parent);
452 cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
453 if (!cache_parent || !cache_dentry)
454 GOTO(exit, rc = -ENOMEM);
456 pre_smfs_inode(dir, cache_dir);
457 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
459 if (!cache_dir->i_op->mknod)
462 if ((rc = cache_dir->i_op->mknod(cache_dir, cache_dentry,
466 SMFS_GET_INODE(dir->i_sb, cache_dentry->d_inode, dir, inode, rc, exit);
468 d_instantiate(dentry, inode);
470 pre_smfs_inode(dir, cache_dir);
471 pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
473 SMFS_HOOK(dir, dentry, NULL, NULL, HOOK_MKNOD, handle, POST_HOOK, rc,
478 post_smfs_dentry(cache_dentry);
479 post_smfs_dentry(cache_parent);
480 smfs_trans_commit(dir, handle, 0);
484 static int smfs_rename(struct inode * old_dir, struct dentry *old_dentry,
485 struct inode * new_dir,struct dentry *new_dentry)
487 struct inode *cache_old_dir = I2CI(old_dir);
488 struct inode *cache_new_dir = I2CI(new_dir);
489 struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
491 struct inode *cache_new_inode = new_dentry->d_inode ?
492 I2CI(new_dentry->d_inode) : NULL;
494 struct dentry *cache_old_dentry = NULL;
495 struct dentry *cache_new_dentry = NULL;
496 struct dentry *cache_new_parent = NULL;
497 struct dentry *cache_old_parent = NULL;
501 if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
504 handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
505 if (IS_ERR(handle)) {
506 CERROR("smfs_do_mkdir: no space for transaction\n");
512 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
515 cache_old_parent = pre_smfs_dentry(NULL, cache_old_dir, old_dentry);
516 cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
518 if (!cache_old_parent || !cache_old_dentry)
519 GOTO(exit, rc = -ENOMEM);
521 cache_new_parent = pre_smfs_dentry(NULL, cache_new_dir, new_dentry);
522 cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
524 if (!cache_new_parent || !cache_new_dentry)
525 GOTO(exit, rc = -ENOMEM);
527 pre_smfs_inode(old_dir, cache_old_dir);
528 pre_smfs_inode(new_dir, cache_new_dir);
530 if (cache_old_dir->i_op->rename)
531 rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
532 cache_new_dir, cache_new_dentry);
534 post_smfs_inode(old_dir, cache_old_dir);
535 post_smfs_inode(new_dir, cache_new_dir);
537 SMFS_HOOK(old_dir, old_dentry, new_dir, new_dentry, HOOK_RENAME, handle,
538 POST_HOOK, rc, exit);
540 if (new_dentry->d_inode)
541 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
544 post_smfs_dentry(cache_old_dentry);
545 post_smfs_dentry(cache_old_parent);
546 post_smfs_dentry(cache_new_dentry);
547 post_smfs_dentry(cache_new_parent);
548 smfs_trans_commit(old_dir, handle, 0);
552 struct inode_operations smfs_dir_iops = {
555 link: smfs_link, /* BKL held */
556 unlink: smfs_unlink, /* BKL held */
557 symlink: smfs_symlink, /* BKL held */
558 mkdir: smfs_mkdir, /* BKL held */
559 rmdir: smfs_rmdir, /* BKL held */
560 mknod: smfs_mknod, /* BKL held */
561 rename: smfs_rename, /* BKL held */
562 setxattr: smfs_setxattr, /* BKL held */
563 getxattr: smfs_getxattr, /* BKL held */
564 listxattr: smfs_listxattr, /* BKL held */
565 removexattr: smfs_removexattr, /* BKL held */
568 static ssize_t smfs_read_dir(struct file *filp, char *buf,
569 size_t size, loff_t *ppos)
571 struct dentry *dentry = filp->f_dentry;
572 struct inode *cache_inode = NULL;
573 struct smfs_file_info *sfi = NULL;
578 cache_inode = I2CI(dentry->d_inode);
584 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
586 if (ppos != &(filp->f_pos))
587 cache_ppos = &tmp_ppos;
589 cache_ppos = &sfi->c_file->f_pos;
592 if (cache_inode->i_fop->read)
593 rc = cache_inode->i_fop->read(sfi->c_file, buf, size,
596 duplicate_file(filp, sfi->c_file);
600 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
602 struct dentry *dentry = filp->f_dentry;
603 struct inode *cache_inode = NULL;
604 struct smfs_file_info *sfi = NULL;
607 cache_inode = I2CI(dentry->d_inode);
612 if (sfi->magic != SMFS_FILE_MAGIC) BUG();
614 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
617 if (cache_inode->i_fop->readdir)
618 rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
620 SMFS_HOOK(dentry->d_inode, filp, dirent, filldir, HOOK_READDIR, NULL,
621 POST_HOOK, rc, exit);
623 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,