^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * sysfile.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Initialize, read, write, etc. system files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2002, 2004 Oracle. All rights reserved.
^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/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <cluster/masklog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ocfs2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "dir.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "journal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "sysfile.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "buffer_head_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct inode * _ocfs2_get_system_file_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u32 slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifdef CONFIG_DEBUG_LOCK_ALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static struct lock_class_key ocfs2_sysfile_cluster_lock_key[NUM_SYSTEM_INODES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static inline int is_global_system_inode(int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return type >= OCFS2_FIRST_ONLINE_SYSTEM_INODE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) type <= OCFS2_LAST_GLOBAL_SYSTEM_INODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static struct inode **get_local_system_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u32 slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct inode **local_system_inodes, **free = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) BUG_ON(slot == OCFS2_INVALID_SLOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) BUG_ON(type < OCFS2_FIRST_LOCAL_SYSTEM_INODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) type > OCFS2_LAST_LOCAL_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) local_system_inodes = osb->local_system_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (unlikely(!local_system_inodes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) local_system_inodes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) kzalloc(array3_size(sizeof(struct inode *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) NUM_LOCAL_SYSTEM_INODES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) osb->max_slots),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!local_system_inodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) mlog_errno(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * return NULL here so that ocfs2_get_sytem_file_inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * will try to create an inode and use it. We will try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * to initialize local_system_inodes next time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) spin_lock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (osb->local_system_inodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Someone has initialized it for us. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) free = local_system_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) local_system_inodes = osb->local_system_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) osb->local_system_inodes = local_system_inodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) spin_unlock(&osb->osb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree(free);
^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) index = (slot * NUM_LOCAL_SYSTEM_INODES) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) (type - OCFS2_FIRST_LOCAL_SYSTEM_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return &local_system_inodes[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct inode *ocfs2_get_system_file_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) u32 slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct inode **arr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* avoid the lookup if cached in local system file array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (is_global_system_inode(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) arr = &(osb->global_system_inodes[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) arr = get_local_system_inode(osb, type, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mutex_lock(&osb->system_file_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (arr && ((inode = *arr) != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* get a ref in addition to the array ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) inode = igrab(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) mutex_unlock(&osb->system_file_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) BUG_ON(!inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* this gets one ref thru iget */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) inode = _ocfs2_get_system_file_inode(osb, type, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* add one more if putting into array for first time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (arr && inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) *arr = igrab(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) BUG_ON(!*arr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) mutex_unlock(&osb->system_file_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct inode * _ocfs2_get_system_file_inode(struct ocfs2_super *osb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) u32 slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) char namebuf[40];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u64 blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ocfs2_sprintf_system_inode_name(namebuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) sizeof(namebuf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) type, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) status = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) strlen(namebuf), &blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) inode = ocfs2_iget(osb, blkno, OCFS2_FI_FLAG_SYSFILE, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mlog_errno(PTR_ERR(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #ifdef CONFIG_DEBUG_LOCK_ALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) type == LOCAL_GROUP_QUOTA_SYSTEM_INODE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) type == JOURNAL_SYSTEM_INODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* Ignore inode lock on these inodes as the lock does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * really belong to any process and lockdep cannot handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) OCFS2_I(inode)->ip_inode_lockres.l_lockdep_map.key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) lockdep_init_map(&OCFS2_I(inode)->ip_inode_lockres.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) l_lockdep_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ocfs2_system_inodes[type].si_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) &ocfs2_sysfile_cluster_lock_key[type], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)