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