2 * Copyright (C) 2008 IBM Corporation
5 * Mimi Zohar <zohar@us.ibm.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2 of the
13 * - implements the IMA hooks: ima_inode_alloc, ima_inode_free
14 * - cache integrity information associated with an inode
17 #include <linux/module.h>
18 #include <linux/spinlock.h>
19 #include <linux/radix-tree.h>
22 #define ima_iint_delete ima_inode_free
24 RADIX_TREE(ima_iint_store, GFP_ATOMIC);
25 DEFINE_SPINLOCK(ima_iint_lock);
27 static struct kmem_cache *iint_cache __read_mostly;
29 /* ima_iint_find_get - return the iint associated with an inode
31 * ima_iint_find_get gets a reference to the iint. Caller must
32 * remember to put the iint reference.
34 struct ima_iint_cache *ima_iint_find_get(struct inode *inode)
36 struct ima_iint_cache *iint;
39 iint = radix_tree_lookup(&ima_iint_store, (unsigned long)inode);
42 kref_get(&iint->refcount);
49 * ima_inode_alloc - allocate an iint associated with an inode
50 * @inode: pointer to the inode
52 int ima_inode_alloc(struct inode *inode)
54 struct ima_iint_cache *iint = NULL;
60 iint = kmem_cache_alloc(iint_cache, GFP_NOFS);
64 rc = radix_tree_preload(GFP_NOFS);
68 spin_lock(&ima_iint_lock);
69 rc = radix_tree_insert(&ima_iint_store, (unsigned long)inode, iint);
70 spin_unlock(&ima_iint_lock);
73 kmem_cache_free(iint_cache, iint);
75 radix_tree_preload_end();
80 /* iint_free - called when the iint refcount goes to zero */
81 void iint_free(struct kref *kref)
83 struct ima_iint_cache *iint = container_of(kref, struct ima_iint_cache,
87 if (iint->readcount != 0) {
88 printk(KERN_INFO "%s: readcount: %ld\n", __FUNCTION__,
92 if (iint->writecount != 0) {
93 printk(KERN_INFO "%s: writecount: %ld\n", __FUNCTION__,
97 if (iint->opencount != 0) {
98 printk(KERN_INFO "%s: opencount: %ld\n", __FUNCTION__,
102 kref_set(&iint->refcount, 1);
103 kmem_cache_free(iint_cache, iint);
106 void iint_rcu_free(struct rcu_head *rcu_head)
108 struct ima_iint_cache *iint = container_of(rcu_head,
109 struct ima_iint_cache, rcu);
110 kref_put(&iint->refcount, iint_free);
114 * ima_iint_delete - called on integrity_inode_free
115 * @inode: pointer to the inode
117 * Free the integrity information(iint) associated with an inode.
119 void ima_iint_delete(struct inode *inode)
121 struct ima_iint_cache *iint;
123 if (!ima_initialized)
125 spin_lock(&ima_iint_lock);
126 iint = radix_tree_delete(&ima_iint_store, (unsigned long)inode);
127 spin_unlock(&ima_iint_lock);
129 call_rcu(&iint->rcu, iint_rcu_free);
132 static void init_once(void *foo)
134 struct ima_iint_cache *iint = foo;
136 memset(iint, 0, sizeof *iint);
139 mutex_init(&iint->mutex);
141 iint->writecount = 0;
143 kref_set(&iint->refcount, 1);
146 void __init ima_iintcache_init(void)
149 kmem_cache_create("iint_cache", sizeof(struct ima_iint_cache), 0,
150 SLAB_PANIC, init_once);