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