proc: Add proc_mkdir_data()
[linux-2.6.git] / fs / nilfs2 / ifile.c
blobd8e65bde083c0c46fb157d321d8ed63a6a6e0ad0
1 /*
2 * ifile.c - NILFS inode file
4 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Amagai Yoshiji <amagai@osrg.net>.
21 * Revised by Ryusuke Konishi <ryusuke@osrg.net>.
25 #include <linux/types.h>
26 #include <linux/buffer_head.h>
27 #include "nilfs.h"
28 #include "mdt.h"
29 #include "alloc.h"
30 #include "ifile.h"
32 /**
33 * struct nilfs_ifile_info - on-memory private data of ifile
34 * @mi: on-memory private data of metadata file
35 * @palloc_cache: persistent object allocator cache of ifile
37 struct nilfs_ifile_info {
38 struct nilfs_mdt_info mi;
39 struct nilfs_palloc_cache palloc_cache;
42 static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
44 return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
47 /**
48 * nilfs_ifile_create_inode - create a new disk inode
49 * @ifile: ifile inode
50 * @out_ino: pointer to a variable to store inode number
51 * @out_bh: buffer_head contains newly allocated disk inode
53 * Return Value: On success, 0 is returned and the newly allocated inode
54 * number is stored in the place pointed by @ino, and buffer_head pointer
55 * that contains newly allocated disk inode structure is stored in the
56 * place pointed by @out_bh
57 * On error, one of the following negative error codes is returned.
59 * %-EIO - I/O error.
61 * %-ENOMEM - Insufficient amount of memory available.
63 * %-ENOSPC - No inode left.
65 int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
66 struct buffer_head **out_bh)
68 struct nilfs_palloc_req req;
69 int ret;
71 req.pr_entry_nr = 0; /* 0 says find free inode from beginning of
72 a group. dull code!! */
73 req.pr_entry_bh = NULL;
75 ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
76 if (!ret) {
77 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
78 &req.pr_entry_bh);
79 if (ret < 0)
80 nilfs_palloc_abort_alloc_entry(ifile, &req);
82 if (ret < 0) {
83 brelse(req.pr_entry_bh);
84 return ret;
86 nilfs_palloc_commit_alloc_entry(ifile, &req);
87 mark_buffer_dirty(req.pr_entry_bh);
88 nilfs_mdt_mark_dirty(ifile);
89 *out_ino = (ino_t)req.pr_entry_nr;
90 *out_bh = req.pr_entry_bh;
91 return 0;
94 /**
95 * nilfs_ifile_delete_inode - delete a disk inode
96 * @ifile: ifile inode
97 * @ino: inode number
99 * Return Value: On success, 0 is returned. On error, one of the following
100 * negative error codes is returned.
102 * %-EIO - I/O error.
104 * %-ENOMEM - Insufficient amount of memory available.
106 * %-ENOENT - The inode number @ino have not been allocated.
108 int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
110 struct nilfs_palloc_req req = {
111 .pr_entry_nr = ino, .pr_entry_bh = NULL
113 struct nilfs_inode *raw_inode;
114 void *kaddr;
115 int ret;
117 ret = nilfs_palloc_prepare_free_entry(ifile, &req);
118 if (!ret) {
119 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
120 &req.pr_entry_bh);
121 if (ret < 0)
122 nilfs_palloc_abort_free_entry(ifile, &req);
124 if (ret < 0) {
125 brelse(req.pr_entry_bh);
126 return ret;
129 kaddr = kmap_atomic(req.pr_entry_bh->b_page);
130 raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
131 req.pr_entry_bh, kaddr);
132 raw_inode->i_flags = 0;
133 kunmap_atomic(kaddr);
135 mark_buffer_dirty(req.pr_entry_bh);
136 brelse(req.pr_entry_bh);
138 nilfs_palloc_commit_free_entry(ifile, &req);
140 return 0;
143 int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
144 struct buffer_head **out_bh)
146 struct super_block *sb = ifile->i_sb;
147 int err;
149 if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
150 nilfs_error(sb, __func__, "bad inode number: %lu",
151 (unsigned long) ino);
152 return -EINVAL;
155 err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
156 if (unlikely(err))
157 nilfs_warning(sb, __func__, "unable to read inode: %lu",
158 (unsigned long) ino);
159 return err;
163 * nilfs_ifile_read - read or get ifile inode
164 * @sb: super block instance
165 * @root: root object
166 * @inode_size: size of an inode
167 * @raw_inode: on-disk ifile inode
168 * @inodep: buffer to store the inode
170 int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
171 size_t inode_size, struct nilfs_inode *raw_inode,
172 struct inode **inodep)
174 struct inode *ifile;
175 int err;
177 ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
178 if (unlikely(!ifile))
179 return -ENOMEM;
180 if (!(ifile->i_state & I_NEW))
181 goto out;
183 err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
184 sizeof(struct nilfs_ifile_info));
185 if (err)
186 goto failed;
188 err = nilfs_palloc_init_blockgroup(ifile, inode_size);
189 if (err)
190 goto failed;
192 nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
194 err = nilfs_read_inode_common(ifile, raw_inode);
195 if (err)
196 goto failed;
198 unlock_new_inode(ifile);
199 out:
200 *inodep = ifile;
201 return 0;
202 failed:
203 iget_failed(ifile);
204 return err;