1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Amagai Yoshiji.
8 * Revised by Ryusuke Konishi.
12 #include <linux/types.h>
13 #include <linux/buffer_head.h>
21 * struct nilfs_ifile_info - on-memory private data of ifile
22 * @mi: on-memory private data of metadata file
23 * @palloc_cache: persistent object allocator cache of ifile
25 struct nilfs_ifile_info {
26 struct nilfs_mdt_info mi;
27 struct nilfs_palloc_cache palloc_cache;
30 static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
32 return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
36 * nilfs_ifile_create_inode - create a new disk inode
38 * @out_ino: pointer to a variable to store inode number
39 * @out_bh: buffer_head contains newly allocated disk inode
41 * Return Value: On success, 0 is returned and the newly allocated inode
42 * number is stored in the place pointed by @ino, and buffer_head pointer
43 * that contains newly allocated disk inode structure is stored in the
44 * place pointed by @out_bh
45 * On error, one of the following negative error codes is returned.
49 * %-ENOMEM - Insufficient amount of memory available.
51 * %-ENOSPC - No inode left.
53 int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
54 struct buffer_head **out_bh)
56 struct nilfs_palloc_req req;
59 req.pr_entry_nr = 0; /*
60 * 0 says find free inode from beginning
61 * of a group. dull code!!
63 req.pr_entry_bh = NULL;
65 ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
67 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
70 nilfs_palloc_abort_alloc_entry(ifile, &req);
73 brelse(req.pr_entry_bh);
76 nilfs_palloc_commit_alloc_entry(ifile, &req);
77 mark_buffer_dirty(req.pr_entry_bh);
78 nilfs_mdt_mark_dirty(ifile);
79 *out_ino = (ino_t)req.pr_entry_nr;
80 *out_bh = req.pr_entry_bh;
85 * nilfs_ifile_delete_inode - delete a disk inode
89 * Return Value: On success, 0 is returned. On error, one of the following
90 * negative error codes is returned.
94 * %-ENOMEM - Insufficient amount of memory available.
96 * %-ENOENT - The inode number @ino have not been allocated.
98 int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
100 struct nilfs_palloc_req req = {
101 .pr_entry_nr = ino, .pr_entry_bh = NULL
103 struct nilfs_inode *raw_inode;
107 ret = nilfs_palloc_prepare_free_entry(ifile, &req);
109 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
112 nilfs_palloc_abort_free_entry(ifile, &req);
115 brelse(req.pr_entry_bh);
119 kaddr = kmap_local_page(req.pr_entry_bh->b_page);
120 raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
121 req.pr_entry_bh, kaddr);
122 raw_inode->i_flags = 0;
125 mark_buffer_dirty(req.pr_entry_bh);
126 brelse(req.pr_entry_bh);
128 nilfs_palloc_commit_free_entry(ifile, &req);
133 int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
134 struct buffer_head **out_bh)
136 struct super_block *sb = ifile->i_sb;
139 if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
140 nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
144 err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
146 nilfs_warn(sb, "error %d reading inode: ino=%lu",
147 err, (unsigned long)ino);
152 * nilfs_ifile_count_free_inodes - calculate free inodes count
153 * @ifile: ifile inode
154 * @nmaxinodes: current maximum of available inodes count [out]
155 * @nfreeinodes: free inodes count [out]
157 int nilfs_ifile_count_free_inodes(struct inode *ifile,
158 u64 *nmaxinodes, u64 *nfreeinodes)
166 nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
167 err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
169 *nfreeinodes = *nmaxinodes - nused;
174 * nilfs_ifile_read - read or get ifile inode
175 * @sb: super block instance
177 * @cno: number of checkpoint entry to read
178 * @inode_size: size of an inode
180 * Return: 0 on success, or the following negative error code on failure.
181 * * %-EINVAL - Invalid checkpoint.
182 * * %-ENOMEM - Insufficient memory available.
183 * * %-EIO - I/O error (including metadata corruption).
185 int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
186 __u64 cno, size_t inode_size)
188 struct the_nilfs *nilfs;
192 ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
193 if (unlikely(!ifile))
195 if (!(ifile->i_state & I_NEW))
198 err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
199 sizeof(struct nilfs_ifile_info));
203 err = nilfs_palloc_init_blockgroup(ifile, inode_size);
207 nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
209 nilfs = sb->s_fs_info;
210 err = nilfs_cpfile_read_checkpoint(nilfs->ns_cpfile, cno, root, ifile);
214 unlock_new_inode(ifile);