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 <libcfs/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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7))
56 # define lock_24kernel() lock_kernel()
57 # define unlock_24kernel() unlock_kernel()
59 # define lock_24kernel() do {} while (0)
60 # define unlock_24kernel() do {} while (0)
63 static kmem_cache_t *fcb_cache;
64 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
66 struct fsfilt_cb_data {
67 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
68 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
69 struct obd_device *cb_obd; /* MDS/OBD completion device */
70 __u64 cb_last_num; /* MDS/OST last committed operation */
71 void *cb_data; /* MDS/OST completion function data */
74 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
75 #define EXT3_XATTR_INDEX_TRUSTED 4
78 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
79 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
80 #define XATTR_LUSTRE_MDS_MID_EA "mid"
81 #define XATTR_LUSTRE_MDS_SID_EA "sid"
82 #define XATTR_LUSTRE_MDS_PID_EA "pid"
83 #define XATTR_LUSTRE_MDS_KEY_EA "key"
86 * We don't currently need any additional blocks for rmdir and
87 * unlink transactions because we are storing the OST oa_id inside
88 * the inode (which we will be changing anyways as part of this
91 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
94 /* For updates to the last recieved file */
95 int nblocks = EXT3_SINGLEDATA_TRANS_BLOCKS;
99 if (current->journal_info) {
100 CDEBUG(D_INODE, "increasing refcount on %p\n",
101 current->journal_info);
106 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
107 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;
110 case FSFILT_OP_RMDIR:
111 case FSFILT_OP_UNLINK:
112 /* delete one file + create/update logs for each stripe */
113 nblocks += EXT3_DELETE_TRANS_BLOCKS;
114 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
115 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
117 case FSFILT_OP_RENAME:
118 /* modify additional directory */
119 nblocks += EXT3_SINGLEDATA_TRANS_BLOCKS;
121 case FSFILT_OP_SYMLINK:
122 /* additional block + block bitmap + GDT for long symlink */
125 case FSFILT_OP_CREATE:
126 /* create/update logs for each stripe */
127 /*nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
128 EXT3_SINGLEDATA_TRANS_BLOCKS) * logs;*/
130 case FSFILT_OP_MKDIR:
131 case FSFILT_OP_MKNOD:
132 /* modify one inode + block bitmap + GDT */
136 /* modify parent directory */
137 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
138 EXT3_DATA_TRANS_BLOCKS;
140 case FSFILT_OP_SETATTR:
141 /* Setattr on inode */
144 case FSFILT_OP_CANCEL_UNLINK:
145 /* blocks for log header bitmap update OR
146 * blocks for catalog header bitmap update + unlink of logs */
147 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
148 EXT3_DELETE_TRANS_BLOCKS * logs;
151 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
153 default: CERROR("unknown transaction start op %d\n", op);
157 LASSERT(current->journal_info == desc_private);
158 journal = EXT3_SB(inode->i_sb)->s_journal;
159 if (nblocks > journal->j_max_transaction_buffers) {
160 CERROR("too many credits %d for op %ux%u using %d instead\n",
161 nblocks, op, logs, journal->j_max_transaction_buffers);
162 nblocks = journal->j_max_transaction_buffers;
166 LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
168 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
172 LASSERT(current->journal_info == handle);
174 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
175 op, nblocks, PTR_ERR(handle));
180 * Calculate the number of buffer credits needed to write multiple pages in
181 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
182 * doesn't have a nice API for calculating this sort of thing in advance.
184 * See comment above ext3_writepage_trans_blocks for details. We assume
185 * no data journaling is being done, but it does allow for all of the pages
186 * being non-contiguous. If we are guaranteed contiguous pages we could
187 * reduce the number of (d)indirect blocks a lot.
189 * With N blocks per page and P pages, for each inode we have at most:
191 * min(N*P, blocksize/4 + 1) dindirect blocks
194 * For the entire filesystem, we have at most:
195 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
196 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
197 * objcount inode blocks
199 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
201 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
203 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
204 int niocount, struct niobuf_local *nb)
206 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
208 const int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
209 int nbitmaps = 0, ngdblocks;
210 int needed = objcount + 1; /* inodes + superblock */
213 for (i = 0, j = 0; i < objcount; i++, fso++) {
214 /* two or more dindirect blocks in case we cross boundary */
215 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
217 sb->s_blocksize_bits) /
218 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
219 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
221 /* leaf, indirect, tindirect blocks for first block */
222 nbitmaps += blockpp + 2;
224 j += fso->fso_bufcnt;
227 next_indir = nb[0].offset +
228 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
229 for (i = 1; i < niocount; i++) {
230 if (nb[i].offset >= next_indir) {
231 nbitmaps++; /* additional indirect */
232 next_indir = nb[i].offset +
233 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
234 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
235 nbitmaps++; /* additional indirect */
237 nbitmaps += blockpp; /* each leaf in different group? */
240 ngdblocks = nbitmaps;
241 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
242 nbitmaps = EXT3_SB(sb)->s_groups_count;
243 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
244 ngdblocks = EXT3_SB(sb)->s_gdb_count;
246 needed += nbitmaps + ngdblocks;
248 /* last_rcvd update */
249 needed += EXT3_DATA_TRANS_BLOCKS;
251 #if defined(CONFIG_QUOTA) && !defined(__x86_64__) /* XXX */
252 /* We assume that there will be 1 bit set in s_dquot.flags for each
253 * quota file that is active. This is at least true for now.
255 needed += hweight32(sb_any_quota_enabled(sb)) *
256 EXT3_SINGLEDATA_TRANS_BLOCKS;
262 /* We have to start a huge journal transaction here to hold all of the
263 * metadata for the pages being written here. This is necessitated by
264 * the fact that we do lots of prepare_write operations before we do
265 * any of the matching commit_write operations, so even if we split
266 * up to use "smaller" transactions none of them could complete until
267 * all of them were opened. By having a single journal transaction,
268 * we eliminate duplicate reservations for common blocks like the
269 * superblock and group descriptors or bitmaps.
271 * We will start the transaction here, but each prepare_write will
272 * add a refcount to the transaction, and each commit_write will
273 * remove a refcount. The transaction will be closed when all of
274 * the pages have been written.
276 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
277 int niocount, struct niobuf_local *nb,
278 void *desc_private, int logs)
285 LASSERT(current->journal_info == desc_private);
286 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
287 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
289 /* The number of blocks we could _possibly_ dirty can very large.
290 * We reduce our request if it is absurd (and we couldn't get that
291 * many credits for a single handle anyways).
293 * At some point we have to limit the size of I/Os sent at one time,
294 * increase the size of the journal, or we have to calculate the
295 * actual journal requirements more carefully by checking all of
296 * the blocks instead of being maximally pessimistic. It remains to
297 * be seen if this is a real problem or not.
299 if (needed > journal->j_max_transaction_buffers) {
300 CERROR("want too many journal credits (%d) using %d instead\n",
301 needed, journal->j_max_transaction_buffers);
302 needed = journal->j_max_transaction_buffers;
305 LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
307 handle = journal_start(journal, needed);
309 if (IS_ERR(handle)) {
310 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
313 LASSERT(handle->h_buffer_credits >= needed);
314 LASSERT(current->journal_info == handle);
320 static int fsfilt_ext3_commit(struct super_block *sb, struct inode *inode,
321 void *h, int force_sync)
324 handle_t *handle = h;
326 LASSERT(current->journal_info == handle);
328 handle->h_sync = 1; /* recovery likes this */
331 rc = journal_stop(handle);
337 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
341 transaction_t *transaction;
342 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
345 handle_t *handle = h;
349 LASSERT(current->journal_info == handle);
352 transaction = handle->h_transaction;
353 journal = transaction->t_journal;
354 tid = transaction->t_tid;
355 /* we don't want to be blocked */
357 rc = journal_stop(handle);
359 CERROR("error while stopping transaction: %d\n", rc);
363 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
364 rtid = log_start_commit(journal, transaction);
366 CERROR("strange race: %lu != %lu\n",
367 (unsigned long) tid, (unsigned long) rtid);
369 log_start_commit(journal, tid);
373 *wait_handle = (void *) tid;
374 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
378 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
380 tid_t tid = (tid_t)(long)h;
382 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
383 if (is_journal_aborted(EXT3_JOURNAL(inode)))
386 log_wait_commit(EXT3_JOURNAL(inode), tid);
391 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
392 struct iattr *iattr, int do_trunc)
394 struct inode *inode = dentry->d_inode;
399 /* A _really_ horrible hack to avoid removing the data stored
400 * in the block pointers; this is really the "small" stripe MD data.
401 * We can avoid further hackery by virtue of the MDS file size being
402 * zero all the time (which doesn't invoke block truncate at unlink
403 * time), so we assert we never change the MDS file size from zero. */
404 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
405 /* ATTR_SIZE would invoke truncate: clear it */
406 iattr->ia_valid &= ~ATTR_SIZE;
407 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
409 /* make sure _something_ gets set - so new inode
410 * goes to disk (probably won't work over XFS */
411 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
412 iattr->ia_valid |= ATTR_MODE;
413 iattr->ia_mode = inode->i_mode;
417 /* Don't allow setattr to change file type */
418 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
420 /* We set these flags on the client, but have already checked perms
421 * so don't confuse inode_change_ok. */
422 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
424 if (inode->i_op->setattr) {
425 rc = inode->i_op->setattr(dentry, iattr);
427 rc = inode_change_ok(inode, iattr);
429 rc = inode_setattr(inode, iattr);
437 static int fsfilt_ext3_iocontrol(struct inode * inode, struct file *file,
438 unsigned int cmd, unsigned long arg)
443 if (inode->i_fop->ioctl)
444 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
451 static int fsfilt_ext3_set_xattr(struct inode * inode, void *handle, char *name,
452 void *buffer, int buffer_size)
458 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
459 name, buffer, buffer_size, 0);
462 CERROR("set xattr %s from inode %lu: rc %d\n",
463 name, inode->i_ino, rc);
467 static int fsfilt_ext3_get_xattr(struct inode *inode, char *name,
468 void *buffer, int buffer_size)
474 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
475 name, buffer, buffer_size);
479 return (rc == -ENODATA) ? 0 : rc;
481 CDEBUG(D_INFO, "error getting EA %s from inode %lu: rc %d\n",
482 name, inode->i_ino, rc);
483 memset(buffer, 0, buffer_size);
484 return (rc == -ENODATA) ? 0 : rc;
490 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
491 void *lmm, int lmm_size,
498 rc = fsfilt_ext3_set_xattr(inode, handle,
499 XATTR_LUSTRE_MDS_LOV_EA,
503 rc = fsfilt_ext3_set_xattr(inode, handle,
504 XATTR_LUSTRE_MDS_MEA_EA,
508 rc = fsfilt_ext3_set_xattr(inode, handle,
509 XATTR_LUSTRE_MDS_SID_EA,
513 rc = fsfilt_ext3_set_xattr(inode, handle,
514 XATTR_LUSTRE_MDS_MID_EA,
518 rc = fsfilt_ext3_set_xattr(inode, handle,
519 XATTR_LUSTRE_MDS_PID_EA,
523 rc = fsfilt_ext3_set_xattr(inode, handle,
524 XATTR_LUSTRE_MDS_KEY_EA,
534 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm,
535 int lmm_size, enum ea_type type)
541 rc = fsfilt_ext3_get_xattr(inode,
542 XATTR_LUSTRE_MDS_LOV_EA,
546 rc = fsfilt_ext3_get_xattr(inode,
547 XATTR_LUSTRE_MDS_MEA_EA,
551 rc = fsfilt_ext3_get_xattr(inode,
552 XATTR_LUSTRE_MDS_SID_EA,
556 rc = fsfilt_ext3_get_xattr(inode,
557 XATTR_LUSTRE_MDS_MID_EA,
561 rc = fsfilt_ext3_get_xattr(inode,
562 XATTR_LUSTRE_MDS_PID_EA,
566 rc = fsfilt_ext3_get_xattr(inode,
567 XATTR_LUSTRE_MDS_KEY_EA,
577 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, void *bio)
580 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
581 submit_bio(rw, (struct bio *)bio);
583 struct bio *b = (struct kiobuf *)bio;
586 rc = brw_kiovec(rw, 1, &b, inode->i_dev,
587 b->blocks, 1 << inode->i_blkbits);
589 blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
591 if (rc != (1 << inode->i_blkbits) * b->nr_pages * blocks_per_page) {
592 CERROR("short write? expected %d, wrote %d\n",
593 (1 << inode->i_blkbits) * b->nr_pages *
594 blocks_per_page, rc);
600 static struct page *fsfilt_ext3_getpage(struct inode *inode, long int index)
605 page = grab_cache_page(inode->i_mapping, index);
607 return ERR_PTR(-ENOMEM);
609 if (PageUptodate(page)) {
614 rc = inode->i_mapping->a_ops->readpage(NULL, page);
616 page_cache_release(page);
623 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
626 struct inode *inode = file->f_dentry->d_inode;
629 if (S_ISREG(inode->i_mode))
630 rc = file->f_op->read(file, buf, count, off);
632 const int blkbits = inode->i_sb->s_blocksize_bits;
633 const int blksize = inode->i_sb->s_blocksize;
635 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
636 count, inode->i_ino, *off);
638 struct buffer_head *bh;
641 if (*off < inode->i_size) {
644 bh = ext3_bread(NULL, inode, *off >> blkbits,
647 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
650 memcpy(buf, bh->b_data, blksize);
653 /* XXX in theory we should just fake
654 * this buffer and continue like ext3,
655 * especially if this is a partial read
657 CERROR("error read dir %lu+%llu: %d\n",
658 inode->i_ino, *off, err);
663 struct ext3_dir_entry_2 *fake = (void *)buf;
665 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
666 memset(fake, 0, sizeof(*fake));
667 fake->rec_len = cpu_to_le32(blksize);
679 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
681 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
683 fcb->cb_func(fcb->cb_obd, fcb->cb_last_num, fcb->cb_data, error);
685 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
686 atomic_dec(&fcb_cache_count);
689 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd,
690 struct super_block *sb,
691 __u64 last_num, void *handle,
695 struct fsfilt_cb_data *fcb;
697 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
701 atomic_inc(&fcb_cache_count);
702 fcb->cb_func = cb_func;
704 fcb->cb_last_num = last_num;
705 fcb->cb_data = cb_data;
707 CDEBUG(D_EXT2, "set callback for last_num: "LPD64"\n", last_num);
709 journal_callback_set(handle, fsfilt_ext3_cb_func,
710 (struct journal_callback *)fcb);
716 * We need to hack the return value for the free inode counts because
717 * the current EA code requires one filesystem block per inode with EAs,
718 * so it is possible to run out of blocks before we run out of inodes.
720 * This can be removed when the ext3 EA code is fixed.
722 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
727 memset(&sfs, 0, sizeof(sfs));
729 rc = sb->s_op->statfs(sb, &sfs);
731 if (!rc && sfs.f_bfree < sfs.f_ffree) {
732 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
733 sfs.f_ffree = sfs.f_bfree;
736 statfs_pack(osfs, &sfs);
740 static int fsfilt_ext3_sync(struct super_block *sb)
742 return ext3_force_commit(sb);
745 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
746 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
747 #define ext3_up_truncate_sem(inode) up_write(&EXT3_I(inode)->truncate_sem);
748 #define ext3_down_truncate_sem(inode) down_write(&EXT3_I(inode)->truncate_sem);
750 #define ext3_up_truncate_sem(inode) up(&EXT3_I(inode)->truncate_sem);
751 #define ext3_down_truncate_sem(inode) down(&EXT3_I(inode)->truncate_sem);
754 #include <linux/lustre_version.h>
755 #if EXT3_EXT_MAGIC == 0xf301
756 #define ee_start e_start
757 #define ee_block e_block
760 #ifndef EXT3_BB_MAX_BLOCKS
761 #define ext3_mb_new_blocks(handle, inode, goal, count, aflags, err) \
762 ext3_new_blocks(handle, inode, count, goal, err)
766 unsigned long *blocks;
773 static int ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
774 unsigned long block, int *aflags)
776 struct ext3_inode_info *ei = EXT3_I(inode);
777 unsigned long bg_start;
778 unsigned long colour;
782 struct ext3_extent *ex;
783 depth = path->p_depth;
785 /* try to predict block placement */
786 if ((ex = path[depth].p_ext)) {
788 /* This prefers to eat into a contiguous extent
789 * rather than find an extent that the whole
790 * request will fit into. This can fragment data
791 * block allocation and prevents our lovely 1M I/Os
792 * from reaching the disk intact. */
794 if (ex->ee_block + ex->ee_len == block)
797 return ex->ee_start + (block - ex->ee_block);
800 /* it looks index is empty
801 * try to find starting from index itself */
802 if (path[depth].p_bh)
803 return path[depth].p_bh->b_blocknr;
806 /* OK. use inode's group */
807 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
808 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
809 colour = (current->pid % 16) *
810 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
811 return bg_start + colour + block;
814 static int ext3_ext_new_extent_cb(struct ext3_extents_tree *tree,
815 struct ext3_ext_path *path,
816 struct ext3_extent *newex, int exist)
818 struct inode *inode = tree->inode;
819 struct bpointers *bp = tree->private;
820 int count, err, goal;
821 unsigned long pblock;
828 EXT_ASSERT(i == path->p_depth);
829 EXT_ASSERT(path[i].p_hdr);
836 if (bp->create == 0) {
838 if (newex->ee_block < bp->start)
839 i = bp->start - newex->ee_block;
840 if (i >= newex->ee_len)
841 CERROR("nothing to do?! i = %d, e_num = %u\n",
843 for (; i < newex->ee_len && bp->num; i++) {
854 tgen = EXT_GENERATION(tree);
855 count = ext3_ext_calc_credits_for_insert(tree, path);
856 ext3_up_truncate_sem(inode);
858 handle = journal_start(EXT3_JOURNAL(inode), count + EXT3_ALLOC_NEEDED + 1);
860 if (IS_ERR(handle)) {
861 ext3_down_truncate_sem(inode);
862 return PTR_ERR(handle);
865 if (tgen != EXT_GENERATION(tree)) {
866 /* the tree has changed. so path can be invalid at moment */
868 journal_stop(handle);
870 ext3_down_truncate_sem(inode);
873 ext3_down_truncate_sem(inode);
874 count = newex->ee_len;
875 goal = ext3_ext_find_goal(inode, path, newex->ee_block, &aflags);
876 aflags |= 2; /* block have been already reserved */
877 pblock = ext3_mb_new_blocks(handle, inode, goal, &count, aflags, &err);
880 EXT_ASSERT(count <= newex->ee_len);
882 /* insert new extent */
883 newex->ee_start = pblock;
884 newex->ee_len = count;
885 err = ext3_ext_insert_extent(handle, tree, path, newex);
889 /* correct on-disk inode size */
890 if (newex->ee_len > 0) {
891 new_i_size = (loff_t) newex->ee_block + newex->ee_len;
892 new_i_size = new_i_size << inode->i_blkbits;
893 if (new_i_size > EXT3_I(inode)->i_disksize) {
894 EXT3_I(inode)->i_disksize = new_i_size;
895 err = ext3_mark_inode_dirty(handle, inode);
900 journal_stop(handle);
906 CERROR("hmm. why do we find this extent?\n");
907 CERROR("initial space: %lu:%u\n",
908 bp->start, bp->init_num);
909 CERROR("current extent: %u/%u/%u %d\n",
910 newex->ee_block, newex->ee_len,
911 newex->ee_start, exist);
914 if (newex->ee_block < bp->start)
915 i = bp->start - newex->ee_block;
916 if (i >= newex->ee_len)
917 CERROR("nothing to do?! i = %d, e_num = %u\n",
919 for (; i < newex->ee_len && bp->num; i++) {
920 *(bp->created) = (exist == 0 ? 1 : 0);
922 *(bp->blocks) = newex->ee_start + i;
931 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
932 unsigned long num, unsigned long *blocks,
933 int *created, int create)
935 struct ext3_extents_tree tree;
939 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
940 block, block + num, (unsigned) inode->i_ino);
942 ext3_init_tree_desc(&tree, inode);
945 bp.created = created;
947 bp.init_num = bp.num = num;
950 ext3_down_truncate_sem(inode);
951 err = ext3_ext_walk_space(&tree, block, num, ext3_ext_new_extent_cb);
952 ext3_ext_invalidate_cache(&tree);
953 ext3_up_truncate_sem(inode);
955 /* unmap underlying pages/buffers from blockdevice mapping */
957 struct block_device *bdev = inode->i_sb->s_bdev;
958 for (i = 0; i < num; i++) {
961 unmap_underlying_metadata(bdev, blocks[i]);
967 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
968 int pages, unsigned long *blocks,
969 int *created, int create)
971 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
973 struct page *fp = NULL;
976 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
977 inode->i_ino, pages, (*page)->index);
979 /* pages are sorted already. so, we just have to find
980 * contig. space and process them properly */
983 /* start new extent */
988 } else if (fp->index + clen == (*page)->index) {
989 /* continue the extent */
996 /* process found extent */
997 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
998 clen * blocks_per_page, blocks,
1003 /* look for next extent */
1005 blocks += blocks_per_page * clen;
1006 created += blocks_per_page * clen;
1010 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1011 clen * blocks_per_page, blocks,
1018 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
1019 unsigned long *blocks, int *created, int create);
1020 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
1021 int pages, unsigned long *blocks,
1022 int *created, int create)
1024 int blocks_per_page = PAGE_SIZE >> inode->i_blkbits;
1028 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1029 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1031 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1032 inode->i_ino, *b, *cr, create, rc);
1036 b += blocks_per_page;
1037 cr += blocks_per_page;
1042 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1043 int pages, unsigned long *blocks,
1044 int *created, int create,
1045 struct semaphore *optional_sem)
1048 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
1049 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1050 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1051 blocks, created, create);
1055 if (optional_sem != NULL)
1057 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1059 if (optional_sem != NULL)
1065 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
1066 int nblocks, loff_t newsize);
1067 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
1068 int nblocks, loff_t newsize)
1070 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
1073 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1074 int size, loff_t *offs)
1076 struct inode *inode = file->f_dentry->d_inode;
1077 unsigned long block;
1078 struct buffer_head *bh;
1079 int err, blocksize, csize, boffs;
1081 /* prevent reading after eof */
1083 if (inode->i_size < *offs + size) {
1084 size = inode->i_size - *offs;
1087 CERROR("size %llu is too short for read %u@%llu\n",
1088 inode->i_size, size, *offs);
1090 } else if (size == 0) {
1097 blocksize = 1 << inode->i_blkbits;
1100 block = *offs >> inode->i_blkbits;
1101 boffs = *offs & (blocksize - 1);
1102 csize = min(blocksize - boffs, size);
1103 bh = ext3_bread(NULL, inode, block, 0, &err);
1105 CERROR("can't read block: %d\n", err);
1109 memcpy(buf, bh->b_data + boffs, csize);
1119 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1120 loff_t *offs, int force_sync)
1122 struct buffer_head *bh = NULL;
1123 unsigned long block;
1124 struct inode *inode = file->f_dentry->d_inode;
1125 loff_t old_size = inode->i_size, offset = *offs;
1126 loff_t new_size = inode->i_size;
1129 int err = 0, block_count = 0, blocksize, size, boffs;
1131 /* Determine how many transaction credits are needed */
1132 blocksize = 1 << inode->i_blkbits;
1133 block_count = (*offs & (blocksize - 1)) + bufsize;
1134 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1136 journal = EXT3_SB(inode->i_sb)->s_journal;
1138 handle = journal_start(journal,
1139 block_count * EXT3_DATA_TRANS_BLOCKS + 2);
1141 if (IS_ERR(handle)) {
1142 CERROR("can't start transaction\n");
1143 return PTR_ERR(handle);
1146 while (bufsize > 0) {
1150 block = offset >> inode->i_blkbits;
1151 boffs = offset & (blocksize - 1);
1152 size = min(blocksize - boffs, bufsize);
1153 bh = ext3_bread(handle, inode, block, 1, &err);
1155 CERROR("can't read/create block: %d\n", err);
1159 err = ext3_journal_get_write_access(handle, bh);
1161 CERROR("journal_get_write_access() returned error %d\n",
1165 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1166 memcpy(bh->b_data + boffs, buf, size);
1167 err = ext3_journal_dirty_metadata(handle, bh);
1169 CERROR("journal_dirty_metadata() returned error %d\n",
1173 if (offset + size > new_size)
1174 new_size = offset + size;
1181 handle->h_sync = 1; /* recovery likes this */
1186 /* correct in-core and on-disk sizes */
1187 if (new_size > inode->i_size) {
1189 if (new_size > inode->i_size)
1190 inode->i_size = new_size;
1191 if (inode->i_size > EXT3_I(inode)->i_disksize)
1192 EXT3_I(inode)->i_disksize = inode->i_size;
1193 if (inode->i_size > old_size)
1194 mark_inode_dirty(inode);
1199 journal_stop(handle);
1207 static int fsfilt_ext3_setup(struct obd_device *obd, struct super_block *sb)
1209 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1210 struct mds_obd *mds = &obd->u.mds;
1213 EXT3_SB(sb)->dx_lock = fsfilt_ext3_dx_lock;
1214 EXT3_SB(sb)->dx_unlock = fsfilt_ext3_dx_unlock;
1217 CWARN("Enabling PDIROPS\n");
1218 set_opt(EXT3_SB(sb)->s_mount_opt, PDIROPS);
1219 sb->s_flags |= S_PDIROPS;
1221 /* setup mdsnum in underlying fs */
1222 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1223 if (mds->mds_md_obd) {
1224 struct ext3_sb_info *sbi = EXT3_SB(sb);
1225 struct ext3_super_block *es = sbi->s_es;
1229 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_MDSNUM)) {
1230 CWARN("%s: set mdsnum %d in ext3\n",
1231 obd->obd_name, mds->mds_num);
1233 handle = journal_start(sbi->s_journal, 1);
1235 LASSERT(!IS_ERR(handle));
1236 err = ext3_journal_get_write_access(handle, sbi->s_sbh);
1238 EXT3_SET_INCOMPAT_FEATURE(sb,
1239 EXT3_FEATURE_INCOMPAT_MDSNUM);
1240 es->s_mdsnum = mds->mds_num;
1241 err = ext3_journal_dirty_metadata(handle, sbi->s_sbh);
1244 journal_stop(handle);
1247 CWARN("%s: mdsnum initialized to %u in ext3fs\n",
1248 obd->obd_name, es->s_mdsnum);
1250 sbi->s_mdsnum = es->s_mdsnum;
1256 extern int ext3_add_dir_entry(struct dentry *dentry);
1257 extern int ext3_del_dir_entry(struct dentry *dentry);
1259 static int fsfilt_ext3_add_dir_entry(struct obd_device *obd,
1260 struct dentry *parent,
1261 char *name, int namelen,
1263 unsigned long generation,
1267 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1268 struct dentry *dentry;
1271 LASSERT(namelen != 0);
1272 dentry = ll_lookup_one_len(name, parent, namelen);
1273 if (IS_ERR(dentry)) {
1274 CERROR("can't lookup %*s in %lu/%lu: %d\n", dentry->d_name.len,
1275 dentry->d_name.name, dentry->d_inode->i_ino,
1276 (unsigned long) dentry->d_inode->i_generation,
1277 (int) PTR_ERR(dentry));
1278 RETURN(PTR_ERR(dentry));
1280 if (dentry->d_inode != NULL || dentry->d_flags & DCACHE_CROSS_REF) {
1281 CERROR("dentry %*s(0x%p) found\n", dentry->d_name.len,
1282 dentry->d_name.name, dentry);
1287 /* mds_reint_rename() may use this method to add dir entry
1288 * that points onto local inode. and we don't want to find
1289 * it cross-ref by subsequent lookups */
1292 dentry->d_flags |= DCACHE_CROSS_REF;
1293 dentry->d_inum = ino;
1294 dentry->d_mdsnum = mds;
1295 dentry->d_generation = generation;
1296 dentry->d_fid = fid;
1298 err = ext3_add_dir_entry(dentry);
1305 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1310 static int fsfilt_ext3_del_dir_entry(struct obd_device *obd,
1311 struct dentry *dentry)
1313 #ifdef EXT3_FEATURE_INCOMPAT_MDSNUM
1316 err = ext3_del_dir_entry(dentry);
1322 #error "rebuild kernel and lustre with ext3-mds-num patch!"
1327 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1328 objects. Logs is number of logfiles to update */
1329 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1333 case FSFILT_OP_CREATE:
1334 /* directory leaf, index & indirect & EA*/
1335 return 4 + 3 * logs;
1336 case FSFILT_OP_UNLINK:
1342 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1343 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
1344 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1345 for (i = 0; i < op; i++, fso++) {
1346 int nblocks = fso->fso_bufcnt * blockpp;
1347 int ndindirect = min(nblocks, addrpp + 1);
1348 int nindir = nblocks + ndindirect + 1;
1352 return needed + 3 * logs;
1359 #define EXTENTS_EA "write_extents"
1360 #define EXTENTS_EA_SIZE 64
1362 int ext3_ext_in_ea_alloc_space(struct inode *, int, const char *, unsigned long, unsigned long);
1363 int ext3_ext_in_ea_remove_space(struct inode *, int, const char *, unsigned long, unsigned long);
1364 int ext3_ext_in_ea_get_extents(struct inode *, int, const char *, char **, int *);
1365 int ext3_ext_in_ea_get_extents_num(struct inode *, int, const char *, int *);
1367 static int fsfilt_ext3_insert_extents_ea(struct inode *inode,
1373 rc = ext3_ext_in_ea_alloc_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1374 EXTENTS_EA, from, num);
1378 static int fsfilt_ext3_remove_extents_ea(struct inode *inode,
1384 rc = ext3_ext_in_ea_remove_space(inode, EXT3_XATTR_INDEX_TRUSTED,
1385 EXTENTS_EA, from, num);
1389 extern int ext3_init_tree_in_ea(struct inode *inode, int name_index,
1390 const char *eaname, int size);
1392 static int fsfilt_ext3_init_extents_ea(struct inode *inode)
1396 rc = ext3_init_tree_in_ea(inode, EXT3_XATTR_INDEX_TRUSTED,
1401 static int fsfilt_ext3_get_inode_write_extents(struct inode *inode,
1402 char **pbuf, int *size)
1406 rc = ext3_ext_in_ea_get_extents(inode, EXT3_XATTR_INDEX_TRUSTED,
1407 EXTENTS_EA, pbuf, size);
1411 static int fsfilt_ext3_get_write_extents_num(struct inode *inode, int *size)
1415 rc = ext3_ext_in_ea_get_extents_num(inode, EXT3_XATTR_INDEX_TRUSTED,
1420 static struct fsfilt_operations fsfilt_ext3_ops = {
1422 .fs_owner = THIS_MODULE,
1423 .fs_start = fsfilt_ext3_start,
1424 .fs_brw_start = fsfilt_ext3_brw_start,
1425 .fs_commit = fsfilt_ext3_commit,
1426 .fs_commit_async = fsfilt_ext3_commit_async,
1427 .fs_commit_wait = fsfilt_ext3_commit_wait,
1428 .fs_setattr = fsfilt_ext3_setattr,
1429 .fs_iocontrol = fsfilt_ext3_iocontrol,
1430 .fs_set_md = fsfilt_ext3_set_md,
1431 .fs_get_md = fsfilt_ext3_get_md,
1432 .fs_readpage = fsfilt_ext3_readpage,
1433 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
1434 .fs_statfs = fsfilt_ext3_statfs,
1435 .fs_sync = fsfilt_ext3_sync,
1436 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
1437 .fs_prep_san_write = fsfilt_ext3_prep_san_write,
1438 .fs_write_record = fsfilt_ext3_write_record,
1439 .fs_read_record = fsfilt_ext3_read_record,
1440 .fs_setup = fsfilt_ext3_setup,
1441 .fs_getpage = fsfilt_ext3_getpage,
1442 .fs_send_bio = fsfilt_ext3_send_bio,
1443 .fs_set_xattr = fsfilt_ext3_set_xattr,
1444 .fs_get_xattr = fsfilt_ext3_get_xattr,
1445 .fs_get_op_len = fsfilt_ext3_get_op_len,
1446 .fs_add_dir_entry = fsfilt_ext3_add_dir_entry,
1447 .fs_del_dir_entry = fsfilt_ext3_del_dir_entry,
1448 .fs_init_extents_ea = fsfilt_ext3_init_extents_ea,
1449 .fs_insert_extents_ea = fsfilt_ext3_insert_extents_ea,
1450 .fs_remove_extents_ea = fsfilt_ext3_remove_extents_ea,
1451 .fs_get_inode_write_extents = fsfilt_ext3_get_inode_write_extents,
1452 .fs_get_write_extents_num = fsfilt_ext3_get_write_extents_num,
1455 static int __init fsfilt_ext3_init(void)
1459 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
1460 sizeof(struct fsfilt_cb_data), 0,
1463 CERROR("error allocating fsfilt journal callback cache\n");
1464 GOTO(out, rc = -ENOMEM);
1467 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
1470 kmem_cache_destroy(fcb_cache);
1475 static void __exit fsfilt_ext3_exit(void)
1477 fsfilt_unregister_ops(&fsfilt_ext3_ops);
1478 LASSERTF(kmem_cache_destroy(fcb_cache) == 0,
1479 "can't free fsfilt callback cache: count %d\n",
1480 atomic_read(&fcb_cache_count));
1483 module_init(fsfilt_ext3_init);
1484 module_exit(fsfilt_ext3_exit);
1486 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1487 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
1488 MODULE_LICENSE("GPL");