<commit_before>/*
Copyright 2018 The Ceph-CSI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cephfs

import (
	"fmt"
	"os"
	"path"
	"strconv"
	"strings"

	"github.com/ceph/ceph-csi/pkg/util"

	"k8s.io/klog"
)

const (
	csiSubvolumeGroup = "csi"
)

var (
	// cephfsInit is used to create "csi" subvolume group for the first time the csi plugin loads.
	// Subvolume group create gets called every time the plugin loads, though it doesn't result in error
	// its unnecessary
	cephfsInit = false
)

func getCephRootVolumePathLocalDeprecated(volID volumeID) string {
	return path.Join(getCephRootPathLocalDeprecated(volID), "csi-volumes", string(volID))
}

func getVolumeRootPathCephDeprecated(volID volumeID) string {
	return path.Join("/", "csi-volumes", string(volID))
}

func getCephRootPathLocalDeprecated(volID volumeID) string {
	return fmt.Sprintf("%s/controller/volumes/root-%s", PluginFolder, string(volID))
}

func getVolumeRootPathCeph(volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (string, error) {
	stdout, _, err := util.ExecCommand(
		"ceph",
		"fs",
		"subvolume",
		"getpath",
		volOptions.FsName,
		string(volID),
		"--group_name",
		csiSubvolumeGroup,
		"-m", volOptions.Monitors,
		"-c", util.CephConfigPath,
		"-n", cephEntityClientPrefix+cr.ID,
		"--keyfile="+cr.KeyFile)

	if err != nil {
		klog.Errorf("failed to get the rootpath for the vol %s(%s)", string(volID), err)
		return "", err
	}
	return strings.TrimSuffix(string(stdout), "\n"), nil
}

func createVolume(volOptions *volumeOptions, cr *util.Credentials, volID volumeID, bytesQuota int64) error {
	//TODO: When we support multiple fs, need to hande subvolume group create for all fs's
	if !cephfsInit {
		err := execCommandErr(
			"ceph",
			"fs",
			"subvolumegroup",
			"create",
			volOptions.FsName,
			csiSubvolumeGroup,
			"--mode",
			"777",
			"-m", volOptions.Monitors,
			"-c", util.CephConfigPath,
			"-n", cephEntityClientPrefix+cr.ID,
			"--keyfile="+cr.KeyFile)
		if err != nil {
			klog.Errorf("failed to create subvolume group csi, for the vol %s(%s)", string(volID), err)
			return err
		}
		klog.V(4).Infof("cephfs: created subvolume group csi")
		cephfsInit = true
	}
	err := execCommandErr(
		"ceph",
		"fs",
		"subvolume",
		"create",
		volOptions.FsName,
		string(volID),
		strconv.FormatInt(bytesQuota, 10),
		"--group_name",
		csiSubvolumeGroup,
		"--pool_layout", volOptions.Pool,
		"-m", volOptions.Monitors,
		"-c", util.CephConfigPath,
		"-n", cephEntityClientPrefix+cr.ID,
		"--keyfile="+cr.KeyFile)
	if err != nil {
		klog.Errorf("failed to create subvolume %s(%s) in fs %s", string(volID), err, volOptions.FsName)
		return err<commit_msg>cephfs: set the mode of the FS subvolumes<commit_after>/*
Copyright 2018 The Ceph-CSI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cephfs

import (
	"fmt"
	"os"
	"path"
	"strconv"
	"strings"

	"github.com/ceph/ceph-csi/pkg/util"

	"k8s.io/klog"
)

const (
	csiSubvolumeGroup = "csi"
)

var (
	// cephfsInit is used to create "csi" subvolume group for the first time the csi plugin loads.
	// Subvolume group create gets called every time the plugin loads, though it doesn't result in error
	// its unnecessary
	cephfsInit = false
)

func getCephRootVolumePathLocalDeprecated(volID volumeID) string {
	return path.Join(getCephRootPathLocalDeprecated(volID), "csi-volumes", string(volID))
}

func getVolumeRootPathCephDeprecated(volID volumeID) string {
	return path.Join("/", "csi-volumes", string(volID))
}

func getCephRootPathLocalDeprecated(volID volumeID) string {
	return fmt.Sprintf("%s/controller/volumes/root-%s", PluginFolder, string(volID))
}

func getVolumeRootPathCeph(volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (string, error) {
	stdout, _, err := util.ExecCommand(
		"ceph",
		"fs",
		"subvolume",
		"getpath",
		volOptions.FsName,
		string(volID),
		"--group_name",
		csiSubvolumeGroup,
		"-m", volOptions.Monitors,
		"-c", util.CephConfigPath,
		"-n", cephEntityClientPrefix+cr.ID,
		"--keyfile="+cr.KeyFile)

	if err != nil {
		klog.Errorf("failed to get the rootpath for the vol %s(%s)", string(volID), err)
		return "", err
	}
	return strings.TrimSuffix(string(stdout), "\n"), nil
}

func createVolume(volOptions *volumeOptions, cr *util.Credentials, volID volumeID, bytesQuota int64) error {
	//TODO: When we support multiple fs, need to hande subvolume group create for all fs's
	if !cephfsInit {
		err := execCommandErr(
			"ceph",
			"fs",
			"subvolumegroup",
			"create",
			volOptions.FsName,
			csiSubvolumeGroup,
			"-m", volOptions.Monitors,
			"-c", util.CephConfigPath,
			"-n", cephEntityClientPrefix+cr.ID,
			"--keyfile="+cr.KeyFile)
		if err != nil {
			klog.Errorf("failed to create subvolume group csi, for the vol %s(%s)", string(volID), err)
			return err
		}
		klog.V(4).Infof("cephfs: created subvolume group csi")
		cephfsInit = true
	}
	err := execCommandErr(
		"ceph",
		"fs",
		"subvolume",
		"create",
		volOptions.FsName,
		string(volID),
		strconv.FormatInt(bytesQuota, 10),
		"--group_name",
		csiSubvolumeGroup,
		"--pool_layout", volOptions.Pool,
		"--mode", "777",
		"-m", volOptions.Monitors,
		"-c", util.CephConfigPath,
		"-n", cephEntityClientPrefix+cr.ID,
		"--keyfile="+cr.KeyFile)
	if err != nil {
		klog.Errorf("failed to create subvolume %s(%s) in fs %s", string(volID), err, volOptions.FsName)
		return err
{'fraction_non_alphanumeric': 0.1284995281535074, 'fraction_numerical': 0.0053475935828877, 'mean_word_length': 4.325795644891122, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '194111', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2170, 'n_words': 626}