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/ext3_extents.h>
38 #include <linux/version.h>
39 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
40 #include <linux/ext3_xattr.h>
42 #include <ext3/xattr.h>
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/module.h>
51 #include <linux/iobuf.h>
55 static kmem_cache_t *fcb_cache;
56 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
58 struct fsfilt_cb_data {
59 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
60 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
61 struct obd_device *cb_obd; /* MDS/OBD completion device */
62 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
63 void *cb_data; /* MDS/OST completion function data */
66 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
67 #define EXT3_XATTR_INDEX_TRUSTED 4
69 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
72 * We don't currently need any additional blocks for rmdir and
73 * unlink transactions because we are storing the OST oa_id inside
74 * the inode (which we will be changing anyways as part of this
77 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
80 /* For updates to the last recieved file */
81 int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
85 if (current->journal_info) {
86 CDEBUG(D_INODE, "increasing refcount on %p\n",
87 current->journal_info);
91 /* XXX BUG 3188 -- must return to one set of opcodes */
92 /* FIXME - cache hook */
94 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
100 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
105 case FSFILT_OP_RMDIR:
106 case FSFILT_OP_UNLINK:
107 /* delete one file + create/update logs for each stripe */
108 nblocks += EXT3_DELETE_TRANS_BLOCKS;
109 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
110 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
112 case FSFILT_OP_RENAME:
113 /* modify additional directory */
114 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
116 case FSFILT_OP_SYMLINK:
117 /* additional block + block bitmap + GDT for long symlink */
120 case FSFILT_OP_CREATE:
121 /* create/update logs for each stripe */
122 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
123 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
125 case FSFILT_OP_MKDIR:
126 case FSFILT_OP_MKNOD:
127 /* modify one inode + block bitmap + GDT */
131 /* modify parent directory */
132 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
133 EXT3_DATA_TRANS_BLOCKS;
135 case FSFILT_OP_SETATTR:
136 /* Setattr on inode */
139 case FSFILT_OP_CANCEL_UNLINK:
140 /* blocks for log header bitmap update OR
141 * blocks for catalog header bitmap update + unlink of logs */
142 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
143 EXT3_DELETE_TRANS_BLOCKS * logs;
146 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
148 default: CERROR("unknown transaction start op %d\n", op);
152 LASSERT(current->journal_info == desc_private);
153 journal = EXT3_SB(inode->i_sb)->s_journal;
154 if (nblocks > journal->j_max_transaction_buffers) {
155 CERROR("too many credits %d for op %ux%u using %d instead\n",
156 nblocks, op, logs, journal->j_max_transaction_buffers);
157 nblocks = journal->j_max_transaction_buffers;
162 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
166 LASSERT(current->journal_info == handle);
168 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
169 op, nblocks, PTR_ERR(handle));
174 * Calculate the number of buffer credits needed to write multiple pages in
175 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
176 * doesn't have a nice API for calculating this sort of thing in advance.
178 * See comment above ext3_writepage_trans_blocks for details. We assume
179 * no data journaling is being done, but it does allow for all of the pages
180 * being non-contiguous. If we are guaranteed contiguous pages we could
181 * reduce the number of (d)indirect blocks a lot.
183 * With N blocks per page and P pages, for each inode we have at most:
185 * min(N*P, blocksize/4 + 1) dindirect blocks
188 * For the entire filesystem, we have at most:
189 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
190 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
191 * objcount inode blocks
193 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
195 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
197 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
198 int niocount, struct niobuf_local *nb)
200 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
202 const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
203 int nbitmaps = 0, ngdblocks;
204 int needed = objcount + 1; /* inodes + superblock */
207 for (i = 0, j = 0; i < objcount; i++, fso++) {
208 /* two or more dindirect blocks in case we cross boundary */
209 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
211 sb->s_blocksize_bits) /
212 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
213 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
215 /* leaf, indirect, tindirect blocks for first block */
216 nbitmaps += blockpp + 2;
218 j += fso->fso_bufcnt;
221 next_indir = nb[0].offset +
222 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
223 for (i = 1; i < niocount; i++) {
224 if (nb[i].offset >= next_indir) {
225 nbitmaps++; /* additional indirect */
226 next_indir = nb[i].offset +
227 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
228 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
229 nbitmaps++; /* additional indirect */
231 nbitmaps += blockpp; /* each leaf in different group? */
234 ngdblocks = nbitmaps;
235 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
236 nbitmaps = EXT3_SB(sb)->s_groups_count;
237 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
238 ngdblocks = EXT3_SB(sb)->s_gdb_count;
240 needed += nbitmaps + ngdblocks;
242 /* last_rcvd update */
243 needed += EXT3_DATA_TRANS_BLOCKS;
245 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
246 /* We assume that there will be 1 bit set in s_dquot.flags for each
247 * quota file that is active. This is at least true for now.
249 needed += hweight32(sb_any_quota_enabled(sb)) *
250 EXT3_SINGLEDATA_TRANS_BLOCKS;
256 /* We have to start a huge journal transaction here to hold all of the
257 * metadata for the pages being written here. This is necessitated by
258 * the fact that we do lots of prepare_write operations before we do
259 * any of the matching commit_write operations, so even if we split
260 * up to use "smaller" transactions none of them could complete until
261 * all of them were opened. By having a single journal transaction,
262 * we eliminate duplicate reservations for common blocks like the
263 * superblock and group descriptors or bitmaps.
265 * We will start the transaction here, but each prepare_write will
266 * add a refcount to the transaction, and each commit_write will
267 * remove a refcount. The transaction will be closed when all of
268 * the pages have been written.
270 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
271 int niocount, struct niobuf_local *nb,
272 void *desc_private, int logs)
279 LASSERT(current->journal_info == desc_private);
280 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
281 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
283 /* The number of blocks we could _possibly_ dirty can very large.
284 * We reduce our request if it is absurd (and we couldn't get that
285 * many credits for a single handle anyways).
287 * At some point we have to limit the size of I/Os sent at one time,
288 * increase the size of the journal, or we have to calculate the
289 * actual journal requirements more carefully by checking all of
290 * the blocks instead of being maximally pessimistic. It remains to
291 * be seen if this is a real problem or not.
293 if (needed > journal->j_max_transaction_buffers) {
294 CERROR("want too many journal credits (%d) using %d instead\n",
295 needed, journal->j_max_transaction_buffers);
296 needed = journal->j_max_transaction_buffers;
300 handle = journal_start(journal, needed);
302 if (IS_ERR(handle)) {
303 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
306 LASSERT(handle->h_buffer_credits >= needed);
307 LASSERT(current->journal_info == handle);
313 static int fsfilt_ext3_commit(struct super_block *sb, struct inode *inode,
314 void *h, int force_sync)
317 handle_t *handle = h;
319 LASSERT(current->journal_info == handle);
321 handle->h_sync = 1; /* recovery likes this */
324 rc = journal_stop(handle);
330 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
334 transaction_t *transaction;
335 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
338 handle_t *handle = h;
342 LASSERT(current->journal_info == handle);
345 transaction = handle->h_transaction;
346 journal = transaction->t_journal;
347 tid = transaction->t_tid;
348 /* we don't want to be blocked */
350 rc = journal_stop(handle);
352 CERROR("error while stopping transaction: %d\n", rc);
356 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
357 rtid = log_start_commit(journal, transaction);
359 CERROR("strange race: %lu != %lu\n",
360 (unsigned long) tid, (unsigned long) rtid);
362 log_start_commit(journal, transaction->t_tid);
366 *wait_handle = (void *) tid;
367 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
371 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
373 tid_t tid = (tid_t)(long)h;
375 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
376 if (is_journal_aborted(EXT3_JOURNAL(inode)))
379 log_wait_commit(EXT3_JOURNAL(inode), tid);
384 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
385 struct iattr *iattr, int do_trunc)
387 struct inode *inode = dentry->d_inode;
392 /* A _really_ horrible hack to avoid removing the data stored
393 * in the block pointers; this is really the "small" stripe MD data.
394 * We can avoid further hackery by virtue of the MDS file size being
395 * zero all the time (which doesn't invoke block truncate at unlink
396 * time), so we assert we never change the MDS file size from zero. */
397 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
398 /* ATTR_SIZE would invoke truncate: clear it */
399 iattr->ia_valid &= ~ATTR_SIZE;
400 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
402 /* make sure _something_ gets set - so new inode
403 * goes to disk (probably won't work over XFS */
404 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
405 iattr->ia_valid |= ATTR_MODE;
406 iattr->ia_mode = inode->i_mode;
410 /* Don't allow setattr to change file type */
411 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
413 /* We set these flags on the client, but have already checked perms
414 * so don't confuse inode_change_ok. */
415 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
417 if (inode->i_op->setattr) {
418 rc = inode->i_op->setattr(dentry, iattr);
420 rc = inode_change_ok(inode, iattr);
422 rc = inode_setattr(inode, iattr);
430 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
431 unsigned int cmd, unsigned long arg)
436 if (inode->i_fop->ioctl)
437 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
444 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
445 void *lmm, int lmm_size)
449 /* keep this when we get rid of OLD_EA (too noisy during conversion) */
450 if (EXT3_I(inode)->i_file_acl /* || large inode EA flag */)
451 CWARN("setting EA on %lu/%u again... interesting\n",
452 inode->i_ino, inode->i_generation);
455 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
456 XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size, 0);
461 CERROR("error adding MD data to inode %lu: rc = %d\n",
466 /* Must be called with i_sem held */
467 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
471 LASSERT(down_trylock(&inode->i_sem) != 0);
474 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
475 XATTR_LUSTRE_MDS_LOV_EA, lmm, lmm_size);
478 /* This gives us the MD size */
480 return (rc == -ENODATA) ? 0 : rc;
483 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
484 EXT3_XATTR_INDEX_TRUSTED, XATTR_LUSTRE_MDS_LOV_EA,
486 memset(lmm, 0, lmm_size);
487 return (rc == -ENODATA) ? 0 : rc;
493 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
494 static int fsfilt_ext3_send_bio(struct inode *inode, struct bio *bio)
496 submit_bio(WRITE, bio);
500 static int fsfilt_ext3_send_bio(struct inode *inode, struct kiobuf *bio)
502 int rc, blocks_per_page;
504 rc = brw_kiovec(WRITE, 1, &bio, inode->i_dev,
505 bio->blocks, 1 << inode->i_blkbits);
507 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
509 if (rc != (1 << inode->i_blkbits) * bio->nr_pages *
511 CERROR("short write? expected %d, wrote %d\n",
512 (1 << inode->i_blkbits) * bio->nr_pages *
513 blocks_per_page, rc);
520 /* FIXME-UMKA: This should be used in 2.6.x io code later. */
521 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
526 page = grab_cache_page(inode->i_mapping, index);
528 return ERR_PTR(-ENOMEM);
530 if (PageUptodate(page)) {
535 rc = inode->i_mapping->a_ops->readpage(NULL, page);
537 page_cache_release(page);
544 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
547 struct inode *inode = file->f_dentry->d_inode;
550 if (S_ISREG(inode->i_mode))
551 rc = file->f_op->read(file, buf, count, off);
553 const int blkbits = inode->i_sb->s_blocksize_bits;
554 const int blksize = inode->i_sb->s_blocksize;
556 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
557 count, inode->i_ino, *off);
559 struct buffer_head *bh;
562 if (*off < inode->i_size) {
565 bh = ext3_bread(NULL, inode, *off >> blkbits,
568 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
571 memcpy(buf, bh->b_data, blksize);
574 /* XXX in theory we should just fake
575 * this buffer and continue like ext3,
576 * especially if this is a partial read
578 CERROR("error read dir %lu+%llu: %d\n",
579 inode->i_ino, *off, err);
584 struct ext3_dir_entry_2 *fake = (void *)buf;
586 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
587 memset(fake, 0, sizeof(*fake));
588 fake->rec_len = cpu_to_le32(blksize);
600 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
602 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
604 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
606 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
607 atomic_dec(&fcb_cache_count);
610 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
611 struct super_block *sb,
613 void *handle, fsfilt_cb_t cb_func,
616 struct fsfilt_cb_data *fcb;
618 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
622 atomic_inc(&fcb_cache_count);
623 fcb->cb_func = cb_func;
625 fcb->cb_last_rcvd = last_rcvd;
626 fcb->cb_data = cb_data;
628 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
630 journal_callback_set(handle, fsfilt_ext3_cb_func,
631 (struct journal_callback *)fcb);
638 * We need to hack the return value for the free inode counts because
639 * the current EA code requires one filesystem block per inode with EAs,
640 * so it is possible to run out of blocks before we run out of inodes.
642 * This can be removed when the ext3 EA code is fixed.
644 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
649 memset(&sfs, 0, sizeof(sfs));
651 rc = sb->s_op->statfs(sb, &sfs);
653 if (!rc && sfs.f_bfree < sfs.f_ffree) {
654 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
655 sfs.f_ffree = sfs.f_bfree;
658 statfs_pack(osfs, &sfs);
662 static int fsfilt_ext3_sync(struct super_block *sb)
664 return ext3_force_commit(sb);
667 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
669 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 if (bp->create == 0) {
701 if (newex->e_block < bp->start)
702 i = bp->start - newex->e_block;
703 if (i >= newex->e_num)
704 CERROR("nothing to do?! i = %d, e_num = %u\n",
706 for (; i < newex->e_num && bp->num; i++) {
718 tgen = EXT_GENERATION(tree);
719 count = ext3_ext_calc_credits_for_insert(tree, path);
720 up_write(&EXT3_I(inode)->truncate_sem);
722 handle = ext3_journal_start(inode, count + EXT3_ALLOC_NEEDED + 1);
723 if (IS_ERR(handle)) {
724 down_write(&EXT3_I(inode)->truncate_sem);
725 return PTR_ERR(handle);
728 if (tgen != EXT_GENERATION(tree)) {
729 /* the tree has changed. so path can be invalid at moment */
730 ext3_journal_stop(handle, inode);
731 down_write(&EXT3_I(inode)->truncate_sem);
735 down_write(&EXT3_I(inode)->truncate_sem);
736 goal = ext3_ext_find_goal(inode, path, newex->e_block);
737 count = newex->e_num;
738 pblock = ext3_new_blocks(handle, inode, &count, goal, &err);
741 EXT_ASSERT(count <= newex->e_num);
743 /* insert new extent */
744 newex->e_start = pblock;
745 newex->e_num = count;
746 err = ext3_ext_insert_extent(handle, tree, path, newex);
750 /* correct on-disk inode size */
751 if (newex->e_num > 0) {
752 new_i_size = (loff_t) newex->e_block + newex->e_num;
753 new_i_size = new_i_size << inode->i_blkbits;
754 if (new_i_size > EXT3_I(inode)->i_disksize) {
755 EXT3_I(inode)->i_disksize = new_i_size;
756 err = ext3_mark_inode_dirty(handle, inode);
761 ext3_journal_stop(handle, inode);
766 CERROR("hmm. why do we find this extent?\n");
767 CERROR("initial space: %lu:%u\n",
768 bp->start, bp->init_num);
769 CERROR("current extent: %u/%u/%u %d\n",
770 newex->e_block, newex->e_num,
771 newex->e_start, exist);
774 if (newex->e_block < bp->start)
775 i = bp->start - newex->e_block;
776 if (i >= newex->e_num)
777 CERROR("nothing to do?! i = %d, e_num = %u\n",
779 for (; i < newex->e_num && bp->num; i++) {
780 *(bp->created) = (exist == 0 ? 1 : 0);
782 *(bp->blocks) = newex->e_start + i;
790 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
791 unsigned long num, unsigned long *blocks,
792 int *created, int create)
794 struct ext3_extents_tree tree;
798 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
799 block, block + num, (unsigned) inode->i_ino);
801 ext3_init_tree_desc(&tree, inode);
804 bp.created = created;
806 bp.init_num = bp.num = num;
809 down_write(&EXT3_I(inode)->truncate_sem);
810 err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
811 ext3_ext_invalidate_cache(&tree);
812 up_write(&EXT3_I(inode)->truncate_sem);
817 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
818 int pages, unsigned long *blocks,
819 int *created, int create)
821 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
823 struct page *fp = NULL;
826 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
827 inode->i_ino, pages, (*page)->index);
829 /* pages are sorted already. so, we just have to find
830 * contig. space and process them properly */
833 /* start new extent */
838 } else if (fp->index + clen == (*page)->index) {
839 /* continue the extent */
846 /* process found extent */
847 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
848 clen * blocks_per_page, blocks,
853 /* look for next extent */
855 blocks += blocks_per_page * clen;
856 created += blocks_per_page * clen;
860 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
861 clen * blocks_per_page, blocks,
868 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
869 unsigned long *blocks, int *created, int create);
870 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
871 int pages, unsigned long *blocks,
872 int *created, int create)
874 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
878 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
879 rc = ext3_map_inode_page(inode, *page, b, cr, create);
881 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
882 inode->i_ino, *b, *cr, create, rc);
886 b += blocks_per_page;
887 cr += blocks_per_page;
892 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
893 int pages, unsigned long *blocks,
894 int *created, int create,
895 struct semaphore *optional_sem)
898 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
899 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
900 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
901 blocks, created, create);
905 if (optional_sem != NULL)
907 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
909 if (optional_sem != NULL)
915 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
916 int nblocks, loff_t newsize);
917 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
918 int nblocks, loff_t newsize)
920 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
923 static int fsfilt_ext3_read_record(struct file * file, void *buf,
924 int size, loff_t *offs)
926 struct inode *inode = file->f_dentry->d_inode;
928 struct buffer_head *bh;
929 int err, blocksize, csize, boffs;
931 /* prevent reading after eof */
933 if (inode->i_size < *offs + size) {
934 size = inode->i_size - *offs;
937 CERROR("size %llu is too short for read %u@%llu\n",
938 inode->i_size, size, *offs);
940 } else if (size == 0) {
947 blocksize = 1 << inode->i_blkbits;
950 block = *offs >> inode->i_blkbits;
951 boffs = *offs & (blocksize - 1);
952 csize = min(blocksize - boffs, size);
953 bh = ext3_bread(NULL, inode, block, 0, &err);
955 CERROR("can't read block: %d\n", err);
959 memcpy(buf, bh->b_data + boffs, csize);
969 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
970 loff_t *offs, int force_sync)
972 struct buffer_head *bh = NULL;
974 struct inode *inode = file->f_dentry->d_inode;
975 loff_t old_size = inode->i_size, offset = *offs;
976 loff_t new_size = inode->i_size;
979 int err, block_count = 0, blocksize, size, boffs;
981 /* Determine how many transaction credits are needed */
982 blocksize = 1 << inode->i_blkbits;
983 block_count = (*offs & (blocksize - 1)) + bufsize;
984 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
986 journal = EXT3_SB(inode->i_sb)->s_journal;
988 handle = journal_start(journal,
989 block_count * EXT3_DATA_TRANS_BLOCKS + 2);
991 if (IS_ERR(handle)) {
992 CERROR("can't start transaction\n");
993 return PTR_ERR(handle);
996 while (bufsize > 0) {
1000 block = offset >> inode->i_blkbits;
1001 boffs = offset & (blocksize - 1);
1002 size = min(blocksize - boffs, bufsize);
1003 bh = ext3_bread(handle, inode, block, 1, &err);
1005 CERROR("can't read/create block: %d\n", err);
1009 err = ext3_journal_get_write_access(handle, bh);
1011 CERROR("journal_get_write_access() returned error %d\n",
1015 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1016 memcpy(bh->b_data + boffs, buf, size);
1017 err = ext3_journal_dirty_metadata(handle, bh);
1019 CERROR("journal_dirty_metadata() returned error %d\n",
1023 if (offset + size > new_size)
1024 new_size = offset + size;
1031 handle->h_sync = 1; /* recovery likes this */
1036 /* correct in-core and on-disk sizes */
1037 if (new_size > inode->i_size) {
1039 if (new_size > inode->i_size)
1040 inode->i_size = new_size;
1041 if (inode->i_size > EXT3_I(inode)->i_disksize)
1042 EXT3_I(inode)->i_disksize = inode->i_size;
1043 if (inode->i_size > old_size)
1044 mark_inode_dirty(inode);
1049 journal_stop(handle);
1057 static int fsfilt_ext3_setup(struct obd_device *obd, struct super_block *sb)
1059 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1060 struct mds_obd *mds = &obd->u.mds;
1063 EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1064 EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1067 CWARN("Enabling PDIROPS\n");
1068 set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1069 sb->s_flags |= S_PDIROPS;
1071 /* setup mdsnum in underlying fs */
1072 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1073 if (mds->mds_lmv_obd) {
1074 struct ext3_sb_info *sbi = EXT3_SB(sb);
1075 struct ext3_super_block *es = sbi->s_es;
1079 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MDSNUM)) {
1080 CWARN("%s: set mdsnum %d in ext3fs\n",
1081 obd->obd_name, mds->mds_num);
1083 handle = journal_start(sbi->s_journal, 1);
1085 LASSERT(!IS_ERR(handle));
1086 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1088 EXT3_SET_INCOMPAT_FEATURE(sb,
1089 EXT3_FEATURE_INCOMPAT_MDSNUM);
1090 es->s_mdsnum = mds->mds_num;
1091 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1094 journal_stop(handle);
1097 CWARN("%s: mdsnum initialized to %u in ext3fs\n",
1098 obd->obd_name, es->s_mdsnum);
1100 sbi->s_mdsnum = es->s_mdsnum;
1106 extern int ext3_add_dir_entry(struct dentry *dentry);
1107 extern int ext3_del_dir_entry(struct dentry *dentry);
1109 static int fsfilt_ext3_add_dir_entry(struct obd_device *obd,
1110 struct dentry *parent,
1111 char *name, int namelen,
1113 unsigned long generation,
1116 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1117 struct dentry *dentry;
1119 dentry = ll_lookup_one_len(name, parent, namelen);
1120 if (IS_ERR(dentry)) {
1121 CERROR("can't lookup %*s in %lu/%lu: %d\n", dentry->d_name.len,
1122 dentry->d_name.name, dentry->d_inode->i_ino,
1123 (unsigned long) dentry->d_inode->i_generation,
1124 (int) PTR_ERR(dentry));
1125 RETURN(PTR_ERR(dentry));
1127 if (dentry->d_inode != NULL) {
1128 CERROR("dentry %*s(0x%p) found\n", dentry->d_name.len,
1129 dentry->d_name.name, dentry);
1134 /* mds_reint_rename() may use this method to add dir entry
1135 * that points onto local inode. and we don't want to find
1136 * it cross-ref by subsequent lookups */
1139 dentry->d_flags |= DCACHE_CROSS_REF;
1140 dentry->d_inum = ino;
1141 dentry->d_mdsnum = mds;
1142 dentry->d_generation = generation;
1144 err = ext3_add_dir_entry(dentry);
1151 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1156 static int fsfilt_ext3_del_dir_entry(struct obd_device *obd,
1157 struct dentry *dentry)
1159 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1162 err = ext3_del_dir_entry(dentry);
1168 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1173 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
1174 void *buffer, int buffer_size)
1180 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
1181 name, buffer, buffer_size, 0);
1184 CERROR("set xattr %s from inode %lu: rc %d\n",
1185 name, inode->i_ino, rc);
1189 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
1190 void *buffer, int buffer_size)
1196 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
1197 name, buffer, buffer_size);
1201 return (rc == -ENODATA) ? 0 : rc;
1203 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
1204 name, inode->i_ino, rc);
1205 memset(buffer, 0, buffer_size);
1206 return (rc == -ENODATA) ? 0 : rc;
1212 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1213 objects. Logs is number of logfiles to update */
1214 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1218 case FSFILT_OP_CREATE:
1219 /* directory leaf, index & indirect & EA*/
1220 return 4 + 3 * logs;
1221 case FSFILT_OP_UNLINK:
1227 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1228 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1229 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1230 for (i = 0; i < op; i++, fso++) {
1231 int nblocks = fso->fso_bufcnt * blockpp;
1232 int ndindirect = min(nblocks, addrpp + 1);
1233 int nindir = nblocks + ndindirect + 1;
1237 return needed + 3 * logs;
1244 #define EXTENTS_EA "write_extents"
1245 #define EXTENTS_EA_SIZE 64
1247 static int fsfilt_ext3_insert_extents_ea(struct inode *inode,
1253 rc = ext3_ext_in_ea_alloc_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1254 EXTENTS_EA, from, num);
1258 static int fsfilt_ext3_remove_extents_ea(struct inode *inode,
1264 rc = ext3_ext_in_ea_remove_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1265 EXTENTS_EA, from, num);
1269 extern int ext3_init_tree_in_ea(struct inode *inode, int name_index,
1270 const char *eaname, int size);
1272 static int fsfilt_ext3_init_extents_ea(struct inode *inode)
1276 rc = ext3_init_tree_in_ea(inode, EXT3_XATTR_INDEX_TRUSTED,
1281 static int fsfilt_ext3_get_inode_write_extents(struct inode *inode,
1282 char **pbuf, int *size)
1286 rc = ext3_ext_in_ea_get_extents(inode, EXT3_XATTR_INDEX_TRUSTED,
1287 EXTENTS_EA, pbuf, size);
1291 static int fsfilt_ext3_get_write_extents_num(struct inode *inode, int *size)
1295 rc = ext3_ext_in_ea_get_extents_num(inode, EXT3_XATTR_INDEX_TRUSTED,
1300 static struct fsfilt_operations fsfilt_ext3_ops = {
1302 .fs_owner = THIS_MODULE,
1303 .fs_start = fsfilt_ext3_start,
1304 .fs_brw_start = fsfilt_ext3_brw_start,
1305 .fs_commit = fsfilt_ext3_commit,
1306 .fs_commit_async = fsfilt_ext3_commit_async,
1307 .fs_commit_wait = fsfilt_ext3_commit_wait,
1308 .fs_setattr = fsfilt_ext3_setattr,
1309 .fs_iocontrol = fsfilt_ext3_iocontrol,
1310 .fs_set_md = fsfilt_ext3_set_md,
1311 .fs_get_md = fsfilt_ext3_get_md,
1312 .fs_readpage = fsfilt_ext3_readpage,
1313 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
1314 .fs_statfs = fsfilt_ext3_statfs,
1315 .fs_sync = fsfilt_ext3_sync,
1316 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
1317 .fs_prep_san_write = fsfilt_ext3_prep_san_write,
1318 .fs_write_record = fsfilt_ext3_write_record,
1319 .fs_read_record = fsfilt_ext3_read_record,
1320 .fs_setup = fsfilt_ext3_setup,
1321 .fs_getpage = fsfilt_ext3_getpage,
1322 .fs_send_bio = fsfilt_ext3_send_bio,
1323 .fs_set_xattr = fsfilt_ext3_set_xattr,
1324 .fs_get_xattr = fsfilt_ext3_get_xattr,
1325 .fs_get_op_len = fsfilt_ext3_get_op_len,
1326 .fs_add_dir_entry = fsfilt_ext3_add_dir_entry,
1327 .fs_del_dir_entry = fsfilt_ext3_del_dir_entry,
1328 .fs_init_extents_ea = fsfilt_ext3_init_extents_ea,
1329 .fs_insert_extents_ea = fsfilt_ext3_insert_extents_ea,
1330 .fs_remove_extents_ea = fsfilt_ext3_remove_extents_ea,
1331 .fs_get_inode_write_extents = fsfilt_ext3_get_inode_write_extents,
1332 .fs_get_write_extents_num = fsfilt_ext3_get_write_extents_num,
1335 static int __init fsfilt_ext3_init(void)
1339 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1340 sizeof(struct fsfilt_cb_data), 0,
1343 CERROR("error allocating fsfilt journal callback cache\n");
1344 GOTO(out, rc = -ENOMEM);
1347 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1350 kmem_cache_destroy(fcb_cache);
1355 static void __exit fsfilt_ext3_exit(void)
1359 fsfilt_unregister_ops(&fsfilt_ext3_ops);
1360 rc = kmem_cache_destroy(fcb_cache);
1362 if (rc || atomic_read(&fcb_cache_count)) {
1363 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
1364 atomic_read(&fcb_cache_count), rc);
1368 module_init(fsfilt_ext3_init);
1369 module_exit(fsfilt_ext3_exit);
1371 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1372 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1373 MODULE_LICENSE("GPL");