title: Can someone help me replace "lock_kernel" on a block device driver?
body: <p>Thank you for looking at this post. I am trying to patch up a network block device driver. If you need to see the sources they are at http : / / code.ximeta.com.</p>
  
  <p>I noticed that lock_kernel() seems deprecated as of linux 2.6.37.  I read "The new way of ioctl()" and found that device drivers now should perform a specific lock before operating. </p>
  
  <p>So I would like some advice replacing this if possible.</p>
  
  <p>I have found two sections in the current code that I think are related, in the block folder section.  </p>
  
  <pre><code>Source 
        block-&gt;io.c
             -&gt;ctrldev.c
  </code></pre>
  
  <p>I put snippets from each for your consideration.</p>
  
  <p>io.c contains one call to lock_kernel:</p>
  
  <pre><code>NDAS_SAL_API xbool     sal_file_get_size(sal_file file, xuint64* size)
  {
      definitions and declarations etc..
  
  lock_kernel();
  
  #ifdef HAVE_UNLOCKED_IOCTL
      if (filp-&gt;f_op-&gt;unlocked_ioctl) {   
         some small statements
  
         error = filp-&gt;f_op-&gt;unlocked_ioctl(filp, BLKGETSIZE64, (unsigned long)size);
  
         actions if error or not etc.
     }
  #endif
  
     unlock_kernel(); 
     return ret;
  }
  </code></pre>
  
  <p>And ctrldev.c contains the main io function:</p>
  
  <pre><code>#include &lt;linux/spinlock.h&gt; // spinklock_t
  #include &lt;linux/semaphore.h&gt; // struct semaphore
  #include &lt;asm/atomic.h&gt; // atomic
  #include &lt;linux/interrupt.h&gt;
  #include &lt;linux/fs.h&gt;
  #include &lt;asm/uaccess.h&gt;
  #include &lt;linux/ide.h&gt;
  #include &lt;linux/smp_lock.h&gt;
  #include &lt;linux/time.h&gt;
  
  ......
  
  int ndas_ctrldev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  {
    lots of operations and functions. 
  
    return result;
  }
  </code></pre>
  
  <p>Later ndas_ctrldev_ioctl function is set as the former .ioctl.</p>
  
  <pre><code>static struct file_operations ndasctrl_fops = {
      .write = ndas_ctrldev_write,
      .read = ndas_ctrldev_read,
      .open = ndas_ctrldev_open,
      .release = ndas_ctrldev_release,
      .ioctl = ndas_ctrldev_ioctl, 
  };
  </code></pre>
  
  <p>Now I want to convert this to avoid using lock_kernel();</p>
  
  <p>According to my understanding I will modified the former sections as below:</p>
  
  <pre><code>NDAS_SAL_API xbool     sal_file_get_size(sal_file file, xuint64* size)
  {
      definitions and declarations etc..
  
  #ifndef HAVE_UNLOCKED_IOCTL
      lock_kernel();
  #endif
  
  #ifdef HAVE_UNLOCKED_IOCTL
      if (filp-&gt;f_op-&gt;unlocked_ioctl) {   
         some small statements
  
         error = filp-&gt;f_op-&gt;unlocked_ioctl(filp, BLKGETSIZE64, (unsigned long)size);
  
         actions if error or not etc.
     }
  #endif
  
  #ifndef HAVE_UNLOCKED_IOCTL
     unlock_kernel(); 
  #endif
     return ret;
  
  }
  
  #ifdef HAVE_UNLOCKED_IOCTL
  long ndas_ctrldev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  #else
  int ndas_ctrldev_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  #endif
  {
  
  #ifdef HAVE_UNLOCKED_IOCTL
    ! add some sort of lock here !
  #endif
  
    lots of operations and functions. 
  
  #ifdef HAVE_UNLOCKED_IOCTL
    ! add unlock statement here  !
  #endif
    return result;
  }
  
  static struct file_operations ndasctrl_fops = {
      .write = ndas_ctrldev_write,
      .read = ndas_ctrldev_read,
      .open = ndas_ctrldev_open,
      .release = ndas_ctrldev_release,
  #ifdef HAVE_UNLOCKED_IOCTL
      .unlocked_ioctl = ndas_ctrldev_ioctl, 
  #else
      .ioctl = ndas_ctrldev_ioctl, 
  #endif
  };
  </code></pre>
  
  <p>So, I would ask the following advice.</p>
  
  <ol>
  <li><p>Does this look like the right
  proceedure? </p></li>
  <li><p>Do I understand correct to move the
  lock into the io function?</p></li>
  <li><p>Based on the includes in crtrldev.c, can you
  recommend any lock off the top of
  your head? (I tried to research some
  other drivers dealing with filp and
  lock_kernel, but I am too much a
  noob to find the answer right away.)</p></li>
  </ol>