1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/types.h>
28#include <linux/param.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/fs.h>
32#include <linux/slab.h>
33#include <linux/vmalloc.h>
34#include <linux/stat.h>
35#include <linux/errno.h>
36#include <linux/locks.h>
37#include <asm/segment.h>
38#include <asm/uaccess.h>
39#include <linux/string.h>
40#include <linux/smp_lock.h>
41#if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
42#include <linux/jbd.h>
43#include <linux/ext3_fs.h>
44#include <linux/ext3_jbd.h>
45#endif
46
47#include <linux/intermezzo_fs.h>
48#include <linux/intermezzo_psdev.h>
49
50#if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
51
52#define MAX_PATH_BLOCKS(inode) (PATH_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
53#define MAX_NAME_BLOCKS(inode) (NAME_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69static loff_t presto_e3_freespace(struct presto_cache *cache,
70 struct super_block *sb)
71{
72 loff_t freebl = le32_to_cpu(sb->u.ext3_sb.s_es->s_free_blocks_count);
73 loff_t avail = freebl -
74 le32_to_cpu(sb->u.ext3_sb.s_es->s_r_blocks_count);
75 return (avail << EXT3_BLOCK_SIZE_BITS(sb));
76}
77
78
79static void *presto_e3_trans_start(struct presto_file_set *fset,
80 struct inode *inode,
81 int op)
82{
83 int jblocks;
84 int trunc_blks, one_path_blks, extra_path_blks,
85 extra_name_blks, lml_blks;
86 __u32 avail_kmlblocks;
87 handle_t *handle;
88
89 if ( presto_no_journal(fset) ||
90 strcmp(fset->fset_cache->cache_type, "ext3"))
91 {
92 CDEBUG(D_JOURNAL, "got cache_type \"%s\"\n",
93 fset->fset_cache->cache_type);
94 return NULL;
95 }
96
97 avail_kmlblocks = inode->i_sb->u.ext3_sb.s_es->s_free_blocks_count;
98
99 if ( avail_kmlblocks < 3 ) {
100 return ERR_PTR(-ENOSPC);
101 }
102
103 if ( (op != KML_OPCODE_UNLINK && op != KML_OPCODE_RMDIR)
104 && avail_kmlblocks < 6 ) {
105 return ERR_PTR(-ENOSPC);
106 }
107
108
109
110
111
112
113
114
115 trunc_blks = EXT3_DATA_TRANS_BLOCKS + 1;
116 one_path_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 3;
117 lml_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 2;
118 extra_path_blks = EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode);
119 extra_name_blks = EXT3_DATA_TRANS_BLOCKS + MAX_NAME_BLOCKS(inode);
120
121
122
123
124 switch (op) {
125 case KML_OPCODE_TRUNC:
126 jblocks = one_path_blks + extra_name_blks + trunc_blks
127 + EXT3_DELETE_TRANS_BLOCKS;
128 break;
129 case KML_OPCODE_KML_TRUNC:
130
131
132
133 jblocks = extra_name_blks + trunc_blks +
134 EXT3_DELETE_TRANS_BLOCKS + 2;
135
136
137 jblocks += extra_name_blks + trunc_blks +
138 EXT3_DELETE_TRANS_BLOCKS + 2;
139
140
141 jblocks += 2 * extra_path_blks + trunc_blks +
142 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
143
144
145 jblocks += 2 * extra_path_blks + trunc_blks +
146 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
147 break;
148 case KML_OPCODE_RELEASE:
149
150
151
152 jblocks = one_path_blks;
153 break;
154 case KML_OPCODE_SETATTR:
155 jblocks = one_path_blks + trunc_blks + 1 ;
156 break;
157 case KML_OPCODE_CREATE:
158 jblocks = one_path_blks + trunc_blks
159 + EXT3_DATA_TRANS_BLOCKS + 3 + 2;
160 break;
161 case KML_OPCODE_LINK:
162 jblocks = one_path_blks + trunc_blks
163 + EXT3_DATA_TRANS_BLOCKS + 2;
164 break;
165 case KML_OPCODE_UNLINK:
166 jblocks = one_path_blks + extra_name_blks + trunc_blks
167 + EXT3_DELETE_TRANS_BLOCKS + 2;
168 break;
169 case KML_OPCODE_SYMLINK:
170 jblocks = one_path_blks + extra_path_blks + trunc_blks
171 + EXT3_DATA_TRANS_BLOCKS + 5;
172 break;
173 case KML_OPCODE_MKDIR:
174 jblocks = one_path_blks + trunc_blks
175 + EXT3_DATA_TRANS_BLOCKS + 4 + 2;
176 break;
177 case KML_OPCODE_RMDIR:
178 jblocks = one_path_blks + extra_name_blks + trunc_blks
179 + EXT3_DELETE_TRANS_BLOCKS + 1;
180 break;
181 case KML_OPCODE_MKNOD:
182 jblocks = one_path_blks + trunc_blks +
183 EXT3_DATA_TRANS_BLOCKS + 3 + 2;
184 break;
185 case KML_OPCODE_RENAME:
186 jblocks = one_path_blks + extra_path_blks + trunc_blks +
187 2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
188 break;
189 case KML_OPCODE_WRITE:
190 jblocks = one_path_blks;
191
192
193
194
195 break;
196 default:
197 CDEBUG(D_JOURNAL, "invalid operation %d for journal\n", op);
198 return NULL;
199 }
200
201 CDEBUG(D_JOURNAL, "creating journal handle (%d blocks) for op %d\n",
202 jblocks, op);
203
204
205
206
207 lock_kernel();
208 handle = journal_start(EXT3_JOURNAL(inode), jblocks);
209 unlock_kernel();
210 return handle;
211}
212
213static void presto_e3_trans_commit(struct presto_file_set *fset, void *handle)
214{
215 if ( presto_no_journal(fset) || !handle)
216 return;
217
218
219 lock_kernel();
220 journal_stop(handle);
221 unlock_kernel();
222}
223
224static void presto_e3_journal_file_data(struct inode *inode)
225{
226#ifdef EXT3_JOURNAL_DATA_FL
227 inode->u.ext3_i.i_flags |= EXT3_JOURNAL_DATA_FL;
228#else
229#warning You must have a facility to enable journaled writes for recovery!
230#endif
231}
232
233
234
235static int presto_e3_has_all_data(struct inode *inode)
236{
237 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
238 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
239 const long direct_blocks = EXT3_NDIR_BLOCKS,
240 indirect_blocks = ptrs,
241 double_blocks = (1 << (ptrs_bits * 2));
242 long block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
243 inode->i_sb->s_blocksize_bits;
244
245 ENTRY;
246
247 if (inode->i_size == 0) {
248 EXIT;
249 return 1;
250 }
251
252 if (block < direct_blocks) {
253
254 } else if (block < indirect_blocks + direct_blocks) {
255 block++;
256 } else if (block < double_blocks + indirect_blocks + direct_blocks) {
257 block += 2;
258 } else if (((block - double_blocks - indirect_blocks - direct_blocks)
259 >> (ptrs_bits * 2)) < ptrs) {
260 block += 3;
261 }
262
263 block *= (inode->i_sb->s_blocksize / 512);
264
265 CDEBUG(D_CACHE, "Need %ld blocks, have %ld.\n", block, inode->i_blocks);
266
267 if (block > inode->i_blocks) {
268 EXIT;
269 return 0;
270 }
271
272 EXIT;
273 return 1;
274}
275
276struct journal_ops presto_ext3_journal_ops = {
277 .tr_all_data = presto_e3_has_all_data,
278 .tr_avail = presto_e3_freespace,
279 .tr_start = presto_e3_trans_start,
280 .tr_commit = presto_e3_trans_commit,
281 .tr_journal_data = presto_e3_journal_file_data,
282 .tr_ilookup = presto_iget_ilookup
283};
284
285#endif
286