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