1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lib/fsfilt_ext3.c
5 * Lustre filesystem abstraction routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_FILTER
28 #include <linux/init.h>
29 #include <linux/module.h>
31 #include <linux/jbd.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
35 #include <linux/ext3_fs.h>
36 #include <linux/ext3_jbd.h>
37 #include <linux/version.h>
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
39 #include <linux/ext3_xattr.h>
41 #include <ext3/xattr.h>
44 #include <linux/kp30.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/obd.h>
47 #include <linux/obd_class.h>
48 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
49 #include <linux/module.h>
50 #include <linux/iobuf.h>
53 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
54 #include <linux/ext3_extents.h>
57 static kmem_cache_t *fcb_cache;
58 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
60 struct fsfilt_cb_data {
61 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
62 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
63 struct obd_device *cb_obd; /* MDS/OBD completion device */
64 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
65 void *cb_data; /* MDS/OST completion function data */
68 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
69 #define EXT3_XATTR_INDEX_TRUSTED 4
71 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
74 * We don't currently need any additional blocks for rmdir and
75 * unlink transactions because we are storing the OST oa_id inside
76 * the inode (which we will be changing anyways as part of this
79 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
82 /* For updates to the last recieved file */
83 int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
87 if (current->journal_info) {
88 CDEBUG(D_INODE, "increasing refcount on %p\n",
89 current->journal_info);
93 /* XXX BUG 3188 -- must return to one set of opcodes */
94 /* FIXME - cache hook */
96 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
102 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
107 case FSFILT_OP_RMDIR:
108 case FSFILT_OP_UNLINK:
109 /* delete one file + create/update logs for each stripe */
110 nblocks += EXT3_DELETE_TRANS_BLOCKS;
111 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
112 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
114 case FSFILT_OP_RENAME:
115 /* modify additional directory */
116 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
118 case FSFILT_OP_SYMLINK:
119 /* additional block + block bitmap + GDT for long symlink */
122 case FSFILT_OP_CREATE:
123 /* create/update logs for each stripe */
124 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
125 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
127 case FSFILT_OP_MKDIR:
128 case FSFILT_OP_MKNOD:
129 /* modify one inode + block bitmap + GDT */
133 /* modify parent directory */
134 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
135 EXT3_DATA_TRANS_BLOCKS;
137 case FSFILT_OP_SETATTR:
138 /* Setattr on inode */
141 case FSFILT_OP_CANCEL_UNLINK:
142 /* blocks for log header bitmap update OR
143 * blocks for catalog header bitmap update + unlink of logs */
144 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
145 EXT3_DELETE_TRANS_BLOCKS * logs;
148 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
150 default: CERROR("unknown transaction start op %d\n", op);
154 LASSERT(current->journal_info == desc_private);
155 journal = EXT3_SB(inode->i_sb)->s_journal;
156 if (nblocks > journal->j_max_transaction_buffers) {
157 CERROR("too many credits %d for op %ux%u using %d instead\n",
158 nblocks, op, logs, journal->j_max_transaction_buffers);
159 nblocks = journal->j_max_transaction_buffers;
164 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
168 LASSERT(current->journal_info == handle);
170 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
171 op, nblocks, PTR_ERR(handle));
176 * Calculate the number of buffer credits needed to write multiple pages in
177 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
178 * doesn't have a nice API for calculating this sort of thing in advance.
180 * See comment above ext3_writepage_trans_blocks for details. We assume
181 * no data journaling is being done, but it does allow for all of the pages
182 * being non-contiguous. If we are guaranteed contiguous pages we could
183 * reduce the number of (d)indirect blocks a lot.
185 * With N blocks per page and P pages, for each inode we have at most:
187 * min(N*P, blocksize/4 + 1) dindirect blocks
190 * For the entire filesystem, we have at most:
191 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
192 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
193 * objcount inode blocks
195 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
197 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
199 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
200 int niocount, struct niobuf_local *nb)
202 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
204 const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
205 int nbitmaps = 0, ngdblocks;
206 int needed = objcount + 1; /* inodes + superblock */
209 for (i = 0, j = 0; i < objcount; i++, fso++) {
210 /* two or more dindirect blocks in case we cross boundary */
211 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
213 sb->s_blocksize_bits) /
214 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
215 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
217 /* leaf, indirect, tindirect blocks for first block */
218 nbitmaps += blockpp + 2;
220 j += fso->fso_bufcnt;
223 next_indir = nb[0].offset +
224 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
225 for (i = 1; i < niocount; i++) {
226 if (nb[i].offset >= next_indir) {
227 nbitmaps++; /* additional indirect */
228 next_indir = nb[i].offset +
229 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
230 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
231 nbitmaps++; /* additional indirect */
233 nbitmaps += blockpp; /* each leaf in different group? */
236 ngdblocks = nbitmaps;
237 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
238 nbitmaps = EXT3_SB(sb)->s_groups_count;
239 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
240 ngdblocks = EXT3_SB(sb)->s_gdb_count;
242 needed += nbitmaps + ngdblocks;
244 /* last_rcvd update */
245 needed += EXT3_DATA_TRANS_BLOCKS;
247 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
248 /* We assume that there will be 1 bit set in s_dquot.flags for each
249 * quota file that is active. This is at least true for now.
251 needed += hweight32(sb_any_quota_enabled(sb)) *
252 EXT3_SINGLEDATA_TRANS_BLOCKS;
258 /* We have to start a huge journal transaction here to hold all of the
259 * metadata for the pages being written here. This is necessitated by
260 * the fact that we do lots of prepare_write operations before we do
261 * any of the matching commit_write operations, so even if we split
262 * up to use "smaller" transactions none of them could complete until
263 * all of them were opened. By having a single journal transaction,
264 * we eliminate duplicate reservations for common blocks like the
265 * superblock and group descriptors or bitmaps.
267 * We will start the transaction here, but each prepare_write will
268 * add a refcount to the transaction, and each commit_write will
269 * remove a refcount. The transaction will be closed when all of
270 * the pages have been written.
272 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
273 int niocount, struct niobuf_local *nb,
274 void *desc_private, int logs)
281 LASSERT(current->journal_info == desc_private);
282 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
283 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
285 /* The number of blocks we could _possibly_ dirty can very large.
286 * We reduce our request if it is absurd (and we couldn't get that
287 * many credits for a single handle anyways).
289 * At some point we have to limit the size of I/Os sent at one time,
290 * increase the size of the journal, or we have to calculate the
291 * actual journal requirements more carefully by checking all of
292 * the blocks instead of being maximally pessimistic. It remains to
293 * be seen if this is a real problem or not.
295 if (needed > journal->j_max_transaction_buffers) {
296 CERROR("want too many journal credits (%d) using %d instead\n",
297 needed, journal->j_max_transaction_buffers);
298 needed = journal->j_max_transaction_buffers;
302 handle = journal_start(journal, needed);
304 if (IS_ERR(handle)) {
305 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
308 LASSERT(handle->h_buffer_credits >= needed);
309 LASSERT(current->journal_info == handle);
315 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
318 handle_t *handle = h;
320 LASSERT(current->journal_info == handle);
322 handle->h_sync = 1; /* recovery likes this */
325 rc = journal_stop(handle);
331 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
335 transaction_t *transaction;
336 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
339 handle_t *handle = h;
343 LASSERT(current->journal_info == handle);
346 transaction = handle->h_transaction;
347 journal = transaction->t_journal;
348 tid = transaction->t_tid;
349 /* we don't want to be blocked */
351 rc = journal_stop(handle);
353 CERROR("error while stopping transaction: %d\n", rc);
357 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
358 rtid = log_start_commit(journal, transaction);
360 CERROR("strange race: %lu != %lu\n",
361 (unsigned long) tid, (unsigned long) rtid);
363 log_start_commit(journal, transaction->t_tid);
367 *wait_handle = (void *) tid;
368 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
372 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
374 tid_t tid = (tid_t)(long)h;
376 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
377 if (is_journal_aborted(EXT3_JOURNAL(inode)))
380 log_wait_commit(EXT3_JOURNAL(inode), tid);
385 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
386 struct iattr *iattr, int do_trunc)
388 struct inode *inode = dentry->d_inode;
393 /* A _really_ horrible hack to avoid removing the data stored
394 * in the block pointers; this is really the "small" stripe MD data.
395 * We can avoid further hackery by virtue of the MDS file size being
396 * zero all the time (which doesn't invoke block truncate at unlink
397 * time), so we assert we never change the MDS file size from zero. */
398 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
399 /* ATTR_SIZE would invoke truncate: clear it */
400 iattr->ia_valid &= ~ATTR_SIZE;
401 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
403 /* make sure _something_ gets set - so new inode
404 * goes to disk (probably won't work over XFS */
405 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
406 iattr->ia_valid |= ATTR_MODE;
407 iattr->ia_mode = inode->i_mode;
411 /* Don't allow setattr to change file type */
412 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
414 /* We set these flags on the client, but have already checked perms
415 * so don't confuse inode_change_ok. */
416 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
418 if (inode->i_op->setattr) {
419 rc = inode->i_op->setattr(dentry, iattr);
421 rc = inode_change_ok(inode, iattr);
423 rc = inode_setattr(inode, iattr);
431 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
432 unsigned int cmd, unsigned long arg)
437 if (inode->i_fop->ioctl)
438 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
445 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
446 void *lmm, int lmm_size)
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);
456 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
457 XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
462 CERROR("error adding MD data to inode %lu: rc = %d\n",
467 /* Must be called with i_sem held */
468 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
472 LASSERT(down_trylock(&inode->i_sem) != 0);
475 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
476 XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
479 /* This gives us the MD size */
481 return (rc == -ENODATA) ? 0 : rc;
484 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
485 EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
487 memset(lmm, 0, lmm_size);
488 return (rc == -ENODATA) ? 0 : rc;
494 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
495 static int fsfilt_ext3_send_bio(struct inode *inode, struct bio *bio)
497 submit_bio(WRITE, bio);
501 static int fsfilt_ext3_send_bio(struct inode *inode, struct kiobuf *bio)
503 int rc, blocks_per_page;
505 rc = brw_kiovec(WRITE, 1, &bio, inode->i_dev,
506 bio->blocks, 1 << inode->i_blkbits);
508 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
510 if (rc != (1 << inode->i_blkbits) * bio->nr_pages *
512 CERROR("short write? expected %d, wrote %d\n",
513 (1 << inode->i_blkbits) * bio->nr_pages *
514 blocks_per_page, rc);
521 /* FIXME-UMKA: This should be used in 2.6.x io code later. */
522 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
527 page = grab_cache_page(inode->i_mapping, index);
529 return ERR_PTR(-ENOMEM);
531 if (PageUptodate(page)) {
536 rc = inode->i_mapping->a_ops->readpage(NULL, page);
538 page_cache_release(page);
545 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
548 struct inode *inode = file->f_dentry->d_inode;
551 if (S_ISREG(inode->i_mode))
552 rc = file->f_op->read(file, buf, count, off);
554 const int blkbits = inode->i_sb->s_blocksize_bits;
555 const int blksize = inode->i_sb->s_blocksize;
557 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
558 count, inode->i_ino, *off);
560 struct buffer_head *bh;
563 if (*off < inode->i_size) {
566 bh = ext3_bread(NULL, inode, *off >> blkbits,
569 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
572 memcpy(buf, bh->b_data, blksize);
575 /* XXX in theory we should just fake
576 * this buffer and continue like ext3,
577 * especially if this is a partial read
579 CERROR("error read dir %lu+%llu: %d\n",
580 inode->i_ino, *off, err);
585 struct ext3_dir_entry_2 *fake = (void *)buf;
587 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
588 memset(fake, 0, sizeof(*fake));
589 fake->rec_len = cpu_to_le32(blksize);
601 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
603 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
605 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
607 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
608 atomic_dec(&fcb_cache_count);
611 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
612 struct super_block *sb,
614 void *handle, fsfilt_cb_t cb_func,
617 struct fsfilt_cb_data *fcb;
619 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
623 atomic_inc(&fcb_cache_count);
624 fcb->cb_func = cb_func;
626 fcb->cb_last_rcvd = last_rcvd;
627 fcb->cb_data = cb_data;
629 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
631 journal_callback_set(handle, fsfilt_ext3_cb_func,
632 (struct journal_callback *)fcb);
639 * We need to hack the return value for the free inode counts because
640 * the current EA code requires one filesystem block per inode with EAs,
641 * so it is possible to run out of blocks before we run out of inodes.
643 * This can be removed when the ext3 EA code is fixed.
645 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
650 memset(&sfs, 0, sizeof(sfs));
652 rc = sb->s_op->statfs(sb, &sfs);
654 if (!rc && sfs.f_bfree < sfs.f_ffree) {
655 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
656 sfs.f_ffree = sfs.f_bfree;
659 statfs_pack(osfs, &sfs);
663 static int fsfilt_ext3_sync(struct super_block *sb)
665 return ext3_force_commit(sb);
668 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
670 unsigned long *blocks;
677 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
678 struct ext3_ext_path *path,
679 struct ext3_extent *newex, int exist)
681 struct inode *inode = tree->inode;
682 struct bpointers *bp = tree->private;
683 int count, err, goal;
684 unsigned long pblock;
691 EXT_ASSERT(i == path->p_depth);
692 EXT_ASSERT(path[i].p_hdr);
699 tgen = EXT_GENERATION(tree);
700 count = ext3_ext_calc_credits_for_insert(tree, path);
701 up_write(&EXT3_I(inode)->truncate_sem);
703 handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
704 if (IS_ERR(handle)) {
705 down_write(&EXT3_I(inode)->truncate_sem);
706 return PTR_ERR(handle);
709 if (tgen != EXT_GENERATION(tree)) {
710 /* the tree has changed. so path can be invalid at moment */
711 ext3_journal_stop(handle, inode);
712 down_write(&EXT3_I(inode)->truncate_sem);
716 down_write(&EXT3_I(inode)->truncate_sem);
717 goal = ext3_ext_find_goal(inode, path, newex->e_block);
718 count = newex->e_num;
719 pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
722 EXT_ASSERT(count <= newex->e_num);
724 /* insert new extent */
725 newex->e_start = pblock;
726 newex->e_num = count;
727 err = ext3_ext_insert_extent(handle, tree, path, newex);
731 /* correct on-disk inode size */
732 if (newex->e_num > 0) {
733 new_i_size = (loff_t) newex->e_block + newex->e_num;
734 new_i_size = new_i_size << inode->i_blkbits;
735 if (new_i_size > EXT3_I(inode)->i_disksize) {
736 EXT3_I(inode)->i_disksize = new_i_size;
737 err = ext3_mark_inode_dirty(handle, inode);
742 ext3_journal_stop(handle, inode);
747 CERROR("hmm. why do we find this extent?\n");
748 CERROR("initial space: %lu:%u\n",
749 bp->start, bp->init_num);
750 CERROR("current extent: %u/%u/%u %d\n",
751 newex->e_block, newex->e_num,
752 newex->e_start, exist);
755 if (newex->e_block < bp->start)
756 i = bp->start - newex->e_block;
757 if (i >= newex->e_num)
758 CERROR("nothing to do?! i = %d, e_num = %u\n",
760 for (; i < newex->e_num && bp->num; i++) {
761 *(bp->created) = (exist == 0 ? 1 : 0);
763 *(bp->blocks) = newex->e_start + i;
771 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
772 unsigned long num, unsigned long *blocks,
773 int *created, int create)
775 struct ext3_extents_tree tree;
779 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
780 block, block + num, (unsigned) inode->i_ino);
782 ext3_init_tree_desc(&tree, inode);
785 bp.created = created;
787 bp.init_num = bp.num = num;
789 down_write(&EXT3_I(inode)->truncate_sem);
790 err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
791 ext3_ext_invalidate_cache(&tree);
792 up_write(&EXT3_I(inode)->truncate_sem);
797 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
798 int pages, unsigned long *blocks,
799 int *created, int create)
801 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
803 struct page *fp = NULL;
806 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
807 inode->i_ino, pages, (*page)->index);
809 /* pages are sorted already. so, we just have to find
810 * contig. space and process them properly */
813 /* start new extent */
818 } else if (fp->index + clen == (*page)->index) {
819 /* continue the extent */
826 /* process found extent */
827 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
828 clen * blocks_per_page, blocks,
833 /* look for next extent */
835 blocks += blocks_per_page * clen;
836 created += blocks_per_page * clen;
840 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
841 clen * blocks_per_page, blocks,
848 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
849 unsigned long *blocks, int *created, int create);
850 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
851 int pages, unsigned long *blocks,
852 int *created, int create)
854 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
858 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
859 rc = ext3_map_inode_page(inode, *page, b, cr, create);
861 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
862 inode->i_ino, *b, *cr, create, rc);
866 b += blocks_per_page;
867 cr += blocks_per_page;
872 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
873 int pages, unsigned long *blocks,
874 int *created, int create,
875 struct semaphore *optional_sem)
878 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
879 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
880 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
881 blocks, created, create);
885 if (optional_sem != NULL)
887 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
889 if (optional_sem != NULL)
895 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
896 int nblocks, loff_t newsize);
897 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
898 int nblocks, loff_t newsize)
900 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
903 static int fsfilt_ext3_read_record(struct file * file, void *buf,
904 int size, loff_t *offs)
906 struct inode *inode = file->f_dentry->d_inode;
908 struct buffer_head *bh;
909 int err, blocksize, csize, boffs;
911 /* prevent reading after eof */
913 if (inode->i_size < *offs + size) {
914 size = inode->i_size - *offs;
917 CERROR("size %llu is too short for read %u@%llu\n",
918 inode->i_size, size, *offs);
920 } else if (size == 0) {
927 blocksize = 1 << inode->i_blkbits;
930 block = *offs >> inode->i_blkbits;
931 boffs = *offs & (blocksize - 1);
932 csize = min(blocksize - boffs, size);
933 bh = ext3_bread(NULL, inode, block, 0, &err);
935 CERROR("can't read block: %d\n", err);
939 memcpy(buf, bh->b_data + boffs, csize);
949 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
950 loff_t *offs, int force_sync)
952 struct buffer_head *bh = NULL;
954 struct inode *inode = file->f_dentry->d_inode;
955 loff_t old_size = inode->i_size, offset = *offs;
956 loff_t new_size = inode->i_size;
959 int err, block_count = 0, blocksize, size, boffs;
961 /* Determine how many transaction credits are needed */
962 blocksize = 1 << inode->i_blkbits;
963 block_count = (*offs & (blocksize - 1)) + bufsize;
964 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
966 journal = EXT3_SB(inode->i_sb)->s_journal;
968 handle = journal_start(journal,
969 block_count * EXT3_DATA_TRANS_BLOCKS + 2);
971 if (IS_ERR(handle)) {
972 CERROR("can't start transaction\n");
973 return PTR_ERR(handle);
976 while (bufsize > 0) {
980 block = offset >> inode->i_blkbits;
981 boffs = offset & (blocksize - 1);
982 size = min(blocksize - boffs, bufsize);
983 bh = ext3_bread(handle, inode, block, 1, &err);
985 CERROR("can't read/create block: %d\n", err);
989 err = ext3_journal_get_write_access(handle, bh);
991 CERROR("journal_get_write_access() returned error %d\n",
995 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
996 memcpy(bh->b_data + boffs, buf, size);
997 err = ext3_journal_dirty_metadata(handle, bh);
999 CERROR("journal_dirty_metadata() returned error %d\n",
1003 if (offset + size > new_size)
1004 new_size = offset + size;
1011 handle->h_sync = 1; /* recovery likes this */
1016 /* correct in-core and on-disk sizes */
1017 if (new_size > inode->i_size) {
1019 if (new_size > inode->i_size)
1020 inode->i_size = new_size;
1021 if (inode->i_size > EXT3_I(inode)->i_disksize)
1022 EXT3_I(inode)->i_disksize = inode->i_size;
1023 if (inode->i_size > old_size)
1024 mark_inode_dirty(inode);
1029 journal_stop(handle);
1037 static int fsfilt_ext3_setup(struct super_block *sb)
1040 EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1041 EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1044 CWARN("Enabling PDIROPS\n");
1045 set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1046 sb->s_flags |= S_PDIROPS;
1051 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
1052 void *buffer, int buffer_size)
1058 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
1059 name, buffer, buffer_size, 0);
1062 CERROR("set xattr %s from inode %lu: rc %d\n",
1063 name, inode->i_ino, rc);
1067 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
1068 void *buffer, int buffer_size)
1073 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
1074 name, buffer, buffer_size);
1078 return (rc == -ENODATA) ? 0 : rc;
1080 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
1081 name, inode->i_ino, rc);
1082 memset(buffer, 0, buffer_size);
1083 return (rc == -ENODATA) ? 0 : rc;
1089 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1090 objects. Logs is number of logfiles to update */
1091 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1095 case FSFILT_OP_CREATE:
1096 /* directory leaf, index & indirect & EA*/
1097 return 4 + 3 * logs;
1098 case FSFILT_OP_UNLINK:
1104 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1105 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1106 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1107 for (i = 0; i < op; i++, fso++) {
1108 int nblocks = fso->fso_bufcnt * blockpp;
1109 int ndindirect = min(nblocks, addrpp + 1);
1110 int nindir = nblocks + ndindirect + 1;
1114 return needed + 3 * logs;
1120 static struct fsfilt_operations fsfilt_ext3_ops = {
1122 .fs_owner = THIS_MODULE,
1123 .fs_start = fsfilt_ext3_start,
1124 .fs_brw_start = fsfilt_ext3_brw_start,
1125 .fs_commit = fsfilt_ext3_commit,
1126 .fs_commit_async = fsfilt_ext3_commit_async,
1127 .fs_commit_wait = fsfilt_ext3_commit_wait,
1128 .fs_setattr = fsfilt_ext3_setattr,
1129 .fs_iocontrol = fsfilt_ext3_iocontrol,
1130 .fs_set_md = fsfilt_ext3_set_md,
1131 .fs_get_md = fsfilt_ext3_get_md,
1132 .fs_readpage = fsfilt_ext3_readpage,
1133 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
1134 .fs_statfs = fsfilt_ext3_statfs,
1135 .fs_sync = fsfilt_ext3_sync,
1136 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
1137 .fs_prep_san_write = fsfilt_ext3_prep_san_write,
1138 .fs_write_record = fsfilt_ext3_write_record,
1139 .fs_read_record = fsfilt_ext3_read_record,
1140 .fs_setup = fsfilt_ext3_setup,
1141 .fs_getpage = fsfilt_ext3_getpage,
1142 .fs_send_bio = fsfilt_ext3_send_bio,
1143 .fs_set_xattr = fsfilt_ext3_set_xattr,
1144 .fs_get_xattr = fsfilt_ext3_get_xattr,
1145 .fs_get_op_len = fsfilt_ext3_get_op_len,
1148 static int __init fsfilt_ext3_init(void)
1152 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1153 sizeof(struct fsfilt_cb_data), 0,
1156 CERROR("error allocating fsfilt journal callback cache\n");
1157 GOTO(out, rc = -ENOMEM);
1160 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1163 kmem_cache_destroy(fcb_cache);
1168 static void __exit fsfilt_ext3_exit(void)
1172 fsfilt_unregister_ops(&fsfilt_ext3_ops);
1173 rc = kmem_cache_destroy(fcb_cache);
1175 if (rc || atomic_read(&fcb_cache_count)) {
1176 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
1177 atomic_read(&fcb_cache_count), rc);
1181 module_init(fsfilt_ext3_init);
1182 module_exit(fsfilt_ext3_exit);
1184 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1185 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1186 MODULE_LICENSE("GPL");