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> | |
5a0e3ad6 | 25 | #include <linux/gfp.h> |
05fe58fd RK |
26 | #include <linux/mpage.h> |
27 | #include <linux/writeback.h> | |
f30bf3e4 | 28 | #include <linux/uio.h> |
05fe58fd | 29 | #include "nilfs.h" |
6fd1e5c9 | 30 | #include "btnode.h" |
05fe58fd RK |
31 | #include "segment.h" |
32 | #include "page.h" | |
33 | #include "mdt.h" | |
34 | #include "cpfile.h" | |
35 | #include "ifile.h" | |
36 | ||
0e14a359 RK |
37 | struct nilfs_iget_args { |
38 | u64 ino; | |
39 | __u64 cno; | |
4d8d9293 | 40 | struct nilfs_root *root; |
0e14a359 RK |
41 | int for_gc; |
42 | }; | |
05fe58fd RK |
43 | |
44 | /** | |
45 | * nilfs_get_block() - get a file block on the filesystem (callback function) | |
46 | * @inode - inode struct of the target file | |
47 | * @blkoff - file block number | |
48 | * @bh_result - buffer head to be mapped on | |
49 | * @create - indicate whether allocating the block or not when it has not | |
50 | * been allocated yet. | |
51 | * | |
52 | * This function does not issue actual read request of the specified data | |
53 | * block. It is done by VFS. | |
05fe58fd RK |
54 | */ |
55 | int nilfs_get_block(struct inode *inode, sector_t blkoff, | |
56 | struct buffer_head *bh_result, int create) | |
57 | { | |
58 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
c3a7abf0 | 59 | __u64 blknum = 0; |
05fe58fd RK |
60 | int err = 0, ret; |
61 | struct inode *dat = nilfs_dat_inode(NILFS_I_NILFS(inode)); | |
c3a7abf0 | 62 | unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; |
05fe58fd | 63 | |
c3a7abf0 RK |
64 | down_read(&NILFS_MDT(dat)->mi_sem); |
65 | ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); | |
66 | up_read(&NILFS_MDT(dat)->mi_sem); | |
67 | if (ret >= 0) { /* found */ | |
05fe58fd | 68 | map_bh(bh_result, inode->i_sb, blknum); |
c3a7abf0 RK |
69 | if (ret > 0) |
70 | bh_result->b_size = (ret << inode->i_blkbits); | |
05fe58fd RK |
71 | goto out; |
72 | } | |
05fe58fd RK |
73 | /* data block was not found */ |
74 | if (ret == -ENOENT && create) { | |
75 | struct nilfs_transaction_info ti; | |
76 | ||
77 | bh_result->b_blocknr = 0; | |
78 | err = nilfs_transaction_begin(inode->i_sb, &ti, 1); | |
79 | if (unlikely(err)) | |
80 | goto out; | |
81 | err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, | |
82 | (unsigned long)bh_result); | |
05fe58fd RK |
83 | if (unlikely(err != 0)) { |
84 | if (err == -EEXIST) { | |
85 | /* | |
86 | * The get_block() function could be called | |
87 | * from multiple callers for an inode. | |
88 | * However, the page having this block must | |
89 | * be locked in this case. | |
90 | */ | |
1f5abe7e | 91 | printk(KERN_WARNING |
05fe58fd RK |
92 | "nilfs_get_block: a race condition " |
93 | "while inserting a data block. " | |
94 | "(inode number=%lu, file block " | |
95 | "offset=%llu)\n", | |
96 | inode->i_ino, | |
97 | (unsigned long long)blkoff); | |
1f5abe7e | 98 | err = 0; |
05fe58fd RK |
99 | } else if (err == -EINVAL) { |
100 | nilfs_error(inode->i_sb, __func__, | |
101 | "broken bmap (inode=%lu)\n", | |
102 | inode->i_ino); | |
103 | err = -EIO; | |
104 | } | |
47420c79 | 105 | nilfs_transaction_abort(inode->i_sb); |
05fe58fd RK |
106 | goto out; |
107 | } | |
abdb318b | 108 | nilfs_mark_inode_dirty(inode); |
47420c79 | 109 | nilfs_transaction_commit(inode->i_sb); /* never fails */ |
05fe58fd RK |
110 | /* Error handling should be detailed */ |
111 | set_buffer_new(bh_result); | |
112 | map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed | |
113 | to proper value */ | |
114 | } else if (ret == -ENOENT) { | |
115 | /* not found is not error (e.g. hole); must return without | |
116 | the mapped state flag. */ | |
117 | ; | |
118 | } else { | |
119 | err = ret; | |
120 | } | |
121 | ||
122 | out: | |
123 | return err; | |
124 | } | |
125 | ||
126 | /** | |
127 | * nilfs_readpage() - implement readpage() method of nilfs_aops {} | |
128 | * address_space_operations. | |
129 | * @file - file struct of the file to be read | |
130 | * @page - the page to be read | |
131 | */ | |
132 | static int nilfs_readpage(struct file *file, struct page *page) | |
133 | { | |
134 | return mpage_readpage(page, nilfs_get_block); | |
135 | } | |
136 | ||
137 | /** | |
138 | * nilfs_readpages() - implement readpages() method of nilfs_aops {} | |
139 | * address_space_operations. | |
140 | * @file - file struct of the file to be read | |
141 | * @mapping - address_space struct used for reading multiple pages | |
142 | * @pages - the pages to be read | |
143 | * @nr_pages - number of pages to be read | |
144 | */ | |
145 | static int nilfs_readpages(struct file *file, struct address_space *mapping, | |
146 | struct list_head *pages, unsigned nr_pages) | |
147 | { | |
148 | return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); | |
149 | } | |
150 | ||
151 | static int nilfs_writepages(struct address_space *mapping, | |
152 | struct writeback_control *wbc) | |
153 | { | |
f30bf3e4 RK |
154 | struct inode *inode = mapping->host; |
155 | int err = 0; | |
156 | ||
157 | if (wbc->sync_mode == WB_SYNC_ALL) | |
158 | err = nilfs_construct_dsync_segment(inode->i_sb, inode, | |
159 | wbc->range_start, | |
160 | wbc->range_end); | |
161 | return err; | |
05fe58fd RK |
162 | } |
163 | ||
164 | static int nilfs_writepage(struct page *page, struct writeback_control *wbc) | |
165 | { | |
166 | struct inode *inode = page->mapping->host; | |
167 | int err; | |
168 | ||
169 | redirty_page_for_writepage(wbc, page); | |
170 | unlock_page(page); | |
171 | ||
172 | if (wbc->sync_mode == WB_SYNC_ALL) { | |
173 | err = nilfs_construct_segment(inode->i_sb); | |
174 | if (unlikely(err)) | |
175 | return err; | |
176 | } else if (wbc->for_reclaim) | |
177 | nilfs_flush_segment(inode->i_sb, inode->i_ino); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | static int nilfs_set_page_dirty(struct page *page) | |
183 | { | |
184 | int ret = __set_page_dirty_buffers(page); | |
185 | ||
186 | if (ret) { | |
187 | struct inode *inode = page->mapping->host; | |
188 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | |
189 | unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); | |
190 | ||
191 | nilfs_set_file_dirty(sbi, inode, nr_dirty); | |
192 | } | |
193 | return ret; | |
194 | } | |
195 | ||
196 | static int nilfs_write_begin(struct file *file, struct address_space *mapping, | |
197 | loff_t pos, unsigned len, unsigned flags, | |
198 | struct page **pagep, void **fsdata) | |
199 | ||
200 | { | |
201 | struct inode *inode = mapping->host; | |
202 | int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); | |
203 | ||
204 | if (unlikely(err)) | |
205 | return err; | |
206 | ||
155130a4 CH |
207 | err = block_write_begin(mapping, pos, len, flags, pagep, |
208 | nilfs_get_block); | |
209 | if (unlikely(err)) { | |
210 | loff_t isize = mapping->host->i_size; | |
211 | if (pos + len > isize) | |
212 | vmtruncate(mapping->host, isize); | |
213 | ||
47420c79 | 214 | nilfs_transaction_abort(inode->i_sb); |
155130a4 | 215 | } |
05fe58fd RK |
216 | return err; |
217 | } | |
218 | ||
219 | static int nilfs_write_end(struct file *file, struct address_space *mapping, | |
220 | loff_t pos, unsigned len, unsigned copied, | |
221 | struct page *page, void *fsdata) | |
222 | { | |
223 | struct inode *inode = mapping->host; | |
224 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); | |
225 | unsigned nr_dirty; | |
226 | int err; | |
227 | ||
228 | nr_dirty = nilfs_page_count_clean_buffers(page, start, | |
229 | start + copied); | |
230 | copied = generic_write_end(file, mapping, pos, len, copied, page, | |
231 | fsdata); | |
232 | nilfs_set_file_dirty(NILFS_SB(inode->i_sb), inode, nr_dirty); | |
47420c79 | 233 | err = nilfs_transaction_commit(inode->i_sb); |
05fe58fd RK |
234 | return err ? : copied; |
235 | } | |
236 | ||
237 | static ssize_t | |
238 | nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, | |
239 | loff_t offset, unsigned long nr_segs) | |
240 | { | |
241 | struct file *file = iocb->ki_filp; | |
242 | struct inode *inode = file->f_mapping->host; | |
243 | ssize_t size; | |
05fe58fd RK |
244 | |
245 | if (rw == WRITE) | |
246 | return 0; | |
247 | ||
248 | /* Needs synchronization with the cleaner */ | |
249 | size = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | |
250 | offset, nr_segs, nilfs_get_block, NULL); | |
eafdc7d1 CH |
251 | |
252 | /* | |
253 | * In case of error extending write may have instantiated a few | |
254 | * blocks outside i_size. Trim these off again. | |
255 | */ | |
256 | if (unlikely((rw & WRITE) && size < 0)) { | |
257 | loff_t isize = i_size_read(inode); | |
258 | loff_t end = offset + iov_length(iov, nr_segs); | |
259 | ||
260 | if (end > isize) | |
261 | vmtruncate(inode, isize); | |
262 | } | |
263 | ||
05fe58fd RK |
264 | return size; |
265 | } | |
266 | ||
7f09410b | 267 | const struct address_space_operations nilfs_aops = { |
05fe58fd RK |
268 | .writepage = nilfs_writepage, |
269 | .readpage = nilfs_readpage, | |
e85dc1d5 | 270 | .sync_page = block_sync_page, |
05fe58fd RK |
271 | .writepages = nilfs_writepages, |
272 | .set_page_dirty = nilfs_set_page_dirty, | |
273 | .readpages = nilfs_readpages, | |
274 | .write_begin = nilfs_write_begin, | |
275 | .write_end = nilfs_write_end, | |
276 | /* .releasepage = nilfs_releasepage, */ | |
277 | .invalidatepage = block_invalidatepage, | |
278 | .direct_IO = nilfs_direct_IO, | |
258ef67e | 279 | .is_partially_uptodate = block_is_partially_uptodate, |
05fe58fd RK |
280 | }; |
281 | ||
282 | struct inode *nilfs_new_inode(struct inode *dir, int mode) | |
283 | { | |
284 | struct super_block *sb = dir->i_sb; | |
285 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
286 | struct inode *inode; | |
287 | struct nilfs_inode_info *ii; | |
4d8d9293 | 288 | struct nilfs_root *root; |
05fe58fd RK |
289 | int err = -ENOMEM; |
290 | ino_t ino; | |
291 | ||
292 | inode = new_inode(sb); | |
293 | if (unlikely(!inode)) | |
294 | goto failed; | |
295 | ||
296 | mapping_set_gfp_mask(inode->i_mapping, | |
297 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); | |
298 | ||
4d8d9293 | 299 | root = NILFS_I(dir)->i_root; |
05fe58fd RK |
300 | ii = NILFS_I(inode); |
301 | ii->i_state = 1 << NILFS_I_NEW; | |
4d8d9293 | 302 | ii->i_root = root; |
05fe58fd | 303 | |
e912a5b6 | 304 | err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); |
05fe58fd RK |
305 | if (unlikely(err)) |
306 | goto failed_ifile_create_inode; | |
307 | /* reference count of i_bh inherits from nilfs_mdt_read_block() */ | |
308 | ||
b7c06342 | 309 | atomic_inc(&root->inodes_count); |
73459dcc | 310 | inode_init_owner(inode, dir, mode); |
05fe58fd RK |
311 | inode->i_ino = ino; |
312 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
313 | ||
314 | if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { | |
315 | err = nilfs_bmap_read(ii->i_bmap, NULL); | |
316 | if (err < 0) | |
317 | goto failed_bmap; | |
318 | ||
319 | set_bit(NILFS_I_BMAP, &ii->i_state); | |
320 | /* No lock is needed; iget() ensures it. */ | |
321 | } | |
322 | ||
323 | ii->i_flags = NILFS_I(dir)->i_flags; | |
324 | if (S_ISLNK(mode)) | |
325 | ii->i_flags &= ~(NILFS_IMMUTABLE_FL | NILFS_APPEND_FL); | |
326 | if (!S_ISDIR(mode)) | |
327 | ii->i_flags &= ~NILFS_DIRSYNC_FL; | |
328 | ||
329 | /* ii->i_file_acl = 0; */ | |
330 | /* ii->i_dir_acl = 0; */ | |
05fe58fd | 331 | ii->i_dir_start_lookup = 0; |
05fe58fd RK |
332 | nilfs_set_inode_flags(inode); |
333 | spin_lock(&sbi->s_next_gen_lock); | |
334 | inode->i_generation = sbi->s_next_generation++; | |
335 | spin_unlock(&sbi->s_next_gen_lock); | |
336 | insert_inode_hash(inode); | |
337 | ||
338 | err = nilfs_init_acl(inode, dir); | |
339 | if (unlikely(err)) | |
340 | goto failed_acl; /* never occur. When supporting | |
341 | nilfs_init_acl(), proper cancellation of | |
342 | above jobs should be considered */ | |
343 | ||
05fe58fd RK |
344 | return inode; |
345 | ||
346 | failed_acl: | |
347 | failed_bmap: | |
348 | inode->i_nlink = 0; | |
349 | iput(inode); /* raw_inode will be deleted through | |
350 | generic_delete_inode() */ | |
351 | goto failed; | |
352 | ||
353 | failed_ifile_create_inode: | |
354 | make_bad_inode(inode); | |
355 | iput(inode); /* if i_nlink == 1, generic_forget_inode() will be | |
356 | called */ | |
357 | failed: | |
358 | return ERR_PTR(err); | |
359 | } | |
360 | ||
05fe58fd RK |
361 | void nilfs_set_inode_flags(struct inode *inode) |
362 | { | |
363 | unsigned int flags = NILFS_I(inode)->i_flags; | |
364 | ||
365 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | | |
366 | S_DIRSYNC); | |
367 | if (flags & NILFS_SYNC_FL) | |
368 | inode->i_flags |= S_SYNC; | |
369 | if (flags & NILFS_APPEND_FL) | |
370 | inode->i_flags |= S_APPEND; | |
371 | if (flags & NILFS_IMMUTABLE_FL) | |
372 | inode->i_flags |= S_IMMUTABLE; | |
373 | #ifndef NILFS_ATIME_DISABLE | |
374 | if (flags & NILFS_NOATIME_FL) | |
375 | #endif | |
376 | inode->i_flags |= S_NOATIME; | |
377 | if (flags & NILFS_DIRSYNC_FL) | |
378 | inode->i_flags |= S_DIRSYNC; | |
379 | mapping_set_gfp_mask(inode->i_mapping, | |
380 | mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); | |
381 | } | |
382 | ||
383 | int nilfs_read_inode_common(struct inode *inode, | |
384 | struct nilfs_inode *raw_inode) | |
385 | { | |
386 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
387 | int err; | |
388 | ||
389 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); | |
390 | inode->i_uid = (uid_t)le32_to_cpu(raw_inode->i_uid); | |
391 | inode->i_gid = (gid_t)le32_to_cpu(raw_inode->i_gid); | |
392 | inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); | |
393 | inode->i_size = le64_to_cpu(raw_inode->i_size); | |
394 | inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); | |
395 | inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); | |
396 | inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); | |
61239230 RK |
397 | inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); |
398 | inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); | |
399 | inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); | |
400 | if (inode->i_nlink == 0 && inode->i_mode == 0) | |
05fe58fd RK |
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 | |
3cc811bf | 410 | ii->i_dir_start_lookup = 0; |
05fe58fd RK |
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 | ||
e912a5b6 RK |
424 | static int __nilfs_read_inode(struct super_block *sb, |
425 | struct nilfs_root *root, unsigned long ino, | |
05fe58fd RK |
426 | struct inode *inode) |
427 | { | |
428 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
429 | struct inode *dat = nilfs_dat_inode(sbi->s_nilfs); | |
430 | struct buffer_head *bh; | |
431 | struct nilfs_inode *raw_inode; | |
432 | int err; | |
433 | ||
434 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
e912a5b6 | 435 | err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); |
05fe58fd RK |
436 | if (unlikely(err)) |
437 | goto bad_inode; | |
438 | ||
e912a5b6 | 439 | raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); |
05fe58fd | 440 | |
1b2f5a64 RK |
441 | err = nilfs_read_inode_common(inode, raw_inode); |
442 | if (err) | |
05fe58fd RK |
443 | goto failed_unmap; |
444 | ||
445 | if (S_ISREG(inode->i_mode)) { | |
446 | inode->i_op = &nilfs_file_inode_operations; | |
447 | inode->i_fop = &nilfs_file_operations; | |
448 | inode->i_mapping->a_ops = &nilfs_aops; | |
05fe58fd RK |
449 | } else if (S_ISDIR(inode->i_mode)) { |
450 | inode->i_op = &nilfs_dir_inode_operations; | |
451 | inode->i_fop = &nilfs_dir_operations; | |
452 | inode->i_mapping->a_ops = &nilfs_aops; | |
453 | } else if (S_ISLNK(inode->i_mode)) { | |
454 | inode->i_op = &nilfs_symlink_inode_operations; | |
455 | inode->i_mapping->a_ops = &nilfs_aops; | |
456 | } else { | |
457 | inode->i_op = &nilfs_special_inode_operations; | |
458 | init_special_inode( | |
459 | inode, inode->i_mode, | |
cdce214e | 460 | huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); |
05fe58fd | 461 | } |
e912a5b6 | 462 | nilfs_ifile_unmap_inode(root->ifile, ino, bh); |
05fe58fd RK |
463 | brelse(bh); |
464 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
465 | nilfs_set_inode_flags(inode); | |
466 | return 0; | |
467 | ||
468 | failed_unmap: | |
e912a5b6 | 469 | nilfs_ifile_unmap_inode(root->ifile, ino, bh); |
05fe58fd RK |
470 | brelse(bh); |
471 | ||
472 | bad_inode: | |
473 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
474 | return err; | |
475 | } | |
476 | ||
0e14a359 RK |
477 | static int nilfs_iget_test(struct inode *inode, void *opaque) |
478 | { | |
479 | struct nilfs_iget_args *args = opaque; | |
480 | struct nilfs_inode_info *ii; | |
481 | ||
4d8d9293 | 482 | if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) |
0e14a359 RK |
483 | return 0; |
484 | ||
485 | ii = NILFS_I(inode); | |
486 | if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) | |
487 | return !args->for_gc; | |
488 | ||
489 | return args->for_gc && args->cno == ii->i_cno; | |
490 | } | |
491 | ||
492 | static int nilfs_iget_set(struct inode *inode, void *opaque) | |
493 | { | |
494 | struct nilfs_iget_args *args = opaque; | |
495 | ||
496 | inode->i_ino = args->ino; | |
497 | if (args->for_gc) { | |
498 | NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; | |
499 | NILFS_I(inode)->i_cno = args->cno; | |
4d8d9293 RK |
500 | NILFS_I(inode)->i_root = NULL; |
501 | } else { | |
502 | if (args->root && args->ino == NILFS_ROOT_INO) | |
503 | nilfs_get_root(args->root); | |
504 | NILFS_I(inode)->i_root = args->root; | |
0e14a359 RK |
505 | } |
506 | return 0; | |
507 | } | |
508 | ||
032dbb3b RK |
509 | struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, |
510 | unsigned long ino) | |
511 | { | |
512 | struct nilfs_iget_args args = { | |
513 | .ino = ino, .root = root, .cno = 0, .for_gc = 0 | |
514 | }; | |
515 | ||
516 | return ilookup5(sb, ino, nilfs_iget_test, &args); | |
517 | } | |
518 | ||
f1e89c86 RK |
519 | struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, |
520 | unsigned long ino) | |
05fe58fd | 521 | { |
4d8d9293 RK |
522 | struct nilfs_iget_args args = { |
523 | .ino = ino, .root = root, .cno = 0, .for_gc = 0 | |
524 | }; | |
f1e89c86 RK |
525 | |
526 | return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); | |
527 | } | |
528 | ||
529 | struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, | |
530 | unsigned long ino) | |
531 | { | |
05fe58fd RK |
532 | struct inode *inode; |
533 | int err; | |
534 | ||
f1e89c86 | 535 | inode = nilfs_iget_locked(sb, root, ino); |
05fe58fd RK |
536 | if (unlikely(!inode)) |
537 | return ERR_PTR(-ENOMEM); | |
538 | if (!(inode->i_state & I_NEW)) | |
539 | return inode; | |
540 | ||
e912a5b6 | 541 | err = __nilfs_read_inode(sb, root, ino, inode); |
05fe58fd RK |
542 | if (unlikely(err)) { |
543 | iget_failed(inode); | |
544 | return ERR_PTR(err); | |
545 | } | |
546 | unlock_new_inode(inode); | |
547 | return inode; | |
548 | } | |
549 | ||
263d90ce RK |
550 | struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, |
551 | __u64 cno) | |
552 | { | |
4d8d9293 RK |
553 | struct nilfs_iget_args args = { |
554 | .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 | |
555 | }; | |
263d90ce RK |
556 | struct inode *inode; |
557 | int err; | |
558 | ||
559 | inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); | |
560 | if (unlikely(!inode)) | |
561 | return ERR_PTR(-ENOMEM); | |
562 | if (!(inode->i_state & I_NEW)) | |
563 | return inode; | |
564 | ||
565 | err = nilfs_init_gcinode(inode); | |
566 | if (unlikely(err)) { | |
567 | iget_failed(inode); | |
568 | return ERR_PTR(err); | |
569 | } | |
570 | unlock_new_inode(inode); | |
571 | return inode; | |
572 | } | |
573 | ||
05fe58fd RK |
574 | void nilfs_write_inode_common(struct inode *inode, |
575 | struct nilfs_inode *raw_inode, int has_bmap) | |
576 | { | |
577 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
578 | ||
579 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); | |
580 | raw_inode->i_uid = cpu_to_le32(inode->i_uid); | |
581 | raw_inode->i_gid = cpu_to_le32(inode->i_gid); | |
582 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | |
583 | raw_inode->i_size = cpu_to_le64(inode->i_size); | |
584 | raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); | |
585 | raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); | |
61239230 RK |
586 | raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
587 | raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); | |
05fe58fd RK |
588 | raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); |
589 | ||
05fe58fd RK |
590 | raw_inode->i_flags = cpu_to_le32(ii->i_flags); |
591 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | |
592 | ||
593 | if (has_bmap) | |
594 | nilfs_bmap_write(ii->i_bmap, raw_inode); | |
595 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) | |
596 | raw_inode->i_device_code = | |
cdce214e | 597 | cpu_to_le64(huge_encode_dev(inode->i_rdev)); |
05fe58fd RK |
598 | /* When extending inode, nilfs->ns_inode_size should be checked |
599 | for substitutions of appended fields */ | |
600 | } | |
601 | ||
602 | void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) | |
603 | { | |
604 | ino_t ino = inode->i_ino; | |
605 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
e912a5b6 | 606 | struct inode *ifile = ii->i_root->ifile; |
05fe58fd RK |
607 | struct nilfs_inode *raw_inode; |
608 | ||
e912a5b6 | 609 | raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); |
05fe58fd | 610 | |
05fe58fd | 611 | if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) |
e912a5b6 | 612 | memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); |
05fe58fd RK |
613 | set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); |
614 | ||
615 | nilfs_write_inode_common(inode, raw_inode, 0); | |
616 | /* XXX: call with has_bmap = 0 is a workaround to avoid | |
617 | deadlock of bmap. This delays update of i_bmap to just | |
618 | before writing */ | |
e912a5b6 | 619 | nilfs_ifile_unmap_inode(ifile, ino, ibh); |
05fe58fd RK |
620 | } |
621 | ||
622 | #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ | |
623 | ||
624 | static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, | |
625 | unsigned long from) | |
626 | { | |
627 | unsigned long b; | |
628 | int ret; | |
629 | ||
630 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) | |
631 | return; | |
632 | repeat: | |
633 | ret = nilfs_bmap_last_key(ii->i_bmap, &b); | |
634 | if (ret == -ENOENT) | |
635 | return; | |
636 | else if (ret < 0) | |
637 | goto failed; | |
638 | ||
639 | if (b < from) | |
640 | return; | |
641 | ||
642 | b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); | |
643 | ret = nilfs_bmap_truncate(ii->i_bmap, b); | |
644 | nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); | |
645 | if (!ret || (ret == -ENOMEM && | |
646 | nilfs_bmap_truncate(ii->i_bmap, b) == 0)) | |
647 | goto repeat; | |
648 | ||
649 | failed: | |
650 | if (ret == -EINVAL) | |
651 | nilfs_error(ii->vfs_inode.i_sb, __func__, | |
652 | "bmap is broken (ino=%lu)", ii->vfs_inode.i_ino); | |
653 | else | |
654 | nilfs_warning(ii->vfs_inode.i_sb, __func__, | |
655 | "failed to truncate bmap (ino=%lu, err=%d)", | |
656 | ii->vfs_inode.i_ino, ret); | |
657 | } | |
658 | ||
659 | void nilfs_truncate(struct inode *inode) | |
660 | { | |
661 | unsigned long blkoff; | |
662 | unsigned int blocksize; | |
663 | struct nilfs_transaction_info ti; | |
664 | struct super_block *sb = inode->i_sb; | |
665 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
05fe58fd RK |
666 | |
667 | if (!test_bit(NILFS_I_BMAP, &ii->i_state)) | |
668 | return; | |
669 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
670 | return; | |
671 | ||
672 | blocksize = sb->s_blocksize; | |
673 | blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; | |
1f5abe7e | 674 | nilfs_transaction_begin(sb, &ti, 0); /* never fails */ |
05fe58fd RK |
675 | |
676 | block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); | |
677 | ||
678 | nilfs_truncate_bmap(ii, blkoff); | |
679 | ||
680 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
681 | if (IS_SYNC(inode)) | |
682 | nilfs_set_transaction_flag(NILFS_TI_SYNC); | |
683 | ||
abdb318b | 684 | nilfs_mark_inode_dirty(inode); |
05fe58fd | 685 | nilfs_set_file_dirty(NILFS_SB(sb), inode, 0); |
47420c79 | 686 | nilfs_transaction_commit(sb); |
05fe58fd RK |
687 | /* May construct a logical segment and may fail in sync mode. |
688 | But truncate has no return value. */ | |
689 | } | |
690 | ||
6fd1e5c9 AV |
691 | static void nilfs_clear_inode(struct inode *inode) |
692 | { | |
693 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
518d1a6a | 694 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode); |
6fd1e5c9 AV |
695 | |
696 | /* | |
697 | * Free resources allocated in nilfs_read_inode(), here. | |
698 | */ | |
699 | BUG_ON(!list_empty(&ii->i_dirty)); | |
700 | brelse(ii->i_bh); | |
701 | ii->i_bh = NULL; | |
702 | ||
518d1a6a RK |
703 | if (mdi && mdi->mi_palloc_cache) |
704 | nilfs_palloc_destroy_cache(inode); | |
705 | ||
6fd1e5c9 AV |
706 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) |
707 | nilfs_bmap_clear(ii->i_bmap); | |
708 | ||
709 | nilfs_btnode_cache_clear(&ii->i_btnode_cache); | |
4d8d9293 RK |
710 | |
711 | if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) | |
712 | nilfs_put_root(ii->i_root); | |
6fd1e5c9 AV |
713 | } |
714 | ||
715 | void nilfs_evict_inode(struct inode *inode) | |
05fe58fd RK |
716 | { |
717 | struct nilfs_transaction_info ti; | |
718 | struct super_block *sb = inode->i_sb; | |
719 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
05fe58fd | 720 | |
4d8d9293 | 721 | if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { |
05fe58fd RK |
722 | if (inode->i_data.nrpages) |
723 | truncate_inode_pages(&inode->i_data, 0); | |
6fd1e5c9 AV |
724 | end_writeback(inode); |
725 | nilfs_clear_inode(inode); | |
05fe58fd RK |
726 | return; |
727 | } | |
1f5abe7e RK |
728 | nilfs_transaction_begin(sb, &ti, 0); /* never fails */ |
729 | ||
05fe58fd RK |
730 | if (inode->i_data.nrpages) |
731 | truncate_inode_pages(&inode->i_data, 0); | |
732 | ||
e912a5b6 | 733 | /* TODO: some of the following operations may fail. */ |
05fe58fd | 734 | nilfs_truncate_bmap(ii, 0); |
abdb318b | 735 | nilfs_mark_inode_dirty(inode); |
6fd1e5c9 | 736 | end_writeback(inode); |
e912a5b6 RK |
737 | |
738 | nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); | |
b7c06342 | 739 | atomic_dec(&ii->i_root->inodes_count); |
e912a5b6 | 740 | |
6fd1e5c9 | 741 | nilfs_clear_inode(inode); |
e912a5b6 | 742 | |
05fe58fd RK |
743 | if (IS_SYNC(inode)) |
744 | nilfs_set_transaction_flag(NILFS_TI_SYNC); | |
47420c79 | 745 | nilfs_transaction_commit(sb); |
05fe58fd RK |
746 | /* May construct a logical segment and may fail in sync mode. |
747 | But delete_inode has no return value. */ | |
748 | } | |
749 | ||
750 | int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) | |
751 | { | |
752 | struct nilfs_transaction_info ti; | |
753 | struct inode *inode = dentry->d_inode; | |
754 | struct super_block *sb = inode->i_sb; | |
47420c79 | 755 | int err; |
05fe58fd RK |
756 | |
757 | err = inode_change_ok(inode, iattr); | |
758 | if (err) | |
759 | return err; | |
760 | ||
761 | err = nilfs_transaction_begin(sb, &ti, 0); | |
762 | if (unlikely(err)) | |
763 | return err; | |
1025774c CH |
764 | |
765 | if ((iattr->ia_valid & ATTR_SIZE) && | |
766 | iattr->ia_size != i_size_read(inode)) { | |
767 | err = vmtruncate(inode, iattr->ia_size); | |
768 | if (unlikely(err)) | |
769 | goto out_err; | |
770 | } | |
771 | ||
772 | setattr_copy(inode, iattr); | |
773 | mark_inode_dirty(inode); | |
774 | ||
775 | if (iattr->ia_valid & ATTR_MODE) { | |
05fe58fd | 776 | err = nilfs_acl_chmod(inode); |
1025774c CH |
777 | if (unlikely(err)) |
778 | goto out_err; | |
779 | } | |
780 | ||
781 | return nilfs_transaction_commit(sb); | |
47420c79 | 782 | |
1025774c CH |
783 | out_err: |
784 | nilfs_transaction_abort(sb); | |
47420c79 | 785 | return err; |
05fe58fd RK |
786 | } |
787 | ||
b74c79e9 | 788 | int nilfs_permission(struct inode *inode, int mask, unsigned int flags) |
dc3d3b81 | 789 | { |
b74c79e9 NP |
790 | struct nilfs_root *root; |
791 | ||
792 | if (flags & IPERM_FLAG_RCU) | |
793 | return -ECHILD; | |
dc3d3b81 | 794 | |
b74c79e9 | 795 | root = NILFS_I(inode)->i_root; |
dc3d3b81 RK |
796 | if ((mask & MAY_WRITE) && root && |
797 | root->cno != NILFS_CPTREE_CURRENT_CNO) | |
798 | return -EROFS; /* snapshot is not writable */ | |
799 | ||
b74c79e9 | 800 | return generic_permission(inode, mask, flags, NULL); |
dc3d3b81 RK |
801 | } |
802 | ||
05fe58fd RK |
803 | int nilfs_load_inode_block(struct nilfs_sb_info *sbi, struct inode *inode, |
804 | struct buffer_head **pbh) | |
805 | { | |
806 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
807 | int err; | |
808 | ||
809 | spin_lock(&sbi->s_inode_lock); | |
05fe58fd RK |
810 | if (ii->i_bh == NULL) { |
811 | spin_unlock(&sbi->s_inode_lock); | |
e912a5b6 RK |
812 | err = nilfs_ifile_get_inode_block(ii->i_root->ifile, |
813 | inode->i_ino, pbh); | |
05fe58fd RK |
814 | if (unlikely(err)) |
815 | return err; | |
816 | spin_lock(&sbi->s_inode_lock); | |
817 | if (ii->i_bh == NULL) | |
818 | ii->i_bh = *pbh; | |
819 | else { | |
820 | brelse(*pbh); | |
821 | *pbh = ii->i_bh; | |
822 | } | |
823 | } else | |
824 | *pbh = ii->i_bh; | |
825 | ||
826 | get_bh(*pbh); | |
827 | spin_unlock(&sbi->s_inode_lock); | |
828 | return 0; | |
829 | } | |
830 | ||
831 | int nilfs_inode_dirty(struct inode *inode) | |
832 | { | |
833 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
834 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | |
835 | int ret = 0; | |
836 | ||
837 | if (!list_empty(&ii->i_dirty)) { | |
838 | spin_lock(&sbi->s_inode_lock); | |
839 | ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || | |
840 | test_bit(NILFS_I_BUSY, &ii->i_state); | |
841 | spin_unlock(&sbi->s_inode_lock); | |
842 | } | |
843 | return ret; | |
844 | } | |
845 | ||
846 | int nilfs_set_file_dirty(struct nilfs_sb_info *sbi, struct inode *inode, | |
847 | unsigned nr_dirty) | |
848 | { | |
849 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
850 | ||
851 | atomic_add(nr_dirty, &sbi->s_nilfs->ns_ndirtyblks); | |
852 | ||
458c5b08 | 853 | if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) |
05fe58fd RK |
854 | return 0; |
855 | ||
856 | spin_lock(&sbi->s_inode_lock); | |
857 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && | |
858 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { | |
859 | /* Because this routine may race with nilfs_dispose_list(), | |
860 | we have to check NILFS_I_QUEUED here, too. */ | |
861 | if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { | |
862 | /* This will happen when somebody is freeing | |
863 | this inode. */ | |
864 | nilfs_warning(sbi->s_super, __func__, | |
865 | "cannot get inode (ino=%lu)\n", | |
866 | inode->i_ino); | |
867 | spin_unlock(&sbi->s_inode_lock); | |
868 | return -EINVAL; /* NILFS_I_DIRTY may remain for | |
869 | freeing inode */ | |
870 | } | |
871 | list_del(&ii->i_dirty); | |
872 | list_add_tail(&ii->i_dirty, &sbi->s_dirty_files); | |
873 | set_bit(NILFS_I_QUEUED, &ii->i_state); | |
874 | } | |
875 | spin_unlock(&sbi->s_inode_lock); | |
876 | return 0; | |
877 | } | |
878 | ||
879 | int nilfs_mark_inode_dirty(struct inode *inode) | |
880 | { | |
881 | struct nilfs_sb_info *sbi = NILFS_SB(inode->i_sb); | |
882 | struct buffer_head *ibh; | |
883 | int err; | |
884 | ||
885 | err = nilfs_load_inode_block(sbi, inode, &ibh); | |
886 | if (unlikely(err)) { | |
887 | nilfs_warning(inode->i_sb, __func__, | |
888 | "failed to reget inode block.\n"); | |
889 | return err; | |
890 | } | |
05fe58fd | 891 | nilfs_update_inode(inode, ibh); |
05fe58fd | 892 | nilfs_mdt_mark_buffer_dirty(ibh); |
e912a5b6 | 893 | nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); |
05fe58fd RK |
894 | brelse(ibh); |
895 | return 0; | |
896 | } | |
897 | ||
898 | /** | |
899 | * nilfs_dirty_inode - reflect changes on given inode to an inode block. | |
900 | * @inode: inode of the file to be registered. | |
901 | * | |
902 | * nilfs_dirty_inode() loads a inode block containing the specified | |
903 | * @inode and copies data from a nilfs_inode to a corresponding inode | |
904 | * entry in the inode block. This operation is excluded from the segment | |
905 | * construction. This function can be called both as a single operation | |
906 | * and as a part of indivisible file operations. | |
907 | */ | |
908 | void nilfs_dirty_inode(struct inode *inode) | |
909 | { | |
910 | struct nilfs_transaction_info ti; | |
7d6cd92f | 911 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode); |
05fe58fd RK |
912 | |
913 | if (is_bad_inode(inode)) { | |
914 | nilfs_warning(inode->i_sb, __func__, | |
915 | "tried to mark bad_inode dirty. ignored.\n"); | |
916 | dump_stack(); | |
917 | return; | |
918 | } | |
7d6cd92f RK |
919 | if (mdi) { |
920 | nilfs_mdt_mark_dirty(inode); | |
921 | return; | |
922 | } | |
05fe58fd | 923 | nilfs_transaction_begin(inode->i_sb, &ti, 0); |
458c5b08 | 924 | nilfs_mark_inode_dirty(inode); |
47420c79 | 925 | nilfs_transaction_commit(inode->i_sb); /* never fails */ |
05fe58fd | 926 | } |