Whamcloud - gitweb
b=6285
[fs/lustre-release.git] / lustre / smfs / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_SM
24
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>
39 #include <linux/security.h>
40 #include "smfs_internal.h"
41
42 //#define NAME_ALLOC_LEN(len)     ((len+16) & ~15)
43
44 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
45 static int smfs_create(struct inode *dir, struct dentry *dentry,
46                        int mode)
47 #else
48 static int smfs_create(struct inode *dir, struct dentry *dentry,
49                        int mode, struct nameidata *nd)
50 #endif
51 {
52         struct inode *inode = NULL;
53         struct inode *parent = I2CI(dentry->d_parent->d_inode);
54         struct inode *cache_dir = I2CI(dir);
55         struct dentry *cache_dentry = NULL;
56         struct dentry *cache_parent = NULL;
57         void *handle = NULL;
58         struct hook_msg msg = {
59                 .dentry = dentry,
60         };
61         int rc = 0;
62         
63         ENTRY;
64
65         LASSERT(cache_dir);
66         LASSERT(cache_dir->i_op->create);
67         
68         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
69         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
70         if (!cache_dentry || !cache_parent) {
71                 rc = -ENOMEM;
72                 goto exit;
73         }
74        
75         handle = smfs_trans_start(dir, FSFILT_OP_CREATE, NULL);
76         if (IS_ERR(handle)) {
77                 rc = -ENOSPC;
78                 goto exit;
79         }
80         
81         SMFS_PRE_HOOK(dir, HOOK_CREATE, &msg);
82
83         pre_smfs_inode(dir, cache_dir);
84
85 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
86         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode);
87 #else
88         rc = cache_dir->i_op->create(cache_dir, cache_dentry, mode, nd);
89 #endif
90         if (!rc) {
91                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
92                                        I2SMI(dir), 0);
93                 if (inode) {
94                         d_instantiate(dentry, inode);
95                 }
96                 else
97                         rc = -ENOENT;
98         }
99         
100         SMFS_POST_HOOK(dir, HOOK_CREATE, &msg, rc); 
101
102         post_smfs_inode(dir, cache_dir);
103         smfs_trans_commit(dir, handle, 0);
104         
105 exit:
106         post_smfs_dentry(cache_dentry);
107         post_smfs_dentry(cache_parent);
108         RETURN(rc);
109 }
110
111 static struct dentry * iopen_connect_dentry(struct dentry * dentry,
112                                      struct inode *inode, int rehash)
113 {
114         struct dentry *tmp, *goal = NULL;
115         struct list_head *lp;
116
117         /* verify this dentry is really new */
118         LASSERT(dentry->d_inode == NULL);
119         LASSERT(list_empty(&dentry->d_alias));          /* d_instantiate */
120         if (rehash)
121                 LASSERT(d_unhashed(dentry));    /* d_rehash */
122         LASSERT(list_empty(&dentry->d_subdirs));
123
124         spin_lock(&dcache_lock);
125         if (!inode)
126                 goto do_rehash;
127
128         /* preferrably return a connected dentry */
129         list_for_each(lp, &inode->i_dentry) {
130                 tmp = list_entry(lp, struct dentry, d_alias);
131                 if (tmp->d_flags & DCACHE_DISCONNECTED) {
132                         LASSERT(tmp->d_alias.next == &inode->i_dentry);
133                         LASSERT(tmp->d_alias.prev == &inode->i_dentry);
134                         goal = tmp;
135                         dget_locked(goal);
136                         break;
137                 }
138         }
139
140         if (!goal)
141                 goto do_instantiate;
142
143         /* Move the goal to the de hash queue */
144         goal->d_flags &= ~ DCACHE_DISCONNECTED;
145         security_d_instantiate(goal, inode);
146         __d_rehash(dentry);
147         __d_move(goal, dentry);
148         spin_unlock(&dcache_lock);
149         iput(inode);
150
151         RETURN(goal);
152
153         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
154 do_instantiate:
155         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
156         dentry->d_inode = inode;
157 do_rehash:
158         if (rehash)
159                 __d_rehash(dentry);
160         spin_unlock(&dcache_lock);
161
162         RETURN(NULL);
163
164 }
165
166 static int smfs_do_lookup (struct inode * dir, 
167                            struct dentry * dentry,
168                            struct nameidata *nd,
169                            struct inode **inode)
170 {
171         struct inode *cache_dir = I2CI(dir);
172         struct inode *parent = I2CI(dentry->d_parent->d_inode);
173         struct dentry *cache_dentry = NULL;
174         struct dentry *cache_parent = NULL;
175         struct dentry *rdentry = NULL, *tmp = NULL;
176         int rc = 0;
177         struct hook_msg msg = {
178                 .dentry = dentry,
179         };
180
181         ENTRY;
182         
183         if (!cache_dir)
184                 RETURN(-ENOENT);
185
186         LASSERT(cache_dir->i_op->lookup);
187
188         /* preparing artificial backing fs dentries. */
189         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
190         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
191         if (!cache_dentry || !cache_parent) {
192                 rc = -ENOMEM;
193                 goto exit;
194         }
195         
196         SMFS_PRE_HOOK(dir, HOOK_LOOKUP, &msg);
197         
198         /* perform lookup in backing fs. */
199         rdentry = cache_dir->i_op->lookup(cache_dir, cache_dentry, nd);
200         if (rdentry) {
201                 if (IS_ERR(rdentry)) {
202                         rc = PTR_ERR(rdentry);
203                         rdentry = NULL;
204                 } else {
205                         tmp = rdentry;
206                 }
207         } else {
208                 tmp = cache_dentry;
209         }
210
211         SMFS_POST_HOOK(dir, HOOK_LOOKUP, &msg, rc);
212      
213         if (tmp) {
214                 //copy fields if DCACHE_CROSS_REF
215                 smfs_update_dentry(dentry, tmp);         
216                 
217                 if (tmp->d_inode) {
218                         *inode = smfs_get_inode(dir->i_sb, tmp->d_inode, 
219                                                 I2SMI(dir), 0); 
220                         if (!(*inode))
221                                 rc = -ENOENT;
222                         else {
223                                 if (!tmp->d_inode->i_nlink) {
224                                         struct inode * ind = tmp->d_inode;
225                                 
226                                         CWARN("inode #%lu (%*s) nlink is %i/%i\n",
227                                               ind->i_ino, tmp->d_name.len,
228                                               tmp->d_name.name, ind->i_nlink,
229                                               (*inode)->i_nlink);
230                                         CWARN("parent #%lu (%*s) nlink is %i\n",
231                                               dir->i_ino, tmp->d_parent->d_name.len,
232                                               tmp->d_parent->d_name.name,
233                                               cache_dir->i_nlink);
234                                         LBUG();
235                                 }
236                         }
237                 }
238         }
239         
240         if (rdentry) {
241                 dput(rdentry);
242         }
243         
244 exit:
245         post_smfs_dentry(cache_dentry);
246         post_smfs_dentry(cache_parent);
247         
248         RETURN(rc);
249 }
250
251 static struct dentry * smfs_iopen_lookup(struct inode * dir, 
252                                          struct dentry *dentry,
253                                          struct nameidata *nd)
254 {
255         struct dentry * alternate = NULL;
256         struct inode *inode = NULL;
257         int rc = 0;
258         ENTRY;
259         
260         rc = smfs_do_lookup(dir, dentry, nd, &inode);
261         if (rc)
262                 RETURN(ERR_PTR(rc));
263         
264         LASSERT(inode);
265         /* preferrably return a connected dentry */
266         spin_lock(&dcache_lock);
267         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
268                 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
269         }
270
271         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
272                 dget_locked(alternate);
273                 spin_lock(&alternate->d_lock);
274                 alternate->d_flags |= DCACHE_REFERENCED;
275                 spin_unlock(&alternate->d_lock);
276                 iput(inode);
277                 spin_unlock(&dcache_lock);
278                 RETURN(alternate);
279         }
280         
281         dentry->d_flags |= DCACHE_DISCONNECTED;
282
283         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
284         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
285         dentry->d_inode = inode;
286
287         __d_rehash(dentry);                             /* d_rehash */
288         spin_unlock(&dcache_lock);
289
290         return NULL;
291 }
292
293 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
294 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
295 #else
296 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
297                                   struct nameidata *nd)
298 #endif
299 {
300         struct dentry * rdentry = NULL;
301         struct inode * inode = NULL;
302         int rc;
303
304         ENTRY;
305         
306         rc = smfs_do_lookup(dir, dentry, nd, &inode);
307         if (rc)
308                 RETURN(ERR_PTR(rc));
309         
310         //lmv stuff. Special dentry that has no inode.
311         if (dentry->d_flags & DCACHE_CROSS_REF) {
312                 d_add(dentry, NULL);
313                 RETURN(NULL);
314         }
315         //TODO: should flags be checked and copied before?        
316         rdentry = iopen_connect_dentry(dentry, inode, 1);
317         
318         RETURN(rdentry);
319 }
320
321 #if HAVE_LOOKUP_RAW
322 static int smfs_lookup_raw(struct inode *dir, const char *name,
323                            int len, ino_t *data)
324 {
325         struct inode *cache_dir = I2CI(dir);
326         int rc = 0;
327
328         if (!cache_dir)
329                 RETURN(-ENOENT);
330         
331         if (cache_dir->i_op->lookup_raw) {
332                 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
333         } else {
334                 CWARN("do not have raw lookup ops in bottom fs\n");
335         }
336
337         RETURN(rc);
338 }
339 #endif
340
341 static int smfs_link(struct dentry *old_dentry,
342                      struct inode *dir, struct dentry *dentry)
343 {
344         struct inode *parent = I2CI(dentry->d_parent->d_inode);
345         struct inode *cache_dir = I2CI(dir);
346         struct inode *old_inode = old_dentry->d_inode;
347         struct inode *cache_old_inode = I2CI(old_inode);
348         struct dentry *cache_old_dentry = NULL;
349         struct dentry *cache_dentry = NULL;
350         struct dentry *cache_parent = NULL;
351         void *handle = NULL;
352         int rc = 0;
353         struct hook_link_msg msg = {
354                 .dentry = old_dentry,
355                 .new_dentry = dentry
356         };
357
358         ENTRY;
359
360         if (!cache_dir)
361                 RETURN(-ENOENT);
362         
363         if (!cache_old_inode)
364                 RETURN(-ENOENT);
365         
366         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
367         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
368         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
369         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
370                 rc = -ENOMEM;
371                 goto exit;
372         }        
373         
374         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
375         if (IS_ERR(handle)) {
376                  rc = -ENOSPC;
377                  goto exit;
378         }
379
380         pre_smfs_inode(dir, cache_dir);
381         pre_smfs_inode(old_inode, cache_old_inode);
382
383         //lock_kernel();
384         SMFS_PRE_HOOK(dir, HOOK_LINK, &msg); 
385
386         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
387         if (!rc) {
388                 atomic_inc(&old_inode->i_count);
389                 old_inode->i_nlink++;
390                 dput(iopen_connect_dentry(dentry, old_inode, 0));
391         }
392
393         SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc); 
394         
395         post_smfs_inode(old_inode, cache_old_inode);
396         post_smfs_inode(dir, cache_dir);
397
398         smfs_trans_commit(dir, handle, 0);
399         
400 exit:
401         //unlock_kernel();
402         post_smfs_dentry(cache_dentry);
403         post_smfs_dentry(cache_parent);
404         post_smfs_dentry(cache_old_dentry);
405         
406         RETURN(rc);
407 }
408
409 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
410 {
411         struct inode *cache_dir = I2CI(dir);
412         struct inode *cache_inode = I2CI(dentry->d_inode);
413         struct inode *parent = I2CI(dentry->d_parent->d_inode);
414         struct dentry *cache_dentry = NULL;
415         struct dentry *cache_parent = NULL;
416         void   *handle = NULL;
417         int    rc = 0;
418         //int    mode = 0;
419         struct hook_unlink_msg msg = {
420                 .dentry = dentry,
421                 .mode = dentry->d_inode->i_mode
422         };
423
424         ENTRY;
425         
426         LASSERT(cache_dir);
427         LASSERT(cache_inode);
428         LASSERT(cache_dir->i_op->unlink);
429         LASSERT(parent);
430         
431         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
432         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
433         if (!cache_dentry || !cache_parent) {
434                 rc = -ENOMEM;
435                 goto exit;
436         }
437                 
438         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
439         if (IS_ERR(handle)) {
440                 rc = -ENOSPC;
441                 goto exit;
442         }
443         
444         pre_smfs_inode(dir, cache_dir);
445         pre_smfs_inode(dentry->d_inode, cache_inode);
446
447         SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg); 
448         
449         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
450                 
451         SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc); 
452         if (!rc) {
453                 post_smfs_inode(dentry->d_inode, cache_inode);
454                 dentry->d_inode->i_nlink--;
455                 post_smfs_inode(dir, cache_dir);
456         }
457         
458         smfs_trans_commit(dir, handle, 0);
459 exit:
460         post_smfs_dentry(cache_dentry);
461         post_smfs_dentry(cache_parent);
462         RETURN(rc);
463 }
464
465 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
466                         const char *symname)
467 {
468         struct inode *cache_dir = I2CI(dir);
469         struct inode *inode = NULL;
470         struct inode *parent = I2CI(dentry->d_parent->d_inode);
471         struct dentry *cache_dentry = NULL;
472         struct dentry *cache_parent = NULL;
473         void   *handle = NULL;
474         int    rc = 0;
475         struct hook_symlink_msg msg = {
476                 .dentry = dentry,
477                 .tgt_len = strlen(symname) + 1,
478                 .symname = (char*)symname
479         };
480
481         ENTRY;
482         
483         LASSERT(cache_dir);
484         LASSERT(cache_dir->i_op->symlink);
485         LASSERT(parent);
486
487         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
488         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
489         if (!cache_parent || !cache_dentry) {
490                 rc = -ENOMEM;
491                 goto exit;
492         }
493        
494         handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
495         if (IS_ERR(handle)) {
496                 rc = -ENOSPC;
497                 goto exit;
498         }
499         
500         pre_smfs_inode(dir, cache_dir);
501
502         SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg); 
503         
504         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
505         if (!rc) {        
506                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
507                                        I2SMI(dir), 0);
508                 if (inode) {
509                         d_instantiate(dentry, inode);
510                 }
511                 else
512                         rc = -ENOENT;
513         }
514         
515         SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
516         
517         post_smfs_inode(dir, cache_dir);
518         smfs_trans_commit(dir, handle, 0);
519
520 exit:
521         post_smfs_dentry(cache_dentry);
522         post_smfs_dentry(cache_parent);
523         RETURN(rc);
524 }
525
526 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
527 {
528         struct inode *cache_dir = I2CI(dir);
529         struct inode *parent = I2CI(dentry->d_parent->d_inode);
530         struct inode *inode = NULL;
531         struct dentry *cache_dentry = NULL;
532         struct dentry *cache_parent = NULL;
533         void   *handle = NULL;
534         int    rc = 0;
535         struct hook_msg msg = {
536                 .dentry = dentry,
537         };
538
539         ENTRY;
540         
541         LASSERT(cache_dir);
542         LASSERT(parent);
543         
544         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
545         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
546         if (!cache_parent || !cache_dentry) {
547                 rc = -ENOMEM;
548                 goto exit;
549         }
550
551         handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
552         if (IS_ERR(handle)) {
553                 rc = -ENOSPC;
554                 goto exit;
555         }
556         
557         pre_smfs_inode(dir, cache_dir);
558         SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg); 
559         
560         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
561         if (!rc) {
562                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
563                                        I2SMI(dir), 0);
564                 if (inode) {
565                         dir->i_nlink++;
566                         d_instantiate(dentry, inode);
567                 }
568                 else
569                         rc = -ENOENT;
570         }
571
572         SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc); 
573         post_smfs_inode(dir, cache_dir);
574         smfs_trans_commit(dir, handle, 0);
575
576 exit:
577         post_smfs_dentry(cache_dentry);
578         post_smfs_dentry(cache_parent);
579         RETURN(rc);
580 }
581
582 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
583 {
584         struct inode *cache_dir = I2CI(dir);
585         struct inode *cache_inode = I2CI(dentry->d_inode);
586         struct inode *parent = I2CI(dentry->d_parent->d_inode);
587         struct dentry *cache_dentry = NULL;
588         struct dentry *cache_parent = NULL;
589         struct inode * inode = dentry->d_inode;
590         void *handle = NULL;
591         int    rc = 0;
592         struct hook_unlink_msg msg = {
593                 .dentry = dentry,
594                 .mode = S_IFDIR
595         };
596
597         ENTRY;
598         
599         LASSERT(cache_dir);
600         LASSERT(cache_dir->i_op->rmdir);
601         LASSERT(parent);
602
603         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
604         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
605         if (!cache_parent || !cache_dentry) {
606                 rc = -ENOMEM;
607                 goto exit;
608         }
609         
610         handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
611         if (IS_ERR(handle) ) {
612                 rc = -ENOSPC;
613                 goto exit;
614         }
615
616         dentry_unhash(cache_dentry);
617
618         pre_smfs_inode(dir, cache_dir);
619         pre_smfs_inode(inode, cache_inode);
620         
621         SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg); 
622         
623         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
624               
625         SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc); 
626         if (!rc) {
627                 if (inode->i_nlink != 2)
628                         CWARN("Directory #%lu under rmdir has %i nlinks\n",
629                                 inode->i_ino, inode->i_nlink);
630                 inode->i_nlink = 0;
631                 dir->i_nlink--;
632                 post_smfs_inode(dir, cache_dir);
633                 post_smfs_inode(inode, cache_inode);
634                 //like vfs_rmdir is doing with inode
635                 cache_inode->i_flags |= S_DEAD;
636         }
637         smfs_trans_commit(dir, handle, 0);
638         dput(cache_dentry);
639 exit:
640         post_smfs_dentry(cache_dentry);
641         post_smfs_dentry(cache_parent);
642         RETURN(rc);
643 }
644
645 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
646 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
647                       int mode, int rdev)
648 #else
649 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
650                       int mode, dev_t rdev)
651 #endif
652 {
653         struct inode *cache_dir = I2CI(dir);
654         struct inode *inode = NULL;
655         struct inode *parent = I2CI(dentry->d_parent->d_inode);
656         struct dentry *cache_dentry = NULL;
657         struct dentry *cache_parent = NULL;
658         void *handle = NULL;
659         int rc = 0;
660         struct hook_msg msg = {
661                 .dentry = dentry,
662         };
663  
664         ENTRY;
665         
666         LASSERT(parent);
667         LASSERT(cache_dir);
668         LASSERT(cache_dir->i_op->mknod);
669
670         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
671         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
672         if (!cache_parent || !cache_dentry) {
673                 rc = -ENOMEM;
674                 goto exit;
675         }
676
677         handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
678         if (IS_ERR(handle)) {
679                 rc = -ENOSPC;
680                 goto exit;
681         }
682         
683         pre_smfs_inode(dir, cache_dir);
684         
685         SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg); 
686         
687         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
688         if (!rc) {
689                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode,
690                                        I2SMI(dir), 0);
691                 if (inode) {
692                         //smsf_update_dentry(dentry, cache_dentry);
693                         d_instantiate(dentry, inode);
694                 }
695                 else
696                         rc = -ENOENT;
697         }
698
699         SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc); 
700         
701         post_smfs_inode(dir, cache_dir);
702         
703         smfs_trans_commit(dir, handle, 0);
704
705 exit:
706         post_smfs_dentry(cache_dentry);
707         post_smfs_dentry(cache_parent);
708         RETURN(rc);
709 }
710
711 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
712                        struct inode *new_dir,struct dentry *new_dentry)
713 {
714         struct inode *cache_old_dir = I2CI(old_dir);
715         struct inode *cache_new_dir = I2CI(new_dir);
716         struct inode *new_inode = new_dentry->d_inode;
717         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
718         struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
719         struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
720         struct inode *cache_new_inode = NULL;
721         struct dentry *cache_old_dentry = NULL;
722         struct dentry *cache_new_dentry = NULL;
723         struct dentry *cache_new_parent = NULL;
724         struct dentry *cache_old_parent = NULL;
725         void *handle = NULL;
726         int    rc = 0;
727         struct hook_rename_msg msg = {
728                 .dentry = old_dentry,
729                 .new_dir = new_dir,
730                 .new_dentry = new_dentry
731         };
732
733         ENTRY;
734                 
735         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
736                 RETURN(-ENOENT);
737
738         if (new_inode) {
739                 cache_new_inode = I2CI(new_inode);
740                 if (!cache_new_inode)
741                         RETURN(-ENOENT);
742         }
743         
744         cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
745         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
746                                            old_dentry);
747         if (!cache_old_parent || !cache_old_dentry) {
748                 rc = -ENOMEM;
749                 goto exit;
750         }
751         
752         cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
753         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
754                                            new_dentry);
755         if (!cache_new_parent || !cache_new_dentry) {
756                 rc = -ENOMEM;
757                 goto exit;
758         }
759
760         handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
761         if (IS_ERR(handle)) {
762                 rc = -ENOSPC;
763                 goto exit;
764         }
765         
766         pre_smfs_inode(old_dir, cache_old_dir);
767         pre_smfs_inode(new_dir, cache_new_dir);
768         pre_smfs_inode(old_dentry->d_inode, cache_old_inode);
769         if (new_inode)
770                 pre_smfs_inode(new_inode, cache_new_inode);
771
772         SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg); 
773         
774         rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
775                                          cache_new_dir, cache_new_dentry);
776         
777         SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc); 
778         if (!rc) {
779                 post_smfs_inode(old_dir, cache_old_dir);
780                 post_smfs_inode(new_dir, cache_new_dir);
781                 post_smfs_inode(old_dentry->d_inode, cache_old_inode);
782                 if (new_inode) {
783                         post_smfs_inode(new_inode, cache_new_inode);
784                         new_inode->i_nlink--;
785                 }
786                 //directory is renamed
787                 if (S_ISDIR(old_dentry->d_inode->i_mode)) {
788                         old_dir->i_nlink--;
789                         if (new_inode) {
790                                 new_inode->i_nlink--;
791                         } else {
792                                 new_dir->i_nlink++;
793                         }
794                 }
795         }
796         smfs_trans_commit(old_dir, handle, 0);
797         
798 exit:
799         post_smfs_dentry(cache_old_dentry);
800         post_smfs_dentry(cache_old_parent);
801         post_smfs_dentry(cache_new_dentry);
802         post_smfs_dentry(cache_new_parent);
803         RETURN(rc);
804 }
805
806 struct inode_operations smfs_dir_iops = {
807         .create         = smfs_create,
808         .lookup         = smfs_lookup,
809 #if HAVE_LOOKUP_RAW
810         .lookup_raw     = smfs_lookup_raw,
811 #endif
812         .link           = smfs_link,              
813         .unlink         = smfs_unlink,            
814         .symlink        = smfs_symlink,           
815         .mkdir          = smfs_mkdir,             
816         .rmdir          = smfs_rmdir,             
817         .mknod          = smfs_mknod,             
818         .rename         = smfs_rename,            
819         .setxattr       = smfs_setxattr,
820         .getxattr       = smfs_getxattr,
821         .listxattr      = smfs_listxattr,
822         .removexattr    = smfs_removexattr,
823         .permission     = smfs_permission,
824 };
825
826 struct inode_operations smfs_iopen_iops = {
827         .lookup         = smfs_iopen_lookup,
828 };
829
830 static ssize_t smfs_read_dir(struct file *filp, char *buf,
831                              size_t size, loff_t *ppos)
832 {
833         struct dentry *dentry = filp->f_dentry;
834         struct inode *cache_inode = NULL;
835         struct smfs_file_info *sfi = NULL;
836         loff_t tmp_ppos;
837         loff_t *cache_ppos = NULL;
838         int    rc = 0;
839
840         ENTRY;
841         
842         cache_inode = I2CI(dentry->d_inode);
843
844         if (!cache_inode || !cache_inode->i_fop->read)
845                 RETURN(-EINVAL);
846
847         sfi = F2SMFI(filp);
848         if (sfi->magic != SMFS_FILE_MAGIC)
849                 BUG();
850
851         if (ppos != &(filp->f_pos))
852                 cache_ppos = &tmp_ppos;
853         else
854                 cache_ppos = &sfi->c_file->f_pos;
855         
856         *cache_ppos = *ppos;
857
858         rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
859         if (rc)
860                 RETURN(rc);
861
862         *ppos = *cache_ppos;
863         
864         duplicate_file(filp, sfi->c_file);
865         
866         RETURN(rc);
867 }
868
869 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
870 {
871         struct dentry *dentry = filp->f_dentry;
872         struct inode *cache_inode = NULL;
873         struct smfs_file_info *sfi = NULL;
874         int    rc = 0;
875         struct hook_readdir_msg msg = {
876                 .dentry = dentry,
877                 .filp = filp,
878                 .dirent = dirent,
879                 .filldir = filldir
880         };
881
882         ENTRY;
883         
884         cache_inode = I2CI(dentry->d_inode);
885         if (!cache_inode || !cache_inode->i_fop->readdir)
886                 RETURN(-EINVAL);
887
888         sfi = F2SMFI(filp);
889         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
890
891         SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg); 
892         
893         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
894         
895         SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
896         duplicate_file(filp, sfi->c_file);
897
898         RETURN(rc);
899 }
900
901 struct file_operations smfs_dir_fops = {
902         .read           = smfs_read_dir,
903         .readdir        = smfs_readdir,       /* BKL held */
904         .ioctl          = smfs_ioctl,         /* BKL held */
905         .fsync          = smfs_fsync,         /* BKL held */
906         .open           = smfs_open,
907         .release        = smfs_release,
908 };
909
910 struct file_operations smfs_iopen_fops = {
911         .read           = smfs_read_dir,
912 };
913