Whamcloud - gitweb
13f7a1663214aa1a0334ceeebf23f47aac1ad51a
[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/bitops.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
40 #include <linux/ext3_xattr.h>
41 #else
42 #include <ext3/xattr.h>
43 #endif
44
45 #include <linux/kp30.h>
46 #include <linux/lustre_fsfilt.h>
47 #include <linux/obd.h>
48 #include <linux/obd_class.h>
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
50 #include <linux/iobuf.h>
51 #endif
52
53 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
54 #include <linux/ext3_extents.h>
55 #endif
56
57 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
58 # define lock_24kernel() lock_kernel()
59 # define unlock_24kernel() unlock_kernel()
60 #else
61 # define lock_24kernel() do {} while (0)
62 # define unlock_24kernel() do {} while (0)
63 #endif
64
65 static kmem_cache_t *fcb_cache;
66 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
67
68 struct fsfilt_cb_data {
69         struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
70         fsfilt_cb_t cb_func;            /* MDS/OBD completion function */
71         struct obd_device *cb_obd;      /* MDS/OBD completion device */
72         __u64 cb_last_rcvd;             /* MDS/OST last committed operation */
73         void *cb_data;                  /* MDS/OST completion function data */
74 };
75
76 #ifndef EXT3_XATTR_INDEX_TRUSTED        /* temporary until we hit l28 kernel */
77 #define EXT3_XATTR_INDEX_TRUSTED        4
78 #endif
79 #define XATTR_LUSTRE_MDS_LOV_EA         "lov"
80
81 /*
82  * We don't currently need any additional blocks for rmdir and
83  * unlink transactions because we are storing the OST oa_id inside
84  * the inode (which we will be changing anyways as part of this
85  * transaction).
86  */
87 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
88                                int logs)
89 {
90         /* For updates to the last recieved file */
91         int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
92         journal_t *journal;
93         void *handle;
94
95         if (current->journal_info) {
96                 CDEBUG(D_INODE, "increasing refcount on %p\n",
97                        current->journal_info);
98                 goto journal_start;
99         }
100
101         switch(op) {
102         case FSFILT_OP_RMDIR:
103         case FSFILT_OP_UNLINK:
104                 /* delete one file + create/update logs for each stripe */
105                 nblocks += EXT3_DELETE_TRANS_BLOCKS;
106                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
107                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
108                 break;
109         case FSFILT_OP_RENAME:
110                 /* modify additional directory */
111                 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
112                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
113                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
114                 /* no break */
115         case FSFILT_OP_SYMLINK:
116                 /* additional block + block bitmap + GDT for long symlink */
117                 nblocks += 3;
118                 /* no break */
119         case FSFILT_OP_CREATE:
120                 /* create/update logs for each stripe */
121                 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
122                             EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
123                 /* no break */
124         case FSFILT_OP_MKDIR:
125         case FSFILT_OP_MKNOD:
126                 /* modify one inode + block bitmap + GDT */
127                 nblocks += 3;
128                 /* no break */
129         case FSFILT_OP_LINK:
130                 /* modify parent directory */
131                 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
132                         EXT3_DATA_TRANS_BLOCKS;
133                 break;
134         case FSFILT_OP_SETATTR:
135                 /* Setattr on inode */
136                 nblocks += 1;
137                 break;
138         case FSFILT_OP_CANCEL_UNLINK:
139                 /* blocks for log header bitmap update OR
140                  * blocks for catalog header bitmap update + unlink of logs */
141                 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
142                         EXT3_DELETE_TRANS_BLOCKS * logs;
143                 break;
144         default: CERROR("unknown transaction start op %d\n", op);
145                 LBUG();
146         }
147
148         LASSERT(current->journal_info == desc_private);
149         journal = EXT3_SB(inode->i_sb)->s_journal;
150         if (nblocks > journal->j_max_transaction_buffers) {
151                 CERROR("too many credits %d for op %ux%u using %d instead\n",
152                        nblocks, op, logs, journal->j_max_transaction_buffers);
153                 nblocks = journal->j_max_transaction_buffers;
154         }
155
156  journal_start:
157         LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
158         lock_24kernel();
159         handle = journal_start(EXT3_JOURNAL(inode), nblocks);
160         unlock_24kernel();
161
162         if (!IS_ERR(handle))
163                 LASSERT(current->journal_info == handle);
164         else
165                 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
166                        op, nblocks, PTR_ERR(handle));
167         return handle;
168 }
169
170 /*
171  * Calculate the number of buffer credits needed to write multiple pages in
172  * a single ext3 transaction.  No, this shouldn't be here, but as yet ext3
173  * doesn't have a nice API for calculating this sort of thing in advance.
174  *
175  * See comment above ext3_writepage_trans_blocks for details.  We assume
176  * no data journaling is being done, but it does allow for all of the pages
177  * being non-contiguous.  If we are guaranteed contiguous pages we could
178  * reduce the number of (d)indirect blocks a lot.
179  *
180  * With N blocks per page and P pages, for each inode we have at most:
181  * N*P indirect
182  * min(N*P, blocksize/4 + 1) dindirect blocks
183  * niocount tindirect
184  *
185  * For the entire filesystem, we have at most:
186  * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
187  * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
188  * objcount inode blocks
189  * 1 superblock
190  * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
191  *
192  * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
193  */
194 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
195                                       int niocount, struct niobuf_local *nb)
196 {
197         struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
198         __u64 next_indir;
199         const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
200         int nbitmaps = 0, ngdblocks;
201         int needed = objcount + 1; /* inodes + superblock */
202         int i, j;
203
204         for (i = 0, j = 0; i < objcount; i++, fso++) {
205                 /* two or more dindirect blocks in case we cross boundary */
206                 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
207                                     nb[j].offset) >>
208                                    sb->s_blocksize_bits) /
209                         (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
210                 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
211
212                 /* leaf, indirect, tindirect blocks for first block */
213                 nbitmaps += blockpp + 2;
214
215                 j += fso->fso_bufcnt;
216         }
217
218         next_indir = nb[0].offset +
219                 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
220         for (i = 1; i < niocount; i++) {
221                 if (nb[i].offset >= next_indir) {
222                         nbitmaps++;     /* additional indirect */
223                         next_indir = nb[i].offset +
224                                 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
225                 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
226                         nbitmaps++;     /* additional indirect */
227                 }
228                 nbitmaps += blockpp;    /* each leaf in different group? */
229         }
230
231         ngdblocks = nbitmaps;
232         if (nbitmaps > EXT3_SB(sb)->s_groups_count)
233                 nbitmaps = EXT3_SB(sb)->s_groups_count;
234         if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
235                 ngdblocks = EXT3_SB(sb)->s_gdb_count;
236
237         needed += nbitmaps + ngdblocks;
238
239         /* last_rcvd update */
240         needed += EXT3_DATA_TRANS_BLOCKS;
241
242 #if defined(CONFIG_QUOTA)
243         /* We assume that there will be 1 bit set in s_dquot.flags for each
244          * quota file that is active.  This is at least true for now.
245          */
246         needed += hweight32(sb_any_quota_enabled(sb)) *
247                 EXT3_SINGLEDATA_TRANS_BLOCKS;
248 #endif
249
250         return needed;
251 }
252
253 /* We have to start a huge journal transaction here to hold all of the
254  * metadata for the pages being written here.  This is necessitated by
255  * the fact that we do lots of prepare_write operations before we do
256  * any of the matching commit_write operations, so even if we split
257  * up to use "smaller" transactions none of them could complete until
258  * all of them were opened.  By having a single journal transaction,
259  * we eliminate duplicate reservations for common blocks like the
260  * superblock and group descriptors or bitmaps.
261  *
262  * We will start the transaction here, but each prepare_write will
263  * add a refcount to the transaction, and each commit_write will
264  * remove a refcount.  The transaction will be closed when all of
265  * the pages have been written.
266  */
267 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
268                                    int niocount, struct niobuf_local *nb,
269                                    void *desc_private, int logs)
270 {
271         journal_t *journal;
272         handle_t *handle;
273         int needed;
274         ENTRY;
275
276         LASSERT(current->journal_info == desc_private);
277         journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
278         needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
279
280         /* The number of blocks we could _possibly_ dirty can very large.
281          * We reduce our request if it is absurd (and we couldn't get that
282          * many credits for a single handle anyways).
283          *
284          * At some point we have to limit the size of I/Os sent at one time,
285          * increase the size of the journal, or we have to calculate the
286          * actual journal requirements more carefully by checking all of
287          * the blocks instead of being maximally pessimistic.  It remains to
288          * be seen if this is a real problem or not.
289          */
290         if (needed > journal->j_max_transaction_buffers) {
291                 CERROR("want too many journal credits (%d) using %d instead\n",
292                        needed, journal->j_max_transaction_buffers);
293                 needed = journal->j_max_transaction_buffers;
294         }
295
296         LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
297         lock_24kernel();
298         handle = journal_start(journal, needed);
299         unlock_24kernel();
300         if (IS_ERR(handle)) {
301                 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
302                        PTR_ERR(handle));
303         } else {
304                 LASSERT(handle->h_buffer_credits >= needed);
305                 LASSERT(current->journal_info == handle);
306         }
307
308         RETURN(handle);
309 }
310
311 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
312 {
313         int rc;
314         handle_t *handle = h;
315
316         LASSERT(current->journal_info == handle);
317         if (force_sync)
318                 handle->h_sync = 1; /* recovery likes this */
319
320         lock_24kernel();
321         rc = journal_stop(handle);
322         unlock_24kernel();
323
324         return rc;
325 }
326
327 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
328                                     void **wait_handle)
329 {
330         unsigned long tid;
331         transaction_t *transaction;
332 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
333         unsigned long rtid;
334 #endif
335         handle_t *handle = h;
336         journal_t *journal;
337         int rc;
338
339         LASSERT(current->journal_info == handle);
340
341         lock_kernel();
342         transaction = handle->h_transaction;
343         journal = transaction->t_journal;
344         tid = transaction->t_tid;
345         /* we don't want to be blocked */
346         handle->h_sync = 0;
347         rc = journal_stop(handle);
348         if (rc) {
349                 CERROR("error while stopping transaction: %d\n", rc);
350                 unlock_kernel();
351                 return rc;
352         }
353 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
354         rtid = log_start_commit(journal, transaction);
355         if (rtid != tid)
356                 CERROR("strange race: %lu != %lu\n",
357                        (unsigned long) tid, (unsigned long) rtid);
358 #else
359         log_start_commit(journal, transaction->t_tid);
360 #endif
361         unlock_kernel();
362
363         *wait_handle = (void *) tid;
364         CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
365         return 0;
366 }
367
368 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
369 {
370         tid_t tid = (tid_t)(long)h;
371
372         CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
373         if (is_journal_aborted(EXT3_JOURNAL(inode)))
374                 return -EIO;
375
376         log_wait_commit(EXT3_JOURNAL(inode), tid);
377
378         return 0;
379 }
380
381 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
382                                struct iattr *iattr, int do_trunc)
383 {
384         struct inode *inode = dentry->d_inode;
385         int rc;
386
387         lock_kernel();
388
389         /* A _really_ horrible hack to avoid removing the data stored
390          * in the block pointers; this is really the "small" stripe MD data.
391          * We can avoid further hackery by virtue of the MDS file size being
392          * zero all the time (which doesn't invoke block truncate at unlink
393          * time), so we assert we never change the MDS file size from zero. */
394         if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
395                 /* ATTR_SIZE would invoke truncate: clear it */
396                 iattr->ia_valid &= ~ATTR_SIZE;
397                 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
398
399                 /* make sure _something_ gets set - so new inode
400                  * goes to disk (probably won't work over XFS */
401                 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
402                         iattr->ia_valid |= ATTR_MODE;
403                         iattr->ia_mode = inode->i_mode;
404                 }
405         }
406
407         /* Don't allow setattr to change file type */
408         iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
409
410         /* We set these flags on the client, but have already checked perms
411          * so don't confuse inode_change_ok. */
412         iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
413
414         if (inode->i_op->setattr) {
415                 rc = inode->i_op->setattr(dentry, iattr);
416         } else {
417                 rc = inode_change_ok(inode, iattr);
418                 if (!rc)
419                         rc = inode_setattr(inode, iattr);
420         }
421
422         unlock_kernel();
423
424         return rc;
425 }
426
427 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
428                                  unsigned int cmd, unsigned long arg)
429 {
430         int rc = 0;
431         ENTRY;
432
433         if (inode->i_fop->ioctl)
434                 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
435         else
436                 RETURN(-ENOTTY);
437
438         RETURN(rc);
439 }
440
441 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
442                               void *lmm, int lmm_size)
443 {
444         int rc;
445
446         LASSERT(down_trylock(&inode->i_sem) != 0);
447
448         if (EXT3_I(inode)->i_file_acl /* || large inode EA flag */)
449                 CWARN("setting EA on %lu/%u again... interesting\n",
450                        inode->i_ino, inode->i_generation);
451
452         lock_kernel();
453         rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
454                                    XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
455
456         unlock_kernel();
457
458         if (rc)
459                 CERROR("error adding MD data to inode %lu: rc = %d\n",
460                        inode->i_ino, rc);
461         return rc;
462 }
463
464 /* Must be called with i_sem held */
465 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
466 {
467         int rc;
468
469         LASSERT(down_trylock(&inode->i_sem) != 0);
470         lock_kernel();
471
472         rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
473                             XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
474         unlock_kernel();
475
476         /* This gives us the MD size */
477         if (lmm == NULL)
478                 return (rc == -ENODATA) ? 0 : rc;
479
480         if (rc < 0) {
481                 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
482                        EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
483                        inode->i_ino, rc);
484                 memset(lmm, 0, lmm_size);
485                 return (rc == -ENODATA) ? 0 : rc;
486         }
487
488         return rc;
489 }
490
491 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
492 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, struct bio *bio)
493 {
494         submit_bio(rw, bio);
495         return 0;
496 }
497 #else
498 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, struct kiobuf *bio)
499 {
500         int rc, blocks_per_page;
501
502         rc = brw_kiovec(rw, 1, &bio, inode->i_dev,
503                         bio->blocks, 1 << inode->i_blkbits);
504
505         blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
506
507         if (rc != (1 << inode->i_blkbits) * bio->nr_pages * blocks_per_page) {
508                 CERROR("short write?  expected %d, wrote %d\n",
509                        (1 << inode->i_blkbits) * bio->nr_pages *
510                        blocks_per_page, rc);
511         }
512
513         return rc;
514 }
515 #endif
516
517 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
518                                     loff_t *off)
519 {
520         struct inode *inode = file->f_dentry->d_inode;
521         int rc = 0;
522
523         if (S_ISREG(inode->i_mode))
524                 rc = file->f_op->read(file, buf, count, off);
525         else {
526                 const int blkbits = inode->i_sb->s_blocksize_bits;
527                 const int blksize = inode->i_sb->s_blocksize;
528
529                 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
530                        count, inode->i_ino, *off);
531                 while (count > 0) {
532                         struct buffer_head *bh;
533
534                         bh = NULL;
535                         if (*off < inode->i_size) {
536                                 int err = 0;
537
538                                 bh = ext3_bread(NULL, inode, *off >> blkbits,
539                                                 0, &err);
540
541                                 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
542
543                                 if (bh) {
544                                         memcpy(buf, bh->b_data, blksize);
545                                         brelse(bh);
546                                 } else if (err) {
547                                         /* XXX in theory we should just fake
548                                          * this buffer and continue like ext3,
549                                          * especially if this is a partial read
550                                          */
551                                         CERROR("error read dir %lu+%llu: %d\n",
552                                                inode->i_ino, *off, err);
553                                         RETURN(err);
554                                 }
555                         }
556                         if (!bh) {
557                                 struct ext3_dir_entry_2 *fake = (void *)buf;
558
559                                 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
560                                 memset(fake, 0, sizeof(*fake));
561                                 fake->rec_len = cpu_to_le32(blksize);
562                         }
563                         count -= blksize;
564                         buf += blksize;
565                         *off += blksize;
566                         rc += blksize;
567                 }
568         }
569
570         return rc;
571 }
572
573 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
574 {
575         struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
576
577         fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
578
579         OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
580         atomic_dec(&fcb_cache_count);
581 }
582
583 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
584                                       void *handle, fsfilt_cb_t cb_func,
585                                       void *cb_data)
586 {
587         struct fsfilt_cb_data *fcb;
588
589         OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
590         if (fcb == NULL)
591                 RETURN(-ENOMEM);
592
593         atomic_inc(&fcb_cache_count);
594         fcb->cb_func = cb_func;
595         fcb->cb_obd = obd;
596         fcb->cb_last_rcvd = last_rcvd;
597         fcb->cb_data = cb_data;
598
599         CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
600         lock_24kernel();
601         journal_callback_set(handle, fsfilt_ext3_cb_func,
602                              (struct journal_callback *)fcb);
603         unlock_24kernel();
604
605         return 0;
606 }
607
608 /*
609  * We need to hack the return value for the free inode counts because
610  * the current EA code requires one filesystem block per inode with EAs,
611  * so it is possible to run out of blocks before we run out of inodes.
612  *
613  * This can be removed when the ext3 EA code is fixed.
614  */
615 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
616 {
617         struct kstatfs sfs;
618         int rc;
619
620         memset(&sfs, 0, sizeof(sfs));
621
622         rc = sb->s_op->statfs(sb, &sfs);
623
624         if (!rc && sfs.f_bfree < sfs.f_ffree) {
625                 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
626                 sfs.f_ffree = sfs.f_bfree;
627         }
628
629         statfs_pack(osfs, &sfs);
630         return rc;
631 }
632
633 static int fsfilt_ext3_sync(struct super_block *sb)
634 {
635         return ext3_force_commit(sb);
636 }
637
638 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
639 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
640 #define ext3_up_truncate_sem(inode)  up_write(&EXT3_I(inode)->truncate_sem);
641 #define ext3_down_truncate_sem(inode)  down_write(&EXT3_I(inode)->truncate_sem);
642 #else
643 #define ext3_up_truncate_sem(inode)  up(&EXT3_I(inode)->truncate_sem);
644 #define ext3_down_truncate_sem(inode)  down(&EXT3_I(inode)->truncate_sem);
645 #endif
646
647 #include <linux/lustre_version.h>
648 #if EXT3_EXT_MAGIC == 0xf301
649 #define ee_start e_start
650 #define ee_block e_block
651 #define ee_len   e_num
652 #endif
653 #ifndef EXT3_BB_MAX_BLOCKS
654 #define ext3_mb_new_blocks(handle, inode, goal, count, aflags, err) \
655         ext3_new_blocks(handle, inode, count, goal, err)
656 #endif
657
658 struct bpointers {
659         unsigned long *blocks;
660         int *created;
661         unsigned long start;
662         int num;
663         int init_num;
664         int create;
665 };
666
667 static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
668                               unsigned long block, int *aflags)
669 {
670         struct ext3_inode_info *ei = EXT3_I(inode);
671         unsigned long bg_start;
672         unsigned long colour;
673         int depth;
674
675         if (path) {
676                 struct ext3_extent *ex;
677                 depth = path->p_depth;
678
679                 /* try to predict block placement */
680                 if ((ex = path[depth].p_ext)) {
681 #if 0
682                         /* This prefers to eat into a contiguous extent
683                          * rather than find an extent that the whole
684                          * request will fit into.  This can fragment data
685                          * block allocation and prevents our lovely 1M I/Os
686                          * from reaching the disk intact. */
687                         if (ex->ee_block + ex->ee_len == block)
688                                 *aflags |= 1;
689 #endif
690                         return ex->ee_start + (block - ex->ee_block);
691                 }
692
693                 /* it looks index is empty
694                  * try to find starting from index itself */
695                 if (path[depth].p_bh)
696                         return path[depth].p_bh->b_blocknr;
697         }
698
699         /* OK. use inode's group */
700         bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
701                 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
702         colour = (current->pid % 16) *
703                 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
704         return bg_start + colour + block;
705 }
706
707 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
708                                   struct ext3_ext_path *path,
709                                   struct ext3_extent *newex, int exist)
710 {
711         struct inode *inode = tree->inode;
712         struct bpointers *bp = tree->private;
713         int count, err, goal;
714         unsigned long pblock;
715         unsigned long tgen;
716         loff_t new_i_size;
717         handle_t *handle;
718         int i, aflags = 0;
719
720         i = EXT_DEPTH(tree);
721         EXT_ASSERT(i == path->p_depth);
722         EXT_ASSERT(path[i].p_hdr);
723
724         if (exist) {
725                 err = EXT_CONTINUE;
726                 goto map;
727         }
728
729         if (bp->create == 0) {
730                 i = 0;
731                 if (newex->ee_block < bp->start)
732                         i = bp->start - newex->ee_block;
733                 if (i >= newex->ee_len)
734                         CERROR("nothing to do?! i = %d, e_num = %u\n",
735                                         i, newex->ee_len);
736                 for (; i < newex->ee_len && bp->num; i++) {
737                         *(bp->created) = 0;
738                         bp->created++;
739                         *(bp->blocks) = 0;
740                         bp->blocks++;
741                         bp->num--;
742                         bp->start++;
743                 }
744
745                 return EXT_CONTINUE;
746         }
747
748         tgen = EXT_GENERATION(tree);
749         count = ext3_ext_calc_credits_for_insert(tree, path);
750         ext3_up_truncate_sem(inode);
751
752         lock_24kernel();
753         handle = journal_start(EXT3_JOURNAL(inode), count + EXT3_ALLOC_NEEDED + 1);
754         unlock_24kernel();
755         if (IS_ERR(handle)) {
756                 ext3_down_truncate_sem(inode);
757                 return PTR_ERR(handle);
758         }
759
760         if (tgen != EXT_GENERATION(tree)) {
761                 /* the tree has changed. so path can be invalid at moment */
762                 lock_24kernel();
763                 journal_stop(handle);
764                 unlock_24kernel();
765                 ext3_down_truncate_sem(inode);
766                 return EXT_REPEAT;
767         }
768
769         ext3_down_truncate_sem(inode);
770         count = newex->ee_len;
771         goal = ext3_ext_find_goal(inode, path, newex->ee_block, &aflags);
772         aflags |= 2; /* block have been already reserved */
773         pblock = ext3_mb_new_blocks(handle, inode, goal, &count, aflags, &err);
774         if (!pblock)
775                 goto out;
776         EXT_ASSERT(count <= newex->ee_len);
777
778         /* insert new extent */
779         newex->ee_start = pblock;
780         newex->ee_len = count;
781         err = ext3_ext_insert_extent(handle, tree, path, newex);
782         if (err)
783                 goto out;
784
785         /* correct on-disk inode size */
786         if (newex->ee_len > 0) {
787                 new_i_size = (loff_t) newex->ee_block + newex->ee_len;
788                 new_i_size = new_i_size << inode->i_blkbits;
789                 if (new_i_size > EXT3_I(inode)->i_disksize) {
790                         EXT3_I(inode)->i_disksize = new_i_size;
791                         err = ext3_mark_inode_dirty(handle, inode);
792                 }
793         }
794
795 out:
796         lock_24kernel();
797         journal_stop(handle);
798         unlock_24kernel();
799 map:
800         if (err >= 0) {
801                 /* map blocks */
802                 if (bp->num == 0) {
803                         CERROR("hmm. why do we find this extent?\n");
804                         CERROR("initial space: %lu:%u\n",
805                                 bp->start, bp->init_num);
806                         CERROR("current extent: %u/%u/%u %d\n",
807                                 newex->ee_block, newex->ee_len,
808                                 newex->ee_start, exist);
809                 }
810                 i = 0;
811                 if (newex->ee_block < bp->start)
812                         i = bp->start - newex->ee_block;
813                 if (i >= newex->ee_len)
814                         CERROR("nothing to do?! i = %d, e_num = %u\n",
815                                         i, newex->ee_len);
816                 for (; i < newex->ee_len && bp->num; i++) {
817                         *(bp->created) = (exist == 0 ? 1 : 0);
818                         bp->created++;
819                         *(bp->blocks) = newex->ee_start + i;
820                         bp->blocks++;
821                         bp->num--;
822                         bp->start++;
823                 }
824         }
825         return err;
826 }
827
828 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
829                        unsigned long num, unsigned long *blocks,
830                        int *created, int create)
831 {
832         struct ext3_extents_tree tree;
833         struct bpointers bp;
834         int err;
835
836         CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
837                 block, block + num, (unsigned) inode->i_ino);
838
839         ext3_init_tree_desc(&tree, inode);
840         tree.private = &bp;
841         bp.blocks = blocks;
842         bp.created = created;
843         bp.start = block;
844         bp.init_num = bp.num = num;
845         bp.create = create;
846
847         ext3_down_truncate_sem(inode);
848         err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
849         ext3_ext_invalidate_cache(&tree);
850         ext3_up_truncate_sem(inode);
851
852         return err;
853 }
854
855 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
856                                     int pages, unsigned long *blocks,
857                                     int *created, int create)
858 {
859         int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
860         int rc = 0, i = 0;
861         struct page *fp = NULL;
862         int clen = 0;
863
864         CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
865                 inode->i_ino, pages, (*page)->index);
866
867         /* pages are sorted already. so, we just have to find
868          * contig. space and process them properly */
869         while (i < pages) {
870                 if (fp == NULL) {
871                         /* start new extent */
872                         fp = *page++;
873                         clen = 1;
874                         i++;
875                         continue;
876                 } else if (fp->index + clen == (*page)->index) {
877                         /* continue the extent */
878                         page++;
879                         clen++;
880                         i++;
881                         continue;
882                 }
883
884                 /* process found extent */
885                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
886                                         clen * blocks_per_page, blocks,
887                                         created, create);
888                 if (rc)
889                         GOTO(cleanup, rc);
890
891                 /* look for next extent */
892                 fp = NULL;
893                 blocks += blocks_per_page * clen;
894                 created += blocks_per_page * clen;
895         }
896
897         if (fp)
898                 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
899                                         clen * blocks_per_page, blocks,
900                                         created, create);
901 cleanup:
902         return rc;
903 }
904 #endif
905
906 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
907                                unsigned long *blocks, int *created, int create);
908 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
909                                    int pages, unsigned long *blocks,
910                                    int *created, int create)
911 {
912         int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
913         unsigned long *b;
914         int rc = 0, i, *cr;
915
916         for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
917                 rc = ext3_map_inode_page(inode, *page, b, cr, create);
918                 if (rc) {
919                         CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
920                                inode->i_ino, *b, *cr, create, rc);
921                         break;
922                 }
923
924                 b += blocks_per_page;
925                 cr += blocks_per_page;
926         }
927         return rc;
928 }
929
930 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
931                                 int pages, unsigned long *blocks,
932                                 int *created, int create,
933                                 struct semaphore *optional_sem)
934 {
935         int rc;
936 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
937         if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
938                 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
939                                                      blocks, created, create);
940                 return rc;
941         }
942 #endif
943         if (optional_sem != NULL)
944                 down(optional_sem);
945         rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
946                                             created, create);
947         if (optional_sem != NULL)
948                 up(optional_sem);
949
950         return rc;
951 }
952
953 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
954                                int nblocks, loff_t newsize);
955 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
956                                       int nblocks, loff_t newsize)
957 {
958         return ext3_prep_san_write(inode, blocks, nblocks, newsize);
959 }
960
961 static int fsfilt_ext3_read_record(struct file * file, void *buf,
962                                    int size, loff_t *offs)
963 {
964         struct inode *inode = file->f_dentry->d_inode;
965         unsigned long block;
966         struct buffer_head *bh;
967         int err, blocksize, csize, boffs;
968
969         /* prevent reading after eof */
970         lock_kernel();
971         if (inode->i_size < *offs + size) {
972                 size = inode->i_size - *offs;
973                 unlock_kernel();
974                 if (size < 0) {
975                         CERROR("size %llu is too short for read %u@%llu\n",
976                                inode->i_size, size, *offs);
977                         return -EIO;
978                 } else if (size == 0) {
979                         return 0;
980                 }
981         } else {
982                 unlock_kernel();
983         }
984
985         blocksize = 1 << inode->i_blkbits;
986
987         while (size > 0) {
988                 block = *offs >> inode->i_blkbits;
989                 boffs = *offs & (blocksize - 1);
990                 csize = min(blocksize - boffs, size);
991                 bh = ext3_bread(NULL, inode, block, 0, &err);
992                 if (!bh) {
993                         CERROR("can't read block: %d\n", err);
994                         return err;
995                 }
996
997                 memcpy(buf, bh->b_data + boffs, csize);
998                 brelse(bh);
999
1000                 *offs += csize;
1001                 buf += csize;
1002                 size -= csize;
1003         }
1004         return 0;
1005 }
1006
1007 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1008                                     loff_t *offs, int force_sync)
1009 {
1010         struct buffer_head *bh = NULL;
1011         unsigned long block;
1012         struct inode *inode = file->f_dentry->d_inode;
1013         loff_t old_size = inode->i_size, offset = *offs;
1014         loff_t new_size = inode->i_size;
1015         journal_t *journal;
1016         handle_t *handle;
1017         int err, block_count = 0, blocksize, size, boffs;
1018
1019         /* Determine how many transaction credits are needed */
1020         blocksize = 1 << inode->i_blkbits;
1021         block_count = (*offs & (blocksize - 1)) + bufsize;
1022         block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1023
1024         journal = EXT3_SB(inode->i_sb)->s_journal;
1025         lock_24kernel();
1026         handle = journal_start(journal,
1027                                block_count * EXT3_DATA_TRANS_BLOCKS + 2);
1028         unlock_24kernel();
1029         if (IS_ERR(handle)) {
1030                 CERROR("can't start transaction\n");
1031                 return PTR_ERR(handle);
1032         }
1033
1034         while (bufsize > 0) {
1035                 if (bh != NULL)
1036                         brelse(bh);
1037
1038                 block = offset >> inode->i_blkbits;
1039                 boffs = offset & (blocksize - 1);
1040                 size = min(blocksize - boffs, bufsize);
1041                 bh = ext3_bread(handle, inode, block, 1, &err);
1042                 if (!bh) {
1043                         CERROR("can't read/create block: %d\n", err);
1044                         goto out;
1045                 }
1046
1047                 err = ext3_journal_get_write_access(handle, bh);
1048                 if (err) {
1049                         CERROR("journal_get_write_access() returned error %d\n",
1050                                err);
1051                         goto out;
1052                 }
1053                 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1054                 memcpy(bh->b_data + boffs, buf, size);
1055                 err = ext3_journal_dirty_metadata(handle, bh);
1056                 if (err) {
1057                         CERROR("journal_dirty_metadata() returned error %d\n",
1058                                err);
1059                         goto out;
1060                 }
1061                 if (offset + size > new_size)
1062                         new_size = offset + size;
1063                 offset += size;
1064                 bufsize -= size;
1065                 buf += size;
1066         }
1067
1068         if (force_sync)
1069                 handle->h_sync = 1; /* recovery likes this */
1070 out:
1071         if (bh)
1072                 brelse(bh);
1073
1074         /* correct in-core and on-disk sizes */
1075         if (new_size > inode->i_size) {
1076                 lock_kernel();
1077                 if (new_size > inode->i_size)
1078                         inode->i_size = new_size;
1079                 if (inode->i_size > EXT3_I(inode)->i_disksize)
1080                         EXT3_I(inode)->i_disksize = inode->i_size;
1081                 if (inode->i_size > old_size)
1082                         mark_inode_dirty(inode);
1083                 unlock_kernel();
1084         }
1085
1086         lock_24kernel();
1087         journal_stop(handle);
1088         unlock_24kernel();
1089
1090         if (err == 0)
1091                 *offs = offset;
1092         return err;
1093 }
1094
1095 static int fsfilt_ext3_setup(struct super_block *sb)
1096 {
1097 #if 0
1098         EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1099         EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1100 #endif
1101 #ifdef S_PDIROPS
1102         CWARN("Enabling PDIROPS\n");
1103         set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1104         sb->s_flags |= S_PDIROPS;
1105 #endif
1106         return 0;
1107 }
1108
1109 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1110    objects. Logs is number of logfiles to update */
1111 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1112 {
1113         if ( !fso ) {
1114                 switch(op) {
1115                 case FSFILT_OP_CREATE:
1116                                  /* directory leaf, index & indirect & EA*/
1117                         return 4 + 3 * logs;
1118                 case FSFILT_OP_UNLINK:
1119                         return 3 * logs;
1120                 }
1121         } else {
1122                 int i;
1123                 int needed = 0;
1124                 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1125                 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1126                 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1127                 for (i = 0; i < op; i++, fso++) {
1128                         int nblocks = fso->fso_bufcnt * blockpp;
1129                         int ndindirect = min(nblocks, addrpp + 1);
1130                         int nindir = nblocks + ndindirect + 1;
1131
1132                         needed += nindir;
1133                 }
1134                 return needed + 3 * logs;
1135         }
1136
1137         return 0;
1138 }
1139
1140 static struct fsfilt_operations fsfilt_ext3_ops = {
1141         .fs_type                = "ext3",
1142         .fs_owner               = THIS_MODULE,
1143         .fs_start               = fsfilt_ext3_start,
1144         .fs_brw_start           = fsfilt_ext3_brw_start,
1145         .fs_commit              = fsfilt_ext3_commit,
1146         .fs_commit_async        = fsfilt_ext3_commit_async,
1147         .fs_commit_wait         = fsfilt_ext3_commit_wait,
1148         .fs_setattr             = fsfilt_ext3_setattr,
1149         .fs_iocontrol           = fsfilt_ext3_iocontrol,
1150         .fs_set_md              = fsfilt_ext3_set_md,
1151         .fs_get_md              = fsfilt_ext3_get_md,
1152         .fs_readpage            = fsfilt_ext3_readpage,
1153         .fs_add_journal_cb      = fsfilt_ext3_add_journal_cb,
1154         .fs_statfs              = fsfilt_ext3_statfs,
1155         .fs_sync                = fsfilt_ext3_sync,
1156         .fs_map_inode_pages     = fsfilt_ext3_map_inode_pages,
1157         .fs_prep_san_write      = fsfilt_ext3_prep_san_write,
1158         .fs_write_record        = fsfilt_ext3_write_record,
1159         .fs_read_record         = fsfilt_ext3_read_record,
1160         .fs_setup               = fsfilt_ext3_setup,
1161         .fs_send_bio            = fsfilt_ext3_send_bio,
1162         .fs_get_op_len          = fsfilt_ext3_get_op_len,
1163 };
1164
1165 static int __init fsfilt_ext3_init(void)
1166 {
1167         int rc;
1168
1169         fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1170                                       sizeof(struct fsfilt_cb_data), 0,
1171                                       0, NULL, NULL);
1172         if (!fcb_cache) {
1173                 CERROR("error allocating fsfilt journal callback cache\n");
1174                 GOTO(out, rc = -ENOMEM);
1175         }
1176
1177         rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1178
1179         if (rc)
1180                 kmem_cache_destroy(fcb_cache);
1181 out:
1182         return rc;
1183 }
1184
1185 static void __exit fsfilt_ext3_exit(void)
1186 {
1187         fsfilt_unregister_ops(&fsfilt_ext3_ops);
1188         LASSERTF(kmem_cache_destroy(fcb_cache) == 0,
1189                  "can't free fsfilt callback cache: count %d\n",
1190                  atomic_read(&fcb_cache_count));
1191 }
1192
1193 module_init(fsfilt_ext3_init);
1194 module_exit(fsfilt_ext3_exit);
1195
1196 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1197 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1198 MODULE_LICENSE("GPL");