journal.h revision 8b712cd58adfe6aeeb0be4ecc011dc35620719e7
1ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* -*- mode: c; c-basic-offset: 8; -*-
2ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * vim: noexpandtab sw=8 ts=8 sts=0:
3ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
4ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * journal.h
5ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
6ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Defines journalling api and structures.
7ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
8ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Copyright (C) 2003, 2005 Oracle.  All rights reserved.
9ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
10ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * This program is free software; you can redistribute it and/or
11ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * modify it under the terms of the GNU General Public
12ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * License as published by the Free Software Foundation; either
13ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * version 2 of the License, or (at your option) any later version.
14ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
15ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * This program is distributed in the hope that it will be useful,
16ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * General Public License for more details.
19ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
20ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * You should have received a copy of the GNU General Public
21ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * License along with this program; if not, write to the
22ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Boston, MA 021110-1307, USA.
24ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
25ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
26ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#ifndef OCFS2_JOURNAL_H
27ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_JOURNAL_H
28ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
29ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#include <linux/fs.h>
3053ef99cad9878f02f27bb30bc304fc42af8bdd6eMark Fasheh#include <linux/jbd2.h>
31ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
32ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehenum ocfs2_journal_state {
33ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	OCFS2_JOURNAL_FREE = 0,
34ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	OCFS2_JOURNAL_LOADED,
35ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	OCFS2_JOURNAL_IN_SHUTDOWN,
36ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh};
37ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
38ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstruct ocfs2_super;
39ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstruct ocfs2_dinode;
40ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
4196a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran/*
4296a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran * The recovery_list is a simple linked list of node numbers to recover.
4396a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran * It is protected by the recovery_lock.
4496a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran */
4596a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran
4696a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushranstruct ocfs2_recovery_map {
4796a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran	unsigned int rm_used;
4896a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran	unsigned int *rm_entries;
4996a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran};
5096a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran
5196a6c64b5354b804b3ccfd1b31306565a01ebcb1Sunil Mushran
52ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstruct ocfs2_journal {
53ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	enum ocfs2_journal_state   j_state;    /* Journals current state   */
54ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
55ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	journal_t                 *j_journal; /* The kernels journal type */
56ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct inode              *j_inode;   /* Kernel inode pointing to
57ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * this journal             */
58ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct ocfs2_super        *j_osb;     /* pointer to the super
59ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * block for the node
60ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * we're currently
61ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * running on -- not
62ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * necessarily the super
63ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * block from the node
64ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * which we usually run
65ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * from (recovery,
66ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       * etc)                     */
67ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct buffer_head        *j_bh;      /* Journal disk inode block */
68ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	atomic_t                  j_num_trans; /* Number of transactions
69ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					        * currently in the system. */
70ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	unsigned long             j_trans_id;
71ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct rw_semaphore       j_trans_barrier;
72ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	wait_queue_head_t         j_checkpointed;
73ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
74ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spinlock_t                j_lock;
75ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct list_head          j_la_cleanups;
76ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct work_struct        j_recovery_work;
77ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh};
78ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
79ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehextern spinlock_t trans_inc_lock;
80ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
81ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* wrap j_trans_id so we never have it equal to zero. */
82ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline unsigned long ocfs2_inc_trans_id(struct ocfs2_journal *j)
83ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
84ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	unsigned long old_id;
85ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_lock(&trans_inc_lock);
86ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	old_id = j->j_trans_id++;
87ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	if (unlikely(!j->j_trans_id))
88ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		j->j_trans_id = 1;
89ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_unlock(&trans_inc_lock);
90ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	return old_id;
91ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
92ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
93ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline void ocfs2_set_inode_lock_trans(struct ocfs2_journal *journal,
94ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					      struct inode *inode)
95ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
96ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_lock(&trans_inc_lock);
97ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	OCFS2_I(inode)->ip_last_trans = journal->j_trans_id;
98ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_unlock(&trans_inc_lock);
99ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
100ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
101ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* Used to figure out whether it's safe to drop a metadata lock on an
102ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * inode. Returns true if all the inodes changes have been
103ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * checkpointed to disk. You should be holding the spinlock on the
104ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * metadata lock while calling this to be sure that nobody can take
105ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * the lock and put it on another transaction. */
106ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_inode_fully_checkpointed(struct inode *inode)
107ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
108ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int ret;
109ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct ocfs2_journal *journal = OCFS2_SB(inode->i_sb)->journal;
110ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
111ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_lock(&trans_inc_lock);
112ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	ret = time_after(journal->j_trans_id, OCFS2_I(inode)->ip_last_trans);
113ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_unlock(&trans_inc_lock);
114ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	return ret;
115ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
116ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
117ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* convenience function to check if an inode is still new (has never
118ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * hit disk) Will do you a favor and set created_trans = 0 when you've
119ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * been checkpointed.  returns '1' if the inode is still new. */
120ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_inode_is_new(struct inode *inode)
121ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
122ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int ret;
123ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
124ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* System files are never "new" as they're written out by
125ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * mkfs. This helps us early during mount, before we have the
126ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * journal open and j_trans_id could be junk. */
127ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
128ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		return 0;
129ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_lock(&trans_inc_lock);
130ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	ret = !(time_after(OCFS2_SB(inode->i_sb)->journal->j_trans_id,
131ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh			   OCFS2_I(inode)->ip_created_trans));
132ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	if (!ret)
133ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		OCFS2_I(inode)->ip_created_trans = 0;
134ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_unlock(&trans_inc_lock);
135ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	return ret;
136ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
137ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
138ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline void ocfs2_inode_set_new(struct ocfs2_super *osb,
139ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh				       struct inode *inode)
140ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
141ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_lock(&trans_inc_lock);
142ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	OCFS2_I(inode)->ip_created_trans = osb->journal->j_trans_id;
143ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	spin_unlock(&trans_inc_lock);
144ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
145ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
146ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* Exported only for the journal struct init code in super.c. Do not call. */
147df152c241df9e9d2b9a65d37bd02961abe7f591aSunil Mushranvoid ocfs2_orphan_scan_init(struct ocfs2_super *osb);
1488b712cd58adfe6aeeb0be4ecc011dc35620719e7Jeff Mahoneyvoid ocfs2_orphan_scan_start(struct ocfs2_super *osb);
14983273932fbefb6ceef9c0b82ac4d23900728f4d9Srinivas Eedavoid ocfs2_orphan_scan_stop(struct ocfs2_super *osb);
15083273932fbefb6ceef9c0b82ac4d23900728f4d9Srinivas Eedavoid ocfs2_orphan_scan_exit(struct ocfs2_super *osb);
15183273932fbefb6ceef9c0b82ac4d23900728f4d9Srinivas Eeda
152c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsvoid ocfs2_complete_recovery(struct work_struct *work);
153553abd046af609191a91af7289d87d477adc659fJoel Beckervoid ocfs2_wait_for_recovery(struct ocfs2_super *osb);
154553abd046af609191a91af7289d87d477adc659fJoel Becker
155553abd046af609191a91af7289d87d477adc659fJoel Beckerint ocfs2_recovery_init(struct ocfs2_super *osb);
156553abd046af609191a91af7289d87d477adc659fJoel Beckervoid ocfs2_recovery_exit(struct ocfs2_super *osb);
157ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
1589140db04ef185f934acf2b1b15b3dd5e6a6bfc22Srinivas Eedaint ocfs2_compute_replay_slots(struct ocfs2_super *osb);
159ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/*
160ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Journal Control:
161ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Initialize, Load, Shutdown, Wipe a journal.
162ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
163ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_journal_init     - Initialize journal structures in the OSB.
164ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_journal_load     - Load the given journal off disk. Replay it if
165ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          there's transactions still in there.
166ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_journal_shutdown - Shutdown a journal, this will flush all
167ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          uncommitted, uncheckpointed transactions.
168ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_journal_wipe     - Wipe transactions from a journal. Optionally
169ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          zero out each block.
170ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_recovery_thread  - Perform recovery on a node. osb is our own osb.
171ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_mark_dead_nodes - Start recovery on nodes we won't get a heartbeat
172ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          event on.
173ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_start_checkpoint - Kick the commit thread to do a checkpoint.
174ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
175ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehvoid   ocfs2_set_journal_params(struct ocfs2_super *osb);
176ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehint    ocfs2_journal_init(struct ocfs2_journal *journal,
177ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh			  int *dirty);
178ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehvoid   ocfs2_journal_shutdown(struct ocfs2_super *osb);
179ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehint    ocfs2_journal_wipe(struct ocfs2_journal *journal,
180ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh			  int full);
181539d8264093560b917ee3afe4c7f74e5da09d6a5Sunil Mushranint    ocfs2_journal_load(struct ocfs2_journal *journal, int local,
182539d8264093560b917ee3afe4c7f74e5da09d6a5Sunil Mushran			  int replayed);
183ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehint    ocfs2_check_journals_nolocks(struct ocfs2_super *osb);
184ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehvoid   ocfs2_recovery_thread(struct ocfs2_super *osb,
185ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh			     int node_num);
186ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehint    ocfs2_mark_dead_nodes(struct ocfs2_super *osb);
187ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehvoid   ocfs2_complete_mount_recovery(struct ocfs2_super *osb);
1882205363dce7447b8e85f1ead14387664c1a98753Jan Karavoid ocfs2_complete_quota_recovery(struct ocfs2_super *osb);
189ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
190ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline void ocfs2_start_checkpoint(struct ocfs2_super *osb)
191ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
192ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	atomic_set(&osb->needs_checkpoint, 1);
193ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	wake_up(&osb->checkpoint_event);
194ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
195ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
196ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline void ocfs2_checkpoint_inode(struct inode *inode)
197ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
198ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
199ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
200c271c5c22b0a7ca45fda15f1f4d258bca36a5b94Sunil Mushran	if (ocfs2_mount_local(osb))
201c271c5c22b0a7ca45fda15f1f4d258bca36a5b94Sunil Mushran		return;
202c271c5c22b0a7ca45fda15f1f4d258bca36a5b94Sunil Mushran
203ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	if (!ocfs2_inode_fully_checkpointed(inode)) {
204ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		/* WARNING: This only kicks off a single
205ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		 * checkpoint. If someone races you and adds more
206ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		 * metadata to the journal, you won't know, and will
207ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		 * wind up waiting *alot* longer than necessary. Right
208ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		 * now we only use this in clear_inode so that's
209ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		 * OK. */
210ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		ocfs2_start_checkpoint(osb);
211ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
212ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		wait_event(osb->journal->j_checkpointed,
213ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh			   ocfs2_inode_fully_checkpointed(inode));
214ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	}
215ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
216ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
217ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/*
218ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Transaction Handling:
219ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Manage the lifetime of a transaction handle.
220ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
221ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_start_trans      - Begin a transaction. Give it an upper estimate of
222ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          the number of blocks that will be changed during
223ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          this handle.
2241fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh *  ocfs2_commit_trans - Complete a handle. It might return -EIO if
2251fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh *                       the journal was aborted. The majority of paths don't
2261fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh *                       check the return value as an error there comes too
2271fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh *                       late to do anything (and will be picked up in a
2281fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh *                       later transaction).
229ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_extend_trans     - Extend a handle by nblocks credits. This may
230ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          commit the handle to disk in the process, but will
231ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          not release any locks taken during the transaction.
23250655ae9e91d272d48997bada59efe166aa5e343Joel Becker *  ocfs2_journal_access* - Notify the handle that we want to journal this
233ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          buffer. Will have to call ocfs2_journal_dirty once
234ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *                          we've actually dirtied it. Type is one of . or .
23550655ae9e91d272d48997bada59efe166aa5e343Joel Becker *                          Always call the specific flavor of
23650655ae9e91d272d48997bada59efe166aa5e343Joel Becker *                          ocfs2_journal_access_*() unless you intend to
23750655ae9e91d272d48997bada59efe166aa5e343Joel Becker *                          manage the checksum by hand.
238ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  ocfs2_journal_dirty    - Mark a journalled buffer as having dirty data.
2392b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker *  ocfs2_jbd2_file_inode  - Mark an inode so that its data goes out before
2402b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker *                           the current handle commits.
241ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
242ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
243ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* You must always start_trans with a number of buffs > 0, but it's
244ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * perfectly legal to go through an entire transaction without having
245ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * dirtied any buffers. */
2461fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fashehhandle_t		    *ocfs2_start_trans(struct ocfs2_super *osb,
247ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					       int max_buffs);
2481fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fashehint			     ocfs2_commit_trans(struct ocfs2_super *osb,
2491fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fasheh						handle_t *handle);
2501fc581467e52546195c7ee8233a34d63c1cc1322Mark Fashehint			     ocfs2_extend_trans(handle_t *handle, int nblocks);
251ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
252ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/*
253ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Create access is for when we get a newly created buffer and we're
254ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * not gonna read it off disk, but rather fill it ourselves.  Right
255ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * now, we don't do anything special with this (it turns into a write
256ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * request), but this is a good placeholder in case we do...
257ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
258ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * Write access is for when we read a block off disk and are going to
259ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * modify it. This way the journalling layer knows it may need to make
260ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * a copy of that block (if it's part of another, uncommitted
261ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * transaction) before we do so.
262ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
263ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_JOURNAL_ACCESS_CREATE 0
264ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_JOURNAL_ACCESS_WRITE  1
265ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_JOURNAL_ACCESS_UNDO   2
266ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
26713723d00e374c2a6d6ccb5af6de965e89c3e1b01Joel Becker
26850655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* ocfs2_inode */
26950655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
27013723d00e374c2a6d6ccb5af6de965e89c3e1b01Joel Becker			    struct buffer_head *bh, int type);
27150655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* ocfs2_extent_block */
27250655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
27350655ae9e91d272d48997bada59efe166aa5e343Joel Becker			    struct buffer_head *bh, int type);
27450655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* ocfs2_group_desc */
27550655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
27650655ae9e91d272d48997bada59efe166aa5e343Joel Becker			    struct buffer_head *bh, int type);
27750655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* ocfs2_xattr_block */
27850655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
27950655ae9e91d272d48997bada59efe166aa5e343Joel Becker			    struct buffer_head *bh, int type);
28050655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* quota blocks */
28150655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
28250655ae9e91d272d48997bada59efe166aa5e343Joel Becker			    struct buffer_head *bh, int type);
28350655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* dirblock */
28450655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
28550655ae9e91d272d48997bada59efe166aa5e343Joel Becker			    struct buffer_head *bh, int type);
2869b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh/* ocfs2_dx_root_block */
2879b7895efac906d66d19856194e1ba61f37e231a4Mark Fashehint ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
2889b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh			    struct buffer_head *bh, int type);
2899b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh/* ocfs2_dx_leaf */
2909b7895efac906d66d19856194e1ba61f37e231a4Mark Fashehint ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
2919b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh			    struct buffer_head *bh, int type);
29250655ae9e91d272d48997bada59efe166aa5e343Joel Becker/* Anything that has no ecc */
29350655ae9e91d272d48997bada59efe166aa5e343Joel Beckerint ocfs2_journal_access(handle_t *handle, struct inode *inode,
29450655ae9e91d272d48997bada59efe166aa5e343Joel Becker			 struct buffer_head *bh, int type);
29550655ae9e91d272d48997bada59efe166aa5e343Joel Becker
296ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/*
297ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * A word about the journal_access/journal_dirty "dance". It is
298ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * entirely legal to journal_access a buffer more than once (as long
299ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * as the access type is the same -- I'm not sure what will happen if
300ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * access type is different but this should never happen anyway) It is
301ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * also legal to journal_dirty a buffer more than once. In fact, you
302ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * can even journal_access a buffer after you've done a
303ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * journal_access/journal_dirty pair. The only thing you cannot do
304ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * however, is journal_dirty a buffer which you haven't yet passed to
305ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * journal_access at least once.
306ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
307ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * That said, 99% of the time this doesn't matter and this is what the
308ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * path looks like:
309ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
310ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *	<read a bh>
311ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *	ocfs2_journal_access(handle, bh,	OCFS2_JOURNAL_ACCESS_WRITE);
312ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *	<modify the bh>
313ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * 	ocfs2_journal_dirty(handle, bh);
314ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
3151fabe1481fac9e01bf8bffa60a2307ef379aa5deMark Fashehint                  ocfs2_journal_dirty(handle_t *handle,
316ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					 struct buffer_head *bh);
317ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
318ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/*
319ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Credit Macros:
320ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  Convenience macros to calculate number of credits needed.
321ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *
322ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  For convenience sake, I have a set of macros here which calculate
323ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  the *maximum* number of sectors which will be changed for various
324ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh *  metadata updates.
325ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh */
326ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
327ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* simple file updates like chmod, etc. */
328ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_INODE_UPDATE_CREDITS 1
329ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
330cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang/* extended attribute block update */
331cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang#define OCFS2_XATTR_BLOCK_UPDATE_CREDITS 1
332cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang
333a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* global quotafile inode update, data block */
334a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara#define OCFS2_QINFO_WRITE_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 1)
335a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
336a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/*
337a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara * The two writes below can accidentally see global info dirty due
338a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara * to set_info() quotactl so make them prepared for the writes.
339a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara */
340a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* quota data block, global info */
341a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* Write to local quota file */
342a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara#define OCFS2_QWRITE_CREDITS (OCFS2_QINFO_WRITE_CREDITS + 1)
343a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
344a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* global quota data block, local quota data block, global quota inode,
345a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara * global quota info */
346a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara#define OCFS2_QSYNC_CREDITS (OCFS2_INODE_UPDATE_CREDITS + 3)
347a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
348a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_quota_trans_credits(struct super_block *sb)
349a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
350a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	int credits = 0;
351a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
352a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_USRQUOTA))
353a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara		credits += OCFS2_QWRITE_CREDITS;
354a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	if (OCFS2_HAS_RO_COMPAT_FEATURE(sb, OCFS2_FEATURE_RO_COMPAT_GRPQUOTA))
355a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara		credits += OCFS2_QWRITE_CREDITS;
356a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	return credits;
357a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
358a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
359a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* Number of credits needed for removing quota structure from file */
360a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karaint ocfs2_calc_qdel_credits(struct super_block *sb, int type);
361a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* Number of credits needed for initialization of new quota structure */
362a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karaint ocfs2_calc_qinit_credits(struct super_block *sb, int type);
363a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
364d659072f736837e56b6433d58e5315ad1d4d5ccfTao Ma/* group extend. inode update and last group update. */
365d659072f736837e56b6433d58e5315ad1d4d5ccfTao Ma#define OCFS2_GROUP_EXTEND_CREDITS	(OCFS2_INODE_UPDATE_CREDITS + 1)
366d659072f736837e56b6433d58e5315ad1d4d5ccfTao Ma
3677909f2bf835376a20d6dbf853eb459a27566eba2Tao Ma/* group add. inode update and the new group update. */
3687909f2bf835376a20d6dbf853eb459a27566eba2Tao Ma#define OCFS2_GROUP_ADD_CREDITS	(OCFS2_INODE_UPDATE_CREDITS + 1)
3697909f2bf835376a20d6dbf853eb459a27566eba2Tao Ma
370ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* get one bit out of a suballocator: dinode + group descriptor +
371ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * prev. group desc. if we relink. */
372ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_SUBALLOC_ALLOC (3)
373ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
374a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_inline_to_extents_credits(struct super_block *sb)
375a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
376a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	return OCFS2_SUBALLOC_ALLOC + OCFS2_INODE_UPDATE_CREDITS +
377a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	       ocfs2_quota_trans_credits(sb);
378a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
3791afc32b952335f665327a1a9001ba1b44bb76fd9Mark Fasheh
380ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* dinode + group descriptor update. We don't relink on free yet. */
381ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_SUBALLOC_FREE  (2)
382ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
383ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_TRUNCATE_LOG_UPDATE OCFS2_INODE_UPDATE_CREDITS
384ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC (OCFS2_SUBALLOC_FREE 		      \
385ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					 + OCFS2_TRUNCATE_LOG_UPDATE)
386ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
387a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_remove_extent_credits(struct super_block *sb)
388a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
389a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	return OCFS2_TRUNCATE_LOG_UPDATE + OCFS2_INODE_UPDATE_CREDITS +
390a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	       ocfs2_quota_trans_credits(sb);
391a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
392063c4561f52a74de686fe0ff2f96f4f54c9fecd2Mark Fasheh
393ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* data block for new dir/symlink, 2 for bitmap updates (bitmap fe +
394e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh * bitmap block for the new bit) dx_root update for free list */
395e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh#define OCFS2_DIR_LINK_ADDITIONAL_CREDITS (1 + 2 + 1)
396ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
3979b7895efac906d66d19856194e1ba61f37e231a4Mark Fashehstatic inline int ocfs2_add_dir_index_credits(struct super_block *sb)
3989b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh{
3999b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	/* 1 block for index, 2 allocs (data, metadata), 1 clusters
4009b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	 * worth of blocks for initial extent. */
4019b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	return 1 + 2 * OCFS2_SUBALLOC_ALLOC +
4029b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh		ocfs2_clusters_to_blocks(sb, 1);
4039b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh}
4049b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh
4059b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh/* parent fe, parent block, new file entry, index leaf, inode alloc fe, inode
4069b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh * alloc group descriptor + mkdir/symlink blocks + dir blocks + xattr
4079b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh * blocks + quota update */
4089b7895efac906d66d19856194e1ba61f37e231a4Mark Fashehstatic inline int ocfs2_mknod_credits(struct super_block *sb, int is_dir,
4099b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh				      int xattr_credits)
410a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
4119b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	int dir_credits = OCFS2_DIR_LINK_ADDITIONAL_CREDITS;
4129b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh
4139b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	if (is_dir)
4149b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh		dir_credits += ocfs2_add_dir_index_credits(sb);
4159b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh
4169b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	return 4 + OCFS2_SUBALLOC_ALLOC + dir_credits + xattr_credits +
417a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	       ocfs2_quota_trans_credits(sb);
418a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
419ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
420ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* local alloc metadata change + main bitmap updates */
421ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_WINDOW_MOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS                 \
422ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh				  + OCFS2_SUBALLOC_ALLOC + OCFS2_SUBALLOC_FREE)
423ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
424ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* used when we don't need an allocation change for a dir extend. One
425ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * for the dinode, one for the new block. */
426ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#define OCFS2_SIMPLE_DIR_EXTEND_CREDITS (2)
427ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
428a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara/* file update (nlink, etc) + directory mtime/ctime + dir entry block + quota
429e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh * update on dir + index leaf + dx root update for free list */
430a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_link_credits(struct super_block *sb)
431a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
432e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh	return 2*OCFS2_INODE_UPDATE_CREDITS + 3 +
433a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	       ocfs2_quota_trans_credits(sb);
434a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
435ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
436ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* inode + dir inode (if we unlink a dir), + dir entry block + orphan
437e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh * dir inode link + dir inode index leaf + dir index root */
438a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_unlink_credits(struct super_block *sb)
439a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
440a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	/* The quota update from ocfs2_link_credits is unused here... */
441e7c17e43090afe558c40bfb66637744c27bd2aebMark Fasheh	return 2 * OCFS2_INODE_UPDATE_CREDITS + 3 + ocfs2_link_credits(sb);
442a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
443ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
444ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* dinode + orphan dir dinode + inode alloc dinode + orphan dir entry +
445dfa13f39b798fee68250abe1aed851395c8b51b5Joel Becker * inode alloc group descriptor + orphan dir index root +
446dfa13f39b798fee68250abe1aed851395c8b51b5Joel Becker * orphan dir index leaf */
447dfa13f39b798fee68250abe1aed851395c8b51b5Joel Becker#define OCFS2_DELETE_INODE_CREDITS (3 * OCFS2_INODE_UPDATE_CREDITS + 4)
448ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
449ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh/* dinode update, old dir dinode update, new dir dinode update, old
450ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh * dir dir entry, new dir dir entry, dir entry update for renaming
4519b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh * directory + target unlink + 3 x dir index leaves */
452a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Karastatic inline int ocfs2_rename_credits(struct super_block *sb)
453a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara{
4549b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	return 3 * OCFS2_INODE_UPDATE_CREDITS + 6 + ocfs2_unlink_credits(sb);
455a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara}
456ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
457cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang/* global bitmap dinode, group desc., relinked group,
458cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang * suballocator dinode, group desc., relinked group,
459cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang * dinode, xattr block */
460cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang#define OCFS2_XATTR_BLOCK_CREATE_CREDITS (OCFS2_SUBALLOC_ALLOC * 2 + \
461cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang					  + OCFS2_INODE_UPDATE_CREDITS \
462cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang					  + OCFS2_XATTR_BLOCK_UPDATE_CREDITS)
463cf1d6c763fbcb115263114302485ad17e7933d87Tiger Yang
4649b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh/* inode update, removal of dx root block from allocator */
4659b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh#define OCFS2_DX_ROOT_REMOVE_CREDITS (OCFS2_INODE_UPDATE_CREDITS +	\
4669b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh				      OCFS2_SUBALLOC_FREE)
4679b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh
4684ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fashehstatic inline int ocfs2_calc_dxi_expand_credits(struct super_block *sb)
4694ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh{
4704ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh	int credits = 1 + OCFS2_SUBALLOC_ALLOC;
4714ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh
4724ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh	credits += ocfs2_clusters_to_blocks(sb, 1);
4734ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh	credits += ocfs2_quota_trans_credits(sb);
4744ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh
4754ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh	return credits;
4764ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh}
4774ed8a6bb083bfcc21f1ed66a474b03c0386e4b34Mark Fasheh
478811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma/*
479811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma * Please note that the caller must make sure that root_el is the root
480811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma * of extent tree. So for an inode, it should be &fe->id2.i_list. Otherwise
481811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma * the result may be wrong.
482811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma */
483ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_calc_extend_credits(struct super_block *sb,
484811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma					    struct ocfs2_extent_list *root_el,
485ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh					    u32 bits_wanted)
486ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
487811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma	int bitmap_blocks, sysfile_bitmap_blocks, extent_blocks;
488ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
489ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* bitmap dinode, group desc. + relinked group. */
490ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	bitmap_blocks = OCFS2_SUBALLOC_ALLOC;
491ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
492ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* we might need to shift tree depth so lets assume an
493ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * absolute worst case of complete fragmentation.  Even with
494ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * that, we only need one update for the dinode, and then
495ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * however many metadata chunks needed * a remaining suballoc
496ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * alloc. */
497ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	sysfile_bitmap_blocks = 1 +
498811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma		(OCFS2_SUBALLOC_ALLOC - 1) * ocfs2_extend_meta_needed(root_el);
499ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
500ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* this does not include *new* metadata blocks, which are
501811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma	 * accounted for in sysfile_bitmap_blocks. root_el +
502ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * prev. last_eb_blk + blocks along edge of tree.
503ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * calc_symlink_credits passes because we just need 1
504ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * credit for the dinode there. */
505811f933df1e55615fd0bb4818f31e3868a8e6e23Tao Ma	extent_blocks = 1 + 1 + le16_to_cpu(root_el->l_tree_depth);
506ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
507a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	return bitmap_blocks + sysfile_bitmap_blocks + extent_blocks +
508a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	       ocfs2_quota_trans_credits(sb);
509ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
510ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
511ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_calc_symlink_credits(struct super_block *sb)
512ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
5139b7895efac906d66d19856194e1ba61f37e231a4Mark Fasheh	int blocks = ocfs2_mknod_credits(sb, 0, 0);
514ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
515ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* links can be longer than one block so we may update many
516ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	 * within our single allocated extent. */
517ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	blocks += ocfs2_clusters_to_blocks(sb, 1);
518ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
519a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	return blocks + ocfs2_quota_trans_credits(sb);
520ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
521ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
522ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_calc_group_alloc_credits(struct super_block *sb,
523ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh						 unsigned int cpg)
524ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
525ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int blocks;
526ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int bitmap_blocks = OCFS2_SUBALLOC_ALLOC + 1;
527ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* parent inode update + new block group header + bitmap inode update
528ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	   + bitmap blocks affected */
529ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	blocks = 1 + 1 + 1 + bitmap_blocks;
530ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	return blocks;
531ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
532ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
533ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fashehstatic inline int ocfs2_calc_tree_trunc_credits(struct super_block *sb,
534ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh						unsigned int clusters_to_del,
535ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh						struct ocfs2_dinode *fe,
536ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh						struct ocfs2_extent_list *last_el)
537ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh{
538ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh 	/* for dinode + all headers in this pass + update to next leaf */
539ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	u16 next_free = le16_to_cpu(last_el->l_next_free_rec);
540ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	u16 tree_depth = le16_to_cpu(fe->id2.i_list.l_tree_depth);
541ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int credits = 1 + tree_depth + 1;
542ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	int i;
543ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
544ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	i = next_free - 1;
545ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	BUG_ON(i < 0);
546ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
547ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* We may be deleting metadata blocks, so metadata alloc dinode +
548ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	   one desc. block for each possible delete. */
549ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	if (tree_depth && next_free == 1 &&
550e48edee2d8eab812f31f0ff62c6ba635ca2e1e21Mark Fasheh	    ocfs2_rec_clusters(last_el, &last_el->l_recs[i]) == clusters_to_del)
551ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh		credits += 1 + tree_depth;
552ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
553ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	/* update to the truncate log. */
554ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	credits += OCFS2_TRUNCATE_LOG_UPDATE;
555ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
556a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara	credits += ocfs2_quota_trans_credits(sb);
557a90714c150e3ce677c57a9dac3ab1ec342c75a95Jan Kara
558ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh	return credits;
559ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh}
560ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh
5612b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Beckerstatic inline int ocfs2_jbd2_file_inode(handle_t *handle, struct inode *inode)
5622b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker{
5632b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker	return jbd2_journal_file_inode(handle, &OCFS2_I(inode)->ip_jinode);
5642b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker}
5652b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker
5662b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Beckerstatic inline int ocfs2_begin_ordered_truncate(struct inode *inode,
5672b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker					       loff_t new_size)
5682b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker{
5697f5aa215088b817add9c71914b83650bdd49f8a9Jan Kara	return jbd2_journal_begin_ordered_truncate(
5707f5aa215088b817add9c71914b83650bdd49f8a9Jan Kara				OCFS2_SB(inode->i_sb)->journal->j_journal,
5717f5aa215088b817add9c71914b83650bdd49f8a9Jan Kara				&OCFS2_I(inode)->ip_jinode,
5727f5aa215088b817add9c71914b83650bdd49f8a9Jan Kara				new_size);
5732b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker}
5742b4e30fbde425828b17f0e9c8f8e3fd3ecb2bc75Joel Becker
575ccd979bdbce9fba8412beb3f1de68a9d0171b12cMark Fasheh#endif /* OCFS2_JOURNAL_H */
576