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