util_xfs_linux.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build linux
  2. /*
  3. Copyright 2016 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package e2enode
  15. import (
  16. "path/filepath"
  17. "syscall"
  18. "k8s.io/utils/mount"
  19. )
  20. func detectMountpoint(m mount.Interface, path string) string {
  21. path, err := filepath.Abs(path)
  22. if err == nil {
  23. path, err = filepath.EvalSymlinks(path)
  24. }
  25. if err != nil {
  26. return ""
  27. }
  28. for path != "" && path != "/" {
  29. isNotMount, err := m.IsLikelyNotMountPoint(path)
  30. if err != nil {
  31. return ""
  32. }
  33. if !isNotMount {
  34. return path
  35. }
  36. path = filepath.Dir(path)
  37. }
  38. return "/"
  39. }
  40. const (
  41. xfsMagic = 0x58465342
  42. )
  43. // XFS over-allocates and then eventually removes that excess allocation.
  44. // That can lead to a file growing beyond its eventual size, causing
  45. // an unnecessary eviction:
  46. //
  47. // % ls -ls
  48. // total 32704
  49. // 32704 -rw-r--r-- 1 rkrawitz rkrawitz 20971520 Jan 15 13:16 foo.bin
  50. //
  51. // This issue can be hit regardless of the means used to count storage.
  52. // It is not present in ext4fs.
  53. func isXfs(dir string) bool {
  54. mountpoint := detectMountpoint(mount.New(""), dir)
  55. if mountpoint == "" {
  56. return false
  57. }
  58. var buf syscall.Statfs_t
  59. err := syscall.Statfs(mountpoint, &buf)
  60. if err != nil {
  61. return false
  62. }
  63. return buf.Type == xfsMagic
  64. }