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