Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * extent_map.c
0004  *
0005  * Block/Cluster mapping functions
0006  *
0007  * Copyright (C) 2004 Oracle.  All rights reserved.
0008  */
0009 
0010 #include <linux/fs.h>
0011 #include <linux/init.h>
0012 #include <linux/slab.h>
0013 #include <linux/types.h>
0014 #include <linux/fiemap.h>
0015 
0016 #include <cluster/masklog.h>
0017 
0018 #include "ocfs2.h"
0019 
0020 #include "alloc.h"
0021 #include "dlmglue.h"
0022 #include "extent_map.h"
0023 #include "inode.h"
0024 #include "super.h"
0025 #include "symlink.h"
0026 #include "aops.h"
0027 #include "ocfs2_trace.h"
0028 
0029 #include "buffer_head_io.h"
0030 
0031 /*
0032  * The extent caching implementation is intentionally trivial.
0033  *
0034  * We only cache a small number of extents stored directly on the
0035  * inode, so linear order operations are acceptable. If we ever want
0036  * to increase the size of the extent map, then these algorithms must
0037  * get smarter.
0038  */
0039 
0040 void ocfs2_extent_map_init(struct inode *inode)
0041 {
0042     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0043 
0044     oi->ip_extent_map.em_num_items = 0;
0045     INIT_LIST_HEAD(&oi->ip_extent_map.em_list);
0046 }
0047 
0048 static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
0049                       unsigned int cpos,
0050                       struct ocfs2_extent_map_item **ret_emi)
0051 {
0052     unsigned int range;
0053     struct ocfs2_extent_map_item *emi;
0054 
0055     *ret_emi = NULL;
0056 
0057     list_for_each_entry(emi, &em->em_list, ei_list) {
0058         range = emi->ei_cpos + emi->ei_clusters;
0059 
0060         if (cpos >= emi->ei_cpos && cpos < range) {
0061             list_move(&emi->ei_list, &em->em_list);
0062 
0063             *ret_emi = emi;
0064             break;
0065         }
0066     }
0067 }
0068 
0069 static int ocfs2_extent_map_lookup(struct inode *inode, unsigned int cpos,
0070                    unsigned int *phys, unsigned int *len,
0071                    unsigned int *flags)
0072 {
0073     unsigned int coff;
0074     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0075     struct ocfs2_extent_map_item *emi;
0076 
0077     spin_lock(&oi->ip_lock);
0078 
0079     __ocfs2_extent_map_lookup(&oi->ip_extent_map, cpos, &emi);
0080     if (emi) {
0081         coff = cpos - emi->ei_cpos;
0082         *phys = emi->ei_phys + coff;
0083         if (len)
0084             *len = emi->ei_clusters - coff;
0085         if (flags)
0086             *flags = emi->ei_flags;
0087     }
0088 
0089     spin_unlock(&oi->ip_lock);
0090 
0091     if (emi == NULL)
0092         return -ENOENT;
0093 
0094     return 0;
0095 }
0096 
0097 /*
0098  * Forget about all clusters equal to or greater than cpos.
0099  */
0100 void ocfs2_extent_map_trunc(struct inode *inode, unsigned int cpos)
0101 {
0102     struct ocfs2_extent_map_item *emi, *n;
0103     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0104     struct ocfs2_extent_map *em = &oi->ip_extent_map;
0105     LIST_HEAD(tmp_list);
0106     unsigned int range;
0107 
0108     spin_lock(&oi->ip_lock);
0109     list_for_each_entry_safe(emi, n, &em->em_list, ei_list) {
0110         if (emi->ei_cpos >= cpos) {
0111             /* Full truncate of this record. */
0112             list_move(&emi->ei_list, &tmp_list);
0113             BUG_ON(em->em_num_items == 0);
0114             em->em_num_items--;
0115             continue;
0116         }
0117 
0118         range = emi->ei_cpos + emi->ei_clusters;
0119         if (range > cpos) {
0120             /* Partial truncate */
0121             emi->ei_clusters = cpos - emi->ei_cpos;
0122         }
0123     }
0124     spin_unlock(&oi->ip_lock);
0125 
0126     list_for_each_entry_safe(emi, n, &tmp_list, ei_list) {
0127         list_del(&emi->ei_list);
0128         kfree(emi);
0129     }
0130 }
0131 
0132 /*
0133  * Is any part of emi2 contained within emi1
0134  */
0135 static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
0136                  struct ocfs2_extent_map_item *emi2)
0137 {
0138     unsigned int range1, range2;
0139 
0140     /*
0141      * Check if logical start of emi2 is inside emi1
0142      */
0143     range1 = emi1->ei_cpos + emi1->ei_clusters;
0144     if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
0145         return 1;
0146 
0147     /*
0148      * Check if logical end of emi2 is inside emi1
0149      */
0150     range2 = emi2->ei_cpos + emi2->ei_clusters;
0151     if (range2 > emi1->ei_cpos && range2 <= range1)
0152         return 1;
0153 
0154     return 0;
0155 }
0156 
0157 static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
0158                   struct ocfs2_extent_map_item *src)
0159 {
0160     dest->ei_cpos = src->ei_cpos;
0161     dest->ei_phys = src->ei_phys;
0162     dest->ei_clusters = src->ei_clusters;
0163     dest->ei_flags = src->ei_flags;
0164 }
0165 
0166 /*
0167  * Try to merge emi with ins. Returns 1 if merge succeeds, zero
0168  * otherwise.
0169  */
0170 static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
0171                      struct ocfs2_extent_map_item *ins)
0172 {
0173     /*
0174      * Handle contiguousness
0175      */
0176     if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
0177         ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
0178         ins->ei_flags == emi->ei_flags) {
0179         emi->ei_clusters += ins->ei_clusters;
0180         return 1;
0181     } else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
0182            (ins->ei_cpos + ins->ei_clusters) == emi->ei_cpos &&
0183            ins->ei_flags == emi->ei_flags) {
0184         emi->ei_phys = ins->ei_phys;
0185         emi->ei_cpos = ins->ei_cpos;
0186         emi->ei_clusters += ins->ei_clusters;
0187         return 1;
0188     }
0189 
0190     /*
0191      * Overlapping extents - this shouldn't happen unless we've
0192      * split an extent to change it's flags. That is exceedingly
0193      * rare, so there's no sense in trying to optimize it yet.
0194      */
0195     if (ocfs2_ei_is_contained(emi, ins) ||
0196         ocfs2_ei_is_contained(ins, emi)) {
0197         ocfs2_copy_emi_fields(emi, ins);
0198         return 1;
0199     }
0200 
0201     /* No merge was possible. */
0202     return 0;
0203 }
0204 
0205 /*
0206  * In order to reduce complexity on the caller, this insert function
0207  * is intentionally liberal in what it will accept.
0208  *
0209  * The only rule is that the truncate call *must* be used whenever
0210  * records have been deleted. This avoids inserting overlapping
0211  * records with different physical mappings.
0212  */
0213 void ocfs2_extent_map_insert_rec(struct inode *inode,
0214                  struct ocfs2_extent_rec *rec)
0215 {
0216     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0217     struct ocfs2_extent_map *em = &oi->ip_extent_map;
0218     struct ocfs2_extent_map_item *emi, *new_emi = NULL;
0219     struct ocfs2_extent_map_item ins;
0220 
0221     ins.ei_cpos = le32_to_cpu(rec->e_cpos);
0222     ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
0223                            le64_to_cpu(rec->e_blkno));
0224     ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
0225     ins.ei_flags = rec->e_flags;
0226 
0227 search:
0228     spin_lock(&oi->ip_lock);
0229 
0230     list_for_each_entry(emi, &em->em_list, ei_list) {
0231         if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
0232             list_move(&emi->ei_list, &em->em_list);
0233             spin_unlock(&oi->ip_lock);
0234             goto out;
0235         }
0236     }
0237 
0238     /*
0239      * No item could be merged.
0240      *
0241      * Either allocate and add a new item, or overwrite the last recently
0242      * inserted.
0243      */
0244 
0245     if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
0246         if (new_emi == NULL) {
0247             spin_unlock(&oi->ip_lock);
0248 
0249             new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
0250             if (new_emi == NULL)
0251                 goto out;
0252 
0253             goto search;
0254         }
0255 
0256         ocfs2_copy_emi_fields(new_emi, &ins);
0257         list_add(&new_emi->ei_list, &em->em_list);
0258         em->em_num_items++;
0259         new_emi = NULL;
0260     } else {
0261         BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
0262         emi = list_entry(em->em_list.prev,
0263                  struct ocfs2_extent_map_item, ei_list);
0264         list_move(&emi->ei_list, &em->em_list);
0265         ocfs2_copy_emi_fields(emi, &ins);
0266     }
0267 
0268     spin_unlock(&oi->ip_lock);
0269 
0270 out:
0271     kfree(new_emi);
0272 }
0273 
0274 static int ocfs2_last_eb_is_empty(struct inode *inode,
0275                   struct ocfs2_dinode *di)
0276 {
0277     int ret, next_free;
0278     u64 last_eb_blk = le64_to_cpu(di->i_last_eb_blk);
0279     struct buffer_head *eb_bh = NULL;
0280     struct ocfs2_extent_block *eb;
0281     struct ocfs2_extent_list *el;
0282 
0283     ret = ocfs2_read_extent_block(INODE_CACHE(inode), last_eb_blk, &eb_bh);
0284     if (ret) {
0285         mlog_errno(ret);
0286         goto out;
0287     }
0288 
0289     eb = (struct ocfs2_extent_block *) eb_bh->b_data;
0290     el = &eb->h_list;
0291 
0292     if (el->l_tree_depth) {
0293         ocfs2_error(inode->i_sb,
0294                 "Inode %lu has non zero tree depth in leaf block %llu\n",
0295                 inode->i_ino,
0296                 (unsigned long long)eb_bh->b_blocknr);
0297         ret = -EROFS;
0298         goto out;
0299     }
0300 
0301     next_free = le16_to_cpu(el->l_next_free_rec);
0302 
0303     if (next_free == 0 ||
0304         (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0])))
0305         ret = 1;
0306 
0307 out:
0308     brelse(eb_bh);
0309     return ret;
0310 }
0311 
0312 /*
0313  * Return the 1st index within el which contains an extent start
0314  * larger than v_cluster.
0315  */
0316 static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
0317                        u32 v_cluster)
0318 {
0319     int i;
0320     struct ocfs2_extent_rec *rec;
0321 
0322     for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
0323         rec = &el->l_recs[i];
0324 
0325         if (v_cluster < le32_to_cpu(rec->e_cpos))
0326             break;
0327     }
0328 
0329     return i;
0330 }
0331 
0332 /*
0333  * Figure out the size of a hole which starts at v_cluster within the given
0334  * extent list.
0335  *
0336  * If there is no more allocation past v_cluster, we return the maximum
0337  * cluster size minus v_cluster.
0338  *
0339  * If we have in-inode extents, then el points to the dinode list and
0340  * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
0341  * containing el.
0342  */
0343 int ocfs2_figure_hole_clusters(struct ocfs2_caching_info *ci,
0344                    struct ocfs2_extent_list *el,
0345                    struct buffer_head *eb_bh,
0346                    u32 v_cluster,
0347                    u32 *num_clusters)
0348 {
0349     int ret, i;
0350     struct buffer_head *next_eb_bh = NULL;
0351     struct ocfs2_extent_block *eb, *next_eb;
0352 
0353     i = ocfs2_search_for_hole_index(el, v_cluster);
0354 
0355     if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
0356         eb = (struct ocfs2_extent_block *)eb_bh->b_data;
0357 
0358         /*
0359          * Check the next leaf for any extents.
0360          */
0361 
0362         if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
0363             goto no_more_extents;
0364 
0365         ret = ocfs2_read_extent_block(ci,
0366                           le64_to_cpu(eb->h_next_leaf_blk),
0367                           &next_eb_bh);
0368         if (ret) {
0369             mlog_errno(ret);
0370             goto out;
0371         }
0372 
0373         next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
0374         el = &next_eb->h_list;
0375         i = ocfs2_search_for_hole_index(el, v_cluster);
0376     }
0377 
0378 no_more_extents:
0379     if (i == le16_to_cpu(el->l_next_free_rec)) {
0380         /*
0381          * We're at the end of our existing allocation. Just
0382          * return the maximum number of clusters we could
0383          * possibly allocate.
0384          */
0385         *num_clusters = UINT_MAX - v_cluster;
0386     } else {
0387         *num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
0388     }
0389 
0390     ret = 0;
0391 out:
0392     brelse(next_eb_bh);
0393     return ret;
0394 }
0395 
0396 static int ocfs2_get_clusters_nocache(struct inode *inode,
0397                       struct buffer_head *di_bh,
0398                       u32 v_cluster, unsigned int *hole_len,
0399                       struct ocfs2_extent_rec *ret_rec,
0400                       unsigned int *is_last)
0401 {
0402     int i, ret, tree_height, len;
0403     struct ocfs2_dinode *di;
0404     struct ocfs2_extent_block *eb;
0405     struct ocfs2_extent_list *el;
0406     struct ocfs2_extent_rec *rec;
0407     struct buffer_head *eb_bh = NULL;
0408 
0409     memset(ret_rec, 0, sizeof(*ret_rec));
0410     if (is_last)
0411         *is_last = 0;
0412 
0413     di = (struct ocfs2_dinode *) di_bh->b_data;
0414     el = &di->id2.i_list;
0415     tree_height = le16_to_cpu(el->l_tree_depth);
0416 
0417     if (tree_height > 0) {
0418         ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
0419                       &eb_bh);
0420         if (ret) {
0421             mlog_errno(ret);
0422             goto out;
0423         }
0424 
0425         eb = (struct ocfs2_extent_block *) eb_bh->b_data;
0426         el = &eb->h_list;
0427 
0428         if (el->l_tree_depth) {
0429             ocfs2_error(inode->i_sb,
0430                     "Inode %lu has non zero tree depth in leaf block %llu\n",
0431                     inode->i_ino,
0432                     (unsigned long long)eb_bh->b_blocknr);
0433             ret = -EROFS;
0434             goto out;
0435         }
0436     }
0437 
0438     i = ocfs2_search_extent_list(el, v_cluster);
0439     if (i == -1) {
0440         /*
0441          * Holes can be larger than the maximum size of an
0442          * extent, so we return their lengths in a separate
0443          * field.
0444          */
0445         if (hole_len) {
0446             ret = ocfs2_figure_hole_clusters(INODE_CACHE(inode),
0447                              el, eb_bh,
0448                              v_cluster, &len);
0449             if (ret) {
0450                 mlog_errno(ret);
0451                 goto out;
0452             }
0453 
0454             *hole_len = len;
0455         }
0456         goto out_hole;
0457     }
0458 
0459     rec = &el->l_recs[i];
0460 
0461     BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
0462 
0463     if (!rec->e_blkno) {
0464         ocfs2_error(inode->i_sb,
0465                 "Inode %lu has bad extent record (%u, %u, 0)\n",
0466                 inode->i_ino,
0467                 le32_to_cpu(rec->e_cpos),
0468                 ocfs2_rec_clusters(el, rec));
0469         ret = -EROFS;
0470         goto out;
0471     }
0472 
0473     *ret_rec = *rec;
0474 
0475     /*
0476      * Checking for last extent is potentially expensive - we
0477      * might have to look at the next leaf over to see if it's
0478      * empty.
0479      *
0480      * The first two checks are to see whether the caller even
0481      * cares for this information, and if the extent is at least
0482      * the last in it's list.
0483      *
0484      * If those hold true, then the extent is last if any of the
0485      * additional conditions hold true:
0486      *  - Extent list is in-inode
0487      *  - Extent list is right-most
0488      *  - Extent list is 2nd to rightmost, with empty right-most
0489      */
0490     if (is_last) {
0491         if (i == (le16_to_cpu(el->l_next_free_rec) - 1)) {
0492             if (tree_height == 0)
0493                 *is_last = 1;
0494             else if (eb->h_blkno == di->i_last_eb_blk)
0495                 *is_last = 1;
0496             else if (eb->h_next_leaf_blk == di->i_last_eb_blk) {
0497                 ret = ocfs2_last_eb_is_empty(inode, di);
0498                 if (ret < 0) {
0499                     mlog_errno(ret);
0500                     goto out;
0501                 }
0502                 if (ret == 1)
0503                     *is_last = 1;
0504             }
0505         }
0506     }
0507 
0508 out_hole:
0509     ret = 0;
0510 out:
0511     brelse(eb_bh);
0512     return ret;
0513 }
0514 
0515 static void ocfs2_relative_extent_offsets(struct super_block *sb,
0516                       u32 v_cluster,
0517                       struct ocfs2_extent_rec *rec,
0518                       u32 *p_cluster, u32 *num_clusters)
0519 
0520 {
0521     u32 coff = v_cluster - le32_to_cpu(rec->e_cpos);
0522 
0523     *p_cluster = ocfs2_blocks_to_clusters(sb, le64_to_cpu(rec->e_blkno));
0524     *p_cluster = *p_cluster + coff;
0525 
0526     if (num_clusters)
0527         *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
0528 }
0529 
0530 int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
0531                  u32 *p_cluster, u32 *num_clusters,
0532                  struct ocfs2_extent_list *el,
0533                  unsigned int *extent_flags)
0534 {
0535     int ret = 0, i;
0536     struct buffer_head *eb_bh = NULL;
0537     struct ocfs2_extent_block *eb;
0538     struct ocfs2_extent_rec *rec;
0539     u32 coff;
0540 
0541     if (el->l_tree_depth) {
0542         ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
0543                       &eb_bh);
0544         if (ret) {
0545             mlog_errno(ret);
0546             goto out;
0547         }
0548 
0549         eb = (struct ocfs2_extent_block *) eb_bh->b_data;
0550         el = &eb->h_list;
0551 
0552         if (el->l_tree_depth) {
0553             ocfs2_error(inode->i_sb,
0554                     "Inode %lu has non zero tree depth in xattr leaf block %llu\n",
0555                     inode->i_ino,
0556                     (unsigned long long)eb_bh->b_blocknr);
0557             ret = -EROFS;
0558             goto out;
0559         }
0560     }
0561 
0562     i = ocfs2_search_extent_list(el, v_cluster);
0563     if (i == -1) {
0564         ret = -EROFS;
0565         mlog_errno(ret);
0566         goto out;
0567     } else {
0568         rec = &el->l_recs[i];
0569         BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
0570 
0571         if (!rec->e_blkno) {
0572             ocfs2_error(inode->i_sb,
0573                     "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
0574                     inode->i_ino,
0575                     le32_to_cpu(rec->e_cpos),
0576                     ocfs2_rec_clusters(el, rec));
0577             ret = -EROFS;
0578             goto out;
0579         }
0580         coff = v_cluster - le32_to_cpu(rec->e_cpos);
0581         *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
0582                             le64_to_cpu(rec->e_blkno));
0583         *p_cluster = *p_cluster + coff;
0584         if (num_clusters)
0585             *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
0586 
0587         if (extent_flags)
0588             *extent_flags = rec->e_flags;
0589     }
0590 out:
0591     brelse(eb_bh);
0592     return ret;
0593 }
0594 
0595 int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
0596                u32 *p_cluster, u32 *num_clusters,
0597                unsigned int *extent_flags)
0598 {
0599     int ret;
0600     unsigned int hole_len, flags = 0;
0601     struct buffer_head *di_bh = NULL;
0602     struct ocfs2_extent_rec rec;
0603 
0604     if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
0605         ret = -ERANGE;
0606         mlog_errno(ret);
0607         goto out;
0608     }
0609 
0610     ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
0611                       num_clusters, extent_flags);
0612     if (ret == 0)
0613         goto out;
0614 
0615     ret = ocfs2_read_inode_block(inode, &di_bh);
0616     if (ret) {
0617         mlog_errno(ret);
0618         goto out;
0619     }
0620 
0621     ret = ocfs2_get_clusters_nocache(inode, di_bh, v_cluster, &hole_len,
0622                      &rec, NULL);
0623     if (ret) {
0624         mlog_errno(ret);
0625         goto out;
0626     }
0627 
0628     if (rec.e_blkno == 0ULL) {
0629         /*
0630          * A hole was found. Return some canned values that
0631          * callers can key on. If asked for, num_clusters will
0632          * be populated with the size of the hole.
0633          */
0634         *p_cluster = 0;
0635         if (num_clusters) {
0636             *num_clusters = hole_len;
0637         }
0638     } else {
0639         ocfs2_relative_extent_offsets(inode->i_sb, v_cluster, &rec,
0640                           p_cluster, num_clusters);
0641         flags = rec.e_flags;
0642 
0643         ocfs2_extent_map_insert_rec(inode, &rec);
0644     }
0645 
0646     if (extent_flags)
0647         *extent_flags = flags;
0648 
0649 out:
0650     brelse(di_bh);
0651     return ret;
0652 }
0653 
0654 /*
0655  * This expects alloc_sem to be held. The allocation cannot change at
0656  * all while the map is in the process of being updated.
0657  */
0658 int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
0659                 u64 *ret_count, unsigned int *extent_flags)
0660 {
0661     int ret;
0662     int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
0663     u32 cpos, num_clusters, p_cluster;
0664     u64 boff = 0;
0665 
0666     cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
0667 
0668     ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
0669                  extent_flags);
0670     if (ret) {
0671         mlog_errno(ret);
0672         goto out;
0673     }
0674 
0675     /*
0676      * p_cluster == 0 indicates a hole.
0677      */
0678     if (p_cluster) {
0679         boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
0680         boff += (v_blkno & (u64)(bpc - 1));
0681     }
0682 
0683     *p_blkno = boff;
0684 
0685     if (ret_count) {
0686         *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
0687         *ret_count -= v_blkno & (u64)(bpc - 1);
0688     }
0689 
0690 out:
0691     return ret;
0692 }
0693 
0694 /*
0695  * The ocfs2_fiemap_inline() may be a little bit misleading, since
0696  * it not only handles the fiemap for inlined files, but also deals
0697  * with the fast symlink, cause they have no difference for extent
0698  * mapping per se.
0699  */
0700 static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
0701                    struct fiemap_extent_info *fieinfo,
0702                    u64 map_start)
0703 {
0704     int ret;
0705     unsigned int id_count;
0706     struct ocfs2_dinode *di;
0707     u64 phys;
0708     u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
0709     struct ocfs2_inode_info *oi = OCFS2_I(inode);
0710 
0711     di = (struct ocfs2_dinode *)di_bh->b_data;
0712     if (ocfs2_inode_is_fast_symlink(inode))
0713         id_count = ocfs2_fast_symlink_chars(inode->i_sb);
0714     else
0715         id_count = le16_to_cpu(di->id2.i_data.id_count);
0716 
0717     if (map_start < id_count) {
0718         phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
0719         if (ocfs2_inode_is_fast_symlink(inode))
0720             phys += offsetof(struct ocfs2_dinode, id2.i_symlink);
0721         else
0722             phys += offsetof(struct ocfs2_dinode,
0723                      id2.i_data.id_data);
0724 
0725         ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
0726                           flags);
0727         if (ret < 0)
0728             return ret;
0729     }
0730 
0731     return 0;
0732 }
0733 
0734 int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
0735          u64 map_start, u64 map_len)
0736 {
0737     int ret, is_last;
0738     u32 mapping_end, cpos;
0739     unsigned int hole_size;
0740     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0741     u64 len_bytes, phys_bytes, virt_bytes;
0742     struct buffer_head *di_bh = NULL;
0743     struct ocfs2_extent_rec rec;
0744 
0745     ret = fiemap_prep(inode, fieinfo, map_start, &map_len, 0);
0746     if (ret)
0747         return ret;
0748 
0749     ret = ocfs2_inode_lock(inode, &di_bh, 0);
0750     if (ret) {
0751         mlog_errno(ret);
0752         goto out;
0753     }
0754 
0755     down_read(&OCFS2_I(inode)->ip_alloc_sem);
0756 
0757     /*
0758      * Handle inline-data and fast symlink separately.
0759      */
0760     if ((OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
0761         ocfs2_inode_is_fast_symlink(inode)) {
0762         ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
0763         goto out_unlock;
0764     }
0765 
0766     cpos = map_start >> osb->s_clustersize_bits;
0767     mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
0768                            map_start + map_len);
0769     is_last = 0;
0770     while (cpos < mapping_end && !is_last) {
0771         u32 fe_flags;
0772 
0773         ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
0774                          &hole_size, &rec, &is_last);
0775         if (ret) {
0776             mlog_errno(ret);
0777             goto out_unlock;
0778         }
0779 
0780         if (rec.e_blkno == 0ULL) {
0781             cpos += hole_size;
0782             continue;
0783         }
0784 
0785         fe_flags = 0;
0786         if (rec.e_flags & OCFS2_EXT_UNWRITTEN)
0787             fe_flags |= FIEMAP_EXTENT_UNWRITTEN;
0788         if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
0789             fe_flags |= FIEMAP_EXTENT_SHARED;
0790         if (is_last)
0791             fe_flags |= FIEMAP_EXTENT_LAST;
0792         len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
0793         phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
0794         virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
0795 
0796         ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
0797                           len_bytes, fe_flags);
0798         if (ret)
0799             break;
0800 
0801         cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
0802     }
0803 
0804     if (ret > 0)
0805         ret = 0;
0806 
0807 out_unlock:
0808     brelse(di_bh);
0809 
0810     up_read(&OCFS2_I(inode)->ip_alloc_sem);
0811 
0812     ocfs2_inode_unlock(inode, 0);
0813 out:
0814 
0815     return ret;
0816 }
0817 
0818 /* Is IO overwriting allocated blocks? */
0819 int ocfs2_overwrite_io(struct inode *inode, struct buffer_head *di_bh,
0820                u64 map_start, u64 map_len)
0821 {
0822     int ret = 0, is_last;
0823     u32 mapping_end, cpos;
0824     struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
0825     struct ocfs2_extent_rec rec;
0826 
0827     if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
0828         if (ocfs2_size_fits_inline_data(di_bh, map_start + map_len))
0829             return ret;
0830         else
0831             return -EAGAIN;
0832     }
0833 
0834     cpos = map_start >> osb->s_clustersize_bits;
0835     mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
0836                            map_start + map_len);
0837     is_last = 0;
0838     while (cpos < mapping_end && !is_last) {
0839         ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
0840                          NULL, &rec, &is_last);
0841         if (ret) {
0842             mlog_errno(ret);
0843             goto out;
0844         }
0845 
0846         if (rec.e_blkno == 0ULL)
0847             break;
0848 
0849         if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
0850             break;
0851 
0852         cpos = le32_to_cpu(rec.e_cpos) +
0853             le16_to_cpu(rec.e_leaf_clusters);
0854     }
0855 
0856     if (cpos < mapping_end)
0857         ret = -EAGAIN;
0858 out:
0859     return ret;
0860 }
0861 
0862 int ocfs2_seek_data_hole_offset(struct file *file, loff_t *offset, int whence)
0863 {
0864     struct inode *inode = file->f_mapping->host;
0865     int ret;
0866     unsigned int is_last = 0, is_data = 0;
0867     u16 cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
0868     u32 cpos, cend, clen, hole_size;
0869     u64 extoff, extlen;
0870     struct buffer_head *di_bh = NULL;
0871     struct ocfs2_extent_rec rec;
0872 
0873     BUG_ON(whence != SEEK_DATA && whence != SEEK_HOLE);
0874 
0875     ret = ocfs2_inode_lock(inode, &di_bh, 0);
0876     if (ret) {
0877         mlog_errno(ret);
0878         goto out;
0879     }
0880 
0881     down_read(&OCFS2_I(inode)->ip_alloc_sem);
0882 
0883     if (*offset >= i_size_read(inode)) {
0884         ret = -ENXIO;
0885         goto out_unlock;
0886     }
0887 
0888     if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
0889         if (whence == SEEK_HOLE)
0890             *offset = i_size_read(inode);
0891         goto out_unlock;
0892     }
0893 
0894     clen = 0;
0895     cpos = *offset >> cs_bits;
0896     cend = ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
0897 
0898     while (cpos < cend && !is_last) {
0899         ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos, &hole_size,
0900                          &rec, &is_last);
0901         if (ret) {
0902             mlog_errno(ret);
0903             goto out_unlock;
0904         }
0905 
0906         extoff = cpos;
0907         extoff <<= cs_bits;
0908 
0909         if (rec.e_blkno == 0ULL) {
0910             clen = hole_size;
0911             is_data = 0;
0912         } else {
0913             clen = le16_to_cpu(rec.e_leaf_clusters) -
0914                 (cpos - le32_to_cpu(rec.e_cpos));
0915             is_data = (rec.e_flags & OCFS2_EXT_UNWRITTEN) ?  0 : 1;
0916         }
0917 
0918         if ((!is_data && whence == SEEK_HOLE) ||
0919             (is_data && whence == SEEK_DATA)) {
0920             if (extoff > *offset)
0921                 *offset = extoff;
0922             goto out_unlock;
0923         }
0924 
0925         if (!is_last)
0926             cpos += clen;
0927     }
0928 
0929     if (whence == SEEK_HOLE) {
0930         extoff = cpos;
0931         extoff <<= cs_bits;
0932         extlen = clen;
0933         extlen <<=  cs_bits;
0934 
0935         if ((extoff + extlen) > i_size_read(inode))
0936             extlen = i_size_read(inode) - extoff;
0937         extoff += extlen;
0938         if (extoff > *offset)
0939             *offset = extoff;
0940         goto out_unlock;
0941     }
0942 
0943     ret = -ENXIO;
0944 
0945 out_unlock:
0946 
0947     brelse(di_bh);
0948 
0949     up_read(&OCFS2_I(inode)->ip_alloc_sem);
0950 
0951     ocfs2_inode_unlock(inode, 0);
0952 out:
0953     return ret;
0954 }
0955 
0956 int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
0957                struct buffer_head *bhs[], int flags,
0958                int (*validate)(struct super_block *sb,
0959                        struct buffer_head *bh))
0960 {
0961     int rc = 0;
0962     u64 p_block, p_count;
0963     int i, count, done = 0;
0964 
0965     trace_ocfs2_read_virt_blocks(
0966          inode, (unsigned long long)v_block, nr, bhs, flags,
0967          validate);
0968 
0969     if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
0970         i_size_read(inode)) {
0971         BUG_ON(!(flags & OCFS2_BH_READAHEAD));
0972         goto out;
0973     }
0974 
0975     while (done < nr) {
0976         down_read(&OCFS2_I(inode)->ip_alloc_sem);
0977         rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
0978                          &p_block, &p_count, NULL);
0979         up_read(&OCFS2_I(inode)->ip_alloc_sem);
0980         if (rc) {
0981             mlog_errno(rc);
0982             break;
0983         }
0984 
0985         if (!p_block) {
0986             rc = -EIO;
0987             mlog(ML_ERROR,
0988                  "Inode #%llu contains a hole at offset %llu\n",
0989                  (unsigned long long)OCFS2_I(inode)->ip_blkno,
0990                  (unsigned long long)(v_block + done) <<
0991                  inode->i_sb->s_blocksize_bits);
0992             break;
0993         }
0994 
0995         count = nr - done;
0996         if (p_count < count)
0997             count = p_count;
0998 
0999         /*
1000          * If the caller passed us bhs, they should have come
1001          * from a previous readahead call to this function.  Thus,
1002          * they should have the right b_blocknr.
1003          */
1004         for (i = 0; i < count; i++) {
1005             if (!bhs[done + i])
1006                 continue;
1007             BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
1008         }
1009 
1010         rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
1011                        bhs + done, flags, validate);
1012         if (rc) {
1013             mlog_errno(rc);
1014             break;
1015         }
1016         done += count;
1017     }
1018 
1019 out:
1020     return rc;
1021 }
1022 
1023