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