1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/lvfs/fsfilt_ext3.c
38 * Author: Andreas Dilger <adilger@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_FILTER
43 #include <linux/init.h>
44 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/pagemap.h>
48 #include <linux/quotaops.h>
49 #ifdef HAVE_EXT4_LDISKFS
50 #include <ext4/ext4.h>
51 #include <ext4/ext4_jbd2.h>
53 #include <linux/jbd.h>
54 #include <linux/ext3_fs.h>
55 #include <linux/ext3_jbd.h>
57 #include <linux/version.h>
58 #include <linux/bitops.h>
59 #include <linux/quota.h>
60 #include <linux/quotaio_v1.h>
61 #include <linux/quotaio_v2.h>
62 #include <ext3/xattr.h>
64 #include <libcfs/libcfs.h>
65 #include <lustre_fsfilt.h>
67 #include <lustre_quota.h>
68 #include <linux/lustre_compat25.h>
69 #include <linux/lprocfs_status.h>
71 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
72 #ifdef HAVE_EXT4_LDISKFS
73 #include <ext4/ext4_extents.h>
75 #include <linux/ext3_extents.h>
79 #include "lustre_quota_fmt.h"
81 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
82 #define FSFILT_DATA_TRANS_BLOCKS(sb) EXT3_DATA_TRANS_BLOCKS
83 #define FSFILT_DELETE_TRANS_BLOCKS(sb) EXT3_DELETE_TRANS_BLOCKS
85 #define FSFILT_DATA_TRANS_BLOCKS(sb) EXT3_DATA_TRANS_BLOCKS(sb)
86 #define FSFILT_DELETE_TRANS_BLOCKS(sb) EXT3_DELETE_TRANS_BLOCKS(sb)
89 #ifdef EXT3_SINGLEDATA_TRANS_BLOCKS_HAS_SB
90 /* for kernels 2.6.18 and later */
91 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS(sb)
93 #define FSFILT_SINGLEDATA_TRANS_BLOCKS(sb) EXT3_SINGLEDATA_TRANS_BLOCKS
96 #define fsfilt_ext3_journal_start(inode, nblocks) ext3_journal_start(inode, nblocks)
97 #define fsfilt_ext3_journal_stop(handle) ext3_journal_stop(handle)
99 static cfs_mem_cache_t *fcb_cache;
101 struct fsfilt_cb_data {
102 struct journal_callback cb_jcb; /* jbd private data - MUST BE FIRST */
103 fsfilt_cb_t cb_func; /* MDS/OBD completion function */
104 struct obd_device *cb_obd; /* MDS/OBD completion device */
105 __u64 cb_last_rcvd; /* MDS/OST last committed operation */
106 void *cb_data; /* MDS/OST completion function data */
109 #ifndef EXT3_XATTR_INDEX_TRUSTED /* temporary until we hit l28 kernel */
110 #define EXT3_XATTR_INDEX_TRUSTED 4
113 #ifdef HAVE_EXT4_LDISKFS
114 #define fsfilt_log_start_commit(journal, tid) jbd2_log_start_commit(journal, tid)
115 #define fsfilt_log_wait_commit(journal, tid) jbd2_log_wait_commit(journal, tid)
116 #define fsfilt_journal_callback_set(handle, func, jcb) jbd2_journal_callback_set(handle, func, jcb)
118 #define fsfilt_log_start_commit(journal, tid) log_start_commit(journal, tid)
119 #define fsfilt_log_wait_commit(journal, tid) log_wait_commit(journal, tid)
120 #define fsfilt_journal_callback_set(handle, func, jcb) journal_callback_set(handle, func, jcb)
121 #define ext_pblock(ex) le32_to_cpu((ex)->ee_start)
122 #define ext3_ext_store_pblock(ex, pblock) ((ex)->ee_start = cpu_to_le32(pblock))
123 #define ext3_inode_bitmap(sb,desc) le32_to_cpu((desc)->bg_inode_bitmap)
126 static char *fsfilt_ext3_get_label(struct super_block *sb)
128 return EXT3_SB(sb)->s_es->s_volume_name;
131 static int fsfilt_ext3_set_label(struct super_block *sb, char *label)
133 /* see e.g. fsfilt_ext3_write_record() */
138 journal = EXT3_SB(sb)->s_journal;
139 handle = ext3_journal_start_sb(sb, 1);
140 if (IS_ERR(handle)) {
141 CERROR("can't start transaction\n");
142 return(PTR_ERR(handle));
145 err = ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh);
149 memcpy(EXT3_SB(sb)->s_es->s_volume_name, label,
150 sizeof(EXT3_SB(sb)->s_es->s_volume_name));
152 err = ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
155 ext3_journal_stop(handle);
160 static char *fsfilt_ext3_uuid(struct super_block *sb)
162 return EXT3_SB(sb)->s_es->s_uuid;
165 #ifdef HAVE_DISK_INODE_VERSION
167 static __u64 get_i_version(struct inode *inode)
169 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)) && defined(HAVE_EXT4_LDISKFS)
170 return inode->i_version;
172 return EXT3_I(inode)->i_fs_version;
176 static void set_i_version(struct inode *inode, __u64 new_version)
178 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)) && defined(HAVE_EXT4_LDISKFS)
179 inode->i_version = new_version;
181 (EXT3_I(inode))->i_fs_version = new_version;
186 * Get the 64-bit version for an inode.
188 static __u64 fsfilt_ext3_get_version(struct inode *inode)
190 CDEBUG(D_INFO, "Get version "LPX64" for inode %lu\n",
191 get_i_version(inode), inode->i_ino);
192 return get_i_version(inode);
196 * Set the 64-bit version and return the old version.
198 static __u64 fsfilt_ext3_set_version(struct inode *inode, __u64 new_version)
200 __u64 old_version = get_i_version(inode);
202 CDEBUG(D_INFO, "Set version "LPX64" (old "LPX64") for inode %lu\n",
203 new_version, old_version, inode->i_ino);
204 set_i_version(inode, new_version);
205 /* version is set after all inode operations are finished, so we should
206 * mark it dirty here */
207 inode->i_sb->s_op->dirty_inode(inode);
214 * We don't currently need any additional blocks for rmdir and
215 * unlink transactions because we are storing the OST oa_id inside
216 * the inode (which we will be changing anyways as part of this
219 static void *fsfilt_ext3_start(struct inode *inode, int op, void *desc_private,
222 /* For updates to the last received file */
223 int nblocks = FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
227 if (current->journal_info) {
228 CDEBUG(D_INODE, "increasing refcount on %p\n",
229 current->journal_info);
234 case FSFILT_OP_RMDIR:
235 case FSFILT_OP_UNLINK:
236 /* delete one file + create/update logs for each stripe */
237 nblocks += FSFILT_DELETE_TRANS_BLOCKS(inode->i_sb);
238 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
239 FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
241 case FSFILT_OP_RENAME:
242 /* modify additional directory */
243 nblocks += FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb);
245 case FSFILT_OP_SYMLINK:
246 /* additional block + block bitmap + GDT for long symlink */
249 case FSFILT_OP_CREATE: {
250 #if defined(EXT3_EXTENTS_FL) && defined(EXT3_INDEX_FL) && !defined(HAVE_EXT4_LDISKFS)
253 if (!test_opt(inode->i_sb, EXTENTS)) {
255 } else if (((EXT3_I(inode)->i_flags &
256 cpu_to_le32(EXT3_EXTENTS_FL | EXT3_INDEX_FL)) ==
257 cpu_to_le32(EXT3_EXTENTS_FL | EXT3_INDEX_FL))) {
258 CWARN("extent-mapped directory found with "
259 "ext3-based ldiskfs - contact "
260 "http://bugzilla.lustre.org/\n");
267 case FSFILT_OP_MKDIR:
268 case FSFILT_OP_MKNOD:
269 /* modify one inode + block bitmap + GDT */
273 /* modify parent directory */
274 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
275 FSFILT_DATA_TRANS_BLOCKS(inode->i_sb);
276 /* create/update logs for each stripe */
277 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
278 FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
280 case FSFILT_OP_SETATTR:
281 /* Setattr on inode */
283 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
284 FSFILT_DATA_TRANS_BLOCKS(inode->i_sb);
285 /* quota chown log for each stripe */
286 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
287 FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb)) * logs;
289 case FSFILT_OP_CANCEL_UNLINK:
290 /* blocks for log header bitmap update OR
291 * blocks for catalog header bitmap update + unlink of logs */
292 nblocks = (LLOG_CHUNK_SIZE >> inode->i_blkbits) +
293 FSFILT_DELETE_TRANS_BLOCKS(inode->i_sb) * logs;
296 /* delete 2 file(file + array id) + create 1 file (array id)
297 * create/update logs for each stripe */
298 nblocks += 2 * FSFILT_DELETE_TRANS_BLOCKS(inode->i_sb);
300 /*create array log for head file*/
302 nblocks += (EXT3_INDEX_EXTRA_TRANS_BLOCKS +
303 FSFILT_SINGLEDATA_TRANS_BLOCKS(inode->i_sb));
304 /*update head file array */
305 nblocks += EXT3_INDEX_EXTRA_TRANS_BLOCKS +
306 FSFILT_DATA_TRANS_BLOCKS(inode->i_sb);
308 default: CERROR("unknown transaction start op %d\n", op);
312 LASSERT(current->journal_info == desc_private);
313 journal = EXT3_SB(inode->i_sb)->s_journal;
314 if (nblocks > journal->j_max_transaction_buffers) {
315 CWARN("too many credits %d for op %ux%u using %d instead\n",
316 nblocks, op, logs, journal->j_max_transaction_buffers);
317 nblocks = journal->j_max_transaction_buffers;
321 LASSERTF(nblocks > 0, "can't start %d credit transaction\n", nblocks);
322 handle = fsfilt_ext3_journal_start(inode, nblocks);
325 LASSERT(current->journal_info == handle);
327 CERROR("error starting handle for op %u (%u credits): rc %ld\n",
328 op, nblocks, PTR_ERR(handle));
333 * Calculate the number of buffer credits needed to write multiple pages in
334 * a single ext3 transaction. No, this shouldn't be here, but as yet ext3
335 * doesn't have a nice API for calculating this sort of thing in advance.
337 * See comment above ext3_writepage_trans_blocks for details. We assume
338 * no data journaling is being done, but it does allow for all of the pages
339 * being non-contiguous. If we are guaranteed contiguous pages we could
340 * reduce the number of (d)indirect blocks a lot.
342 * With N blocks per page and P pages, for each inode we have at most:
344 * min(N*P, blocksize/4 + 1) dindirect blocks
347 * For the entire filesystem, we have at most:
348 * min(sum(nindir + P), ngroups) bitmap blocks (from the above)
349 * min(sum(nindir + P), gdblocks) group descriptor blocks (from the above)
350 * objcount inode blocks
352 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quota files
354 * 1 EXT3_DATA_TRANS_BLOCKS for the last_rcvd update.
356 static int fsfilt_ext3_credits_needed(int objcount, struct fsfilt_objinfo *fso,
357 int niocount, struct niobuf_local *nb)
359 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
361 const int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
362 int nbitmaps = 0, ngdblocks;
363 int needed = objcount + 1; /* inodes + superblock */
366 for (i = 0, j = 0; i < objcount; i++, fso++) {
367 /* two or more dindirect blocks in case we cross boundary */
368 int ndind = (long)((nb[j + fso->fso_bufcnt - 1].offset -
370 sb->s_blocksize_bits) /
371 (EXT3_ADDR_PER_BLOCK(sb) * EXT3_ADDR_PER_BLOCK(sb));
372 nbitmaps += min(fso->fso_bufcnt, ndind > 0 ? ndind : 2);
374 /* leaf, indirect, tindirect blocks for first block */
375 nbitmaps += blockpp + 2;
377 j += fso->fso_bufcnt;
380 next_indir = nb[0].offset +
381 (EXT3_ADDR_PER_BLOCK(sb) << sb->s_blocksize_bits);
382 for (i = 1; i < niocount; i++) {
383 if (nb[i].offset >= next_indir) {
384 nbitmaps++; /* additional indirect */
385 next_indir = nb[i].offset +
386 (EXT3_ADDR_PER_BLOCK(sb)<<sb->s_blocksize_bits);
387 } else if (nb[i].offset != nb[i - 1].offset + sb->s_blocksize) {
388 nbitmaps++; /* additional indirect */
390 nbitmaps += blockpp; /* each leaf in different group? */
393 ngdblocks = nbitmaps;
394 if (nbitmaps > EXT3_SB(sb)->s_groups_count)
395 nbitmaps = EXT3_SB(sb)->s_groups_count;
396 if (ngdblocks > EXT3_SB(sb)->s_gdb_count)
397 ngdblocks = EXT3_SB(sb)->s_gdb_count;
399 needed += nbitmaps + ngdblocks;
401 /* last_rcvd update */
402 needed += FSFILT_DATA_TRANS_BLOCKS(sb);
404 #if defined(CONFIG_QUOTA)
405 /* We assume that there will be 1 bit set in s_dquot.flags for each
406 * quota file that is active. This is at least true for now.
408 needed += hweight32(sb_any_quota_enabled(sb)) *
409 FSFILT_SINGLEDATA_TRANS_BLOCKS(sb);
415 /* We have to start a huge journal transaction here to hold all of the
416 * metadata for the pages being written here. This is necessitated by
417 * the fact that we do lots of prepare_write operations before we do
418 * any of the matching commit_write operations, so even if we split
419 * up to use "smaller" transactions none of them could complete until
420 * all of them were opened. By having a single journal transaction,
421 * we eliminate duplicate reservations for common blocks like the
422 * superblock and group descriptors or bitmaps.
424 * We will start the transaction here, but each prepare_write will
425 * add a refcount to the transaction, and each commit_write will
426 * remove a refcount. The transaction will be closed when all of
427 * the pages have been written.
429 static void *fsfilt_ext3_brw_start(int objcount, struct fsfilt_objinfo *fso,
430 int niocount, struct niobuf_local *nb,
431 void *desc_private, int logs)
438 LASSERT(current->journal_info == desc_private);
439 journal = EXT3_SB(fso->fso_dentry->d_inode->i_sb)->s_journal;
440 needed = fsfilt_ext3_credits_needed(objcount, fso, niocount, nb);
442 /* The number of blocks we could _possibly_ dirty can very large.
443 * We reduce our request if it is absurd (and we couldn't get that
444 * many credits for a single handle anyways).
446 * At some point we have to limit the size of I/Os sent at one time,
447 * increase the size of the journal, or we have to calculate the
448 * actual journal requirements more carefully by checking all of
449 * the blocks instead of being maximally pessimistic. It remains to
450 * be seen if this is a real problem or not.
452 if (needed > journal->j_max_transaction_buffers) {
453 CERROR("want too many journal credits (%d) using %d instead\n",
454 needed, journal->j_max_transaction_buffers);
455 needed = journal->j_max_transaction_buffers;
458 LASSERTF(needed > 0, "can't start %d credit transaction\n", needed);
459 handle = fsfilt_ext3_journal_start(fso->fso_dentry->d_inode, needed);
460 if (IS_ERR(handle)) {
461 CERROR("can't get handle for %d credits: rc = %ld\n", needed,
464 LASSERT(handle->h_buffer_credits >= needed);
465 LASSERT(current->journal_info == handle);
471 static int fsfilt_ext3_extend(struct inode *inode, unsigned int nblocks,void *h)
473 handle_t *handle = h;
475 /* fsfilt_extend called with nblocks = 0 for testing in special cases */
477 handle->h_buffer_credits = 0;
478 CWARN("setting credits of handle %p to zero by request\n", h);
481 if (handle->h_buffer_credits > nblocks)
483 if (ext3_journal_extend(handle, nblocks) == 0)
486 ext3_mark_inode_dirty(handle, inode);
487 return ext3_journal_restart(handle, nblocks);
490 static int fsfilt_ext3_commit(struct inode *inode, void *h, int force_sync)
493 handle_t *handle = h;
495 LASSERT(current->journal_info == handle);
497 handle->h_sync = 1; /* recovery likes this */
499 rc = fsfilt_ext3_journal_stop(handle);
504 static int fsfilt_ext3_commit_async(struct inode *inode, void *h,
508 transaction_t *transaction;
509 handle_t *handle = h;
513 LASSERT(current->journal_info == handle);
515 transaction = handle->h_transaction;
516 journal = transaction->t_journal;
517 tid = transaction->t_tid;
518 /* we don't want to be blocked */
520 rc = fsfilt_ext3_journal_stop(handle);
522 CERROR("error while stopping transaction: %d\n", rc);
525 fsfilt_log_start_commit(journal, tid);
527 *wait_handle = (void *) tid;
528 CDEBUG(D_INODE, "commit async: %lu\n", (unsigned long) tid);
532 static int fsfilt_ext3_commit_wait(struct inode *inode, void *h)
534 journal_t *journal = EXT3_JOURNAL(inode);
535 tid_t tid = (tid_t)(long)h;
537 CDEBUG(D_INODE, "commit wait: %lu\n", (unsigned long) tid);
538 if (unlikely(is_journal_aborted(journal)))
541 fsfilt_log_wait_commit(EXT3_JOURNAL(inode), tid);
543 if (unlikely(is_journal_aborted(journal)))
548 static int fsfilt_ext3_setattr(struct dentry *dentry, void *handle,
549 struct iattr *iattr, int do_trunc)
551 struct inode *inode = dentry->d_inode;
554 /* Avoid marking the inode dirty on the superblock list unnecessarily.
555 * We are already writing the inode to disk as part of this
556 * transaction and want to avoid a lot of extra inode writeout
557 * later on. b=9828 */
558 if (iattr->ia_valid & ATTR_SIZE && !do_trunc) {
559 /* ATTR_SIZE would invoke truncate: clear it */
560 iattr->ia_valid &= ~ATTR_SIZE;
561 EXT3_I(inode)->i_disksize = iattr->ia_size;
562 i_size_write(inode, iattr->ia_size);
564 if (iattr->ia_valid & ATTR_UID)
565 inode->i_uid = iattr->ia_uid;
566 if (iattr->ia_valid & ATTR_GID)
567 inode->i_gid = iattr->ia_gid;
568 if (iattr->ia_valid & ATTR_ATIME)
569 inode->i_atime = iattr->ia_atime;
570 if (iattr->ia_valid & ATTR_MTIME)
571 inode->i_mtime = iattr->ia_mtime;
572 if (iattr->ia_valid & ATTR_CTIME)
573 inode->i_ctime = iattr->ia_ctime;
574 if (iattr->ia_valid & ATTR_MODE) {
575 inode->i_mode = iattr->ia_mode;
577 if (!in_group_p(inode->i_gid) &&
578 !cfs_capable(CFS_CAP_FSETID))
579 inode->i_mode &= ~S_ISGID;
582 inode->i_sb->s_op->dirty_inode(inode);
587 /* Don't allow setattr to change file type */
588 if (iattr->ia_valid & ATTR_MODE)
589 iattr->ia_mode = (inode->i_mode & S_IFMT) |
590 (iattr->ia_mode & ~S_IFMT);
592 /* We set these flags on the client, but have already checked perms
593 * so don't confuse inode_change_ok. */
594 iattr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET);
596 if (inode->i_op->setattr) {
597 rc = inode->i_op->setattr(dentry, iattr);
599 rc = inode_change_ok(inode, iattr);
601 rc = inode_setattr(inode, iattr);
608 static int fsfilt_ext3_iocontrol(struct inode *inode, struct file *file,
609 unsigned int cmd, unsigned long arg)
614 /* FIXME: Can't do this because of nested transaction deadlock */
615 if (cmd == EXT3_IOC_SETFLAGS && (*(int *)arg) & EXT3_JOURNAL_DATA_FL) {
616 CERROR("can't set data journal flag on file\n");
620 #ifdef HAVE_EXT4_LDISKFS
621 /* ext4_ioctl does not have a inode argument */
622 if (inode->i_fop->unlocked_ioctl)
623 rc = inode->i_fop->unlocked_ioctl(file, cmd, arg);
625 if (inode->i_fop->ioctl)
626 rc = inode->i_fop->ioctl(inode, file, cmd, arg);
634 static int fsfilt_ext3_set_md(struct inode *inode, void *handle,
635 void *lmm, int lmm_size, const char *name)
639 LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
641 rc = ext3_xattr_set_handle(handle, inode, EXT3_XATTR_INDEX_TRUSTED,
642 name, lmm, lmm_size, XATTR_NO_CTIME);
645 if (rc && rc != -EROFS)
646 CERROR("error adding MD data to inode %lu: rc = %d\n",
651 /* Must be called with i_mutex held */
652 static int fsfilt_ext3_get_md(struct inode *inode, void *lmm, int lmm_size,
657 LASSERT(TRYLOCK_INODE_MUTEX(inode) == 0);
659 rc = ext3_xattr_get(inode, EXT3_XATTR_INDEX_TRUSTED,
660 name, lmm, lmm_size);
662 /* This gives us the MD size */
664 return (rc == -ENODATA) ? 0 : rc;
667 CDEBUG(D_INFO, "error getting EA %d/%s from inode %lu: rc %d\n",
668 EXT3_XATTR_INDEX_TRUSTED, name,
670 memset(lmm, 0, lmm_size);
671 return (rc == -ENODATA) ? 0 : rc;
677 static int fsfilt_ext3_send_bio(int rw, struct inode *inode, struct bio *bio)
683 static ssize_t fsfilt_ext3_readpage(struct file *file, char *buf, size_t count,
686 struct inode *inode = file->f_dentry->d_inode;
689 if (S_ISREG(inode->i_mode))
690 rc = file->f_op->read(file, buf, count, off);
692 const int blkbits = inode->i_sb->s_blocksize_bits;
693 const int blksize = inode->i_sb->s_blocksize;
695 CDEBUG(D_EXT2, "reading %lu at dir %lu+%llu\n",
696 (unsigned long)count, inode->i_ino, *off);
698 struct buffer_head *bh;
701 if (*off < i_size_read(inode)) {
704 bh = ext3_bread(NULL, inode, *off >> blkbits,
707 CDEBUG(D_EXT2, "read %u@%llu\n", blksize, *off);
710 memcpy(buf, bh->b_data, blksize);
713 /* XXX in theory we should just fake
714 * this buffer and continue like ext3,
715 * especially if this is a partial read
717 CERROR("error read dir %lu+%llu: %d\n",
718 inode->i_ino, *off, err);
723 struct ext3_dir_entry_2 *fake = (void *)buf;
725 CDEBUG(D_EXT2, "fake %u@%llu\n", blksize, *off);
726 memset(fake, 0, sizeof(*fake));
727 fake->rec_len = cpu_to_le16(blksize);
739 static void fsfilt_ext3_cb_func(struct journal_callback *jcb, int error)
741 struct fsfilt_cb_data *fcb = (struct fsfilt_cb_data *)jcb;
743 fcb->cb_func(fcb->cb_obd, fcb->cb_last_rcvd, fcb->cb_data, error);
745 OBD_SLAB_FREE(fcb, fcb_cache, sizeof *fcb);
748 static int fsfilt_ext3_add_journal_cb(struct obd_device *obd, __u64 last_rcvd,
749 void *handle, fsfilt_cb_t cb_func,
752 struct fsfilt_cb_data *fcb;
754 OBD_SLAB_ALLOC_PTR_GFP(fcb, fcb_cache, CFS_ALLOC_IO);
758 fcb->cb_func = cb_func;
760 fcb->cb_last_rcvd = last_rcvd;
761 fcb->cb_data = cb_data;
763 CDEBUG(D_EXT2, "set callback for last_rcvd: "LPD64"\n", last_rcvd);
764 fsfilt_journal_callback_set(handle, fsfilt_ext3_cb_func,
765 (struct journal_callback *)fcb);
771 * We need to hack the return value for the free inode counts because
772 * the current EA code requires one filesystem block per inode with EAs,
773 * so it is possible to run out of blocks before we run out of inodes.
775 * This can be removed when the ext3 EA code is fixed.
777 static int fsfilt_ext3_statfs(struct super_block *sb, struct obd_statfs *osfs)
782 memset(&sfs, 0, sizeof(sfs));
783 rc = ll_do_statfs(sb, &sfs);
784 if (!rc && sfs.f_bfree < sfs.f_ffree) {
785 sfs.f_files = (sfs.f_files - sfs.f_ffree) + sfs.f_bfree;
786 sfs.f_ffree = sfs.f_bfree;
789 statfs_pack(osfs, &sfs);
793 static int fsfilt_ext3_sync(struct super_block *sb)
795 return ext3_force_commit(sb);
798 #if defined(EXT3_MULTIBLOCK_ALLOCATOR) && (!defined(EXT3_EXT_CACHE_NO) || defined(EXT_CACHE_MARK))
799 #warning "kernel code has old extents/mballoc patch, disabling"
800 #undef EXT3_MULTIBLOCK_ALLOCATOR
802 #ifndef EXT3_EXTENTS_FL
803 #define EXT3_EXTENTS_FL 0x00080000 /* Inode uses extents */
806 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
807 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
808 #define fsfilt_up_truncate_sem(inode) up(&EXT3_I(inode)->truncate_sem);
809 #define fsfilt_down_truncate_sem(inode) down(&EXT3_I(inode)->truncate_sem);
811 #ifdef HAVE_EXT4_LDISKFS
812 #define fsfilt_up_truncate_sem(inode) up_write((&EXT4_I(inode)->i_data_sem));
813 #define fsfilt_down_truncate_sem(inode) down_write((&EXT4_I(inode)->i_data_sem));
815 #define fsfilt_up_truncate_sem(inode) mutex_unlock(&EXT3_I(inode)->truncate_mutex);
816 #define fsfilt_down_truncate_sem(inode) mutex_lock(&EXT3_I(inode)->truncate_mutex);
821 #define EXT_ASSERT(cond) BUG_ON(!(cond))
824 #ifdef EXT3_EXT_HAS_NO_TREE
825 /* for kernels 2.6.18 and later */
826 #ifdef HAVE_EXT4_LDISKFS
827 #define EXT_GENERATION(inode) (EXT4_I(inode)->i_ext_generation)
829 #define EXT_GENERATION(inode) ext_generation(inode)
831 #define ext3_ext_base inode
832 #define ext3_ext_base2inode(inode) (inode)
833 #define EXT_DEPTH(inode) ext_depth(inode)
834 #define fsfilt_ext3_ext_walk_space(inode, block, num, cb, cbdata) \
835 ext3_ext_walk_space(inode, block, num, cb, cbdata);
837 #define ext3_ext_base ext3_extents_tree
838 #define ext3_ext_base2inode(tree) (tree->inode)
839 #define fsfilt_ext3_ext_walk_space(tree, block, num, cb, cbdata) \
840 ext3_ext_walk_space(tree, block, num, cb);
843 #include <linux/lustre_version.h>
846 unsigned long *blocks;
854 static long ext3_ext_find_goal(struct inode *inode, struct ext3_ext_path *path,
855 unsigned long block, int *aflags)
857 struct ext3_inode_info *ei = EXT3_I(inode);
858 unsigned long bg_start;
859 unsigned long colour;
863 struct ext3_extent *ex;
864 depth = path->p_depth;
866 /* try to predict block placement */
867 if ((ex = path[depth].p_ext))
868 return ext_pblock(ex) + (block - le32_to_cpu(ex->ee_block));
870 /* it looks index is empty
871 * try to find starting from index itself */
872 if (path[depth].p_bh)
873 return path[depth].p_bh->b_blocknr;
876 /* OK. use inode's group */
877 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
878 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
879 colour = (current->pid % 16) *
880 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
881 return bg_start + colour + block;
884 #define ll_unmap_underlying_metadata(sb, blocknr) \
885 unmap_underlying_metadata((sb)->s_bdev, blocknr)
887 #ifndef EXT3_MB_HINT_GROUP_ALLOC
888 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
889 struct ext3_ext_path *path, unsigned long block,
890 unsigned long *count, int *err)
892 unsigned long pblock, goal;
894 struct inode *inode = ext3_ext_base2inode(base);
896 goal = ext3_ext_find_goal(inode, path, block, &aflags);
897 aflags |= 2; /* block have been already reserved */
898 pblock = ext3_mb_new_blocks(handle, inode, goal, count, aflags, err);
903 static unsigned long new_blocks(handle_t *handle, struct ext3_ext_base *base,
904 struct ext3_ext_path *path, unsigned long block,
905 unsigned long *count, int *err)
907 struct inode *inode = ext3_ext_base2inode(base);
908 struct ext3_allocation_request ar;
909 unsigned long pblock;
912 /* find neighbour allocated blocks */
914 *err = ext3_ext_search_left(base, path, &ar.lleft, &ar.pleft);
918 *err = ext3_ext_search_right(base, path, &ar.lright, &ar.pright);
922 /* allocate new block */
923 ar.goal = ext3_ext_find_goal(inode, path, block, &aflags);
927 ar.flags = EXT3_MB_HINT_DATA;
928 pblock = ext3_mb_new_blocks(handle, &ar, err);
934 #ifdef EXT3_EXT_HAS_NO_TREE
935 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
936 struct ext3_ext_path *path,
937 struct ext3_ext_cache *cex,
938 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
939 struct ext3_extent *ex,
943 struct bpointers *bp = cbdata;
945 static int ext3_ext_new_extent_cb(struct ext3_ext_base *base,
946 struct ext3_ext_path *path,
947 struct ext3_ext_cache *cex
948 #ifdef HAVE_EXT_PREPARE_CB_EXTENT
949 , struct ext3_extent *ex
953 struct bpointers *bp = base->private;
955 struct inode *inode = ext3_ext_base2inode(base);
956 struct ext3_extent nex;
957 unsigned long pblock;
964 EXT_ASSERT(i == path->p_depth);
965 EXT_ASSERT(path[i].p_hdr);
967 if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
972 if (bp->create == 0) {
974 if (cex->ec_block < bp->start)
975 i = bp->start - cex->ec_block;
976 if (i >= cex->ec_len)
977 CERROR("nothing to do?! i = %d, e_num = %u\n",
979 for (; i < cex->ec_len && bp->num; i++) {
991 tgen = EXT_GENERATION(base);
992 count = ext3_ext_calc_credits_for_insert(base, path);
993 fsfilt_up_truncate_sem(inode);
995 handle = fsfilt_ext3_journal_start(inode, count+EXT3_ALLOC_NEEDED+1);
996 if (IS_ERR(handle)) {
997 fsfilt_down_truncate_sem(inode);
998 return PTR_ERR(handle);
1001 fsfilt_down_truncate_sem(inode);
1002 if (tgen != EXT_GENERATION(base)) {
1003 /* the tree has changed. so path can be invalid at moment */
1004 fsfilt_ext3_journal_stop(handle);
1008 count = cex->ec_len;
1009 pblock = new_blocks(handle, base, path, cex->ec_block, &count, &err);
1012 EXT_ASSERT(count <= cex->ec_len);
1014 /* insert new extent */
1015 nex.ee_block = cpu_to_le32(cex->ec_block);
1016 ext3_ext_store_pblock(&nex, pblock);
1017 nex.ee_len = cpu_to_le16(count);
1018 err = ext3_ext_insert_extent(handle, base, path, &nex);
1020 /* free data blocks we just allocated */
1021 /* not a good idea to call discard here directly,
1022 * but otherwise we'd need to call it every free() */
1023 #ifdef EXT3_MB_HINT_GROUP_ALLOC
1024 ext3_mb_discard_inode_preallocations(inode);
1026 ext3_free_blocks(handle, inode, ext_pblock(&nex),
1027 cpu_to_le16(nex.ee_len), 0);
1032 * Putting len of the actual extent we just inserted,
1033 * we are asking ext3_ext_walk_space() to continue
1034 * scaning after that block
1036 cex->ec_len = le16_to_cpu(nex.ee_len);
1037 cex->ec_start = ext_pblock(&nex);
1038 BUG_ON(le16_to_cpu(nex.ee_len) == 0);
1039 BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
1042 fsfilt_ext3_journal_stop(handle);
1047 CERROR("hmm. why do we find this extent?\n");
1048 CERROR("initial space: %lu:%u\n",
1049 bp->start, bp->init_num);
1050 CERROR("current extent: %u/%u/%llu %d\n",
1051 cex->ec_block, cex->ec_len,
1052 (unsigned long long)cex->ec_start,
1056 if (cex->ec_block < bp->start)
1057 i = bp->start - cex->ec_block;
1058 if (i >= cex->ec_len)
1059 CERROR("nothing to do?! i = %d, e_num = %u\n",
1061 for (; i < cex->ec_len && bp->num; i++) {
1062 *(bp->blocks) = cex->ec_start + i;
1063 if (cex->ec_type == EXT3_EXT_CACHE_EXTENT) {
1067 /* unmap any possible underlying metadata from
1068 * the block device mapping. bug 6998. */
1069 ll_unmap_underlying_metadata(inode->i_sb,
1081 int fsfilt_map_nblocks(struct inode *inode, unsigned long block,
1082 unsigned long num, unsigned long *blocks,
1083 int *created, int create)
1085 #ifdef EXT3_EXT_HAS_NO_TREE
1086 struct ext3_ext_base *base = inode;
1088 struct ext3_extents_tree tree;
1089 struct ext3_ext_base *base = &tree;
1091 struct bpointers bp;
1094 CDEBUG(D_OTHER, "blocks %lu-%lu requested for inode %u\n",
1095 block, block + num - 1, (unsigned) inode->i_ino);
1097 #ifndef EXT3_EXT_HAS_NO_TREE
1098 ext3_init_tree_desc(base, inode);
1102 bp.created = created;
1104 bp.init_num = bp.num = num;
1107 fsfilt_down_truncate_sem(inode);
1108 err = fsfilt_ext3_ext_walk_space(base, block, num,
1109 ext3_ext_new_extent_cb, &bp);
1110 ext3_ext_invalidate_cache(base);
1111 fsfilt_up_truncate_sem(inode);
1116 int fsfilt_ext3_map_ext_inode_pages(struct inode *inode, struct page **page,
1117 int pages, unsigned long *blocks,
1118 int *created, int create)
1120 int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1122 struct page *fp = NULL;
1125 CDEBUG(D_OTHER, "inode %lu: map %d pages from %lu\n",
1126 inode->i_ino, pages, (*page)->index);
1128 /* pages are sorted already. so, we just have to find
1129 * contig. space and process them properly */
1132 /* start new extent */
1137 } else if (fp->index + clen == (*page)->index) {
1138 /* continue the extent */
1145 /* process found extent */
1146 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1147 clen * blocks_per_page, blocks,
1152 /* look for next extent */
1154 blocks += blocks_per_page * clen;
1155 created += blocks_per_page * clen;
1159 rc = fsfilt_map_nblocks(inode, fp->index * blocks_per_page,
1160 clen * blocks_per_page, blocks,
1165 #endif /* EXT3_MULTIBLOCK_ALLOCATOR */
1167 extern int ext3_map_inode_page(struct inode *inode, struct page *page,
1168 unsigned long *blocks, int *created, int create);
1169 int fsfilt_ext3_map_bm_inode_pages(struct inode *inode, struct page **page,
1170 int pages, unsigned long *blocks,
1171 int *created, int create)
1173 int blocks_per_page = CFS_PAGE_SIZE >> inode->i_blkbits;
1177 for (i = 0, cr = created, b = blocks; i < pages; i++, page++) {
1178 rc = ext3_map_inode_page(inode, *page, b, cr, create);
1180 CERROR("ino %lu, blk %lu cr %u create %d: rc %d\n",
1181 inode->i_ino, *b, *cr, create, rc);
1185 b += blocks_per_page;
1186 cr += blocks_per_page;
1191 int fsfilt_ext3_map_inode_pages(struct inode *inode, struct page **page,
1192 int pages, unsigned long *blocks,
1193 int *created, int create,
1194 struct semaphore *optional_sem)
1197 #ifdef EXT3_MULTIBLOCK_ALLOCATOR
1198 if (EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL) {
1199 rc = fsfilt_ext3_map_ext_inode_pages(inode, page, pages,
1200 blocks, created, create);
1204 if (optional_sem != NULL)
1206 rc = fsfilt_ext3_map_bm_inode_pages(inode, page, pages, blocks,
1208 if (optional_sem != NULL)
1214 int fsfilt_ext3_read(struct inode *inode, void *buf, int size, loff_t *offs)
1216 unsigned long block;
1217 struct buffer_head *bh;
1218 int err, blocksize, csize, boffs, osize = size;
1220 /* prevent reading after eof */
1222 if (i_size_read(inode) < *offs + size) {
1223 size = i_size_read(inode) - *offs;
1226 CERROR("size %llu is too short for read %u@%llu\n",
1227 i_size_read(inode), size, *offs);
1229 } else if (size == 0) {
1236 blocksize = 1 << inode->i_blkbits;
1239 block = *offs >> inode->i_blkbits;
1240 boffs = *offs & (blocksize - 1);
1241 csize = min(blocksize - boffs, size);
1242 bh = ext3_bread(NULL, inode, block, 0, &err);
1244 CERROR("can't read block: %d\n", err);
1248 memcpy(buf, bh->b_data + boffs, csize);
1257 EXPORT_SYMBOL(fsfilt_ext3_read);
1259 static int fsfilt_ext3_read_record(struct file * file, void *buf,
1260 int size, loff_t *offs)
1263 rc = fsfilt_ext3_read(file->f_dentry->d_inode, buf, size, offs);
1269 int fsfilt_ext3_write_handle(struct inode *inode, void *buf, int bufsize,
1270 loff_t *offs, handle_t *handle)
1272 struct buffer_head *bh = NULL;
1273 loff_t old_size = i_size_read(inode), offset = *offs;
1274 loff_t new_size = i_size_read(inode);
1275 unsigned long block;
1276 int err = 0, blocksize = 1 << inode->i_blkbits, size, boffs;
1278 while (bufsize > 0) {
1282 block = offset >> inode->i_blkbits;
1283 boffs = offset & (blocksize - 1);
1284 size = min(blocksize - boffs, bufsize);
1285 bh = ext3_bread(handle, inode, block, 1, &err);
1287 CERROR("can't read/create block: %d\n", err);
1291 err = ext3_journal_get_write_access(handle, bh);
1293 CERROR("journal_get_write_access() returned error %d\n",
1297 LASSERT(bh->b_data + boffs + size <= bh->b_data + bh->b_size);
1298 memcpy(bh->b_data + boffs, buf, size);
1299 err = ext3_journal_dirty_metadata(handle, bh);
1301 CERROR("journal_dirty_metadata() returned error %d\n",
1305 if (offset + size > new_size)
1306 new_size = offset + size;
1314 /* correct in-core and on-disk sizes */
1315 if (new_size > i_size_read(inode)) {
1317 if (new_size > i_size_read(inode))
1318 i_size_write(inode, new_size);
1319 if (i_size_read(inode) > EXT3_I(inode)->i_disksize)
1320 EXT3_I(inode)->i_disksize = i_size_read(inode);
1321 if (i_size_read(inode) > old_size)
1322 mark_inode_dirty(inode);
1330 EXPORT_SYMBOL(fsfilt_ext3_write_handle);
1332 static int fsfilt_ext3_write_record(struct file *file, void *buf, int bufsize,
1333 loff_t *offs, int force_sync)
1335 struct inode *inode = file->f_dentry->d_inode;
1337 int err, block_count = 0, blocksize;
1339 /* Determine how many transaction credits are needed */
1340 blocksize = 1 << inode->i_blkbits;
1341 block_count = (*offs & (blocksize - 1)) + bufsize;
1342 block_count = (block_count + blocksize - 1) >> inode->i_blkbits;
1344 handle = fsfilt_ext3_journal_start(inode,
1345 block_count * FSFILT_DATA_TRANS_BLOCKS(inode->i_sb) + 2);
1346 if (IS_ERR(handle)) {
1347 CERROR("can't start transaction for %d blocks (%d bytes)\n",
1348 block_count * FSFILT_DATA_TRANS_BLOCKS(inode->i_sb) + 2, bufsize);
1349 return PTR_ERR(handle);
1352 err = fsfilt_ext3_write_handle(inode, buf, bufsize, offs, handle);
1354 if (!err && force_sync)
1355 handle->h_sync = 1; /* recovery likes this */
1357 fsfilt_ext3_journal_stop(handle);
1362 static int fsfilt_ext3_setup(struct super_block *sb)
1364 #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)) && \
1365 defined(HAVE_QUOTA_SUPPORT)) || defined(S_PDIROPS)
1366 struct ext3_sb_info *sbi = EXT3_SB(sb);
1368 sbi->dx_lock = fsfilt_ext3_dx_lock;
1369 sbi->dx_unlock = fsfilt_ext3_dx_unlock;
1373 CWARN("Enabling PDIROPS\n");
1374 set_opt(sbi->s_mount_opt, PDIROPS);
1375 sb->s_flags |= S_PDIROPS;
1377 if (!EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_DIR_INDEX))
1378 CWARN("filesystem doesn't have dir_index feature enabled\n");
1379 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)) && defined(HAVE_QUOTA_SUPPORT)
1380 /* enable journaled quota support */
1381 /* kfreed in ext3_put_super() */
1382 sbi->s_qf_names[USRQUOTA] = kstrdup("lquota.user.reserved", GFP_KERNEL);
1383 if (!sbi->s_qf_names[USRQUOTA])
1385 sbi->s_qf_names[GRPQUOTA] = kstrdup("lquota.group.reserved", GFP_KERNEL);
1386 if (!sbi->s_qf_names[GRPQUOTA]) {
1387 kfree(sbi->s_qf_names[USRQUOTA]);
1388 sbi->s_qf_names[USRQUOTA] = NULL;
1391 sbi->s_jquota_fmt = QFMT_VFS_V0;
1392 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13))
1393 set_opt(sbi->s_mount_opt, QUOTA);
1399 /* If fso is NULL, op is FSFILT operation, otherwise op is number of fso
1400 objects. Logs is number of logfiles to update */
1401 static int fsfilt_ext3_get_op_len(int op, struct fsfilt_objinfo *fso, int logs)
1405 case FSFILT_OP_CREATE:
1406 /* directory leaf, index & indirect & EA*/
1407 return 4 + 3 * logs;
1408 case FSFILT_OP_UNLINK:
1414 struct super_block *sb = fso->fso_dentry->d_inode->i_sb;
1415 int blockpp = 1 << (CFS_PAGE_SHIFT - sb->s_blocksize_bits);
1416 int addrpp = EXT3_ADDR_PER_BLOCK(sb) * blockpp;
1417 for (i = 0; i < op; i++, fso++) {
1418 int nblocks = fso->fso_bufcnt * blockpp;
1419 int ndindirect = min(nblocks, addrpp + 1);
1420 int nindir = nblocks + ndindirect + 1;
1424 return needed + 3 * logs;
1430 #ifdef HAVE_QUOTA_SUPPORT
1431 #define DQINFO_COPY(out, in) \
1433 Q_COPY(out, in, dqi_bgrace); \
1434 Q_COPY(out, in, dqi_igrace); \
1435 Q_COPY(out, in, dqi_flags); \
1436 Q_COPY(out, in, dqi_valid); \
1439 #define DQBLK_COPY(out, in) \
1441 Q_COPY(out, in, dqb_bhardlimit); \
1442 Q_COPY(out, in, dqb_bsoftlimit); \
1443 Q_COPY(out, in, dqb_curspace); \
1444 Q_COPY(out, in, dqb_ihardlimit); \
1445 Q_COPY(out, in, dqb_isoftlimit); \
1446 Q_COPY(out, in, dqb_curinodes); \
1447 Q_COPY(out, in, dqb_btime); \
1448 Q_COPY(out, in, dqb_itime); \
1449 Q_COPY(out, in, dqb_valid); \
1452 static int fsfilt_ext3_quotactl(struct super_block *sb,
1453 struct obd_quotactl *oqc)
1455 int i, rc = 0, error = 0;
1456 struct quotactl_ops *qcop;
1457 struct if_dqinfo *info;
1458 struct if_dqblk *dqblk;
1464 OBD_ALLOC_PTR(info);
1467 OBD_ALLOC_PTR(dqblk);
1473 DQINFO_COPY(info, &oqc->qc_dqinfo);
1474 DQBLK_COPY(dqblk, &oqc->qc_dqblk);
1477 if (oqc->qc_cmd == Q_QUOTAON || oqc->qc_cmd == Q_QUOTAOFF) {
1478 for (i = 0; i < MAXQUOTAS; i++) {
1479 if (!Q_TYPESET(oqc, i))
1482 if (oqc->qc_cmd == Q_QUOTAON) {
1483 char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1485 LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1487 if (!qcop->quota_on)
1488 GOTO(out, rc = -ENOSYS);
1490 rc = qcop->quota_on(sb, i, QFMT_VFS_V0,
1492 } else if (oqc->qc_cmd == Q_QUOTAOFF) {
1493 if (!qcop->quota_off)
1494 GOTO(out, rc = -ENOSYS);
1495 rc = qcop->quota_off(sb, i);
1503 GOTO(out, rc ?: error);
1506 switch (oqc->qc_cmd) {
1509 if (!qcop->get_info)
1510 GOTO(out, rc = -ENOSYS);
1511 rc = qcop->get_info(sb, oqc->qc_type, info);
1515 if (!qcop->set_dqblk)
1516 GOTO(out, rc = -ENOSYS);
1517 rc = qcop->set_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
1521 if (!qcop->get_dqblk)
1522 GOTO(out, rc = -ENOSYS);
1523 rc = qcop->get_dqblk(sb, oqc->qc_type, oqc->qc_id, dqblk);
1525 dqblk->dqb_valid = QIF_LIMITS | QIF_USAGE;
1528 if (!sb->s_qcop->quota_sync)
1529 GOTO(out, rc = -ENOSYS);
1530 qcop->quota_sync(sb, oqc->qc_type);
1533 CDEBUG(D_WARNING, "invalidating operational quota files\n");
1534 for (i = 0; i < MAXQUOTAS; i++) {
1536 char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1538 LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1540 if (!Q_TYPESET(oqc, i))
1543 fp = filp_open(name[i], O_CREAT | O_TRUNC | O_RDWR, 0644);
1546 CERROR("error invalidating operational quota file"
1547 " %s (rc:%d)\n", name[i], rc);
1555 CERROR("unsupported quotactl command: %d\n", oqc->qc_cmd);
1559 DQINFO_COPY(&oqc->qc_dqinfo, info);
1560 DQBLK_COPY(&oqc->qc_dqblk, dqblk);
1563 OBD_FREE_PTR(dqblk);
1566 CDEBUG(D_QUOTA, "quotactl command %#x, id %u, type %u "
1568 oqc->qc_cmd, oqc->qc_id, oqc->qc_type, rc);
1573 struct hlist_node dqb_hash; /** quotacheck hash */
1574 struct list_head dqb_list; /** in list also */
1575 qid_t dqb_id; /** uid/gid */
1576 short dqb_type; /** USRQUOTA/GRPQUOTA */
1577 qsize_t dqb_bhardlimit; /** block hard limit */
1578 qsize_t dqb_bsoftlimit; /** block soft limit */
1579 qsize_t dqb_curspace; /** current space */
1580 qsize_t dqb_ihardlimit; /** inode hard limit */
1581 qsize_t dqb_isoftlimit; /** inode soft limit */
1582 qsize_t dqb_curinodes; /** current inodes */
1583 __u64 dqb_btime; /** block grace time */
1584 __u64 dqb_itime; /** inode grace time */
1585 __u32 dqb_valid; /** flag for above fields */
1588 static inline unsigned int chkquot_hash(qid_t id, int type)
1589 __attribute__((__const__));
1591 static inline unsigned int chkquot_hash(qid_t id, int type)
1593 return (id * (MAXQUOTAS - type)) % NR_DQHASH;
1596 static inline struct chk_dqblk *
1597 find_chkquot(struct hlist_head *head, qid_t id, int type)
1599 struct hlist_node *node;
1600 struct chk_dqblk *cdqb;
1602 hlist_for_each(node, head) {
1603 cdqb = hlist_entry(node, struct chk_dqblk, dqb_hash);
1604 if (cdqb->dqb_id == id && cdqb->dqb_type == type)
1611 static struct chk_dqblk *alloc_chkquot(qid_t id, int type)
1613 struct chk_dqblk *cdqb;
1615 OBD_ALLOC_PTR(cdqb);
1617 INIT_HLIST_NODE(&cdqb->dqb_hash);
1618 INIT_LIST_HEAD(&cdqb->dqb_list);
1620 cdqb->dqb_type = type;
1626 static struct chk_dqblk *
1627 cqget(struct super_block *sb, struct hlist_head *hash, struct list_head *list,
1628 qid_t id, int type, int first_check)
1630 struct hlist_head *head = hash + chkquot_hash(id, type);
1631 struct if_dqblk dqb;
1632 struct chk_dqblk *cdqb;
1635 cdqb = find_chkquot(head, id, type);
1639 cdqb = alloc_chkquot(id, type);
1644 rc = sb->s_qcop->get_dqblk(sb, type, id, &dqb);
1646 CERROR("get_dqblk of id %u, type %d failed: %d\n",
1649 DQBLK_COPY(cdqb, &dqb);
1650 cdqb->dqb_curspace = 0;
1651 cdqb->dqb_curinodes = 0;
1655 hlist_add_head(&cdqb->dqb_hash, head);
1656 list_add_tail(&cdqb->dqb_list, list);
1661 static inline int quota_onoff(struct super_block *sb, int cmd, int type, int qfmt)
1663 struct obd_quotactl *oqctl;
1666 OBD_ALLOC_PTR(oqctl);
1670 oqctl->qc_cmd = cmd;
1671 oqctl->qc_id = qfmt;
1672 oqctl->qc_type = type;
1673 rc = fsfilt_ext3_quotactl(sb, oqctl);
1675 OBD_FREE_PTR(oqctl);
1679 static inline int read_old_dqinfo(struct super_block *sb, int type,
1680 struct if_dqinfo *dqinfo)
1682 struct obd_quotactl *oqctl;
1686 OBD_ALLOC_PTR(oqctl);
1690 oqctl->qc_cmd = Q_GETINFO;
1691 oqctl->qc_type = type;
1692 rc = fsfilt_ext3_quotactl(sb, oqctl);
1694 ((struct obd_dqinfo *)dqinfo)[type] = oqctl->qc_dqinfo;
1696 OBD_FREE_PTR(oqctl);
1700 static inline struct ext3_group_desc *
1701 get_group_desc(struct super_block *sb, int group)
1703 unsigned long desc_block, desc;
1704 struct ext3_group_desc *gdp;
1706 desc_block = group / EXT3_DESC_PER_BLOCK(sb);
1707 desc = group % EXT3_DESC_PER_BLOCK(sb);
1708 gdp = (struct ext3_group_desc *)
1709 EXT3_SB(sb)->s_group_desc[desc_block]->b_data;
1715 #ifndef HAVE_EXT4_LDISKFS
1716 static inline struct buffer_head *
1717 ext3_read_inode_bitmap(struct super_block *sb, unsigned long group)
1719 struct ext3_group_desc *desc;
1720 struct buffer_head *bh;
1722 desc = get_group_desc(sb, group);
1723 bh = sb_bread(sb, ext3_inode_bitmap(sb, desc));
1728 static inline struct inode *ext3_iget_inuse(struct super_block *sb,
1729 struct buffer_head *bitmap_bh,
1730 int index, unsigned long ino)
1732 struct inode *inode = NULL;
1735 if (ext3_test_bit(index, bitmap_bh->b_data))
1736 #ifdef HAVE_EXT4_LDISKFS
1737 inode = ext4_iget(sb, ino);
1739 /* Newer kernels return an error instead of a NULL pointer */
1742 inode = iget(sb, ino);
1748 struct hlist_head qckt_hash[NR_DQHASH]; /* quotacheck hash */
1749 struct list_head qckt_list; /* quotacheck list */
1750 int qckt_first_check[MAXQUOTAS]; /* 1 if no old quotafile */
1751 struct if_dqinfo qckt_dqinfo[MAXQUOTAS]; /* old dqinfo */
1754 static int add_inode_quota(struct inode *inode, struct qchk_ctxt *qctxt,
1755 struct obd_quotactl *oqc)
1757 struct chk_dqblk *cdqb[MAXQUOTAS] = { NULL, };
1759 qid_t qid[MAXQUOTAS];
1765 qid[USRQUOTA] = inode->i_uid;
1766 qid[GRPQUOTA] = inode->i_gid;
1768 if (S_ISDIR(inode->i_mode) ||
1769 S_ISREG(inode->i_mode) ||
1770 S_ISLNK(inode->i_mode))
1771 size = inode_get_bytes(inode);
1773 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1774 if (!Q_TYPESET(oqc, cnt))
1777 cdqb[cnt] = cqget(inode->i_sb, qctxt->qckt_hash,
1778 &qctxt->qckt_list, qid[cnt], cnt,
1779 qctxt->qckt_first_check[cnt]);
1785 cdqb[cnt]->dqb_curspace += size;
1786 cdqb[cnt]->dqb_curinodes++;
1790 for (i = 0; i < cnt; i++) {
1791 if (!Q_TYPESET(oqc, i))
1794 cdqb[i]->dqb_curspace -= size;
1795 cdqb[i]->dqb_curinodes--;
1802 /* write dqinfo struct in a new quota file */
1803 static int v3_write_dqinfo(struct file *f, int type, struct if_dqinfo *info)
1805 struct v2_disk_dqinfo dqinfo;
1806 __u32 blocks = V2_DQTREEOFF + 1;
1807 loff_t offset = V2_DQINFOOFF;
1810 dqinfo.dqi_bgrace = cpu_to_le32(info->dqi_bgrace);
1811 dqinfo.dqi_igrace = cpu_to_le32(info->dqi_igrace);
1812 dqinfo.dqi_flags = cpu_to_le32(info->dqi_flags & DQF_MASK &
1815 dqinfo.dqi_bgrace = cpu_to_le32(MAX_DQ_TIME);
1816 dqinfo.dqi_igrace = cpu_to_le32(MAX_IQ_TIME);
1817 dqinfo.dqi_flags = 0;
1820 dqinfo.dqi_blocks = cpu_to_le32(blocks);
1821 dqinfo.dqi_free_blk = 0;
1822 dqinfo.dqi_free_entry = 0;
1824 return cfs_user_write(f, (char *)&dqinfo, sizeof(dqinfo), &offset);
1827 static int v3_write_dqheader(struct file *f, int type)
1829 static const __u32 quota_magics[] = V2_INITQMAGICS;
1830 static const __u32 quota_versions[] = V2_INITQVERSIONS_R1;
1831 struct v2_disk_dqheader dqhead;
1834 CLASSERT(ARRAY_SIZE(quota_magics) == ARRAY_SIZE(quota_versions));
1835 LASSERT(0 <= type && type < ARRAY_SIZE(quota_magics));
1837 dqhead.dqh_magic = cpu_to_le32(quota_magics[type]);
1838 dqhead.dqh_version = cpu_to_le32(quota_versions[type]);
1840 return cfs_user_write(f, (char *)&dqhead, sizeof(dqhead), &offset);
1843 static int create_new_quota_files(struct qchk_ctxt *qctxt,
1844 struct obd_quotactl *oqc)
1849 for (i = 0; i < MAXQUOTAS; i++) {
1850 struct if_dqinfo *info = qctxt->qckt_first_check[i]?
1851 NULL : &qctxt->qckt_dqinfo[i];
1853 const char *name[MAXQUOTAS] = LUSTRE_OPQFILES_NAMES_V2;
1855 if (!Q_TYPESET(oqc, i))
1858 LASSERT(oqc->qc_id == LUSTRE_QUOTA_V2);
1860 file = filp_open(name[i], O_RDWR | O_CREAT | O_TRUNC, 0644);
1863 CERROR("can't create %s file: rc = %d\n",
1868 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1869 CERROR("file %s is not regular", name[i]);
1870 filp_close(file, 0);
1871 GOTO(out, rc = -EINVAL);
1874 DQUOT_DROP(file->f_dentry->d_inode);
1876 rc = v3_write_dqheader(file, i);
1878 filp_close(file, 0);
1882 rc = v3_write_dqinfo(file, i, info);
1883 filp_close(file, 0);
1893 static int commit_chkquot(struct super_block *sb, struct qchk_ctxt *qctxt,
1894 struct chk_dqblk *cdqb)
1896 struct obd_quotactl *oqc;
1905 now = cfs_time_current_sec();
1907 if (cdqb->dqb_bsoftlimit &&
1908 toqb(cdqb->dqb_curspace) >= cdqb->dqb_bsoftlimit &&
1911 now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_bgrace;
1913 if (cdqb->dqb_isoftlimit &&
1914 cdqb->dqb_curinodes >= cdqb->dqb_isoftlimit &&
1917 now + qctxt->qckt_dqinfo[cdqb->dqb_type].dqi_igrace;
1919 cdqb->dqb_valid = QIF_ALL;
1921 oqc->qc_cmd = Q_SETQUOTA;
1922 oqc->qc_type = cdqb->dqb_type;
1923 oqc->qc_id = cdqb->dqb_id;
1924 DQBLK_COPY(&oqc->qc_dqblk, cdqb);
1926 rc = fsfilt_ext3_quotactl(sb, oqc);
1931 static int prune_chkquots(struct super_block *sb,
1932 struct qchk_ctxt *qctxt, int error)
1934 struct chk_dqblk *cdqb, *tmp;
1937 list_for_each_entry_safe(cdqb, tmp, &qctxt->qckt_list, dqb_list) {
1939 rc = commit_chkquot(sb, qctxt, cdqb);
1943 hlist_del_init(&cdqb->dqb_hash);
1944 list_del(&cdqb->dqb_list);
1951 static int fsfilt_ext3_quotacheck(struct super_block *sb,
1952 struct obd_quotactl *oqc)
1954 struct ext3_sb_info *sbi = EXT3_SB(sb);
1956 struct qchk_ctxt *qctxt;
1957 struct buffer_head *bitmap_bh = NULL;
1959 struct inode *inode;
1963 /* turn on quota and read dqinfo if existed */
1964 OBD_ALLOC_PTR(qctxt);
1966 oqc->qc_stat = -ENOMEM;
1970 for (i = 0; i < NR_DQHASH; i++)
1971 INIT_HLIST_HEAD(&qctxt->qckt_hash[i]);
1972 INIT_LIST_HEAD(&qctxt->qckt_list);
1974 for (i = 0; i < MAXQUOTAS; i++) {
1975 if (!Q_TYPESET(oqc, i))
1978 rc = quota_onoff(sb, Q_QUOTAON, i, oqc->qc_id);
1979 if (!rc || rc == -EBUSY) {
1980 rc = read_old_dqinfo(sb, i, qctxt->qckt_dqinfo);
1983 } else if (rc == -ENOENT || rc == -EINVAL || rc == -EEXIST) {
1984 qctxt->qckt_first_check[i] = 1;
1990 /* check quota and update in hash */
1991 for (group = 0; group < sbi->s_groups_count; group++) {
1992 ino = group * sbi->s_inodes_per_group + 1;
1993 bitmap_bh = ext3_read_inode_bitmap(sb, group);
1995 CERROR("ext3_read_inode_bitmap group %d failed", group);
1996 GOTO(out, rc = -EIO);
1999 for (i = 0; i < sbi->s_inodes_per_group; i++, ino++) {
2000 if (ino < sbi->s_first_ino)
2003 inode = ext3_iget_inuse(sb, bitmap_bh, i, ino);
2004 rc = add_inode_quota(inode, qctxt, oqc);
2015 /* read old quota limits from old quota file. (only for the user
2016 * has limits but hasn't file) */
2017 #ifdef HAVE_QUOTA_SUPPORT
2018 for (i = 0; i < MAXQUOTAS; i++) {
2019 struct list_head id_list;
2020 struct dquot_id *dqid, *tmp;
2022 if (!Q_TYPESET(oqc, i))
2025 if (qctxt->qckt_first_check[i])
2029 LASSERT(sb_dqopt(sb)->files[i] != NULL);
2030 INIT_LIST_HEAD(&id_list);
2031 #ifndef KERNEL_SUPPORTS_QUOTA_READ
2032 rc = lustre_get_qids(sb_dqopt(sb)->files[i], NULL, i, &id_list);
2034 rc = lustre_get_qids(NULL, sb_dqopt(sb)->files[i], i, &id_list);
2037 CERROR("read old limits failed. (rc:%d)\n", rc);
2039 list_for_each_entry_safe(dqid, tmp, &id_list, di_link) {
2040 list_del_init(&dqid->di_link);
2043 cqget(sb, qctxt->qckt_hash, &qctxt->qckt_list,
2045 qctxt->qckt_first_check[i]);
2050 /* turn off quota cause we are to dump chk_dqblk to files */
2051 quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
2053 rc = create_new_quota_files(qctxt, oqc);
2057 /* we use vfs functions to set dqblk, so turn quota on */
2058 rc = quota_onoff(sb, Q_QUOTAON, oqc->qc_type, oqc->qc_id);
2060 /* dump and free chk_dqblk */
2061 rc = prune_chkquots(sb, qctxt, rc);
2062 OBD_FREE_PTR(qctxt);
2064 /* turn off quota, `lfs quotacheck` will turn on when all
2065 * nodes quotacheck finish. */
2066 quota_onoff(sb, Q_QUOTAOFF, oqc->qc_type, oqc->qc_id);
2070 CERROR("quotacheck failed: rc = %d\n", rc);
2075 static int fsfilt_ext3_quotainfo(struct lustre_quota_info *lqi, int type,
2081 if (lqi->qi_files[type] == NULL) {
2082 CERROR("operate qinfo before it's enabled!\n");
2088 rc = lustre_check_quota_file(lqi, type);
2091 rc = lustre_read_quota_info(lqi, type);
2094 rc = lustre_write_quota_info(lqi, type);
2096 case QFILE_INIT_INFO:
2097 rc = lustre_init_quota_info(lqi, type);
2101 CERROR("quota CONVERT command is not supported\n");
2105 CERROR("Unsupported admin quota file cmd %d\n"
2106 "Are lquota.ko and fsfilt_ldiskfs.ko modules in sync?\n",
2113 static int fsfilt_ext3_qids(struct file *file, struct inode *inode, int type,
2114 struct list_head *list)
2116 return lustre_get_qids(file, inode, type, list);
2119 static int fsfilt_ext3_dquot(struct lustre_dquot *dquot, int cmd)
2124 if (dquot->dq_info->qi_files[dquot->dq_type] == NULL) {
2125 CERROR("operate dquot before it's enabled!\n");
2130 case QFILE_RD_DQUOT:
2131 rc = lustre_read_dquot(dquot);
2133 case QFILE_WR_DQUOT:
2134 if (dquot->dq_dqb.dqb_ihardlimit ||
2135 dquot->dq_dqb.dqb_isoftlimit ||
2136 dquot->dq_dqb.dqb_bhardlimit ||
2137 dquot->dq_dqb.dqb_bsoftlimit)
2138 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2140 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2142 rc = lustre_commit_dquot(dquot);
2147 CERROR("Unsupported admin quota file cmd %d\n", cmd);
2154 static int fsfilt_ext3_get_mblk(struct super_block *sb, int *count,
2155 struct inode *inode, int frags)
2157 #ifdef EXT3_EXT_HAS_NO_TREE
2158 struct ext3_ext_base *base = inode;
2160 struct ext3_extents_tree tree;
2161 struct ext3_ext_base *base = &tree;
2163 ext3_init_tree_desc(base, inode);
2165 /* for an ost_write request, it needs <#fragments> * <tree depth + 1>
2166 * metablocks at maxium b=16542 */
2167 *count = frags * (EXT_DEPTH(base) + 1) * EXT3_BLOCK_SIZE(sb);
2173 lvfs_sbdev_type fsfilt_ext3_journal_sbdev(struct super_block *sb)
2175 return (EXT3_SB(sb)->journal_bdev);
2177 EXPORT_SYMBOL(fsfilt_ext3_journal_sbdev);
2179 static struct fsfilt_operations fsfilt_ext3_ops = {
2181 .fs_owner = THIS_MODULE,
2182 .fs_getlabel = fsfilt_ext3_get_label,
2183 .fs_setlabel = fsfilt_ext3_set_label,
2184 .fs_uuid = fsfilt_ext3_uuid,
2185 .fs_start = fsfilt_ext3_start,
2186 .fs_brw_start = fsfilt_ext3_brw_start,
2187 .fs_extend = fsfilt_ext3_extend,
2188 .fs_commit = fsfilt_ext3_commit,
2189 .fs_commit_async = fsfilt_ext3_commit_async,
2190 .fs_commit_wait = fsfilt_ext3_commit_wait,
2191 .fs_setattr = fsfilt_ext3_setattr,
2192 .fs_iocontrol = fsfilt_ext3_iocontrol,
2193 .fs_set_md = fsfilt_ext3_set_md,
2194 .fs_get_md = fsfilt_ext3_get_md,
2195 .fs_readpage = fsfilt_ext3_readpage,
2196 .fs_add_journal_cb = fsfilt_ext3_add_journal_cb,
2197 .fs_statfs = fsfilt_ext3_statfs,
2198 .fs_sync = fsfilt_ext3_sync,
2199 .fs_map_inode_pages = fsfilt_ext3_map_inode_pages,
2200 .fs_write_record = fsfilt_ext3_write_record,
2201 .fs_read_record = fsfilt_ext3_read_record,
2202 .fs_setup = fsfilt_ext3_setup,
2203 .fs_send_bio = fsfilt_ext3_send_bio,
2204 .fs_get_op_len = fsfilt_ext3_get_op_len,
2205 #ifdef HAVE_DISK_INODE_VERSION
2206 .fs_get_version = fsfilt_ext3_get_version,
2207 .fs_set_version = fsfilt_ext3_set_version,
2209 #ifdef HAVE_QUOTA_SUPPORT
2210 .fs_quotactl = fsfilt_ext3_quotactl,
2211 .fs_quotacheck = fsfilt_ext3_quotacheck,
2212 .fs_quotainfo = fsfilt_ext3_quotainfo,
2213 .fs_qids = fsfilt_ext3_qids,
2214 .fs_dquot = fsfilt_ext3_dquot,
2215 .fs_get_mblk = fsfilt_ext3_get_mblk,
2217 .fs_journal_sbdev = fsfilt_ext3_journal_sbdev,
2220 static int __init fsfilt_ext3_init(void)
2224 fcb_cache = cfs_mem_cache_create("fsfilt_ext3_fcb",
2225 sizeof(struct fsfilt_cb_data), 0, 0);
2227 CERROR("error allocating fsfilt journal callback cache\n");
2228 GOTO(out, rc = -ENOMEM);
2231 rc = fsfilt_register_ops(&fsfilt_ext3_ops);
2234 int err = cfs_mem_cache_destroy(fcb_cache);
2235 LASSERTF(err == 0, "error destroying new cache: rc %d\n", err);
2241 static void __exit fsfilt_ext3_exit(void)
2245 fsfilt_unregister_ops(&fsfilt_ext3_ops);
2246 rc = cfs_mem_cache_destroy(fcb_cache);
2247 LASSERTF(rc == 0, "couldn't destroy fcb_cache slab\n");
2250 module_init(fsfilt_ext3_init);
2251 module_exit(fsfilt_ext3_exit);
2253 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2254 MODULE_DESCRIPTION("Lustre ext3 Filesystem Helper v0.1");
2255 MODULE_LICENSE("GPL");