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