| 1 | package btrfsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.12 os/" |
| 5 | "sortscip-go gomod github.com/golang/go/src go1.12 sort/" |
| 6 | "syscallscip-go gomod github.com/golang/go/src go1.12 syscall/" |
| 7 | ) |
| 8 | |
| 9 | func cmpChunkBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/cmpChunkBlockGroup().(f1, f2 blockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroup#) int { |
| 10 | var mask blockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroup# |
| 11 | |
| 12 | if (f1 & _BTRFS_BLOCK_GROUP_TYPE_MASKscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/_BTRFS_BLOCK_GROUP_TYPE_MASK.) == |
| 13 | (f2 & _BTRFS_BLOCK_GROUP_TYPE_MASKscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/_BTRFS_BLOCK_GROUP_TYPE_MASK.) { |
| 14 | mask = _BTRFS_BLOCK_GROUP_PROFILE_MASKscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/_BTRFS_BLOCK_GROUP_PROFILE_MASK. |
| 15 | } else if f2&blockGroupSystemscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupSystem. != 0 { |
| 16 | return -1 |
| 17 | } else if f1&blockGroupSystemscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupSystem. != 0 { |
| 18 | return +1 |
| 19 | } else { |
| 20 | mask = _BTRFS_BLOCK_GROUP_TYPE_MASKscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/_BTRFS_BLOCK_GROUP_TYPE_MASK. |
| 21 | } |
| 22 | |
| 23 | if (f1 & mask) > (f2 & mask) { |
| 24 | return +1 |
| 25 | } else if (f1 & mask) < (f2 & mask) { |
| 26 | return -1 |
| 27 | } else { |
| 28 | return 0 |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | type spaceInfoByBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup# []spaceInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo# |
| 33 | |
| 34 | func (a spaceInfoByBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#) Lenscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#Len().() int { return len(a) } |
| 35 | func (a spaceInfoByBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#) Swapscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#Swap().(i, j int) { a[i], a[j] = a[j], a[i] } |
| 36 | func (a spaceInfoByBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#) Lessscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#Less().(i, j int) bool { |
| 37 | return cmpChunkBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/cmpChunkBlockGroup().(blockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroup#(a[i].Flagsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#Flags.), blockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroup#(a[j].Flagsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#Flags.)) < 0 |
| 38 | } |
| 39 | |
| 40 | type UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo# struct { |
| 41 | Totalscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#Total. uint64 |
| 42 | TotalUnusedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUnused. uint64 |
| 43 | TotalUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUsed. uint64 |
| 44 | TotalChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalChunks. uint64 |
| 45 | |
| 46 | FreeEstimatedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeEstimated. uint64 |
| 47 | FreeMinscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeMin. uint64 |
| 48 | |
| 49 | LogicalDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalDataChunks. uint64 |
| 50 | RawDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataChunks. uint64 |
| 51 | RawDataUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataUsed. uint64 |
| 52 | |
| 53 | LogicalMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalMetaChunks. uint64 |
| 54 | RawMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaChunks. uint64 |
| 55 | RawMetaUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaUsed. uint64 |
| 56 | |
| 57 | SystemUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemUsed. uint64 |
| 58 | SystemChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemChunks. uint64 |
| 59 | |
| 60 | DataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#DataRatio. float64 |
| 61 | MetadataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#MetadataRatio. float64 |
| 62 | |
| 63 | GlobalReservescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserve. uint64 |
| 64 | GlobalReserveUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserveUsed. uint64 |
| 65 | } |
| 66 | |
| 67 | const minUnallocatedThresholdscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/minUnallocatedThreshold. = 16 * 1024 * 1024 |
| 68 | |
| 69 | func spaceUsagescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceUsage().(f *osscip-go gomod github.com/golang/go/src go1.12 os/.Filescip-go gomod github.com/golang/go/src go1.12 os/File#) (UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#, error) { |
| 70 | info, err := iocFsInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/iocFsInfo().(f) |
| 71 | if err != nil { |
| 72 | return UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#{}, err |
| 73 | } |
| 74 | var u UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo# |
| 75 | for i := uint64(0); i <= info.max_idscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_fs_info_args#max_id.; i++ { |
| 76 | dev, err := iocDevInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/iocDevInfo().(f, i, UUIDscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UUID#{}) |
| 77 | if err == syscallscip-go gomod github.com/golang/go/src go1.12 syscall/.ENODEVscip-go gomod github.com/golang/go/src go1.12 syscall/ENODEV. { |
| 78 | continue |
| 79 | } else if err != nil { |
| 80 | return UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#{}, err |
| 81 | } |
| 82 | u.Totalscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#Total. += dev.total_bytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/btrfs_ioctl_dev_info_args#total_bytes. |
| 83 | } |
| 84 | |
| 85 | spaces, err := iocSpaceInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/iocSpaceInfo().(f) |
| 86 | if err != nil { |
| 87 | return UsageInfoscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#{}, err |
| 88 | } |
| 89 | sortscip-go gomod github.com/golang/go/src go1.12 sort/.Sortscip-go gomod github.com/golang/go/src go1.12 sort/Sort().(spaceInfoByBlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoByBlockGroup#(spaces)) |
| 90 | var ( |
| 91 | maxDataRatio int = 1 |
| 92 | mixed bool |
| 93 | ) |
| 94 | for _, s := range spaces { |
| 95 | ratio := 1 |
| 96 | bg := s.Flagsscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#Flags..BlockGroupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceFlags#BlockGroup().() |
| 97 | switch { |
| 98 | case bg&blockGroupRaid0scip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupRaid0. != 0: |
| 99 | ratio = 1 |
| 100 | case bg&blockGroupRaid1scip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupRaid1. != 0: |
| 101 | ratio = 2 |
| 102 | case bg&blockGroupRaid5scip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupRaid5. != 0: |
| 103 | ratio = 0 |
| 104 | case bg&blockGroupRaid6scip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupRaid6. != 0: |
| 105 | ratio = 0 |
| 106 | case bg&blockGroupDupscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupDup. != 0: |
| 107 | ratio = 2 |
| 108 | case bg&blockGroupRaid10scip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupRaid10. != 0: |
| 109 | ratio = 2 |
| 110 | } |
| 111 | if ratio > maxDataRatio { |
| 112 | maxDataRatio = ratio |
| 113 | } |
| 114 | if bg&spaceInfoGlobalRsvscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfoGlobalRsv. != 0 { |
| 115 | u.GlobalReservescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserve. = s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. |
| 116 | u.GlobalReserveUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserveUsed. = s.UsedBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#UsedBytes. |
| 117 | } |
| 118 | if bg&(blockGroupDatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupData.|blockGroupMetadatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupMetadata.) == (blockGroupDatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupData. | blockGroupMetadatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupMetadata.) { |
| 119 | mixed = true |
| 120 | } |
| 121 | if bg&blockGroupDatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupData. != 0 { |
| 122 | u.RawDataUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataUsed. += s.UsedBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#UsedBytes. * uint64(ratio) |
| 123 | u.RawDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataChunks. += s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. * uint64(ratio) |
| 124 | u.LogicalDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalDataChunks. += s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. |
| 125 | } |
| 126 | if bg&blockGroupMetadatascip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupMetadata. != 0 { |
| 127 | u.RawMetaUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaUsed. += s.UsedBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#UsedBytes. * uint64(ratio) |
| 128 | u.RawMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaChunks. += s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. * uint64(ratio) |
| 129 | u.LogicalMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalMetaChunks. += s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. |
| 130 | } |
| 131 | if bg&blockGroupSystemscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/blockGroupSystem. != 0 { |
| 132 | u.SystemUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemUsed. += s.UsedBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#UsedBytes. * uint64(ratio) |
| 133 | u.SystemChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemChunks. += s.TotalBytesscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/spaceInfo#TotalBytes. * uint64(ratio) |
| 134 | } |
| 135 | } |
| 136 | u.TotalChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalChunks. = u.RawDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataChunks. + u.SystemChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemChunks. |
| 137 | u.TotalUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUsed. = u.RawDataUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataUsed. + u.SystemUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#SystemUsed. |
| 138 | if !mixed { |
| 139 | u.TotalChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalChunks. += u.RawMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaChunks. |
| 140 | u.TotalUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUsed. += u.RawMetaUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaUsed. |
| 141 | } |
| 142 | u.TotalUnusedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUnused. = u.Totalscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#Total. - u.TotalChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalChunks. |
| 143 | |
| 144 | u.DataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#DataRatio. = float64(u.RawDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataChunks.) / float64(u.LogicalDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalDataChunks.) |
| 145 | if mixed { |
| 146 | u.MetadataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#MetadataRatio. = u.DataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#DataRatio. |
| 147 | } else { |
| 148 | u.MetadataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#MetadataRatio. = float64(u.RawMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawMetaChunks.) / float64(u.LogicalMetaChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#LogicalMetaChunks.) |
| 149 | } |
| 150 | |
| 151 | // We're able to fill at least DATA for the unused space |
| 152 | // |
| 153 | // With mixed raid levels, this gives a rough estimate but more |
| 154 | // accurate than just counting the logical free space |
| 155 | // (l_data_chunks - l_data_used) |
| 156 | // |
| 157 | // In non-mixed case there's no difference. |
| 158 | u.FreeEstimatedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeEstimated. = uint64(float64(u.RawDataChunksscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataChunks.-u.RawDataUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#RawDataUsed.) / u.DataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#DataRatio.) |
| 159 | |
| 160 | // For mixed-bg the metadata are left out in calculations thus global |
| 161 | // reserve would be lost. Part of it could be permanently allocated, |
| 162 | // we have to subtract the used bytes so we don't go under zero free. |
| 163 | if mixed { |
| 164 | u.FreeEstimatedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeEstimated. -= u.GlobalReservescip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserve. - u.GlobalReserveUsedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#GlobalReserveUsed. |
| 165 | } |
| 166 | u.FreeMinscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeMin. = u.FreeEstimatedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeEstimated. |
| 167 | |
| 168 | // Chop unallocatable space |
| 169 | // FIXME: must be applied per device |
| 170 | if u.TotalUnusedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUnused. >= minUnallocatedThresholdscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/minUnallocatedThreshold. { |
| 171 | u.FreeEstimatedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeEstimated. += uint64(float64(u.TotalUnusedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUnused.) / u.DataRatioscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#DataRatio.) |
| 172 | // Match the calculation of 'df', use the highest raid ratio |
| 173 | u.FreeMinscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#FreeMin. += u.TotalUnusedscip-go gomod github.com/dennwc/btrfs . `github.com/dennwc/btrfs`/UsageInfo#TotalUnused. / uint64(maxDataRatio) |
| 174 | } |
| 175 | return u, nil |
| 176 | } |