1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/lib/fsfilt_extN.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/extN_fs.h>
34 #include <linux/extN_jbd.h>
35 #include <linux/extN_xattr.h>
36 #include <linux/kp30.h>
37 #include <linux/lustre_fsfilt.h>
38 #include <linux/obd.h>
39 #include <linux/obd_class.h>
40 #include <linux/module.h>
42 static kmem_cache_t *fcb_cache;
43 static atomic_t fcb_cache_count = ATOMIC_INIT(0);
45 struct fsfilt_cb_data {
46 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
47 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
48 struct obd_device *cb_obd; /* MDS/OBD completion device */
49 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
50 void *cb_data; /* MDS/OST completion function data */
53 #define EXTN_XATTR_INDEX_LUSTRE 5
54 #define XATTR_LUSTRE_MDS_OBJID "system.lustre_mds_objid"
57 * We don't currently need any additional blocks for rmdir and
58 * unlink transactions because we are storing the OST oa_id inside
59 * the inode (which we will be changing anyways as part of this
62 static void *fsfilt_extN_start(struct inode *inode, int op, void *desc_private)
64 /* For updates to the last recieved file */
65 int nblocks = EXTN_DATA_TRANS_BLOCKS;
69 case FSFILT_OP_CREATE_LOG:
70 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
71 op = FSFILT_OP_CREATE;
73 case FSFILT_OP_UNLINK_LOG:
74 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
75 op = FSFILT_OP_UNLINK;
81 case FSFILT_OP_UNLINK:
82 nblocks += EXTN_DELETE_TRANS_BLOCKS;
84 case FSFILT_OP_RENAME:
85 /* modify additional directory */
86 nblocks += EXTN_DATA_TRANS_BLOCKS;
88 case FSFILT_OP_SYMLINK:
89 /* additional block + block bitmap + GDT for long symlink */
92 case FSFILT_OP_CREATE:
95 /* modify one inode + block bitmap + GDT */
99 /* modify parent directory */
100 nblocks += EXTN_INDEX_EXTRA_TRANS_BLOCKS+EXTN_DATA_TRANS_BLOCKS;
102 case FSFILT_OP_SETATTR:
103 /* Setattr on inode */
106 default: CERROR("unknown transaction start op %d\n", op);
110 LASSERT(current->journal_info == desc_private);
112 handle = journal_start(EXTN_JOURNAL(inode), nblocks);
119 * Calculate the number of buffer credits needed to write multiple pages in
120 * a single extN transaction. No, this shouldn't be here, but as yet extN
121 * doesn't have a nice API for calculating this sort of thing in advance.
123 * See comment above extN_writepage_trans_blocks for details. We assume
124 * no data journaling is being done, but it does allow for all of the pages
125 * being non-contiguous. If we are guaranteed contiguous pages we could
126 * reduce the number of (d)indirect blocks a lot.
128 * With N blocks per page and P pages, for each inode we have at most:
130 * min(N*P, blocksize/4 + 1) dindirect blocks
133 * For the entire filesystem, we have at most:
134 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
135 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
136 * objcount inode blocks
138 * 2 * EXTN_SINGLEDATA_TRANS_BLOCKS for the quota files
140 * 1 EXTN_DATA_TRANS_BLOCKS for the last_rcvd update.
142 static int fsfilt_extN_credits_needed(int objcount, struct fsfilt_objinfo *fso)
144 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
145 int blockpp = 1 << (PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
146 int addrpp = EXTN_ADDR_PER_BLOCK(sb) * blockpp;
149 int needed = objcount + 1;
152 for (i = 0; i < objcount; i++, fso++) {
153 int nblocks = fso->fso_bufcnt * blockpp;
154 int ndindirect = min(nblocks, addrpp + 1);
155 int nindir = nblocks + ndindirect + 1;
157 nbitmaps += nindir + nblocks;
158 ngdblocks += nindir + nblocks;
163 /* Assumes extN and extN have same sb_info layout at the start. */
164 if (nbitmaps > EXTN_SB(sb)->s_groups_count)
165 nbitmaps = EXTN_SB(sb)->s_groups_count;
166 if (ngdblocks > EXTN_SB(sb)->s_gdb_count)
167 ngdblocks = EXTN_SB(sb)->s_gdb_count;
169 needed += nbitmaps + ngdblocks;
171 /* last_rcvd update */
172 needed += EXTN_DATA_TRANS_BLOCKS;
175 /* We assume that there will be 1 bit set in s_dquot.flags for each
176 * quota file that is active. This is at least true for now.
178 needed += hweight32(sb_any_quota_enabled(sb)) *
179 EXTN_SINGLEDATA_TRANS_BLOCKS;
185 /* We have to start a huge journal transaction here to hold all of the
186 * metadata for the pages being written here. This is necessitated by
187 * the fact that we do lots of prepare_write operations before we do
188 * any of the matching commit_write operations, so even if we split
189 * up to use "smaller" transactions none of them could complete until
190 * all of them were opened. By having a single journal transaction,
191 * we eliminate duplicate reservations for common blocks like the
192 * superblock and group descriptors or bitmaps.
194 * We will start the transaction here, but each prepare_write will
195 * add a refcount to the transaction, and each commit_write will
196 * remove a refcount. The transaction will be closed when all of
197 * the pages have been written.
199 static void *fsfilt_extN_brw_start(int objcount, struct fsfilt_objinfo *fso,
200 int niocount, void *desc_private)
207 LASSERT(current->journal_info == desc_private);
208 journal = EXTN_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
209 needed = fsfilt_extN_credits_needed(objcount, fso);
211 /* The number of blocks we could _possibly_ dirty can very large.
212 * We reduce our request if it is absurd (and we couldn't get that
213 * many credits for a single handle anyways).
215 * At some point we have to limit the size of I/Os sent at one time,
216 * increase the size of the journal, or we have to calculate the
217 * actual journal requirements more carefully by checking all of
218 * the blocks instead of being maximally pessimistic. It remains to
219 * be seen if this is a real problem or not.
221 if (needed > journal->j_max_transaction_buffers) {
222 CERROR("want too many journal credits (%d) using %d instead\n",
223 needed, journal->j_max_transaction_buffers);
224 needed = journal->j_max_transaction_buffers;
228 handle = journal_start(journal, needed);
231 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
234 LASSERT(handle->h_buffer_credits >= needed);
239 static int fsfilt_extN_commit(struct inode *inode, void *h, int force_sync)
242 handle_t *handle = h;
245 handle->h_sync = 1; /* recovery likes this */
248 rc = journal_stop(handle);
254 static int fsfilt_extN_setattr(struct dentry *dentry, void *handle,
255 struct iattr *iattr, int do_trunc)
257 struct inode *inode = dentry->d_inode;
262 /* A _really_ horrible hack to avoid removing the data stored
263 * in the block pointers; this is really the "small" stripe MD data.
264 * We can avoid further hackery by virtue of the MDS file size being
265 * zero all the time (which doesn't invoke block truncate at unlink
266 * time), so we assert we never change the MDS file size from zero. */
267 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
268 /* ATTR_SIZE would invoke truncate: clear it */
269 iattr->ia_valid &= ~ATTR_SIZE;
270 EXTN_I(inode)->i_disksize = inode->i_size = iattr->ia_size;
272 /* make sure _something_ gets set - so new inode
273 * goes to disk (probably won't work over XFS */
274 if (!(iattr->ia_valid & (ATTR_MODE | ATTR_MTIME | ATTR_CTIME))){
275 iattr->ia_valid |= ATTR_MODE;
276 iattr->ia_mode = inode->i_mode;
280 /* Don't allow setattr to change file type */
281 iattr->ia_mode = (inode->i_mode & S_IFMT)|(iattr->ia_mode & ~S_IFMT);
283 if (inode->i_op->setattr) {
284 rc = inode->i_op->setattr(dentry, iattr);
286 rc = inode_change_ok(inode, iattr);
288 rc = inode_setattr(inode, iattr);
296 static int fsfilt_extN_set_md(struct inode *inode, void *handle,
297 void *lmm, int lmm_size)
301 /* Nasty hack city - store stripe MD data in the block pointers if
302 * it will fit, because putting it in an EA currently kills the MDS
303 * performance. We'll fix this with "fast EAs" in the future.
305 if (inode->i_blocks == 0 && lmm_size <= sizeof(EXTN_I(inode)->i_data) -
306 sizeof(EXTN_I(inode)->i_data[0])) {
307 /* XXX old_size is debugging only */
308 int old_size = EXTN_I(inode)->i_data[0];
310 LASSERT(old_size < sizeof(EXTN_I(inode)->i_data));
311 CERROR("setting EA on %lu again... interesting\n",
315 EXTN_I(inode)->i_data[0] = cpu_to_le32(lmm_size);
316 memcpy(&EXTN_I(inode)->i_data[1], lmm, lmm_size);
317 mark_inode_dirty(inode);
322 rc = extN_xattr_set(handle, inode, EXTN_XATTR_INDEX_LUSTRE,
323 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size, 0);
329 CERROR("error adding MD data to inode %lu: rc = %d\n",
334 static int fsfilt_extN_get_md(struct inode *inode, void *lmm, int lmm_size)
338 if (inode->i_blocks == 0 && EXTN_I(inode)->i_data[0]) {
339 int size = le32_to_cpu(EXTN_I(inode)->i_data[0]);
340 LASSERT(size < sizeof(EXTN_I(inode)->i_data));
344 memcpy(lmm, &EXTN_I(inode)->i_data[1], size);
351 rc = extN_xattr_get(inode, EXTN_XATTR_INDEX_LUSTRE,
352 XATTR_LUSTRE_MDS_OBJID, lmm, lmm_size);
356 /* This gives us the MD size */
358 return (rc == -ENODATA) ? 0 : rc;
361 CDEBUG(D_INFO, "error getting EA %s from inode %lu: "
362 "rc = %d\n", XATTR_LUSTRE_MDS_OBJID, inode->i_ino, rc);
363 memset(lmm, 0, lmm_size);
364 return (rc == -ENODATA) ? 0 : rc;
370 static ssize_t fsfilt_extN_readpage(struct file *file, char *buf, size_t count,
373 struct inode *inode = file->f_dentry->d_inode;
376 if (S_ISREG(inode->i_mode))
377 rc = file->f_op->read(file, buf, count, off);
379 const int blkbits = inode->i_sb->s_blocksize_bits;
380 const int blksize = inode->i_sb->s_blocksize;
382 CDEBUG(D_EXT2, "reading "LPSZ" at dir %lu+%llu\n",
383 count, inode->i_ino, *off);
385 struct buffer_head *bh;
388 if (*off < inode->i_size) {
391 bh = extN_bread(NULL, inode, *off >> blkbits,
394 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
397 memcpy(buf, bh->b_data, blksize);
400 /* XXX in theory we should just fake
401 * this buffer and continue like extN,
402 * especially if this is a partial read
404 CERROR("error read dir %lu+%llu: %d\n",
405 inode->i_ino, *off, err);
410 struct extN_dir_entry_2 *fake = (void *)buf;
412 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
413 memset(fake, 0, sizeof(*fake));
414 fake->rec_len = cpu_to_le32(blksize);
426 static void fsfilt_extN_cb_func(struct journal_callback *jcb, int error)
428 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
430 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
432 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
433 atomic_dec(&fcb_cache_count);
436 static int fsfilt_extN_set_last_rcvd(struct obd_device *obd, __u64 last_rcvd,
437 void *handle, fsfilt_cb_t cb_func,
440 struct fsfilt_cb_data *fcb;
442 OBD_SLAB_ALLOC(fcb, fcb_cache, GFP_NOFS, sizeof *fcb);
446 atomic_inc(&fcb_cache_count);
447 fcb->cb_func = cb_func;
449 fcb->cb_last_rcvd = last_rcvd;
450 fcb->cb_data = cb_data;
452 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
454 journal_callback_set(handle, fsfilt_extN_cb_func,
455 (struct journal_callback *)fcb);
461 static int fsfilt_extN_journal_data(struct file *filp)
463 struct inode *inode = filp->f_dentry->d_inode;
465 EXTN_I(inode)->i_flags |= EXTN_JOURNAL_DATA_FL;
471 * We need to hack the return value for the free inode counts because
472 * the current EA code requires one filesystem block per inode with EAs,
473 * so it is possible to run out of blocks before we run out of inodes.
475 * This can be removed when the extN EA code is fixed.
477 static int fsfilt_extN_statfs(struct super_block *sb, struct obd_statfs *osfs)
480 int rc = vfs_statfs(sb, &sfs);
482 if (!rc && sfs.f_bfree < sfs.f_ffree) {
483 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
484 sfs.f_ffree = sfs.f_bfree;
487 statfs_pack(osfs, &sfs);
491 static int fsfilt_extN_sync(struct super_block *sb)
493 return extN_force_commit(sb);
496 extern int extN_prep_san_write(struct inode *inode, long *blocks,
497 int nblocks, loff_t newsize);
498 static int fsfilt_extN_prep_san_write(struct inode *inode, long *blocks,
499 int nblocks, loff_t newsize)
501 return extN_prep_san_write(inode, blocks, nblocks, newsize);
504 static int fsfilt_extN_read_record(struct file * file, char *buf,
505 int size, loff_t *offs)
507 struct buffer_head *bh;
508 unsigned long block, boffs;
509 struct inode *inode = file->f_dentry->d_inode;
512 if (inode->i_size < *offs + size) {
513 CERROR("file size %llu is too short for read %u@%llu\n",
514 inode->i_size, size, *offs);
518 block = *offs >> inode->i_blkbits;
519 bh = extN_bread(NULL, inode, block, 0, &err);
521 CERROR("can't read block: %d\n", err);
525 boffs = (unsigned)*offs % bh->b_size;
526 if (boffs + size > bh->b_size) {
527 CERROR("request crosses block's border. offset %llu, size %u\n",
533 memcpy(buf, bh->b_data + boffs, size);
539 static int fsfilt_extN_write_record(struct file * file, char *buf,
540 int size, loff_t *offs)
542 struct buffer_head *bh;
543 unsigned long block, boffs;
544 struct inode *inode = file->f_dentry->d_inode;
545 loff_t old_size = inode->i_size;
550 journal = EXTN_SB(inode->i_sb)->s_journal;
551 handle = journal_start(journal, EXTN_DATA_TRANS_BLOCKS + 2);
552 if (handle == NULL) {
553 CERROR("can't start transaction\n");
557 block = *offs >> inode->i_blkbits;
558 if (*offs + size > inode->i_size) {
560 if (*offs + size > inode->i_size)
561 inode->i_size = ((loff_t)block + 1) << inode->i_blkbits;
565 bh = extN_bread(handle, inode, block, 1, &err);
567 CERROR("can't read/create block: %d\n", err);
571 /* This is a hack only needed because extN_get_block_handle() updates
572 * i_disksize after marking the inode dirty in extN_splice_branch().
573 * We will fix that when we get a chance, as extN_mark_inode_dirty()
574 * is not without cost, nor is it even exported.
576 if (inode->i_size > old_size)
577 mark_inode_dirty(inode);
579 boffs = (unsigned)*offs % bh->b_size;
580 if (boffs + size > bh->b_size) {
581 CERROR("request crosses block's border. offset %llu, size %u\n",
587 err = extN_journal_get_write_access(handle, bh);
589 CERROR("journal_get_write_access() returned error %d\n", err);
592 memcpy(bh->b_data + boffs, buf, size);
593 err = extN_journal_dirty_metadata(handle, bh);
595 CERROR("journal_dirty_metadata() returned error %d\n", err);
602 journal_stop(handle);
608 static struct fsfilt_operations fsfilt_extN_ops = {
610 fs_owner: THIS_MODULE,
611 fs_start: fsfilt_extN_start,
612 fs_brw_start: fsfilt_extN_brw_start,
613 fs_commit: fsfilt_extN_commit,
614 fs_setattr: fsfilt_extN_setattr,
615 fs_set_md: fsfilt_extN_set_md,
616 fs_get_md: fsfilt_extN_get_md,
617 fs_readpage: fsfilt_extN_readpage,
618 fs_journal_data: fsfilt_extN_journal_data,
619 fs_set_last_rcvd: fsfilt_extN_set_last_rcvd,
620 fs_statfs: fsfilt_extN_statfs,
621 fs_sync: fsfilt_extN_sync,
622 fs_prep_san_write: fsfilt_extN_prep_san_write,
623 fs_write_record: fsfilt_extN_write_record,
624 fs_read_record: fsfilt_extN_read_record,
627 static int __init fsfilt_extN_init(void)
631 //rc = extN_xattr_register();
632 fcb_cache = kmem_cache_create("fsfilt_extN_fcb",
633 sizeof(struct fsfilt_cb_data), 0,
636 CERROR("error allocating fsfilt journal callback cache\n");
637 GOTO(out, rc = -ENOMEM);
640 rc = fsfilt_register_ops(&fsfilt_extN_ops);
643 kmem_cache_destroy(fcb_cache);
648 static void __exit fsfilt_extN_exit(void)
652 fsfilt_unregister_ops(&fsfilt_extN_ops);
653 rc = kmem_cache_destroy(fcb_cache);
655 if (rc || atomic_read(&fcb_cache_count)) {
656 CERROR("can't free fsfilt callback cache: count %d, rc = %d\n",
657 atomic_read(&fcb_cache_count), rc);
660 //rc = extN_xattr_unregister();
663 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
664 MODULE_DESCRIPTION("Lustre extN Filesystem Helper v0.1");
665 MODULE_LICENSE("GPL");
667 module_init(fsfilt_extN_init);
668 module_exit(fsfilt_extN_exit);