title: Which error value should I use?
body: <p>I am currently building a module for the Linux Kernel. My working revision is 3.8-rc3+. My work lead me to implement some <code>ioctl()</code> commands. As you know, my commands should return an appropriate error code to describe what went wrong during the execution. This seems quite simple, but I have a use case for which I can't figure out which error code I should return.</p>
  
  <p>Basically, I want the user to be able to set cryptographic keys for a given device. My module stores the keys in a R-B tree, indexed by the devices unique identifier (a basic <code>int</code>). If the "target" device has already an entry in the tree, then this entry should be updated, otherwise, the module simply adds a newly allocated entry to the tree for that device, with the requested cryptographic key. That said, multiple things can occur while trying to set the key :</p>
  
  <ul>
  <li>Something inside the module might be using the cryptographic key the user wants to update : the module returns <code>EBUSY</code> error.</li>
  <li>There was no entry and allocation failed : <code>ENOMEM</code> error.</li>
  <li>The module is releasing its resources. The existing key entry might be marked for deletion (the entry has a <code>dying</code> flag to signal this) : internally I currently use <code>EPERM</code> error code, since the caller has not the "permission" to alter the entry while it's being destroyed.</li>
  </ul>
  
  <p>As I said, for the latter case, I use <code>EPERM</code> error code, but I have the feeling it's wrong and I don't know which error code I should use for that purpose. Any advice is welcome !</p>
  
  <p><em>I also specified linux tag as the <code>ioctl()</code> could be used within user-space applications.</em></p>
  
  <p><strong>EDIT</strong> : After having read through comments and answers, I think I will make it this way :</p>
  
  <ul>
  <li>When the module is releasing its resources, <code>ESHUTDOWN</code> will be returned.</li>
  <li>When only the target key is being destroyed, while the rest of the
  tree is still sane, <code>EACCES</code> will be used.</li>
  </ul>