1 // SPDX-License-Identifier: GPL-2.0+
3 * ifile.c - NILFS inode file
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>
20 * struct nilfs_ifile_info - on-memory private data of ifile
21 * @mi: on-memory private data of metadata file
22 * @palloc_cache: persistent object allocator cache of ifile
24 struct nilfs_ifile_info {
25 struct nilfs_mdt_info mi;
26 struct nilfs_palloc_cache palloc_cache;
29 static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
31 return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
35 * nilfs_ifile_create_inode - create a new disk inode
37 * @out_ino: pointer to a variable to store inode number
38 * @out_bh: buffer_head contains newly allocated disk inode
40 * Return Value: On success, 0 is returned and the newly allocated inode
41 * number is stored in the place pointed by @ino, and buffer_head pointer
42 * that contains newly allocated disk inode structure is stored in the
43 * place pointed by @out_bh
44 * On error, one of the following negative error codes is returned.
48 * %-ENOMEM - Insufficient amount of memory available.
50 * %-ENOSPC - No inode left.
52 int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
53 struct buffer_head **out_bh)
55 struct nilfs_palloc_req req;
58 req.pr_entry_nr = 0; /*
59 * 0 says find free inode from beginning
60 * of a group. dull code!!
62 req.pr_entry_bh = NULL;
64 ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
66 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
69 nilfs_palloc_abort_alloc_entry(ifile, &req);
72 brelse(req.pr_entry_bh);
75 nilfs_palloc_commit_alloc_entry(ifile, &req);
76 mark_buffer_dirty(req.pr_entry_bh);
77 nilfs_mdt_mark_dirty(ifile);
78 *out_ino = (ino_t)req.pr_entry_nr;
79 *out_bh = req.pr_entry_bh;
84 * nilfs_ifile_delete_inode - delete a disk inode
88 * Return Value: On success, 0 is returned. On error, one of the following
89 * negative error codes is returned.
93 * %-ENOMEM - Insufficient amount of memory available.
95 * %-ENOENT - The inode number @ino have not been allocated.
97 int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
99 struct nilfs_palloc_req req = {
100 .pr_entry_nr = ino, .pr_entry_bh = NULL
102 struct nilfs_inode *raw_inode;
106 ret = nilfs_palloc_prepare_free_entry(ifile, &req);
108 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
111 nilfs_palloc_abort_free_entry(ifile, &req);
114 brelse(req.pr_entry_bh);
118 kaddr = kmap_atomic(req.pr_entry_bh->b_page);
119 raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
120 req.pr_entry_bh, kaddr);
121 raw_inode->i_flags = 0;
122 kunmap_atomic(kaddr);
124 mark_buffer_dirty(req.pr_entry_bh);
125 brelse(req.pr_entry_bh);
127 nilfs_palloc_commit_free_entry(ifile, &req);
132 int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
133 struct buffer_head **out_bh)
135 struct super_block *sb = ifile->i_sb;
138 if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
139 nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
143 err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
145 nilfs_warn(sb, "error %d reading inode: ino=%lu",
146 err, (unsigned long)ino);
151 * nilfs_ifile_count_free_inodes - calculate free inodes count
152 * @ifile: ifile inode
153 * @nmaxinodes: current maximum of available inodes count [out]
154 * @nfreeinodes: free inodes count [out]
156 int nilfs_ifile_count_free_inodes(struct inode *ifile,
157 u64 *nmaxinodes, u64 *nfreeinodes)
165 nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
166 err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
168 *nfreeinodes = *nmaxinodes - nused;
173 * nilfs_ifile_read - read or get ifile inode
174 * @sb: super block instance
176 * @inode_size: size of an inode
177 * @raw_inode: on-disk ifile inode
178 * @inodep: buffer to store the inode
180 int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
181 size_t inode_size, struct nilfs_inode *raw_inode,
182 struct inode **inodep)
187 ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
188 if (unlikely(!ifile))
190 if (!(ifile->i_state & I_NEW))
193 err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
194 sizeof(struct nilfs_ifile_info));
198 err = nilfs_palloc_init_blockgroup(ifile, inode_size);
202 nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
204 err = nilfs_read_inode_common(ifile, raw_inode);
208 unlock_new_inode(ifile);