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