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