// Should be called with devices.Lock() held.
323349
func (devices *DeviceSet) unregisterDevice(id int, hash string) error {
	logrus.Debugf("devmapper: unregisterDevice(%v, %v)", id, hash)
	info := &devInfo{
		Hash:     hash,
		DeviceID: id,
	}

	delete(devices.Devices, hash)

	if err := devices.removeMetadata(info); err != nil {
		logrus.Debugf("devmapper: Error removing metadata: %s", err)
		return err
	}

	return nil
}
451191
func (e *Unlock) Run(m libkb.MetaContext) (err error) {
	defer m.Trace("Unlock#Run", func() error { return err })()

	un := m.CurrentUsername()
	m.Debug("Active device: %+v", *m.ActiveDevice())
	if un.IsNil() {
		return libkb.NewNoUsernameError()
	}
	m = m.WithNewProvisionalLoginContext()
	if e.passphrase == "" {
		err = libkb.PassphraseLoginPromptThenSecretStore(m, un.String(), 5, false /* failOnStoreError */)
	} else {
		err = libkb.PassphraseLoginNoPromptThenSecretStore(m, un.String(), e.passphrase, false /* failOnStoreError */)
	}
	if err != nil {
		return err
	}
	m.CommitProvisionalLogin()

	return nil
}
1132078
func DeviceMotion(listener func(*vecty.Event)) *vecty.EventListener {
	return &vecty.EventListener{Name: "devicemotion", Listener: listener}
}
304203
func (w *wrapper) Device(id protocol.DeviceID) (DeviceConfiguration, bool) {
	w.mut.Lock()
	defer w.mut.Unlock()
	for _, device := range w.cfg.Devices {
		if device.DeviceID == id {
			return device.Copy(), true
		}
	}
	return DeviceConfiguration{}, false
}
1177284
func (s *UpdateDeviceStatusInput) SetDeviceRememberedStatus(v string) *UpdateDeviceStatusInput {
	s.DeviceRememberedStatus = &v
	return s
}
317193
func (device *Device) Release() {
	C.cairo_device_release(device.Ptr)
	if err := device.status(); err != nil {
		panic(err)
	}
}
439208
func (i *InmemHABackend) LockWith(key, value string) (physical.Lock, error) {
	l := &InmemLock{
		in:    i,
		key:   key,
		value: value,
	}
	return l, nil
}
72679
func (device *Device) Finish() {
	C.cairo_device_finish(device.Ptr)
	if err := device.status(); err != nil {
		panic(err)
	}
}
980452
func (r *keyring) Lock(passphrase []byte) error {
	r.mu.Lock()
	defer r.mu.Unlock()
	if r.locked {
		return errLocked
	}

	r.locked = true
	r.passphrase = passphrase
	return nil
}
225998
func (l *lockfile) Touch() error {
	l.stateMutex.Lock()
	if !l.locked || (l.locktype != unix.F_WRLCK) {
		panic("attempted to update last-writer in lockfile without the write lock")
	}
	l.stateMutex.Unlock()
	l.lw = stringid.GenerateRandomID()
	id := []byte(l.lw)
	_, err := unix.Seek(int(l.fd), 0, os.SEEK_SET)
	if err != nil {
		return err
	}
	n, err := unix.Write(int(l.fd), id)
	if err != nil {
		return err
	}
	if n != len(id) {
		return unix.ENOSPC
	}
	err = unix.Fsync(int(l.fd))
	if err != nil {
		return err
	}
	return nil
}
933545
func (c *sioClient) WaitForDetachedDevice(token string) error {
	if token == "" {
		return fmt.Errorf("invalid detach token")
	}

	// wait for attach.Token to show up in local device list
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	timer := time.NewTimer(30 * time.Second)
	defer timer.Stop()

	for {
		select {
		case <-ticker.C:
			devMap, err := c.Devs()
			if err != nil {
				klog.Error(log("failed while waiting for volume to unmap/detach: %v", err))
				return err
			}
			go func() {
				klog.V(4).Info(log("waiting for volume %s to be unmapped/detached", token))
			}()
			// cant find vol id, then ok.
			if _, ok := devMap[token]; !ok {
				return nil
			}
		case <-timer.C:
			klog.Error(log("timed out while waiting for volume %s to be unmapped/detached", token))
			return fmt.Errorf("volume detach timeout")
		}
	}
}
730113
func (m *MutexKV) Lock(key string) {
	log.Printf("[DEBUG] Locking %q", key)
	m.get(key).Lock()
	log.Printf("[DEBUG] Locked %q", key)
}
908667
func (e *DeviceWrap) SwitchConfigAndActiveDevice(m libkb.MetaContext) (err error) {
	defer m.Trace("DeviceWrap#SetActiveDevice", func() error { return err })()

	if err = e.refreshMe(m); err != nil {
		return err
	}

	salt, err := e.args.Me.GetSalt()
	if err != nil {
		return err
	}
	me := e.args.Me
	// Atomically swap to the new config and active device
	if err := m.SwitchUserNewConfigActiveDevice(me.ToUserVersion(), me.GetNormalizedName(), salt,
		e.deviceID, e.signingKey, e.encryptionKey, e.args.DeviceName); err != nil {
		return err
	}

	// Sync down secrets for future offline login attempts to work.
	// This will largely just download what we just uploaded, but it's
	// easy to do this way.
	_, w := m.ActiveDevice().SyncSecrets(m)
	if w != nil {
		m.Warning("Error sync secrets: %s", w.Error())
	}
	return nil
}
863403
func (a *Application) DeviceConstraints() (map[string]DeviceConstraints, error) {
	cons, err := readDeviceConstraints(a.st, a.deviceConstraintsKey())
	if errors.IsNotFound(err) {
		return nil, nil
	} else if err != nil {
		return nil, errors.Trace(err)
	}
	return cons, nil
}
246894
func (devices *DeviceSet) Shutdown(home string) error {
	logger := logrus.WithField("storage-driver", "devicemapper")

	logger.Debugf("[deviceset %s] Shutdown()", devices.devicePrefix)
	logger.Debugf("Shutting down DeviceSet: %s", devices.root)
	defer logger.Debugf("[deviceset %s] Shutdown() END", devices.devicePrefix)

	// Stop deletion worker. This should start delivering new events to
	// ticker channel. That means no new instance of cleanupDeletedDevice()
	// will run after this call. If one instance is already running at
	// the time of the call, it must be holding devices.Lock() and
	// we will block on this lock till cleanup function exits.
	devices.deletionWorkerTicker.Stop()

	devices.Lock()
	// Save DeviceSet Metadata first. Docker kills all threads if they
	// don't finish in certain time. It is possible that Shutdown()
	// routine does not finish in time as we loop trying to deactivate
	// some devices while these are busy. In that case shutdown() routine
	// will be killed and we will not get a chance to save deviceset
	// metadata. Hence save this early before trying to deactivate devices.
	devices.saveDeviceSetMetaData()
	devices.unmountAndDeactivateAll(path.Join(home, "mnt"))
	devices.Unlock()

	info, _ := devices.lookupDeviceWithLock("")
	if info != nil {
		info.lock.Lock()
		devices.Lock()
		if err := devices.deactivateDevice(info); err != nil {
			logger.Debugf("Shutdown deactivate base , error: %s", err)
		}
		devices.Unlock()
		info.lock.Unlock()
	}

	devices.Lock()
	if devices.thinPoolDevice == "" {
		if err := devices.deactivatePool(); err != nil {
			logger.Debugf("Shutdown deactivate pool , error: %s", err)
		}
	}
	devices.Unlock()

	return nil
}
366886
func (mc MongoClient) EventsForDevice(id string) ([]contract.Event, error) {
	return mc.mapEvents(mc.getEvents(bson.M{"device": id}))
}
1302548
func SuspendDevice(name string) error {
	task, err := TaskCreateNamed(deviceSuspend, name)
	if task == nil {
		return err
	}
	if err := task.run(); err != nil {
		return fmt.Errorf("devicemapper: Error running deviceSuspend %s", err)
	}
	return nil
}