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