ethoc: Clear command buffer after write
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nilfs2 / ifile.c
blob922d9dd42c8fb352f6e024688c73f9ede88e6738
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"
33 struct nilfs_ifile_info {
34 struct nilfs_mdt_info mi;
35 struct nilfs_palloc_cache palloc_cache;
38 static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
40 return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
43 /**
44 * nilfs_ifile_create_inode - create a new disk inode
45 * @ifile: ifile inode
46 * @out_ino: pointer to a variable to store inode number
47 * @out_bh: buffer_head contains newly allocated disk inode
49 * Return Value: On success, 0 is returned and the newly allocated inode
50 * number is stored in the place pointed by @ino, and buffer_head pointer
51 * that contains newly allocated disk inode structure is stored in the
52 * place pointed by @out_bh
53 * On error, one of the following negative error codes is returned.
55 * %-EIO - I/O error.
57 * %-ENOMEM - Insufficient amount of memory available.
59 * %-ENOSPC - No inode left.
61 int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
62 struct buffer_head **out_bh)
64 struct nilfs_palloc_req req;
65 int ret;
67 req.pr_entry_nr = 0; /* 0 says find free inode from beginning of
68 a group. dull code!! */
69 req.pr_entry_bh = NULL;
71 ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
72 if (!ret) {
73 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
74 &req.pr_entry_bh);
75 if (ret < 0)
76 nilfs_palloc_abort_alloc_entry(ifile, &req);
78 if (ret < 0) {
79 brelse(req.pr_entry_bh);
80 return ret;
82 nilfs_palloc_commit_alloc_entry(ifile, &req);
83 nilfs_mdt_mark_buffer_dirty(req.pr_entry_bh);
84 nilfs_mdt_mark_dirty(ifile);
85 *out_ino = (ino_t)req.pr_entry_nr;
86 *out_bh = req.pr_entry_bh;
87 return 0;
90 /**
91 * nilfs_ifile_delete_inode - delete a disk inode
92 * @ifile: ifile inode
93 * @ino: inode number
95 * Return Value: On success, 0 is returned. On error, one of the following
96 * negative error codes is returned.
98 * %-EIO - I/O error.
100 * %-ENOMEM - Insufficient amount of memory available.
102 * %-ENOENT - The inode number @ino have not been allocated.
104 int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
106 struct nilfs_palloc_req req = {
107 .pr_entry_nr = ino, .pr_entry_bh = NULL
109 struct nilfs_inode *raw_inode;
110 void *kaddr;
111 int ret;
113 ret = nilfs_palloc_prepare_free_entry(ifile, &req);
114 if (!ret) {
115 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
116 &req.pr_entry_bh);
117 if (ret < 0)
118 nilfs_palloc_abort_free_entry(ifile, &req);
120 if (ret < 0) {
121 brelse(req.pr_entry_bh);
122 return ret;
125 kaddr = kmap_atomic(req.pr_entry_bh->b_page, KM_USER0);
126 raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
127 req.pr_entry_bh, kaddr);
128 raw_inode->i_flags = 0;
129 kunmap_atomic(kaddr, KM_USER0);
131 nilfs_mdt_mark_buffer_dirty(req.pr_entry_bh);
132 brelse(req.pr_entry_bh);
134 nilfs_palloc_commit_free_entry(ifile, &req);
136 return 0;
139 int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
140 struct buffer_head **out_bh)
142 struct super_block *sb = ifile->i_sb;
143 int err;
145 if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
146 nilfs_error(sb, __func__, "bad inode number: %lu",
147 (unsigned long) ino);
148 return -EINVAL;
151 err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
152 if (unlikely(err)) {
153 if (err == -EINVAL)
154 nilfs_error(sb, __func__, "ifile is broken");
155 else
156 nilfs_warning(sb, __func__,
157 "unable to read inode: %lu",
158 (unsigned long) ino);
160 return err;
164 * nilfs_ifile_new - create inode file
165 * @sbi: nilfs_sb_info struct
166 * @inode_size: size of an inode
168 struct inode *nilfs_ifile_new(struct nilfs_sb_info *sbi, size_t inode_size)
170 struct inode *ifile;
171 int err;
173 ifile = nilfs_mdt_new(sbi->s_nilfs, sbi->s_super, NILFS_IFILE_INO,
174 sizeof(struct nilfs_ifile_info));
175 if (ifile) {
176 err = nilfs_palloc_init_blockgroup(ifile, inode_size);
177 if (unlikely(err)) {
178 nilfs_mdt_destroy(ifile);
179 return NULL;
181 nilfs_palloc_setup_cache(ifile,
182 &NILFS_IFILE_I(ifile)->palloc_cache);
184 return ifile;