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