Whamcloud - gitweb
Update snapfs: 1) some fix on clonefs read in lustre
[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
399         if (cache_fsfilt->fs_readpage)
400                 rc = cache_fsfilt->fs_readpage(sfi->c_file, buf, count,
401                                                cache_ppos);
402
403         *off = *cache_ppos;
404         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
405         duplicate_file(file, sfi->c_file);
406
407         RETURN(rc);
408 }
409
410
411 static int fsfilt_smfs_add_journal_cb(struct obd_device *obd,
412                                       struct super_block *sb, __u64 last_rcvd,
413                                       void *handle, fsfilt_cb_t cb_func,
414                                       void *cb_data)
415 {
416         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
417         struct super_block *csb = S2CSB(sb);
418         int rc = -EIO;
419
420         if (!cache_fsfilt)
421                  RETURN(rc);
422         if (cache_fsfilt->fs_add_journal_cb)
423                 rc = cache_fsfilt->fs_add_journal_cb(obd, csb, last_rcvd,
424                                                      handle, cb_func, cb_data);
425         RETURN(rc);
426 }
427
428 static int fsfilt_smfs_statfs(struct super_block *sb, struct obd_statfs *osfs)
429 {
430         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
431         struct super_block *csb = S2CSB(sb);
432         int rc = -EIO;
433
434         if (!cache_fsfilt)
435                 RETURN(rc);
436
437         if (!cache_fsfilt->fs_statfs)
438                 RETURN(-ENOSYS);
439
440         rc = cache_fsfilt->fs_statfs(csb, osfs);
441         duplicate_sb(csb, sb);
442
443         RETURN(rc);
444 }
445
446 static int fsfilt_smfs_sync(struct super_block *sb)
447 {
448         struct fsfilt_operations *cache_fsfilt = S2SMI(sb)->sm_cache_fsfilt;
449         struct super_block *csb = S2CSB(sb);
450         int    rc = -EIO;
451
452         if (!cache_fsfilt)
453                 RETURN(-EINVAL);
454
455         if (!cache_fsfilt->fs_sync)
456                 RETURN(-ENOSYS);
457
458         rc = cache_fsfilt->fs_sync(csb);
459
460         RETURN(rc);
461 }
462
463 int fsfilt_smfs_map_inode_pages(struct inode *inode, struct page **page,
464                                 int pages, unsigned long *blocks, int *created,
465                                 int create, struct semaphore *sem)
466 {
467         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
468         struct  inode *cache_inode = NULL;
469         int     rc = -EIO;
470
471         if (!cache_fsfilt)
472                 RETURN(-EINVAL);
473
474         cache_inode = I2CI(inode);
475
476         if (!cache_inode)
477                 RETURN(rc);
478
479         if (!cache_fsfilt->fs_map_inode_pages)
480                 RETURN(-ENOSYS);
481
482         down(&cache_inode->i_sem);
483         rc = cache_fsfilt->fs_map_inode_pages(cache_inode, page, pages, blocks,
484                                               created, create, sem);
485         up(&cache_inode->i_sem);
486
487         RETURN(rc);
488 }
489
490 static int fsfilt_smfs_prep_san_write(struct inode *inode, long *blocks,
491                                       int nblocks, loff_t newsize)
492 {
493         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
494         struct  inode *cache_inode = NULL;
495         int     rc = -EIO;
496
497         if (!cache_fsfilt)
498                 RETURN(-EINVAL);
499
500         cache_inode = I2CI(inode);
501
502         if (!cache_inode)
503                 RETURN(-EINVAL);
504
505         if (!cache_fsfilt->fs_prep_san_write)
506                 RETURN(-ENOSYS);
507
508         down(&cache_inode->i_sem);
509         rc = cache_fsfilt->fs_prep_san_write(cache_inode, blocks, nblocks,
510                                              newsize);
511         up(&cache_inode->i_sem);
512
513         RETURN(rc);
514 }
515
516 static int fsfilt_smfs_read_record(struct file * file, void *buf,
517                                    int size, loff_t *offs)
518 {
519         struct  fsfilt_operations *cache_fsfilt;
520         struct  inode *cache_inode;
521         struct  smfs_file_info *sfi;
522         loff_t  tmp_ppos;
523         loff_t  *cache_ppos;
524         ssize_t rc;
525
526         ENTRY;
527         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
528         if (!cache_fsfilt)
529                 RETURN(-EINVAL);
530
531         cache_inode = I2CI(file->f_dentry->d_inode);
532
533         if (!cache_inode)
534                 RETURN(-EINVAL);
535
536         sfi = F2SMFI(file);
537         if (sfi->magic != SMFS_FILE_MAGIC) BUG();
538
539         if (offs != &(file->f_pos))
540                 cache_ppos = &tmp_ppos;
541         else
542                 cache_ppos = &sfi->c_file->f_pos;
543         *cache_ppos = *offs;
544
545         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
546
547         if (!cache_fsfilt->fs_read_record)
548                 RETURN(-ENOSYS);
549
550         rc = cache_fsfilt->fs_read_record(sfi->c_file, buf, size, cache_ppos);
551
552         *offs = *cache_ppos;
553         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
554         duplicate_file(file, sfi->c_file);
555
556         RETURN(rc);
557 }
558
559 static int fsfilt_smfs_write_record(struct file *file, void *buf, int bufsize,
560                                     loff_t *offs, int force_sync)
561 {
562         struct  fsfilt_operations *cache_fsfilt;
563         struct  inode *cache_inode;
564         struct  smfs_file_info *sfi;
565         loff_t  tmp_ppos;
566         loff_t  *cache_ppos;
567         ssize_t rc = -EIO;
568
569         ENTRY;
570
571         cache_fsfilt = I2FOPS(file->f_dentry->d_inode);
572         if (!cache_fsfilt)
573                 RETURN(-EINVAL);
574
575         cache_inode = I2CI(file->f_dentry->d_inode);
576
577         if (!cache_inode)
578                 RETURN(-EINVAL);
579
580         sfi = F2SMFI(file);
581         if (sfi->magic != SMFS_FILE_MAGIC)
582                 BUG();
583
584         if (offs != &(file->f_pos))
585                 cache_ppos = &tmp_ppos;
586         else
587                 cache_ppos = &sfi->c_file->f_pos;
588         *cache_ppos = *offs;
589
590         pre_smfs_inode(file->f_dentry->d_inode, cache_inode);
591
592         if (!cache_fsfilt->fs_write_record)
593                 RETURN(-ENOSYS);
594
595         rc = cache_fsfilt->fs_write_record(sfi->c_file, buf,
596                                            bufsize, cache_ppos, force_sync);
597         *offs = *cache_ppos;
598         post_smfs_inode(file->f_dentry->d_inode, cache_inode);
599         duplicate_file(file, sfi->c_file);
600
601         RETURN(rc);
602 }
603
604 static int fsfilt_smfs_post_setup(struct obd_device *obd, struct vfsmount *mnt,
605                                   struct dentry *root_dentry)
606 {
607         struct super_block *sb = NULL;
608         int rc = 0;
609
610         if (mnt) {
611                 sb = mnt->mnt_sb;
612                 S2SMI(sb)->smsi_exp = obd->obd_self_export;
613                 smfs_post_setup(sb, mnt);
614                 if (SMFS_DO_REC(S2SMI(sb)))
615                         rc = smfs_start_rec(sb, mnt);
616 #if CONFIG_SNAPFS
617                 if (SMFS_DO_COW(S2SMI(sb))) {
618                        S2SNAPI(sb)->snap_root = root_dentry;  
619                        rc = smfs_start_cow(sb);
620                 }
621 #endif
622                 if (rc)
623                         GOTO(exit, rc);
624                 if (obd)
625                         obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT] =
626                                         S2SMI(sb)->smsi_rec_log;
627         }
628 exit:
629         if (rc)
630                 CERROR("can not do post setup in obd %p rc=%d", obd, rc);
631
632         RETURN(rc);
633 }
634
635 static int fsfilt_smfs_post_cleanup(struct obd_device *obd,
636                                     struct vfsmount *mnt)
637 {
638         struct super_block *sb = NULL;
639         int rc = 0;
640         ENTRY;
641         
642         if (mnt) {
643                 sb = mnt->mnt_sb;
644                 if (SMFS_DO_REC(S2SMI(sb)))
645                         rc = smfs_stop_rec(sb);
646 #if CONFIG_SNAPFS
647                 if (SMFS_DO_COW(S2SMI(sb)))
648                         rc = smfs_stop_cow(sb);
649 #endif
650                 smfs_post_cleanup(sb);
651         }
652         RETURN(rc);
653 }
654
655 static int fsfilt_smfs_set_fs_flags(struct inode *inode, int flags)
656 {
657         int rc = 0;
658         ENTRY;
659
660         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
661                 SMFS_SET_INODE_REC(inode);
662         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
663                 SMFS_SET_INODE_COW(inode);
664         RETURN(rc);
665 }
666
667 static int fsfilt_smfs_clear_fs_flags(struct inode *inode, int flags)
668 {
669         int rc = 0;
670         ENTRY;
671         
672         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && (flags & SM_DO_REC))
673                 SMFS_CLEAN_INODE_REC(inode);
674         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && (flags & SM_DO_COW))
675                 SMFS_CLEAN_INODE_COW(inode);
676         RETURN(rc);
677 }
678
679 static int fsfilt_smfs_get_fs_flags(struct dentry *de)
680 {
681         struct inode *inode = de->d_inode;
682         int flags = 0;
683         ENTRY;
684
685         LASSERT(inode);
686
687         if (SMFS_DO_REC(S2SMI(inode->i_sb)) && SMFS_DO_INODE_REC(inode))
688                 flags |= SM_DO_REC;
689         if (SMFS_DO_COW(S2SMI(inode->i_sb)) && SMFS_DO_INODE_COW(inode))
690                 flags |= SM_DO_COW;
691        
692         RETURN(flags); 
693 }
694 static int fsfilt_smfs_set_ost_flags(struct super_block *sb)
695 {
696         int rc = 0;
697         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_OST);
698         RETURN(rc);
699 }
700
701 static int fsfilt_smfs_set_mds_flags(struct super_block *sb)
702 {
703         int rc = 0;
704         SET_REC_PACK_TYPE_INDEX(S2SMI(sb)->smsi_flags, PACK_MDS);
705         RETURN(rc);
706 }
707
708 static int fsfilt_smfs_get_reint_log_ctxt(struct super_block *sb,
709                                           struct llog_ctxt **ctxt)
710 {
711         struct smfs_super_info *smfs_info = S2SMI(sb);
712         int rc = 0;
713
714         *ctxt = smfs_info->smsi_rec_log;
715         RETURN(rc);
716 }
717
718 static int fsfilt_smfs_setup(struct obd_device *obd, struct super_block *sb)
719 {
720         struct smfs_super_info *smfs_info = S2SMI(sb);
721         struct fsfilt_operations *cache_fsfilt;
722         struct super_block *csb;
723         int rc = 0;
724
725         /* It should be initialized olready by smfs_read_super(). */
726         if (!(cache_fsfilt = smfs_info->sm_cache_fsfilt))
727                     cache_fsfilt = fsfilt_get_ops(smfs_info->smsi_cache_ftype);
728
729         if (!cache_fsfilt)
730                 RETURN(-ENOENT);
731
732         csb = S2CSB(sb);
733         if (cache_fsfilt->fs_setup) 
734                 rc = cache_fsfilt->fs_setup(obd, csb);
735         
736         duplicate_sb(sb, csb);
737         
738         RETURN(rc);
739 }
740
741 static int fsfilt_smfs_set_xattr(struct inode *inode, void *handle, char *name,
742                                  void *buffer, int buffer_size)
743 {
744         struct  fsfilt_operations *cache_fsfilt = I2FOPS(inode);
745         struct  inode *cache_inode = NULL;
746         int     rc = -EIO;
747
748         if (!cache_fsfilt)
749                 RETURN(rc);
750
751         cache_inode = I2CI(inode);
752         if (!cache_inode)
753                 RETURN(rc);
754
755         pre_smfs_inode(inode, cache_inode);
756
757         if (cache_fsfilt->fs_set_xattr)
758                 rc = cache_fsfilt->fs_set_xattr(cache_inode, handle, name,
759                                                 buffer, buffer_size);
760         post_smfs_inode(inode, cache_inode);
761
762         RETURN(rc);
763 }
764
765 static int fsfilt_smfs_get_xattr(struct inode *inode, char *name,
766                                  void *buffer, int buffer_size)
767 {
768         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
769         struct inode *cache_inode = NULL;
770         int    rc = -EIO;
771
772         if (!cache_fsfilt)
773                 RETURN(rc);
774
775         cache_inode = I2CI(inode);
776         if (!cache_inode)
777                 RETURN(rc);
778
779         pre_smfs_inode(inode, cache_inode);
780
781         if (cache_fsfilt->fs_get_xattr)
782                 rc = cache_fsfilt->fs_get_xattr(cache_inode, name,
783                                                 buffer, buffer_size);
784         post_smfs_inode(inode, cache_inode);
785
786         RETURN(rc);
787 }
788
789 static int fsfilt_smfs_insert_extents_ea(struct inode *inode,
790                                          unsigned long from, unsigned long num)
791 {
792         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
793         struct inode *cache_inode = NULL;
794         int    rc = -EIO;
795
796         if (!cache_fsfilt)
797                 RETURN(rc);
798
799         cache_inode = I2CI(inode);
800         if (!cache_inode)
801                 RETURN(rc);
802
803         pre_smfs_inode(inode, cache_inode);
804
805         if (cache_fsfilt->fs_insert_extents_ea)
806                 rc = cache_fsfilt->fs_insert_extents_ea(cache_inode, from, num);
807
808         post_smfs_inode(inode, cache_inode);
809         return rc;
810 }
811
812 static int fsfilt_smfs_remove_extents_ea(struct inode *inode,
813                                          unsigned long from, unsigned long num)
814 {
815         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
816         struct inode *cache_inode = NULL;
817         int    rc = -EIO;
818
819         if (!cache_fsfilt)
820                 RETURN(rc);
821
822         cache_inode = I2CI(inode);
823         if (!cache_inode)
824                 RETURN(rc);
825
826         pre_smfs_inode(inode, cache_inode);
827
828         if (cache_fsfilt->fs_remove_extents_ea)
829                 rc = cache_fsfilt->fs_remove_extents_ea(cache_inode, from, num);
830
831         post_smfs_inode(inode, cache_inode);
832         return rc;
833 }
834
835 static int fsfilt_smfs_init_extents_ea(struct inode *inode)
836 {
837         struct fsfilt_operations *cache_fsfilt = I2FOPS(inode);
838         struct inode *cache_inode = NULL;
839         int    rc = -EIO;
840
841         if (!cache_fsfilt)
842                 RETURN(rc);
843
844         cache_inode = I2CI(inode);
845         if (!cache_inode)
846                 RETURN(rc);
847
848         pre_smfs_inode(inode, cache_inode);
849
850         if (cache_fsfilt->fs_init_extents_ea)
851                 rc = cache_fsfilt->fs_init_extents_ea(cache_inode);
852
853         post_smfs_inode(inode, cache_inode);
854         return rc;
855 }
856
857 static int fsfilt_smfs_free_extents(struct super_block *sb, ino_t ino,
858                                     char *pbuf, int size)
859 {
860         OBD_FREE(pbuf, size * (sizeof(struct ldlm_extent)));
861         return 0;
862 }
863
864 static int fsfilt_smfs_write_extents(struct dentry *dentry,
865                                      unsigned long from, unsigned long num)
866 {
867         int rc = 0;
868
869         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
870                 rc = smfs_write_extents(dentry->d_inode, dentry, from, num);
871
872         return rc;
873 }
874
875 static int fsfilt_smfs_precreate_rec(struct dentry *dentry, int *count, 
876                                      struct obdo *oa)
877 {
878         int rc = 0;
879
880         if (SMFS_DO_REC(S2SMI(dentry->d_inode->i_sb)))
881                 rc = smfs_rec_precreate(dentry, count, oa);
882
883         return rc;
884 }
885
886 static int fsfilt_smfs_get_ino_write_extents(struct super_block *sb, ino_t ino,
887                                              char **pbuf, int *size)
888 {
889         struct fs_extent *fs_extents;
890         struct ldlm_extent *extents = NULL;
891         struct inode *inode;
892         struct inode *cache_inode;
893         struct fsfilt_operations *cache_fsfilt = NULL;
894         struct lvfs_run_ctxt saved;
895         int    rc = 0, fs_ex_size, ex_num, flags;
896         char   *buf = NULL, *ex_buf = NULL;
897
898         push_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
899
900         inode = iget(sb, ino);
901
902         if (!inode || is_bad_inode(inode)) {
903                 CWARN("Can not get inode %lu ino\n", ino);
904                 GOTO(out, rc = 0);
905         }
906         cache_inode = I2CI(inode);
907         cache_fsfilt = I2FOPS(inode);
908
909         rc = cache_fsfilt->fs_get_xattr(cache_inode, REINT_EXTENTS_FLAGS,
910                                         &flags, sizeof(int));
911         if (!(flags & SMFS_OVER_WRITE) && !(flags & SMFS_DIRTY_WRITE)) {
912                 GOTO(out, rc = 0);
913         } else if (flags & SMFS_OVER_WRITE) {
914                 *size = 1;
915                 OBD_ALLOC(ex_buf, sizeof(struct ldlm_extent));
916                 if (!ex_buf)
917                         GOTO(out, rc=-ENOMEM);
918                 extents = (struct ldlm_extent*)(ex_buf);
919                 extents->start = 0;
920                 extents->end = 0xffffffff;
921         }
922         if (rc < 0)
923                 GOTO(out, rc);
924         rc = cache_fsfilt->fs_get_write_extents_num(cache_inode, &fs_ex_size);
925         if (rc)
926                 GOTO(out, rc);
927         OBD_ALLOC(buf, fs_ex_size);
928         if (!buf)
929                 GOTO(out, rc=-ENOMEM);
930
931         rc = cache_fsfilt->fs_get_inode_write_extents(cache_inode, &buf,
932                                                       &fs_ex_size);
933         if (rc < 0)
934                 GOTO(out, rc);
935         rc = 0;
936         ex_num = fs_ex_size / sizeof(struct fs_extent);
937         *size =  ex_num;
938         OBD_ALLOC(ex_buf, ex_num* sizeof(struct ldlm_extent));
939         if (!ex_buf)
940                 GOTO(out, rc=-ENOMEM);
941
942         fs_extents = (struct fs_extent*)(buf);
943         extents = (struct ldlm_extent*)(ex_buf);
944         while (ex_num > 0) {
945                 int blk_size = I2CI(inode)->i_blksize;
946
947                 extents->start = fs_extents->e_block * blk_size;
948                 extents->end = extents->start + fs_extents->e_num * blk_size;
949                 fs_extents++;
950                 extents++;
951                 ex_num--;
952         }
953         *pbuf = ex_buf;
954 out:
955         iput(inode);
956         if (buf)
957                 OBD_FREE(buf, fs_ex_size);
958         if (rc && extents)
959                 OBD_FREE(ex_buf, (*size) * (sizeof(struct ldlm_extent)));
960         pop_ctxt(&saved, S2SMI(sb)->smsi_ctxt, NULL);
961         return rc;
962 }
963
964 static int fsfilt_smfs_set_snap_item(struct super_block *sb, char *name)
965 {
966         int rc = 0;
967
968         ENTRY;
969 #if CONFIG_SNAPFS
970         rc = smfs_add_snap_item(sb, name);
971 #endif
972         RETURN(rc);        
973 }
974 static int fsfilt_smfs_do_write_cow(struct dentry *de, void *extents,
975                                     int num_extents)
976 {
977         int rc = 0;
978 #if CONFIG_SNAPFS
979         struct write_extents *w_ext = (struct write_extents *)extents;
980         int i = 0;
981         ENTRY;
982         for (i = 0; i < num_extents; i++) {
983                size_t count = w_ext->w_count;
984                loff_t off = w_ext->w_pos;
985                rc = smfs_cow_write(de->d_inode, de, &count, &off);
986                if (rc)
987                         RETURN(rc);  
988                w_ext ++;
989         }
990 #endif
991         RETURN(rc);
992 }
993 static struct fsfilt_operations fsfilt_smfs_ops = {
994         .fs_type                = "smfs",
995         .fs_owner               = THIS_MODULE,
996         .fs_start               = fsfilt_smfs_start,
997         .fs_brw_start           = fsfilt_smfs_brw_start,
998         .fs_commit              = fsfilt_smfs_commit,
999         .fs_commit_async        = fsfilt_smfs_commit_async,
1000         .fs_commit_wait         = fsfilt_smfs_commit_wait,
1001         .fs_setattr             = fsfilt_smfs_setattr,
1002         .fs_iocontrol           = fsfilt_smfs_iocontrol,
1003         .fs_set_md              = fsfilt_smfs_set_md,
1004         .fs_get_md              = fsfilt_smfs_get_md,
1005         .fs_readpage            = fsfilt_smfs_readpage,
1006         .fs_getpage             = fsfilt_smfs_getpage,
1007         .fs_add_journal_cb      = fsfilt_smfs_add_journal_cb,
1008         .fs_statfs              = fsfilt_smfs_statfs,
1009         .fs_sync                = fsfilt_smfs_sync,
1010         .fs_map_inode_pages     = fsfilt_smfs_map_inode_pages,
1011         .fs_prep_san_write      = fsfilt_smfs_prep_san_write,
1012         .fs_write_record        = fsfilt_smfs_write_record,
1013         .fs_read_record         = fsfilt_smfs_read_record,
1014         .fs_setup               = fsfilt_smfs_setup,
1015         .fs_post_setup          = fsfilt_smfs_post_setup,
1016         .fs_post_cleanup        = fsfilt_smfs_post_cleanup,
1017         .fs_set_fs_flags       = fsfilt_smfs_set_fs_flags,
1018         .fs_clear_fs_flags     = fsfilt_smfs_clear_fs_flags,
1019         .fs_get_fs_flags       = fsfilt_smfs_get_fs_flags,
1020         .fs_set_ost_flags       = fsfilt_smfs_set_ost_flags,
1021         .fs_set_mds_flags       = fsfilt_smfs_set_mds_flags,
1022         .fs_precreate_rec       = fsfilt_smfs_precreate_rec,
1023         .fs_get_reint_log_ctxt  = fsfilt_smfs_get_reint_log_ctxt,
1024         .fs_send_bio            = fsfilt_smfs_send_bio,
1025         .fs_set_xattr           = fsfilt_smfs_set_xattr,
1026         .fs_get_xattr           = fsfilt_smfs_get_xattr,
1027         .fs_init_extents_ea     = fsfilt_smfs_init_extents_ea,
1028         .fs_insert_extents_ea   = fsfilt_smfs_insert_extents_ea,
1029         .fs_remove_extents_ea   = fsfilt_smfs_remove_extents_ea,
1030         .fs_get_ino_write_extents = fsfilt_smfs_get_ino_write_extents,
1031         .fs_free_write_extents  = fsfilt_smfs_free_extents,
1032         .fs_write_extents       = fsfilt_smfs_write_extents,
1033         .fs_set_snap_item       = fsfilt_smfs_set_snap_item,
1034         .fs_do_write_cow        = fsfilt_smfs_do_write_cow,
1035         /* FIXME-UMKA: probably fsfilt_smfs_get_op_len() should be
1036          * put here too. */
1037 };
1038
1039 static int __init fsfilt_smfs_init(void)
1040 {
1041         int rc;
1042
1043         rc = fsfilt_register_ops(&fsfilt_smfs_ops);
1044         return rc;
1045 }
1046
1047 static void __exit fsfilt_smfs_exit(void)
1048 {
1049         fsfilt_unregister_ops(&fsfilt_smfs_ops);
1050 }
1051
1052 module_init(fsfilt_smfs_init);
1053 module_exit(fsfilt_smfs_exit);
1054
1055 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1056 MODULE_DESCRIPTION("Lustre SMFS Filesystem Helper v0.1");
1057 MODULE_LICENSE("GPL");