Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ifile.c - NILFS inode file |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 16 | * Written by Amagai Yoshiji. |
| 17 | * Revised by Ryusuke Konishi. |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/buffer_head.h> |
| 23 | #include "nilfs.h" |
| 24 | #include "mdt.h" |
| 25 | #include "alloc.h" |
| 26 | #include "ifile.h" |
| 27 | |
Vyacheslav Dubeyko | f5974c8 | 2012-07-30 14:42:10 -0700 | [diff] [blame] | 28 | /** |
| 29 | * struct nilfs_ifile_info - on-memory private data of ifile |
| 30 | * @mi: on-memory private data of metadata file |
| 31 | * @palloc_cache: persistent object allocator cache of ifile |
| 32 | */ |
Ryusuke Konishi | 49fa7a5 | 2009-11-14 16:44:22 +0900 | [diff] [blame] | 33 | struct nilfs_ifile_info { |
| 34 | struct nilfs_mdt_info mi; |
| 35 | struct nilfs_palloc_cache palloc_cache; |
| 36 | }; |
| 37 | |
| 38 | static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile) |
| 39 | { |
| 40 | return (struct nilfs_ifile_info *)NILFS_MDT(ifile); |
| 41 | } |
| 42 | |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 43 | /** |
| 44 | * nilfs_ifile_create_inode - create a new disk inode |
| 45 | * @ifile: ifile inode |
| 46 | * @out_ino: pointer to a variable to store inode number |
| 47 | * @out_bh: buffer_head contains newly allocated disk inode |
| 48 | * |
| 49 | * Return Value: On success, 0 is returned and the newly allocated inode |
| 50 | * number is stored in the place pointed by @ino, and buffer_head pointer |
| 51 | * that contains newly allocated disk inode structure is stored in the |
| 52 | * place pointed by @out_bh |
| 53 | * On error, one of the following negative error codes is returned. |
| 54 | * |
| 55 | * %-EIO - I/O error. |
| 56 | * |
| 57 | * %-ENOMEM - Insufficient amount of memory available. |
| 58 | * |
| 59 | * %-ENOSPC - No inode left. |
| 60 | */ |
| 61 | int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino, |
| 62 | struct buffer_head **out_bh) |
| 63 | { |
| 64 | struct nilfs_palloc_req req; |
| 65 | int ret; |
| 66 | |
Ryusuke Konishi | 076a378 | 2016-05-23 16:23:48 -0700 | [diff] [blame] | 67 | req.pr_entry_nr = 0; /* |
| 68 | * 0 says find free inode from beginning |
| 69 | * of a group. dull code!! |
| 70 | */ |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 71 | req.pr_entry_bh = NULL; |
| 72 | |
| 73 | ret = nilfs_palloc_prepare_alloc_entry(ifile, &req); |
| 74 | if (!ret) { |
| 75 | ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1, |
| 76 | &req.pr_entry_bh); |
| 77 | if (ret < 0) |
| 78 | nilfs_palloc_abort_alloc_entry(ifile, &req); |
| 79 | } |
| 80 | if (ret < 0) { |
| 81 | brelse(req.pr_entry_bh); |
| 82 | return ret; |
| 83 | } |
| 84 | nilfs_palloc_commit_alloc_entry(ifile, &req); |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 85 | mark_buffer_dirty(req.pr_entry_bh); |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 86 | nilfs_mdt_mark_dirty(ifile); |
| 87 | *out_ino = (ino_t)req.pr_entry_nr; |
| 88 | *out_bh = req.pr_entry_bh; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * nilfs_ifile_delete_inode - delete a disk inode |
| 94 | * @ifile: ifile inode |
| 95 | * @ino: inode number |
| 96 | * |
| 97 | * Return Value: On success, 0 is returned. On error, one of the following |
| 98 | * negative error codes is returned. |
| 99 | * |
| 100 | * %-EIO - I/O error. |
| 101 | * |
| 102 | * %-ENOMEM - Insufficient amount of memory available. |
| 103 | * |
| 104 | * %-ENOENT - The inode number @ino have not been allocated. |
| 105 | */ |
| 106 | int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino) |
| 107 | { |
| 108 | struct nilfs_palloc_req req = { |
| 109 | .pr_entry_nr = ino, .pr_entry_bh = NULL |
| 110 | }; |
| 111 | struct nilfs_inode *raw_inode; |
| 112 | void *kaddr; |
| 113 | int ret; |
| 114 | |
| 115 | ret = nilfs_palloc_prepare_free_entry(ifile, &req); |
| 116 | if (!ret) { |
| 117 | ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0, |
| 118 | &req.pr_entry_bh); |
| 119 | if (ret < 0) |
| 120 | nilfs_palloc_abort_free_entry(ifile, &req); |
| 121 | } |
| 122 | if (ret < 0) { |
| 123 | brelse(req.pr_entry_bh); |
| 124 | return ret; |
| 125 | } |
| 126 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 127 | kaddr = kmap_atomic(req.pr_entry_bh->b_page); |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 128 | raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr, |
| 129 | req.pr_entry_bh, kaddr); |
| 130 | raw_inode->i_flags = 0; |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 131 | kunmap_atomic(kaddr); |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 132 | |
Ryusuke Konishi | 5fc7b14 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 133 | mark_buffer_dirty(req.pr_entry_bh); |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 134 | brelse(req.pr_entry_bh); |
| 135 | |
| 136 | nilfs_palloc_commit_free_entry(ifile, &req); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino, |
| 142 | struct buffer_head **out_bh) |
| 143 | { |
| 144 | struct super_block *sb = ifile->i_sb; |
| 145 | int err; |
| 146 | |
| 147 | if (unlikely(!NILFS_VALID_INODE(sb, ino))) { |
| 148 | nilfs_error(sb, __func__, "bad inode number: %lu", |
| 149 | (unsigned long) ino); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh); |
Ryusuke Konishi | e828949 | 2010-11-19 15:26:20 +0900 | [diff] [blame] | 154 | if (unlikely(err)) |
| 155 | nilfs_warning(sb, __func__, "unable to read inode: %lu", |
| 156 | (unsigned long) ino); |
Ryusuke Konishi | 43bfb45 | 2009-04-06 19:01:30 -0700 | [diff] [blame] | 157 | return err; |
| 158 | } |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 159 | |
| 160 | /** |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 161 | * nilfs_ifile_count_free_inodes - calculate free inodes count |
| 162 | * @ifile: ifile inode |
| 163 | * @nmaxinodes: current maximum of available inodes count [out] |
| 164 | * @nfreeinodes: free inodes count [out] |
| 165 | */ |
| 166 | int nilfs_ifile_count_free_inodes(struct inode *ifile, |
| 167 | u64 *nmaxinodes, u64 *nfreeinodes) |
| 168 | { |
| 169 | u64 nused; |
| 170 | int err; |
| 171 | |
| 172 | *nmaxinodes = 0; |
| 173 | *nfreeinodes = 0; |
| 174 | |
Vyacheslav Dubeyko | e5f7f84 | 2013-07-03 15:08:06 -0700 | [diff] [blame] | 175 | nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count); |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 176 | err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes); |
| 177 | if (likely(!err)) |
| 178 | *nfreeinodes = *nmaxinodes - nused; |
| 179 | return err; |
| 180 | } |
| 181 | |
| 182 | /** |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 183 | * nilfs_ifile_read - read or get ifile inode |
| 184 | * @sb: super block instance |
| 185 | * @root: root object |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 186 | * @inode_size: size of an inode |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 187 | * @raw_inode: on-disk ifile inode |
| 188 | * @inodep: buffer to store the inode |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 189 | */ |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 190 | int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root, |
| 191 | size_t inode_size, struct nilfs_inode *raw_inode, |
| 192 | struct inode **inodep) |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 193 | { |
| 194 | struct inode *ifile; |
| 195 | int err; |
| 196 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 197 | ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO); |
| 198 | if (unlikely(!ifile)) |
| 199 | return -ENOMEM; |
| 200 | if (!(ifile->i_state & I_NEW)) |
| 201 | goto out; |
| 202 | |
| 203 | err = nilfs_mdt_init(ifile, NILFS_MDT_GFP, |
| 204 | sizeof(struct nilfs_ifile_info)); |
| 205 | if (err) |
| 206 | goto failed; |
| 207 | |
| 208 | err = nilfs_palloc_init_blockgroup(ifile, inode_size); |
| 209 | if (err) |
| 210 | goto failed; |
| 211 | |
| 212 | nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache); |
| 213 | |
| 214 | err = nilfs_read_inode_common(ifile, raw_inode); |
| 215 | if (err) |
| 216 | goto failed; |
| 217 | |
| 218 | unlock_new_inode(ifile); |
| 219 | out: |
| 220 | *inodep = ifile; |
| 221 | return 0; |
| 222 | failed: |
| 223 | iget_failed(ifile); |
| 224 | return err; |
Ryusuke Konishi | 7973956 | 2009-11-12 23:56:43 +0900 | [diff] [blame] | 225 | } |