Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * slot_map.c
0004  *
0005  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
0006  */
0007 
0008 #include <linux/types.h>
0009 #include <linux/slab.h>
0010 #include <linux/highmem.h>
0011 
0012 #include <cluster/masklog.h>
0013 
0014 #include "ocfs2.h"
0015 
0016 #include "dlmglue.h"
0017 #include "extent_map.h"
0018 #include "heartbeat.h"
0019 #include "inode.h"
0020 #include "slot_map.h"
0021 #include "super.h"
0022 #include "sysfile.h"
0023 #include "ocfs2_trace.h"
0024 
0025 #include "buffer_head_io.h"
0026 
0027 
0028 struct ocfs2_slot {
0029     int sl_valid;
0030     unsigned int sl_node_num;
0031 };
0032 
0033 struct ocfs2_slot_info {
0034     int si_extended;
0035     int si_slots_per_block;
0036     struct inode *si_inode;
0037     unsigned int si_blocks;
0038     struct buffer_head **si_bh;
0039     unsigned int si_num_slots;
0040     struct ocfs2_slot si_slots[];
0041 };
0042 
0043 
0044 static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
0045                     unsigned int node_num);
0046 
0047 static void ocfs2_invalidate_slot(struct ocfs2_slot_info *si,
0048                   int slot_num)
0049 {
0050     BUG_ON((slot_num < 0) || (slot_num >= si->si_num_slots));
0051     si->si_slots[slot_num].sl_valid = 0;
0052 }
0053 
0054 static void ocfs2_set_slot(struct ocfs2_slot_info *si,
0055                int slot_num, unsigned int node_num)
0056 {
0057     BUG_ON((slot_num < 0) || (slot_num >= si->si_num_slots));
0058 
0059     si->si_slots[slot_num].sl_valid = 1;
0060     si->si_slots[slot_num].sl_node_num = node_num;
0061 }
0062 
0063 /* This version is for the extended slot map */
0064 static void ocfs2_update_slot_info_extended(struct ocfs2_slot_info *si)
0065 {
0066     int b, i, slotno;
0067     struct ocfs2_slot_map_extended *se;
0068 
0069     slotno = 0;
0070     for (b = 0; b < si->si_blocks; b++) {
0071         se = (struct ocfs2_slot_map_extended *)si->si_bh[b]->b_data;
0072         for (i = 0;
0073              (i < si->si_slots_per_block) &&
0074              (slotno < si->si_num_slots);
0075              i++, slotno++) {
0076             if (se->se_slots[i].es_valid)
0077                 ocfs2_set_slot(si, slotno,
0078                            le32_to_cpu(se->se_slots[i].es_node_num));
0079             else
0080                 ocfs2_invalidate_slot(si, slotno);
0081         }
0082     }
0083 }
0084 
0085 /*
0086  * Post the slot information on disk into our slot_info struct.
0087  * Must be protected by osb_lock.
0088  */
0089 static void ocfs2_update_slot_info_old(struct ocfs2_slot_info *si)
0090 {
0091     int i;
0092     struct ocfs2_slot_map *sm;
0093 
0094     sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data;
0095 
0096     for (i = 0; i < si->si_num_slots; i++) {
0097         if (le16_to_cpu(sm->sm_slots[i]) == (u16)OCFS2_INVALID_SLOT)
0098             ocfs2_invalidate_slot(si, i);
0099         else
0100             ocfs2_set_slot(si, i, le16_to_cpu(sm->sm_slots[i]));
0101     }
0102 }
0103 
0104 static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
0105 {
0106     /*
0107      * The slot data will have been refreshed when ocfs2_super_lock
0108      * was taken.
0109      */
0110     if (si->si_extended)
0111         ocfs2_update_slot_info_extended(si);
0112     else
0113         ocfs2_update_slot_info_old(si);
0114 }
0115 
0116 int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
0117 {
0118     int ret;
0119     struct ocfs2_slot_info *si = osb->slot_info;
0120 
0121     if (si == NULL)
0122         return 0;
0123 
0124     BUG_ON(si->si_blocks == 0);
0125     BUG_ON(si->si_bh == NULL);
0126 
0127     trace_ocfs2_refresh_slot_info(si->si_blocks);
0128 
0129     /*
0130      * We pass -1 as blocknr because we expect all of si->si_bh to
0131      * be !NULL.  Thus, ocfs2_read_blocks() will ignore blocknr.  If
0132      * this is not true, the read of -1 (UINT64_MAX) will fail.
0133      */
0134     ret = ocfs2_read_blocks(INODE_CACHE(si->si_inode), -1, si->si_blocks,
0135                 si->si_bh, OCFS2_BH_IGNORE_CACHE, NULL);
0136     if (ret == 0) {
0137         spin_lock(&osb->osb_lock);
0138         ocfs2_update_slot_info(si);
0139         spin_unlock(&osb->osb_lock);
0140     }
0141 
0142     return ret;
0143 }
0144 
0145 /* post the our slot info stuff into it's destination bh and write it
0146  * out. */
0147 static void ocfs2_update_disk_slot_extended(struct ocfs2_slot_info *si,
0148                         int slot_num,
0149                         struct buffer_head **bh)
0150 {
0151     int blkind = slot_num / si->si_slots_per_block;
0152     int slotno = slot_num % si->si_slots_per_block;
0153     struct ocfs2_slot_map_extended *se;
0154 
0155     BUG_ON(blkind >= si->si_blocks);
0156 
0157     se = (struct ocfs2_slot_map_extended *)si->si_bh[blkind]->b_data;
0158     se->se_slots[slotno].es_valid = si->si_slots[slot_num].sl_valid;
0159     if (si->si_slots[slot_num].sl_valid)
0160         se->se_slots[slotno].es_node_num =
0161             cpu_to_le32(si->si_slots[slot_num].sl_node_num);
0162     *bh = si->si_bh[blkind];
0163 }
0164 
0165 static void ocfs2_update_disk_slot_old(struct ocfs2_slot_info *si,
0166                        int slot_num,
0167                        struct buffer_head **bh)
0168 {
0169     int i;
0170     struct ocfs2_slot_map *sm;
0171 
0172     sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data;
0173     for (i = 0; i < si->si_num_slots; i++) {
0174         if (si->si_slots[i].sl_valid)
0175             sm->sm_slots[i] =
0176                 cpu_to_le16(si->si_slots[i].sl_node_num);
0177         else
0178             sm->sm_slots[i] = cpu_to_le16(OCFS2_INVALID_SLOT);
0179     }
0180     *bh = si->si_bh[0];
0181 }
0182 
0183 static int ocfs2_update_disk_slot(struct ocfs2_super *osb,
0184                   struct ocfs2_slot_info *si,
0185                   int slot_num)
0186 {
0187     int status;
0188     struct buffer_head *bh;
0189 
0190     spin_lock(&osb->osb_lock);
0191     if (si->si_extended)
0192         ocfs2_update_disk_slot_extended(si, slot_num, &bh);
0193     else
0194         ocfs2_update_disk_slot_old(si, slot_num, &bh);
0195     spin_unlock(&osb->osb_lock);
0196 
0197     status = ocfs2_write_block(osb, bh, INODE_CACHE(si->si_inode));
0198     if (status < 0)
0199         mlog_errno(status);
0200 
0201     return status;
0202 }
0203 
0204 /*
0205  * Calculate how many bytes are needed by the slot map.  Returns
0206  * an error if the slot map file is too small.
0207  */
0208 static int ocfs2_slot_map_physical_size(struct ocfs2_super *osb,
0209                     struct inode *inode,
0210                     unsigned long long *bytes)
0211 {
0212     unsigned long long bytes_needed;
0213 
0214     if (ocfs2_uses_extended_slot_map(osb)) {
0215         bytes_needed = osb->max_slots *
0216             sizeof(struct ocfs2_extended_slot);
0217     } else {
0218         bytes_needed = osb->max_slots * sizeof(__le16);
0219     }
0220     if (bytes_needed > i_size_read(inode)) {
0221         mlog(ML_ERROR,
0222              "Slot map file is too small!  (size %llu, needed %llu)\n",
0223              i_size_read(inode), bytes_needed);
0224         return -ENOSPC;
0225     }
0226 
0227     *bytes = bytes_needed;
0228     return 0;
0229 }
0230 
0231 /* try to find global node in the slot info. Returns -ENOENT
0232  * if nothing is found. */
0233 static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
0234                     unsigned int node_num)
0235 {
0236     int i, ret = -ENOENT;
0237 
0238     for(i = 0; i < si->si_num_slots; i++) {
0239         if (si->si_slots[i].sl_valid &&
0240             (node_num == si->si_slots[i].sl_node_num)) {
0241             ret = i;
0242             break;
0243         }
0244     }
0245 
0246     return ret;
0247 }
0248 
0249 static int __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
0250                    int preferred)
0251 {
0252     int i, ret = -ENOSPC;
0253 
0254     if ((preferred >= 0) && (preferred < si->si_num_slots)) {
0255         if (!si->si_slots[preferred].sl_valid) {
0256             ret = preferred;
0257             goto out;
0258         }
0259     }
0260 
0261     for(i = 0; i < si->si_num_slots; i++) {
0262         if (!si->si_slots[i].sl_valid) {
0263             ret = i;
0264             break;
0265         }
0266     }
0267 out:
0268     return ret;
0269 }
0270 
0271 int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
0272 {
0273     int slot;
0274     struct ocfs2_slot_info *si = osb->slot_info;
0275 
0276     spin_lock(&osb->osb_lock);
0277     slot = __ocfs2_node_num_to_slot(si, node_num);
0278     spin_unlock(&osb->osb_lock);
0279 
0280     return slot;
0281 }
0282 
0283 int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
0284                   unsigned int *node_num)
0285 {
0286     struct ocfs2_slot_info *si = osb->slot_info;
0287 
0288     assert_spin_locked(&osb->osb_lock);
0289 
0290     BUG_ON(slot_num < 0);
0291     BUG_ON(slot_num >= osb->max_slots);
0292 
0293     if (!si->si_slots[slot_num].sl_valid)
0294         return -ENOENT;
0295 
0296     *node_num = si->si_slots[slot_num].sl_node_num;
0297     return 0;
0298 }
0299 
0300 static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
0301 {
0302     unsigned int i;
0303 
0304     if (si == NULL)
0305         return;
0306 
0307     iput(si->si_inode);
0308     if (si->si_bh) {
0309         for (i = 0; i < si->si_blocks; i++) {
0310             if (si->si_bh[i]) {
0311                 brelse(si->si_bh[i]);
0312                 si->si_bh[i] = NULL;
0313             }
0314         }
0315         kfree(si->si_bh);
0316     }
0317 
0318     kfree(si);
0319 }
0320 
0321 int ocfs2_clear_slot(struct ocfs2_super *osb, int slot_num)
0322 {
0323     struct ocfs2_slot_info *si = osb->slot_info;
0324 
0325     if (si == NULL)
0326         return 0;
0327 
0328     spin_lock(&osb->osb_lock);
0329     ocfs2_invalidate_slot(si, slot_num);
0330     spin_unlock(&osb->osb_lock);
0331 
0332     return ocfs2_update_disk_slot(osb, osb->slot_info, slot_num);
0333 }
0334 
0335 static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
0336                   struct ocfs2_slot_info *si)
0337 {
0338     int status = 0;
0339     u64 blkno;
0340     unsigned long long blocks, bytes = 0;
0341     unsigned int i;
0342     struct buffer_head *bh;
0343 
0344     status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes);
0345     if (status)
0346         goto bail;
0347 
0348     blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes);
0349     BUG_ON(blocks > UINT_MAX);
0350     si->si_blocks = blocks;
0351     if (!si->si_blocks)
0352         goto bail;
0353 
0354     if (si->si_extended)
0355         si->si_slots_per_block =
0356             (osb->sb->s_blocksize /
0357              sizeof(struct ocfs2_extended_slot));
0358     else
0359         si->si_slots_per_block = osb->sb->s_blocksize / sizeof(__le16);
0360 
0361     /* The size checks above should ensure this */
0362     BUG_ON((osb->max_slots / si->si_slots_per_block) > blocks);
0363 
0364     trace_ocfs2_map_slot_buffers(bytes, si->si_blocks);
0365 
0366     si->si_bh = kcalloc(si->si_blocks, sizeof(struct buffer_head *),
0367                 GFP_KERNEL);
0368     if (!si->si_bh) {
0369         status = -ENOMEM;
0370         mlog_errno(status);
0371         goto bail;
0372     }
0373 
0374     for (i = 0; i < si->si_blocks; i++) {
0375         status = ocfs2_extent_map_get_blocks(si->si_inode, i,
0376                              &blkno, NULL, NULL);
0377         if (status < 0) {
0378             mlog_errno(status);
0379             goto bail;
0380         }
0381 
0382         trace_ocfs2_map_slot_buffers_block((unsigned long long)blkno, i);
0383 
0384         bh = NULL;  /* Acquire a fresh bh */
0385         status = ocfs2_read_blocks(INODE_CACHE(si->si_inode), blkno,
0386                        1, &bh, OCFS2_BH_IGNORE_CACHE, NULL);
0387         if (status < 0) {
0388             mlog_errno(status);
0389             goto bail;
0390         }
0391 
0392         si->si_bh[i] = bh;
0393     }
0394 
0395 bail:
0396     return status;
0397 }
0398 
0399 int ocfs2_init_slot_info(struct ocfs2_super *osb)
0400 {
0401     int status;
0402     struct inode *inode = NULL;
0403     struct ocfs2_slot_info *si;
0404 
0405     si = kzalloc(struct_size(si, si_slots, osb->max_slots), GFP_KERNEL);
0406     if (!si) {
0407         status = -ENOMEM;
0408         mlog_errno(status);
0409         return status;
0410     }
0411 
0412     si->si_extended = ocfs2_uses_extended_slot_map(osb);
0413     si->si_num_slots = osb->max_slots;
0414 
0415     inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
0416                         OCFS2_INVALID_SLOT);
0417     if (!inode) {
0418         status = -EINVAL;
0419         mlog_errno(status);
0420         goto bail;
0421     }
0422 
0423     si->si_inode = inode;
0424     status = ocfs2_map_slot_buffers(osb, si);
0425     if (status < 0) {
0426         mlog_errno(status);
0427         goto bail;
0428     }
0429 
0430     osb->slot_info = (struct ocfs2_slot_info *)si;
0431 bail:
0432     if (status < 0)
0433         __ocfs2_free_slot_info(si);
0434 
0435     return status;
0436 }
0437 
0438 void ocfs2_free_slot_info(struct ocfs2_super *osb)
0439 {
0440     struct ocfs2_slot_info *si = osb->slot_info;
0441 
0442     osb->slot_info = NULL;
0443     __ocfs2_free_slot_info(si);
0444 }
0445 
0446 int ocfs2_find_slot(struct ocfs2_super *osb)
0447 {
0448     int status;
0449     int slot;
0450     struct ocfs2_slot_info *si;
0451 
0452     si = osb->slot_info;
0453 
0454     spin_lock(&osb->osb_lock);
0455     ocfs2_update_slot_info(si);
0456 
0457     /* search for ourselves first and take the slot if it already
0458      * exists. Perhaps we need to mark this in a variable for our
0459      * own journal recovery? Possibly not, though we certainly
0460      * need to warn to the user */
0461     slot = __ocfs2_node_num_to_slot(si, osb->node_num);
0462     if (slot < 0) {
0463         /* if no slot yet, then just take 1st available
0464          * one. */
0465         slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
0466         if (slot < 0) {
0467             spin_unlock(&osb->osb_lock);
0468             mlog(ML_ERROR, "no free slots available!\n");
0469             status = -EINVAL;
0470             goto bail;
0471         }
0472     } else
0473         printk(KERN_INFO "ocfs2: Slot %d on device (%s) was already "
0474                "allocated to this node!\n", slot, osb->dev_str);
0475 
0476     ocfs2_set_slot(si, slot, osb->node_num);
0477     osb->slot_num = slot;
0478     spin_unlock(&osb->osb_lock);
0479 
0480     trace_ocfs2_find_slot(osb->slot_num);
0481 
0482     status = ocfs2_update_disk_slot(osb, si, osb->slot_num);
0483     if (status < 0) {
0484         mlog_errno(status);
0485         /*
0486          * if write block failed, invalidate slot to avoid overwrite
0487          * slot during dismount in case another node rightly has mounted
0488          */
0489         spin_lock(&osb->osb_lock);
0490         ocfs2_invalidate_slot(si, osb->slot_num);
0491         osb->slot_num = OCFS2_INVALID_SLOT;
0492         spin_unlock(&osb->osb_lock);
0493     }
0494 
0495 bail:
0496     return status;
0497 }
0498 
0499 void ocfs2_put_slot(struct ocfs2_super *osb)
0500 {
0501     int status, slot_num;
0502     struct ocfs2_slot_info *si = osb->slot_info;
0503 
0504     if (!si)
0505         return;
0506 
0507     spin_lock(&osb->osb_lock);
0508     ocfs2_update_slot_info(si);
0509 
0510     slot_num = osb->slot_num;
0511     ocfs2_invalidate_slot(si, osb->slot_num);
0512     osb->slot_num = OCFS2_INVALID_SLOT;
0513     spin_unlock(&osb->osb_lock);
0514 
0515     status = ocfs2_update_disk_slot(osb, si, slot_num);
0516     if (status < 0)
0517         mlog_errno(status);
0518 
0519     ocfs2_free_slot_info(osb);
0520 }