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 = NILFS_FIRST_INO(ifile->i_sb);
60 req.pr_entry_bh = NULL;
62 ret = nilfs_palloc_prepare_alloc_entry(ifile, &req, false);
64 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
67 nilfs_palloc_abort_alloc_entry(ifile, &req);
70 brelse(req.pr_entry_bh);
73 nilfs_palloc_commit_alloc_entry(ifile, &req);
74 mark_buffer_dirty(req.pr_entry_bh);
75 nilfs_mdt_mark_dirty(ifile);
76 *out_ino = (ino_t)req.pr_entry_nr;
77 *out_bh = req.pr_entry_bh;
82 * nilfs_ifile_delete_inode - delete a disk inode
86 * Return Value: On success, 0 is returned. On error, one of the following
87 * negative error codes is returned.
91 * %-ENOMEM - Insufficient amount of memory available.
93 * %-ENOENT - The inode number @ino have not been allocated.
95 int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
97 struct nilfs_palloc_req req = {
98 .pr_entry_nr = ino, .pr_entry_bh = NULL
100 struct nilfs_inode *raw_inode;
104 ret = nilfs_palloc_prepare_free_entry(ifile, &req);
106 ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
109 nilfs_palloc_abort_free_entry(ifile, &req);
112 brelse(req.pr_entry_bh);
116 offset = nilfs_palloc_entry_offset(ifile, req.pr_entry_nr,
118 raw_inode = kmap_local_folio(req.pr_entry_bh->b_folio, offset);
119 raw_inode->i_flags = 0;
120 kunmap_local(raw_inode);
122 mark_buffer_dirty(req.pr_entry_bh);
123 brelse(req.pr_entry_bh);
125 nilfs_palloc_commit_free_entry(ifile, &req);
130 int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
131 struct buffer_head **out_bh)
133 struct super_block *sb = ifile->i_sb;
136 if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
137 nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
141 err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
143 nilfs_warn(sb, "error %d reading inode: ino=%lu",
144 err, (unsigned long)ino);
149 * nilfs_ifile_count_free_inodes - calculate free inodes count
150 * @ifile: ifile inode
151 * @nmaxinodes: current maximum of available inodes count [out]
152 * @nfreeinodes: free inodes count [out]
154 int nilfs_ifile_count_free_inodes(struct inode *ifile,
155 u64 *nmaxinodes, u64 *nfreeinodes)
163 nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
164 err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
166 *nfreeinodes = *nmaxinodes - nused;
171 * nilfs_ifile_read - read or get ifile inode
172 * @sb: super block instance
174 * @cno: number of checkpoint entry to read
175 * @inode_size: size of an inode
177 * Return: 0 on success, or the following negative error code on failure.
178 * * %-EINVAL - Invalid checkpoint.
179 * * %-ENOMEM - Insufficient memory available.
180 * * %-EIO - I/O error (including metadata corruption).
182 int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
183 __u64 cno, size_t inode_size)
185 struct the_nilfs *nilfs;
189 ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
190 if (unlikely(!ifile))
192 if (!(ifile->i_state & I_NEW))
195 err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
196 sizeof(struct nilfs_ifile_info));
200 err = nilfs_palloc_init_blockgroup(ifile, inode_size);
204 nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
206 nilfs = sb->s_fs_info;
207 err = nilfs_cpfile_read_checkpoint(nilfs->ns_cpfile, cno, root, ifile);
211 unlock_new_inode(ifile);