/* ----------------------------- Device ---------------------------------- */
547748
func (mc MongoClient) AddDevice(d contract.Device) (string, error) {
	s := mc.session.Copy()
	defer s.Close()

	col := s.DB(mc.database.Name).C(db.Device)

	// Check if the name exist (Device names must be unique)
	count, err := col.Find(bson.M{"name": d.Name}).Count()
	if err != nil {
		return "", errorMap(err)
	}
	if count > 0 {
		return "", db.ErrNotUnique
	}

	var mapped models.Device
	id, err := mapped.FromContract(d, mc, mc, mc, mc)
	if err != nil {
		return "", errors.New("FromContract failed")
	}

	mapped.TimestampForAdd()

	return id, errorMap(col.Insert(mapped))
}
56190
func (d *driver) NextDevice(
	ctx types.Context,
	opts types.Store) (string, error) {

	return "", types.ErrNotImplemented
}
619150
func (devices *DeviceSet) AddDevice(hash, baseHash string, storageOpt map[string]string) error {
	logrus.WithField("storage-driver", "devicemapper").Debugf("AddDevice START(hash=%s basehash=%s)", hash, baseHash)
	defer logrus.WithField("storage-driver", "devicemapper").Debugf("AddDevice END(hash=%s basehash=%s)", hash, baseHash)

	// If a deleted device exists, return error.
	baseInfo, err := devices.lookupDeviceWithLock(baseHash)
	if err != nil {
		return err
	}

	if baseInfo.Deleted {
		return fmt.Errorf("devmapper: Base device %v has been marked for deferred deletion", baseInfo.Hash)
	}

	baseInfo.lock.Lock()
	defer baseInfo.lock.Unlock()

	devices.Lock()
	defer devices.Unlock()

	// Also include deleted devices in case hash of new device is
	// same as one of the deleted devices.
	if info, _ := devices.lookupDevice(hash); info != nil {
		return fmt.Errorf("devmapper: device %s already exists. Deleted=%v", hash, info.Deleted)
	}

	size, err := devices.parseStorageOpt(storageOpt)
	if err != nil {
		return err
	}

	if size == 0 {
		size = baseInfo.Size
	}

	if size < baseInfo.Size {
		return fmt.Errorf("devmapper: Container size cannot be smaller than %s", units.HumanSize(float64(baseInfo.Size)))
	}

	if err := devices.takeSnapshot(hash, baseInfo, size); err != nil {
		return err
	}

	// Grow the container rootfs.
	if size > baseInfo.Size {
		info, err := devices.lookupDevice(hash)
		if err != nil {
			return err
		}

		if err := devices.growFS(info); err != nil {
			return err
		}
	}

	return nil
}
1084786
func NewDeviceBackend(st *State) (*deviceBackend, error) {
	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &deviceBackend{
		mb:          st,
		settings:    NewStateSettings(st),
		modelType:   m.Type(),
		config:      m.ModelConfig,
		application: st.Application,
		unit:        st.Unit,
		machine:     st.Machine,
	}, nil
}
1182476
func pathDeviceType(path string) (deviceType, error) {
	fi, err := os.Stat(path)
	if err != nil {
		return modeUnsupported, err
	}
	switch mode := fi.Mode(); {
	case mode&os.ModeDevice != 0:
		return modeBlock, nil
	case mode.IsRegular():
		return modeFile, nil
	default:
		return modeUnsupported, nil
	}
}
264791
func (addr *Address) Device() (*LinkLayerDevice, error) {
	return addr.machineProxy().LinkLayerDevice(addr.doc.DeviceName)
}
718128
func (m *model) DeviceStatistics() map[string]stats.DeviceStatistics {
	res := make(map[string]stats.DeviceStatistics)
	for id := range m.cfg.Devices() {
		res[id.String()] = m.deviceStatRef(id).GetStatistics()
	}
	return res
}
1114217
func (a *ActiveDevice) DeviceID() keybase1.DeviceID {
	a.RLock()
	defer a.RUnlock()
	return a.deviceID
}
182599
func (s *Volume) SetDevice(v string) *Volume {
	s.Device = &v
	return s
}
1052049
func (d *DeviceMapperDriver) addDevice(headDeviceHash string) (string, error) {

	// Generate the device ID.
	deviceHash, err := utils.NewUUID62()
	if err != nil {
		return "", err
	}

	// Create the device.
	if err := d.DeviceSet.AddDevice(deviceHash, headDeviceHash); err != nil {
		glog.Errorf("Unable to create device %s: %s", deviceHash, err)
		return "", err
	}

	if headDeviceHash == "" {

		// Activate the device.
		d.DeviceSet.GetDeviceStatus(deviceHash)

		// CC-2219: Ensure the device base size is equal or greater than the
		// configured dm.basesize.
		size, err := d.deviceSize(deviceHash)
		if err != nil {
			glog.Errorf("Could not read the size of device %s: %s", deviceHash, err)
			return "", err
		}
		if baseSize := d.baseSize(); baseSize > size {

			// Make the device to match dm.basesize
			glog.V(2).Infof("Device size is smaller than dm.basesize; expanding")
			if err := d.resize(deviceHash, baseSize); err != nil {
				glog.Errorf("Could not update the size of the device %s: %s", deviceHash, err)
				return "", err
			}
		}
	}

	return deviceHash, nil
}
993767
func Device(device Dev, frontend, backend *Socket) error {
	if !(frontend.opened && backend.opened) {
		return ErrorSocketClosed
	}
	_, err := C.zmq_device(C.int(device), frontend.soc, backend.soc)
	return errget(err)
}
797924
func NewDeviceAllocator() DeviceAllocator {
	possibleDevices := make(map[mountDevice]int)
	for _, firstChar := range []rune{'b', 'c'} {
		for i := 'a'; i <= 'z'; i++ {
			dev := mountDevice([]rune{firstChar, i})
			possibleDevices[dev] = 0
		}
	}
	return &deviceAllocator{
		possibleDevices: possibleDevices,
		counter:         0,
	}
}
1160339
func (pdev podDevices) devices() map[string]sets.String {
	ret := make(map[string]sets.String)
	for _, containerDevices := range pdev {
		for _, resources := range containerDevices {
			for resource, devices := range resources {
				if _, exists := ret[resource]; !exists {
					ret[resource] = sets.NewString()
				}
				if devices.allocResp != nil {
					ret[resource] = ret[resource].Union(devices.deviceIds)
				}
			}
		}
	}
	return ret
}
386081
func (w *wrapper) IgnoredDevice(id protocol.DeviceID) bool {
	w.mut.Lock()
	defer w.mut.Unlock()
	for _, device := range w.cfg.IgnoredDevices {
		if device.ID == id {
			return true
		}
	}
	return false
}
1109148
func (_m *DBClient) GetDeviceByName(n string) (models.Device, error) {
	ret := _m.Called(n)

	var r0 models.Device
	if rf, ok := ret.Get(0).(func(string) models.Device); ok {
		r0 = rf(n)
	} else {
		r0 = ret.Get(0).(models.Device)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(string) error); ok {
		r1 = rf(n)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
1350725
func DeviceScanType(config gofig.Config) types.DeviceScanType {
	return types.ParseDeviceScanType(config.GetInt(types.ConfigDeviceScanType))
}
1048623
func (devices *DeviceSet) MountDevice(hash, path, mountLabel string) error {
	info, err := devices.lookupDeviceWithLock(hash)
	if err != nil {
		return err
	}

	if info.Deleted {
		return fmt.Errorf("devmapper: Can't mount device %v as it has been marked for deferred deletion", info.Hash)
	}

	info.lock.Lock()
	defer info.lock.Unlock()

	devices.Lock()
	defer devices.Unlock()

	if info.mountCount > 0 {
		if path != info.mountPath {
			return fmt.Errorf("devmapper: Trying to mount devmapper device in multiple places (%s, %s)", info.mountPath, path)
		}

		info.mountCount++
		return nil
	}

	if err := devices.activateDeviceIfNeeded(info, false); err != nil {
		return fmt.Errorf("devmapper: Error activating devmapper device for '%s': %s", hash, err)
	}

	fstype, err := ProbeFsType(info.DevName())
	if err != nil {
		return err
	}

	options := ""

	if fstype == "xfs" {
		// XFS needs nouuid or it can't mount filesystems with the same fs
		options = joinMountOptions(options, "nouuid")
	}

	options = joinMountOptions(options, devices.mountOptions)
	options = joinMountOptions(options, label.FormatMountLabel("", mountLabel))

	if err := mount.Mount(info.DevName(), path, fstype, options); err != nil {
		return fmt.Errorf("devmapper: Error mounting '%s' on '%s': %s", info.DevName(), path, err)
	}

	info.mountCount = 1
	info.mountPath = path

	return nil
}