]>
Commit | Line | Data |
---|---|---|
43bfb45e RK |
1 | /* |
2 | * ifile.c - NILFS inode file | |
3 | * | |
4 | * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation. | |
5 | * | |
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. | |
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 | |
14 | * GNU General Public License for more details. | |
15 | * | |
43bfb45e RK |
16 | * Written by Amagai Yoshiji <[email protected]>. |
17 | * Revised by Ryusuke Konishi <[email protected]>. | |
18 | * | |
19 | */ | |
20 | ||
21 | #include <linux/types.h> | |
22 | #include <linux/buffer_head.h> | |
23 | #include "nilfs.h" | |
24 | #include "mdt.h" | |
25 | #include "alloc.h" | |
26 | #include "ifile.h" | |
27 | ||
f5974c8f VD |
28 | /** |
29 | * struct nilfs_ifile_info - on-memory private data of ifile | |
30 | * @mi: on-memory private data of metadata file | |
31 | * @palloc_cache: persistent object allocator cache of ifile | |
32 | */ | |
49fa7a59 RK |
33 | struct nilfs_ifile_info { |
34 | struct nilfs_mdt_info mi; | |
35 | struct nilfs_palloc_cache palloc_cache; | |
36 | }; | |
37 | ||
38 | static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile) | |
39 | { | |
40 | return (struct nilfs_ifile_info *)NILFS_MDT(ifile); | |
41 | } | |
42 | ||
43bfb45e RK |
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 | |
48 | * | |
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. | |
54 | * | |
55 | * %-EIO - I/O error. | |
56 | * | |
57 | * %-ENOMEM - Insufficient amount of memory available. | |
58 | * | |
59 | * %-ENOSPC - No inode left. | |
60 | */ | |
61 | int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino, | |
62 | struct buffer_head **out_bh) | |
63 | { | |
64 | struct nilfs_palloc_req req; | |
65 | int ret; | |
66 | ||
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; | |
70 | ||
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); | |
77 | } | |
78 | if (ret < 0) { | |
79 | brelse(req.pr_entry_bh); | |
80 | return ret; | |
81 | } | |
82 | nilfs_palloc_commit_alloc_entry(ifile, &req); | |
5fc7b141 | 83 | mark_buffer_dirty(req.pr_entry_bh); |
43bfb45e RK |
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; | |
88 | } | |
89 | ||
90 | /** | |
91 | * nilfs_ifile_delete_inode - delete a disk inode | |
92 | * @ifile: ifile inode | |
93 | * @ino: inode number | |
94 | * | |
95 | * Return Value: On success, 0 is returned. On error, one of the following | |
96 | * negative error codes is returned. | |
97 | * | |
98 | * %-EIO - I/O error. | |
99 | * | |
100 | * %-ENOMEM - Insufficient amount of memory available. | |
101 | * | |
102 | * %-ENOENT - The inode number @ino have not been allocated. | |
103 | */ | |
104 | int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino) | |
105 | { | |
106 | struct nilfs_palloc_req req = { | |
107 | .pr_entry_nr = ino, .pr_entry_bh = NULL | |
108 | }; | |
109 | struct nilfs_inode *raw_inode; | |
110 | void *kaddr; | |
111 | int ret; | |
112 | ||
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); | |
119 | } | |
120 | if (ret < 0) { | |
121 | brelse(req.pr_entry_bh); | |
122 | return ret; | |
123 | } | |
124 | ||
7b9c0976 | 125 | kaddr = kmap_atomic(req.pr_entry_bh->b_page); |
43bfb45e RK |
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; | |
7b9c0976 | 129 | kunmap_atomic(kaddr); |
43bfb45e | 130 | |
5fc7b141 | 131 | mark_buffer_dirty(req.pr_entry_bh); |
43bfb45e RK |
132 | brelse(req.pr_entry_bh); |
133 | ||
134 | nilfs_palloc_commit_free_entry(ifile, &req); | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino, | |
140 | struct buffer_head **out_bh) | |
141 | { | |
142 | struct super_block *sb = ifile->i_sb; | |
143 | int err; | |
144 | ||
145 | if (unlikely(!NILFS_VALID_INODE(sb, ino))) { | |
146 | nilfs_error(sb, __func__, "bad inode number: %lu", | |
147 | (unsigned long) ino); | |
148 | return -EINVAL; | |
149 | } | |
150 | ||
151 | err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh); | |
e828949e RK |
152 | if (unlikely(err)) |
153 | nilfs_warning(sb, __func__, "unable to read inode: %lu", | |
154 | (unsigned long) ino); | |
43bfb45e RK |
155 | return err; |
156 | } | |
79739565 | 157 | |
c7ef972c VD |
158 | /** |
159 | * nilfs_ifile_count_free_inodes - calculate free inodes count | |
160 | * @ifile: ifile inode | |
161 | * @nmaxinodes: current maximum of available inodes count [out] | |
162 | * @nfreeinodes: free inodes count [out] | |
163 | */ | |
164 | int nilfs_ifile_count_free_inodes(struct inode *ifile, | |
165 | u64 *nmaxinodes, u64 *nfreeinodes) | |
166 | { | |
167 | u64 nused; | |
168 | int err; | |
169 | ||
170 | *nmaxinodes = 0; | |
171 | *nfreeinodes = 0; | |
172 | ||
e5f7f848 | 173 | nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count); |
c7ef972c VD |
174 | err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes); |
175 | if (likely(!err)) | |
176 | *nfreeinodes = *nmaxinodes - nused; | |
177 | return err; | |
178 | } | |
179 | ||
79739565 | 180 | /** |
f1e89c86 RK |
181 | * nilfs_ifile_read - read or get ifile inode |
182 | * @sb: super block instance | |
183 | * @root: root object | |
79739565 | 184 | * @inode_size: size of an inode |
f1e89c86 RK |
185 | * @raw_inode: on-disk ifile inode |
186 | * @inodep: buffer to store the inode | |
79739565 | 187 | */ |
f1e89c86 RK |
188 | int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root, |
189 | size_t inode_size, struct nilfs_inode *raw_inode, | |
190 | struct inode **inodep) | |
79739565 RK |
191 | { |
192 | struct inode *ifile; | |
193 | int err; | |
194 | ||
f1e89c86 RK |
195 | ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO); |
196 | if (unlikely(!ifile)) | |
197 | return -ENOMEM; | |
198 | if (!(ifile->i_state & I_NEW)) | |
199 | goto out; | |
200 | ||
201 | err = nilfs_mdt_init(ifile, NILFS_MDT_GFP, | |
202 | sizeof(struct nilfs_ifile_info)); | |
203 | if (err) | |
204 | goto failed; | |
205 | ||
206 | err = nilfs_palloc_init_blockgroup(ifile, inode_size); | |
207 | if (err) | |
208 | goto failed; | |
209 | ||
210 | nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache); | |
211 | ||
212 | err = nilfs_read_inode_common(ifile, raw_inode); | |
213 | if (err) | |
214 | goto failed; | |
215 | ||
216 | unlock_new_inode(ifile); | |
217 | out: | |
218 | *inodep = ifile; | |
219 | return 0; | |
220 | failed: | |
221 | iget_failed(ifile); | |
222 | return err; | |
79739565 | 223 | } |