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