diff options
author | Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp> | 2009-04-06 22:01:32 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-04-07 11:31:14 -0400 |
commit | 05fe58fdc10df9ebea04c0eaed57adc47af5c184 (patch) | |
tree | 04fbeb7b48e6362ec11e14631b1f42eb74b9c865 | |
parent | 6c98cd4ecb0ae35f0368c5c2190712689c2064aa (diff) |
nilfs2: inode operations
This adds inode level operations of the nilfs2 file system.
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | fs/nilfs2/inode.c | 819 |
1 files changed, 819 insertions, 0 deletions
diff --git a/fs/nilfs2/inode.c b/fs/nilfs2/inode.c new file mode 100644 index 000000000000..b4697d9d7e5d --- /dev/null +++ b/fs/nilfs2/inode.c | |||
@@ -0,0 +1,819 @@ | |||
1 | /* | ||
2 | * inode.c - NILFS inode operations. | ||
3 | * | ||
4 | * Copyright (C) 2005-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 | * | ||
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 | ||
19 | * | ||
20 | * Written by Ryusuke Konishi <ryusuke@osrg.net> | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include <linux/buffer_head.h> | ||
25 | #include <linux/mpage.h> | ||
26 | #include <linux/writeback.h> | ||
27 | #include "nilfs.h" | ||
28 | #include "segment.h" | ||
29 | #include "page.h" | ||
30 | #include "mdt.h" | ||
31 | #include "cpfile.h" | ||
32 | #include "ifile.h" | ||
33 | |||
34 | |||
35 | /** | ||
36 | * nilfs_get_block() - get a file block on the filesystem (callback function) | ||
37 | * @inode - inode struct of the target file | ||
38 | * @blkoff - file block number | ||
39 | * @bh_result - buffer head to be mapped on | ||
40 | * @create - indicate whether allocating the block or not when it has not | ||
41 | * been allocated yet. | ||
42 | * | ||
43 | * This function does not issue actual read request of the specified data | ||
44 | * block. It is done by VFS. | ||
45 | * Bulk read for direct-io is not supported yet. (should be supported) | ||
46 | */ | ||
47 | int nilfs_get_block(struct inode *inode, sector_t blkoff, | ||
48 | struct buffer_head *bh_result, int create) | ||
49 | { | ||
50 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
51 | unsigned long blknum = 0; | ||
52 | int err = 0, ret; | ||
53 | struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode)); | ||
54 | |||
55 | /* This exclusion control is a workaround; should be revised */ | ||
56 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | ||
57 | ret = nilfs_bmap_lookup(ii->i_bmap, (unsigned long)blkoff, &blknum); | ||
58 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | ||
59 | if (ret == 0) { /* found */ | ||
60 | map_bh(bh_result, inode->i_sb, blknum); | ||
61 | goto out; | ||
62 | } | ||
63 | if (unlikely(ret == 1)) { | ||
64 | printk(KERN_ERR "nilfs_get_block: bmap_lookup returns " | ||
65 | "buffer_head pointer (blkoff=%llu, blknum=%lu)\n", | ||
66 | (unsigned long long)blkoff, blknum); | ||
67 | BUG(); | ||
68 | } | ||
69 | /* data block was not found */ | ||
70 | if (ret == -ENOENT && create) { | ||
71 | struct nilfs_transaction_info ti; | ||
72 | |||
73 | bh_result->b_blocknr = 0; | ||
74 | err = nilfs_transaction_begin(inode->i_sb, &ti, 1); | ||
75 | if (unlikely(err)) | ||
76 | goto out; | ||
77 | err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, | ||
78 | (unsigned long)bh_result); | ||
79 | nilfs_transaction_end(inode->i_sb, !err); | ||
80 | if (unlikely(err != 0)) { | ||
81 | if (err == -EEXIST) { | ||
82 | /* | ||
83 | * The get_block() function could be called | ||
84 | * from multiple callers for an inode. | ||
85 | * However, the page having this block must | ||
86 | * be locked in this case. | ||
87 | */ | ||
88 | printk(KERN_ERR | ||
89 | "nilfs_get_block: a race condition " | ||
90 | "while inserting a data block. " | ||
91 | "(inode number=%lu, file block " | ||
92 | "offset=%llu)\n", | ||
93 | inode->i_ino, | ||
94 | (unsigned long long)blkoff); | ||
95 | BUG(); | ||
96 | } else if (err == -EINVAL) { | ||
97 | nilfs_error(inode->i_sb, __func__, | ||
98 | "broken bmap (inode=%lu)\n", | ||
99 | inode->i_ino); | ||
100 | err = -EIO; | ||
101 | } | ||
102 | goto out; | ||
103 | } | ||
104 | /* Error handling should be detailed */ | ||
105 | set_buffer_new(bh_result); | ||
106 | map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed | ||
107 | to proper value */ | ||
108 | } else if (ret == -ENOENT) { | ||
109 | /* not found is not error (e.g. hole); must return without | ||
110 | the mapped state flag. */ | ||
111 | ; | ||
112 | } else { | ||
113 | err = ret; | ||
114 | } | ||
115 | |||
116 | out: | ||
117 | return err; | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * nilfs_readpage() - implement readpage() method of nilfs_aops {} | ||
122 | * address_space_operations. | ||
123 | * @file - file struct of the file to be read | ||
124 | * @page - the page to be read | ||
125 | */ | ||
126 | static int nilfs_readpage(struct file *file, struct page *page) | ||
127 | { | ||
128 | return mpage_readpage(page, nilfs_get_block); | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * nilfs_readpages() - implement readpages() method of nilfs_aops {} | ||
133 | * address_space_operations. | ||
134 | * @file - file struct of the file to be read | ||
135 | * @mapping - address_space struct used for reading multiple pages | ||
136 | * @pages - the pages to be read | ||
137 | * @nr_pages - number of pages to be read | ||
138 | */ | ||
139 | static int nilfs_readpages(struct file *file, struct address_space *mapping, | ||
140 | struct list_head *pages, unsigned nr_pages) | ||
141 | { | ||
142 | return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); | ||
143 | } | ||
144 | |||
145 | static int nilfs_writepages(struct address_space *mapping, | ||
146 | struct writeback_control *wbc) | ||
147 | { | ||
148 | /* This empty method is required not to call generic_writepages() */ | ||
149 | return 0; | ||
150 | } | ||
151 | |||
152 | static int nilfs_writepage(struct page *page, struct writeback_control *wbc) | ||
153 | { | ||
154 | struct inode *inode = page->mapping->host; | ||
155 | int err; | ||
156 | |||
157 | redirty_page_for_writepage(wbc, page); | ||
158 | unlock_page(page); | ||
159 | |||
160 | if (wbc->sync_mode == WB_SYNC_ALL) { | ||
161 | err = nilfs_construct_segment(inode->i_sb); | ||
162 | if (unlikely(err)) | ||
163 | return err; | ||
164 | } else if (wbc->for_reclaim) | ||
165 | nilfs_flush_segment(inode->i_sb, inode->i_ino); | ||
166 | |||
167 | return 0; | ||
168 | } | ||
169 | |||
170 | static int nilfs_set_page_dirty(struct page *page) | ||
171 | { | ||
172 | int ret = __set_page_dirty_buffers(page); | ||
173 | |||
174 | if (ret) { | ||
175 | struct inode *inode = page->mapping->host; | ||
176 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | ||
177 | unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); | ||
178 | |||
179 | nilfs_set_file_dirty(sbi, inode, nr_dirty); | ||
180 | } | ||
181 | return ret; | ||
182 | } | ||
183 | |||
184 | static int nilfs_write_begin(struct file *file, struct address_space *mapping, | ||
185 | loff_t pos, unsigned len, unsigned flags, | ||
186 | struct page **pagep, void **fsdata) | ||
187 | |||
188 | { | ||
189 | struct inode *inode = mapping->host; | ||
190 | int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); | ||
191 | |||
192 | if (unlikely(err)) | ||
193 | return err; | ||
194 | |||
195 | *pagep = NULL; | ||
196 | err = block_write_begin(file, mapping, pos, len, flags, pagep, | ||
197 | fsdata, nilfs_get_block); | ||
198 | if (unlikely(err)) | ||
199 | nilfs_transaction_end(inode->i_sb, 0); | ||
200 | return err; | ||
201 | } | ||
202 | |||
203 | static int nilfs_write_end(struct file *file, struct address_space *mapping, | ||
204 | loff_t pos, unsigned len, unsigned copied, | ||
205 | struct page *page, void *fsdata) | ||
206 | { | ||
207 | struct inode *inode = mapping->host; | ||
208 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); | ||
209 | unsigned nr_dirty; | ||
210 | int err; | ||
211 | |||
212 | nr_dirty = nilfs_page_count_clean_buffers(page, start, | ||
213 | start + copied); | ||
214 | copied = generic_write_end(file, mapping, pos, len, copied, page, | ||
215 | fsdata); | ||
216 | nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty); | ||
217 | err = nilfs_transaction_end(inode->i_sb, 1); | ||
218 | return err ? : copied; | ||
219 | } | ||
220 | |||
221 | static ssize_t | ||
222 | nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | ||
223 | loff_t offset, unsigned long nr_segs) | ||
224 | { | ||
225 | struct file *file = iocb->ki_filp; | ||
226 | struct inode *inode = file->f_mapping->host; | ||
227 | ssize_t size; | ||
228 | int err; | ||
229 | |||
230 | err = nilfs_construct_dsync_segment(inode->i_sb, inode); | ||
231 | if (unlikely(err)) | ||
232 | return err; | ||
233 | |||
234 | if (rw == WRITE) | ||
235 | return 0; | ||
236 | |||
237 | /* Needs synchronization with the cleaner */ | ||
238 | size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | ||
239 | offset, nr_segs, nilfs_get_block, NULL); | ||
240 | return size; | ||
241 | } | ||
242 | |||
243 | struct address_space_operations nilfs_aops = { | ||
244 | .writepage = nilfs_writepage, | ||
245 | .readpage = nilfs_readpage, | ||
246 | /* .sync_page = nilfs_sync_page, */ | ||
247 | .writepages = nilfs_writepages, | ||
248 | .set_page_dirty = nilfs_set_page_dirty, | ||
249 | .readpages = nilfs_readpages, | ||
250 | .write_begin = nilfs_write_begin, | ||
251 | .write_end = nilfs_write_end, | ||
252 | /* .releasepage = nilfs_releasepage, */ | ||
253 | .invalidatepage = block_invalidatepage, | ||
254 | .direct_IO = nilfs_direct_IO, | ||
255 | }; | ||
256 | |||
257 | struct inode *nilfs_new_inode(struct inode *dir, int mode) | ||
258 | { | ||
259 | struct super_block *sb = dir->i_sb; | ||
260 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | ||
261 | struct inode *inode; | ||
262 | struct nilfs_inode_info *ii; | ||
263 | int err = -ENOMEM; | ||
264 | ino_t ino; | ||
265 | |||
266 | inode = new_inode(sb); | ||
267 | if (unlikely(!inode)) | ||
268 | goto failed; | ||
269 | |||
270 | mapping_set_gfp_mask(inode->i_mapping, | ||
271 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); | ||
272 | |||
273 | ii = NILFS_I(inode); | ||
274 | ii->i_state = 1 << NILFS_I_NEW; | ||
275 | |||
276 | err = nilfs_ifile_create_inode(sbi->s_ifile, &ino, &ii->i_bh); | ||
277 | if (unlikely(err)) | ||
278 | goto failed_ifile_create_inode; | ||
279 | /* reference count of i_bh inherits from nilfs_mdt_read_block() */ | ||
280 | |||
281 | atomic_inc(&sbi->s_inodes_count); | ||
282 | |||
283 | inode->i_uid = current_fsuid(); | ||
284 | if (dir->i_mode & S_ISGID) { | ||
285 | inode->i_gid = dir->i_gid; | ||
286 | if (S_ISDIR(mode)) | ||
287 | mode |= S_ISGID; | ||
288 | } else | ||
289 | inode->i_gid = current_fsgid(); | ||
290 | |||
291 | inode->i_mode = mode; | ||
292 | inode->i_ino = ino; | ||
293 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | ||
294 | |||
295 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { | ||
296 | err = nilfs_bmap_read(ii->i_bmap, NULL); | ||
297 | if (err < 0) | ||
298 | goto failed_bmap; | ||
299 | |||
300 | set_bit(NILFS_I_BMAP, &ii->i_state); | ||
301 | /* No lock is needed; iget() ensures it. */ | ||
302 | } | ||
303 | |||
304 | ii->i_flags = NILFS_I(dir)->i_flags; | ||
305 | if (S_ISLNK(mode)) | ||
306 | ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL); | ||
307 | if (!S_ISDIR(mode)) | ||
308 | ii->i_flags &= ~NILFS_DIRSYNC_FL; | ||
309 | |||
310 | /* ii->i_file_acl = 0; */ | ||
311 | /* ii->i_dir_acl = 0; */ | ||
312 | ii->i_dtime = 0; | ||
313 | ii->i_dir_start_lookup = 0; | ||
314 | #ifdef CONFIG_NILFS_FS_POSIX_ACL | ||
315 | ii->i_acl = NULL; | ||
316 | ii->i_default_acl = NULL; | ||
317 | #endif | ||
318 | ii->i_cno = 0; | ||
319 | nilfs_set_inode_flags(inode); | ||
320 | spin_lock(&sbi->s_next_gen_lock); | ||
321 | inode->i_generation = sbi->s_next_generation++; | ||
322 | spin_unlock(&sbi->s_next_gen_lock); | ||
323 | insert_inode_hash(inode); | ||
324 | |||
325 | err = nilfs_init_acl(inode, dir); | ||
326 | if (unlikely(err)) | ||
327 | goto failed_acl; /* never occur. When supporting | ||
328 | nilfs_init_acl(), proper cancellation of | ||
329 | above jobs should be considered */ | ||
330 | |||
331 | mark_inode_dirty(inode); | ||
332 | return inode; | ||
333 | |||
334 | failed_acl: | ||
335 | failed_bmap: | ||
336 | inode->i_nlink = 0; | ||
337 | iput(inode); /* raw_inode will be deleted through | ||
338 | generic_delete_inode() */ | ||
339 | goto failed; | ||
340 | |||
341 | failed_ifile_create_inode: | ||
342 | make_bad_inode(inode); | ||
343 | iput(inode); /* if i_nlink == 1, generic_forget_inode() will be | ||
344 | called */ | ||
345 | failed: | ||
346 | return ERR_PTR(err); | ||
347 | } | ||
348 | |||
349 | void nilfs_free_inode(struct inode *inode) | ||
350 | { | ||
351 | struct super_block *sb = inode->i_sb; | ||
352 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | ||
353 | |||
354 | clear_inode(inode); | ||
355 | /* XXX: check error code? Is there any thing I can do? */ | ||
356 | (void) nilfs_ifile_delete_inode(sbi->s_ifile, inode->i_ino); | ||
357 | atomic_dec(&sbi->s_inodes_count); | ||
358 | } | ||
359 | |||
360 | void nilfs_set_inode_flags(struct inode *inode) | ||
361 | { | ||
362 | unsigned int flags = NILFS_I(inode)->i_flags; | ||
363 | |||
364 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | | ||
365 | S_DIRSYNC); | ||
366 | if (flags & NILFS_SYNC_FL) | ||
367 | inode->i_flags |= S_SYNC; | ||
368 | if (flags & NILFS_APPEND_FL) | ||
369 | inode->i_flags |= S_APPEND; | ||
370 | if (flags & NILFS_IMMUTABLE_FL) | ||
371 | inode->i_flags |= S_IMMUTABLE; | ||
372 | #ifndef NILFS_ATIME_DISABLE | ||
373 | if (flags & NILFS_NOATIME_FL) | ||
374 | #endif | ||
375 | inode->i_flags |= S_NOATIME; | ||
376 | if (flags & NILFS_DIRSYNC_FL) | ||
377 | inode->i_flags |= S_DIRSYNC; | ||
378 | mapping_set_gfp_mask(inode->i_mapping, | ||
379 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); | ||
380 | } | ||
381 | |||
382 | int nilfs_read_inode_common(struct inode *inode, | ||
383 | struct nilfs_inode *raw_inode) | ||
384 | { | ||
385 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
386 | int err; | ||
387 | |||
388 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | ||
389 | inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid); | ||
390 | inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid); | ||
391 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | ||
392 | inode->i_size = le64_to_cpu(raw_inode->i_size); | ||
393 | inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); | ||
394 | inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); | ||
395 | inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); | ||
396 | inode->i_atime.tv_nsec = 0; | ||
397 | inode->i_ctime.tv_nsec = 0; | ||
398 | inode->i_mtime.tv_nsec = 0; | ||
399 | ii->i_dtime = le64_to_cpu(raw_inode->i_dtime); | ||
400 | if (inode->i_nlink == 0 && (inode->i_mode == 0 || ii->i_dtime)) | ||
401 | return -EINVAL; /* this inode is deleted */ | ||
402 | |||
403 | inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); | ||
404 | ii->i_flags = le32_to_cpu(raw_inode->i_flags); | ||
405 | #if 0 | ||
406 | ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); | ||
407 | ii->i_dir_acl = S_ISREG(inode->i_mode) ? | ||
408 | 0 : le32_to_cpu(raw_inode->i_dir_acl); | ||
409 | #endif | ||
410 | ii->i_cno = 0; | ||
411 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); | ||
412 | |||
413 | if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | ||
414 | S_ISLNK(inode->i_mode)) { | ||
415 | err = nilfs_bmap_read(ii->i_bmap, raw_inode); | ||
416 | if (err < 0) | ||
417 | return err; | ||
418 | set_bit(NILFS_I_BMAP, &ii->i_state); | ||
419 | /* No lock is needed; iget() ensures it. */ | ||
420 | } | ||
421 | return 0; | ||
422 | } | ||
423 | |||
424 | static int nilfs_read_sketch_inode(struct inode *inode) | ||
425 | { | ||
426 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | ||
427 | int err = 0; | ||
428 | |||
429 | if (sbi->s_snapshot_cno) { | ||
430 | struct the_nilfs *nilfs = sbi->s_nilfs; | ||
431 | struct buffer_head *bh_cp; | ||
432 | struct nilfs_checkpoint *raw_cp; | ||
433 | |||
434 | err = nilfs_cpfile_get_checkpoint( | ||
435 | nilfs->ns_cpfile, sbi->s_snapshot_cno, 0, &raw_cp, | ||
436 | &bh_cp); | ||
437 | if (likely(!err)) { | ||
438 | if (!nilfs_checkpoint_sketch(raw_cp)) | ||
439 | inode->i_size = 0; | ||
440 | nilfs_cpfile_put_checkpoint( | ||
441 | nilfs->ns_cpfile, sbi->s_snapshot_cno, bh_cp); | ||
442 | } | ||
443 | inode->i_flags |= S_NOCMTIME; | ||
444 | } | ||
445 | return err; | ||
446 | } | ||
447 | |||
448 | static int __nilfs_read_inode(struct super_block *sb, unsigned long ino, | ||
449 | struct inode *inode) | ||
450 | { | ||
451 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | ||
452 | struct inode *dat = nilfs_dat_inode(sbi->s_nilfs); | ||
453 | struct buffer_head *bh; | ||
454 | struct nilfs_inode *raw_inode; | ||
455 | int err; | ||
456 | |||
457 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | ||
458 | err = nilfs_ifile_get_inode_block(sbi->s_ifile, ino, &bh); | ||
459 | if (unlikely(err)) | ||
460 | goto bad_inode; | ||
461 | |||
462 | raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, bh); | ||
463 | |||
464 | #ifdef CONFIG_NILFS_FS_POSIX_ACL | ||
465 | ii->i_acl = NILFS_ACL_NOT_CACHED; | ||
466 | ii->i_default_acl = NILFS_ACL_NOT_CACHED; | ||
467 | #endif | ||
468 | if (nilfs_read_inode_common(inode, raw_inode)) | ||
469 | goto failed_unmap; | ||
470 | |||
471 | if (S_ISREG(inode->i_mode)) { | ||
472 | inode->i_op = &nilfs_file_inode_operations; | ||
473 | inode->i_fop = &nilfs_file_operations; | ||
474 | inode->i_mapping->a_ops = &nilfs_aops; | ||
475 | if (unlikely(inode->i_ino == NILFS_SKETCH_INO)) { | ||
476 | err = nilfs_read_sketch_inode(inode); | ||
477 | if (unlikely(err)) | ||
478 | goto failed_unmap; | ||
479 | } | ||
480 | } else if (S_ISDIR(inode->i_mode)) { | ||
481 | inode->i_op = &nilfs_dir_inode_operations; | ||
482 | inode->i_fop = &nilfs_dir_operations; | ||
483 | inode->i_mapping->a_ops = &nilfs_aops; | ||
484 | } else if (S_ISLNK(inode->i_mode)) { | ||
485 | inode->i_op = &nilfs_symlink_inode_operations; | ||
486 | inode->i_mapping->a_ops = &nilfs_aops; | ||
487 | } else { | ||
488 | inode->i_op = &nilfs_special_inode_operations; | ||
489 | init_special_inode( | ||
490 | inode, inode->i_mode, | ||
491 | new_decode_dev(le64_to_cpu(raw_inode->i_device_code))); | ||
492 | } | ||
493 | nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); | ||
494 | brelse(bh); | ||
495 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | ||
496 | nilfs_set_inode_flags(inode); | ||
497 | return 0; | ||
498 | |||
499 | failed_unmap: | ||
500 | nilfs_ifile_unmap_inode(sbi->s_ifile, ino, bh); | ||
501 | brelse(bh); | ||
502 | |||
503 | bad_inode: | ||
504 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | ||
505 | return err; | ||
506 | } | ||
507 | |||
508 | struct inode *nilfs_iget(struct super_block *sb, unsigned long ino) | ||
509 | { | ||
510 | struct inode *inode; | ||
511 | int err; | ||
512 | |||
513 | inode = iget_locked(sb, ino); | ||
514 | if (unlikely(!inode)) | ||
515 | return ERR_PTR(-ENOMEM); | ||
516 | if (!(inode->i_state & I_NEW)) | ||
517 | return inode; | ||
518 | |||
519 | err = __nilfs_read_inode(sb, ino, inode); | ||
520 | if (unlikely(err)) { | ||
521 | iget_failed(inode); | ||
522 | return ERR_PTR(err); | ||
523 | } | ||
524 | unlock_new_inode(inode); | ||
525 | return inode; | ||
526 | } | ||
527 | |||
528 | void nilfs_write_inode_common(struct inode *inode, | ||
529 | struct nilfs_inode *raw_inode, int has_bmap) | ||
530 | { | ||
531 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
532 | |||
533 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); | ||
534 | raw_inode->i_uid = cpu_to_le32(inode->i_uid); | ||
535 | raw_inode->i_gid = cpu_to_le32(inode->i_gid); | ||
536 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | ||
537 | raw_inode->i_size = cpu_to_le64(inode->i_size); | ||
538 | raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); | ||
539 | raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); | ||
540 | raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); | ||
541 | |||
542 | raw_inode->i_dtime = cpu_to_le64(ii->i_dtime); | ||
543 | raw_inode->i_flags = cpu_to_le32(ii->i_flags); | ||
544 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | ||
545 | |||
546 | if (has_bmap) | ||
547 | nilfs_bmap_write(ii->i_bmap, raw_inode); | ||
548 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) | ||
549 | raw_inode->i_device_code = | ||
550 | cpu_to_le64(new_encode_dev(inode->i_rdev)); | ||
551 | /* When extending inode, nilfs->ns_inode_size should be checked | ||
552 | for substitutions of appended fields */ | ||
553 | } | ||
554 | |||
555 | void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) | ||
556 | { | ||
557 | ino_t ino = inode->i_ino; | ||
558 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
559 | struct super_block *sb = inode->i_sb; | ||
560 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | ||
561 | struct nilfs_inode *raw_inode; | ||
562 | |||
563 | raw_inode = nilfs_ifile_map_inode(sbi->s_ifile, ino, ibh); | ||
564 | |||
565 | /* The buffer is guarded with lock_buffer() by the caller */ | ||
566 | if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) | ||
567 | memset(raw_inode, 0, NILFS_MDT(sbi->s_ifile)->mi_entry_size); | ||
568 | set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); | ||
569 | |||
570 | nilfs_write_inode_common(inode, raw_inode, 0); | ||
571 | /* XXX: call with has_bmap = 0 is a workaround to avoid | ||
572 | deadlock of bmap. This delays update of i_bmap to just | ||
573 | before writing */ | ||
574 | nilfs_ifile_unmap_inode(sbi->s_ifile, ino, ibh); | ||
575 | } | ||
576 | |||
577 | #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ | ||
578 | |||
579 | static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, | ||
580 | unsigned long from) | ||
581 | { | ||
582 | unsigned long b; | ||
583 | int ret; | ||
584 | |||
585 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) | ||
586 | return; | ||
587 | repeat: | ||
588 | ret = nilfs_bmap_last_key(ii->i_bmap, &b); | ||
589 | if (ret == -ENOENT) | ||
590 | return; | ||
591 | else if (ret < 0) | ||
592 | goto failed; | ||
593 | |||
594 | if (b < from) | ||
595 | return; | ||
596 | |||
597 | b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); | ||
598 | ret = nilfs_bmap_truncate(ii->i_bmap, b); | ||
599 | nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); | ||
600 | if (!ret || (ret == -ENOMEM && | ||
601 | nilfs_bmap_truncate(ii->i_bmap, b) == 0)) | ||
602 | goto repeat; | ||
603 | |||
604 | failed: | ||
605 | if (ret == -EINVAL) | ||
606 | nilfs_error(ii->vfs_inode.i_sb, __func__, | ||
607 | "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino); | ||
608 | else | ||
609 | nilfs_warning(ii->vfs_inode.i_sb, __func__, | ||
610 | "failed to truncate bmap (ino=%lu, err=%d)", | ||
611 | ii->vfs_inode.i_ino, ret); | ||
612 | } | ||
613 | |||
614 | void nilfs_truncate(struct inode *inode) | ||
615 | { | ||
616 | unsigned long blkoff; | ||
617 | unsigned int blocksize; | ||
618 | struct nilfs_transaction_info ti; | ||
619 | struct super_block *sb = inode->i_sb; | ||
620 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
621 | int ret; | ||
622 | |||
623 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) | ||
624 | return; | ||
625 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | ||
626 | return; | ||
627 | |||
628 | blocksize = sb->s_blocksize; | ||
629 | blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; | ||
630 | ret = nilfs_transaction_begin(sb, &ti, 0); | ||
631 | BUG_ON(ret); | ||
632 | |||
633 | block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); | ||
634 | |||
635 | nilfs_truncate_bmap(ii, blkoff); | ||
636 | |||
637 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | ||
638 | if (IS_SYNC(inode)) | ||
639 | nilfs_set_transaction_flag(NILFS_TI_SYNC); | ||
640 | |||
641 | nilfs_set_file_dirty(NILFS_SB(sb), inode, 0); | ||
642 | nilfs_transaction_end(sb, 1); | ||
643 | /* May construct a logical segment and may fail in sync mode. | ||
644 | But truncate has no return value. */ | ||
645 | } | ||
646 | |||
647 | void nilfs_delete_inode(struct inode *inode) | ||
648 | { | ||
649 | struct nilfs_transaction_info ti; | ||
650 | struct super_block *sb = inode->i_sb; | ||
651 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
652 | int err; | ||
653 | |||
654 | if (unlikely(is_bad_inode(inode))) { | ||
655 | if (inode->i_data.nrpages) | ||
656 | truncate_inode_pages(&inode->i_data, 0); | ||
657 | clear_inode(inode); | ||
658 | return; | ||
659 | } | ||
660 | err = nilfs_transaction_begin(sb, &ti, 0); | ||
661 | BUG_ON(err); | ||
662 | if (inode->i_data.nrpages) | ||
663 | truncate_inode_pages(&inode->i_data, 0); | ||
664 | |||
665 | nilfs_truncate_bmap(ii, 0); | ||
666 | nilfs_free_inode(inode); | ||
667 | /* nilfs_free_inode() marks inode buffer dirty */ | ||
668 | if (IS_SYNC(inode)) | ||
669 | nilfs_set_transaction_flag(NILFS_TI_SYNC); | ||
670 | nilfs_transaction_end(sb, 1); | ||
671 | /* May construct a logical segment and may fail in sync mode. | ||
672 | But delete_inode has no return value. */ | ||
673 | } | ||
674 | |||
675 | int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) | ||
676 | { | ||
677 | struct nilfs_transaction_info ti; | ||
678 | struct inode *inode = dentry->d_inode; | ||
679 | struct super_block *sb = inode->i_sb; | ||
680 | int err, err2; | ||
681 | |||
682 | err = inode_change_ok(inode, iattr); | ||
683 | if (err) | ||
684 | return err; | ||
685 | |||
686 | err = nilfs_transaction_begin(sb, &ti, 0); | ||
687 | if (unlikely(err)) | ||
688 | return err; | ||
689 | err = inode_setattr(inode, iattr); | ||
690 | if (!err && (iattr->ia_valid & ATTR_MODE)) | ||
691 | err = nilfs_acl_chmod(inode); | ||
692 | err2 = nilfs_transaction_end(sb, 1); | ||
693 | return err ? : err2; | ||
694 | } | ||
695 | |||
696 | int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode, | ||
697 | struct buffer_head **pbh) | ||
698 | { | ||
699 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
700 | int err; | ||
701 | |||
702 | spin_lock(&sbi->s_inode_lock); | ||
703 | /* Caller of this function MUST lock s_inode_lock */ | ||
704 | if (ii->i_bh == NULL) { | ||
705 | spin_unlock(&sbi->s_inode_lock); | ||
706 | err = nilfs_ifile_get_inode_block(sbi->s_ifile, inode->i_ino, | ||
707 | pbh); | ||
708 | if (unlikely(err)) | ||
709 | return err; | ||
710 | spin_lock(&sbi->s_inode_lock); | ||
711 | if (ii->i_bh == NULL) | ||
712 | ii->i_bh = *pbh; | ||
713 | else { | ||
714 | brelse(*pbh); | ||
715 | *pbh = ii->i_bh; | ||
716 | } | ||
717 | } else | ||
718 | *pbh = ii->i_bh; | ||
719 | |||
720 | get_bh(*pbh); | ||
721 | spin_unlock(&sbi->s_inode_lock); | ||
722 | return 0; | ||
723 | } | ||
724 | |||
725 | int nilfs_inode_dirty(struct inode *inode) | ||
726 | { | ||
727 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
728 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | ||
729 | int ret = 0; | ||
730 | |||
731 | if (!list_empty(&ii->i_dirty)) { | ||
732 | spin_lock(&sbi->s_inode_lock); | ||
733 | ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || | ||
734 | test_bit(NILFS_I_BUSY, &ii->i_state); | ||
735 | spin_unlock(&sbi->s_inode_lock); | ||
736 | } | ||
737 | return ret; | ||
738 | } | ||
739 | |||
740 | int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode, | ||
741 | unsigned nr_dirty) | ||
742 | { | ||
743 | struct nilfs_inode_info *ii = NILFS_I(inode); | ||
744 | |||
745 | atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks); | ||
746 | |||
747 | if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state) || | ||
748 | unlikely(inode->i_ino == NILFS_SKETCH_INO)) | ||
749 | return 0; | ||
750 | |||
751 | spin_lock(&sbi->s_inode_lock); | ||
752 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && | ||
753 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { | ||
754 | /* Because this routine may race with nilfs_dispose_list(), | ||
755 | we have to check NILFS_I_QUEUED here, too. */ | ||
756 | if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { | ||
757 | /* This will happen when somebody is freeing | ||
758 | this inode. */ | ||
759 | nilfs_warning(sbi->s_super, __func__, | ||
760 | "cannot get inode (ino=%lu)\n", | ||
761 | inode->i_ino); | ||
762 | spin_unlock(&sbi->s_inode_lock); | ||
763 | return -EINVAL; /* NILFS_I_DIRTY may remain for | ||
764 | freeing inode */ | ||
765 | } | ||
766 | list_del(&ii->i_dirty); | ||
767 | list_add_tail(&ii->i_dirty, &sbi->s_dirty_files); | ||
768 | set_bit(NILFS_I_QUEUED, &ii->i_state); | ||
769 | } | ||
770 | spin_unlock(&sbi->s_inode_lock); | ||
771 | return 0; | ||
772 | } | ||
773 | |||
774 | int nilfs_mark_inode_dirty(struct inode *inode) | ||
775 | { | ||
776 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | ||
777 | struct buffer_head *ibh; | ||
778 | int err; | ||
779 | |||
780 | err = nilfs_load_inode_block(sbi, inode, &ibh); | ||
781 | if (unlikely(err)) { | ||
782 | nilfs_warning(inode->i_sb, __func__, | ||
783 | "failed to reget inode block.\n"); | ||
784 | return err; | ||
785 | } | ||
786 | lock_buffer(ibh); | ||
787 | nilfs_update_inode(inode, ibh); | ||
788 | unlock_buffer(ibh); | ||
789 | nilfs_mdt_mark_buffer_dirty(ibh); | ||
790 | nilfs_mdt_mark_dirty(sbi->s_ifile); | ||
791 | brelse(ibh); | ||
792 | return 0; | ||
793 | } | ||
794 | |||
795 | /** | ||
796 | * nilfs_dirty_inode - reflect changes on given inode to an inode block. | ||
797 | * @inode: inode of the file to be registered. | ||
798 | * | ||
799 | * nilfs_dirty_inode() loads a inode block containing the specified | ||
800 | * @inode and copies data from a nilfs_inode to a corresponding inode | ||
801 | * entry in the inode block. This operation is excluded from the segment | ||
802 | * construction. This function can be called both as a single operation | ||
803 | * and as a part of indivisible file operations. | ||
804 | */ | ||
805 | void nilfs_dirty_inode(struct inode *inode) | ||
806 | { | ||
807 | struct nilfs_transaction_info ti; | ||
808 | |||
809 | if (is_bad_inode(inode)) { | ||
810 | nilfs_warning(inode->i_sb, __func__, | ||
811 | "tried to mark bad_inode dirty. ignored.\n"); | ||
812 | dump_stack(); | ||
813 | return; | ||
814 | } | ||
815 | nilfs_transaction_begin(inode->i_sb, &ti, 0); | ||
816 | if (likely(inode->i_ino != NILFS_SKETCH_INO)) | ||
817 | nilfs_mark_inode_dirty(inode); | ||
818 | nilfs_transaction_end(inode->i_sb, 1); /* never fails */ | ||
819 | } | ||