File Index
Files and directories
Sign in
FILE

ifile.c

File preview with syntax highlighting.

linux-6.19.14/fs/nilfs2/ifile.c C
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * NILFS inode file
  4 *
  5 * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  6 *
  7 * Written by Amagai Yoshiji.
  8 * Revised by Ryusuke Konishi.
  9 *
 10 */
 11
 12#include <linux/types.h>
 13#include <linux/buffer_head.h>
 14#include "nilfs.h"
 15#include "mdt.h"
 16#include "alloc.h"
 17#include "ifile.h"
 18#include "cpfile.h"
 19
 20/**
 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
 24 */
 25struct nilfs_ifile_info {
 26	struct nilfs_mdt_info mi;
 27	struct nilfs_palloc_cache palloc_cache;
 28};
 29
 30static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
 31{
 32	return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
 33}
 34
 35/**
 36 * nilfs_ifile_create_inode - create a new disk inode
 37 * @ifile: ifile inode
 38 * @out_ino: pointer to a variable to store inode number
 39 * @out_bh: buffer_head contains newly allocated disk inode
 40 *
 41 * nilfs_ifile_create_inode() allocates a new inode in the ifile metadata
 42 * file and stores the inode number in the variable pointed to by @out_ino,
 43 * as well as storing the ifile's buffer with the disk inode in the location
 44 * pointed to by @out_bh.
 45 *
 46 * Return: 0 on success, or one of the following negative error codes on
 47 * failure:
 48 * * %-EIO	- I/O error (including metadata corruption).
 49 * * %-ENOMEM	- Insufficient memory available.
 50 * * %-ENOSPC	- No inode left.
 51 */
 52int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
 53			     struct buffer_head **out_bh)
 54{
 55	struct nilfs_palloc_req req;
 56	int ret;
 57
 58	req.pr_entry_nr = NILFS_FIRST_INO(ifile->i_sb);
 59	req.pr_entry_bh = NULL;
 60
 61	ret = nilfs_palloc_prepare_alloc_entry(ifile, &req, false);
 62	if (!ret) {
 63		ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
 64						   &req.pr_entry_bh);
 65		if (ret < 0)
 66			nilfs_palloc_abort_alloc_entry(ifile, &req);
 67	}
 68	if (ret < 0) {
 69		brelse(req.pr_entry_bh);
 70		return ret;
 71	}
 72	nilfs_palloc_commit_alloc_entry(ifile, &req);
 73	mark_buffer_dirty(req.pr_entry_bh);
 74	nilfs_mdt_mark_dirty(ifile);
 75	*out_ino = (ino_t)req.pr_entry_nr;
 76	*out_bh = req.pr_entry_bh;
 77	return 0;
 78}
 79
 80/**
 81 * nilfs_ifile_delete_inode - delete a disk inode
 82 * @ifile: ifile inode
 83 * @ino: inode number
 84 *
 85 * Return: 0 on success, or one of the following negative error codes on
 86 * failure:
 87 * * %-EIO	- I/O error (including metadata corruption).
 88 * * %-ENOENT	- Inode number unallocated.
 89 * * %-ENOMEM	- Insufficient memory available.
 90 */
 91int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
 92{
 93	struct nilfs_palloc_req req = {
 94		.pr_entry_nr = ino, .pr_entry_bh = NULL
 95	};
 96	struct nilfs_inode *raw_inode;
 97	size_t offset;
 98	int ret;
 99
100	ret = nilfs_palloc_prepare_free_entry(ifile, &req);
101	if (!ret) {
102		ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
103						   &req.pr_entry_bh);
104		if (ret < 0)
105			nilfs_palloc_abort_free_entry(ifile, &req);
106	}
107	if (ret < 0) {
108		brelse(req.pr_entry_bh);
109		return ret;
110	}
111
112	offset = nilfs_palloc_entry_offset(ifile, req.pr_entry_nr,
113					   req.pr_entry_bh);
114	raw_inode = kmap_local_folio(req.pr_entry_bh->b_folio, offset);
115	raw_inode->i_flags = 0;
116	kunmap_local(raw_inode);
117
118	mark_buffer_dirty(req.pr_entry_bh);
119	brelse(req.pr_entry_bh);
120
121	nilfs_palloc_commit_free_entry(ifile, &req);
122
123	return 0;
124}
125
126int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
127				struct buffer_head **out_bh)
128{
129	struct super_block *sb = ifile->i_sb;
130	int err;
131
132	if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
133		nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
134		return -EINVAL;
135	}
136
137	err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
138	if (unlikely(err))
139		nilfs_warn(sb, "error %d reading inode: ino=%lu",
140			   err, (unsigned long)ino);
141	return err;
142}
143
144/**
145 * nilfs_ifile_count_free_inodes - calculate free inodes count
146 * @ifile: ifile inode
147 * @nmaxinodes: current maximum of available inodes count [out]
148 * @nfreeinodes: free inodes count [out]
149 *
150 * Return: 0 on success, or a negative error code on failure.
151 */
152int nilfs_ifile_count_free_inodes(struct inode *ifile,
153				    u64 *nmaxinodes, u64 *nfreeinodes)
154{
155	u64 nused;
156	int err;
157
158	*nmaxinodes = 0;
159	*nfreeinodes = 0;
160
161	nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
162	err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
163	if (likely(!err))
164		*nfreeinodes = *nmaxinodes - nused;
165	return err;
166}
167
168/**
169 * nilfs_ifile_read - read or get ifile inode
170 * @sb: super block instance
171 * @root: root object
172 * @cno: number of checkpoint entry to read
173 * @inode_size: size of an inode
174 *
175 * Return: 0 on success, or one of the following negative error codes on
176 * failure:
177 * * %-EINVAL	- Invalid checkpoint.
178 * * %-ENOMEM	- Insufficient memory available.
179 * * %-EIO	- I/O error (including metadata corruption).
180 */
181int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
182		     __u64 cno, size_t inode_size)
183{
184	struct the_nilfs *nilfs;
185	struct inode *ifile;
186	int err;
187
188	ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
189	if (unlikely(!ifile))
190		return -ENOMEM;
191	if (!(inode_state_read_once(ifile) & I_NEW))
192		goto out;
193
194	err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
195			     sizeof(struct nilfs_ifile_info));
196	if (err)
197		goto failed;
198
199	err = nilfs_palloc_init_blockgroup(ifile, inode_size);
200	if (err)
201		goto failed;
202
203	nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
204
205	nilfs = sb->s_fs_info;
206	err = nilfs_cpfile_read_checkpoint(nilfs->ns_cpfile, cno, root, ifile);
207	if (err)
208		goto failed;
209
210	unlock_new_inode(ifile);
211 out:
212	return 0;
213 failed:
214	iget_failed(ifile);
215	return err;
216}
~/ Read-only file preview.