summaryrefslogtreecommitdiffstats
path: root/quota.lyx
blob: e84338456826a711282fb5181e891839859ea0d8 (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
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100

\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Title
Btrfs Subvolume Quota Groups
\end_layout

\begin_layout Author
Arne Jansen, Strato AG
\end_layout

\begin_layout Date
October 2011
\end_layout

\begin_layout Section
Subvolume Quota Concepts
\end_layout

\begin_layout Standard
The concept of quota has a long-standing tradition in the Unix world.
 Ever since computers allow multiple users to work simultaneously in one
 filesystem, there is the need to prevent one user from using up the entire
 space.
 Every user should get his fair share of the available resources.
\end_layout

\begin_layout Standard
In case of files, the solution is quite straightforward.
 Each file has an 
\begin_inset Quotes eld
\end_inset

owner
\begin_inset Quotes erd
\end_inset

 recorded along with it, and it has a size.
 Traditional quota just restricts the total size of all files that are owned
 by a user.
 The concept is quite flexible: if a user hits his quota limit, the administrato
r can raise it on the fly.
\end_layout

\begin_layout Standard
On the other hand, the traditional approach has only a poor solution to
 restrict directories.
 At installation time, the harddisk can be partitioned so that every directory
 (e.
\begin_inset space \thinspace{}
\end_inset

g.
\begin_inset space \space{}
\end_inset


\family typewriter
/usr
\family default
, 
\family typewriter
/var
\family default
, ...) that needs a limit gets its own partition.
 The obvious problem is, that those limits cannot be changed without a reinstall
ation.
 The btrfs subvolume feature builds a bridge.
 Subvolumes correspond in many ways to partitions, as every subvolume looks
 like its own filesystem.
 With subvolume quota, it is now possible to restrict each subvolume like
 a partition, but keep the flexibility of quota.
 The space for each subvolume can be expanded or restricted on the fly.
\end_layout

\begin_layout Standard
As subvolumes are the basis for snapshots, interesting questions arise as
 to how to account used space in the presence of snapshots.
 If you have a file shared between a subvolume and a snapshot, whom to account
 the file to? The creator? Both? What if the file gets modified in the snapshot,
 should only these changes be accounted to it? But wait, both the snapshot
 and the subvolume belong to the same user home.
 I just want to limit the total space used by both! But somebody else might
 not want to charge the snapshots to the users.
\end_layout

\begin_layout Standard
Btrfs subvolume quota solves these problems by introducing groups of subvolumes
 and let the user put limits on them.
 It is even possible to have groups of groups.
 In the following, we refer to them as 
\begin_inset Quotes eld
\end_inset

qgroups
\begin_inset Quotes erd
\end_inset

.
 Each qgroup primarily tracks two numbers, the amount of total referenced
 space and the amount of exclusively referenced space.
\end_layout

\begin_layout Standard

\emph on
Referenced
\emph default
 space is the amount of data that can be reached from any of the subvolumes
 contained in the qgroup, while 
\emph on
exclusive
\emph default
 is the amount of data where all references to this data can be reached
 from within this qgroup.
\end_layout

\begin_layout Section
Subvolume Quota Groups
\end_layout

\begin_layout Standard
The basic notion of the Subvolume Quota feature is the qouta group, short
 qgroup.
 Qgroups are notated as <level>/<id>, e.
\begin_inset space \thinspace{}
\end_inset

g.
\begin_inset space \space{}
\end_inset

the qgroup 3/2 is a qgroup of level
\begin_inset space ~
\end_inset

3.
 For level
\begin_inset space ~
\end_inset

0, the leading 
\begin_inset Quotes eld
\end_inset

0/
\begin_inset Quotes erd
\end_inset

 can be omitted.
 Qgroups of level
\begin_inset space ~
\end_inset

0 get created automatically when a subvolume/snapshot gets created.
 The ID of the qgroup corresponds to the ID of the subvolume, so 0/5 is
 the qgroup for the root subvolume.
 For the 
\begin_inset Quotes eld
\end_inset

btrfs qgroup
\begin_inset Quotes erd
\end_inset

 command, the path to the subvolume can also be used instead of 0/<ID>.
 For all higher levels, the ID can be choosen freely.
\end_layout

\begin_layout Standard
Each qgroup can contain a set of lower level qgroups, thus creating a hierarchy
 of qgroups.
 Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:Sample-qgroup-hierarchy"

\end_inset


\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename qgroups1.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:Sample-qgroup-hierarchy"

\end_inset

Sample qgroup hierarchy
\end_layout

\end_inset


\end_layout

\end_inset

 shows an example qgroup tree.
 At the bottom, some extents are depicted showing which qgroups reference
 which extents.
 It is important to understand the notion of 
\emph on
referenced
\emph default
 versus 
\emph on
exclusive
\emph default
.
 In the example, qgroup 0/2 references extents 2 and 3, while 1/2 references
 extents 2--4.
 2/1 references all extents.
\end_layout

\begin_layout Standard
On the other hand, extent
\begin_inset space ~
\end_inset

1 is exclusive to 0/1, extent
\begin_inset space ~
\end_inset

2 is exclusive to 0/2, while extent
\begin_inset space ~
\end_inset

3 is neither exclusive to 0/2 nor to 0/3.
 But because both references can be reached from 1/2, extent
\begin_inset space ~
\end_inset

3 is exclusive to 1/2.
 All extents are exclusive to 2/1.
 So 
\emph on
exclusive
\emph default
 does not mean there is no other way to reach the extent, but it does mean
 that if you delete all subvolumes contained in a qgroup, the extent will
 get deleted.
 
\emph on
Exclusive
\emph default
 of a qgroup conveys the useful information how much space will be freed
 in case all subvolumes of the qgroup get deleted.
\end_layout

\begin_layout Standard
All data extents are accounted this way.
 Metadata that belongs to a specific subvolume (i.
\begin_inset space \space{}
\end_inset

e.
\begin_inset space \space{}
\end_inset

its filesystem tree) is also accounted.
 Checksums and extent allocation information are not accounted.
\end_layout

\begin_layout Standard
In turn, the 
\emph on
referenced
\emph default
 count of a qgroup can be limited.
 All writes beyond this limit will lead to a 
\begin_inset Quotes eld
\end_inset

Quota Exceeded
\begin_inset Quotes erd
\end_inset

 error.
\end_layout

\begin_layout Section
Inheritance
\end_layout

\begin_layout Standard
Things get a bit more complicated when new subvolumes or snapshots are created.
 The case of (empty) subvolumes is still quite easy.
 If a subvolume should be part of a qgroup, it has to be added to the qgroup
 at creation time.
 To add it at a later time, it would be necessary to at least rescan the
 full subvolume for a proper accounting.
\end_layout

\begin_layout Standard
Creation of a snapshot is the hard case.
 Obviously, the snapshot will reference the exact amount of space as its
 source, and both source and destination now have an 
\emph on
exclusive
\emph default
 count of 0 (4
\begin_inset space \thinspace{}
\end_inset

kB to be precise, as the roots of the trees are not shared).
 But what about qgroups of higher levels? If the qgroup contains both the
 source and the destination, nothing changes.
 If the qgroup contains only the source, it might lose some 
\emph on
exclusive
\emph default
.
 But how much? The tempting answer is, 
\begin_inset Quotes eld
\end_inset

subtract all 
\emph on
exclusive
\emph default
 of the source from the qgroup
\begin_inset Quotes erd
\end_inset

, but that is wrong, or at least not enough.
 There could have been an extent that is referenced from the source and
 another subvolume from that qgroup.
 This extent would have been exclusive to the qgroup, but not to the source
 subvolume.
 With the creation of the snapshot, the qgroup would also lose this extent
 from its exclusive set.
\end_layout

\begin_layout Standard
So how can this problem be solved? In the instant the snapshot gets created,
 we already have to know the correct 
\emph on
exclusive
\emph default
 count.
 We need to have a second qgroup that contains all the subvolumes as the
 first qgroup, except the subvolume we want to snapshot.
 The moment we create the snapshot, the 
\emph on
exclusive
\emph default
 count from the second qgroup needs to be copied to the first qgroup, as
 it represents the correct value.
 The second qgroup is called a tracking qgroup.
 It is only there in case a snapshot is needed.
\end_layout

\begin_layout Section
Use Cases
\end_layout

\begin_layout Subsection
Single-user machine
\end_layout

\begin_layout Subsubsection
Replacement for partitions
\end_layout

\begin_layout Standard
The simplest use case is to use qgroups as simple replacement for partitions.
 Btrfs takes the disk as a whole, and 
\family typewriter
/
\family default
, 
\family typewriter
/usr
\family default
, 
\family typewriter
/var
\family default
 etc.
\begin_inset space \space{}
\end_inset

are created as subvolumes.
 As each subvolume gets it own qgroup automatically, they can simply be
 restricted.
 No hierarchy is needed for that.
\end_layout

\begin_layout Subsubsection
Track usage of snapshots
\end_layout

\begin_layout Standard
When a snapshot is taken, a qgroup for it will automatically be created
 with the correct values.
 
\emph on
Referenced
\emph default
 will show how much is in it, possibly shared with other subvolumes.
 
\emph on
Exclusive
\emph default
 will be the amount of space that gets freed when the subvolume is deleted.
\end_layout

\begin_layout Subsection
Multi-user machine / Hosting
\end_layout

\begin_layout Subsubsection
\begin_inset CommandInset label
LatexCommand label
name "sub:restricting-homes"

\end_inset

Restricting homes
\end_layout

\begin_layout Standard
When you have several users on a machine, with home directories probably
 under 
\family typewriter
/home
\family default
, you might want to restrict 
\family typewriter
/home
\family default
 as a whole, while restricting every user to an indiviual limit as well.
 This is easily accomplished by creating a qgroup for 
\family typewriter
/home
\family default
, e.
\begin_inset space \thinspace{}
\end_inset

g.
\begin_inset space \space{}
\end_inset

1/1, and assigning all user subvolumes to it.
 Restricting this qgroup will limit 
\family typewriter
/home
\family default
, while every user subvolume can get its own (lower) limit.
\end_layout

\begin_layout Subsubsection
\begin_inset CommandInset label
LatexCommand label
name "sub:accounting-snapshots-to"

\end_inset

Accounting snapshots to the user
\end_layout

\begin_layout Standard
Let's say the user is allowed to create snapshots via some mechanism.
 It would only be fair to account space used by the snapshots to the user.
 This does not mean the user doubles his usage as soon as he takes a snapshot.
 Of course, files that are present in his home and the snapshot should only
 be accounted once.
 This can be accomplished by creating a qgroup for each user, say 1/<uid>.
 The user home and all snapshots are assigned to this qgroup.
 Limiting it will extend the limit to all snapshots, counting files only
 once.
 To limit 
\family typewriter
/home
\family default
 as a whole, a higher level group 2/1 replacing 1/1 from the previous example
 is needed, with all user qgroups assigned to it.
\end_layout

\begin_layout Subsubsection
Do not account snapshots
\end_layout

\begin_layout Standard
On the other hand, when the snapshots get created automatically, the user
 has no chance to control them, so the space used by them should not be
 accounted to him.
 This is already the case when creating snapshots in the example from section
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "sub:restricting-homes"

\end_inset

.
\end_layout

\begin_layout Subsubsection
Snapshots for backup purposes
\end_layout

\begin_layout Standard
This scenario is a mixture of the previous two.
 The user can create snapshots, but some snapshots for backup purposes are
 being created by the system.
 The user's snapshots should be accounted to the user, not the system.
 The solution is similar to the one from section
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "sub:accounting-snapshots-to"

\end_inset

, but do not assign system snapshots to user's qgroup.
\end_layout

\begin_layout Section
Implementation
\end_layout

\begin_layout Subsection
Update algorithm
\end_layout

\begin_layout Standard
The update algorithm is the core of the quota implementation.
 Whenever a reference is added or removed, the update algorithm is called.
\end_layout

\begin_layout Standard
The algorithm is called with the address of the extent for which to add/remove
 the reference, the root of the reference, the amount of space to add/remove,
 and of course the operation to perform.
\end_layout

\begin_layout Standard
A call could look like this
\end_layout

\begin_layout LyX-Code
qgroup_record_ref(ref_root, start, num_bytes, operation);
\end_layout

\begin_layout Standard
In fact, these parameters are all contained in the delayed ref structure,
 so just the delayed ref node is passed instead.
 This function gets called from the central point where backrefs are added
 to the filesystem, 
\family typewriter
btrfs_\SpecialChar \-
add_\SpecialChar \-
delayed_*_ref
\family default
.
\end_layout

\begin_layout Standard
The algorithm works in multiple steps:
\end_layout

\begin_layout Enumerate
Find all referencing roots
\end_layout

\begin_layout Enumerate
Calculate refcnt for all qgroups
\end_layout

\begin_layout Enumerate
Tag qgroups
\end_layout

\begin_layout Enumerate
Update 
\emph on
exclusive
\end_layout

\begin_layout Subsubsection
Find all referencing roots
\end_layout

\begin_layout Standard
The first step is to find all roots that are currently referencing the extent.
 Though btrfs is fully back-referenced, this step is not as easy as it may
 seem, because of the lazy refcounting scheme.
 
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename 5roots.svg
	scale 50

\end_inset


\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:Extent-with-lazy"

\end_inset

Extent with lazy references
\end_layout

\end_inset


\end_layout

\end_inset

The back references that are recorded for the extent may not tell the full
 truth.
 In figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:Extent-with-lazy"

\end_inset

, a tree is depicted where the actual extent only has two back references
 recorded, whereas there are five roots referencing it.
\end_layout

\begin_layout Standard
The solution is to walk up the tree and follow all back references until
 all roots are found.
 This looks like a classic problem for a recursive tree walk, but recursion
 here is not possible for two reasons:
\end_layout

\begin_layout Enumerate
The code runs in kernel space with very limited stack space.
 With a recursion, the stack may overflow.
\end_layout

\begin_layout Enumerate
To follow a back reference, the referenced extent has to be searched.
 This is due to the nature of the indirect back references used.
 These back references point to a key in the tree, not to the address of
 an extent.
\end_layout

\begin_layout Standard
The code solves this by keeping two lists, one for all roots found and one
 for all backrefs to follow.
 Initially, the list of roots is empty, while the list of backrefs is filled
 with only one item, the reference to the extent for which all backrefs
 are to be found.
\end_layout

\begin_layout Standard
The following pseudo-code describes how all roots are found:
\end_layout

\begin_layout LyX-Code
foreach ref (0 ...
 #refs in ulist)
\end_layout

\begin_deeper
\begin_layout LyX-Code
find extent for ref
\end_layout

\begin_layout LyX-Code
add all refs for extent to ulist
\end_layout

\begin_layout LyX-Code
if (extent is root)
\end_layout

\begin_deeper
\begin_layout LyX-Code
add root to ulist of roots
\end_layout

\begin_layout LyX-Code

\end_layout

\end_deeper
\end_deeper
\begin_layout Standard
The lists here are called 
\begin_inset Quotes eld
\end_inset

ulists
\begin_inset Quotes erd
\end_inset

, because they only accept new items if they are not already in the list,
 i.
\begin_inset space \thinspace{}
\end_inset

e.
\begin_inset space \space{}
\end_inset

if they are unique.
\end_layout

\begin_layout Standard
The step to add all backrefs for an extent involves finding all recorded
 inline backrefs, all in-tree backrefs and all delayed refs for the extent
 up to the moment the algorithms starts to run.
 Because this code might run some time, new delayed refs for any extent
 in the tree might be added in the meantime.
 To avoid a race condition here, each delayed ref gets a sequence number.
 Only delayed refs with seq
\begin_inset space ~
\end_inset

< own seq are considered.
 Also, no delayed ref with a higher seq than own seq must be run while the
 roots are searched for.
\end_layout

\begin_layout Standard
The code will never include the reference to add/delete.
\end_layout

\begin_layout Subsubsection
Calculate refcnt for all qgroups
\end_layout

\begin_layout Standard
After the list of referencing roots is known, the next three steps all operate
 on the qgroup hierarchy.
 A sample hierarchy is depicted in figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:Sample-qgroup-hierarchy"

\end_inset

.
\end_layout

\begin_layout Standard
The first operation on the tree is to calculate the number of references
 that can be reached from every given qgroup.
 This is done by walking the tree upwards from every root found in the previous
 step and incrementing a count on each qgroup visited, where each root can
 only increment the count by one for every qgroup it can reach, even if
 it can reach it by several paths.
 The calculated count is called the refcnt.
\end_layout

\begin_layout Standard
As in the previous step, the tree is walked iteratively with the help of
 ulists to avoid recursion.
 Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:qgroup-tree-after-1"

\end_inset

 depicts the state after this step is done for extent
\begin_inset space ~
\end_inset

3, where the ref from 0/2 should get deleted.
 The Figure omits the fs trees and their roots, as qgroups of level
\begin_inset space ~
\end_inset

0 directly correspond to a root.
\end_layout

\begin_layout Standard
As the refcnt is part of the qgroup struct, the algorithm would require
 that all refcnts in all qgroups be set to zero before it can run.
 To avoid this, a global sequence number is used to determine the refcnt.
 Only one thread at a time can currently do refcounting on the tree (this
 is easily changable, should it impose a limit).
 This thread grabs the next sequence number and walks up the tree.
 If the refcnt of the visited qgroup is smaller than the seq, it is not
 yet set and known to be
\begin_inset space ~
\end_inset

0.
 Otherwise, it is incremented.
 After the algorithm has run, the global sequence number is incremented
 by the max refcnt found.
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename qgroups2.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:qgroup-tree-after"

\end_inset

qgroup tree after refcnt augmentation for extent
\begin_inset space ~
\end_inset

3
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Subsubsection
Tag qgroups
\end_layout

\begin_layout Standard
The next step is to walk up the tree again, but this time starting with
 ref_root, the root to add/remove.
 Remember that the previous step does not include the ref_root.
 Every qgroup that is being visited on the way up will be tagged in preparation
 for the next step.
 Additionally, under certain conditions, a first adjustment is made to the
 values of the visited qgroups.
\end_layout

\begin_layout Itemize
If the refcnt is zero and the operation is to add a reference, this means
 this qgroup is not yet referencing this extent, but after the operation,
 it will, so the 
\emph on
referenced
\emph default
 value of the qgroup is increased by num_bytes.
\end_layout

\begin_layout Itemize
If the refcnt is zero and the operation is to remove a reference, this means
 this qgroup is currently referencing the extent, but through the operation,
 it will lose its last reference, so the 
\emph on
referenced
\emph default
 value is decreased by num_bytes.
\end_layout

\begin_layout Itemize
If the refcnt is zero and the number of roots found in the first step is
 also zero, this means:
\end_layout

\begin_layout Itemize
In case of addition: the added reference will be the only reference, so
 
\emph on
exclusive
\emph default
 of the qgroup is increased by num_bytes.
\end_layout

\begin_layout Itemize
In case of removal: the reference is the last to remove, which means it
 is currently exclusive to ref_root, so 
\emph on
exclusive
\emph default
 of the qgroup is decreased by num_bytes.
\end_layout

\begin_layout Standard
Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:qgroup-tree-after-1"

\end_inset

 depicts the situation given the reference for 0/2 to extent
\begin_inset space ~
\end_inset

3 is to be deleted.
 
\emph on
Referenced
\emph default
 of 0/2 and 1/1 will get decreased.
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename qgroups3.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:qgroup-tree-after-1"

\end_inset

qgroup tree after tagging
\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

\end_layout

\end_inset


\end_layout

\begin_layout Subsubsection
Update 
\emph on
exclusive
\end_layout

\begin_layout Standard
The last step adjusts the 
\emph on
exclusive
\emph default
 counts of all untagged qgroups.
 The 
\emph on
exclusive
\emph default
 counts of the tagged qgroups already got adjusted in the previous step.
 All roots from step
\begin_inset space ~
\end_inset

1 are walked again, tagged qgroups are skipped.
 If the refcnt equals the number of roots found in step one, 
\emph on
exclusive
\emph default
 gets increased if the ref is to be removed and decreased otherwise.
 Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:update-of-exclusive"

\end_inset

 shows the outcome of this step.
 Extent
\begin_inset space ~
\end_inset

3 is now 
\emph on
exclusive
\emph default
 to 0/3.
 All other 
\emph on
exclusives
\emph default
 are untouched.
 Extent
\begin_inset space ~
\end_inset

3 was exclusive to 1/2 and 2/1 and still is, while it was not exclusive
 to 0/2 and 1/1 and still is not.
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename qgroups4.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:update-of-exclusive"

\end_inset

Update of 
\emph on
exclusive
\emph default
 on qgroup tree
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Subsection
Tracking Groups 
\end_layout

\begin_layout Standard
As seen in the introductory chapter, when taking a snapshot, the values
 of several qgroups might need to be adjusted.
 This is easiest to see when looking at some examples.
 Figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:Tracking-snapshots"

\end_inset

 shows a simple example where tracking groups are needed.
 
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename tracking1.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:Tracking-snapshots"

\end_inset

Tracking snapshots
\end_layout

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Standard
The exercise is to track 
\emph on
referenced
\emph default
 and 
\emph on
exclusive
\emph default
 for all snapshots of a subvolume.
 The gray qgroups 0/2--0/4 are all snapshot of 0/1.
 Before 0/4 is created, 1/2 contains 0/2 and 0/3.
 The moment 0/4 gets created, it is added to 1/2.
 The 
\emph on
exclusive
\emph default
 count of 1/2 will not change, as all extents that become reachable from
 1/2 are also reachable from 1/1.
 More problematic is the 
\emph on
referenced
\emph default
 count, as not all extents from 0/4 might be new to 1/2.
 The solution is to add another qgroup, 1/3, that tracks 0/1 and all subvolumes
 of it (figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:A-tracking-qgroup"

\end_inset

).
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename tracking2.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:A-tracking-qgroup"

\end_inset

A tracking qgroup is needed for 1/2
\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

\end_layout

\end_inset


\end_layout

\begin_layout Standard
The moment the snapshot gets created, 1/3 holds the correct 
\emph on
referenced
\emph default
 count for all snapshots.
 To get 1/2 back to the correct values, 
\emph on
referenced
\emph default
 from 1/3 has to be copied to 1/2, while 
\emph on
exclusive
\emph default
 of 1/2 stays untouched.
\end_layout

\begin_layout Standard
In the next step, we want to take a snapshot of 0/2.
 The resulting snapshot should not be part of 1/2.
 This poses another problem: while 
\emph on
referenced 
\emph default
does not change, 
\emph on
exclusive
\emph default
 needs to be corrected.
 For this, we need another tracking group, 1/4 (figure
\begin_inset space ~
\end_inset


\begin_inset CommandInset ref
LatexCommand ref
reference "fig:A-snapshot-of"

\end_inset

).
\end_layout

\begin_layout Standard
\begin_inset Float figure
wide false
sideways false
status open

\begin_layout Plain Layout
\begin_inset Graphics
	filename tracking3.svg
	scale 20

\end_inset


\end_layout

\begin_layout Plain Layout
\begin_inset Caption

\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "fig:A-snapshot-of"

\end_inset

A snapshot of 0/2
\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

\end_layout

\end_inset


\end_layout

\begin_layout Standard
When 0/5 is created, 
\emph on
exclusive
\emph default
 from 1/4 needs to be copied to 1/2.
 Snapshotting 0/2 also invalidates the 
\emph on
exclusive
\emph default
 of 1/3.
 Also, another snapshot of 0/1 would invalidate 1/4.
 So one more tracking groups is needed, containing 0/1, 0/4 and 0/3.
\end_layout

\begin_layout Standard
It is planned that the btrfs userland utility will keep track of the needed
 tracking groups and takes care that all the necessary copies happen.
 For this, a format needs to be found how a user can describe what snapshots
 he intends to take.
 Keeping tracking groups for all possible combinations would lead to an
 exponential number of tracking groups.
\end_layout

\begin_layout Subsection
On-disk quota tree layout
\end_layout

\begin_layout Standard
Qgroups add a new tree, the quota tree.
 Four new keys are used in this tree.
 The overall status is recorded in a status item, and each qgroup has two
 items, one to record the user configured limits and one to record the current
 
\emph on
referenced
\emph default
/
\emph on
exclusive
\emph default
 counts.
 Each parent/child-relationship between qgroups gets two qgroup_relation
 items, one per direction.
 The on-disk structure is still preliminary.
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * Records the overall state of the qgroups.
\end_layout

\begin_layout LyX-Code
 * There is only one instance of this key present,
\end_layout

\begin_layout LyX-Code
 * (0, BTRFS_QGROUP_STATUS_KEY, 0)
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_STATUS_KEY 240
\end_layout

\begin_layout LyX-Code
/* 
\end_layout

\begin_layout LyX-Code
 * Records the currently used space of the qgroup.
\end_layout

\begin_layout LyX-Code
 * One key per qgroup, (0, BTRFS_QGROUP_INFO_KEY, qgroupid).
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_INFO_KEY 242
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * Contains the user configured limits for the qgroup.
\end_layout

\begin_layout LyX-Code
 * One key per qgroup, (0, BTRFS_QGROUP_LIMIT_KEY, qgroupid).
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_KEY 244
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * Records the child-parent relationship of qgroups.
 For
\end_layout

\begin_layout LyX-Code
 * each relation, 2 keys are present:
\end_layout

\begin_layout LyX-Code
 * (childid, BTRFS_QGROUP_RELATION_KEY, parentid)
\end_layout

\begin_layout LyX-Code
 * (parentid, BTRFS_QGROUP_RELATION_KEY, childid)
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_RELATION_KEY 246
\end_layout

\begin_layout Standard
The keys are chosen in a way such that 
\family typewriter
STATUS_KEY
\family default
 comes first, followed by all 
\family typewriter
INFO_KEY
\family default
s, followed by all 
\family typewriter
LIMIT_KEY
\family default
s.
 After that, for each qgroup present, all relations follow.
 Only the 
\family typewriter
INFO_KEY
\family default
s and the 
\family typewriter
STATUS_KEY
\family default
 get updated regularly.
 The idea is that those keys stay close to each other to minimize writes.
 The 
\family typewriter
RELATION_KEY
\family default
 is chosen in a way that, by a simple enumeration, all children and parents
 for a given qgroup can be found.
 The qgroupid is composed of a 16\SpecialChar \nobreakdash-
bit 
\begin_inset Quotes eld
\end_inset

level
\begin_inset Quotes erd
\end_inset

 field, followed by a 48\SpecialChar \nobreakdash-
bit 
\begin_inset Quotes eld
\end_inset

id
\begin_inset Quotes erd
\end_inset

 field.
 A qgroupid is represented as level/id, e.
\begin_inset space \thinspace{}
\end_inset

g.
\begin_inset space \space{}
\end_inset

2/100.
 In the case of a subvolume, the level is 0, and the 
\begin_inset Quotes eld
\end_inset

id
\begin_inset Quotes erd
\end_inset

 is just the internal tree objectid (5 or >= 256).
 On the command line, the user will be able to use the subvolume path as
 the identifier.
\end_layout

\begin_layout LyX-Code
/* 
\end_layout

\begin_layout LyX-Code
 * is subvolume quota turned on? 
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_STATUS_FLAG_ON (1ULL << 0)
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * SCANNING is set during the initialization phase
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_STATUS_FLAG_SCANNING (1ULL << 1)
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * Some qgroup entries are known to be out of date, 
\end_layout

\begin_layout LyX-Code
 * either because the configuration has changed in a way that 
\end_layout

\begin_layout LyX-Code
 * makes a rescan necessary, or because the fs has been mounted 
\end_layout

\begin_layout LyX-Code
 * with a non-qgroup-aware version.
 
\end_layout

\begin_layout LyX-Code
 * Turning qouta off and on again makes it inconsistent, too.
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT (1ULL << 2)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_STATUS_VERSION 1
\end_layout

\begin_layout LyX-Code
struct btrfs_qgroup_status_item {
\end_layout

\begin_deeper
\begin_layout LyX-Code
__le64 version;
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * the generation is updated during every commit.
 As older
\end_layout

\begin_layout LyX-Code
 * versions of btrfs are not aware of qgroups, it will be
\end_layout

\begin_layout LyX-Code
 * possible to detect inconsistencies by checking the
\end_layout

\begin_layout LyX-Code
 * generation on mount time
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
__le64 generation;
\end_layout

\begin_layout LyX-Code
/* flag definitions see above */
\end_layout

\begin_layout LyX-Code
__le64 flags;
\end_layout

\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * only used during scanning to record the progress
\end_layout

\begin_layout LyX-Code
 * of the scan.
 It contains a logical address
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
__le64 scan;
\end_layout

\end_deeper
\begin_layout LyX-Code
} __attribute__ ((__packed__));
\end_layout

\begin_layout LyX-Code

\end_layout

\begin_layout Standard
Instead of hosting the scan cursor in the structure, one could also make
 a separate key instead that is only present during scanning.
\end_layout

\begin_layout LyX-Code
struct btrfs_qgroup_info_item {
\end_layout

\begin_deeper
\begin_layout LyX-Code
/*
\end_layout

\begin_layout LyX-Code
 * only updated when any of the other values change
\end_layout

\begin_layout LyX-Code
 */
\end_layout

\begin_layout LyX-Code
__le64 generation;
\end_layout

\begin_layout LyX-Code
__le64 rfer;
\end_layout

\begin_layout LyX-Code
__le64 rfer_cmpr;
\end_layout

\begin_layout LyX-Code
__le64 excl;
\end_layout

\begin_layout LyX-Code
__le64 excl_cmpr;
\end_layout

\end_deeper
\begin_layout LyX-Code
} __attribute__ ((__packed__));
\end_layout

\begin_layout Standard
For all uncompressed data, the same value will be recorded for compressed
 and uncompressed.
 The 
\family typewriter
*_cmpr
\family default
 values represent the amount of disk space used, the other values the amount
 of space from a user perspective.
 The uncompressed values are hard to get, so a first version might not support
 them yet and just record the on-disk values instead.
\end_layout

\begin_layout LyX-Code
/* flags definition for qgroup limits */
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_MAX_RFER (1ULL << 0)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_MAX_EXCL (1ULL << 1)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_RSV_RFER (1ULL << 2)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_RSV_EXCL (1ULL << 3)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_RFER_CMPR (1ULL << 4)
\end_layout

\begin_layout LyX-Code
#define BTRFS_QGROUP_LIMIT_EXCL_CMPR (1ULL << 5)
\end_layout

\begin_layout LyX-Code
struct btrfs_qgroup_limit_item {
\end_layout

\begin_deeper
\begin_layout LyX-Code
__le64 flags;
\end_layout

\begin_layout LyX-Code
__le64 max_referenced;
\end_layout

\begin_layout LyX-Code
__le64 max_exclusive;
\end_layout

\begin_layout LyX-Code
__le64 rsv_referenced;
\end_layout

\begin_layout LyX-Code
__le64 rsv_exclusive;
\end_layout

\end_deeper
\begin_layout LyX-Code
} __attribute__ ((__packed__));
\end_layout

\begin_layout Standard
The flags record which of the limits are to be enforced.
 The last two flags indicate whether the compressed or the uncompressed
 value is to limit.
 This structure also contains reservations, though they might be hard to
 implement, as btrfs has no clear understanding of how much free space is
 left.
 A straightforward implementation might be very inaccurate and the first
 version will probably not implement it.
 Those values are nevertheless included here as a means for future expansion.
\end_layout

\begin_layout Subsection
Estimation
\end_layout

\begin_layout Standard
In btrfs, each file operation is encapsulated into a transaction.
 All necessary space for the transaction has to be reserved before any modificat
ion is done to the structures, as there is no way to back out in the middle.
 That is what block reserves are used for.
\end_layout

\begin_layout Standard
The same holds for quota: it is not possible to deny an operation in the
 middle of it.
 The only point where an 
\family typewriter
EDQUOT
\family default
 (Quota exceeded) error can be generated is before the start of the operation.
 The easiest way would be to only deny it if one of the affected qgroups
 is already over quota, but that would allow large operations to exceed
 the quota by far.
 This implementation tries to estimate the needed space for the operation
 and reserves it at the start of the operation.
 If the reservation fails, the operation is denied.
\end_layout

\begin_layout Standard
The reservation is recorded in each qgroup.
 Also, it is saved in the trans_handle, so it can be freed on end_transaction.
 The estimation is not a worst-case estimation like the block reservation.
 It should not deny requests too early.
 On the other hand, it might be possible that a qgroup goes slightly over
 quota.
\end_layout

\end_body
\end_document