Whamcloud - gitweb
landing smfs.
[fs/lustre-release.git] / lustre / lvfs / fsfilt_ext3.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  lustre/lib/fsfilt_ext3.c
5  *  Lustre filesystem abstraction routines
6  *
7  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8  *   Author: Andreas Dilger <adilger@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/init.h>
29 #include <linux/module.h>
30 #include <linux/fs.h>
31 #include <linux/jbd.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/ext3_jbd.h>
37 #include <linux/version.h>
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
39 #include <linux/ext3_xattr.h>
40 #else
41 #include <ext3/xattr.h>
42 #endif
43
44 #include <linux/kp30.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/obd.h>
47 #include <linux/obd_class.h>
48 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
49 #include <linux/module.h>
50 #include <linux/iobuf.h>
51 #endif
52
53 static kmem_cache_t *fcb_cache;
54 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
55
56 struct fsfilt_cb_data {
57         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
58         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
59         struct obd_device *cb_obd;      /* MDS/OBD completion device */
60         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
61         void *cb_data;                  /* MDS/OST completion function data */
62 };
63
64 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
65 #define EXT3_XATTR_INDEX_TRUSTED        4
66 #endif
67 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
68
69 /*
70  * We don't currently need any additional blocks for rmdir and
71  * unlink transactions because we are storing the OST oa_id inside
72  * the inode (which we will be changing anyways as part of this
73  * transaction).
74  */
75 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
76                                int logs)
77 {
78         /* For updates to the last recieved file */
79         int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
80         journal_t *journal;
81         void *handle;
82
83         if (current->journal_info) {
84                 CDEBUG(D_INODE, "increasing refcount on %p\n",
85                        current->journal_info);
86                 goto journal_start;
87         }
88
89         /* XXX BUG 3188 -- must return to one set of opcodes */
90         /* FIXME - cache hook */
91         if (op & 0x20) {
92                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
93                 op = op & ~0x20;
94         }
95
96         /* FIXME - kml */
97         if (op & 0x10) {
98                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
99                 op = op & ~0x10;
100         }
101
102         switch(op) {
103         case FSFILT_OP_RMDIR:
104         case FSFILT_OP_UNLINK:
105                 /* delete one file + create/update logs for each stripe */
106                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
107                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
108                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
109                 break;
110         case FSFILT_OP_RENAME:
111                 /* modify additional directory */
112                 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
113                 /* no break */
114         case FSFILT_OP_SYMLINK:
115                 /* additional block + block bitmap + GDT for long symlink */
116                 nblocks += 3;
117                 /* no break */
118         case FSFILT_OP_CREATE:
119                 /* create/update logs for each stripe */
120                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
121                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
122                 /* no break */
123         case FSFILT_OP_MKDIR:
124         case FSFILT_OP_MKNOD:
125                 /* modify one inode + block bitmap + GDT */
126                 nblocks += 3;
127                 /* no break */
128         case FSFILT_OP_LINK:
129                 /* modify parent directory */
130                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
131                         EXT3_DATA_TRANS_BLOCKS;
132                 break;
133         case FSFILT_OP_SETATTR:
134                 /* Setattr on inode */
135                 nblocks += 1;
136                 break;
137         case FSFILT_OP_CANCEL_UNLINK:
138                 /* blocks for log header bitmap update OR
139                  * blocks for catalog header bitmap update + unlink of logs */
140                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
141                         EXT3_DELETE_TRANS_BLOCKS * logs;
142                 break;
143         case FSFILT_OP_NOOP:
144                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
145                 break;
146         default: CERROR("unknown transaction start op %d\n", op);
147                 LBUG();
148         }
149
150         LASSERT(current->journal_info == desc_private);
151         journal = EXT3_SB(inode->i_sb)->s_journal;
152         if (nblocks > journal->j_max_transaction_buffers) {
153                 CERROR("too many credits %d for op %ux%u using %d instead\n",
154                        nblocks, op, logs, journal->j_max_transaction_buffers);
155                 nblocks = journal->j_max_transaction_buffers;
156         }
157
158  journal_start:
159         lock_kernel();
160         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
161         unlock_kernel();
162
163         if (!IS_ERR(handle))
164                 LASSERT(current->journal_info == handle);
165         else
166                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
167                        op, nblocks, PTR_ERR(handle));
168         return handle;
169 }
170
171 /*
172  * Calculate the number of buffer credits needed to write multiple pages in
173  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
174  * doesn't have a nice API for calculating this sort of thing in advance.
175  *
176  * See comment above ext3_writepage_trans_blocks for details.  We assume
177  * no data journaling is being done, but it does allow for all of the pages
178  * being non-contiguous.  If we are guaranteed contiguous pages we could
179  * reduce the number of (d)indirect blocks a lot.
180  *
181  * With N blocks per page and P pages, for each inode we have at most:
182  * N*P indirect
183  * min(N*P, blocksize/4 + 1) dindirect blocks
184  * niocount tindirect
185  *
186  * For the entire filesystem, we have at most:
187  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
188  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
189  * objcount inode blocks
190  * 1 superblock
191  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
192  *
193  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
194  */
195 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
196                                       int niocount, struct niobuf_local *nb)
197 {
198         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
199         __u64 next_indir;
200         const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
201         int nbitmaps = 0, ngdblocks;
202         int needed = objcount + 1; /* inodes + superblock */
203         int i, j;
204
205         for (i = 0, j = 0; i < objcount; i++, fso++) {
206                 /* two or more dindirect blocks in case we cross boundary */
207                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
208                                     nb[j].offset) >>
209                                    sb->s_blocksize_bits) /
210                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
211                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
212
213                 /* leaf, indirect, tindirect blocks for first block */
214                 nbitmaps += blockpp + 2;
215
216                 j += fso->fso_bufcnt;
217         }
218
219         next_indir = nb[0].offset +
220                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
221         for (i = 1; i < niocount; i++) {
222                 if (nb[i].offset >= next_indir) {
223                         nbitmaps++;     /* additional indirect */
224                         next_indir = nb[i].offset +
225                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
226                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
227                         nbitmaps++;     /* additional indirect */
228                 }
229                 nbitmaps += blockpp;    /* each leaf in different group? */
230         }
231
232         ngdblocks = nbitmaps;
233         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
234                 nbitmaps = EXT3_SB(sb)->s_groups_count;
235         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
236                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
237
238         needed += nbitmaps + ngdblocks;
239
240         /* last_rcvd update */
241         needed += EXT3_DATA_TRANS_BLOCKS;
242
243 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
244         /* We assume that there will be 1 bit set in s_dquot.flags for each
245          * quota file that is active.  This is at least true for now.
246          */
247         needed += hweight32(sb_any_quota_enabled(sb)) *
248                 EXT3_SINGLEDATA_TRANS_BLOCKS;
249 #endif
250
251         return needed;
252 }
253
254 /* We have to start a huge journal transaction here to hold all of the
255  * metadata for the pages being written here.  This is necessitated by
256  * the fact that we do lots of prepare_write operations before we do
257  * any of the matching commit_write operations, so even if we split
258  * up to use "smaller" transactions none of them could complete until
259  * all of them were opened.  By having a single journal transaction,
260  * we eliminate duplicate reservations for common blocks like the
261  * superblock and group descriptors or bitmaps.
262  *
263  * We will start the transaction here, but each prepare_write will
264  * add a refcount to the transaction, and each commit_write will
265  * remove a refcount.  The transaction will be closed when all of
266  * the pages have been written.
267  */
268 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
269                                    int niocount, struct niobuf_local *nb,
270                                    void *desc_private, int logs)
271 {
272         journal_t *journal;
273         handle_t *handle;
274         int needed;
275         ENTRY;
276
277         LASSERT(current->journal_info == desc_private);
278         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
279         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
280
281         /* The number of blocks we could _possibly_ dirty can very large.
282          * We reduce our request if it is absurd (and we couldn't get that
283          * many credits for a single handle anyways).
284          *
285          * At some point we have to limit the size of I/Os sent at one time,
286          * increase the size of the journal, or we have to calculate the
287          * actual journal requirements more carefully by checking all of
288          * the blocks instead of being maximally pessimistic.  It remains to
289          * be seen if this is a real problem or not.
290          */
291         if (needed > journal->j_max_transaction_buffers) {
292                 CERROR("want too many journal credits (%d) using %d instead\n",
293                        needed, journal->j_max_transaction_buffers);
294                 needed = journal->j_max_transaction_buffers;
295         }
296
297         lock_kernel();
298         handle = journal_start(journal, needed);
299         unlock_kernel();
300         if (IS_ERR(handle)) {
301                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
302                        PTR_ERR(handle));
303         } else {
304                 LASSERT(handle->h_buffer_credits >= needed);
305                 LASSERT(current->journal_info == handle);
306         }
307
308         RETURN(handle);
309 }
310
311 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
312 {
313         int rc;
314         handle_t *handle = h;
315
316         LASSERT(current->journal_info == handle);
317         if (force_sync)
318                 handle->h_sync = 1; /* recovery likes this */
319
320         lock_kernel();
321         rc = journal_stop(handle);
322         unlock_kernel();
323
324         return rc;
325 }
326
327 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
328                                     void **wait_handle)
329 {
330         unsigned long tid;
331         transaction_t *transaction;
332 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
333         unsigned long rtid;
334 #endif
335         handle_t *handle = h;
336         journal_t *journal;
337         int rc;
338
339         LASSERT(current->journal_info == handle);
340
341         lock_kernel();
342         transaction = handle->h_transaction;
343         journal = transaction->t_journal;
344         tid = transaction->t_tid;
345         /* we don't want to be blocked */
346         handle->h_sync = 0;
347         rc = journal_stop(handle);
348         if (rc) {
349                 CERROR("error while stopping transaction: %d\n", rc);
350                 unlock_kernel();
351                 return rc;
352         }
353 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
354         rtid = log_start_commit(journal, transaction);
355         if (rtid != tid)
356                 CERROR("strange race: %lu != %lu\n",
357                        (unsigned long) tid, (unsigned long) rtid);
358 #else
359         log_start_commit(journal, transaction->t_tid);
360 #endif
361         unlock_kernel();
362
363         *wait_handle = (void *) tid;
364         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
365         return 0;
366 }
367
368 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
369 {
370         tid_t tid = (tid_t)(long)h;
371
372         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
373         if (is_journal_aborted(EXT3_JOURNAL(inode)))
374                 return -EIO;
375
376         log_wait_commit(EXT3_JOURNAL(inode), tid);
377
378         return 0;
379 }
380
381 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
382                                struct iattr *iattr, int do_trunc)
383 {
384         struct inode *inode = dentry->d_inode;
385         int rc;
386
387         lock_kernel();
388
389         /* A _really_ horrible hack to avoid removing the data stored
390          * in the block pointers; this is really the "small" stripe MD data.
391          * We can avoid further hackery by virtue of the MDS file size being
392          * zero all the time (which doesn't invoke block truncate at unlink
393          * time), so we assert we never change the MDS file size from zero. */
394         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
395                 /* ATTR_SIZE would invoke truncate: clear it */
396                 iattr->ia_valid &= ~ATTR_SIZE;
397                 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
398
399                 /* make sure _something_ gets set - so new inode
400                  * goes to disk (probably won't work over XFS */
401                 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
402                         iattr->ia_valid |= ATTR_MODE;
403                         iattr->ia_mode = inode->i_mode;
404                 }
405         }
406
407         /* Don't allow setattr to change file type */
408         iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
409
410         /* We set these flags on the client, but have already checked perms
411          * so don't confuse inode_change_ok. */
412         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
413
414         if (inode->i_op->setattr) {
415                 rc = inode->i_op->setattr(dentry, iattr);
416         } else {
417                 rc = inode_change_ok(inode, iattr);
418                 if (!rc)
419                         rc = inode_setattr(inode, iattr);
420         }
421
422         unlock_kernel();
423
424         return rc;
425 }
426
427 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
428                                  unsigned int cmd, unsigned long arg)
429 {
430         int rc = 0;
431         ENTRY;
432
433         if (inode->i_fop->ioctl)
434                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
435         else
436                 RETURN(-ENOTTY);
437
438         RETURN(rc);
439 }
440
441 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
442                               void *lmm, int lmm_size)
443 {
444         int rc;
445
446         /* keep this when we get rid of OLD_EA (too noisy during conversion) */
447         if (EXT3_I(inode)->i_file_acl /* || large inode EA flag */)
448                 CWARN("setting EA on %lu/%u again... interesting\n",
449                        inode->i_ino, inode->i_generation);
450
451         lock_kernel();
452         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
453                                    XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
454
455         unlock_kernel();
456
457         if (rc)
458                 CERROR("error adding MD data to inode %lu: rc = %d\n",
459                        inode->i_ino, rc);
460         return rc;
461 }
462
463 /* Must be called with i_sem held */
464 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
465 {
466         int rc;
467
468         LASSERT(down_trylock(&inode->i_sem) != 0);
469         lock_kernel();
470
471         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
472                             XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
473         unlock_kernel();
474
475         /* This gives us the MD size */
476         if (lmm == NULL)
477                 return (rc == -ENODATA) ? 0 : rc;
478
479         if (rc < 0) {
480                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
481                        EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
482                        inode->i_ino, rc);
483                 memset(lmm, 0, lmm_size);
484                 return (rc == -ENODATA) ? 0 : rc;
485         }
486
487         return rc;
488 }
489
490 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
491 static int fsfilt_ext3_send_bio(struct inode *inode, struct bio *bio)
492 {
493         submit_bio(WRITE, bio);
494         return 0;
495 }
496 #else
497 static int fsfilt_ext3_send_bio(struct inode *inode, struct kiobuf *bio)
498 {
499         int rc, blocks_per_page;
500
501         rc = brw_kiovec(WRITE, 1, &bio, inode->i_dev,
502                         bio->blocks, 1 << inode->i_blkbits);
503
504         blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
505
506         if (rc != (1 << inode->i_blkbits) * bio->nr_pages *
507             blocks_per_page) {
508                 CERROR("short write?  expected %d, wrote %d\n",
509                        (1 << inode->i_blkbits) * bio->nr_pages *
510                        blocks_per_page, rc);
511         }
512
513         return rc;
514 }
515 #endif
516
517 /* FIXME-UMKA: This should be used in 2.6.x io code later. */
518 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
519 {
520         int rc;
521         struct page *page;
522
523         page = grab_cache_page(inode->i_mapping, index);
524         if (page == NULL)
525                 return ERR_PTR(-ENOMEM);
526
527         if (PageUptodate(page)) {
528                 unlock_page(page);
529                 return page;
530         }
531
532         rc = inode->i_mapping->a_ops->readpage(NULL, page);
533         if (rc < 0) {
534                 page_cache_release(page);
535                 return ERR_PTR(rc);
536         }
537
538         return page;
539 }
540
541 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
542                                     loff_t *off)
543 {
544         struct inode *inode = file->f_dentry->d_inode;
545         int rc = 0;
546
547         if (S_ISREG(inode->i_mode))
548                 rc = file->f_op->read(file, buf, count, off);
549         else {
550                 const int blkbits = inode->i_sb->s_blocksize_bits;
551                 const int blksize = inode->i_sb->s_blocksize;
552
553                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
554                        count, inode->i_ino, *off);
555                 while (count > 0) {
556                         struct buffer_head *bh;
557
558                         bh = NULL;
559                         if (*off < inode->i_size) {
560                                 int err = 0;
561
562                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
563                                                 0, &err);
564
565                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
566
567                                 if (bh) {
568                                         memcpy(buf, bh->b_data, blksize);
569                                         brelse(bh);
570                                 } else if (err) {
571                                         /* XXX in theory we should just fake
572                                          * this buffer and continue like ext3,
573                                          * especially if this is a partial read
574                                          */
575                                         CERROR("error read dir %lu+%llu: %d\n",
576                                                inode->i_ino, *off, err);
577                                         RETURN(err);
578                                 }
579                         }
580                         if (!bh) {
581                                 struct ext3_dir_entry_2 *fake = (void *)buf;
582
583                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
584                                 memset(fake, 0, sizeof(*fake));
585                                 fake->rec_len = cpu_to_le32(blksize);
586                         }
587                         count -= blksize;
588                         buf += blksize;
589                         *off += blksize;
590                         rc += blksize;
591                 }
592         }
593
594         return rc;
595 }
596
597 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
598 {
599         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
600
601         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
602
603         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
604         atomic_dec(&fcb_cache_count);
605 }
606
607 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
608                                       struct super_block *sb,
609                                       __u64 last_rcvd,
610                                       void *handle, fsfilt_cb_t cb_func,
611                                       void *cb_data)
612 {
613         struct fsfilt_cb_data *fcb;
614
615         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
616         if (fcb == NULL)
617                 RETURN(-ENOMEM);
618
619         atomic_inc(&fcb_cache_count);
620         fcb->cb_func = cb_func;
621         fcb->cb_obd = obd;
622         fcb->cb_last_rcvd = last_rcvd;
623         fcb->cb_data = cb_data;
624
625         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
626         lock_kernel();
627         journal_callback_set(handle, fsfilt_ext3_cb_func,
628                              (struct journal_callback *)fcb);
629         unlock_kernel();
630
631         return 0;
632 }
633
634 /*
635  * We need to hack the return value for the free inode counts because
636  * the current EA code requires one filesystem block per inode with EAs,
637  * so it is possible to run out of blocks before we run out of inodes.
638  *
639  * This can be removed when the ext3 EA code is fixed.
640  */
641 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
642 {
643         struct kstatfs sfs;
644         int rc;
645
646         memset(&sfs, 0, sizeof(sfs));
647
648         rc = sb->s_op->statfs(sb, &sfs);
649
650         if (!rc && sfs.f_bfree < sfs.f_ffree) {
651                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
652                 sfs.f_ffree = sfs.f_bfree;
653         }
654
655         statfs_pack(osfs, &sfs);
656         return rc;
657 }
658
659 static int fsfilt_ext3_sync(struct super_block *sb)
660 {
661         return ext3_force_commit(sb);
662 }
663
664 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
665                                unsigned long *blocks, int *created, int create);
666 int fsfilt_ext3_map_inode_page(struct inode *inode, struct page *page,
667                                unsigned long *blocks, int *created, int create)
668 {
669         return ext3_map_inode_page(inode, page, blocks, created, create);
670 }
671
672 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
673                                int nblocks, loff_t newsize);
674 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
675                                       int nblocks, loff_t newsize)
676 {
677         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
678 }
679
680 static int fsfilt_ext3_read_record(struct file * file, void *buf,
681                                    int size, loff_t *offs)
682 {
683         struct inode *inode = file->f_dentry->d_inode;
684         unsigned long block;
685         struct buffer_head *bh;
686         int err, blocksize, csize, boffs;
687
688         /* prevent reading after eof */
689         lock_kernel();
690         if (inode->i_size < *offs + size) {
691                 size = inode->i_size - *offs;
692                 unlock_kernel();
693                 if (size < 0) {
694                         CERROR("size %llu is too short for read %u@%llu\n",
695                                inode->i_size, size, *offs);
696                         return -EIO;
697                 } else if (size == 0) {
698                         return 0;
699                 }
700         } else {
701                 unlock_kernel();
702         }
703
704         blocksize = 1 << inode->i_blkbits;
705
706         while (size > 0) {
707                 block = *offs >> inode->i_blkbits;
708                 boffs = *offs & (blocksize - 1);
709                 csize = min(blocksize - boffs, size);
710                 bh = ext3_bread(NULL, inode, block, 0, &err);
711                 if (!bh) {
712                         CERROR("can't read block: %d\n", err);
713                         return err;
714                 }
715
716                 memcpy(buf, bh->b_data + boffs, csize);
717                 brelse(bh);
718
719                 *offs += csize;
720                 buf += csize;
721                 size -= csize;
722         }
723         return 0;
724 }
725
726 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
727                                     loff_t *offs, int force_sync)
728 {
729         struct buffer_head *bh = NULL;
730         unsigned long block;
731         struct inode *inode = file->f_dentry->d_inode;
732         loff_t old_size = inode->i_size, offset = *offs;
733         loff_t new_size = inode->i_size;
734         journal_t *journal;
735         handle_t *handle;
736         int err, block_count = 0, blocksize, size, boffs;
737
738         /* Determine how many transaction credits are needed */
739         blocksize = 1 << inode->i_blkbits;
740         block_count = (*offs & (blocksize - 1)) + bufsize;
741         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
742
743         journal = EXT3_SB(inode->i_sb)->s_journal;
744         lock_kernel();
745         handle = journal_start(journal,
746                                block_count * EXT3_DATA_TRANS_BLOCKS + 2);
747         unlock_kernel();
748         if (IS_ERR(handle)) {
749                 CERROR("can't start transaction\n");
750                 return PTR_ERR(handle);
751         }
752
753         while (bufsize > 0) {
754                 if (bh != NULL)
755                         brelse(bh);
756
757                 block = offset >> inode->i_blkbits;
758                 boffs = offset & (blocksize - 1);
759                 size = min(blocksize - boffs, bufsize);
760                 bh = ext3_bread(handle, inode, block, 1, &err);
761                 if (!bh) {
762                         CERROR("can't read/create block: %d\n", err);
763                         goto out;
764                 }
765
766                 err = ext3_journal_get_write_access(handle, bh);
767                 if (err) {
768                         CERROR("journal_get_write_access() returned error %d\n",
769                                err);
770                         goto out;
771                 }
772                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
773                 memcpy(bh->b_data + boffs, buf, size);
774                 err = ext3_journal_dirty_metadata(handle, bh);
775                 if (err) {
776                         CERROR("journal_dirty_metadata() returned error %d\n",
777                                err);
778                         goto out;
779                 }
780                 if (offset + size > new_size)
781                         new_size = offset + size;
782                 offset += size;
783                 bufsize -= size;
784                 buf += size;
785         }
786
787         if (force_sync)
788                 handle->h_sync = 1; /* recovery likes this */
789 out:
790         if (bh)
791                 brelse(bh);
792
793         /* correct in-core and on-disk sizes */
794         if (new_size > inode->i_size) {
795                 lock_kernel();
796                 if (new_size > inode->i_size)
797                         inode->i_size = new_size;
798                 if (inode->i_size > EXT3_I(inode)->i_disksize)
799                         EXT3_I(inode)->i_disksize = inode->i_size;
800                 if (inode->i_size > old_size)
801                         mark_inode_dirty(inode);
802                 unlock_kernel();
803         }
804
805         lock_kernel();
806         journal_stop(handle);
807         unlock_kernel();
808
809         if (err == 0)
810                 *offs = offset;
811         return err;
812 }
813
814 static int fsfilt_ext3_setup(struct super_block *sb)
815 {
816 #if 0
817         EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
818         EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
819 #endif
820 #ifdef S_PDIROPS
821         CWARN("Enabling PDIROPS\n");
822         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
823         sb->s_flags |= S_PDIROPS;
824 #endif
825         return 0;
826 }
827
828 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
829                                  void *buffer, int buffer_size)
830 {
831         int rc = 0;
832
833         lock_kernel();
834
835         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
836                                    name, buffer, buffer_size, 0);
837         unlock_kernel();
838         if (rc)
839                 CERROR("set xattr %s from inode %lu: rc %d\n",
840                        name,  inode->i_ino, rc);
841         return rc;
842 }
843
844 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
845                                  void *buffer, int buffer_size)
846 {
847         int rc = 0;
848         lock_kernel();
849
850         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
851                             name, buffer, buffer_size);
852         unlock_kernel();
853
854         if (buffer == NULL)
855                 return (rc == -ENODATA) ? 0 : rc;
856         if (rc < 0) {
857                 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
858                        name,  inode->i_ino, rc);
859                 memset(buffer, 0, buffer_size);
860                 return (rc == -ENODATA) ? 0 : rc;
861         }
862
863         return rc;
864 }
865
866 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
867    objects. Logs is number of logfiles to update */
868 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
869 {
870         if ( !fso ) {
871                 switch(op) {
872                 case FSFILT_OP_CREATE:
873                                  /* directory leaf, index & indirect & EA*/
874                         return 4 + 3 * logs;
875                 case FSFILT_OP_UNLINK:
876                         return 3 * logs;
877                 }
878         } else {
879                 int i;
880                 int needed = 0;
881                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
882                 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
883                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
884                 for (i = 0; i < op; i++, fso++) {
885                         int nblocks = fso->fso_bufcnt * blockpp;
886                         int ndindirect = min(nblocks, addrpp + 1);
887                         int nindir = nblocks + ndindirect + 1;
888
889                         needed += nindir;
890                 }
891                 return needed + 3 * logs;
892         }
893
894         return 0;
895 }
896
897 static struct fsfilt_operations fsfilt_ext3_ops = {
898         .fs_type                = "ext3",
899         .fs_owner               = THIS_MODULE,
900         .fs_start               = fsfilt_ext3_start,
901         .fs_brw_start           = fsfilt_ext3_brw_start,
902         .fs_commit              = fsfilt_ext3_commit,
903         .fs_commit_async        = fsfilt_ext3_commit_async,
904         .fs_commit_wait         = fsfilt_ext3_commit_wait,
905         .fs_setattr             = fsfilt_ext3_setattr,
906         .fs_iocontrol           = fsfilt_ext3_iocontrol,
907         .fs_set_md              = fsfilt_ext3_set_md,
908         .fs_get_md              = fsfilt_ext3_get_md,
909         .fs_readpage            = fsfilt_ext3_readpage,
910         .fs_add_journal_cb      = fsfilt_ext3_add_journal_cb,
911         .fs_statfs              = fsfilt_ext3_statfs,
912         .fs_sync                = fsfilt_ext3_sync,
913         .fs_map_inode_page      = fsfilt_ext3_map_inode_page,
914         .fs_prep_san_write      = fsfilt_ext3_prep_san_write,
915         .fs_write_record        = fsfilt_ext3_write_record,
916         .fs_read_record         = fsfilt_ext3_read_record,
917         .fs_setup               = fsfilt_ext3_setup,
918         .fs_getpage             = fsfilt_ext3_getpage,
919         .fs_send_bio            = fsfilt_ext3_send_bio,
920         .fs_set_xattr           = fsfilt_ext3_set_xattr,
921         .fs_get_xattr           = fsfilt_ext3_get_xattr,
922         .fs_get_op_len          = fsfilt_ext3_get_op_len,
923 };
924
925 static int __init fsfilt_ext3_init(void)
926 {
927         int rc;
928
929         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
930                                       sizeof(struct fsfilt_cb_data), 0,
931                                       0, NULL, NULL);
932         if (!fcb_cache) {
933                 CERROR("error allocating fsfilt journal callback cache\n");
934                 GOTO(out, rc = -ENOMEM);
935         }
936
937         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
938
939         if (rc)
940                 kmem_cache_destroy(fcb_cache);
941 out:
942         return rc;
943 }
944
945 static void __exit fsfilt_ext3_exit(void)
946 {
947         int rc;
948
949         fsfilt_unregister_ops(&fsfilt_ext3_ops);
950         rc = kmem_cache_destroy(fcb_cache);
951
952         if (rc || atomic_read(&fcb_cache_count)) {
953                 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
954                        atomic_read(&fcb_cache_count), rc);
955         }
956 }
957
958 module_init(fsfilt_ext3_init);
959 module_exit(fsfilt_ext3_exit);
960
961 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
962 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
963 MODULE_LICENSE("GPL");