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