@@ -124,12 +124,14 @@ const (
124124 readAheadKBMountFlagRegexPattern = "^read_ahead_kb=(.+)$"
125125 btrfsReclaimDataRegexPattern = "^btrfs-allocation-data-bg_reclaim_threshold=(\\ d{1,2})$" // 0-99 are valid, incl. 00
126126 btrfsReclaimMetadataRegexPattern = "^btrfs-allocation-metadata-bg_reclaim_threshold=(\\ d{1,2})$" // ditto ^
127+ btrfsReadAheadKBRegexPattern = "^btrfs-bdi-read_ahead_kb=(\\ d+)$"
127128)
128129
129130var (
130131 readAheadKBMountFlagRegex = regexp .MustCompile (readAheadKBMountFlagRegexPattern )
131132 btrfsReclaimDataRegex = regexp .MustCompile (btrfsReclaimDataRegexPattern )
132133 btrfsReclaimMetadataRegex = regexp .MustCompile (btrfsReclaimMetadataRegexPattern )
134+ btrfsReadAheadKBRegex = regexp .MustCompile (btrfsReadAheadKBRegexPattern )
133135)
134136
135137func getDefaultFsType () string {
@@ -402,7 +404,7 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
402404 // Part 3: Mount device to stagingTargetPath
403405 fstype := getDefaultFsType ()
404406
405- var btrfsReclaimData , btrfsReclaimMetadata string
407+ var btrfsReclaimData , btrfsReclaimMetadata , btrfsReadAheadKb string
406408 shouldUpdateReadAhead := false
407409 var readAheadKB int64
408410 options := []string {}
@@ -418,7 +420,7 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
418420 }
419421
420422 if mnt .FsType == fsTypeBtrfs {
421- btrfsReclaimData , btrfsReclaimMetadata = extractBtrfsReclaimFlags (mnt .MountFlags )
423+ btrfsReclaimData , btrfsReclaimMetadata , btrfsReadAheadKb = extractBtrfsFlags (mnt .MountFlags )
422424 }
423425 } else if blk := volumeCapability .GetBlock (); blk != nil {
424426 // Noop for Block NodeStageVolume
@@ -465,47 +467,52 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
465467 }
466468 }
467469
468- // Part 5: Update read_ahead
470+ // Part 5: Update read_ahead for the block device
469471 if shouldUpdateReadAhead {
470472 if err := ns .updateReadAhead (devicePath , readAheadKB ); err != nil {
471473 return nil , status .Errorf (codes .Internal , "failure updating readahead for %s to %dKB: %v" , devicePath , readAheadKB , err .Error ())
472474 }
473475 }
474476
475- // Part 6: if configured, write sysfs values
477+ btrfsSysfs := map [string ]string {}
478+
479+ if btrfsReadAheadKb != "" {
480+ btrfsSysfs ["bdi/read_ahead_kb" ] = btrfsReadAheadKb
481+ }
482+
476483 if ! readonly {
477- sysfs := map [string ]string {}
478484 if btrfsReclaimData != "" {
479- sysfs ["allocation/data/bg_reclaim_threshold" ] = btrfsReclaimData
485+ btrfsSysfs ["allocation/data/bg_reclaim_threshold" ] = btrfsReclaimData
480486 }
481487 if btrfsReclaimMetadata != "" {
482- sysfs ["allocation/metadata/bg_reclaim_threshold" ] = btrfsReclaimMetadata
483- }
484-
485- if len (sysfs ) > 0 {
486- args := []string {"--match-tag" , "UUID" , "--output" , "value" , stagingTargetPath }
487- cmd := ns .Mounter .Exec .Command ("blkid" , args ... )
488- var stderr bytes.Buffer
489- cmd .SetStderr (& stderr )
490- klog .V (4 ).Infof (
491- "running %q for volume %s" ,
492- strings .Join (append ([]string {"blkid" }, args ... ), " " ),
493- volumeID ,
494- )
495- uuid , err := cmd .Output ()
496- if err != nil {
497- klog .Errorf ("blkid failed for %s. stderr:\n %s" , volumeID , stderr .String ())
498- return nil , status .Errorf (codes .Internal , "blkid failed: %v" , err )
499- }
500- uuid = bytes .TrimRight (uuid , "\n " )
488+ btrfsSysfs ["allocation/metadata/bg_reclaim_threshold" ] = btrfsReclaimMetadata
489+ }
490+ }
501491
502- for key , value := range sysfs {
503- path := fmt .Sprintf ("%s/fs/btrfs/%s/%s" , ns .SysfsPath , uuid , key )
504- if err := writeSysfs (path , value ); err != nil {
505- return nil , status .Error (codes .Internal , err .Error ())
506- }
507- klog .V (4 ).Infof ("NodeStageVolume set %s %s=%s" , volumeID , key , value )
492+ // Part 6: if configured, write sysfs values
493+ if len (btrfsSysfs ) > 0 {
494+ args := []string {"--match-tag" , "UUID" , "--output" , "value" , stagingTargetPath }
495+ cmd := ns .Mounter .Exec .Command ("blkid" , args ... )
496+ var stderr bytes.Buffer
497+ cmd .SetStderr (& stderr )
498+ klog .V (4 ).Infof (
499+ "running %q for volume %s" ,
500+ strings .Join (append ([]string {"blkid" }, args ... ), " " ),
501+ volumeID ,
502+ )
503+ uuid , err := cmd .Output ()
504+ if err != nil {
505+ klog .Errorf ("blkid failed for %s. stderr:\n %s" , volumeID , stderr .String ())
506+ return nil , status .Errorf (codes .Internal , "blkid failed: %v" , err )
507+ }
508+ uuid = bytes .TrimRight (uuid , "\n " )
509+
510+ for key , value := range btrfsSysfs {
511+ path := fmt .Sprintf ("%s/fs/btrfs/%s/%s" , ns .SysfsPath , uuid , key )
512+ if err := writeSysfs (path , value ); err != nil {
513+ return nil , status .Error (codes .Internal , err .Error ())
508514 }
515+ klog .V (4 ).Infof ("NodeStageVolume set %s %s=%s" , volumeID , key , value )
509516 }
510517 }
511518
@@ -526,7 +533,6 @@ func writeSysfs(path, value string) (_err error) {
526533 if _ , err := f .Write ([]byte (value )); err != nil {
527534 return err
528535 }
529-
530536 return nil
531537}
532538
@@ -546,16 +552,18 @@ func (ns *GCENodeServer) updateReadAhead(devicePath string, readAheadKB int64) e
546552 return nil
547553}
548554
549- func extractBtrfsReclaimFlags (mountFlags []string ) (string , string ) {
550- var reclaimData , reclaimMetadata string
555+ func extractBtrfsFlags (mountFlags []string ) (string , string , string ) {
556+ var reclaimData , reclaimMetadata , readAheadKb string
551557 for _ , mountFlag := range mountFlags {
552558 if got := btrfsReclaimDataRegex .FindStringSubmatch (mountFlag ); len (got ) == 2 {
553559 reclaimData = got [1 ]
554560 } else if got := btrfsReclaimMetadataRegex .FindStringSubmatch (mountFlag ); len (got ) == 2 {
555561 reclaimMetadata = got [1 ]
562+ } else if got := btrfsReadAheadKBRegex .FindStringSubmatch (mountFlag ); len (got ) == 2 {
563+ readAheadKb = got [1 ]
556564 }
557565 }
558- return reclaimData , reclaimMetadata
566+ return reclaimData , reclaimMetadata , readAheadKb
559567}
560568
561569func extractReadAheadKBMountFlag (mountFlags []string ) (int64 , bool , error ) {
0 commit comments