sysfile.c 4.0 KB

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