From patchwork Sun Sep 6 17:59:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Duncan X-Patchwork-Id: 7132381 Return-Path: X-Original-To: patchwork-linux-scsi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D80AF9F32B for ; Sun, 6 Sep 2015 17:59:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 63900206CE for ; Sun, 6 Sep 2015 17:59:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5A16C206CC for ; Sun, 6 Sep 2015 17:59:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753262AbbIFR7w (ORCPT ); Sun, 6 Sep 2015 13:59:52 -0400 Received: from mx2.suse.de ([195.135.220.15]:46817 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753223AbbIFR7p (ORCPT ); Sun, 6 Sep 2015 13:59:45 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 7D650ACCA; Sun, 6 Sep 2015 17:59:43 +0000 (UTC) Received: by worklaptop.gonzoleeman.net (Postfix, from userid 1000) id C933340C76; Sun, 6 Sep 2015 10:59:36 -0700 (PDT) From: Lee Duncan To: linux-scsi Cc: Christoph Hellwig , hare@suse.com, jthumshirn@suse.de, JBottomley@Parallels.com, Lee Duncan Subject: [PATCH 1/6] Add ida helper routines Date: Sun, 6 Sep 2015 10:59:29 -0700 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: In-Reply-To: References: Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Clients of the ida API routinely follow the same steps to allocate and ida index, as well as to free said index. These helper routines should make it a little easier to use these APIs. Signed-off-by: Lee Duncan --- include/linux/idr.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/linux/idr.h b/include/linux/idr.h index 013fd9bc4cb6..5a9526dc6298 100644 --- a/include/linux/idr.h +++ b/include/linux/idr.h @@ -16,6 +16,8 @@ #include #include #include +#include +#include /* * We want shallower trees and thus more bits covered at each layer. 8 @@ -183,4 +185,44 @@ static inline int ida_get_new(struct ida *ida, int *p_id) void __init idr_init_cache(void); +/** + * ida_get_index - allocate a ida index value + * @ida idr handle + * @lock spinlock handle protecting this index + * @p_id pointer to allocated index value + * + * A helper function for safely allocating an index value (id), + * returning a negative errno value on failure, else 0. + */ +static inline int ida_get_index(struct ida *ida, spinlock_t *lock, int *p_id) +{ + int error = -ENOMEM; + + do { + if (!ida_pre_get(ida, GFP_KERNEL)) + break; + spin_lock(lock); + error = ida_get_new(ida, p_id); + spin_unlock(lock); + } while (error == -EAGAIN); + + return error; +} + +/** + * ida_put_index - free an allocated ida index value + * @ida idr handle + * @lock spinlock handle protecting this index + * @id the value of the allocated index + * + * A helper function that goes with @ida_get_index, which safely + * frees a previously-allocated index value. + */ +static inline void ida_put_index(struct ida *ida, spinlock_t *lock, int id) +{ + spin_lock(lock); + ida_remove(ida, id); + spin_unlock(lock); +} + #endif /* __IDR_H__ */