Whamcloud - gitweb
LU-1199 build: Cleanup ldiskfs kernel config defines
[fs/lustre-release.git] / lustre / lvfs / fsfilt_ext3.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lvfs/fsfilt_ext3.c
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/fs.h>
46 #include <linux/slab.h>
47 #include <linux/pagemap.h>
48 #include <ldiskfs/ldiskfs_config.h>
49 #include <ext4/ext4.h>
50 #include <ext4/ext4_jbd2.h>
51 #include <linux/version.h>
52 #include <linux/bitops.h>
53 #include <linux/quota.h>
54
55 #include <libcfs/libcfs.h>
56 #include <lustre_fsfilt.h>
57 #include <obd.h>
58 #include <linux/lustre_compat25.h>
59 #include <linux/lprocfs_status.h>
60
61 #include <ext4/ext4_extents.h>
62
63 /* for kernels 2.6.18 and later */
64 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS(sb)
65
66 #define fsfilt_ext3_ext_insert_extent(handle, inode, path, newext, flag) \
67                ext3_ext_insert_extent(handle, inode, path, newext, flag)
68
69 #define ext3_mb_discard_inode_preallocations(inode) \
70                  ext3_discard_preallocations(inode)
71
72 #define fsfilt_log_start_commit(journal, tid) jbd2_log_start_commit(journal, tid)
73 #define fsfilt_log_wait_commit(journal, tid) jbd2_log_wait_commit(journal, tid)
74
75 static cfs_mem_cache_t *fcb_cache;
76
77 struct fsfilt_cb_data {
78         struct ext4_journal_cb_entry cb_jcb; /* private data - MUST BE FIRST */
79         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
80         struct obd_device *cb_obd;      /* MDS/OBD completion device */
81         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
82         void *cb_data;                  /* MDS/OST completion function data */
83 };
84
85 static char *fsfilt_ext3_get_label(struct super_block *sb)
86 {
87         return EXT3_SB(sb)->s_es->s_volume_name;
88 }
89
90 /* kernel has ext4_blocks_for_truncate since linux-3.1.1 */
91 #ifdef HAVE_BLOCKS_FOR_TRUNCATE
92 # include <ext4/truncate.h>
93 #else
94 static inline unsigned long ext4_blocks_for_truncate(struct inode *inode)
95 {
96         ext4_lblk_t needed;
97
98         needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
99         if (needed < 2)
100                 needed = 2;
101         if (needed > EXT4_MAX_TRANS_DATA)
102                 needed = EXT4_MAX_TRANS_DATA;
103         return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
104 }
105 #endif
106
107 /*
108  * We don't currently need any additional blocks for rmdir and
109  * unlink transactions because we are storing the OST oa_id inside
110  * the inode (which we will be changing anyways as part of this
111  * transaction).
112  */
113 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
114                                int logs)
115 {
116         /* For updates to the last received file */
117         int nblocks = FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
118         journal_t *journal;
119         void *handle;
120
121         if (current->journal_info) {
122                 CDEBUG(D_INODE, "increasing refcount on %p\n",
123                        current->journal_info);
124                 goto journal_start;
125         }
126
127         switch(op) {
128         case FSFILT_OP_UNLINK:
129                 /* delete one file + create/update logs for each stripe */
130                 nblocks += EXT3_DELETE_TRANS_BLOCKS(inode->i_sb);
131                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
132                             FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
133                 break;
134         case FSFILT_OP_CANCEL_UNLINK:
135                 LASSERT(logs == 1);
136
137                 /* blocks for log header bitmap update OR
138                  * blocks for catalog header bitmap update + unlink of logs +
139                  * blocks for delete the inode (include blocks truncating). */
140                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
141                           EXT3_DELETE_TRANS_BLOCKS(inode->i_sb) +
142                           ext4_blocks_for_truncate(inode) + 3;
143                 break;
144         default: CERROR("unknown transaction start op %d\n", op);
145                 LBUG();
146         }
147
148         LASSERT(current->journal_info == desc_private);
149         journal = EXT3_SB(inode->i_sb)->s_journal;
150         if (nblocks > journal->j_max_transaction_buffers) {
151                 CWARN("too many credits %d for op %ux%u using %d instead\n",
152                        nblocks, op, logs, journal->j_max_transaction_buffers);
153                 nblocks = journal->j_max_transaction_buffers;
154         }
155
156  journal_start:
157         LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
158         handle = ext3_journal_start(inode, nblocks);
159
160         if (!IS_ERR(handle))
161                 LASSERT(current->journal_info == handle);
162         else
163                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
164                        op, nblocks, PTR_ERR(handle));
165         return handle;
166 }
167
168 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
169 {
170         int rc;
171         handle_t *handle = h;
172
173         LASSERT(current->journal_info == handle);
174         if (force_sync)
175                 handle->h_sync = 1; /* recovery likes this */
176
177         rc = ext3_journal_stop(handle);
178
179         return rc;
180 }
181
182 #ifndef EXT3_EXTENTS_FL
183 #define EXT3_EXTENTS_FL                 0x00080000 /* Inode uses extents */
184 #endif
185
186 #ifndef EXT_ASSERT
187 #define EXT_ASSERT(cond)  BUG_ON(!(cond))
188 #endif
189
190 #define EXT_GENERATION(inode)           (EXT4_I(inode)->i_ext_generation)
191 #define ext3_ext_base                   inode
192 #define ext3_ext_base2inode(inode)      (inode)
193 #define EXT_DEPTH(inode)                ext_depth(inode)
194 #define fsfilt_ext3_ext_walk_space(inode, block, num, cb, cbdata) \
195                         ext3_ext_walk_space(inode, block, num, cb, cbdata);
196
197 struct bpointers {
198         unsigned long *blocks;
199         unsigned long start;
200         int num;
201         int init_num;
202         int create;
203 };
204
205 static long ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
206                                unsigned long block, int *aflags)
207 {
208         struct ext3_inode_info *ei = EXT3_I(inode);
209         unsigned long bg_start;
210         unsigned long colour;
211         int depth;
212
213         if (path) {
214                 struct ext3_extent *ex;
215                 depth = path->p_depth;
216
217                 /* try to predict block placement */
218                 if ((ex = path[depth].p_ext))
219                         return ext_pblock(ex) + (block - le32_to_cpu(ex->ee_block));
220
221                 /* it looks index is empty
222                  * try to find starting from index itself */
223                 if (path[depth].p_bh)
224                         return path[depth].p_bh->b_blocknr;
225         }
226
227         /* OK. use inode's group */
228         bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
229                 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
230         colour = (current->pid % 16) *
231                 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
232         return bg_start + colour + block;
233 }
234
235 #define ll_unmap_underlying_metadata(sb, blocknr) \
236         unmap_underlying_metadata((sb)->s_bdev, blocknr)
237
238 #ifndef EXT3_MB_HINT_GROUP_ALLOC
239 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
240                                 struct ext3_ext_path *path, unsigned long block,
241                                 unsigned long *count, int *err)
242 {
243         unsigned long pblock, goal;
244         int aflags = 0;
245         struct inode *inode = ext3_ext_base2inode(base);
246
247         goal = ext3_ext_find_goal(inode, path, block, &aflags);
248         aflags |= 2; /* block have been already reserved */
249         pblock = ext3_mb_new_blocks(handle, inode, goal, count, aflags, err);
250         return pblock;
251
252 }
253 #else
254 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
255                                 struct ext3_ext_path *path, unsigned long block,
256                                 unsigned long *count, int *err)
257 {
258         struct inode *inode = ext3_ext_base2inode(base);
259         struct ext3_allocation_request ar;
260         unsigned long pblock;
261         int aflags;
262
263         /* find neighbour allocated blocks */
264         ar.lleft = block;
265         *err = ext3_ext_search_left(base, path, &ar.lleft, &ar.pleft);
266         if (*err)
267                 return 0;
268         ar.lright = block;
269         *err = ext3_ext_search_right(base, path, &ar.lright, &ar.pright);
270         if (*err)
271                 return 0;
272
273         /* allocate new block */
274         ar.goal = ext3_ext_find_goal(inode, path, block, &aflags);
275         ar.inode = inode;
276         ar.logical = block;
277         ar.len = *count;
278         ar.flags = EXT3_MB_HINT_DATA;
279         pblock = ext3_mb_new_blocks(handle, &ar, err);
280         *count = ar.len;
281         return pblock;
282 }
283 #endif
284
285 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
286                                   struct ext3_ext_path *path,
287                                   struct ext3_ext_cache *cex,
288 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
289                                    struct ext3_extent *ex,
290 #endif
291                                   void *cbdata)
292 {
293         struct bpointers *bp = cbdata;
294         struct inode *inode = ext3_ext_base2inode(base);
295         struct ext3_extent nex;
296         unsigned long pblock;
297         unsigned long tgen;
298         int err, i;
299         unsigned long count;
300         handle_t *handle;
301
302 #ifdef EXT3_EXT_CACHE_EXTENT
303         if (cex->ec_type == EXT3_EXT_CACHE_EXTENT)
304 #else
305         if ((cex->ec_len != 0) && (cex->ec_start != 0))
306 #endif
307                                                    {
308                 err = EXT_CONTINUE;
309                 goto map;
310         }
311
312         if (bp->create == 0) {
313                 i = 0;
314                 if (cex->ec_block < bp->start)
315                         i = bp->start - cex->ec_block;
316                 if (i >= cex->ec_len)
317                         CERROR("nothing to do?! i = %d, e_num = %u\n",
318                                         i, cex->ec_len);
319                 for (; i < cex->ec_len && bp->num; i++) {
320                         *(bp->blocks) = 0;
321                         bp->blocks++;
322                         bp->num--;
323                         bp->start++;
324                 }
325
326                 return EXT_CONTINUE;
327         }
328
329         tgen = EXT_GENERATION(base);
330         count = ext3_ext_calc_credits_for_insert(base, path);
331
332         handle = ext3_journal_start(inode, count+EXT3_ALLOC_NEEDED+1);
333         if (IS_ERR(handle)) {
334                 return PTR_ERR(handle);
335         }
336
337         if (tgen != EXT_GENERATION(base)) {
338                 /* the tree has changed. so path can be invalid at moment */
339                 ext3_journal_stop(handle);
340                 return EXT_REPEAT;
341         }
342
343         /* In 2.6.32 kernel, ext4_ext_walk_space()'s callback func is not
344          * protected by i_data_sem as whole. so we patch it to store
345          * generation to path and now verify the tree hasn't changed */
346         down_write((&EXT4_I(inode)->i_data_sem));
347
348         /* validate extent, make sure the extent tree does not changed */
349         if (EXT_GENERATION(base) != path[0].p_generation) {
350                 /* cex is invalid, try again */
351                 up_write(&EXT4_I(inode)->i_data_sem);
352                 ext3_journal_stop(handle);
353                 return EXT_REPEAT;
354         }
355
356         count = cex->ec_len;
357         pblock = new_blocks(handle, base, path, cex->ec_block, &count, &err);
358         if (!pblock)
359                 goto out;
360         EXT_ASSERT(count <= cex->ec_len);
361
362         /* insert new extent */
363         nex.ee_block = cpu_to_le32(cex->ec_block);
364         ext3_ext_store_pblock(&nex, pblock);
365         nex.ee_len = cpu_to_le16(count);
366         err = fsfilt_ext3_ext_insert_extent(handle, base, path, &nex, 0);
367         if (err) {
368                 /* free data blocks we just allocated */
369                 /* not a good idea to call discard here directly,
370                  * but otherwise we'd need to call it every free() */
371 #ifdef EXT3_MB_HINT_GROUP_ALLOC
372                 ext3_mb_discard_inode_preallocations(inode);
373 #endif
374                 ext3_free_blocks(handle, inode, ext_pblock(&nex),
375                                  cpu_to_le16(nex.ee_len), 0);
376                 goto out;
377         }
378
379         /*
380          * Putting len of the actual extent we just inserted,
381          * we are asking ext3_ext_walk_space() to continue
382          * scaning after that block
383          */
384         cex->ec_len = le16_to_cpu(nex.ee_len);
385         cex->ec_start = ext_pblock(&nex);
386         BUG_ON(le16_to_cpu(nex.ee_len) == 0);
387         BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
388
389 out:
390         up_write((&EXT4_I(inode)->i_data_sem));
391         ext3_journal_stop(handle);
392 map:
393         if (err >= 0) {
394                 /* map blocks */
395                 if (bp->num == 0) {
396                         CERROR("hmm. why do we find this extent?\n");
397                         CERROR("initial space: %lu:%u\n",
398                                 bp->start, bp->init_num);
399 #ifdef EXT3_EXT_CACHE_EXTENT
400                         CERROR("current extent: %u/%u/%llu %d\n",
401                                 cex->ec_block, cex->ec_len,
402                                 (unsigned long long)cex->ec_start,
403                                 cex->ec_type);
404 #else
405                         CERROR("current extent: %u/%u/%llu\n",
406                                 cex->ec_block, cex->ec_len,
407                                 (unsigned long long)cex->ec_start);
408 #endif
409                 }
410                 i = 0;
411                 if (cex->ec_block < bp->start)
412                         i = bp->start - cex->ec_block;
413                 if (i >= cex->ec_len)
414                         CERROR("nothing to do?! i = %d, e_num = %u\n",
415                                         i, cex->ec_len);
416                 for (; i < cex->ec_len && bp->num; i++) {
417                         *(bp->blocks) = cex->ec_start + i;
418 #ifdef EXT3_EXT_CACHE_EXTENT
419                         if (cex->ec_type != EXT3_EXT_CACHE_EXTENT)
420 #else
421                         if ((cex->ec_len == 0) || (cex->ec_start == 0))
422 #endif
423                                                                         {
424                                 /* unmap any possible underlying metadata from
425                                  * the block device mapping.  bug 6998. */
426                                 ll_unmap_underlying_metadata(inode->i_sb,
427                                                              *(bp->blocks));
428                         }
429                         bp->blocks++;
430                         bp->num--;
431                         bp->start++;
432                 }
433         }
434         return err;
435 }
436
437 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
438                        unsigned long num, unsigned long *blocks,
439                        int create)
440 {
441         struct ext3_ext_base *base = inode;
442         struct bpointers bp;
443         int err;
444
445         CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
446                block, block + num - 1, (unsigned) inode->i_ino);
447
448         bp.blocks = blocks;
449         bp.start = block;
450         bp.init_num = bp.num = num;
451         bp.create = create;
452
453         err = fsfilt_ext3_ext_walk_space(base, block, num,
454                                          ext3_ext_new_extent_cb, &bp);
455         ext3_ext_invalidate_cache(base);
456
457         return err;
458 }
459
460 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
461                                     int pages, unsigned long *blocks,
462                                     int create)
463 {
464         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
465         int rc = 0, i = 0;
466         struct page *fp = NULL;
467         int clen = 0;
468
469         CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
470                 inode->i_ino, pages, (*page)->index);
471
472         /* pages are sorted already. so, we just have to find
473          * contig. space and process them properly */
474         while (i < pages) {
475                 if (fp == NULL) {
476                         /* start new extent */
477                         fp = *page++;
478                         clen = 1;
479                         i++;
480                         continue;
481                 } else if (fp->index + clen == (*page)->index) {
482                         /* continue the extent */
483                         page++;
484                         clen++;
485                         i++;
486                         continue;
487                 }
488
489                 /* process found extent */
490                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
491                                         clen * blocks_per_page, blocks,
492                                         create);
493                 if (rc)
494                         GOTO(cleanup, rc);
495
496                 /* look for next extent */
497                 fp = NULL;
498                 blocks += blocks_per_page * clen;
499         }
500
501         if (fp)
502                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
503                                         clen * blocks_per_page, blocks,
504                                         create);
505 cleanup:
506         return rc;
507 }
508
509 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
510                                    int pages, unsigned long *blocks,
511                                    int create)
512 {
513         int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
514         unsigned long *b;
515         int rc = 0, i;
516
517         for (i = 0, b = blocks; i < pages; i++, page++) {
518                 rc = ext3_map_inode_page(inode, *page, b, create);
519                 if (rc) {
520                         CERROR("ino %lu, blk %lu create %d: rc %d\n",
521                                inode->i_ino, *b, create, rc);
522                         break;
523                 }
524
525                 b += blocks_per_page;
526         }
527         return rc;
528 }
529
530 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
531                                 int pages, unsigned long *blocks,
532                                 int create, struct mutex *optional_mutex)
533 {
534         int rc;
535
536         if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
537                 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
538                                                      blocks, create);
539                 return rc;
540         }
541         if (optional_mutex != NULL)
542                 mutex_lock(optional_mutex);
543         rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks, create);
544         if (optional_mutex != NULL)
545                 mutex_unlock(optional_mutex);
546
547         return rc;
548 }
549
550 int fsfilt_ext3_read(struct inode *inode, void *buf, int size, loff_t *offs)
551 {
552         unsigned long block;
553         struct buffer_head *bh;
554         int err, blocksize, csize, boffs, osize = size;
555
556         /* prevent reading after eof */
557         spin_lock(&inode->i_lock);
558         if (i_size_read(inode) < *offs + size) {
559                 size = i_size_read(inode) - *offs;
560                 spin_unlock(&inode->i_lock);
561                 if (size < 0) {
562                         CDEBUG(D_EXT2, "size %llu is too short for read @%llu\n",
563                                i_size_read(inode), *offs);
564                         return -EBADR;
565                 } else if (size == 0) {
566                         return 0;
567                 }
568         } else {
569                 spin_unlock(&inode->i_lock);
570         }
571
572         blocksize = 1 << inode->i_blkbits;
573
574         while (size > 0) {
575                 block = *offs >> inode->i_blkbits;
576                 boffs = *offs & (blocksize - 1);
577                 csize = min(blocksize - boffs, size);
578                 bh = ext3_bread(NULL, inode, block, 0, &err);
579                 if (!bh) {
580                         CERROR("can't read block: %d\n", err);
581                         return err;
582                 }
583
584                 memcpy(buf, bh->b_data + boffs, csize);
585                 brelse(bh);
586
587                 *offs += csize;
588                 buf += csize;
589                 size -= csize;
590         }
591         return osize;
592 }
593 EXPORT_SYMBOL(fsfilt_ext3_read);
594
595 static int fsfilt_ext3_read_record(struct file * file, void *buf,
596                                    int size, loff_t *offs)
597 {
598         int rc;
599         rc = fsfilt_ext3_read(file->f_dentry->d_inode, buf, size, offs);
600         if (rc > 0)
601                 rc = 0;
602         return rc;
603 }
604
605 int fsfilt_ext3_write_handle(struct inode *inode, void *buf, int bufsize,
606                                 loff_t *offs, handle_t *handle)
607 {
608         struct buffer_head *bh = NULL;
609         loff_t old_size = i_size_read(inode), offset = *offs;
610         loff_t new_size = i_size_read(inode);
611         unsigned long block;
612         int err = 0, blocksize = 1 << inode->i_blkbits, size, boffs;
613
614         while (bufsize > 0) {
615                 if (bh != NULL)
616                         brelse(bh);
617
618                 block = offset >> inode->i_blkbits;
619                 boffs = offset & (blocksize - 1);
620                 size = min(blocksize - boffs, bufsize);
621                 bh = ext3_bread(handle, inode, block, 1, &err);
622                 if (!bh) {
623                         CERROR("can't read/create block: %d\n", err);
624                         break;
625                 }
626
627                 err = ext3_journal_get_write_access(handle, bh);
628                 if (err) {
629                         CERROR("journal_get_write_access() returned error %d\n",
630                                err);
631                         break;
632                 }
633                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
634                 memcpy(bh->b_data + boffs, buf, size);
635                 err = ext3_journal_dirty_metadata(handle, bh);
636                 if (err) {
637                         CERROR("journal_dirty_metadata() returned error %d\n",
638                                err);
639                         break;
640                 }
641                 if (offset + size > new_size)
642                         new_size = offset + size;
643                 offset += size;
644                 bufsize -= size;
645                 buf += size;
646         }
647         if (bh)
648                 brelse(bh);
649
650         /* correct in-core and on-disk sizes */
651         if (new_size > i_size_read(inode)) {
652                 spin_lock(&inode->i_lock);
653                 if (new_size > i_size_read(inode))
654                         i_size_write(inode, new_size);
655                 if (i_size_read(inode) > EXT3_I(inode)->i_disksize)
656                         EXT3_I(inode)->i_disksize = i_size_read(inode);
657                 if (i_size_read(inode) > old_size) {
658                         spin_unlock(&inode->i_lock);
659                         mark_inode_dirty(inode);
660                 } else {
661                         spin_unlock(&inode->i_lock);
662                 }
663         }
664
665         if (err == 0)
666                 *offs = offset;
667         return err;
668 }
669 EXPORT_SYMBOL(fsfilt_ext3_write_handle);
670
671 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
672                                     loff_t *offs, int force_sync)
673 {
674         struct inode *inode = file->f_dentry->d_inode;
675         handle_t *handle;
676         int err, block_count = 0, blocksize;
677
678         /* Determine how many transaction credits are needed */
679         blocksize = 1 << inode->i_blkbits;
680         block_count = (*offs & (blocksize - 1)) + bufsize;
681         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
682
683         handle = ext3_journal_start(inode,
684                         block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2);
685         if (IS_ERR(handle)) {
686                 CERROR("can't start transaction for %d blocks (%d bytes)\n",
687                        block_count * EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + 2,
688                        bufsize);
689                 return PTR_ERR(handle);
690         }
691
692         err = fsfilt_ext3_write_handle(inode, buf, bufsize, offs, handle);
693
694         if (!err && force_sync)
695                 handle->h_sync = 1; /* recovery likes this */
696
697         ext3_journal_stop(handle);
698
699         return err;
700 }
701
702 static int fsfilt_ext3_setup(struct super_block *sb)
703 {
704         if (!EXT3_HAS_COMPAT_FEATURE(sb,
705                                 EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
706                 CERROR("ext3 mounted without journal\n");
707                 return -EINVAL;
708         }
709
710 #ifdef S_PDIROPS
711         CWARN("Enabling PDIROPS\n");
712         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
713         sb->s_flags |= S_PDIROPS;
714 #endif
715         if (!EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
716                 CWARN("filesystem doesn't have dir_index feature enabled\n");
717         return 0;
718 }
719 static struct fsfilt_operations fsfilt_ext3_ops = {
720         .fs_type                = "ext3",
721         .fs_owner               = THIS_MODULE,
722         .fs_getlabel            = fsfilt_ext3_get_label,
723         .fs_start               = fsfilt_ext3_start,
724         .fs_commit              = fsfilt_ext3_commit,
725         .fs_map_inode_pages     = fsfilt_ext3_map_inode_pages,
726         .fs_write_record        = fsfilt_ext3_write_record,
727         .fs_read_record         = fsfilt_ext3_read_record,
728         .fs_setup               = fsfilt_ext3_setup,
729 };
730
731 static int __init fsfilt_ext3_init(void)
732 {
733         int rc;
734
735         fcb_cache = cfs_mem_cache_create("fsfilt_ext3_fcb",
736                                          sizeof(struct fsfilt_cb_data), 0, 0);
737         if (!fcb_cache) {
738                 CERROR("error allocating fsfilt journal callback cache\n");
739                 GOTO(out, rc = -ENOMEM);
740         }
741
742         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
743
744         if (rc) {
745                 int err = cfs_mem_cache_destroy(fcb_cache);
746                 LASSERTF(err == 0, "error destroying new cache: rc %d\n", err);
747         }
748 out:
749         return rc;
750 }
751
752 static void __exit fsfilt_ext3_exit(void)
753 {
754         int rc;
755
756         fsfilt_unregister_ops(&fsfilt_ext3_ops);
757         rc = cfs_mem_cache_destroy(fcb_cache);
758         LASSERTF(rc == 0, "couldn't destroy fcb_cache slab\n");
759 }
760
761 module_init(fsfilt_ext3_init);
762 module_exit(fsfilt_ext3_exit);
763
764 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
765 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
766 MODULE_LICENSE("GPL");