blob: 6577c588de9d31d1795f7cd683fd81a149a716d1 [file] [log] [blame]
Joel Becker7063fbf2005-12-15 14:29:43 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * inode.c - basic inode and dentry operations.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 *
21 * Based on sysfs:
22 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
23 *
24 * configfs Copyright (C) 2005 Oracle. All rights reserved.
25 *
26 * Please see Documentation/filesystems/configfs.txt for more information.
27 */
28
29#undef DEBUG
30
31#include <linux/pagemap.h>
32#include <linux/namei.h>
33#include <linux/backing-dev.h>
34
35#include <linux/configfs.h>
36#include "configfs_internal.h"
37
38extern struct super_block * configfs_sb;
39
40static struct address_space_operations configfs_aops = {
41 .readpage = simple_readpage,
42 .prepare_write = simple_prepare_write,
43 .commit_write = simple_commit_write
44};
45
46static struct backing_dev_info configfs_backing_dev_info = {
47 .ra_pages = 0, /* No readahead */
48 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
49};
50
51struct inode * configfs_new_inode(mode_t mode)
52{
53 struct inode * inode = new_inode(configfs_sb);
54 if (inode) {
55 inode->i_mode = mode;
56 inode->i_uid = 0;
57 inode->i_gid = 0;
58 inode->i_blksize = PAGE_CACHE_SIZE;
59 inode->i_blocks = 0;
60 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
61 inode->i_mapping->a_ops = &configfs_aops;
62 inode->i_mapping->backing_dev_info = &configfs_backing_dev_info;
63 }
64 return inode;
65}
66
67int configfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
68{
69 int error = 0;
70 struct inode * inode = NULL;
71 if (dentry) {
72 if (!dentry->d_inode) {
73 if ((inode = configfs_new_inode(mode))) {
74 if (dentry->d_parent && dentry->d_parent->d_inode) {
75 struct inode *p_inode = dentry->d_parent->d_inode;
76 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
77 }
78 goto Proceed;
79 }
80 else
81 error = -ENOMEM;
82 } else
83 error = -EEXIST;
84 } else
85 error = -ENOENT;
86 goto Done;
87
88 Proceed:
89 if (init)
90 error = init(inode);
91 if (!error) {
92 d_instantiate(dentry, inode);
93 if (S_ISDIR(mode) || S_ISLNK(mode))
94 dget(dentry); /* pin link and directory dentries in core */
95 } else
96 iput(inode);
97 Done:
98 return error;
99}
100
101/*
102 * Get the name for corresponding element represented by the given configfs_dirent
103 */
104const unsigned char * configfs_get_name(struct configfs_dirent *sd)
105{
106 struct attribute * attr;
107
108 if (!sd || !sd->s_element)
109 BUG();
110
111 /* These always have a dentry, so use that */
112 if (sd->s_type & (CONFIGFS_DIR | CONFIGFS_ITEM_LINK))
113 return sd->s_dentry->d_name.name;
114
115 if (sd->s_type & CONFIGFS_ITEM_ATTR) {
116 attr = sd->s_element;
117 return attr->name;
118 }
119 return NULL;
120}
121
122
123/*
124 * Unhashes the dentry corresponding to given configfs_dirent
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800125 * Called with parent inode's i_mutex held.
Joel Becker7063fbf2005-12-15 14:29:43 -0800126 */
127void configfs_drop_dentry(struct configfs_dirent * sd, struct dentry * parent)
128{
129 struct dentry * dentry = sd->s_dentry;
130
131 if (dentry) {
132 spin_lock(&dcache_lock);
133 if (!(d_unhashed(dentry) && dentry->d_inode)) {
134 dget_locked(dentry);
135 __d_drop(dentry);
136 spin_unlock(&dcache_lock);
137 simple_unlink(parent->d_inode, dentry);
138 } else
139 spin_unlock(&dcache_lock);
140 }
141}
142
143void configfs_hash_and_remove(struct dentry * dir, const char * name)
144{
145 struct configfs_dirent * sd;
146 struct configfs_dirent * parent_sd = dir->d_fsdata;
147
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800148 mutex_lock(&dir->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800149 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
150 if (!sd->s_element)
151 continue;
152 if (!strcmp(configfs_get_name(sd), name)) {
153 list_del_init(&sd->s_sibling);
154 configfs_drop_dentry(sd, dir);
155 configfs_put(sd);
156 break;
157 }
158 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800159 mutex_unlock(&dir->d_inode->i_mutex);
Joel Becker7063fbf2005-12-15 14:29:43 -0800160}
161
162