Whamcloud - gitweb
3dde19f574ac93f1f0e332549fcb41dff97915df
[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->i_ino,
92                                        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                         if (!tmp->d_inode->i_nlink)
219                                 CWARN("inode #%lu (%p) nlink is 0\n",
220                                       tmp->d_inode->i_ino, tmp->d_inode);
221                         
222                         *inode = smfs_get_inode(dir->i_sb, tmp->d_inode->i_ino, 
223                                         dir, 0); 
224                         if (!(*inode))
225                                 rc = -ENOENT;
226                 }
227         }
228         
229         if (rdentry) {
230                 dput(rdentry);
231         }
232         
233 exit:
234         post_smfs_dentry(cache_dentry);
235         post_smfs_dentry(cache_parent);
236         
237         RETURN(rc);
238 }
239
240 static struct dentry * smfs_iopen_lookup(struct inode * dir, 
241                                          struct dentry *dentry,
242                                          struct nameidata *nd)
243 {
244         struct dentry * alternate = NULL;
245         struct inode *inode = NULL;
246         int rc = 0;
247         ENTRY;
248         
249         rc = smfs_do_lookup(dir, dentry, nd, &inode);
250         if (rc)
251                 RETURN(ERR_PTR(rc));
252         
253         LASSERT(inode);
254         /* preferrably return a connected dentry */
255         spin_lock(&dcache_lock);
256         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
257                 LASSERT(!(alternate->d_flags & DCACHE_DISCONNECTED));
258         }
259
260         list_for_each_entry(alternate, &inode->i_dentry, d_alias) {
261                 dget_locked(alternate);
262                 spin_lock(&alternate->d_lock);
263                 alternate->d_flags |= DCACHE_REFERENCED;
264                 spin_unlock(&alternate->d_lock);
265                 iput(inode);
266                 spin_unlock(&dcache_lock);
267                 RETURN(alternate);
268         }
269         
270         dentry->d_flags |= DCACHE_DISCONNECTED;
271
272         /* d_add(), but don't drop dcache_lock before adding dentry to inode */
273         list_add(&dentry->d_alias, &inode->i_dentry);   /* d_instantiate */
274         dentry->d_inode = inode;
275
276         __d_rehash(dentry);                             /* d_rehash */
277         spin_unlock(&dcache_lock);
278
279         return NULL;
280 }
281
282 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
283 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry)
284 #else
285 static struct dentry *smfs_lookup(struct inode *dir, struct dentry *dentry,
286                                   struct nameidata *nd)
287 #endif
288 {
289         struct dentry * rdentry = NULL;
290         struct inode * inode = NULL;
291         int rc;
292
293         ENTRY;
294         
295         rc = smfs_do_lookup(dir, dentry, nd, &inode);
296         if (rc)
297                 RETURN(ERR_PTR(rc));
298         
299         //lmv stuff. Special dentry that has no inode.
300         if (dentry->d_flags & DCACHE_CROSS_REF) {
301                 d_add(dentry, NULL);
302                 RETURN(NULL);
303         }
304         //TODO: should flags be checked and copied before?        
305         rdentry = iopen_connect_dentry(dentry, inode, 1);
306         
307         RETURN(rdentry);
308 }
309
310 static int smfs_lookup_raw(struct inode *dir, const char *name,
311                            int len, ino_t *data)
312 {
313         struct inode *cache_dir = I2CI(dir);
314         int rc = 0;
315
316         if (!cache_dir)
317                 RETURN(-ENOENT);
318         
319         if (cache_dir->i_op->lookup_raw) {
320                 rc = cache_dir->i_op->lookup_raw(cache_dir, name, len, data);
321         } else {
322                 CWARN("do not have raw lookup ops in bottom fs\n");
323         }
324
325         RETURN(rc);
326 }
327
328 static int smfs_link(struct dentry *old_dentry,
329                      struct inode *dir, struct dentry *dentry)
330 {
331         struct inode *parent = I2CI(dentry->d_parent->d_inode);
332         struct inode *cache_dir = I2CI(dir);
333         struct inode *old_inode = old_dentry->d_inode;
334         struct inode *cache_old_inode = I2CI(old_inode);
335         struct dentry *cache_old_dentry = NULL;
336         struct dentry *cache_dentry = NULL;
337         struct dentry *cache_parent = NULL;
338         void *handle = NULL;
339         int rc = 0;
340         struct hook_link_msg msg = {
341                 .dentry = old_dentry,
342                 .new_dentry = dentry
343         };
344
345         ENTRY;
346
347         if (!cache_dir)
348                 RETURN(-ENOENT);
349         
350         if (!cache_old_inode)
351                 RETURN(-ENOENT);
352         
353         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
354         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
355         cache_old_dentry = pre_smfs_dentry(NULL, cache_old_inode, old_dentry);
356         if (!cache_old_dentry || !cache_dentry || !cache_parent) {
357                 rc = -ENOMEM;
358                 goto exit;
359         }        
360         
361         handle = smfs_trans_start(dir, FSFILT_OP_LINK, NULL);
362         if (IS_ERR(handle)) {
363                  rc = -ENOSPC;
364                  goto exit;
365         }
366
367         pre_smfs_inode(dir, cache_dir);
368         pre_smfs_inode(old_inode, cache_old_inode);
369
370         //lock_kernel();
371         SMFS_PRE_HOOK(dir, HOOK_LINK, &msg); 
372
373         rc = cache_dir->i_op->link(cache_old_dentry, cache_dir, cache_dentry);
374         if (!rc) {
375                 atomic_inc(&old_inode->i_count);
376                 dput(iopen_connect_dentry(dentry, old_inode, 0));
377         }
378
379         SMFS_POST_HOOK(dir, HOOK_LINK, &msg, rc); 
380         
381         post_smfs_inode(old_inode, cache_old_inode);
382         post_smfs_inode(dir, cache_dir);
383
384         smfs_trans_commit(dir, handle, 0);
385         
386 exit:
387         //unlock_kernel();
388         post_smfs_dentry(cache_dentry);
389         post_smfs_dentry(cache_parent);
390         post_smfs_dentry(cache_old_dentry);
391         
392         RETURN(rc);
393 }
394
395 static int smfs_unlink(struct inode * dir, struct dentry *dentry)
396 {
397         struct inode *cache_dir = I2CI(dir);
398         struct inode *cache_inode = I2CI(dentry->d_inode);
399         struct inode *parent = I2CI(dentry->d_parent->d_inode);
400         struct dentry *cache_dentry = NULL;
401         struct dentry *cache_parent = NULL;
402         void   *handle = NULL;
403         int    rc = 0;
404         //int    mode = 0;
405         struct hook_unlink_msg msg = {
406                 .dentry = dentry,
407                 .mode = dentry->d_inode->i_mode
408         };
409
410         ENTRY;
411         
412         LASSERT(cache_dir);
413         LASSERT(cache_inode);
414         LASSERT(cache_dir->i_op->unlink);
415         LASSERT(parent);
416         
417         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
418         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
419         if (!cache_dentry || !cache_parent) {
420                 rc = -ENOMEM;
421                 goto exit;
422         }
423                 
424         handle = smfs_trans_start(dir, FSFILT_OP_UNLINK, NULL);
425         if (IS_ERR(handle)) {
426                 rc = -ENOSPC;
427                 goto exit;
428         }
429         
430         pre_smfs_inode(dir, cache_dir);
431         pre_smfs_inode(dentry->d_inode, cache_inode);
432
433         SMFS_PRE_HOOK(dir, HOOK_UNLINK, &msg); 
434         
435         rc = cache_dir->i_op->unlink(cache_dir, cache_dentry);
436                 
437         SMFS_POST_HOOK(dir, HOOK_UNLINK, &msg, rc); 
438
439         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
440         post_smfs_inode(dir, cache_dir);
441         
442         smfs_trans_commit(dir, handle, 0);
443 exit:
444         post_smfs_dentry(cache_dentry);
445         post_smfs_dentry(cache_parent);
446         RETURN(rc);
447 }
448
449 static int smfs_symlink(struct inode *dir, struct dentry *dentry,
450                         const char *symname)
451 {
452         struct inode *cache_dir = I2CI(dir);
453         struct inode *inode = NULL;
454         struct inode *parent = I2CI(dentry->d_parent->d_inode);
455         struct dentry *cache_dentry = NULL;
456         struct dentry *cache_parent = NULL;
457         void   *handle = NULL;
458         int    rc = 0;
459         struct hook_symlink_msg msg = {
460                 .dentry = dentry,
461                 .tgt_len = strlen(symname) + 1,
462                 .symname = (char*)symname
463         };
464
465         ENTRY;
466         
467         LASSERT(cache_dir);
468         LASSERT(cache_dir->i_op->symlink);
469         LASSERT(parent);
470
471         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
472         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
473         if (!cache_parent || !cache_dentry) {
474                 rc = -ENOMEM;
475                 goto exit;
476         }
477        
478         handle = smfs_trans_start(dir, FSFILT_OP_SYMLINK, NULL);
479         if (IS_ERR(handle)) {
480                 rc = -ENOSPC;
481                 goto exit;
482         }
483         
484         pre_smfs_inode(dir, cache_dir);
485
486         SMFS_PRE_HOOK(dir, HOOK_SYMLINK, &msg); 
487         
488         rc = cache_dir->i_op->symlink(cache_dir, cache_dentry, symname);
489         if (!rc) {        
490                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
491                                        dir, 0);
492                 if (inode) {
493                         d_instantiate(dentry, inode);
494                 }
495                 else
496                         rc = -ENOENT;
497         }
498         
499         SMFS_POST_HOOK(dir, HOOK_SYMLINK, &msg, rc);
500         
501         post_smfs_inode(dir, cache_dir);
502         smfs_trans_commit(dir, handle, 0);
503
504 exit:
505         post_smfs_dentry(cache_dentry);
506         post_smfs_dentry(cache_parent);
507         RETURN(rc);
508 }
509
510 static int smfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
511 {
512         struct inode *cache_dir = I2CI(dir);
513         struct inode *parent = I2CI(dentry->d_parent->d_inode);
514         struct inode *inode = NULL;
515         struct dentry *cache_dentry = NULL;
516         struct dentry *cache_parent = NULL;
517         void   *handle = NULL;
518         int    rc = 0;
519         struct hook_msg msg = {
520                 .dentry = dentry,
521         };
522
523         ENTRY;
524         
525         LASSERT(cache_dir);
526         LASSERT(parent);
527         
528         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
529         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
530         if (!cache_parent || !cache_dentry) {
531                 rc = -ENOMEM;
532                 goto exit;
533         }
534
535         handle = smfs_trans_start(dir, FSFILT_OP_MKDIR, NULL);
536         if (IS_ERR(handle)) {
537                 rc = -ENOSPC;
538                 goto exit;
539         }
540         
541         pre_smfs_inode(dir, cache_dir);
542         SMFS_PRE_HOOK(dir, HOOK_MKDIR, &msg); 
543         
544         rc = cache_dir->i_op->mkdir(cache_dir, cache_dentry, mode);
545         if (!rc) {
546                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
547                                        dir, 0);
548                 if (inode) {
549                         //smsf_update_dentry(dentry, cache_dentry);
550                         d_instantiate(dentry, inode);
551                 }
552                 else
553                         rc = -ENOENT;
554         }
555
556         SMFS_POST_HOOK(dir, HOOK_MKDIR, &msg, rc); 
557         post_smfs_inode(dir, cache_dir);
558         smfs_trans_commit(dir, handle, 0);
559
560 exit:
561         post_smfs_dentry(cache_dentry);
562         post_smfs_dentry(cache_parent);
563         RETURN(rc);
564 }
565
566 static int smfs_rmdir(struct inode *dir, struct dentry *dentry)
567 {
568         struct inode *cache_dir = I2CI(dir);
569         struct inode *cache_inode = I2CI(dentry->d_inode);
570         struct inode *parent = I2CI(dentry->d_parent->d_inode);
571         struct dentry *cache_dentry = NULL;
572         struct dentry *cache_parent = NULL;
573         void *handle = NULL;
574         int    rc = 0;
575         struct hook_unlink_msg msg = {
576                 .dentry = dentry,
577                 .mode = S_IFDIR
578         };
579
580         ENTRY;
581         
582         LASSERT(cache_dir);
583         LASSERT(cache_dir->i_op->rmdir);
584         LASSERT(parent);
585
586         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
587         cache_dentry = pre_smfs_dentry(cache_parent, cache_inode, dentry);
588         if (!cache_parent || !cache_dentry) {
589                 rc = -ENOMEM;
590                 goto exit;
591         }
592
593         handle = smfs_trans_start(dir, FSFILT_OP_RMDIR, NULL);
594         if (IS_ERR(handle) ) {
595                 rc = -ENOSPC;
596                 goto exit;
597         }
598         
599         pre_smfs_inode(dir, cache_dir);
600         pre_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
601         
602         SMFS_PRE_HOOK(dir, HOOK_RMDIR, &msg); 
603         
604         rc = cache_dir->i_op->rmdir(cache_dir, cache_dentry);
605               
606         SMFS_POST_HOOK(dir, HOOK_RMDIR, &msg, rc); 
607         
608         post_smfs_inode(dir, cache_dir);
609         post_smfs_inode(dentry->d_inode, cache_dentry->d_inode);
610         //like vfs_rmdir is doing with inode
611         if (!rc)
612                 cache_dentry->d_inode->i_flags |= S_DEAD;
613         
614         smfs_trans_commit(dir, handle, 0);
615
616 exit:
617         post_smfs_dentry(cache_dentry);
618         post_smfs_dentry(cache_parent);
619         RETURN(rc);
620 }
621
622 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
623 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
624                       int mode, int rdev)
625 #else
626 static int smfs_mknod(struct inode *dir, struct dentry *dentry,
627                       int mode, dev_t rdev)
628 #endif
629 {
630         struct inode *cache_dir = I2CI(dir);
631         struct inode *inode = NULL;
632         struct inode *parent = I2CI(dentry->d_parent->d_inode);
633         struct dentry *cache_dentry = NULL;
634         struct dentry *cache_parent = NULL;
635         void *handle = NULL;
636         int rc = 0;
637         struct hook_msg msg = {
638                 .dentry = dentry,
639         };
640  
641         ENTRY;
642         
643         LASSERT(parent);
644         LASSERT(cache_dir);
645         LASSERT(cache_dir->i_op->mknod);
646
647         cache_parent = pre_smfs_dentry(NULL, parent, dentry->d_parent);
648         cache_dentry = pre_smfs_dentry(cache_parent, NULL, dentry);
649         if (!cache_parent || !cache_dentry) {
650                 rc = -ENOMEM;
651                 goto exit;
652         }
653
654         handle = smfs_trans_start(dir, FSFILT_OP_MKNOD, NULL);
655         if (IS_ERR(handle)) {
656                 rc = -ENOSPC;
657                 goto exit;
658         }
659         
660         pre_smfs_inode(dir, cache_dir);
661         
662         SMFS_PRE_HOOK(dir, HOOK_MKNOD, &msg); 
663         
664         rc = cache_dir->i_op->mknod(cache_dir, cache_dentry, mode, rdev);
665         if (!rc) {
666                 inode = smfs_get_inode(dir->i_sb, cache_dentry->d_inode->i_ino,
667                                        dir, 0);
668                 if (inode) {
669                         //smsf_update_dentry(dentry, cache_dentry);
670                         d_instantiate(dentry, inode);
671                 }
672                 else
673                         rc = -ENOENT;
674         }
675
676         SMFS_POST_HOOK(dir, HOOK_MKNOD, &msg, rc); 
677         
678         post_smfs_inode(dir, cache_dir);
679         
680         smfs_trans_commit(dir, handle, 0);
681
682 exit:
683         post_smfs_dentry(cache_dentry);
684         post_smfs_dentry(cache_parent);
685         RETURN(rc);
686 }
687
688 static int smfs_rename(struct inode *old_dir, struct dentry *old_dentry,
689                        struct inode *new_dir,struct dentry *new_dentry)
690 {
691         struct inode *cache_old_dir = I2CI(old_dir);
692         struct inode *cache_new_dir = I2CI(new_dir);
693         struct inode *cache_old_inode = I2CI(old_dentry->d_inode);
694         struct inode *old_parent = I2CI(old_dentry->d_parent->d_inode);
695         struct inode *new_parent = I2CI(new_dentry->d_parent->d_inode);
696         struct inode *cache_new_inode = NULL;
697         struct dentry *cache_old_dentry = NULL;
698         struct dentry *cache_new_dentry = NULL;
699         struct dentry *cache_new_parent = NULL;
700         struct dentry *cache_old_parent = NULL;
701         void *handle = NULL;
702         int    rc = 0;
703         struct hook_rename_msg msg = {
704                 .dentry = old_dentry,
705                 .new_dir = new_dir,
706                 .new_dentry = new_dentry
707         };
708
709         ENTRY;
710                 
711         if (!cache_old_dir || !cache_new_dir || !cache_old_inode)
712                 RETURN(-ENOENT);
713
714         if (new_dentry->d_inode) {
715                 cache_new_inode = I2CI(new_dentry->d_inode);
716                 if (!cache_new_inode)
717                         RETURN(-ENOENT);
718         }
719         
720         cache_old_parent = pre_smfs_dentry(NULL, old_parent, old_dentry->d_parent);
721         cache_old_dentry = pre_smfs_dentry(cache_old_parent, cache_old_inode,
722                                            old_dentry);
723         if (!cache_old_parent || !cache_old_dentry) {
724                 rc = -ENOMEM;
725                 goto exit;
726         }
727         
728         cache_new_parent = pre_smfs_dentry(NULL, new_parent, new_dentry->d_parent);
729         cache_new_dentry = pre_smfs_dentry(cache_new_parent, cache_new_inode,
730                                            new_dentry);
731         if (!cache_new_parent || !cache_new_dentry) {
732                 rc = -ENOMEM;
733                 goto exit;
734         }
735
736         handle = smfs_trans_start(old_dir, FSFILT_OP_RENAME, NULL);
737         if (IS_ERR(handle)) {
738                 rc = -ENOSPC;
739                 goto exit;
740         }
741         
742         pre_smfs_inode(old_dir, cache_old_dir);
743         pre_smfs_inode(new_dir, cache_new_dir);
744         if (new_dentry->d_inode)
745                 pre_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
746
747         SMFS_PRE_HOOK(old_dir, HOOK_RENAME, &msg); 
748         
749         rc = cache_old_dir->i_op->rename(cache_old_dir, cache_old_dentry,
750                                          cache_new_dir, cache_new_dentry);
751         
752         SMFS_POST_HOOK(old_dir, HOOK_RENAME, &msg, rc); 
753
754         post_smfs_inode(old_dir, cache_old_dir);
755         post_smfs_inode(new_dir, cache_new_dir);
756         if (new_dentry->d_inode)
757                 post_smfs_inode(new_dentry->d_inode, cache_new_dentry->d_inode);
758         
759         smfs_trans_commit(old_dir, handle, 0);
760         
761 exit:
762         post_smfs_dentry(cache_old_dentry);
763         post_smfs_dentry(cache_old_parent);
764         post_smfs_dentry(cache_new_dentry);
765         post_smfs_dentry(cache_new_parent);
766         RETURN(rc);
767 }
768
769 struct inode_operations smfs_dir_iops = {
770         create:         smfs_create,
771         lookup:         smfs_lookup,
772         lookup_raw:     smfs_lookup_raw,
773         link:           smfs_link,              /* BKL held */
774         unlink:         smfs_unlink,            /* BKL held */
775         symlink:        smfs_symlink,           /* BKL held */
776         mkdir:          smfs_mkdir,             /* BKL held */
777         rmdir:          smfs_rmdir,             /* BKL held */
778         mknod:          smfs_mknod,             /* BKL held */
779         rename:         smfs_rename,            /* BKL held */
780         setxattr:       smfs_setxattr,          /* BKL held */
781         getxattr:       smfs_getxattr,          /* BKL held */
782         listxattr:      smfs_listxattr,         /* BKL held */
783         removexattr:    smfs_removexattr,       /* BKL held */
784 };
785
786 struct inode_operations smfs_iopen_iops = {
787         lookup:         smfs_iopen_lookup,
788 };
789
790 static ssize_t smfs_read_dir(struct file *filp, char *buf,
791                              size_t size, loff_t *ppos)
792 {
793         struct dentry *dentry = filp->f_dentry;
794         struct inode *cache_inode = NULL;
795         struct smfs_file_info *sfi = NULL;
796         loff_t tmp_ppos;
797         loff_t *cache_ppos = NULL;
798         int    rc = 0;
799
800         ENTRY;
801         
802         cache_inode = I2CI(dentry->d_inode);
803
804         if (!cache_inode || !cache_inode->i_fop->read)
805                 RETURN(-EINVAL);
806
807         sfi = F2SMFI(filp);
808         if (sfi->magic != SMFS_FILE_MAGIC)
809                 BUG();
810
811         if (ppos != &(filp->f_pos))
812                 cache_ppos = &tmp_ppos;
813         else
814                 cache_ppos = &sfi->c_file->f_pos;
815         
816         *cache_ppos = *ppos;
817
818         rc = cache_inode->i_fop->read(sfi->c_file, buf, size, cache_ppos);
819         if (rc)
820                 RETURN(rc);
821
822         *ppos = *cache_ppos;
823         
824         duplicate_file(filp, sfi->c_file);
825         
826         RETURN(rc);
827 }
828
829 static int smfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
830 {
831         struct dentry *dentry = filp->f_dentry;
832         struct inode *cache_inode = NULL;
833         struct smfs_file_info *sfi = NULL;
834         int    rc = 0;
835         struct hook_readdir_msg msg = {
836                 .dentry = dentry,
837                 .filp = filp,
838                 .dirent = dirent,
839                 .filldir = filldir
840         };
841
842         ENTRY;
843         
844         cache_inode = I2CI(dentry->d_inode);
845         if (!cache_inode || !cache_inode->i_fop->readdir)
846                 RETURN(-EINVAL);
847
848         sfi = F2SMFI(filp);
849         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
850
851         SMFS_PRE_HOOK(dentry->d_inode, HOOK_READDIR, &msg); 
852         
853         rc = cache_inode->i_fop->readdir(sfi->c_file, dirent, filldir);
854         
855         SMFS_POST_HOOK(dentry->d_inode, HOOK_READDIR, &msg, rc);
856         duplicate_file(filp, sfi->c_file);
857
858         RETURN(rc);
859 }
860
861 struct file_operations smfs_dir_fops = {
862         .read           = smfs_read_dir,
863         .readdir        = smfs_readdir,       /* BKL held */
864         .ioctl          = smfs_ioctl,         /* BKL held */
865         .fsync          = smfs_fsync,         /* BKL held */
866         .open           = smfs_open,
867         .release        = smfs_release,
868 };
869
870 struct file_operations smfs_iopen_fops = {
871         .read           = smfs_read_dir,
872 };
873