1
2
3
4
5
6
7
8#include "logfs.h"
9#include <linux/slab.h>
10#include <linux/writeback.h>
11#include <linux/backing-dev.h>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#define INOS_PER_WRAP (0x10000)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49static struct kmem_cache *logfs_inode_cache;
50
51static DEFINE_SPINLOCK(logfs_inode_lock);
52
53static void logfs_inode_setops(struct inode *inode)
54{
55 switch (inode->i_mode & S_IFMT) {
56 case S_IFDIR:
57 inode->i_op = &logfs_dir_iops;
58 inode->i_fop = &logfs_dir_fops;
59 inode->i_mapping->a_ops = &logfs_reg_aops;
60 break;
61 case S_IFREG:
62 inode->i_op = &logfs_reg_iops;
63 inode->i_fop = &logfs_reg_fops;
64 inode->i_mapping->a_ops = &logfs_reg_aops;
65 break;
66 case S_IFLNK:
67 inode->i_op = &logfs_symlink_iops;
68 inode->i_mapping->a_ops = &logfs_reg_aops;
69 break;
70 case S_IFSOCK:
71 case S_IFBLK:
72 case S_IFCHR:
73 case S_IFIFO:
74 init_special_inode(inode, inode->i_mode, inode->i_rdev);
75 break;
76 default:
77 BUG();
78 }
79}
80
81static struct inode *__logfs_iget(struct super_block *sb, ino_t ino)
82{
83 struct inode *inode = iget_locked(sb, ino);
84 int err;
85
86 if (!inode)
87 return ERR_PTR(-ENOMEM);
88 if (!(inode->i_state & I_NEW))
89 return inode;
90
91 err = logfs_read_inode(inode);
92 if (err || inode->i_nlink == 0) {
93
94
95
96 clear_nlink(inode);
97 logfs_inode(inode)->li_flags |= LOGFS_IF_ZOMBIE;
98 iget_failed(inode);
99 if (!err)
100 err = -ENOENT;
101 return ERR_PTR(err);
102 }
103
104 logfs_inode_setops(inode);
105 unlock_new_inode(inode);
106 return inode;
107}
108
109struct inode *logfs_iget(struct super_block *sb, ino_t ino)
110{
111 BUG_ON(ino == LOGFS_INO_MASTER);
112 BUG_ON(ino == LOGFS_INO_SEGFILE);
113 return __logfs_iget(sb, ino);
114}
115
116
117
118
119
120struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *is_cached)
121{
122 struct logfs_super *super = logfs_super(sb);
123 struct logfs_inode *li;
124
125 if (ino == LOGFS_INO_MASTER)
126 return super->s_master_inode;
127 if (ino == LOGFS_INO_SEGFILE)
128 return super->s_segfile_inode;
129
130 spin_lock(&logfs_inode_lock);
131 list_for_each_entry(li, &super->s_freeing_list, li_freeing_list)
132 if (li->vfs_inode.i_ino == ino) {
133 li->li_refcount++;
134 spin_unlock(&logfs_inode_lock);
135 *is_cached = 1;
136 return &li->vfs_inode;
137 }
138 spin_unlock(&logfs_inode_lock);
139
140 *is_cached = 0;
141 return __logfs_iget(sb, ino);
142}
143
144static void logfs_i_callback(struct rcu_head *head)
145{
146 struct inode *inode = container_of(head, struct inode, i_rcu);
147 kmem_cache_free(logfs_inode_cache, logfs_inode(inode));
148}
149
150static void __logfs_destroy_inode(struct inode *inode)
151{
152 struct logfs_inode *li = logfs_inode(inode);
153
154 BUG_ON(li->li_block);
155 list_del(&li->li_freeing_list);
156 call_rcu(&inode->i_rcu, logfs_i_callback);
157}
158
159static void __logfs_destroy_meta_inode(struct inode *inode)
160{
161 struct logfs_inode *li = logfs_inode(inode);
162 BUG_ON(li->li_block);
163 call_rcu(&inode->i_rcu, logfs_i_callback);
164}
165
166static void logfs_destroy_inode(struct inode *inode)
167{
168 struct logfs_inode *li = logfs_inode(inode);
169
170 if (inode->i_ino < LOGFS_RESERVED_INOS) {
171
172
173
174
175 __logfs_destroy_meta_inode(inode);
176 return;
177 }
178
179 BUG_ON(list_empty(&li->li_freeing_list));
180 spin_lock(&logfs_inode_lock);
181 li->li_refcount--;
182 if (li->li_refcount == 0)
183 __logfs_destroy_inode(inode);
184 spin_unlock(&logfs_inode_lock);
185}
186
187void logfs_safe_iput(struct inode *inode, int is_cached)
188{
189 if (inode->i_ino == LOGFS_INO_MASTER)
190 return;
191 if (inode->i_ino == LOGFS_INO_SEGFILE)
192 return;
193
194 if (is_cached) {
195 logfs_destroy_inode(inode);
196 return;
197 }
198
199 iput(inode);
200}
201
202static void logfs_init_inode(struct super_block *sb, struct inode *inode)
203{
204 struct logfs_inode *li = logfs_inode(inode);
205 int i;
206
207 li->li_flags = 0;
208 li->li_height = 0;
209 li->li_used_bytes = 0;
210 li->li_block = NULL;
211 i_uid_write(inode, 0);
212 i_gid_write(inode, 0);
213 inode->i_size = 0;
214 inode->i_blocks = 0;
215 inode->i_ctime = CURRENT_TIME;
216 inode->i_mtime = CURRENT_TIME;
217 li->li_refcount = 1;
218 INIT_LIST_HEAD(&li->li_freeing_list);
219
220 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
221 li->li_data[i] = 0;
222
223 return;
224}
225
226static struct inode *logfs_alloc_inode(struct super_block *sb)
227{
228 struct logfs_inode *li;
229
230 li = kmem_cache_alloc(logfs_inode_cache, GFP_NOFS);
231 if (!li)
232 return NULL;
233 logfs_init_inode(sb, &li->vfs_inode);
234 return &li->vfs_inode;
235}
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino)
263{
264 struct inode *inode;
265
266 inode = new_inode(sb);
267 if (!inode)
268 return ERR_PTR(-ENOMEM);
269
270 inode->i_mode = S_IFREG;
271 inode->i_ino = ino;
272 inode->i_data.a_ops = &logfs_reg_aops;
273 mapping_set_gfp_mask(&inode->i_data, GFP_NOFS);
274
275 return inode;
276}
277
278struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino)
279{
280 struct inode *inode;
281 int err;
282
283 inode = logfs_new_meta_inode(sb, ino);
284 if (IS_ERR(inode))
285 return inode;
286
287 err = logfs_read_inode(inode);
288 if (err) {
289 iput(inode);
290 return ERR_PTR(err);
291 }
292 logfs_inode_setops(inode);
293 return inode;
294}
295
296static int logfs_write_inode(struct inode *inode, struct writeback_control *wbc)
297{
298 int ret;
299 long flags = WF_LOCK;
300
301
302 if (logfs_inode(inode)->li_flags & LOGFS_IF_STILLBORN)
303 return 0;
304
305 ret = __logfs_write_inode(inode, NULL, flags);
306 LOGFS_BUG_ON(ret, inode->i_sb);
307 return ret;
308}
309
310
311static int logfs_drop_inode(struct inode *inode)
312{
313 struct logfs_super *super = logfs_super(inode->i_sb);
314 struct logfs_inode *li = logfs_inode(inode);
315
316 spin_lock(&logfs_inode_lock);
317 list_move(&li->li_freeing_list, &super->s_freeing_list);
318 spin_unlock(&logfs_inode_lock);
319 return generic_drop_inode(inode);
320}
321
322static void logfs_set_ino_generation(struct super_block *sb,
323 struct inode *inode)
324{
325 struct logfs_super *super = logfs_super(sb);
326 u64 ino;
327
328 mutex_lock(&super->s_journal_mutex);
329 ino = logfs_seek_hole(super->s_master_inode, super->s_last_ino + 1);
330 super->s_last_ino = ino;
331 super->s_inos_till_wrap--;
332 if (super->s_inos_till_wrap < 0) {
333 super->s_last_ino = LOGFS_RESERVED_INOS;
334 super->s_generation++;
335 super->s_inos_till_wrap = INOS_PER_WRAP;
336 }
337 inode->i_ino = ino;
338 inode->i_generation = super->s_generation;
339 mutex_unlock(&super->s_journal_mutex);
340}
341
342struct inode *logfs_new_inode(struct inode *dir, umode_t mode)
343{
344 struct super_block *sb = dir->i_sb;
345 struct inode *inode;
346
347 inode = new_inode(sb);
348 if (!inode)
349 return ERR_PTR(-ENOMEM);
350
351 logfs_init_inode(sb, inode);
352
353
354 logfs_inode(inode)->li_flags |=
355 logfs_inode(dir)->li_flags & LOGFS_FL_INHERITED;
356
357 inode->i_mode = mode;
358 logfs_set_ino_generation(sb, inode);
359
360 inode_init_owner(inode, dir, mode);
361 logfs_inode_setops(inode);
362 insert_inode_hash(inode);
363
364 return inode;
365}
366
367static void logfs_init_once(void *_li)
368{
369 struct logfs_inode *li = _li;
370 int i;
371
372 li->li_flags = 0;
373 li->li_used_bytes = 0;
374 li->li_refcount = 1;
375 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
376 li->li_data[i] = 0;
377 inode_init_once(&li->vfs_inode);
378}
379
380static int logfs_sync_fs(struct super_block *sb, int wait)
381{
382 logfs_get_wblocks(sb, NULL, WF_LOCK);
383 logfs_write_anchor(sb);
384 logfs_put_wblocks(sb, NULL, WF_LOCK);
385 return 0;
386}
387
388static void logfs_put_super(struct super_block *sb)
389{
390 struct logfs_super *super = logfs_super(sb);
391
392 iput(super->s_segfile_inode);
393 iput(super->s_master_inode);
394 iput(super->s_mapping_inode);
395}
396
397const struct super_operations logfs_super_operations = {
398 .alloc_inode = logfs_alloc_inode,
399 .destroy_inode = logfs_destroy_inode,
400 .evict_inode = logfs_evict_inode,
401 .drop_inode = logfs_drop_inode,
402 .put_super = logfs_put_super,
403 .write_inode = logfs_write_inode,
404 .statfs = logfs_statfs,
405 .sync_fs = logfs_sync_fs,
406};
407
408int logfs_init_inode_cache(void)
409{
410 logfs_inode_cache = kmem_cache_create("logfs_inode_cache",
411 sizeof(struct logfs_inode), 0, SLAB_RECLAIM_ACCOUNT,
412 logfs_init_once);
413 if (!logfs_inode_cache)
414 return -ENOMEM;
415 return 0;
416}
417
418void logfs_destroy_inode_cache(void)
419{
420
421
422
423
424 rcu_barrier();
425 kmem_cache_destroy(logfs_inode_cache);
426}
427