aboutsummaryrefslogtreecommitdiffstats
path: root/fs/tux3/inode.c
blob: ef2019bfa2ad8bd389ad7753a7dff263ec6366bc (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/*
 * Tux3 versioning filesystem inode operations
 *
 * Original copyright (c) 2008 Daniel Phillips <phillips@phunq.net>
 * Licensed under the GPL version 2
 *
 * By contributing changes to this file you grant the original copyright holder
 * the right to distribute those changes under any license.
 */

#include "tux3.h"
#include "filemap_hole.h"
#include "ileaf.h"
#include "iattr.h"

#ifndef trace
#define trace trace_on
#endif

static void tux_setup_inode(struct inode *inode);

static inline void tux_set_inum(struct inode *inode, inum_t inum)
{
#ifdef __KERNEL__
	inode->i_ino = inum;
#endif
	tux_inode(inode)->inum = inum;
}

struct inode *tux_new_volmap(struct sb *sb)
{
	struct inode *inode = new_inode(vfs_sb(sb));
	if (inode) {
		tux_set_inum(inode, TUX_VOLMAP_INO);
		tux_setup_inode(inode);
		insert_inode_hash(inode);
	}
	return inode;
}

struct inode *tux_new_logmap(struct sb *sb)
{
	struct inode *inode = new_inode(vfs_sb(sb));
	if (inode) {
		tux_set_inum(inode, TUX_LOGMAP_INO);
		tux_setup_inode(inode);
		insert_inode_hash(inode);
	}
	return inode;
}

static struct inode *tux_new_inode(struct inode *dir, struct tux_iattr *iattr,
				   dev_t rdev)
{
	struct inode *inode;

	inode = new_inode(dir->i_sb);
	if (!inode)
		return NULL;
	assert(!tux_inode(inode)->present);

	inode->i_mode = iattr->mode;
	inode->i_uid = iattr->uid;
	if (dir->i_mode & S_ISGID) {
		inode->i_gid = dir->i_gid;
		if (S_ISDIR(inode->i_mode))
			inode->i_mode |= S_ISGID;
	} else
		inode->i_gid = iattr->gid;
	inode->i_mtime = inode->i_ctime = inode->i_atime = gettime();
	switch (inode->i_mode & S_IFMT) {
	case S_IFBLK:
	case S_IFCHR:
		/* vfs, trying to be helpful, will rewrite the field */
		inode->i_rdev = rdev;
		tux_inode(inode)->present |= RDEV_BIT;
		break;
	case S_IFDIR:
		inc_nlink(inode);
		break;
	}
	tux_inode(inode)->present |= CTIME_SIZE_BIT|MTIME_BIT|MODE_OWNER_BIT|LINK_COUNT_BIT;

	/* Just for debug, will rewrite by alloc_inum() */
	tux_set_inum(inode, TUX_INVALID_INO);

	return inode;
}

/*
 * Deferred ileaf update for inode number allocation
 */
/* must hold itable->btree.lock */
static int is_defer_alloc_inum(struct inode *inode)
{
	return !list_empty(&tux_inode(inode)->alloc_list);
}

/* must hold itable->btree.lock */
static int find_defer_alloc_inum(struct sb *sb, inum_t inum)
{
	struct tux3_inode *tuxnode;

	list_for_each_entry(tuxnode, &sb->alloc_inodes, alloc_list) {
		if (tuxnode->inum == inum)
			return 1;
	}
	return 0;
}

/* must hold itable->btree.lock */
static void add_defer_alloc_inum(struct inode *inode)
{
	/* FIXME: need to reserve space (ileaf/bnodes) for this inode? */
	struct sb *sb = tux_sb(inode->i_sb);
	list_add_tail(&tux_inode(inode)->alloc_list, &sb->alloc_inodes);
}

/* must hold itable->btree.lock. FIXME: spinlock is enough? */
void del_defer_alloc_inum(struct inode *inode)
{
	list_del_init(&tux_inode(inode)->alloc_list);
}

/*
 * Inode table expansion algorithm
 *
 * First probe for the inode goal.  This retreives the rightmost leaf that
 * contains an inode less than or equal to the goal.  (We could in theory avoid
 * retrieving any leaf at all in some cases if we observe that the the goal must
 * fall into an unallocated gap between two index keys, for what that is worth.
 * Probably not very much.)
 *
 * If not at end then next key is greater than goal.  This block has the highest
 * ibase less than or equal to goal.  Ibase should be equal to btree key, so
 * assert.  Search block even if ibase is way too low.  If goal comes back equal
 * to next_key then there is no room to create more inodes in it, so advance to
 * the next block and repeat.
 *
 * Otherwise, expand the inum goal that came back.  If ibase was too low to
 * create the inode in that block then the low level split will fail and expand
 * will create a new inode table block with ibase at the goal.  We round the
 * goal down to some binary multiple in ileaf_split to reduce the chance of
 * creating inode table blocks with only a small number of inodes.  (Actually
 * we should only round down the split point, not the returned goal.)
 */

static int find_free_inum(struct cursor *cursor, inum_t goal, inum_t *allocated)
{
	int ret;

#ifndef __KERNEL__ /* FIXME: kill this, only mkfs path needs this */
	/* If this is not mkfs path, it should have itable root */
	if (!has_root(cursor->btree)) {
		*allocated = goal;
		return 0;
	}
#endif

	ret = btree_probe(cursor, goal);
	if (ret)
		return ret;

	/* FIXME: need better allocation policy */

	/*
	 * Find free inum from goal, and wrapped to TUX_NORMAL_INO if
	 * not found. This prevent to use less than TUX_NORMAL_INO if
	 * reserved ino was not specified explicitly.
	 */
	ret = btree_traverse(cursor, goal, TUXKEY_LIMIT, ileaf_find_free,
			     allocated);
	if (ret < 0)
		goto out;
	if (ret > 0) {
		/* Found free inum */
		ret = 0;
		goto out;
	}

	if (TUX_NORMAL_INO < goal) {
		u64 len = goal - TUX_NORMAL_INO;

		ret = btree_traverse(cursor, TUX_NORMAL_INO, len,
				     ileaf_find_free, allocated);
		if (ret < 0)
			goto out;
		if (ret > 0) {
			/* Found free inum */
			ret = 0;
			goto out;
		}
	}

	/* Couldn't find free inum */
	ret = -ENOSPC;

out:
	release_cursor(cursor);

	return ret;
}

struct ialloc_policy {
	inum_t (*goal)(struct inode *, void *);
	void (*update)(struct inode *, inum_t);
};

/*
 * Try to allocate specified inum for internal inode.
 */
static inum_t ialloc_specific_goal(struct inode *inode, void *data)
{
	return *(inum_t *)data;
}

static void ialloc_noop_update(struct inode *inode, inum_t inum)
{
}

static struct ialloc_policy ialloc_specific = {
	.goal	= ialloc_specific_goal,
	.update	= ialloc_noop_update,
};

/*
 * Try to allocate inum linearly.
 * FIXME: we should use per-directory inum policy.
 */
static inum_t ialloc_linear_goal(struct inode *inode, void *data)
{
	struct sb *sb = tux_sb(inode->i_sb);
	return sb->nextinum;
}

static void ialloc_linear_update(struct inode *inode, inum_t inum)
{
	struct sb *sb = tux_sb(inode->i_sb);

	inum++;
	if (inum >= MAX_INODES)
		sb->nextinum = TUX_NORMAL_INO;
	else
		sb->nextinum = inum;
}

static struct ialloc_policy ialloc_linear = {
	.goal	= ialloc_linear_goal,
	.update	= ialloc_linear_update,
};

static int alloc_inum(struct inode *inode, struct ialloc_policy *policy,
		      void *policy_data)
{
	struct sb *sb = tux_sb(inode->i_sb);
	struct btree *itable = itable_btree(sb);
	struct cursor *cursor;
	inum_t goal;
	int err = 0;

	cursor = alloc_cursor(itable, 1); /* +1 for now depth */
	if (!cursor)
		return -ENOMEM;

	down_write(&cursor->btree->lock);
	goal = policy->goal(inode, policy_data);
	while (1) {
		err = find_free_inum(cursor, goal, &goal);
		if (err)
			goto error;

		/* Is this inum already used by deferred inum allocation? */
		if (!find_defer_alloc_inum(sb, goal))
			break;

		goal++;
		while (find_defer_alloc_inum(sb, goal))
			goal++;
	}

	init_btree(&tux_inode(inode)->btree, sb, no_root, dtree_ops());

	/* Final initialization of inode */
	tux_set_inum(inode, goal);
	tux_setup_inode(inode);

	add_defer_alloc_inum(inode);

	policy->update(inode, goal);

	/*
	 * If inum is not reserved area, account it. If inum is
	 * reserved area, inode might not be written into itable. So,
	 * we don't include the reserved area into dynamic accounting.
	 * FIXME: what happen if snapshot was introduced?
	 */
	if (goal >= TUX_NORMAL_INO) {
		assert(sb->freeinodes > TUX_NORMAL_INO);
		sb->freeinodes--;
	}

error:
	up_write(&cursor->btree->lock);
	free_cursor(cursor);

	return err;
}

static int tux_test(struct inode *inode, void *data)
{
	return tux_inode(inode)->inum == *(inum_t *)data;
}

static int tux_set(struct inode *inode, void *data)
{
	tux_set_inum(inode, *(inum_t *)data);
	return 0;
}

static struct inode *
__tux_create_inode(struct inode *dir, struct tux_iattr *iattr, dev_t rdev,
		   struct ialloc_policy *policy, void *policy_data)
{
	struct inode *inode;

	inode = tux_new_inode(dir, iattr, rdev);
	if (!inode)
		return ERR_PTR(-ENOMEM);

	int err = alloc_inum(inode, policy, policy_data);
	if (err) {
		make_bad_inode(inode);
		iput(inode);
		return ERR_PTR(err);
	}
	inum_t inum = tux_inode(inode)->inum;
	if (insert_inode_locked4(inode, inum, tux_test, &inum) < 0) {
		/* Can be nfsd race happened, or fs corruption. */
		tux3_warn(tux_sb(dir->i_sb), "inode insert error: inum %Lx",
			  inum);
		iput(inode);
		return ERR_PTR(-EIO);
	}
	/*
	 * The unhashed inode ignores mark_inode_dirty(), so it should
	 * be called after insert_inode_hash().
	 */
	tux3_iattrdirty(inode);
	tux3_mark_inode_dirty(inode);

	return inode;
}

/* Allocate inode with linear inum allocation policy */
struct inode *tux_create_inode(struct inode *dir, struct tux_iattr *iattr,
			       dev_t rdev)
{
	return __tux_create_inode(dir, iattr, rdev, &ialloc_linear, NULL);
}

/* Allocate inode with specific inum allocation policy */
struct inode *tux_create_specific_inode(struct inode *dir, inum_t inum,
					struct tux_iattr *iattr, dev_t rdev)
{
	return __tux_create_inode(dir, iattr, rdev, &ialloc_specific, &inum);
}

static int check_present(struct inode *inode)
{
	struct tux3_inode *tuxnode = tux_inode(inode);

	switch (inode->i_mode & S_IFMT) {
	case S_IFSOCK:
	case S_IFIFO:
		assert(tuxnode->present & MODE_OWNER_BIT);
		assert(!(tuxnode->present & RDEV_BIT));
		break;
	case S_IFBLK:
	case S_IFCHR:
		assert(tuxnode->present & MODE_OWNER_BIT);
//		assert(tuxnode->present & RDEV_BIT);
		break;
	case S_IFREG:
		assert(tuxnode->present & MODE_OWNER_BIT);
		assert(!(tuxnode->present & RDEV_BIT));
		break;
	case S_IFDIR:
		assert(tuxnode->present & MODE_OWNER_BIT);
		assert(!(tuxnode->present & RDEV_BIT));
		break;
	case S_IFLNK:
		assert(tuxnode->present & MODE_OWNER_BIT);
		assert(!(tuxnode->present & RDEV_BIT));
		break;
	case 0: /* internal inode */
		if (tux_inode(inode)->inum == TUX_VOLMAP_INO)
			assert(tuxnode->present == 0);
		else {
			assert(!(tuxnode->present & RDEV_BIT));
		}
		break;
	default:
		tux3_fs_error(tux_sb(inode->i_sb),
			      "Unknown mode: inum %Lx, mode %07ho",
			      tuxnode->inum, inode->i_mode);
		break;
	}
	return 0;
}

static int open_inode(struct inode *inode)
{
	struct sb *sb = tux_sb(inode->i_sb);
	struct btree *itable = itable_btree(sb);
	int err;

	struct cursor *cursor = alloc_cursor(itable, 0);
	if (!cursor)
		return -ENOMEM;

	down_read(&cursor->btree->lock);
	if ((err = btree_probe(cursor, tux_inode(inode)->inum)))
		goto out;

	/* Read inode attribute from inode btree */
	struct ileaf_req rq = {
		.key = {
			.start	= tux_inode(inode)->inum,
			.len	= 1,
		},
		.data	= inode,
	};
	err = btree_read(cursor, &rq.key);
	if (err == -ENOENT)
		tux3_warn(sb, "inum %Lu not found", tux_inode(inode)->inum);
	if (!err) {
		check_present(inode);
		tux_setup_inode(inode);
	}

	release_cursor(cursor);
out:
	up_read(&cursor->btree->lock);
	free_cursor(cursor);

	return err;
}

struct inode *tux3_iget(struct sb *sb, inum_t inum)
{
	struct inode *inode;
	int err;

	inode = iget5_locked(vfs_sb(sb), inum, tux_test, tux_set, &inum);
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (!(inode->i_state & I_NEW))
		return inode;

	err = open_inode(inode);
	if (err) {
		iget_failed(inode);
		return ERR_PTR(err);
	}
	unlock_new_inode(inode);
	return inode;
}

static int save_inode(struct inode *inode, struct tux3_iattr_data *idata,
		      unsigned delta)
{
	struct sb *sb = tux_sb(inode->i_sb);
	struct btree *itable = itable_btree(sb);
	inum_t inum = tux_inode(inode)->inum;
	int err = 0;

	trace("save inode 0x%Lx", inum);

#ifndef __KERNEL__
	/* FIXME: kill this, only mkfs path needs this */
	/* FIXME: this should be merged to btree_expand()? */
	down_write(&itable->lock);
	if (!has_root(itable))
		err = alloc_empty_btree(itable);
	up_write(&itable->lock);
	if (err)
		return err;
#endif

	struct cursor *cursor = alloc_cursor(itable, 1); /* +1 for new depth */
	if (!cursor)
		return -ENOMEM;

	down_write(&cursor->btree->lock);
	if ((err = btree_probe(cursor, inum)))
		goto out;
	/* paranoia check */
	if (!is_defer_alloc_inum(inode)) {
		unsigned size;
		assert(ileaf_lookup(itable, inum, bufdata(cursor_leafbuf(cursor)), &size));
	}

	/* Write inode attributes to inode btree */
	struct iattr_req_data iattr_data = {
		.idata	= idata,
		.btree	= &tux_inode(inode)->btree,
		.inode	= inode,
	};
	struct ileaf_req rq = {
		.key = {
			.start	= inum,
			.len	= 1,
		},
		.data		= &iattr_data,
	};
	err = btree_write(cursor, &rq.key);
	if (err)
		goto error_release;

	/*
	 * If inode is newly added into itable, account to on-disk usedinodes.
	 * ->usedinodes is used only by backend, no need lock.
	 * FIXME: what happen if snapshot was introduced?
	 */
	if (is_defer_alloc_inum(inode) && inum >= TUX_NORMAL_INO) {
		assert(be64_to_cpu(sb->super.usedinodes) < MAX_INODES);
		be64_add_cpu(&sb->super.usedinodes, 1);
	}
	del_defer_alloc_inum(inode);

error_release:
	release_cursor(cursor);
out:
	up_write(&cursor->btree->lock);
	free_cursor(cursor);
	return err;
}

int tux3_save_inode(struct inode *inode, struct tux3_iattr_data *idata,
		    unsigned delta)
{
	/* Those inodes must not be marked as I_DIRTY_SYNC/DATASYNC. */
	assert(tux_inode(inode)->inum != TUX_VOLMAP_INO &&
	       tux_inode(inode)->inum != TUX_LOGMAP_INO &&
	       tux_inode(inode)->inum != TUX_INVALID_INO);
	switch (tux_inode(inode)->inum) {
	case TUX_BITMAP_INO:
	case TUX_VTABLE_INO:
	case TUX_ATABLE_INO:
		/* FIXME: assert(only btree should be changed); */
		break;
	}
	return save_inode(inode, idata, delta);
}

/* FIXME: we wait page under I/O though, we would like to fork it instead */
static int tux3_truncate(struct inode *inode, loff_t newsize)
{
	/* FIXME: expanding size is not tested */
#ifdef __KERNEL__
	const unsigned boundary = PAGE_CACHE_SIZE;
#else
	const unsigned boundary = tux_sb(inode->i_sb)->blocksize;
#endif
	loff_t oldsize, holebegin;
	int is_expand, err;

	if (newsize == inode->i_size)
		return 0;

	/* inode_dio_wait(inode); */	/* FIXME: for direct I/O */

	err = 0;
	oldsize = inode->i_size;
	is_expand = newsize > oldsize;

	if (!is_expand) {
		err = tux3_truncate_partial_block(inode, newsize);
		if (err)
			goto error;
	}

	/* Change i_size, then clean buffers */
	i_size_write(inode, newsize);
	/* Roundup. Partial page is handled by tux3_truncate_partial_block() */
	holebegin = round_up(newsize, boundary);
#ifdef __KERNEL__
	/* FIXME: The buffer fork before invalidate. We should merge to
	 * truncate_pagecache() */
	tux3_truncate_inode_pages_range(inode->i_mapping, holebegin, LLONG_MAX);
#endif
	truncate_pagecache(inode, oldsize, holebegin);

	if (!is_expand) {
		err = tux3_add_truncate_hole(inode, newsize);
		if (err)
			goto error;
	}

	inode->i_mtime = inode->i_ctime = gettime();
	tux3_mark_inode_dirty(inode);
error:

	return err;
}

/* Remove inode from itable */
static int purge_inode(struct inode *inode)
{
	struct sb *sb = tux_sb(inode->i_sb);
	struct btree *itable = itable_btree(sb);
	int reserved_inum = tux_inode(inode)->inum < TUX_NORMAL_INO;

	down_write(&itable->lock);	/* FIXME: spinlock is enough? */

	/*
	 * If inum is not reserved area, account it.
	 * FIXME: what happen if snapshot was introduced?
	 */
	if (!reserved_inum) {
		assert(sb->freeinodes < MAX_INODES);
		sb->freeinodes++;
	}

	if (is_defer_alloc_inum(inode)) {
		del_defer_alloc_inum(inode);
		up_write(&itable->lock);
		return 0;
	}
	up_write(&itable->lock);

	/*
	 * If inode is deleted from itable, account to on-disk usedinodes.
	 * ->usedinodes is used only by backend, no need lock.
	 * FIXME: what happen if snapshot was introduced?
	 */
	if (!reserved_inum) {
		assert(be64_to_cpu(sb->super.usedinodes) > TUX_NORMAL_INO);
		be64_add_cpu(&sb->super.usedinodes, -1);
	}

	/* Remove inum from inode btree */
	return btree_chop(itable, tux_inode(inode)->inum, 1);
}

static int tux3_truncate_blocks(struct inode *inode, loff_t newsize)
{
	struct sb *sb = tux_sb(inode->i_sb);
	tuxkey_t index = (newsize + sb->blockmask) >> sb->blockbits;

	return btree_chop(&tux_inode(inode)->btree, index, TUXKEY_LIMIT);
}

int tux3_purge_inode(struct inode *inode, struct tux3_iattr_data *idata,
		     unsigned delta)
{
	int err, has_hole;

	/*
	 * If there is hole extents, i_size was changed and is not
	 * represent last extent in dtree.
	 *
	 * So, clear hole extents, then free all extents.
	 */
	has_hole = tux3_clear_hole(inode, delta);

	/*
	 * FIXME: i_blocks (if implemented) would be better way than
	 * inode->i_size to know whether we have to traverse
	 * btree. (Or another better info?)
	 *
	 * inode->i_size = 0;
	 * if (inode->i_blocks)
	 */
	if (idata->i_size || has_hole) {
		idata->i_size = 0;
		err = tux3_truncate_blocks(inode, 0);
		if (err)
			goto error;
	}
	err = free_empty_btree(&tux_inode(inode)->btree);
	if (err)
		goto error;

	err = xcache_remove_all(inode);
	if (err)
		goto error;

	err = purge_inode(inode);

error:
	return err;
}

/*
 * Decide whether in-core inode can be freed or not.
 */
int tux3_drop_inode(struct inode *inode)
{
	if (!is_bad_inode(inode)) {
		/* If inode->i_nlink == 0, mark dirty to delete */
		if (inode->i_nlink == 0)
			tux3_mark_inode_to_delete(inode);

		/* If inode is dirty, we still keep in-core inode. */
		if (inode->i_state & I_DIRTY) {
#ifdef __KERNEL__
			/* If unmount path, inode should be clean */
			if (!(inode->i_sb->s_flags & MS_ACTIVE)) {
				tux3_err(tux_sb(inode->i_sb),
					 "inode %p, inum %Lu, state %lx/%x",
					 inode, tux_inode(inode)->inum,
					 inode->i_state,
					 tux_inode(inode)->flags);
				assert(inode->i_sb->s_flags & MS_ACTIVE);
			}
#endif
			return 0;
		}
	}
	return generic_drop_inode(inode);
}

/*
 * In-core inode is going to be freed, do job for it.
 */
void tux3_evict_inode(struct inode *inode)
{
	struct sb *sb = tux_sb(inode->i_sb);
	void *ptr;

	/*
	 * evict_inode() should be called only if there is no
	 * in-progress buffers in backend. So we don't have to call
	 * tux3_truncate_inode_pages_range() here.
	 *
	 * We don't change anything here though, change_{begin,end}
	 * are needed to provide the current delta for debugging in
	 * tux3_invalidate_buffer().
	 *
	 * The ->evict_inode() is called from slab reclaim path, and
	 * reclaim path is called from memory allocation path, so, we
	 * have to use *_nested() here.
	 */
	change_begin_atomic_nested(sb, &ptr);
#ifdef __KERNEL__
	/* Block device special file is still overwriting i_mapping */
	truncate_inode_pages(&inode->i_data, 0);
#else
	truncate_inode_pages(mapping(inode), 0);
#endif
	change_end_atomic_nested(sb, ptr);


	/*
	 * On theory, reader can be holding the forked page until
	 * evicting inode. So, we have to check the related forked
	 * page, and free forked pages before freeing host
	 * inode. Because page->mapping points freed inode->i_mapping.
	 *
	 * FIXME: we would want to avoid this (e.g. we want to use
	 * refcount to free). If impossible, we would want to use
	 * per-inode forked-buffers list, instead.
	 */
	free_forked_buffers(sb, inode, 1);

	clear_inode(inode);
	free_xcache(inode);
}

#ifdef __KERNEL__
/* This is used by tux3_clear_dirty_inodes() to tell inode state was changed */
void iget_if_dirty(struct inode *inode)
{
	assert(!(inode->i_state & I_FREEING));
	if (atomic_read(&inode->i_count)) {
		atomic_inc(&inode->i_count);
		return;
	}
	/* i_count == 0 should happen only dirty inode */
	assert(inode->i_state & I_DIRTY);
	atomic_inc(&inode->i_count);
}

/* Synchronize changes to a file and directory. */
int tux3_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
{
	struct inode *inode = file->f_mapping->host;
	struct sb *sb = tux_sb(inode->i_sb);

	/* FIXME: this is sync(2). We should implement real one */
	static int print_once;
	if (!print_once) {
		print_once++;
		tux3_warn(sb,
			  "fsync(2) fall-back to sync(2): %Lx-%Lx, datasync %d",
			  start, end, datasync);
	}

	return force_delta(sb);
}

int tux3_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
{
	struct inode *inode = dentry->d_inode;
	struct sb *sb = tux_sb(inode->i_sb);

	generic_fillattr(inode, stat);
	stat->ino = tux_inode(inode)->inum;
	/*
	 * FIXME: need to implement ->i_blocks?
	 *
	 * If we want to add i_blocks account, we have to check
	 * existent extent for dirty buffer.  And only if there is no
	 * existent extent, we add to ->i_blocks.
	 *
	 * Yes, ->i_blocks must be including delayed allocation buffers
	 * as allocated, because some apps (e.g. tar) think it is empty file
	 * if i_blocks == 0.
	 *
	 * But, it is purely unnecessary overhead.
	 */
	stat->blocks = ALIGN(inode->i_size, sb->blocksize) >> 9;
	return 0;
}

int tux3_setattr(struct dentry *dentry, struct iattr *iattr)
{
	struct inode *inode = dentry->d_inode;
	struct sb *sb = tux_sb(inode->i_sb);
	int err, need_truncate = 0;

	err = inode_change_ok(inode, iattr);
	if (err)
		return err;

	if (iattr->ia_valid & ATTR_SIZE && iattr->ia_size != inode->i_size) {
		inode_dio_wait(inode);
		need_truncate = 1;
	}

	change_begin(sb);

	tux3_iattrdirty(inode);

	if (need_truncate) {
		err = tux3_truncate(inode, iattr->ia_size);
		if (err)
			return err;
	}

	setattr_copy(inode, iattr);
	tux3_mark_inode_dirty(inode);

	change_end(sb);

	return 0;
}

#include "inode_vfslib.c"

static const struct file_operations tux_file_fops = {
	.llseek		= generic_file_llseek,
	.read		= do_sync_read,
	.write		= do_sync_write,
	.aio_read	= generic_file_aio_read,
	.aio_write	= tux3_file_aio_write,
//	.unlocked_ioctl	= fat_generic_ioctl,
#ifdef CONFIG_COMPAT
//	.compat_ioctl	= fat_compat_dir_ioctl,
#endif
	.mmap		= generic_file_mmap,
	.open		= generic_file_open,
	.fsync		= tux3_sync_file,
	.splice_read	= generic_file_splice_read,
	.splice_write	= tux3_file_splice_write,
};

static const struct inode_operations tux_file_iops = {
//	.permission	= ext4_permission,
	.setattr	= tux3_setattr,
	.getattr	= tux3_getattr
#ifdef CONFIG_EXT4DEV_FS_XATTR
//	.setxattr	= generic_setxattr,
//	.getxattr	= generic_getxattr,
//	.listxattr	= ext4_listxattr,
//	.removexattr	= generic_removexattr,
#endif
//	.fallocate	= ext4_fallocate,
//	.fiemap		= ext4_fiemap,
};

static const struct inode_operations tux_special_iops = {
//	.permission	= ext4_permission,
	.setattr	= tux3_setattr,
	.getattr	= tux3_getattr
#ifdef CONFIG_EXT4DEV_FS_XATTR
//	.setxattr	= generic_setxattr,
//	.getxattr	= generic_getxattr,
//	.listxattr	= ext4_listxattr,
//	.removexattr	= generic_removexattr,
#endif
};

const struct inode_operations tux_symlink_iops = {
	.readlink	= generic_readlink,
	.follow_link	= page_follow_link_light,
	.put_link	= page_put_link,
	.setattr	= tux3_setattr,
	.getattr	= tux3_getattr,
#if 0
//	.setxattr	= generic_setxattr,
//	.getxattr	= generic_getxattr,
//	.listxattr	= ext4_listxattr,
//	.removexattr	= generic_removexattr,
#endif
};

static void tux_setup_inode(struct inode *inode)
{
	struct sb *sb = tux_sb(inode->i_sb);

	assert(tux_inode(inode)->inum != TUX_INVALID_INO);

//	inode->i_generation = 0;
//	inode->i_flags = 0;

	switch (inode->i_mode & S_IFMT) {
	case S_IFSOCK:
	case S_IFIFO:
	case S_IFBLK:
	case S_IFCHR:
		inode->i_op = &tux_special_iops;
		init_special_inode(inode, inode->i_mode, inode->i_rdev);
		break;
	case S_IFREG:
		inode->i_op = &tux_file_iops;
		inode->i_fop = &tux_file_fops;
		inode->i_mapping->a_ops = &tux_file_aops;
//		tux_inode(inode)->io = tux3_filemap_overwrite_io;
		tux_inode(inode)->io = tux3_filemap_redirect_io;
		break;
	case S_IFDIR:
		inode->i_op = &tux_dir_iops;
		inode->i_fop = &tux_dir_fops;
		inode->i_mapping->a_ops = &tux_blk_aops;
		tux_inode(inode)->io = tux3_filemap_redirect_io;
		mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
		break;
	case S_IFLNK:
		inode->i_op = &tux_symlink_iops;
		inode->i_mapping->a_ops = &tux_symlink_aops;
		tux_inode(inode)->io = tux3_filemap_redirect_io;
		break;
	case 0: /* internal inode */
	{
		inum_t inum = tux_inode(inode)->inum;
		gfp_t gfp_mask = GFP_USER;

		/* FIXME: bitmap, logmap, vtable, atable doesn't have S_IFMT */
		switch (inum) {
		case TUX_BITMAP_INO:
		case TUX_VTABLE_INO:
		case TUX_ATABLE_INO:
			/* set fake i_size to escape the check of block_* */
			inode->i_size = vfs_sb(sb)->s_maxbytes;
			inode->i_mapping->a_ops = &tux_blk_aops;
			tux_inode(inode)->io = tux3_filemap_redirect_io;
			/* Flushed by tux3_flush_inode_internal() */
			tux3_set_inode_no_flush(inode);
			break;
		case TUX_VOLMAP_INO:
		case TUX_LOGMAP_INO:
			inode->i_size = (loff_t)sb->volblocks << sb->blockbits;
			inode->i_mapping->a_ops = &tux_vol_aops;
			if (inum == TUX_VOLMAP_INO)
				tux_inode(inode)->io = tux3_volmap_io;
			else
				tux_inode(inode)->io = tux3_logmap_io;
			/* Flushed by tux3_flush_inode_internal() */
			tux3_set_inode_no_flush(inode);
			break;
		default:
			BUG();
			break;
		}

		/* Prevent reentering into our fs recursively by mem reclaim */
		switch (inum) {
		case TUX_BITMAP_INO:
		case TUX_VOLMAP_INO:
		case TUX_LOGMAP_INO:
			/* FIXME: we should use non-__GFP_FS for all? */
			gfp_mask &= ~__GFP_FS;
			break;
		}
		mapping_set_gfp_mask(inode->i_mapping, gfp_mask);
		break;
	}
	default:
		tux3_fs_error(sb, "Unknown mode: inum %Lx, mode %07ho",
			      tux_inode(inode)->inum, inode->i_mode);
		break;
	}
}
#endif /* !__KERNEL__ */