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