Whamcloud - gitweb
update .snap on smfs
[fs/lustre-release.git] / lustre / lvfs / fsfilt_smfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_smfs.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2004 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@clusterfs.com>
9  *
10  *   This file is part of Lustre, http://www.lustre.org.
11  *
12  *   Lustre is free software; you can redistribute it and/or
13  *   modify it under the terms of version 2 of the GNU General Public
14  *   License as published by the Free Software Foundation.
15  *
16  *   Lustre is distributed in the hope that it will be useful,
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *   GNU General Public License for more details.
20  *
21  *   You should have received a copy of the GNU General Public License
22  *   along with Lustre; if not, write to the Free Software
23  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #define DEBUG_SUBSYSTEM S_FILTER
27
28 #include <linux/fs.h>
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/version.h>
34 #include <linux/kp30.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_smfs.h>
37 #include <linux/obd.h>
38 #include <linux/obd_class.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41
42 #include <linux/lustre_snap.h>
43 #include <linux/lustre_smfs.h>
44 static void *fsfilt_smfs_start(struct inode *inode, int op,
45                                void *desc_private, int logs)
46 {
47         void *handle;
48         struct inode *cache_inode = I2CI(inode);
49         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
50
51         if (cache_fsfilt == NULL)
52                 return NULL;
53
54         SMFS_TRANS_OP(inode, op);
55
56         if (!cache_fsfilt->fs_start)
57                 return ERR_PTR(-ENOSYS);
58
59         handle = cache_fsfilt->fs_start(cache_inode, op, desc_private, logs);
60         return handle;
61 }
62
63 static void *fsfilt_smfs_brw_start(int objcount, struct fsfilt_objinfo *fso,
64                                    int niocount, struct niobuf_local *nb,
65                                    void *desc_private, int logs)
66 {
67         struct fsfilt_operations *cache_fsfilt;
68         struct dentry *cache_dentry = NULL;
69         struct inode *cache_inode = NULL;
70         struct fsfilt_objinfo cache_fso;
71         void   *rc = NULL;
72
73         ENTRY;
74         cache_fsfilt = I2FOPS(fso->fso_dentry->d_inode);
75         if (cache_fsfilt == NULL)
76                 return NULL;
77
78         cache_inode = I2CI(fso->fso_dentry->d_inode);
79         cache_dentry = pre_smfs_dentry(NULL, cache_inode, fso->fso_dentry);
80
81         if (!cache_dentry)
82                 GOTO(exit, rc = ERR_PTR(-ENOMEM));
83
84         cache_fso.fso_dentry = cache_dentry;
85         cache_fso.fso_bufcnt = fso->fso_bufcnt;
86
87         if (!cache_fsfilt->fs_brw_start)
88                 return ERR_PTR(-ENOSYS);
89
90         rc = cache_fsfilt->fs_brw_start(objcount, &cache_fso, niocount, nb,
91                                         desc_private, logs);
92 exit:
93         post_smfs_dentry(cache_dentry);
94         return rc;
95 }
96
97 /* FIXME-WANGDI: here we can easily have inode == NULL due to
98    mds_open() behavior. It passes NULL inode to mds_finish_transno()
99    sometimes. Probably we should have spare way to get cache fsfilt
100    operations. */
101 static int fsfilt_smfs_commit(struct super_block *sb, struct inode *inode, 
102                               void *h, int force_sync)
103 {
104         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
105         struct super_block *csb = S2CSB(sb); 
106         struct inode *cache_inode = NULL;
107         int    rc = -EIO;
108
109         if (inode)
110                 cache_inode = I2CI(inode);
111
112         if (cache_fsfilt == NULL)
113                 RETURN(rc);
114
115         if (!cache_fsfilt->fs_commit)
116                 RETURN(-ENOSYS);
117
118         rc = cache_fsfilt->fs_commit(csb, cache_inode, h, force_sync);
119
120         RETURN(rc);
121 }
122
123 static int fsfilt_smfs_commit_async(struct inode *inode, void *h,
124                                     void **wait_handle)
125 {
126         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
127         struct inode *cache_inode = NULL;
128         int    rc = -EIO;
129
130         cache_inode = I2CI(inode);
131         if (cache_fsfilt == NULL)
132                 RETURN(-EINVAL);
133
134         if (!cache_fsfilt->fs_commit_async)
135                 RETURN(-ENOSYS);
136
137         rc = cache_fsfilt->fs_commit_async(cache_inode, h, wait_handle);
138
139         RETURN(rc);
140 }
141
142 static int fsfilt_smfs_commit_wait(struct inode *inode, void *h)
143 {
144         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
145         struct inode *cache_inode = NULL;
146         int    rc = -EIO;
147
148         cache_inode = I2CI(inode);
149         if (cache_fsfilt == NULL)
150                 RETURN(-EINVAL);
151
152         if (!cache_fsfilt->fs_commit_wait)
153                 RETURN(-ENOSYS);
154
155         rc = cache_fsfilt->fs_commit_wait(cache_inode, h);
156
157         RETURN(rc);
158 }
159
160 static int fsfilt_smfs_setattr(struct dentry *dentry, void *handle,
161                                struct iattr *iattr, int do_trunc)
162 {
163         struct fsfilt_operations *cache_fsfilt = I2FOPS(dentry->d_inode);
164         struct dentry *cache_dentry = NULL;
165         struct inode *cache_inode = NULL;
166         int    rc = -EIO;
167
168         if (!cache_fsfilt)
169                 RETURN(rc);
170
171         cache_inode = I2CI(dentry->d_inode);
172
173         cache_dentry = pre_smfs_dentry(NULL, cache_inode, dentry);
174         if (!cache_dentry)
175                 GOTO(exit, rc = -ENOMEM);
176
177         pre_smfs_inode(dentry->d_inode, cache_inode);
178
179         if (!cache_fsfilt->fs_setattr)
180                 RETURN(-ENOSYS);
181
182         rc = cache_fsfilt->fs_setattr(cache_dentry, handle, iattr, do_trunc);
183
184         post_smfs_inode(dentry->d_inode, cache_inode);
185
186         if (rc == 0) {
187                 struct super_block *sb = dentry->d_inode->i_sb;
188
189                 if (SMFS_DO_REC(S2SMI(sb)) && 
190                     SMFS_DO_INODE_REC(dentry->d_inode)) {
191                         rc = smfs_rec_setattr(dentry->d_inode, dentry, iattr);
192                 }
193         }
194 exit:
195         post_smfs_dentry(cache_dentry);
196         RETURN(rc);
197 }
198
199 static int fsfilt_smfs_iocontrol(struct inode *inode, struct file *file,
200                                  unsigned int cmd, unsigned long arg)
201 {
202         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
203         struct inode *cache_inode = NULL;
204         struct smfs_file_info *sfi = NULL;
205         int    rc = -EIO;
206         ENTRY;
207
208         if (!cache_fsfilt)
209                 RETURN(rc);
210
211         cache_inode = I2CI(inode);
212
213         if (!cache_inode)
214                 RETURN(rc);
215
216         if (file != NULL) {
217                 sfi = F2SMFI(file);
218
219                 if (sfi->magic != SMFS_FILE_MAGIC)
220                         BUG();
221         } else {
222                 sfi = NULL;
223         }
224
225         if (!cache_fsfilt->fs_iocontrol)
226                 RETURN(-ENOSYS);
227
228         if (sfi) {
229                 rc = cache_fsfilt->fs_iocontrol(cache_inode, sfi->c_file, cmd,
230                                                 arg);
231         } else {
232                 rc = cache_fsfilt->fs_iocontrol(cache_inode, NULL, cmd, arg);
233         }
234
235         /* FIXME-UMKA: Should this be in duplicate_inode()? */
236         if (rc == 0 && cmd == EXT3_IOC_SETFLAGS)
237                 inode->i_flags = cache_inode->i_flags;
238
239         post_smfs_inode(inode, cache_inode);
240
241         RETURN(rc);
242 }
243
244 static int fsfilt_smfs_set_md(struct inode *inode, void *handle,
245                               void *lmm, int lmm_size)
246 {
247         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
248         struct inode *cache_inode = NULL;
249         int    rc = -EIO;
250
251         if (!cache_fsfilt)
252                 RETURN(-EINVAL);
253
254         cache_inode = I2CI(inode);
255
256         if (!cache_inode)
257                 RETURN(-ENOENT);
258
259         pre_smfs_inode(inode, cache_inode);
260
261         if (!cache_fsfilt->fs_set_md)
262                 RETURN(-ENOSYS);
263
264         down(&cache_inode->i_sem);
265         rc = cache_fsfilt->fs_set_md(cache_inode, handle, lmm, lmm_size);
266         up(&cache_inode->i_sem);
267
268         post_smfs_inode(inode, cache_inode);
269
270         smfs_rec_md(inode, lmm, lmm_size);
271
272         RETURN(rc);
273 }
274
275 /* Must be called with i_sem held */
276 static int fsfilt_smfs_get_md(struct inode *inode, void *lmm, int lmm_size)
277 {
278         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
279         struct inode *cache_inode = NULL;
280         int    rc = -EIO;
281
282         if (!cache_fsfilt)
283                 RETURN(-EINVAL);
284
285         cache_inode = I2CI(inode);
286
287         if (!cache_inode)
288                 RETURN(-ENOENT);
289
290         pre_smfs_inode(inode, cache_inode);
291
292         if (!cache_fsfilt->fs_get_md)
293                 RETURN(-ENOSYS);
294
295         down(&cache_inode->i_sem);
296         rc = cache_fsfilt->fs_get_md(cache_inode, lmm, lmm_size);
297         up(&cache_inode->i_sem);
298
299         post_smfs_inode(inode, cache_inode);
300
301         RETURN(rc);
302 }
303
304 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
305 static int fsfilt_smfs_send_bio(struct inode *inode, struct bio *bio)
306 #else
307 static int fsfilt_smfs_send_bio(struct inode *inode, struct kiobuf *bio)
308 #endif
309 {
310         struct inode *cache_inode;
311         struct fsfilt_operations *cache_fsfilt;
312
313         cache_fsfilt = I2FOPS(inode);
314         if (!cache_fsfilt)
315                 RETURN(-EINVAL);
316
317         cache_inode = I2CI(inode);
318         if (!cache_inode)
319                 RETURN(-EINVAL);
320
321         if (!cache_fsfilt->fs_send_bio)
322                 RETURN(-ENOSYS);
323
324         return cache_fsfilt->fs_send_bio(cache_inode, bio);
325 }
326
327 static struct page *
328 fsfilt_smfs_getpage(struct inode *inode, long int index)
329 {
330         struct  fsfilt_operations *cache_fsfilt;
331         struct  inode *cache_inode;
332
333         cache_fsfilt = I2FOPS(inode);
334         if (!cache_fsfilt)
335                 RETURN(ERR_PTR(-EINVAL));
336
337         cache_inode = I2CI(inode);
338         if (!cache_inode)
339                 RETURN(ERR_PTR(-EINVAL));
340
341         if (!cache_fsfilt->fs_getpage)
342                 RETURN(ERR_PTR(-ENOSYS));
343 #if CONFIG_SNAPFS
344         if (SMFS_DO_COW(S2SMI(inode->i_sb))) {
345                 struct address_space_operations *aops = 
346                                 cache_inode->i_mapping->a_ops;
347                 if (aops->bmap(cache_inode->i_mapping, index)) {
348                         struct inode *ind_inode = NULL;
349                         struct inode *cache_ind = NULL;
350                         struct page  *page = NULL;
351                         
352                         ind_inode = smfs_cow_get_ind(inode, index);
353                         if (!ind_inode) {
354                                 RETURN(ERR_PTR(-EIO));
355                         }
356                         cache_ind = I2CI(ind_inode);
357                         /*FIXME cow inode should be bottom fs inode */         
358                         page = cache_fsfilt->fs_getpage(cache_ind, index);
359                         iput(ind_inode); 
360                         RETURN(page);
361                 } 
362         }
363 #endif
364         return cache_fsfilt->fs_getpage(cache_inode, index);
365 }
366
367 static ssize_t fsfilt_smfs_readpage(struct file *file, char *buf,
368                                     size_t count, loff_t *off)
369 {
370         struct fsfilt_operations *cache_fsfilt;
371         struct smfs_file_info *sfi;
372         struct inode *cache_inode;
373         loff_t tmp_ppos;
374         loff_t *cache_ppos;
375         ssize_t rc = -EIO;
376
377         ENTRY;
378
379         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
380         if (!cache_fsfilt)
381                 RETURN(rc);
382
383         cache_inode = I2CI(file->f_dentry->d_inode);
384         if (!cache_inode)
385                 RETURN(rc);
386
387         sfi = F2SMFI(file);
388         if (sfi->magic != SMFS_FILE_MAGIC)
389                 BUG();
390
391         if (off != &(file->f_pos))
392                 cache_ppos = &tmp_ppos;
393         else
394                 cache_ppos = &sfi->c_file->f_pos;
395         *cache_ppos = *off;
396
397         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
398 #if CONFIG_SNAPFS
399         /*readdir page*/
400         if (smfs_dotsnap_inode(file->f_dentry->d_inode)) {
401                 struct fsfilt_operations *snapops = 
402                                         I2SNAPOPS(file->f_dentry->d_inode);
403                 
404                 LASSERT(S_ISDIR(file->f_dentry->d_inode->i_mode));
405                 
406                 rc = snapops->fs_read_dotsnap_dir_page(sfi->c_file, buf, count, 
407                                                        cache_ppos); 
408         } else {
409                 if (cache_fsfilt->fs_readpage)
410                         rc = cache_fsfilt->fs_readpage(sfi->c_file, buf, count,
411                                                        cache_ppos);
412         }
413 #else
414         if (cache_fsfilt->fs_readpage)
415                 rc = cache_fsfilt->fs_readpage(sfi->c_file, buf, count,
416                                                cache_ppos);
417
418 #endif
419         *off = *cache_ppos;
420         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
421         duplicate_file(file, sfi->c_file);
422
423         RETURN(rc);
424 }
425
426
427 static int fsfilt_smfs_add_journal_cb(struct obd_device *obd,
428                                       struct super_block *sb, __u64 last_rcvd,
429                                       void *handle, fsfilt_cb_t cb_func,
430                                       void *cb_data)
431 {
432         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
433         struct super_block *csb = S2CSB(sb);
434         int rc = -EIO;
435
436         if (!cache_fsfilt)
437                  RETURN(rc);
438         if (cache_fsfilt->fs_add_journal_cb)
439                 rc = cache_fsfilt->fs_add_journal_cb(obd, csb, last_rcvd,
440                                                      handle, cb_func, cb_data);
441         RETURN(rc);
442 }
443
444 static int fsfilt_smfs_statfs(struct super_block *sb, struct obd_statfs *osfs)
445 {
446         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
447         struct super_block *csb = S2CSB(sb);
448         int rc = -EIO;
449
450         if (!cache_fsfilt)
451                 RETURN(rc);
452
453         if (!cache_fsfilt->fs_statfs)
454                 RETURN(-ENOSYS);
455
456         rc = cache_fsfilt->fs_statfs(csb, osfs);
457         duplicate_sb(csb, sb);
458
459         RETURN(rc);
460 }
461
462 static int fsfilt_smfs_sync(struct super_block *sb)
463 {
464         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
465         struct super_block *csb = S2CSB(sb);
466         int    rc = -EIO;
467
468         if (!cache_fsfilt)
469                 RETURN(-EINVAL);
470
471         if (!cache_fsfilt->fs_sync)
472                 RETURN(-ENOSYS);
473
474         rc = cache_fsfilt->fs_sync(csb);
475
476         RETURN(rc);
477 }
478
479 int fsfilt_smfs_map_inode_pages(struct inode *inode, struct page **page,
480                                 int pages, unsigned long *blocks, int *created,
481                                 int create, struct semaphore *sem)
482 {
483         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
484         struct  inode *cache_inode = NULL;
485         int     rc = -EIO;
486
487         if (!cache_fsfilt)
488                 RETURN(-EINVAL);
489
490         cache_inode = I2CI(inode);
491
492         if (!cache_inode)
493                 RETURN(rc);
494
495         if (!cache_fsfilt->fs_map_inode_pages)
496                 RETURN(-ENOSYS);
497
498         down(&cache_inode->i_sem);
499         rc = cache_fsfilt->fs_map_inode_pages(cache_inode, page, pages, blocks,
500                                               created, create, sem);
501         up(&cache_inode->i_sem);
502
503         RETURN(rc);
504 }
505
506 static int fsfilt_smfs_prep_san_write(struct inode *inode, long *blocks,
507                                       int nblocks, loff_t newsize)
508 {
509         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
510         struct  inode *cache_inode = NULL;
511         int     rc = -EIO;
512
513         if (!cache_fsfilt)
514                 RETURN(-EINVAL);
515
516         cache_inode = I2CI(inode);
517
518         if (!cache_inode)
519                 RETURN(-EINVAL);
520
521         if (!cache_fsfilt->fs_prep_san_write)
522                 RETURN(-ENOSYS);
523
524         down(&cache_inode->i_sem);
525         rc = cache_fsfilt->fs_prep_san_write(cache_inode, blocks, nblocks,
526                                              newsize);
527         up(&cache_inode->i_sem);
528
529         RETURN(rc);
530 }
531
532 static int fsfilt_smfs_read_record(struct file * file, void *buf,
533                                    int size, loff_t *offs)
534 {
535         struct  fsfilt_operations *cache_fsfilt;
536         struct  inode *cache_inode;
537         struct  smfs_file_info *sfi;
538         loff_t  tmp_ppos;
539         loff_t  *cache_ppos;
540         ssize_t rc;
541
542         ENTRY;
543         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
544         if (!cache_fsfilt)
545                 RETURN(-EINVAL);
546
547         cache_inode = I2CI(file->f_dentry->d_inode);
548
549         if (!cache_inode)
550                 RETURN(-EINVAL);
551
552         sfi = F2SMFI(file);
553         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
554
555         if (offs != &(file->f_pos))
556                 cache_ppos = &tmp_ppos;
557         else
558                 cache_ppos = &sfi->c_file->f_pos;
559         *cache_ppos = *offs;
560
561         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
562
563         if (!cache_fsfilt->fs_read_record)
564                 RETURN(-ENOSYS);
565
566         rc = cache_fsfilt->fs_read_record(sfi->c_file, buf, size, cache_ppos);
567
568         *offs = *cache_ppos;
569         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
570         duplicate_file(file, sfi->c_file);
571
572         RETURN(rc);
573 }
574
575 static int fsfilt_smfs_write_record(struct file *file, void *buf, int bufsize,
576                                     loff_t *offs, int force_sync)
577 {
578         struct  fsfilt_operations *cache_fsfilt;
579         struct  inode *cache_inode;
580         struct  smfs_file_info *sfi;
581         loff_t  tmp_ppos;
582         loff_t  *cache_ppos;
583         ssize_t rc = -EIO;
584
585         ENTRY;
586
587         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
588         if (!cache_fsfilt)
589                 RETURN(-EINVAL);
590
591         cache_inode = I2CI(file->f_dentry->d_inode);
592
593         if (!cache_inode)
594                 RETURN(-EINVAL);
595
596         sfi = F2SMFI(file);
597         if (sfi->magic != SMFS_FILE_MAGIC)
598                 BUG();
599
600         if (offs != &(file->f_pos))
601                 cache_ppos = &tmp_ppos;
602         else
603                 cache_ppos = &sfi->c_file->f_pos;
604         *cache_ppos = *offs;
605
606         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
607
608         if (!cache_fsfilt->fs_write_record)
609                 RETURN(-ENOSYS);
610
611         rc = cache_fsfilt->fs_write_record(sfi->c_file, buf,
612                                            bufsize, cache_ppos, force_sync);
613         *offs = *cache_ppos;
614         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
615         duplicate_file(file, sfi->c_file);
616
617         RETURN(rc);
618 }
619
620 static int fsfilt_smfs_post_setup(struct obd_device *obd, struct vfsmount *mnt,
621                                   struct dentry *root_dentry)
622 {
623         struct super_block *sb = NULL;
624         int rc = 0;
625
626         if (mnt) {
627                 sb = mnt->mnt_sb;
628                 S2SMI(sb)->smsi_exp = obd->obd_self_export;
629                 smfs_post_setup(sb, mnt);
630                 if (SMFS_DO_REC(S2SMI(sb)))
631                         rc = smfs_start_rec(sb, mnt);
632                 if (rc)
633                         GOTO(exit, rc);
634                 if (obd)
635                         obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT] =
636                                         S2SMI(sb)->smsi_rec_log;
637         }
638 exit:
639         if (rc)
640                 CERROR("can not do post setup in obd %p rc=%d", obd, rc);
641
642         RETURN(rc);
643 }
644
645 static int fsfilt_smfs_post_cleanup(struct obd_device *obd,
646                                     struct vfsmount *mnt)
647 {
648         struct super_block *sb = NULL;
649         int rc = 0;
650         ENTRY;
651         
652         if (mnt) {
653                 sb = mnt->mnt_sb;
654                 if (SMFS_DO_REC(S2SMI(sb)))
655                         rc = smfs_stop_rec(sb);
656                 smfs_post_cleanup(sb);
657         }
658         RETURN(rc);
659 }
660
661 static int fsfilt_smfs_set_fs_flags(struct inode *inode, int flags)
662 {
663         int rc = 0;
664         ENTRY;
665
666         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
667                 SMFS_SET_INODE_REC(inode);
668         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
669                 SMFS_SET_INODE_COW(inode);
670         RETURN(rc);
671 }
672
673 static int fsfilt_smfs_clear_fs_flags(struct inode *inode, int flags)
674 {
675         int rc = 0;
676         ENTRY;
677         
678         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
679                 SMFS_CLEAN_INODE_REC(inode);
680         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
681                 SMFS_CLEAN_INODE_COW(inode);
682         RETURN(rc);
683 }
684
685 static int fsfilt_smfs_get_fs_flags(struct dentry *de)
686 {
687         struct inode *inode = de->d_inode;
688         int flags = 0;
689         ENTRY;
690
691         LASSERT(inode);
692
693         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && SMFS_DO_INODE_REC(inode))
694                 flags |= SM_DO_REC;
695         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && SMFS_DO_INODE_COW(inode))
696                 flags |= SM_DO_COW;
697        
698         RETURN(flags); 
699 }
700 static int fsfilt_smfs_set_ost_flags(struct super_block *sb)
701 {
702         int rc = 0;
703         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_OST);
704         RETURN(rc);
705 }
706
707 static int fsfilt_smfs_set_mds_flags(struct super_block *sb)
708 {
709         int rc = 0;
710         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_MDS);
711         RETURN(rc);
712 }
713
714 static int fsfilt_smfs_get_reint_log_ctxt(struct super_block *sb,
715                                           struct llog_ctxt **ctxt)
716 {
717         struct smfs_super_info *smfs_info = S2SMI(sb);
718         int rc = 0;
719
720         *ctxt = smfs_info->smsi_rec_log;
721         RETURN(rc);
722 }
723
724 static int fsfilt_smfs_setup(struct obd_device *obd, struct super_block *sb)
725 {
726         struct smfs_super_info *smfs_info = S2SMI(sb);
727         struct fsfilt_operations *cache_fsfilt;
728         struct super_block *csb;
729         int rc = 0;
730
731         /* It should be initialized olready by smfs_read_super(). */
732         if (!(cache_fsfilt = smfs_info->sm_cache_fsfilt))
733                     cache_fsfilt = fsfilt_get_ops(smfs_info->smsi_cache_ftype);
734
735         if (!cache_fsfilt)
736                 RETURN(-ENOENT);
737
738         csb = S2CSB(sb);
739         if (cache_fsfilt->fs_setup) 
740                 rc = cache_fsfilt->fs_setup(obd, csb);
741         
742         duplicate_sb(sb, csb);
743         
744         RETURN(rc);
745 }
746
747 static int fsfilt_smfs_set_xattr(struct inode *inode, void *handle, char *name,
748                                  void *buffer, int buffer_size)
749 {
750         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
751         struct  inode *cache_inode = NULL;
752         int     rc = -EIO;
753
754         if (!cache_fsfilt)
755                 RETURN(rc);
756
757         cache_inode = I2CI(inode);
758         if (!cache_inode)
759                 RETURN(rc);
760
761         pre_smfs_inode(inode, cache_inode);
762
763         if (cache_fsfilt->fs_set_xattr)
764                 rc = cache_fsfilt->fs_set_xattr(cache_inode, handle, name,
765                                                 buffer, buffer_size);
766         post_smfs_inode(inode, cache_inode);
767
768         RETURN(rc);
769 }
770
771 static int fsfilt_smfs_get_xattr(struct inode *inode, char *name,
772                                  void *buffer, int buffer_size)
773 {
774         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
775         struct inode *cache_inode = NULL;
776         int    rc = -EIO;
777
778         if (!cache_fsfilt)
779                 RETURN(rc);
780
781         cache_inode = I2CI(inode);
782         if (!cache_inode)
783                 RETURN(rc);
784
785         pre_smfs_inode(inode, cache_inode);
786
787         if (cache_fsfilt->fs_get_xattr)
788                 rc = cache_fsfilt->fs_get_xattr(cache_inode, name,
789                                                 buffer, buffer_size);
790         post_smfs_inode(inode, cache_inode);
791
792         RETURN(rc);
793 }
794
795 static int fsfilt_smfs_insert_extents_ea(struct inode *inode,
796                                          unsigned long from, unsigned long num)
797 {
798         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
799         struct inode *cache_inode = NULL;
800         int    rc = -EIO;
801
802         if (!cache_fsfilt)
803                 RETURN(rc);
804
805         cache_inode = I2CI(inode);
806         if (!cache_inode)
807                 RETURN(rc);
808
809         pre_smfs_inode(inode, cache_inode);
810
811         if (cache_fsfilt->fs_insert_extents_ea)
812                 rc = cache_fsfilt->fs_insert_extents_ea(cache_inode, from, num);
813
814         post_smfs_inode(inode, cache_inode);
815         return rc;
816 }
817
818 static int fsfilt_smfs_remove_extents_ea(struct inode *inode,
819                                          unsigned long from, unsigned long num)
820 {
821         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
822         struct inode *cache_inode = NULL;
823         int    rc = -EIO;
824
825         if (!cache_fsfilt)
826                 RETURN(rc);
827
828         cache_inode = I2CI(inode);
829         if (!cache_inode)
830                 RETURN(rc);
831
832         pre_smfs_inode(inode, cache_inode);
833
834         if (cache_fsfilt->fs_remove_extents_ea)
835                 rc = cache_fsfilt->fs_remove_extents_ea(cache_inode, from, num);
836
837         post_smfs_inode(inode, cache_inode);
838         return rc;
839 }
840
841 static int fsfilt_smfs_init_extents_ea(struct inode *inode)
842 {
843         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
844         struct inode *cache_inode = NULL;
845         int    rc = -EIO;
846
847         if (!cache_fsfilt)
848                 RETURN(rc);
849
850         cache_inode = I2CI(inode);
851         if (!cache_inode)
852                 RETURN(rc);
853
854         pre_smfs_inode(inode, cache_inode);
855
856         if (cache_fsfilt->fs_init_extents_ea)
857                 rc = cache_fsfilt->fs_init_extents_ea(cache_inode);
858
859         post_smfs_inode(inode, cache_inode);
860         return rc;
861 }
862
863 static int fsfilt_smfs_free_extents(struct super_block *sb, ino_t ino,
864                                     char *pbuf, int size)
865 {
866         OBD_FREE(pbuf, size * (sizeof(struct ldlm_extent)));
867         return 0;
868 }
869
870 static int fsfilt_smfs_write_extents(struct dentry *dentry,
871                                      unsigned long from, unsigned long num)
872 {
873         int rc = 0;
874
875         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
876                 rc = smfs_write_extents(dentry->d_inode, dentry, from, num);
877
878         return rc;
879 }
880
881 static int fsfilt_smfs_precreate_rec(struct dentry *dentry, int *count, 
882                                      struct obdo *oa)
883 {
884         int rc = 0;
885
886         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
887                 rc = smfs_rec_precreate(dentry, count, oa);
888
889         return rc;
890 }
891
892 static int fsfilt_smfs_get_ino_write_extents(struct super_block *sb, ino_t ino,
893                                              char **pbuf, int *size)
894 {
895         struct fs_extent *fs_extents;
896         struct ldlm_extent *extents = NULL;
897         struct inode *inode;
898         struct inode *cache_inode;
899         struct fsfilt_operations *cache_fsfilt = NULL;
900         struct lvfs_run_ctxt saved;
901         int    rc = 0, fs_ex_size, ex_num, flags;
902         char   *buf = NULL, *ex_buf = NULL;
903
904         push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
905
906         inode = iget(sb, ino);
907
908         if (!inode || is_bad_inode(inode)) {
909                 CWARN("Can not get inode %lu ino\n", ino);
910                 GOTO(out, rc = 0);
911         }
912         cache_inode = I2CI(inode);
913         cache_fsfilt = I2FOPS(inode);
914
915         rc = cache_fsfilt->fs_get_xattr(cache_inode, REINT_EXTENTS_FLAGS,
916                                         &flags, sizeof(int));
917         if (!(flags & SMFS_OVER_WRITE) && !(flags & SMFS_DIRTY_WRITE)) {
918                 GOTO(out, rc = 0);
919         } else if (flags & SMFS_OVER_WRITE) {
920                 *size = 1;
921                 OBD_ALLOC(ex_buf, sizeof(struct ldlm_extent));
922                 if (!ex_buf)
923                         GOTO(out, rc=-ENOMEM);
924                 extents = (struct ldlm_extent*)(ex_buf);
925                 extents->start = 0;
926                 extents->end = 0xffffffff;
927         }
928         if (rc < 0)
929                 GOTO(out, rc);
930         rc = cache_fsfilt->fs_get_write_extents_num(cache_inode, &fs_ex_size);
931         if (rc)
932                 GOTO(out, rc);
933         OBD_ALLOC(buf, fs_ex_size);
934         if (!buf)
935                 GOTO(out, rc=-ENOMEM);
936
937         rc = cache_fsfilt->fs_get_inode_write_extents(cache_inode, &buf,
938                                                       &fs_ex_size);
939         if (rc < 0)
940                 GOTO(out, rc);
941         rc = 0;
942         ex_num = fs_ex_size / sizeof(struct fs_extent);
943         *size =  ex_num;
944         OBD_ALLOC(ex_buf, ex_num* sizeof(struct ldlm_extent));
945         if (!ex_buf)
946                 GOTO(out, rc=-ENOMEM);
947
948         fs_extents = (struct fs_extent*)(buf);
949         extents = (struct ldlm_extent*)(ex_buf);
950         while (ex_num > 0) {
951                 int blk_size = I2CI(inode)->i_blksize;
952
953                 extents->start = fs_extents->e_block * blk_size;
954                 extents->end = extents->start + fs_extents->e_num * blk_size;
955                 fs_extents++;
956                 extents++;
957                 ex_num--;
958         }
959         *pbuf = ex_buf;
960 out:
961         iput(inode);
962         if (buf)
963                 OBD_FREE(buf, fs_ex_size);
964         if (rc && extents)
965                 OBD_FREE(ex_buf, (*size) * (sizeof(struct ldlm_extent)));
966         pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
967         return rc;
968 }
969
970 static int fsfilt_smfs_set_snap_item(struct super_block *sb, char *name)
971 {
972         int rc = 0;
973
974         ENTRY;
975 #if CONFIG_SNAPFS
976 #warning "still not implement for add snap item -wangdi"         
977 #endif
978         RETURN(rc);        
979 }
980 static int fsfilt_smfs_do_write_cow(struct dentry *de, void *extents,
981                                     int num_extents)
982 {
983         int rc = 0;
984 #if CONFIG_SNAPFS
985         struct write_extents *w_ext = (struct write_extents *)extents;
986         int i = 0;
987         ENTRY;
988         for (i = 0; i < num_extents; i++) {
989                size_t count = w_ext->w_count;
990                loff_t off = w_ext->w_pos;
991                rc = smfs_cow_write(de->d_inode, de, &count, &off);
992                if (rc)
993                         RETURN(rc);  
994                w_ext ++;
995         }
996 #endif
997         RETURN(rc);
998 }
999 static struct fsfilt_operations fsfilt_smfs_ops = {
1000         .fs_type                = "smfs",
1001         .fs_owner               = THIS_MODULE,
1002         .fs_start               = fsfilt_smfs_start,
1003         .fs_brw_start           = fsfilt_smfs_brw_start,
1004         .fs_commit              = fsfilt_smfs_commit,
1005         .fs_commit_async        = fsfilt_smfs_commit_async,
1006         .fs_commit_wait         = fsfilt_smfs_commit_wait,
1007         .fs_setattr             = fsfilt_smfs_setattr,
1008         .fs_iocontrol           = fsfilt_smfs_iocontrol,
1009         .fs_set_md              = fsfilt_smfs_set_md,
1010         .fs_get_md              = fsfilt_smfs_get_md,
1011         .fs_readpage            = fsfilt_smfs_readpage,
1012         .fs_getpage             = fsfilt_smfs_getpage,
1013         .fs_add_journal_cb      = fsfilt_smfs_add_journal_cb,
1014         .fs_statfs              = fsfilt_smfs_statfs,
1015         .fs_sync                = fsfilt_smfs_sync,
1016         .fs_map_inode_pages     = fsfilt_smfs_map_inode_pages,
1017         .fs_prep_san_write      = fsfilt_smfs_prep_san_write,
1018         .fs_write_record        = fsfilt_smfs_write_record,
1019         .fs_read_record         = fsfilt_smfs_read_record,
1020         .fs_setup               = fsfilt_smfs_setup,
1021         .fs_post_setup          = fsfilt_smfs_post_setup,
1022         .fs_post_cleanup        = fsfilt_smfs_post_cleanup,
1023         .fs_set_fs_flags       = fsfilt_smfs_set_fs_flags,
1024         .fs_clear_fs_flags     = fsfilt_smfs_clear_fs_flags,
1025         .fs_get_fs_flags       = fsfilt_smfs_get_fs_flags,
1026         .fs_set_ost_flags       = fsfilt_smfs_set_ost_flags,
1027         .fs_set_mds_flags       = fsfilt_smfs_set_mds_flags,
1028         .fs_precreate_rec       = fsfilt_smfs_precreate_rec,
1029         .fs_get_reint_log_ctxt  = fsfilt_smfs_get_reint_log_ctxt,
1030         .fs_send_bio            = fsfilt_smfs_send_bio,
1031         .fs_set_xattr           = fsfilt_smfs_set_xattr,
1032         .fs_get_xattr           = fsfilt_smfs_get_xattr,
1033         .fs_init_extents_ea     = fsfilt_smfs_init_extents_ea,
1034         .fs_insert_extents_ea   = fsfilt_smfs_insert_extents_ea,
1035         .fs_remove_extents_ea   = fsfilt_smfs_remove_extents_ea,
1036         .fs_get_ino_write_extents = fsfilt_smfs_get_ino_write_extents,
1037         .fs_free_write_extents  = fsfilt_smfs_free_extents,
1038         .fs_write_extents       = fsfilt_smfs_write_extents,
1039         .fs_set_snap_item       = fsfilt_smfs_set_snap_item,
1040         .fs_do_write_cow        = fsfilt_smfs_do_write_cow,
1041         /* FIXME-UMKA: probably fsfilt_smfs_get_op_len() should be
1042          * put here too. */
1043 };
1044
1045 static int __init fsfilt_smfs_init(void)
1046 {
1047         int rc;
1048
1049         rc = fsfilt_register_ops(&fsfilt_smfs_ops);
1050         return rc;
1051 }
1052
1053 static void __exit fsfilt_smfs_exit(void)
1054 {
1055         fsfilt_unregister_ops(&fsfilt_smfs_ops);
1056 }
1057
1058 module_init(fsfilt_smfs_init);
1059 module_exit(fsfilt_smfs_exit);
1060
1061 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1062 MODULE_DESCRIPTION("Lustre SMFS Filesystem Helper v0.1");
1063 MODULE_LICENSE("GPL");