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
29 #include <linux/jbd.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <linux/quotaops.h>
33 #include <linux/ext3_fs.h>
34 #include <linux/ext3_jbd.h>
35 #include <linux/version.h>
37 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
38 #include <linux/ext3_xattr.h>
40 #include <linux/../../fs/ext3/xattr.h>
42 #include <linux/kp30.h>
43 #include <linux/lustre_fsfilt.h>
44 #include <linux/obd.h>
45 #include <linux/obd_class.h>
46 #include <linux/module.h>
48 static kmem_cache_t *fcb_cache;
49 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
51 struct fsfilt_cb_data {
52 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
53 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
54 struct obd_device *cb_obd; /* MDS/OBD completion device */
55 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
56 void *cb_data; /* MDS/OST completion function data */
59 #define EXT3_XATTR_INDEX_LUSTRE 5
60 #define XATTR_LUSTRE_MDS_OBJID "system.lustre_mds_objid"
63 * We don't currently need any additional blocks for rmdir and
64 * unlink transactions because we are storing the OST oa_id inside
65 * the inode (which we will be changing anyways as part of this
68 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private)
70 /* For updates to the last recieved file */
71 int nblocks = EXT3_DATA_TRANS_BLOCKS;
75 case FSFILT_OP_CREATE_LOG:
76 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
77 op = FSFILT_OP_CREATE;
79 case FSFILT_OP_UNLINK_LOG:
80 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
81 op = FSFILT_OP_UNLINK;
87 case FSFILT_OP_UNLINK:
88 nblocks += EXT3_DELETE_TRANS_BLOCKS;
90 case FSFILT_OP_RENAME:
91 /* modify additional directory */
92 nblocks += EXT3_DATA_TRANS_BLOCKS;
94 case FSFILT_OP_SYMLINK:
95 /* additional block + block bitmap + GDT for long symlink */
98 case FSFILT_OP_CREATE:
100 case FSFILT_OP_MKNOD:
101 /* modify one inode + block bitmap + GDT */
105 /* modify parent directory */
106 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS+EXT3_DATA_TRANS_BLOCKS;
108 case FSFILT_OP_SETATTR:
109 /* Setattr on inode */
112 default: CERROR("unknown transaction start op %d\n", op);
116 LASSERT(current->journal_info == desc_private);
118 handle = journal_start(EXT3_JOURNAL(inode), nblocks);
125 * Calculate the number of buffer credits needed to write multiple pages in
126 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
127 * doesn't have a nice API for calculating this sort of thing in advance.
129 * See comment above ext3_writepage_trans_blocks for details. We assume
130 * no data journaling is being done, but it does allow for all of the pages
131 * being non-contiguous. If we are guaranteed contiguous pages we could
132 * reduce the number of (d)indirect blocks a lot.
134 * With N blocks per page and P pages, for each inode we have at most:
136 * min(N*P, blocksize/4 + 1) dindirect blocks
139 * For the entire filesystem, we have at most:
140 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
141 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
142 * objcount inode blocks
144 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
146 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
148 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso)
150 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
151 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
152 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
155 int needed = objcount + 1;
158 for (i = 0; i < objcount; i++, fso++) {
159 int nblocks = fso->fso_bufcnt * blockpp;
160 int ndindirect = min(nblocks, addrpp + 1);
161 int nindir = nblocks + ndindirect + 1;
163 nbitmaps += nindir + nblocks;
164 ngdblocks += nindir + nblocks;
169 /* Assumes ext3 and ext3 have same sb_info layout at the start. */
170 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
171 nbitmaps = EXT3_SB(sb)->s_groups_count;
172 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
173 ngdblocks = EXT3_SB(sb)->s_gdb_count;
175 needed += nbitmaps + ngdblocks;
177 /* last_rcvd update */
178 needed += EXT3_DATA_TRANS_BLOCKS;
181 /* We assume that there will be 1 bit set in s_dquot.flags for each
182 * quota file that is active. This is at least true for now.
184 needed += hweight32(sb_any_quota_enabled(sb)) *
185 EXT3_SINGLEDATA_TRANS_BLOCKS;
191 /* We have to start a huge journal transaction here to hold all of the
192 * metadata for the pages being written here. This is necessitated by
193 * the fact that we do lots of prepare_write operations before we do
194 * any of the matching commit_write operations, so even if we split
195 * up to use "smaller" transactions none of them could complete until
196 * all of them were opened. By having a single journal transaction,
197 * we eliminate duplicate reservations for common blocks like the
198 * superblock and group descriptors or bitmaps.
200 * We will start the transaction here, but each prepare_write will
201 * add a refcount to the transaction, and each commit_write will
202 * remove a refcount. The transaction will be closed when all of
203 * the pages have been written.
205 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
206 int niocount, void *desc_private)
213 LASSERT(current->journal_info == desc_private);
214 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
215 needed = fsfilt_ext3_credits_needed(objcount, fso);
217 /* The number of blocks we could _possibly_ dirty can very large.
218 * We reduce our request if it is absurd (and we couldn't get that
219 * many credits for a single handle anyways).
221 * At some point we have to limit the size of I/Os sent at one time,
222 * increase the size of the journal, or we have to calculate the
223 * actual journal requirements more carefully by checking all of
224 * the blocks instead of being maximally pessimistic. It remains to
225 * be seen if this is a real problem or not.
227 if (needed > journal->j_max_transaction_buffers) {
228 CERROR("want too many journal credits (%d) using %d instead\n",
229 needed, journal->j_max_transaction_buffers);
230 needed = journal->j_max_transaction_buffers;
234 handle = journal_start(journal, needed);
237 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
240 LASSERT(handle->h_buffer_credits >= needed);
245 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
248 handle_t *handle = h;
251 handle->h_sync = 1; /* recovery likes this */
254 rc = journal_stop(handle);
260 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
261 struct iattr *iattr, int do_trunc)
263 struct inode *inode = dentry->d_inode;
268 /* A _really_ horrible hack to avoid removing the data stored
269 * in the block pointers; this is really the "small" stripe MD data.
270 * We can avoid further hackery by virtue of the MDS file size being
271 * zero all the time (which doesn't invoke block truncate at unlink
272 * time), so we assert we never change the MDS file size from zero. */
273 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
274 /* ATTR_SIZE would invoke truncate: clear it */
275 iattr->ia_valid &= ~ATTR_SIZE;
276 EXT3_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
278 /* make sure _something_ gets set - so new inode
279 * goes to disk (probably won't work over XFS */
280 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
281 iattr->ia_valid |= ATTR_MODE;
282 iattr->ia_mode = inode->i_mode;
286 /* Don't allow setattr to change file type */
287 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
289 if (inode->i_op->setattr) {
290 rc = inode->i_op->setattr(dentry, iattr);
292 rc = inode_change_ok(inode, iattr);
294 rc = inode_setattr(inode, iattr);
302 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
303 void *lmm, int lmm_size)
307 /* Nasty hack city - store stripe MD data in the block pointers if
308 * it will fit, because putting it in an EA currently kills the MDS
309 * performance. We'll fix this with "fast EAs" in the future.
311 if (inode->i_blocks == 0 && lmm_size <= sizeof(EXT3_I(inode)->i_data) -
312 sizeof(EXT3_I(inode)->i_data[0])) {
313 /* XXX old_size is debugging only */
314 int old_size = EXT3_I(inode)->i_data[0];
316 LASSERT(old_size < sizeof(EXT3_I(inode)->i_data));
317 CERROR("setting EA on %lu again... interesting\n",
321 EXT3_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
322 memcpy(&EXT3_I(inode)->i_data[1], lmm, lmm_size);
323 mark_inode_dirty(inode);
328 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
329 rc = ext3_xattr_set(handle, inode, EXT3_XATTR_INDEX_LUSTRE,
330 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
332 rc = ext3_xattr_set_handle(handle, inode,
333 EXT3_XATTR_INDEX_LUSTRE,
334 XATTR_LUSTRE_MDS_OBJID, lmm,
342 CERROR("error adding MD data to inode %lu: rc = %d\n",
347 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size)
351 if (inode->i_blocks == 0 && EXT3_I(inode)->i_data[0]) {
352 int size = le32_to_cpu(EXT3_I(inode)->i_data[0]);
353 LASSERT(size < sizeof(EXT3_I(inode)->i_data));
357 memcpy(lmm, &EXT3_I(inode)->i_data[1], size);
364 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_LUSTRE,
365 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
369 /* This gives us the MD size */
371 return (rc == -ENODATA) ? 0 : rc;
374 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
375 "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
376 memset(lmm, 0, lmm_size);
377 return (rc == -ENODATA) ? 0 : rc;
383 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
386 struct inode *inode = file->f_dentry->d_inode;
389 if (S_ISREG(inode->i_mode))
390 rc = file->f_op->read(file, buf, count, off);
392 const int blkbits = inode->i_sb->s_blocksize_bits;
393 const int blksize = inode->i_sb->s_blocksize;
395 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
396 count, inode->i_ino, *off);
398 struct buffer_head *bh;
401 if (*off < inode->i_size) {
404 bh = ext3_bread(NULL, inode, *off >> blkbits,
407 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
410 memcpy(buf, bh->b_data, blksize);
413 /* XXX in theory we should just fake
414 * this buffer and continue like ext3,
415 * especially if this is a partial read
417 CERROR("error read dir %lu+%llu: %d\n",
418 inode->i_ino, *off, err);
423 struct ext3_dir_entry_2 *fake = (void *)buf;
425 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
426 memset(fake, 0, sizeof(*fake));
427 fake->rec_len = cpu_to_le32(blksize);
439 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
441 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
443 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
445 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
446 atomic_dec(&fcb_cache_count);
449 static int fsfilt_ext3_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
450 void *handle, fsfilt_cb_t cb_func,
453 struct fsfilt_cb_data *fcb;
455 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
459 atomic_inc(&fcb_cache_count);
460 fcb->cb_func = cb_func;
462 fcb->cb_last_rcvd = last_rcvd;
463 fcb->cb_data = cb_data;
465 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
467 journal_callback_set(handle, fsfilt_ext3_cb_func,
468 (struct journal_callback *)fcb);
474 static int fsfilt_ext3_journal_data(struct file *filp)
476 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
477 /* bug 1576: enable data journaling on 2.5 when appropriate */
478 struct inode *inode = filp->f_dentry->d_inode;
479 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
485 * We need to hack the return value for the free inode counts because
486 * the current EA code requires one filesystem block per inode with EAs,
487 * so it is possible to run out of blocks before we run out of inodes.
489 * This can be removed when the ext3 EA code is fixed.
491 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
494 int rc = vfs_statfs(sb, &sfs);
496 if (!rc && sfs.f_bfree < sfs.f_ffree) {
497 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
498 sfs.f_ffree = sfs.f_bfree;
501 statfs_pack(osfs, &sfs);
505 static int fsfilt_ext3_sync(struct super_block *sb)
507 return ext3_force_commit(sb);
510 extern int ext3_prep_san_write(struct inode *inode, long *blocks,
511 int nblocks, loff_t newsize);
512 static int fsfilt_ext3_prep_san_write(struct inode *inode, long *blocks,
513 int nblocks, loff_t newsize)
515 return ext3_prep_san_write(inode, blocks, nblocks, newsize);
518 static int fsfilt_ext3_read_record(struct file * file, char *buf,
519 int size, loff_t *offs)
521 struct buffer_head *bh;
522 unsigned long block, boffs;
523 struct inode *inode = file->f_dentry->d_inode;
526 if (inode->i_size < *offs + size) {
527 CERROR("file size %llu is too short for read %u@%llu\n",
528 inode->i_size, size, *offs);
532 block = *offs >> inode->i_blkbits;
533 bh = ext3_bread(NULL, inode, block, 0, &err);
535 CERROR("can't read block: %d\n", err);
539 boffs = (unsigned)*offs % bh->b_size;
540 if (boffs + size > bh->b_size) {
541 CERROR("request crosses block's border. offset %llu, size %u\n",
547 memcpy(buf, bh->b_data + boffs, size);
553 static int fsfilt_ext3_write_record(struct file * file, char *buf,
554 int size, loff_t *offs)
556 struct buffer_head *bh;
557 unsigned long block, boffs;
558 struct inode *inode = file->f_dentry->d_inode;
559 loff_t old_size = inode->i_size;
564 journal = EXT3_SB(inode->i_sb)->s_journal;
565 handle = journal_start(journal, EXT3_DATA_TRANS_BLOCKS + 2);
566 if (handle == NULL) {
567 CERROR("can't start transaction\n");
571 block = *offs >> inode->i_blkbits;
572 if (*offs + size > inode->i_size) {
574 if (*offs + size > inode->i_size)
575 inode->i_size = ((loff_t)block + 1) << inode->i_blkbits;
579 bh = ext3_bread(handle, inode, block, 1, &err);
581 CERROR("can't read/create block: %d\n", err);
585 /* This is a hack only needed because ext3_get_block_handle() updates
586 * i_disksize after marking the inode dirty in ext3_splice_branch().
587 * We will fix that when we get a chance, as ext3_mark_inode_dirty()
588 * is not without cost, nor is it even exported.
590 if (inode->i_size > old_size)
591 mark_inode_dirty(inode);
593 boffs = (unsigned)*offs % bh->b_size;
594 if (boffs + size > bh->b_size) {
595 CERROR("request crosses block's border. offset %llu, size %u\n",
601 err = ext3_journal_get_write_access(handle, bh);
603 CERROR("journal_get_write_access() returned error %d\n", err);
606 memcpy(bh->b_data + boffs, buf, size);
607 err = ext3_journal_dirty_metadata(handle, bh);
609 CERROR("journal_dirty_metadata() returned error %d\n", err);
616 journal_stop(handle);
622 static struct fsfilt_operations fsfilt_ext3_ops = {
624 fs_owner: THIS_MODULE,
625 fs_start: fsfilt_ext3_start,
626 fs_brw_start: fsfilt_ext3_brw_start,
627 fs_commit: fsfilt_ext3_commit,
628 fs_setattr: fsfilt_ext3_setattr,
629 fs_set_md: fsfilt_ext3_set_md,
630 fs_get_md: fsfilt_ext3_get_md,
631 fs_readpage: fsfilt_ext3_readpage,
632 fs_journal_data: fsfilt_ext3_journal_data,
633 fs_set_last_rcvd: fsfilt_ext3_set_last_rcvd,
634 fs_statfs: fsfilt_ext3_statfs,
635 fs_sync: fsfilt_ext3_sync,
636 fs_prep_san_write: fsfilt_ext3_prep_san_write,
637 fs_write_record: fsfilt_ext3_write_record,
638 fs_read_record: fsfilt_ext3_read_record,
641 static int __init fsfilt_ext3_init(void)
645 //rc = ext3_xattr_register();
646 fcb_cache = kmem_cache_create("fsfilt_ext3_fcb",
647 sizeof(struct fsfilt_cb_data), 0,
650 CERROR("error allocating fsfilt journal callback cache\n");
651 GOTO(out, rc = -ENOMEM);
654 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
657 kmem_cache_destroy(fcb_cache);
662 static void __exit fsfilt_ext3_exit(void)
666 fsfilt_unregister_ops(&fsfilt_ext3_ops);
667 rc = kmem_cache_destroy(fcb_cache);
669 if (rc || atomic_read(&fcb_cache_count)) {
670 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
671 atomic_read(&fcb_cache_count), rc);
674 //rc = ext3_xattr_unregister();
677 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
678 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
679 MODULE_LICENSE("GPL");
681 module_init(fsfilt_ext3_init);
682 module_exit(fsfilt_ext3_exit);