1 /*
2 * fs/logfs/readwrite.c
3 *
4 * As should be obvious for Linux kernel code, license is GPLv2
5 *
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
7 *
8 *
9 * Actually contains five sets of very similar functions:
10 * read read blocks from a file
11 * seek_hole find next hole
12 * seek_data find next data block
13 * valid check whether a block still belongs to a file
14 * write write blocks to a file
15 * delete delete a block (for directories and ifile)
16 * rewrite move existing blocks of a file to a new location (gc helper)
17 * truncate truncate a file
18 */
19 #include "logfs.h"
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22
adjust_bix(u64 bix,level_t level)23 static u64 adjust_bix(u64 bix, level_t level)
24 {
25 switch (level) {
26 case 0:
27 return bix;
28 case LEVEL(1):
29 return max_t(u64, bix, I0_BLOCKS);
30 case LEVEL(2):
31 return max_t(u64, bix, I1_BLOCKS);
32 case LEVEL(3):
33 return max_t(u64, bix, I2_BLOCKS);
34 case LEVEL(4):
35 return max_t(u64, bix, I3_BLOCKS);
36 case LEVEL(5):
37 return max_t(u64, bix, I4_BLOCKS);
38 default:
39 WARN_ON(1);
40 return bix;
41 }
42 }
43
maxbix(u8 height)44 static inline u64 maxbix(u8 height)
45 {
46 return 1ULL << (LOGFS_BLOCK_BITS * height);
47 }
48
49 /**
50 * The inode address space is cut in two halves. Lower half belongs to data
51 * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
52 * set, the actual block index (bix) and level can be derived from the page
53 * index.
54 *
55 * The lowest three bits of the block index are set to 0 after packing and
56 * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
57 * anyway this is harmless.
58 */
59 #define ARCH_SHIFT (BITS_PER_LONG - 32)
60 #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
61 #define LEVEL_SHIFT (28 + ARCH_SHIFT)
first_indirect_block(void)62 static inline pgoff_t first_indirect_block(void)
63 {
64 return INDIRECT_BIT | (1ULL << LEVEL_SHIFT);
65 }
66
logfs_pack_index(u64 bix,level_t level)67 pgoff_t logfs_pack_index(u64 bix, level_t level)
68 {
69 pgoff_t index;
70
71 BUG_ON(bix >= INDIRECT_BIT);
72 if (level == 0)
73 return bix;
74
75 index = INDIRECT_BIT;
76 index |= (__force long)level << LEVEL_SHIFT;
77 index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS);
78 return index;
79 }
80
logfs_unpack_index(pgoff_t index,u64 * bix,level_t * level)81 void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level)
82 {
83 u8 __level;
84
85 if (!(index & INDIRECT_BIT)) {
86 *bix = index;
87 *level = 0;
88 return;
89 }
90
91 __level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT;
92 *level = LEVEL(__level);
93 *bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT;
94 *bix = adjust_bix(*bix, *level);
95 return;
96 }
97 #undef ARCH_SHIFT
98 #undef INDIRECT_BIT
99 #undef LEVEL_SHIFT
100
101 /*
102 * Time is stored as nanoseconds since the epoch.
103 */
be64_to_timespec(__be64 betime)104 static struct timespec be64_to_timespec(__be64 betime)
105 {
106 return ns_to_timespec(be64_to_cpu(betime));
107 }
108
timespec_to_be64(struct timespec tsp)109 static __be64 timespec_to_be64(struct timespec tsp)
110 {
111 return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec);
112 }
113
logfs_disk_to_inode(struct logfs_disk_inode * di,struct inode * inode)114 static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode)
115 {
116 struct logfs_inode *li = logfs_inode(inode);
117 int i;
118
119 inode->i_mode = be16_to_cpu(di->di_mode);
120 li->li_height = di->di_height;
121 li->li_flags = be32_to_cpu(di->di_flags);
122 inode->i_uid = be32_to_cpu(di->di_uid);
123 inode->i_gid = be32_to_cpu(di->di_gid);
124 inode->i_size = be64_to_cpu(di->di_size);
125 logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes));
126 inode->i_atime = be64_to_timespec(di->di_atime);
127 inode->i_ctime = be64_to_timespec(di->di_ctime);
128 inode->i_mtime = be64_to_timespec(di->di_mtime);
129 inode->i_nlink = be32_to_cpu(di->di_refcount);
130 inode->i_generation = be32_to_cpu(di->di_generation);
131
132 switch (inode->i_mode & S_IFMT) {
133 case S_IFSOCK: /* fall through */
134 case S_IFBLK: /* fall through */
135 case S_IFCHR: /* fall through */
136 case S_IFIFO:
137 inode->i_rdev = be64_to_cpu(di->di_data[0]);
138 break;
139 case S_IFDIR: /* fall through */
140 case S_IFREG: /* fall through */
141 case S_IFLNK:
142 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
143 li->li_data[i] = be64_to_cpu(di->di_data[i]);
144 break;
145 default:
146 BUG();
147 }
148 }
149
logfs_inode_to_disk(struct inode * inode,struct logfs_disk_inode * di)150 static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di)
151 {
152 struct logfs_inode *li = logfs_inode(inode);
153 int i;
154
155 di->di_mode = cpu_to_be16(inode->i_mode);
156 di->di_height = li->li_height;
157 di->di_pad = 0;
158 di->di_flags = cpu_to_be32(li->li_flags);
159 di->di_uid = cpu_to_be32(inode->i_uid);
160 di->di_gid = cpu_to_be32(inode->i_gid);
161 di->di_size = cpu_to_be64(i_size_read(inode));
162 di->di_used_bytes = cpu_to_be64(li->li_used_bytes);
163 di->di_atime = timespec_to_be64(inode->i_atime);
164 di->di_ctime = timespec_to_be64(inode->i_ctime);
165 di->di_mtime = timespec_to_be64(inode->i_mtime);
166 di->di_refcount = cpu_to_be32(inode->i_nlink);
167 di->di_generation = cpu_to_be32(inode->i_generation);
168
169 switch (inode->i_mode & S_IFMT) {
170 case S_IFSOCK: /* fall through */
171 case S_IFBLK: /* fall through */
172 case S_IFCHR: /* fall through */
173 case S_IFIFO:
174 di->di_data[0] = cpu_to_be64(inode->i_rdev);
175 break;
176 case S_IFDIR: /* fall through */
177 case S_IFREG: /* fall through */
178 case S_IFLNK:
179 for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
180 di->di_data[i] = cpu_to_be64(li->li_data[i]);
181 break;
182 default:
183 BUG();
184 }
185 }
186
__logfs_set_blocks(struct inode * inode)187 static void __logfs_set_blocks(struct inode *inode)
188 {
189 struct super_block *sb = inode->i_sb;
190 struct logfs_inode *li = logfs_inode(inode);
191
192 inode->i_blocks = ULONG_MAX;
193 if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX)
194 inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9;
195 }
196
logfs_set_blocks(struct inode * inode,u64 bytes)197 void logfs_set_blocks(struct inode *inode, u64 bytes)
198 {
199 struct logfs_inode *li = logfs_inode(inode);
200
201 li->li_used_bytes = bytes;
202 __logfs_set_blocks(inode);
203 }
204
prelock_page(struct super_block * sb,struct page * page,int lock)205 static void prelock_page(struct super_block *sb, struct page *page, int lock)
206 {
207 struct logfs_super *super = logfs_super(sb);
208
209 BUG_ON(!PageLocked(page));
210 if (lock) {
211 BUG_ON(PagePreLocked(page));
212 SetPagePreLocked(page);
213 } else {
214 /* We are in GC path. */
215 if (PagePreLocked(page))
216 super->s_lock_count++;
217 else
218 SetPagePreLocked(page);
219 }
220 }
221
preunlock_page(struct super_block * sb,struct page * page,int lock)222 static void preunlock_page(struct super_block *sb, struct page *page, int lock)
223 {
224 struct logfs_super *super = logfs_super(sb);
225
226 BUG_ON(!PageLocked(page));
227 if (lock)
228 ClearPagePreLocked(page);
229 else {
230 /* We are in GC path. */
231 BUG_ON(!PagePreLocked(page));
232 if (super->s_lock_count)
233 super->s_lock_count--;
234 else
235 ClearPagePreLocked(page);
236 }
237 }
238
239 /*
240 * Logfs is prone to an AB-BA deadlock where one task tries to acquire
241 * s_write_mutex with a locked page and GC tries to get that page while holding
242 * s_write_mutex.
243 * To solve this issue logfs will ignore the page lock iff the page in question
244 * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
245 * in addition to PG_locked.
246 */
logfs_get_wblocks(struct super_block * sb,struct page * page,int lock)247 static void logfs_get_wblocks(struct super_block *sb, struct page *page,
248 int lock)
249 {
250 struct logfs_super *super = logfs_super(sb);
251
252 if (page)
253 prelock_page(sb, page, lock);
254
255 if (lock) {
256 mutex_lock(&super->s_write_mutex);
257 logfs_gc_pass(sb);
258 /* FIXME: We also have to check for shadowed space
259 * and mempool fill grade */
260 }
261 }
262
logfs_put_wblocks(struct super_block * sb,struct page * page,int lock)263 static void logfs_put_wblocks(struct super_block *sb, struct page *page,
264 int lock)
265 {
266 struct logfs_super *super = logfs_super(sb);
267
268 if (page)
269 preunlock_page(sb, page, lock);
270 /* Order matters - we must clear PG_pre_locked before releasing
271 * s_write_mutex or we could race against another task. */
272 if (lock)
273 mutex_unlock(&super->s_write_mutex);
274 }
275
logfs_get_read_page(struct inode * inode,u64 bix,level_t level)276 static struct page *logfs_get_read_page(struct inode *inode, u64 bix,
277 level_t level)
278 {
279 return find_or_create_page(inode->i_mapping,
280 logfs_pack_index(bix, level), GFP_NOFS);
281 }
282
logfs_put_read_page(struct page * page)283 static void logfs_put_read_page(struct page *page)
284 {
285 unlock_page(page);
286 page_cache_release(page);
287 }
288
logfs_lock_write_page(struct page * page)289 static void logfs_lock_write_page(struct page *page)
290 {
291 int loop = 0;
292
293 while (unlikely(!trylock_page(page))) {
294 if (loop++ > 0x1000) {
295 /* Has been observed once so far... */
296 printk(KERN_ERR "stack at %p\n", &loop);
297 BUG();
298 }
299 if (PagePreLocked(page)) {
300 /* Holder of page lock is waiting for us, it
301 * is safe to use this page. */
302 break;
303 }
304 /* Some other process has this page locked and has
305 * nothing to do with us. Wait for it to finish.
306 */
307 schedule();
308 }
309 BUG_ON(!PageLocked(page));
310 }
311
logfs_get_write_page(struct inode * inode,u64 bix,level_t level)312 static struct page *logfs_get_write_page(struct inode *inode, u64 bix,
313 level_t level)
314 {
315 struct address_space *mapping = inode->i_mapping;
316 pgoff_t index = logfs_pack_index(bix, level);
317 struct page *page;
318 int err;
319
320 repeat:
321 page = find_get_page(mapping, index);
322 if (!page) {
323 page = __page_cache_alloc(GFP_NOFS);
324 if (!page)
325 return NULL;
326 err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS);
327 if (unlikely(err)) {
328 page_cache_release(page);
329 if (err == -EEXIST)
330 goto repeat;
331 return NULL;
332 }
333 } else logfs_lock_write_page(page);
334 BUG_ON(!PageLocked(page));
335 return page;
336 }
337
logfs_unlock_write_page(struct page * page)338 static void logfs_unlock_write_page(struct page *page)
339 {
340 if (!PagePreLocked(page))
341 unlock_page(page);
342 }
343
logfs_put_write_page(struct page * page)344 static void logfs_put_write_page(struct page *page)
345 {
346 logfs_unlock_write_page(page);
347 page_cache_release(page);
348 }
349
logfs_get_page(struct inode * inode,u64 bix,level_t level,int rw)350 static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level,
351 int rw)
352 {
353 if (rw == READ)
354 return logfs_get_read_page(inode, bix, level);
355 else
356 return logfs_get_write_page(inode, bix, level);
357 }
358
logfs_put_page(struct page * page,int rw)359 static void logfs_put_page(struct page *page, int rw)
360 {
361 if (rw == READ)
362 logfs_put_read_page(page);
363 else
364 logfs_put_write_page(page);
365 }
366
__get_bits(u64 val,int skip,int no)367 static unsigned long __get_bits(u64 val, int skip, int no)
368 {
369 u64 ret = val;
370
371 ret >>= skip * no;
372 ret <<= 64 - no;
373 ret >>= 64 - no;
374 return ret;
375 }
376
get_bits(u64 val,level_t skip)377 static unsigned long get_bits(u64 val, level_t skip)
378 {
379 return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS);
380 }
381
init_shadow_tree(struct super_block * sb,struct shadow_tree * tree)382 static inline void init_shadow_tree(struct super_block *sb,
383 struct shadow_tree *tree)
384 {
385 struct logfs_super *super = logfs_super(sb);
386
387 btree_init_mempool64(&tree->new, super->s_btree_pool);
388 btree_init_mempool64(&tree->old, super->s_btree_pool);
389 }
390
indirect_write_block(struct logfs_block * block)391 static void indirect_write_block(struct logfs_block *block)
392 {
393 struct page *page;
394 struct inode *inode;
395 int ret;
396
397 page = block->page;
398 inode = page->mapping->host;
399 logfs_lock_write_page(page);
400 ret = logfs_write_buf(inode, page, 0);
401 logfs_unlock_write_page(page);
402 /*
403 * This needs some rework. Unless you want your filesystem to run
404 * completely synchronously (you don't), the filesystem will always
405 * report writes as 'successful' before the actual work has been
406 * done. The actual work gets done here and this is where any errors
407 * will show up. And there isn't much we can do about it, really.
408 *
409 * Some attempts to fix the errors (move from bad blocks, retry io,...)
410 * have already been done, so anything left should be either a broken
411 * device or a bug somewhere in logfs itself. Being relatively new,
412 * the odds currently favor a bug, so for now the line below isn't
413 * entirely tasteles.
414 */
415 BUG_ON(ret);
416 }
417
inode_write_block(struct logfs_block * block)418 static void inode_write_block(struct logfs_block *block)
419 {
420 struct inode *inode;
421 int ret;
422
423 inode = block->inode;
424 if (inode->i_ino == LOGFS_INO_MASTER)
425 logfs_write_anchor(inode->i_sb);
426 else {
427 ret = __logfs_write_inode(inode, 0);
428 /* see indirect_write_block comment */
429 BUG_ON(ret);
430 }
431 }
432
433 /*
434 * This silences a false, yet annoying gcc warning. I hate it when my editor
435 * jumps into bitops.h each time I recompile this file.
436 * TODO: Complain to gcc folks about this and upgrade compiler.
437 */
fnb(const unsigned long * addr,unsigned long size,unsigned long offset)438 static unsigned long fnb(const unsigned long *addr,
439 unsigned long size, unsigned long offset)
440 {
441 return find_next_bit(addr, size, offset);
442 }
443
inode_val0(struct inode * inode)444 static __be64 inode_val0(struct inode *inode)
445 {
446 struct logfs_inode *li = logfs_inode(inode);
447 u64 val;
448
449 /*
450 * Explicit shifting generates good code, but must match the format
451 * of the structure. Add some paranoia just in case.
452 */
453 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0);
454 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2);
455 BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4);
456
457 val = (u64)inode->i_mode << 48 |
458 (u64)li->li_height << 40 |
459 (u64)li->li_flags;
460 return cpu_to_be64(val);
461 }
462
inode_write_alias(struct super_block * sb,struct logfs_block * block,write_alias_t * write_one_alias)463 static int inode_write_alias(struct super_block *sb,
464 struct logfs_block *block, write_alias_t *write_one_alias)
465 {
466 struct inode *inode = block->inode;
467 struct logfs_inode *li = logfs_inode(inode);
468 unsigned long pos;
469 u64 ino , bix;
470 __be64 val;
471 level_t level;
472 int err;
473
474 for (pos = 0; ; pos++) {
475 pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
476 if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS)
477 return 0;
478
479 switch (pos) {
480 case INODE_HEIGHT_OFS:
481 val = inode_val0(inode);
482 break;
483 case INODE_USED_OFS:
484 val = cpu_to_be64(li->li_used_bytes);;
485 break;
486 case INODE_SIZE_OFS:
487 val = cpu_to_be64(i_size_read(inode));
488 break;
489 case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1:
490 val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]);
491 break;
492 default:
493 BUG();
494 }
495
496 ino = LOGFS_INO_MASTER;
497 bix = inode->i_ino;
498 level = LEVEL(0);
499 err = write_one_alias(sb, ino, bix, level, pos, val);
500 if (err)
501 return err;
502 }
503 }
504
indirect_write_alias(struct super_block * sb,struct logfs_block * block,write_alias_t * write_one_alias)505 static int indirect_write_alias(struct super_block *sb,
506 struct logfs_block *block, write_alias_t *write_one_alias)
507 {
508 unsigned long pos;
509 struct page *page = block->page;
510 u64 ino , bix;
511 __be64 *child, val;
512 level_t level;
513 int err;
514
515 for (pos = 0; ; pos++) {
516 pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
517 if (pos >= LOGFS_BLOCK_FACTOR)
518 return 0;
519
520 ino = page->mapping->host->i_ino;
521 logfs_unpack_index(page->index, &bix, &level);
522 child = kmap_atomic(page, KM_USER0);
523 val = child[pos];
524 kunmap_atomic(child, KM_USER0);
525 err = write_one_alias(sb, ino, bix, level, pos, val);
526 if (err)
527 return err;
528 }
529 }
530
logfs_write_obj_aliases_pagecache(struct super_block * sb)531 int logfs_write_obj_aliases_pagecache(struct super_block *sb)
532 {
533 struct logfs_super *super = logfs_super(sb);
534 struct logfs_block *block;
535 int err;
536
537 list_for_each_entry(block, &super->s_object_alias, alias_list) {
538 err = block->ops->write_alias(sb, block, write_alias_journal);
539 if (err)
540 return err;
541 }
542 return 0;
543 }
544
__free_block(struct super_block * sb,struct logfs_block * block)545 void __free_block(struct super_block *sb, struct logfs_block *block)
546 {
547 BUG_ON(!list_empty(&block->item_list));
548 list_del(&block->alias_list);
549 mempool_free(block, logfs_super(sb)->s_block_pool);
550 }
551
inode_free_block(struct super_block * sb,struct logfs_block * block)552 static void inode_free_block(struct super_block *sb, struct logfs_block *block)
553 {
554 struct inode *inode = block->inode;
555
556 logfs_inode(inode)->li_block = NULL;
557 __free_block(sb, block);
558 }
559
indirect_free_block(struct super_block * sb,struct logfs_block * block)560 static void indirect_free_block(struct super_block *sb,
561 struct logfs_block *block)
562 {
563 ClearPagePrivate(block->page);
564 block->page->private = 0;
565 __free_block(sb, block);
566 }
567
568
569 static struct logfs_block_ops inode_block_ops = {
570 .write_block = inode_write_block,
571 .free_block = inode_free_block,
572 .write_alias = inode_write_alias,
573 };
574
575 struct logfs_block_ops indirect_block_ops = {
576 .write_block = indirect_write_block,
577 .free_block = indirect_free_block,
578 .write_alias = indirect_write_alias,
579 };
580
__alloc_block(struct super_block * sb,u64 ino,u64 bix,level_t level)581 struct logfs_block *__alloc_block(struct super_block *sb,
582 u64 ino, u64 bix, level_t level)
583 {
584 struct logfs_super *super = logfs_super(sb);
585 struct logfs_block *block;
586
587 block = mempool_alloc(super->s_block_pool, GFP_NOFS);
588 memset(block, 0, sizeof(*block));
589 INIT_LIST_HEAD(&block->alias_list);
590 INIT_LIST_HEAD(&block->item_list);
591 block->sb = sb;
592 block->ino = ino;
593 block->bix = bix;
594 block->level = level;
595 return block;
596 }
597
alloc_inode_block(struct inode * inode)598 static void alloc_inode_block(struct inode *inode)
599 {
600 struct logfs_inode *li = logfs_inode(inode);
601 struct logfs_block *block;
602
603 if (li->li_block)
604 return;
605
606 block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0);
607 block->inode = inode;
608 li->li_block = block;
609 block->ops = &inode_block_ops;
610 }
611
initialize_block_counters(struct page * page,struct logfs_block * block,__be64 * array,int page_is_empty)612 void initialize_block_counters(struct page *page, struct logfs_block *block,
613 __be64 *array, int page_is_empty)
614 {
615 u64 ptr;
616 int i, start;
617
618 block->partial = 0;
619 block->full = 0;
620 start = 0;
621 if (page->index < first_indirect_block()) {
622 /* Counters are pointless on level 0 */
623 return;
624 }
625 if (page->index == first_indirect_block()) {
626 /* Skip unused pointers */
627 start = I0_BLOCKS;
628 block->full = I0_BLOCKS;
629 }
630 if (!page_is_empty) {
631 for (i = start; i < LOGFS_BLOCK_FACTOR; i++) {
632 ptr = be64_to_cpu(array[i]);
633 if (ptr)
634 block->partial++;
635 if (ptr & LOGFS_FULLY_POPULATED)
636 block->full++;
637 }
638 }
639 }
640
alloc_data_block(struct inode * inode,struct page * page)641 static void alloc_data_block(struct inode *inode, struct page *page)
642 {
643 struct logfs_block *block;
644 u64 bix;
645 level_t level;
646
647 if (PagePrivate(page))
648 return;
649
650 logfs_unpack_index(page->index, &bix, &level);
651 block = __alloc_block(inode->i_sb, inode->i_ino, bix, level);
652 block->page = page;
653 SetPagePrivate(page);
654 page->private = (unsigned long)block;
655 block->ops = &indirect_block_ops;
656 }
657
alloc_indirect_block(struct inode * inode,struct page * page,int page_is_empty)658 static void alloc_indirect_block(struct inode *inode, struct page *page,
659 int page_is_empty)
660 {
661 struct logfs_block *block;
662 __be64 *array;
663
664 if (PagePrivate(page))
665 return;
666
667 alloc_data_block(inode, page);
668
669 block = logfs_block(page);
670 array = kmap_atomic(page, KM_USER0);
671 initialize_block_counters(page, block, array, page_is_empty);
672 kunmap_atomic(array, KM_USER0);
673 }
674
block_set_pointer(struct page * page,int index,u64 ptr)675 static void block_set_pointer(struct page *page, int index, u64 ptr)
676 {
677 struct logfs_block *block = logfs_block(page);
678 __be64 *array;
679 u64 oldptr;
680
681 BUG_ON(!block);
682 array = kmap_atomic(page, KM_USER0);
683 oldptr = be64_to_cpu(array[index]);
684 array[index] = cpu_to_be64(ptr);
685 kunmap_atomic(array, KM_USER0);
686 SetPageUptodate(page);
687
688 block->full += !!(ptr & LOGFS_FULLY_POPULATED)
689 - !!(oldptr & LOGFS_FULLY_POPULATED);
690 block->partial += !!ptr - !!oldptr;
691 }
692
block_get_pointer(struct page * page,int index)693 static u64 block_get_pointer(struct page *page, int index)
694 {
695 __be64 *block;
696 u64 ptr;
697
698 block = kmap_atomic(page, KM_USER0);
699 ptr = be64_to_cpu(block[index]);
700 kunmap_atomic(block, KM_USER0);
701 return ptr;
702 }
703
logfs_read_empty(struct page * page)704 static int logfs_read_empty(struct page *page)
705 {
706 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
707 return 0;
708 }
709
logfs_read_direct(struct inode * inode,struct page * page)710 static int logfs_read_direct(struct inode *inode, struct page *page)
711 {
712 struct logfs_inode *li = logfs_inode(inode);
713 pgoff_t index = page->index;
714 u64 block;
715
716 block = li->li_data[index];
717 if (!block)
718 return logfs_read_empty(page);
719
720 return logfs_segment_read(inode, page, block, index, 0);
721 }
722
logfs_read_loop(struct inode * inode,struct page * page,int rw_context)723 static int logfs_read_loop(struct inode *inode, struct page *page,
724 int rw_context)
725 {
726 struct logfs_inode *li = logfs_inode(inode);
727 u64 bix, bofs = li->li_data[INDIRECT_INDEX];
728 level_t level, target_level;
729 int ret;
730 struct page *ipage;
731
732 logfs_unpack_index(page->index, &bix, &target_level);
733 if (!bofs)
734 return logfs_read_empty(page);
735
736 if (bix >= maxbix(li->li_height))
737 return logfs_read_empty(page);
738
739 for (level = LEVEL(li->li_height);
740 (__force u8)level > (__force u8)target_level;
741 level = SUBLEVEL(level)){
742 ipage = logfs_get_page(inode, bix, level, rw_context);
743 if (!ipage)
744 return -ENOMEM;
745
746 ret = logfs_segment_read(inode, ipage, bofs, bix, level);
747 if (ret) {
748 logfs_put_read_page(ipage);
749 return ret;
750 }
751
752 bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
753 logfs_put_page(ipage, rw_context);
754 if (!bofs)
755 return logfs_read_empty(page);
756 }
757
758 return logfs_segment_read(inode, page, bofs, bix, 0);
759 }
760
logfs_read_block(struct inode * inode,struct page * page,int rw_context)761 static int logfs_read_block(struct inode *inode, struct page *page,
762 int rw_context)
763 {
764 pgoff_t index = page->index;
765
766 if (index < I0_BLOCKS)
767 return logfs_read_direct(inode, page);
768 return logfs_read_loop(inode, page, rw_context);
769 }
770
logfs_exist_loop(struct inode * inode,u64 bix)771 static int logfs_exist_loop(struct inode *inode, u64 bix)
772 {
773 struct logfs_inode *li = logfs_inode(inode);
774 u64 bofs = li->li_data[INDIRECT_INDEX];
775 level_t level;
776 int ret;
777 struct page *ipage;
778
779 if (!bofs)
780 return 0;
781 if (bix >= maxbix(li->li_height))
782 return 0;
783
784 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
785 ipage = logfs_get_read_page(inode, bix, level);
786 if (!ipage)
787 return -ENOMEM;
788
789 ret = logfs_segment_read(inode, ipage, bofs, bix, level);
790 if (ret) {
791 logfs_put_read_page(ipage);
792 return ret;
793 }
794
795 bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
796 logfs_put_read_page(ipage);
797 if (!bofs)
798 return 0;
799 }
800
801 return 1;
802 }
803
logfs_exist_block(struct inode * inode,u64 bix)804 int logfs_exist_block(struct inode *inode, u64 bix)
805 {
806 struct logfs_inode *li = logfs_inode(inode);
807
808 if (bix < I0_BLOCKS)
809 return !!li->li_data[bix];
810 return logfs_exist_loop(inode, bix);
811 }
812
seek_holedata_direct(struct inode * inode,u64 bix,int data)813 static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data)
814 {
815 struct logfs_inode *li = logfs_inode(inode);
816
817 for (; bix < I0_BLOCKS; bix++)
818 if (data ^ (li->li_data[bix] == 0))
819 return bix;
820 return I0_BLOCKS;
821 }
822
seek_holedata_loop(struct inode * inode,u64 bix,int data)823 static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data)
824 {
825 struct logfs_inode *li = logfs_inode(inode);
826 __be64 *rblock;
827 u64 increment, bofs = li->li_data[INDIRECT_INDEX];
828 level_t level;
829 int ret, slot;
830 struct page *page;
831
832 BUG_ON(!bofs);
833
834 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
835 increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1));
836 page = logfs_get_read_page(inode, bix, level);
837 if (!page)
838 return bix;
839
840 ret = logfs_segment_read(inode, page, bofs, bix, level);
841 if (ret) {
842 logfs_put_read_page(page);
843 return bix;
844 }
845
846 slot = get_bits(bix, SUBLEVEL(level));
847 rblock = kmap_atomic(page, KM_USER0);
848 while (slot < LOGFS_BLOCK_FACTOR) {
849 if (data && (rblock[slot] != 0))
850 break;
851 if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED))
852 break;
853 slot++;
854 bix += increment;
855 bix &= ~(increment - 1);
856 }
857 if (slot >= LOGFS_BLOCK_FACTOR) {
858 kunmap_atomic(rblock, KM_USER0);
859 logfs_put_read_page(page);
860 return bix;
861 }
862 bofs = be64_to_cpu(rblock[slot]);
863 kunmap_atomic(rblock, KM_USER0);
864 logfs_put_read_page(page);
865 if (!bofs) {
866 BUG_ON(data);
867 return bix;
868 }
869 }
870 return bix;
871 }
872
873 /**
874 * logfs_seek_hole - find next hole starting at a given block index
875 * @inode: inode to search in
876 * @bix: block index to start searching
877 *
878 * Returns next hole. If the file doesn't contain any further holes, the
879 * block address next to eof is returned instead.
880 */
logfs_seek_hole(struct inode * inode,u64 bix)881 u64 logfs_seek_hole(struct inode *inode, u64 bix)
882 {
883 struct logfs_inode *li = logfs_inode(inode);
884
885 if (bix < I0_BLOCKS) {
886 bix = seek_holedata_direct(inode, bix, 0);
887 if (bix < I0_BLOCKS)
888 return bix;
889 }
890
891 if (!li->li_data[INDIRECT_INDEX])
892 return bix;
893 else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED)
894 bix = maxbix(li->li_height);
895 else if (bix >= maxbix(li->li_height))
896 return bix;
897 else {
898 bix = seek_holedata_loop(inode, bix, 0);
899 if (bix < maxbix(li->li_height))
900 return bix;
901 /* Should not happen anymore. But if some port writes semi-
902 * corrupt images (as this one used to) we might run into it.
903 */
904 WARN_ON_ONCE(bix == maxbix(li->li_height));
905 }
906
907 return bix;
908 }
909
__logfs_seek_data(struct inode * inode,u64 bix)910 static u64 __logfs_seek_data(struct inode *inode, u64 bix)
911 {
912 struct logfs_inode *li = logfs_inode(inode);
913
914 if (bix < I0_BLOCKS) {
915 bix = seek_holedata_direct(inode, bix, 1);
916 if (bix < I0_BLOCKS)
917 return bix;
918 }
919
920 if (bix < maxbix(li->li_height)) {
921 if (!li->li_data[INDIRECT_INDEX])
922 bix = maxbix(li->li_height);
923 else
924 return seek_holedata_loop(inode, bix, 1);
925 }
926
927 return bix;
928 }
929
930 /**
931 * logfs_seek_data - find next data block after a given block index
932 * @inode: inode to search in
933 * @bix: block index to start searching
934 *
935 * Returns next data block. If the file doesn't contain any further data
936 * blocks, the last block in the file is returned instead.
937 */
logfs_seek_data(struct inode * inode,u64 bix)938 u64 logfs_seek_data(struct inode *inode, u64 bix)
939 {
940 struct super_block *sb = inode->i_sb;
941 u64 ret, end;
942
943 ret = __logfs_seek_data(inode, bix);
944 end = i_size_read(inode) >> sb->s_blocksize_bits;
945 if (ret >= end)
946 ret = max(bix, end);
947 return ret;
948 }
949
logfs_is_valid_direct(struct logfs_inode * li,u64 bix,u64 ofs)950 static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs)
951 {
952 return pure_ofs(li->li_data[bix]) == ofs;
953 }
954
__logfs_is_valid_loop(struct inode * inode,u64 bix,u64 ofs,u64 bofs)955 static int __logfs_is_valid_loop(struct inode *inode, u64 bix,
956 u64 ofs, u64 bofs)
957 {
958 struct logfs_inode *li = logfs_inode(inode);
959 level_t level;
960 int ret;
961 struct page *page;
962
963 for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){
964 page = logfs_get_write_page(inode, bix, level);
965 BUG_ON(!page);
966
967 ret = logfs_segment_read(inode, page, bofs, bix, level);
968 if (ret) {
969 logfs_put_write_page(page);
970 return 0;
971 }
972
973 bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level)));
974 logfs_put_write_page(page);
975 if (!bofs)
976 return 0;
977
978 if (pure_ofs(bofs) == ofs)
979 return 1;
980 }
981 return 0;
982 }
983
logfs_is_valid_loop(struct inode * inode,u64 bix,u64 ofs)984 static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs)
985 {
986 struct logfs_inode *li = logfs_inode(inode);
987 u64 bofs = li->li_data[INDIRECT_INDEX];
988
989 if (!bofs)
990 return 0;
991
992 if (bix >= maxbix(li->li_height))
993 return 0;
994
995 if (pure_ofs(bofs) == ofs)
996 return 1;
997
998 return __logfs_is_valid_loop(inode, bix, ofs, bofs);
999 }
1000
__logfs_is_valid_block(struct inode * inode,u64 bix,u64 ofs)1001 static int __logfs_is_valid_block(struct inode *inode, u64 bix, u64 ofs)
1002 {
1003 struct logfs_inode *li = logfs_inode(inode);
1004
1005 if ((inode->i_nlink == 0) && atomic_read(&inode->i_count) == 1)
1006 return 0;
1007
1008 if (bix < I0_BLOCKS)
1009 return logfs_is_valid_direct(li, bix, ofs);
1010 return logfs_is_valid_loop(inode, bix, ofs);
1011 }
1012
1013 /**
1014 * logfs_is_valid_block - check whether this block is still valid
1015 *
1016 * @sb - superblock
1017 * @ofs - block physical offset
1018 * @ino - block inode number
1019 * @bix - block index
1020 * @level - block level
1021 *
1022 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
1023 * become invalid once the journal is written.
1024 */
logfs_is_valid_block(struct super_block * sb,u64 ofs,u64 ino,u64 bix,gc_level_t gc_level)1025 int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
1026 gc_level_t gc_level)
1027 {
1028 struct logfs_super *super = logfs_super(sb);
1029 struct inode *inode;
1030 int ret, cookie;
1031
1032 /* Umount closes a segment with free blocks remaining. Those
1033 * blocks are by definition invalid. */
1034 if (ino == -1)
1035 return 0;
1036
1037 LOGFS_BUG_ON((u64)(u_long)ino != ino, sb);
1038
1039 inode = logfs_safe_iget(sb, ino, &cookie);
1040 if (IS_ERR(inode))
1041 goto invalid;
1042
1043 ret = __logfs_is_valid_block(inode, bix, ofs);
1044 logfs_safe_iput(inode, cookie);
1045 if (ret)
1046 return ret;
1047
1048 invalid:
1049 /* Block is nominally invalid, but may still sit in the shadow tree,
1050 * waiting for a journal commit.
1051 */
1052 if (btree_lookup64(&super->s_shadow_tree.old, ofs))
1053 return 2;
1054 return 0;
1055 }
1056
logfs_readpage_nolock(struct page * page)1057 int logfs_readpage_nolock(struct page *page)
1058 {
1059 struct inode *inode = page->mapping->host;
1060 int ret = -EIO;
1061
1062 ret = logfs_read_block(inode, page, READ);
1063
1064 if (ret) {
1065 ClearPageUptodate(page);
1066 SetPageError(page);
1067 } else {
1068 SetPageUptodate(page);
1069 ClearPageError(page);
1070 }
1071 flush_dcache_page(page);
1072
1073 return ret;
1074 }
1075
logfs_reserve_bytes(struct inode * inode,int bytes)1076 static int logfs_reserve_bytes(struct inode *inode, int bytes)
1077 {
1078 struct logfs_super *super = logfs_super(inode->i_sb);
1079 u64 available = super->s_free_bytes + super->s_dirty_free_bytes
1080 - super->s_dirty_used_bytes - super->s_dirty_pages;
1081
1082 if (!bytes)
1083 return 0;
1084
1085 if (available < bytes)
1086 return -ENOSPC;
1087
1088 if (available < bytes + super->s_root_reserve &&
1089 !capable(CAP_SYS_RESOURCE))
1090 return -ENOSPC;
1091
1092 return 0;
1093 }
1094
get_page_reserve(struct inode * inode,struct page * page)1095 int get_page_reserve(struct inode *inode, struct page *page)
1096 {
1097 struct logfs_super *super = logfs_super(inode->i_sb);
1098 struct logfs_block *block = logfs_block(page);
1099 int ret;
1100
1101 if (block && block->reserved_bytes)
1102 return 0;
1103
1104 logfs_get_wblocks(inode->i_sb, page, WF_LOCK);
1105 while ((ret = logfs_reserve_bytes(inode, 6 * LOGFS_MAX_OBJECTSIZE)) &&
1106 !list_empty(&super->s_writeback_list)) {
1107 block = list_entry(super->s_writeback_list.next,
1108 struct logfs_block, alias_list);
1109 block->ops->write_block(block);
1110 }
1111 if (!ret) {
1112 alloc_data_block(inode, page);
1113 block = logfs_block(page);
1114 block->reserved_bytes += 6 * LOGFS_MAX_OBJECTSIZE;
1115 super->s_dirty_pages += 6 * LOGFS_MAX_OBJECTSIZE;
1116 list_move_tail(&block->alias_list, &super->s_writeback_list);
1117 }
1118 logfs_put_wblocks(inode->i_sb, page, WF_LOCK);
1119 return ret;
1120 }
1121
1122 /*
1123 * We are protected by write lock. Push victims up to superblock level
1124 * and release transaction when appropriate.
1125 */
1126 /* FIXME: This is currently called from the wrong spots. */
logfs_handle_transaction(struct inode * inode,struct logfs_transaction * ta)1127 static void logfs_handle_transaction(struct inode *inode,
1128 struct logfs_transaction *ta)
1129 {
1130 struct logfs_super *super = logfs_super(inode->i_sb);
1131
1132 if (!ta)
1133 return;
1134 logfs_inode(inode)->li_block->ta = NULL;
1135
1136 if (inode->i_ino != LOGFS_INO_MASTER) {
1137 BUG(); /* FIXME: Yes, this needs more thought */
1138 /* just remember the transaction until inode is written */
1139 //BUG_ON(logfs_inode(inode)->li_transaction);
1140 //logfs_inode(inode)->li_transaction = ta;
1141 return;
1142 }
1143
1144 switch (ta->state) {
1145 case CREATE_1: /* fall through */
1146 case UNLINK_1:
1147 BUG_ON(super->s_victim_ino);
1148 super->s_victim_ino = ta->ino;
1149 break;
1150 case CREATE_2: /* fall through */
1151 case UNLINK_2:
1152 BUG_ON(super->s_victim_ino != ta->ino);
1153 super->s_victim_ino = 0;
1154 /* transaction ends here - free it */
1155 kfree(ta);
1156 break;
1157 case CROSS_RENAME_1:
1158 BUG_ON(super->s_rename_dir);
1159 BUG_ON(super->s_rename_pos);
1160 super->s_rename_dir = ta->dir;
1161 super->s_rename_pos = ta->pos;
1162 break;
1163 case CROSS_RENAME_2:
1164 BUG_ON(super->s_rename_dir != ta->dir);
1165 BUG_ON(super->s_rename_pos != ta->pos);
1166 super->s_rename_dir = 0;
1167 super->s_rename_pos = 0;
1168 kfree(ta);
1169 break;
1170 case TARGET_RENAME_1:
1171 BUG_ON(super->s_rename_dir);
1172 BUG_ON(super->s_rename_pos);
1173 BUG_ON(super->s_victim_ino);
1174 super->s_rename_dir = ta->dir;
1175 super->s_rename_pos = ta->pos;
1176 super->s_victim_ino = ta->ino;
1177 break;
1178 case TARGET_RENAME_2:
1179 BUG_ON(super->s_rename_dir != ta->dir);
1180 BUG_ON(super->s_rename_pos != ta->pos);
1181 BUG_ON(super->s_victim_ino != ta->ino);
1182 super->s_rename_dir = 0;
1183 super->s_rename_pos = 0;
1184 break;
1185 case TARGET_RENAME_3:
1186 BUG_ON(super->s_rename_dir);
1187 BUG_ON(super->s_rename_pos);
1188 BUG_ON(super->s_victim_ino != ta->ino);
1189 super->s_victim_ino = 0;
1190 kfree(ta);
1191 break;
1192 default:
1193 BUG();
1194 }
1195 }
1196
1197 /*
1198 * Not strictly a reservation, but rather a check that we still have enough
1199 * space to satisfy the write.
1200 */
logfs_reserve_blocks(struct inode * inode,int blocks)1201 static int logfs_reserve_blocks(struct inode *inode, int blocks)
1202 {
1203 return logfs_reserve_bytes(inode, blocks * LOGFS_MAX_OBJECTSIZE);
1204 }
1205
1206 struct write_control {
1207 u64 ofs;
1208 long flags;
1209 };
1210
alloc_shadow(struct inode * inode,u64 bix,level_t level,u64 old_ofs)1211 static struct logfs_shadow *alloc_shadow(struct inode *inode, u64 bix,
1212 level_t level, u64 old_ofs)
1213 {
1214 struct logfs_super *super = logfs_super(inode->i_sb);
1215 struct logfs_shadow *shadow;
1216
1217 shadow = mempool_alloc(super->s_shadow_pool, GFP_NOFS);
1218 memset(shadow, 0, sizeof(*shadow));
1219 shadow->ino = inode->i_ino;
1220 shadow->bix = bix;
1221 shadow->gc_level = expand_level(inode->i_ino, level);
1222 shadow->old_ofs = old_ofs & ~LOGFS_FULLY_POPULATED;
1223 return shadow;
1224 }
1225
free_shadow(struct inode * inode,struct logfs_shadow * shadow)1226 static void free_shadow(struct inode *inode, struct logfs_shadow *shadow)
1227 {
1228 struct logfs_super *super = logfs_super(inode->i_sb);
1229
1230 mempool_free(shadow, super->s_shadow_pool);
1231 }
1232
mark_segment(struct shadow_tree * tree,u32 segno)1233 static void mark_segment(struct shadow_tree *tree, u32 segno)
1234 {
1235 int err;
1236
1237 if (!btree_lookup32(&tree->segment_map, segno)) {
1238 err = btree_insert32(&tree->segment_map, segno, (void *)1,
1239 GFP_NOFS);
1240 BUG_ON(err);
1241 tree->no_shadowed_segments++;
1242 }
1243 }
1244
1245 /**
1246 * fill_shadow_tree - Propagate shadow tree changes due to a write
1247 * @inode: Inode owning the page
1248 * @page: Struct page that was written
1249 * @shadow: Shadow for the current write
1250 *
1251 * Writes in logfs can result in two semi-valid objects. The old object
1252 * is still valid as long as it can be reached by following pointers on
1253 * the medium. Only when writes propagate all the way up to the journal
1254 * has the new object safely replaced the old one.
1255 *
1256 * To handle this problem, a struct logfs_shadow is used to represent
1257 * every single write. It is attached to the indirect block, which is
1258 * marked dirty. When the indirect block is written, its shadows are
1259 * handed up to the next indirect block (or inode). Untimately they
1260 * will reach the master inode and be freed upon journal commit.
1261 *
1262 * This function handles a single step in the propagation. It adds the
1263 * shadow for the current write to the tree, along with any shadows in
1264 * the page's tree, in case it was an indirect block. If a page is
1265 * written, the inode parameter is left NULL, if an inode is written,
1266 * the page parameter is left NULL.
1267 */
fill_shadow_tree(struct inode * inode,struct page * page,struct logfs_shadow * shadow)1268 static void fill_shadow_tree(struct inode *inode, struct page *page,
1269 struct logfs_shadow *shadow)
1270 {
1271 struct logfs_super *super = logfs_super(inode->i_sb);
1272 struct logfs_block *block = logfs_block(page);
1273 struct shadow_tree *tree = &super->s_shadow_tree;
1274
1275 if (PagePrivate(page)) {
1276 if (block->alias_map)
1277 super->s_no_object_aliases -= bitmap_weight(
1278 block->alias_map, LOGFS_BLOCK_FACTOR);
1279 logfs_handle_transaction(inode, block->ta);
1280 block->ops->free_block(inode->i_sb, block);
1281 }
1282 if (shadow) {
1283 if (shadow->old_ofs)
1284 btree_insert64(&tree->old, shadow->old_ofs, shadow,
1285 GFP_NOFS);
1286 else
1287 btree_insert64(&tree->new, shadow->new_ofs, shadow,
1288 GFP_NOFS);
1289
1290 super->s_dirty_used_bytes += shadow->new_len;
1291 super->s_dirty_free_bytes += shadow->old_len;
1292 mark_segment(tree, shadow->old_ofs >> super->s_segshift);
1293 mark_segment(tree, shadow->new_ofs >> super->s_segshift);
1294 }
1295 }
1296
logfs_set_alias(struct super_block * sb,struct logfs_block * block,long child_no)1297 static void logfs_set_alias(struct super_block *sb, struct logfs_block *block,
1298 long child_no)
1299 {
1300 struct logfs_super *super = logfs_super(sb);
1301
1302 if (block->inode && block->inode->i_ino == LOGFS_INO_MASTER) {
1303 /* Aliases in the master inode are pointless. */
1304 return;
1305 }
1306
1307 if (!test_bit(child_no, block->alias_map)) {
1308 set_bit(child_no, block->alias_map);
1309 super->s_no_object_aliases++;
1310 }
1311 list_move_tail(&block->alias_list, &super->s_object_alias);
1312 }
1313
1314 /*
1315 * Object aliases can and often do change the size and occupied space of a
1316 * file. So not only do we have to change the pointers, we also have to
1317 * change inode->i_size and li->li_used_bytes. Which is done by setting
1318 * another two object aliases for the inode itself.
1319 */
set_iused(struct inode * inode,struct logfs_shadow * shadow)1320 static void set_iused(struct inode *inode, struct logfs_shadow *shadow)
1321 {
1322 struct logfs_inode *li = logfs_inode(inode);
1323
1324 if (shadow->new_len == shadow->old_len)
1325 return;
1326
1327 alloc_inode_block(inode);
1328 li->li_used_bytes += shadow->new_len - shadow->old_len;
1329 __logfs_set_blocks(inode);
1330 logfs_set_alias(inode->i_sb, li->li_block, INODE_USED_OFS);
1331 logfs_set_alias(inode->i_sb, li->li_block, INODE_SIZE_OFS);
1332 }
1333
logfs_write_i0(struct inode * inode,struct page * page,struct write_control * wc)1334 static int logfs_write_i0(struct inode *inode, struct page *page,
1335 struct write_control *wc)
1336 {
1337 struct logfs_shadow *shadow;
1338 u64 bix;
1339 level_t level;
1340 int full, err = 0;
1341
1342 logfs_unpack_index(page->index, &bix, &level);
1343 if (wc->ofs == 0)
1344 if (logfs_reserve_blocks(inode, 1))
1345 return -ENOSPC;
1346
1347 shadow = alloc_shadow(inode, bix, level, wc->ofs);
1348 if (wc->flags & WF_WRITE)
1349 err = logfs_segment_write(inode, page, shadow);
1350 if (wc->flags & WF_DELETE)
1351 logfs_segment_delete(inode, shadow);
1352 if (err) {
1353 free_shadow(inode, shadow);
1354 return err;
1355 }
1356
1357 set_iused(inode, shadow);
1358 full = 1;
1359 if (level != 0) {
1360 alloc_indirect_block(inode, page, 0);
1361 full = logfs_block(page)->full == LOGFS_BLOCK_FACTOR;
1362 }
1363 fill_shadow_tree(inode, page, shadow);
1364 wc->ofs = shadow->new_ofs;
1365 if (wc->ofs && full)
1366 wc->ofs |= LOGFS_FULLY_POPULATED;
1367 return 0;
1368 }
1369
logfs_write_direct(struct inode * inode,struct page * page,long flags)1370 static int logfs_write_direct(struct inode *inode, struct page *page,
1371 long flags)
1372 {
1373 struct logfs_inode *li = logfs_inode(inode);
1374 struct write_control wc = {
1375 .ofs = li->li_data[page->index],
1376 .flags = flags,
1377 };
1378 int err;
1379
1380 alloc_inode_block(inode);
1381
1382 err = logfs_write_i0(inode, page, &wc);
1383 if (err)
1384 return err;
1385
1386 li->li_data[page->index] = wc.ofs;
1387 logfs_set_alias(inode->i_sb, li->li_block,
1388 page->index + INODE_POINTER_OFS);
1389 return 0;
1390 }
1391
ptr_change(u64 ofs,struct page * page)1392 static int ptr_change(u64 ofs, struct page *page)
1393 {
1394 struct logfs_block *block = logfs_block(page);
1395 int empty0, empty1, full0, full1;
1396
1397 empty0 = ofs == 0;
1398 empty1 = block->partial == 0;
1399 if (empty0 != empty1)
1400 return 1;
1401
1402 /* The !! is necessary to shrink result to int */
1403 full0 = !!(ofs & LOGFS_FULLY_POPULATED);
1404 full1 = block->full == LOGFS_BLOCK_FACTOR;
1405 if (full0 != full1)
1406 return 1;
1407 return 0;
1408 }
1409
__logfs_write_rec(struct inode * inode,struct page * page,struct write_control * this_wc,pgoff_t bix,level_t target_level,level_t level)1410 static int __logfs_write_rec(struct inode *inode, struct page *page,
1411 struct write_control *this_wc,
1412 pgoff_t bix, level_t target_level, level_t level)
1413 {
1414 int ret, page_empty = 0;
1415 int child_no = get_bits(bix, SUBLEVEL(level));
1416 struct page *ipage;
1417 struct write_control child_wc = {
1418 .flags = this_wc->flags,
1419 };
1420
1421 ipage = logfs_get_write_page(inode, bix, level);
1422 if (!ipage)
1423 return -ENOMEM;
1424
1425 if (this_wc->ofs) {
1426 ret = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
1427 if (ret)
1428 goto out;
1429 } else if (!PageUptodate(ipage)) {
1430 page_empty = 1;
1431 logfs_read_empty(ipage);
1432 }
1433
1434 child_wc.ofs = block_get_pointer(ipage, child_no);
1435
1436 if ((__force u8)level-1 > (__force u8)target_level)
1437 ret = __logfs_write_rec(inode, page, &child_wc, bix,
1438 target_level, SUBLEVEL(level));
1439 else
1440 ret = logfs_write_i0(inode, page, &child_wc);
1441
1442 if (ret)
1443 goto out;
1444
1445 alloc_indirect_block(inode, ipage, page_empty);
1446 block_set_pointer(ipage, child_no, child_wc.ofs);
1447 /* FIXME: first condition seems superfluous */
1448 if (child_wc.ofs || logfs_block(ipage)->partial)
1449 this_wc->flags |= WF_WRITE;
1450 /* the condition on this_wc->ofs ensures that we won't consume extra
1451 * space for indirect blocks in the future, which we cannot reserve */
1452 if (!this_wc->ofs || ptr_change(this_wc->ofs, ipage))
1453 ret = logfs_write_i0(inode, ipage, this_wc);
1454 else
1455 logfs_set_alias(inode->i_sb, logfs_block(ipage), child_no);
1456 out:
1457 logfs_put_write_page(ipage);
1458 return ret;
1459 }
1460
logfs_write_rec(struct inode * inode,struct page * page,pgoff_t bix,level_t target_level,long flags)1461 static int logfs_write_rec(struct inode *inode, struct page *page,
1462 pgoff_t bix, level_t target_level, long flags)
1463 {
1464 struct logfs_inode *li = logfs_inode(inode);
1465 struct write_control wc = {
1466 .ofs = li->li_data[INDIRECT_INDEX],
1467 .flags = flags,
1468 };
1469 int ret;
1470
1471 alloc_inode_block(inode);
1472
1473 if (li->li_height > (__force u8)target_level)
1474 ret = __logfs_write_rec(inode, page, &wc, bix, target_level,
1475 LEVEL(li->li_height));
1476 else
1477 ret = logfs_write_i0(inode, page, &wc);
1478 if (ret)
1479 return ret;
1480
1481 if (li->li_data[INDIRECT_INDEX] != wc.ofs) {
1482 li->li_data[INDIRECT_INDEX] = wc.ofs;
1483 logfs_set_alias(inode->i_sb, li->li_block,
1484 INDIRECT_INDEX + INODE_POINTER_OFS);
1485 }
1486 return ret;
1487 }
1488
logfs_add_transaction(struct inode * inode,struct logfs_transaction * ta)1489 void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta)
1490 {
1491 alloc_inode_block(inode);
1492 logfs_inode(inode)->li_block->ta = ta;
1493 }
1494
logfs_del_transaction(struct inode * inode,struct logfs_transaction * ta)1495 void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta)
1496 {
1497 struct logfs_block *block = logfs_inode(inode)->li_block;
1498
1499 if (block && block->ta)
1500 block->ta = NULL;
1501 }
1502
grow_inode(struct inode * inode,u64 bix,level_t level)1503 static int grow_inode(struct inode *inode, u64 bix, level_t level)
1504 {
1505 struct logfs_inode *li = logfs_inode(inode);
1506 u8 height = (__force u8)level;
1507 struct page *page;
1508 struct write_control wc = {
1509 .flags = WF_WRITE,
1510 };
1511 int err;
1512
1513 BUG_ON(height > 5 || li->li_height > 5);
1514 while (height > li->li_height || bix >= maxbix(li->li_height)) {
1515 page = logfs_get_write_page(inode, I0_BLOCKS + 1,
1516 LEVEL(li->li_height + 1));
1517 if (!page)
1518 return -ENOMEM;
1519 logfs_read_empty(page);
1520 alloc_indirect_block(inode, page, 1);
1521 block_set_pointer(page, 0, li->li_data[INDIRECT_INDEX]);
1522 err = logfs_write_i0(inode, page, &wc);
1523 logfs_put_write_page(page);
1524 if (err)
1525 return err;
1526 li->li_data[INDIRECT_INDEX] = wc.ofs;
1527 wc.ofs = 0;
1528 li->li_height++;
1529 logfs_set_alias(inode->i_sb, li->li_block, INODE_HEIGHT_OFS);
1530 }
1531 return 0;
1532 }
1533
__logfs_write_buf(struct inode * inode,struct page * page,long flags)1534 static int __logfs_write_buf(struct inode *inode, struct page *page, long flags)
1535 {
1536 struct logfs_super *super = logfs_super(inode->i_sb);
1537 pgoff_t index = page->index;
1538 u64 bix;
1539 level_t level;
1540 int err;
1541
1542 flags |= WF_WRITE | WF_DELETE;
1543 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1544
1545 logfs_unpack_index(index, &bix, &level);
1546 if (logfs_block(page) && logfs_block(page)->reserved_bytes)
1547 super->s_dirty_pages -= logfs_block(page)->reserved_bytes;
1548
1549 if (index < I0_BLOCKS)
1550 return logfs_write_direct(inode, page, flags);
1551
1552 bix = adjust_bix(bix, level);
1553 err = grow_inode(inode, bix, level);
1554 if (err)
1555 return err;
1556 return logfs_write_rec(inode, page, bix, level, flags);
1557 }
1558
logfs_write_buf(struct inode * inode,struct page * page,long flags)1559 int logfs_write_buf(struct inode *inode, struct page *page, long flags)
1560 {
1561 struct super_block *sb = inode->i_sb;
1562 int ret;
1563
1564 logfs_get_wblocks(sb, page, flags & WF_LOCK);
1565 ret = __logfs_write_buf(inode, page, flags);
1566 logfs_put_wblocks(sb, page, flags & WF_LOCK);
1567 return ret;
1568 }
1569
__logfs_delete(struct inode * inode,struct page * page)1570 static int __logfs_delete(struct inode *inode, struct page *page)
1571 {
1572 long flags = WF_DELETE;
1573
1574 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1575
1576 if (page->index < I0_BLOCKS)
1577 return logfs_write_direct(inode, page, flags);
1578 return logfs_write_rec(inode, page, page->index, 0, flags);
1579 }
1580
logfs_delete(struct inode * inode,pgoff_t index,struct shadow_tree * shadow_tree)1581 int logfs_delete(struct inode *inode, pgoff_t index,
1582 struct shadow_tree *shadow_tree)
1583 {
1584 struct super_block *sb = inode->i_sb;
1585 struct page *page;
1586 int ret;
1587
1588 page = logfs_get_read_page(inode, index, 0);
1589 if (!page)
1590 return -ENOMEM;
1591
1592 logfs_get_wblocks(sb, page, 1);
1593 ret = __logfs_delete(inode, page);
1594 logfs_put_wblocks(sb, page, 1);
1595
1596 logfs_put_read_page(page);
1597
1598 return ret;
1599 }
1600
logfs_rewrite_block(struct inode * inode,u64 bix,u64 ofs,gc_level_t gc_level,long flags)1601 int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
1602 gc_level_t gc_level, long flags)
1603 {
1604 level_t level = shrink_level(gc_level);
1605 struct page *page;
1606 int err;
1607
1608 page = logfs_get_write_page(inode, bix, level);
1609 if (!page)
1610 return -ENOMEM;
1611
1612 err = logfs_segment_read(inode, page, ofs, bix, level);
1613 if (!err) {
1614 if (level != 0)
1615 alloc_indirect_block(inode, page, 0);
1616 err = logfs_write_buf(inode, page, flags);
1617 if (!err && shrink_level(gc_level) == 0) {
1618 /* Rewrite cannot mark the inode dirty but has to
1619 * write it immediately.
1620 * Q: Can't we just create an alias for the inode
1621 * instead? And if not, why not?
1622 */
1623 if (inode->i_ino == LOGFS_INO_MASTER)
1624 logfs_write_anchor(inode->i_sb);
1625 else {
1626 err = __logfs_write_inode(inode, flags);
1627 }
1628 }
1629 }
1630 logfs_put_write_page(page);
1631 return err;
1632 }
1633
truncate_data_block(struct inode * inode,struct page * page,u64 ofs,struct logfs_shadow * shadow,u64 size)1634 static int truncate_data_block(struct inode *inode, struct page *page,
1635 u64 ofs, struct logfs_shadow *shadow, u64 size)
1636 {
1637 loff_t pageofs = page->index << inode->i_sb->s_blocksize_bits;
1638 u64 bix;
1639 level_t level;
1640 int err;
1641
1642 /* Does truncation happen within this page? */
1643 if (size <= pageofs || size - pageofs >= PAGE_SIZE)
1644 return 0;
1645
1646 logfs_unpack_index(page->index, &bix, &level);
1647 BUG_ON(level != 0);
1648
1649 err = logfs_segment_read(inode, page, ofs, bix, level);
1650 if (err)
1651 return err;
1652
1653 zero_user_segment(page, size - pageofs, PAGE_CACHE_SIZE);
1654 return logfs_segment_write(inode, page, shadow);
1655 }
1656
logfs_truncate_i0(struct inode * inode,struct page * page,struct write_control * wc,u64 size)1657 static int logfs_truncate_i0(struct inode *inode, struct page *page,
1658 struct write_control *wc, u64 size)
1659 {
1660 struct logfs_shadow *shadow;
1661 u64 bix;
1662 level_t level;
1663 int err = 0;
1664
1665 logfs_unpack_index(page->index, &bix, &level);
1666 BUG_ON(level != 0);
1667 shadow = alloc_shadow(inode, bix, level, wc->ofs);
1668
1669 err = truncate_data_block(inode, page, wc->ofs, shadow, size);
1670 if (err) {
1671 free_shadow(inode, shadow);
1672 return err;
1673 }
1674
1675 logfs_segment_delete(inode, shadow);
1676 set_iused(inode, shadow);
1677 fill_shadow_tree(inode, page, shadow);
1678 wc->ofs = shadow->new_ofs;
1679 return 0;
1680 }
1681
logfs_truncate_direct(struct inode * inode,u64 size)1682 static int logfs_truncate_direct(struct inode *inode, u64 size)
1683 {
1684 struct logfs_inode *li = logfs_inode(inode);
1685 struct write_control wc;
1686 struct page *page;
1687 int e;
1688 int err;
1689
1690 alloc_inode_block(inode);
1691
1692 for (e = I0_BLOCKS - 1; e >= 0; e--) {
1693 if (size > (e+1) * LOGFS_BLOCKSIZE)
1694 break;
1695
1696 wc.ofs = li->li_data[e];
1697 if (!wc.ofs)
1698 continue;
1699
1700 page = logfs_get_write_page(inode, e, 0);
1701 if (!page)
1702 return -ENOMEM;
1703 err = logfs_segment_read(inode, page, wc.ofs, e, 0);
1704 if (err) {
1705 logfs_put_write_page(page);
1706 return err;
1707 }
1708 err = logfs_truncate_i0(inode, page, &wc, size);
1709 logfs_put_write_page(page);
1710 if (err)
1711 return err;
1712
1713 li->li_data[e] = wc.ofs;
1714 }
1715 return 0;
1716 }
1717
1718 /* FIXME: these need to become per-sb once we support different blocksizes */
1719 static u64 __logfs_step[] = {
1720 1,
1721 I1_BLOCKS,
1722 I2_BLOCKS,
1723 I3_BLOCKS,
1724 };
1725
1726 static u64 __logfs_start_index[] = {
1727 I0_BLOCKS,
1728 I1_BLOCKS,
1729 I2_BLOCKS,
1730 I3_BLOCKS
1731 };
1732
logfs_step(level_t level)1733 static inline u64 logfs_step(level_t level)
1734 {
1735 return __logfs_step[(__force u8)level];
1736 }
1737
logfs_factor(u8 level)1738 static inline u64 logfs_factor(u8 level)
1739 {
1740 return __logfs_step[level] * LOGFS_BLOCKSIZE;
1741 }
1742
logfs_start_index(level_t level)1743 static inline u64 logfs_start_index(level_t level)
1744 {
1745 return __logfs_start_index[(__force u8)level];
1746 }
1747
logfs_unpack_raw_index(pgoff_t index,u64 * bix,level_t * level)1748 static void logfs_unpack_raw_index(pgoff_t index, u64 *bix, level_t *level)
1749 {
1750 logfs_unpack_index(index, bix, level);
1751 if (*bix <= logfs_start_index(SUBLEVEL(*level)))
1752 *bix = 0;
1753 }
1754
__logfs_truncate_rec(struct inode * inode,struct page * ipage,struct write_control * this_wc,u64 size)1755 static int __logfs_truncate_rec(struct inode *inode, struct page *ipage,
1756 struct write_control *this_wc, u64 size)
1757 {
1758 int truncate_happened = 0;
1759 int e, err = 0;
1760 u64 bix, child_bix, next_bix;
1761 level_t level;
1762 struct page *page;
1763 struct write_control child_wc = { /* FIXME: flags */ };
1764
1765 logfs_unpack_raw_index(ipage->index, &bix, &level);
1766 err = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
1767 if (err)
1768 return err;
1769
1770 for (e = LOGFS_BLOCK_FACTOR - 1; e >= 0; e--) {
1771 child_bix = bix + e * logfs_step(SUBLEVEL(level));
1772 next_bix = child_bix + logfs_step(SUBLEVEL(level));
1773 if (size > next_bix * LOGFS_BLOCKSIZE)
1774 break;
1775
1776 child_wc.ofs = pure_ofs(block_get_pointer(ipage, e));
1777 if (!child_wc.ofs)
1778 continue;
1779
1780 page = logfs_get_write_page(inode, child_bix, SUBLEVEL(level));
1781 if (!page)
1782 return -ENOMEM;
1783
1784 if ((__force u8)level > 1)
1785 err = __logfs_truncate_rec(inode, page, &child_wc, size);
1786 else
1787 err = logfs_truncate_i0(inode, page, &child_wc, size);
1788 logfs_put_write_page(page);
1789 if (err)
1790 return err;
1791
1792 truncate_happened = 1;
1793 alloc_indirect_block(inode, ipage, 0);
1794 block_set_pointer(ipage, e, child_wc.ofs);
1795 }
1796
1797 if (!truncate_happened) {
1798 printk("ineffectual truncate (%lx, %lx, %llx)\n", inode->i_ino, ipage->index, size);
1799 return 0;
1800 }
1801
1802 this_wc->flags = WF_DELETE;
1803 if (logfs_block(ipage)->partial)
1804 this_wc->flags |= WF_WRITE;
1805
1806 return logfs_write_i0(inode, ipage, this_wc);
1807 }
1808
logfs_truncate_rec(struct inode * inode,u64 size)1809 static int logfs_truncate_rec(struct inode *inode, u64 size)
1810 {
1811 struct logfs_inode *li = logfs_inode(inode);
1812 struct write_control wc = {
1813 .ofs = li->li_data[INDIRECT_INDEX],
1814 };
1815 struct page *page;
1816 int err;
1817
1818 alloc_inode_block(inode);
1819
1820 if (!wc.ofs)
1821 return 0;
1822
1823 page = logfs_get_write_page(inode, 0, LEVEL(li->li_height));
1824 if (!page)
1825 return -ENOMEM;
1826
1827 err = __logfs_truncate_rec(inode, page, &wc, size);
1828 logfs_put_write_page(page);
1829 if (err)
1830 return err;
1831
1832 if (li->li_data[INDIRECT_INDEX] != wc.ofs)
1833 li->li_data[INDIRECT_INDEX] = wc.ofs;
1834 return 0;
1835 }
1836
__logfs_truncate(struct inode * inode,u64 size)1837 static int __logfs_truncate(struct inode *inode, u64 size)
1838 {
1839 int ret;
1840
1841 if (size >= logfs_factor(logfs_inode(inode)->li_height))
1842 return 0;
1843
1844 ret = logfs_truncate_rec(inode, size);
1845 if (ret)
1846 return ret;
1847
1848 return logfs_truncate_direct(inode, size);
1849 }
1850
1851 /*
1852 * Truncate, by changing the segment file, can consume a fair amount
1853 * of resources. So back off from time to time and do some GC.
1854 * 8 or 2048 blocks should be well within safety limits even if
1855 * every single block resided in a different segment.
1856 */
1857 #define TRUNCATE_STEP (8 * 1024 * 1024)
logfs_truncate(struct inode * inode,u64 target)1858 int logfs_truncate(struct inode *inode, u64 target)
1859 {
1860 struct super_block *sb = inode->i_sb;
1861 u64 size = i_size_read(inode);
1862 int err = 0;
1863
1864 size = ALIGN(size, TRUNCATE_STEP);
1865 while (size > target) {
1866 if (size > TRUNCATE_STEP)
1867 size -= TRUNCATE_STEP;
1868 else
1869 size = 0;
1870 if (size < target)
1871 size = target;
1872
1873 logfs_get_wblocks(sb, NULL, 1);
1874 err = __logfs_truncate(inode, size);
1875 if (!err)
1876 err = __logfs_write_inode(inode, 0);
1877 logfs_put_wblocks(sb, NULL, 1);
1878 }
1879
1880 if (!err)
1881 err = vmtruncate(inode, target);
1882
1883 /* I don't trust error recovery yet. */
1884 WARN_ON(err);
1885 return err;
1886 }
1887
move_page_to_inode(struct inode * inode,struct page * page)1888 static void move_page_to_inode(struct inode *inode, struct page *page)
1889 {
1890 struct logfs_inode *li = logfs_inode(inode);
1891 struct logfs_block *block = logfs_block(page);
1892
1893 if (!block)
1894 return;
1895
1896 log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
1897 block->ino, block->bix, block->level);
1898 BUG_ON(li->li_block);
1899 block->ops = &inode_block_ops;
1900 block->inode = inode;
1901 li->li_block = block;
1902
1903 block->page = NULL;
1904 page->private = 0;
1905 ClearPagePrivate(page);
1906 }
1907
move_inode_to_page(struct page * page,struct inode * inode)1908 static void move_inode_to_page(struct page *page, struct inode *inode)
1909 {
1910 struct logfs_inode *li = logfs_inode(inode);
1911 struct logfs_block *block = li->li_block;
1912
1913 if (!block)
1914 return;
1915
1916 log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
1917 block->ino, block->bix, block->level);
1918 BUG_ON(PagePrivate(page));
1919 block->ops = &indirect_block_ops;
1920 block->page = page;
1921 page->private = (unsigned long)block;
1922 SetPagePrivate(page);
1923
1924 block->inode = NULL;
1925 li->li_block = NULL;
1926 }
1927
logfs_read_inode(struct inode * inode)1928 int logfs_read_inode(struct inode *inode)
1929 {
1930 struct super_block *sb = inode->i_sb;
1931 struct logfs_super *super = logfs_super(sb);
1932 struct inode *master_inode = super->s_master_inode;
1933 struct page *page;
1934 struct logfs_disk_inode *di;
1935 u64 ino = inode->i_ino;
1936
1937 if (ino << sb->s_blocksize_bits > i_size_read(master_inode))
1938 return -ENODATA;
1939 if (!logfs_exist_block(master_inode, ino))
1940 return -ENODATA;
1941
1942 page = read_cache_page(master_inode->i_mapping, ino,
1943 (filler_t *)logfs_readpage, NULL);
1944 if (IS_ERR(page))
1945 return PTR_ERR(page);
1946
1947 di = kmap_atomic(page, KM_USER0);
1948 logfs_disk_to_inode(di, inode);
1949 kunmap_atomic(di, KM_USER0);
1950 move_page_to_inode(inode, page);
1951 page_cache_release(page);
1952 return 0;
1953 }
1954
1955 /* Caller must logfs_put_write_page(page); */
inode_to_page(struct inode * inode)1956 static struct page *inode_to_page(struct inode *inode)
1957 {
1958 struct inode *master_inode = logfs_super(inode->i_sb)->s_master_inode;
1959 struct logfs_disk_inode *di;
1960 struct page *page;
1961
1962 BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
1963
1964 page = logfs_get_write_page(master_inode, inode->i_ino, 0);
1965 if (!page)
1966 return NULL;
1967
1968 di = kmap_atomic(page, KM_USER0);
1969 logfs_inode_to_disk(inode, di);
1970 kunmap_atomic(di, KM_USER0);
1971 move_inode_to_page(page, inode);
1972 return page;
1973 }
1974
do_write_inode(struct inode * inode)1975 static int do_write_inode(struct inode *inode)
1976 {
1977 struct super_block *sb = inode->i_sb;
1978 struct inode *master_inode = logfs_super(sb)->s_master_inode;
1979 loff_t size = (inode->i_ino + 1) << inode->i_sb->s_blocksize_bits;
1980 struct page *page;
1981 int err;
1982
1983 BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
1984 /* FIXME: lock inode */
1985
1986 if (i_size_read(master_inode) < size)
1987 i_size_write(master_inode, size);
1988
1989 /* TODO: Tell vfs this inode is clean now */
1990
1991 page = inode_to_page(inode);
1992 if (!page)
1993 return -ENOMEM;
1994
1995 /* FIXME: transaction is part of logfs_block now. Is that enough? */
1996 err = logfs_write_buf(master_inode, page, 0);
1997 if (err)
1998 move_page_to_inode(inode, page);
1999
2000 logfs_put_write_page(page);
2001 return err;
2002 }
2003
logfs_mod_segment_entry(struct super_block * sb,u32 segno,int write,void (* change_se)(struct logfs_segment_entry *,long),long arg)2004 static void logfs_mod_segment_entry(struct super_block *sb, u32 segno,
2005 int write,
2006 void (*change_se)(struct logfs_segment_entry *, long),
2007 long arg)
2008 {
2009 struct logfs_super *super = logfs_super(sb);
2010 struct inode *inode;
2011 struct page *page;
2012 struct logfs_segment_entry *se;
2013 pgoff_t page_no;
2014 int child_no;
2015
2016 page_no = segno >> (sb->s_blocksize_bits - 3);
2017 child_no = segno & ((sb->s_blocksize >> 3) - 1);
2018
2019 inode = super->s_segfile_inode;
2020 page = logfs_get_write_page(inode, page_no, 0);
2021 BUG_ON(!page); /* FIXME: We need some reserve page for this case */
2022 if (!PageUptodate(page))
2023 logfs_read_block(inode, page, WRITE);
2024
2025 if (write)
2026 alloc_indirect_block(inode, page, 0);
2027 se = kmap_atomic(page, KM_USER0);
2028 change_se(se + child_no, arg);
2029 if (write) {
2030 logfs_set_alias(sb, logfs_block(page), child_no);
2031 BUG_ON((int)be32_to_cpu(se[child_no].valid) > super->s_segsize);
2032 }
2033 kunmap_atomic(se, KM_USER0);
2034
2035 logfs_put_write_page(page);
2036 }
2037
__get_segment_entry(struct logfs_segment_entry * se,long _target)2038 static void __get_segment_entry(struct logfs_segment_entry *se, long _target)
2039 {
2040 struct logfs_segment_entry *target = (void *)_target;
2041
2042 *target = *se;
2043 }
2044
logfs_get_segment_entry(struct super_block * sb,u32 segno,struct logfs_segment_entry * se)2045 void logfs_get_segment_entry(struct super_block *sb, u32 segno,
2046 struct logfs_segment_entry *se)
2047 {
2048 logfs_mod_segment_entry(sb, segno, 0, __get_segment_entry, (long)se);
2049 }
2050
__set_segment_used(struct logfs_segment_entry * se,long increment)2051 static void __set_segment_used(struct logfs_segment_entry *se, long increment)
2052 {
2053 u32 valid;
2054
2055 valid = be32_to_cpu(se->valid);
2056 valid += increment;
2057 se->valid = cpu_to_be32(valid);
2058 }
2059
logfs_set_segment_used(struct super_block * sb,u64 ofs,int increment)2060 void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment)
2061 {
2062 struct logfs_super *super = logfs_super(sb);
2063 u32 segno = ofs >> super->s_segshift;
2064
2065 if (!increment)
2066 return;
2067
2068 logfs_mod_segment_entry(sb, segno, 1, __set_segment_used, increment);
2069 }
2070
__set_segment_erased(struct logfs_segment_entry * se,long ec_level)2071 static void __set_segment_erased(struct logfs_segment_entry *se, long ec_level)
2072 {
2073 se->ec_level = cpu_to_be32(ec_level);
2074 }
2075
logfs_set_segment_erased(struct super_block * sb,u32 segno,u32 ec,gc_level_t gc_level)2076 void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
2077 gc_level_t gc_level)
2078 {
2079 u32 ec_level = ec << 4 | (__force u8)gc_level;
2080
2081 logfs_mod_segment_entry(sb, segno, 1, __set_segment_erased, ec_level);
2082 }
2083
__set_segment_reserved(struct logfs_segment_entry * se,long ignore)2084 static void __set_segment_reserved(struct logfs_segment_entry *se, long ignore)
2085 {
2086 se->valid = cpu_to_be32(RESERVED);
2087 }
2088
logfs_set_segment_reserved(struct super_block * sb,u32 segno)2089 void logfs_set_segment_reserved(struct super_block *sb, u32 segno)
2090 {
2091 logfs_mod_segment_entry(sb, segno, 1, __set_segment_reserved, 0);
2092 }
2093
__set_segment_unreserved(struct logfs_segment_entry * se,long ec_level)2094 static void __set_segment_unreserved(struct logfs_segment_entry *se,
2095 long ec_level)
2096 {
2097 se->valid = 0;
2098 se->ec_level = cpu_to_be32(ec_level);
2099 }
2100
logfs_set_segment_unreserved(struct super_block * sb,u32 segno,u32 ec)2101 void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec)
2102 {
2103 u32 ec_level = ec << 4;
2104
2105 logfs_mod_segment_entry(sb, segno, 1, __set_segment_unreserved,
2106 ec_level);
2107 }
2108
__logfs_write_inode(struct inode * inode,long flags)2109 int __logfs_write_inode(struct inode *inode, long flags)
2110 {
2111 struct super_block *sb = inode->i_sb;
2112 int ret;
2113
2114 logfs_get_wblocks(sb, NULL, flags & WF_LOCK);
2115 ret = do_write_inode(inode);
2116 logfs_put_wblocks(sb, NULL, flags & WF_LOCK);
2117 return ret;
2118 }
2119
do_delete_inode(struct inode * inode)2120 static int do_delete_inode(struct inode *inode)
2121 {
2122 struct super_block *sb = inode->i_sb;
2123 struct inode *master_inode = logfs_super(sb)->s_master_inode;
2124 struct page *page;
2125 int ret;
2126
2127 page = logfs_get_write_page(master_inode, inode->i_ino, 0);
2128 if (!page)
2129 return -ENOMEM;
2130
2131 move_inode_to_page(page, inode);
2132
2133 logfs_get_wblocks(sb, page, 1);
2134 ret = __logfs_delete(master_inode, page);
2135 logfs_put_wblocks(sb, page, 1);
2136
2137 logfs_put_write_page(page);
2138 return ret;
2139 }
2140
2141 /*
2142 * ZOMBIE inodes have already been deleted before and should remain dead,
2143 * if it weren't for valid checking. No need to kill them again here.
2144 */
logfs_evict_inode(struct inode * inode)2145 void logfs_evict_inode(struct inode *inode)
2146 {
2147 struct super_block *sb = inode->i_sb;
2148 struct logfs_inode *li = logfs_inode(inode);
2149 struct logfs_block *block = li->li_block;
2150 struct page *page;
2151
2152 if (!inode->i_nlink) {
2153 if (!(li->li_flags & LOGFS_IF_ZOMBIE)) {
2154 li->li_flags |= LOGFS_IF_ZOMBIE;
2155 if (i_size_read(inode) > 0)
2156 logfs_truncate(inode, 0);
2157 do_delete_inode(inode);
2158 }
2159 }
2160 truncate_inode_pages(&inode->i_data, 0);
2161 end_writeback(inode);
2162
2163 /* Cheaper version of write_inode. All changes are concealed in
2164 * aliases, which are moved back. No write to the medium happens.
2165 */
2166 /* Only deleted files may be dirty at this point */
2167 BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink);
2168 if (!block)
2169 return;
2170 if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) {
2171 block->ops->free_block(inode->i_sb, block);
2172 return;
2173 }
2174
2175 BUG_ON(inode->i_ino < LOGFS_RESERVED_INOS);
2176 page = inode_to_page(inode);
2177 BUG_ON(!page); /* FIXME: Use emergency page */
2178 logfs_put_write_page(page);
2179 }
2180
btree_write_block(struct logfs_block * block)2181 void btree_write_block(struct logfs_block *block)
2182 {
2183 struct inode *inode;
2184 struct page *page;
2185 int err, cookie;
2186
2187 inode = logfs_safe_iget(block->sb, block->ino, &cookie);
2188 page = logfs_get_write_page(inode, block->bix, block->level);
2189
2190 err = logfs_readpage_nolock(page);
2191 BUG_ON(err);
2192 BUG_ON(!PagePrivate(page));
2193 BUG_ON(logfs_block(page) != block);
2194 err = __logfs_write_buf(inode, page, 0);
2195 BUG_ON(err);
2196 BUG_ON(PagePrivate(page) || page->private);
2197
2198 logfs_put_write_page(page);
2199 logfs_safe_iput(inode, cookie);
2200 }
2201
2202 /**
2203 * logfs_inode_write - write inode or dentry objects
2204 *
2205 * @inode: parent inode (ifile or directory)
2206 * @buf: object to write (inode or dentry)
2207 * @n: object size
2208 * @_pos: object number (file position in blocks/objects)
2209 * @flags: write flags
2210 * @lock: 0 if write lock is already taken, 1 otherwise
2211 * @shadow_tree: shadow below this inode
2212 *
2213 * FIXME: All caller of this put a 200-300 byte variable on the stack,
2214 * only to call here and do a memcpy from that stack variable. A good
2215 * example of wasted performance and stack space.
2216 */
logfs_inode_write(struct inode * inode,const void * buf,size_t count,loff_t bix,long flags,struct shadow_tree * shadow_tree)2217 int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
2218 loff_t bix, long flags, struct shadow_tree *shadow_tree)
2219 {
2220 loff_t pos = bix << inode->i_sb->s_blocksize_bits;
2221 int err;
2222 struct page *page;
2223 void *pagebuf;
2224
2225 BUG_ON(pos & (LOGFS_BLOCKSIZE-1));
2226 BUG_ON(count > LOGFS_BLOCKSIZE);
2227 page = logfs_get_write_page(inode, bix, 0);
2228 if (!page)
2229 return -ENOMEM;
2230
2231 pagebuf = kmap_atomic(page, KM_USER0);
2232 memcpy(pagebuf, buf, count);
2233 flush_dcache_page(page);
2234 kunmap_atomic(pagebuf, KM_USER0);
2235
2236 if (i_size_read(inode) < pos + LOGFS_BLOCKSIZE)
2237 i_size_write(inode, pos + LOGFS_BLOCKSIZE);
2238
2239 err = logfs_write_buf(inode, page, flags);
2240 logfs_put_write_page(page);
2241 return err;
2242 }
2243
logfs_open_segfile(struct super_block * sb)2244 int logfs_open_segfile(struct super_block *sb)
2245 {
2246 struct logfs_super *super = logfs_super(sb);
2247 struct inode *inode;
2248
2249 inode = logfs_read_meta_inode(sb, LOGFS_INO_SEGFILE);
2250 if (IS_ERR(inode))
2251 return PTR_ERR(inode);
2252 super->s_segfile_inode = inode;
2253 return 0;
2254 }
2255
logfs_init_rw(struct super_block * sb)2256 int logfs_init_rw(struct super_block *sb)
2257 {
2258 struct logfs_super *super = logfs_super(sb);
2259 int min_fill = 3 * super->s_no_blocks;
2260
2261 INIT_LIST_HEAD(&super->s_object_alias);
2262 INIT_LIST_HEAD(&super->s_writeback_list);
2263 mutex_init(&super->s_write_mutex);
2264 super->s_block_pool = mempool_create_kmalloc_pool(min_fill,
2265 sizeof(struct logfs_block));
2266 super->s_shadow_pool = mempool_create_kmalloc_pool(min_fill,
2267 sizeof(struct logfs_shadow));
2268 return 0;
2269 }
2270
logfs_cleanup_rw(struct super_block * sb)2271 void logfs_cleanup_rw(struct super_block *sb)
2272 {
2273 struct logfs_super *super = logfs_super(sb);
2274
2275 logfs_mempool_destroy(super->s_block_pool);
2276 logfs_mempool_destroy(super->s_shadow_pool);
2277 }
2278