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