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