1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 1998 Peter J. Braam <braam@clusterfs.com>
5 * Copyright (C) 2000 Red Hat, Inc.
6 * Copyright (C) 2000 Los Alamos National Laboratory
7 * Copyright (C) 2000 TurboLinux, Inc.
8 * Copyright (C) 2001 Mountain View Data, Inc.
9 * Copyright (C) 2001 Tacit Networks, Inc. <phil@off.net>
11 * This file is part of InterMezzo, http://www.inter-mezzo.org.
13 * InterMezzo is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * InterMezzo is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with InterMezzo; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/types.h>
28 #include <linux/param.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/vmalloc.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <asm/segment.h>
37 #include <asm/uaccess.h>
38 #include <linux/string.h>
39 #if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
40 #include <linux/jbd.h>
41 #include <linux/ext3_fs.h>
42 #include <linux/ext3_jbd.h>
45 #include "intermezzo_fs.h"
46 #include "intermezzo_psdev.h"
48 #if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
50 #define MAX_PATH_BLOCKS(inode) (PATH_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
51 #define MAX_NAME_BLOCKS(inode) (NAME_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
53 /* space requirements:
55 used to truncate the KML forward to next fset->chunksize boundary
59 write header (< one block)
60 write one path (< MAX_PATHLEN)
61 possibly write another path (< MAX_PATHLEN)
62 write suffix (< one block)
63 presto_update_last_rcvd
67 static loff_t presto_e3_freespace(struct presto_cache *cache,
68 struct super_block *sb)
70 loff_t freebl = le32_to_cpu(EXT3_SB(sb)->s_es->s_free_blocks_count);
71 loff_t avail = freebl -
72 le32_to_cpu(EXT3_SB(sb)->s_es->s_r_blocks_count);
73 return (avail << EXT3_BLOCK_SIZE_BITS(sb));
76 /* start the filesystem journal operations */
77 static void *presto_e3_trans_start(struct presto_file_set *fset,
82 int trunc_blks, one_path_blks, extra_path_blks,
83 extra_name_blks, lml_blks;
84 __u32 avail_kmlblocks;
87 if ( presto_no_journal(fset) ||
88 strcmp(fset->fset_cache->cache_type, "ext3"))
90 CDEBUG(D_JOURNAL, "got cache_type \"%s\"\n",
91 fset->fset_cache->cache_type);
95 avail_kmlblocks = EXT3_SB(inode->i_sb)->s_es->s_free_blocks_count;
97 if ( avail_kmlblocks < 3 ) {
98 return ERR_PTR(-ENOSPC);
101 if ( (op != KML_OPCODE_UNLINK && op != KML_OPCODE_RMDIR)
102 && avail_kmlblocks < 6 ) {
103 return ERR_PTR(-ENOSPC);
106 /* Need journal space for:
107 at least three writes to KML (two one block writes, one a path)
108 possibly a second name (unlink, rmdir)
109 possibly a second path (symlink, rename)
110 a one block write to the last rcvd file
113 trunc_blks = EXT3_DATA_TRANS_BLOCKS + 1;
114 one_path_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 3;
115 lml_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 2;
116 extra_path_blks = EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode);
117 extra_name_blks = EXT3_DATA_TRANS_BLOCKS + MAX_NAME_BLOCKS(inode);
119 /* additional blocks appear for "two pathname" operations
120 and operations involving the LML records
123 case KML_OPCODE_TRUNC:
124 jblocks = one_path_blks + extra_name_blks + trunc_blks
125 + EXT3_DELETE_TRANS_BLOCKS;
127 case KML_OPCODE_KML_TRUNC:
128 /* Hopefully this is a little better, but I'm still mostly
131 jblocks = extra_name_blks + trunc_blks +
132 EXT3_DELETE_TRANS_BLOCKS + 2;
135 jblocks += extra_name_blks + trunc_blks +
136 EXT3_DELETE_TRANS_BLOCKS + 2;
139 jblocks += 2 * extra_path_blks + trunc_blks +
140 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
143 jblocks += 2 * extra_path_blks + trunc_blks +
144 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
146 case KML_OPCODE_RELEASE:
148 jblocks = one_path_blks + lml_blks + 2*trunc_blks;
150 jblocks = one_path_blks;
152 case KML_OPCODE_SETATTR:
153 jblocks = one_path_blks + trunc_blks + 1 ;
155 case KML_OPCODE_CREATE:
156 jblocks = one_path_blks + trunc_blks
157 + EXT3_DATA_TRANS_BLOCKS + 3 + 2;
159 case KML_OPCODE_LINK:
160 jblocks = one_path_blks + trunc_blks
161 + EXT3_DATA_TRANS_BLOCKS + 2;
163 case KML_OPCODE_UNLINK:
164 jblocks = one_path_blks + extra_name_blks + trunc_blks
165 + EXT3_DELETE_TRANS_BLOCKS + 2;
167 case KML_OPCODE_SYMLINK:
168 jblocks = one_path_blks + extra_path_blks + trunc_blks
169 + EXT3_DATA_TRANS_BLOCKS + 5;
171 case KML_OPCODE_MKDIR:
172 jblocks = one_path_blks + trunc_blks
173 + EXT3_DATA_TRANS_BLOCKS + 4 + 2;
175 case KML_OPCODE_RMDIR:
176 jblocks = one_path_blks + extra_name_blks + trunc_blks
177 + EXT3_DELETE_TRANS_BLOCKS + 1;
179 case KML_OPCODE_MKNOD:
180 jblocks = one_path_blks + trunc_blks +
181 EXT3_DATA_TRANS_BLOCKS + 3 + 2;
183 case KML_OPCODE_RENAME:
184 jblocks = one_path_blks + extra_path_blks + trunc_blks +
185 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
187 case KML_OPCODE_WRITE:
188 jblocks = one_path_blks;
189 /* add this when we can wrap our transaction with
190 that of ext3_file_write (ordered writes)
191 + EXT3_DATA_TRANS_BLOCKS;
195 CDEBUG(D_JOURNAL, "invalid operation %d for journal\n", op);
199 CDEBUG(D_JOURNAL, "creating journal handle (%d blocks) for op %d\n",
201 /* journal_start/stop does not do its own locking while updating
202 * the handle/transaction information. Hence we create our own
203 * critical section to protect these calls. -SHP
206 handle = journal_start(EXT3_JOURNAL(inode), jblocks);
211 static void presto_e3_trans_commit(struct presto_file_set *fset, void *handle)
213 if ( presto_no_journal(fset) || !handle)
216 /* See comments before journal_start above. -SHP */
218 journal_stop(handle);
222 static void presto_e3_journal_file_data(struct inode *inode)
224 #ifdef EXT3_JOURNAL_DATA_FL
225 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
227 #warning You must have a facility to enable journaled writes for recovery!
231 /* The logic here is a slightly modified version of ext3/inode.c:block_to_path
233 static int presto_e3_has_all_data(struct inode *inode)
235 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
236 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
237 const long direct_blocks = EXT3_NDIR_BLOCKS,
238 indirect_blocks = ptrs,
239 double_blocks = (1 << (ptrs_bits * 2));
240 long block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
241 inode->i_sb->s_blocksize_bits;
245 if (inode->i_size == 0) {
250 if (block < direct_blocks) {
251 /* No indirect blocks, no problem. */
252 } else if (block < indirect_blocks + direct_blocks) {
254 } else if (block < double_blocks + indirect_blocks + direct_blocks) {
256 } else if (((block - double_blocks - indirect_blocks - direct_blocks)
257 >> (ptrs_bits * 2)) < ptrs) {
261 block *= (inode->i_sb->s_blocksize / 512);
263 CDEBUG(D_CACHE, "Need %ld blocks, have %ld.\n", block, inode->i_blocks);
265 if (block > inode->i_blocks) {
274 struct journal_ops presto_ext3_journal_ops = {
275 .tr_all_data = presto_e3_has_all_data,
276 .tr_avail = presto_e3_freespace,
277 .tr_start = presto_e3_trans_start,
278 .tr_commit = presto_e3_trans_commit,
279 .tr_journal_data = presto_e3_journal_file_data,
280 .tr_ilookup = presto_iget_ilookup
283 #endif /* CONFIG_EXT3_FS */