^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) * Copyright (C) 2007 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/falloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/btrfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "disk-io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "transaction.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "print-tree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "tree-log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "locking.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "qgroup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "delalloc-space.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "reflink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct kmem_cache *btrfs_inode_defrag_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * when auto defrag is enabled we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * queue up these defrag structs to remember which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * inodes need defragging passes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct inode_defrag {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct rb_node rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* objectid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) u64 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * transid where the defrag was added, we search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * extents newer than this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u64 transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* root objectid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u64 root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* last offset we were able to defrag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) u64 last_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* if we've wrapped around back to zero once already */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int cycled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int __compare_inode_defrag(struct inode_defrag *defrag1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct inode_defrag *defrag2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (defrag1->root > defrag2->root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) else if (defrag1->root < defrag2->root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) else if (defrag1->ino > defrag2->ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else if (defrag1->ino < defrag2->ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^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) /* pop a record for an inode into the defrag tree. The lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * must be held already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * If you're inserting a record for an older transid than an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * existing record, the transid already in the tree is lowered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * If an existing record is found the defrag item you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * pass in is freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static int __btrfs_add_inode_defrag(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct inode_defrag *defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct inode_defrag *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct rb_node **p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) p = &fs_info->defrag_inodes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) entry = rb_entry(parent, struct inode_defrag, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ret = __compare_inode_defrag(defrag, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* if we're reinserting an entry for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * an old defrag run, make sure to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * lower the transid of our existing record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (defrag->transid < entry->transid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) entry->transid = defrag->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (defrag->last_offset > entry->last_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) entry->last_offset = defrag->last_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -EEXIST;
^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) set_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rb_link_node(&defrag->rb_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (btrfs_fs_closing(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * insert a defrag record for this inode if auto defrag is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct inode_defrag *defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) u64 transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!__need_auto_defrag(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) transid = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) transid = inode->root->last_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (!defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) defrag->ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) defrag->transid = transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) defrag->root = root->root_key.objectid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_lock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * If we set IN_DEFRAG flag and evict the inode from memory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * and then re-read this inode, this new inode doesn't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * IN_DEFRAG flag. At the case, we may find the existed defrag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ret = __btrfs_add_inode_defrag(inode, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) spin_unlock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Requeue the defrag object. If there is a defrag object that points to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * the same inode in the tree, we will merge them together (by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * __btrfs_add_inode_defrag()) and free the one that we want to requeue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static void btrfs_requeue_inode_defrag(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct inode_defrag *defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!__need_auto_defrag(fs_info))
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Here we don't check the IN_DEFRAG flag, because we need merge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * them together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) spin_lock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ret = __btrfs_add_inode_defrag(inode, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) spin_unlock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * pick the defragable inode that we want, if it doesn't exist, we will get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static struct inode_defrag *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) btrfs_pick_defrag_inode(struct btrfs_fs_info *fs_info, u64 root, u64 ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct inode_defrag *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct inode_defrag tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) tmp.ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) tmp.root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) spin_lock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) p = fs_info->defrag_inodes.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) parent = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) entry = rb_entry(parent, struct inode_defrag, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = __compare_inode_defrag(&tmp, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) p = parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) p = parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (parent && __compare_inode_defrag(&tmp, entry) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) parent = rb_next(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) entry = rb_entry(parent, struct inode_defrag, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) rb_erase(parent, &fs_info->defrag_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) spin_unlock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct inode_defrag *defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_lock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) node = rb_first(&fs_info->defrag_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) rb_erase(node, &fs_info->defrag_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) defrag = rb_entry(node, struct inode_defrag, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) cond_resched_lock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) node = rb_first(&fs_info->defrag_inodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) spin_unlock(&fs_info->defrag_inodes_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) #define BTRFS_DEFRAG_BATCH 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct inode_defrag *defrag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct btrfs_root *inode_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct btrfs_ioctl_defrag_range_args range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int num_defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* get the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (IS_ERR(inode_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = PTR_ERR(inode_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) btrfs_put_root(inode_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ret = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* do a chunk of defrag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) memset(&range, 0, sizeof(range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) range.len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) range.start = defrag->last_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) sb_start_write(fs_info->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) BTRFS_DEFRAG_BATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) sb_end_write(fs_info->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * if we filled the whole defrag batch, there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * must be more work to do. Queue this defrag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (num_defrag == BTRFS_DEFRAG_BATCH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) defrag->last_offset = range.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) } else if (defrag->last_offset && !defrag->cycled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * we didn't fill our defrag batch, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * we didn't start at zero. Make sure we loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * around to the start of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) defrag->last_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) defrag->cycled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * run through the list of inodes in the FS that need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * defragging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct inode_defrag *defrag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) u64 first_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) u64 root_objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) atomic_inc(&fs_info->defrag_running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* Pause the auto defragger. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (test_bit(BTRFS_FS_STATE_REMOUNTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) &fs_info->fs_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (!__need_auto_defrag(fs_info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* find an inode to defrag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) defrag = btrfs_pick_defrag_inode(fs_info, root_objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) first_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!defrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (root_objectid || first_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) root_objectid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) first_ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) first_ino = defrag->ino + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) root_objectid = defrag->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) __btrfs_run_defrag_inode(fs_info, defrag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) atomic_dec(&fs_info->defrag_running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * during unmount, we use the transaction_wait queue to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * wait for the defragger to stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) wake_up(&fs_info->transaction_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /* simple helper to fault in pages and copy. This should go away
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * and be replaced with calls into generic code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct page **prepared_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct iov_iter *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) size_t copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) size_t total_copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) int pg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) int offset = offset_in_page(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) while (write_bytes > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) size_t count = min_t(size_t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) PAGE_SIZE - offset, write_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct page *page = prepared_pages[pg];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * Copy data from userspace to the current page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* Flush processor's dcache for this page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * if we get a partial write, we can end up with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * partially up to date pages. These add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * a lot of complexity, so make sure they don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * happen by forcing this copy to be retried.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * The rest of the btrfs_file_write code will fall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * back to page at a time copies after we return 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!PageUptodate(page) && copied < count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) iov_iter_advance(i, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) write_bytes -= copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) total_copied += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* Return to btrfs_file_write_iter to fault page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (unlikely(copied == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (copied < PAGE_SIZE - offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) offset += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) pg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return total_copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * unlocks pages after btrfs_file_write is done with them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static void btrfs_drop_pages(struct page **pages, size_t num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /* page checked is some magic around finding pages that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * have been modified without going through btrfs_set_page_dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * clear it here. There should be no need to mark the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * accessed as prepare_pages should have marked them accessed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * in prepare_pages via find_or_create_page()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ClearPageChecked(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * after copy_from_user, pages need to be dirtied and we need to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * sure holes are created between the current EOF and the start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * any next extents (if required).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * this also makes the decision about creating an inline extent vs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * doing real data extents, marking pages dirty and delalloc as required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) int btrfs_dirty_pages(struct btrfs_inode *inode, struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) size_t num_pages, loff_t pos, size_t write_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct extent_state **cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) u64 start_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) u64 end_of_last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) u64 end_pos = pos + write_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) loff_t isize = i_size_read(&inode->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) unsigned int extra_bits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) start_pos = pos & ~((u64) fs_info->sectorsize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) num_bytes = round_up(write_bytes + pos - start_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) end_of_last_block = start_pos + num_bytes - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * The pages may have already been dirty, clear out old accounting so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * we can set things up properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) clear_extent_bit(&inode->io_tree, start_pos, end_of_last_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 0, 0, cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) extra_bits, cached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct page *p = pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) SetPageUptodate(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) ClearPageChecked(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) set_page_dirty(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * we've only changed i_size in ram, and we haven't updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * the disk i_size. There is no need to log the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * at this time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (end_pos > isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) i_size_write(&inode->vfs_inode, end_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * this drops all the extents in the cache that intersect the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * [start, end]. Existing extents are split as required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int skip_pinned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct extent_map *split = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct extent_map *split2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct extent_map_tree *em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) u64 len = end - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) u64 gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int testend = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int compressed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) bool modified;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) WARN_ON(end < start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (end == (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) len = (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) testend = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int no_splits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) modified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (!split)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) split = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (!split2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) split2 = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!split || !split2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) no_splits = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) em = lookup_extent_mapping(em_tree, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) flags = em->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) gen = em->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (testend && em->start + em->len >= start + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) start = em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (testend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) len = start + len - (em->start + em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) clear_bit(EXTENT_FLAG_PINNED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) clear_bit(EXTENT_FLAG_LOGGING, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) modified = !list_empty(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (no_splits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (em->start < start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) split->start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) split->len = start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (em->block_start < EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) split->orig_start = em->orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) split->block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (compressed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) split->block_len = em->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) split->block_len = split->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) split->orig_block_len = max(split->block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) em->orig_block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) split->ram_bytes = em->ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) split->orig_start = split->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) split->block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) split->block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) split->orig_block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) split->ram_bytes = split->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) split->generation = gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) split->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) split->compress_type = em->compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) replace_extent_mapping(em_tree, em, split, modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) free_extent_map(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) split = split2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) split2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) if (testend && em->start + em->len > start + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) u64 diff = start + len - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) split->start = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) split->len = em->start + em->len - (start + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) split->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) split->compress_type = em->compress_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) split->generation = gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (em->block_start < EXTENT_MAP_LAST_BYTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) split->orig_block_len = max(em->block_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) em->orig_block_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) split->ram_bytes = em->ram_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (compressed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) split->block_len = em->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) split->block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) split->orig_start = em->orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) split->block_len = split->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) split->block_start = em->block_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) + diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) split->orig_start = em->orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) split->ram_bytes = split->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) split->orig_start = split->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) split->block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) split->block_start = em->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) split->orig_block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (extent_map_in_tree(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) replace_extent_mapping(em_tree, em, split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ret = add_extent_mapping(em_tree, split,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) ASSERT(ret == 0); /* Logic error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) free_extent_map(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) split = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (extent_map_in_tree(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) remove_extent_mapping(em_tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) /* once for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /* once for the tree*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (split)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) free_extent_map(split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (split2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) free_extent_map(split2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * this is very complex, but the basic idea is to drop all extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * in the range start - end. hint_block is filled in with a block number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * that would be a good hint to the block allocator for this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * If an extent intersects the range but is not entirely inside the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * it is either truncated or split. Anything entirely inside the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * is deleted from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) struct btrfs_root *root, struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) struct btrfs_path *path, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) u64 *drop_end, int drop_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) int replace_extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) u32 extent_item_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) int *key_inserted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) struct btrfs_fs_info *fs_info = root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) struct btrfs_key new_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) struct inode *vfs_inode = &inode->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) u64 search_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) u64 disk_bytenr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) u64 num_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) u64 extent_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) u64 extent_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) u64 last_end = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int del_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) int del_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) int extent_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) int recow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) int modify_tree = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) int update_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) int leafs_visited = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (drop_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) btrfs_drop_extent_cache(inode, start, end - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (start >= inode->disk_i_size && !replace_extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) modify_tree = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) update_refs = (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) recow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) ret = btrfs_lookup_file_extent(trans, root, path, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) search_start, modify_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (ret > 0 && path->slots[0] > 0 && search_start == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (key.objectid == ino &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) key.type == BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) leafs_visited++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) next_slot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (path->slots[0] >= btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) BUG_ON(del_nr > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) ret = btrfs_next_leaf(root, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) leafs_visited++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) recow = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (key.objectid > ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (WARN_ON_ONCE(key.objectid < ino) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) key.type < BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) ASSERT(del_nr == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) extent_type = btrfs_file_extent_type(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (extent_type == BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) extent_offset = btrfs_file_extent_offset(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) extent_end = key.offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) extent_end = key.offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) btrfs_file_extent_ram_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /* can't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * Don't skip extent items representing 0 byte lengths. They
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) * used to be created (bug) if while punching holes we hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * -ENOSPC condition. So if we find one here, just ensure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * delete it, otherwise we would insert a new file extent item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * with the same key (offset) as that 0 bytes length file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * extent item in the call to setup_items_for_insert() later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * in this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (extent_end == key.offset && extent_end >= search_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) last_end = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) goto delete_extent_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (extent_end <= search_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) search_start = max(key.offset, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (recow || !modify_tree) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) modify_tree = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) * | - range to drop - |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) * | -------- extent -------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (start > key.offset && end < extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) BUG_ON(del_nr > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) memcpy(&new_key, &key, sizeof(new_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) new_key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) ret = btrfs_duplicate_item(trans, root, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) &new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) start - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) extent_offset += start - key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) btrfs_set_file_extent_offset(leaf, fi, extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) extent_end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) if (update_refs && disk_bytenr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) btrfs_init_generic_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) BTRFS_ADD_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) disk_bytenr, num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) btrfs_init_data_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) new_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) start - extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) ret = btrfs_inc_extent_ref(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * From here on out we will have actually dropped something, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * last_end can be updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) last_end = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * | ---- range to drop ----- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * | -------- extent -------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (start <= key.offset && end < extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) memcpy(&new_key, &key, sizeof(new_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) new_key.offset = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) btrfs_set_item_key_safe(fs_info, path, &new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) extent_offset += end - key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) btrfs_set_file_extent_offset(leaf, fi, extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) extent_end - end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (update_refs && disk_bytenr > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) inode_sub_bytes(vfs_inode, end - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) search_start = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * | ---- range to drop ----- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * | -------- extent -------- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) if (start > key.offset && end >= extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) BUG_ON(del_nr > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) start - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) if (update_refs && disk_bytenr > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) inode_sub_bytes(vfs_inode, extent_end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (end == extent_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) * | ---- range to drop ----- |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * | ------ extent ------ |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (start <= key.offset && end >= extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) delete_extent_item:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) if (del_nr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) del_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) del_nr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) BUG_ON(del_slot + del_nr != path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) del_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (update_refs &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) extent_type == BTRFS_FILE_EXTENT_INLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) inode_sub_bytes(vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) extent_end - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) extent_end = ALIGN(extent_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) } else if (update_refs && disk_bytenr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) btrfs_init_generic_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) BTRFS_DROP_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) disk_bytenr, num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) btrfs_init_data_ref(&ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) key.offset - extent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) ret = btrfs_free_extent(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) BUG_ON(ret); /* -ENOMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) inode_sub_bytes(vfs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) extent_end - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (end == extent_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) goto next_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) ret = btrfs_del_items(trans, root, path, del_slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) del_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) del_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) del_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (!ret && del_nr > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * Set path->slots[0] to first slot, so that after the delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) * if items are move off from our leaf to its immediate left or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) * right neighbor leafs, we end up with a correct and adjusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * path->slots[0] for our insertion (if replace_extent != 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) path->slots[0] = del_slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) * If btrfs_del_items() was called, it might have deleted a leaf, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) * which case it unlocked our path, so check path->locks[0] matches a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) * write lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (!ret && replace_extent && leafs_visited == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) path->locks[0] == BTRFS_WRITE_LOCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) btrfs_leaf_free_space(leaf) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) sizeof(struct btrfs_item) + extent_item_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (!del_nr && path->slots[0] < btrfs_header_nritems(leaf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) struct btrfs_key slot_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) btrfs_item_key_to_cpu(leaf, &slot_key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) if (btrfs_comp_cpu_keys(&key, &slot_key) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) setup_items_for_insert(root, path, &key, &extent_item_size, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) *key_inserted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) if (!replace_extent || !(*key_inserted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) if (drop_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) *drop_end = found ? min(end, last_end) : end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) int btrfs_drop_extents(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct btrfs_root *root, struct inode *inode, u64 start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) u64 end, int drop_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) end, NULL, drop_cache, 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static int extent_mergeable(struct extent_buffer *leaf, int slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) u64 objectid, u64 bytenr, u64 orig_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) u64 *start, u64 *end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (slot < 0 || slot >= btrfs_header_nritems(leaf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) btrfs_file_extent_compression(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) btrfs_file_extent_encryption(leaf, fi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) btrfs_file_extent_other_encoding(leaf, fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if ((*start && *start != key.offset) || (*end && *end != extent_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) *start = key.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) *end = extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * Mark extent in the range start - end as written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * This changes extent type from 'pre-allocated' to 'regular'. If only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * part of extent is marked as written, the extent will be split into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * two or three.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) struct btrfs_inode *inode, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) struct btrfs_key new_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) u64 bytenr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) u64 extent_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) u64 orig_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) u64 other_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) u64 other_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) u64 split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) int del_nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) int del_slot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) int recow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) u64 ino = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (!path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) recow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) split = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) key.objectid = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) key.offset = split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) if (ret > 0 && path->slots[0] > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (key.objectid != ino ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) key.type != BTRFS_EXTENT_DATA_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_PREALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) if (key.offset > start || extent_end < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) memcpy(&new_key, &key, sizeof(new_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) if (start == key.offset && end < extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) other_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) other_end = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) if (extent_mergeable(leaf, path->slots[0] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) ino, bytenr, orig_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) &other_start, &other_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) new_key.offset = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) btrfs_set_item_key_safe(fs_info, path, &new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) btrfs_set_file_extent_generation(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) extent_end - end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) btrfs_set_file_extent_offset(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) end - orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) btrfs_set_file_extent_generation(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) end - other_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) if (start > key.offset && end == extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) other_start = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) other_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) if (extent_mergeable(leaf, path->slots[0] + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) ino, bytenr, orig_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) &other_start, &other_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) start - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) btrfs_set_file_extent_generation(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) path->slots[0]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) new_key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) btrfs_set_item_key_safe(fs_info, path, &new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) btrfs_set_file_extent_generation(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) other_end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) btrfs_set_file_extent_offset(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) start - orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) while (start > key.offset || end < extent_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) if (key.offset == start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) split = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) new_key.offset = split;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) ret = btrfs_duplicate_item(trans, root, path, &new_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (ret == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) btrfs_set_file_extent_generation(leaf, fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) split - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) btrfs_set_file_extent_generation(leaf, fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) extent_end - split);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) btrfs_init_data_ref(&ref, root->root_key.objectid, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) ret = btrfs_inc_extent_ref(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) if (split == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) key.offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) if (start != key.offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) extent_end = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) recow = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) other_start = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) other_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) num_bytes, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) btrfs_init_data_ref(&ref, root->root_key.objectid, ino, orig_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (extent_mergeable(leaf, path->slots[0] + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) ino, bytenr, orig_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) &other_start, &other_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) if (recow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) extent_end = other_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) del_slot = path->slots[0] + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) del_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) ret = btrfs_free_extent(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) other_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) other_end = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (extent_mergeable(leaf, path->slots[0] - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) ino, bytenr, orig_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) &other_start, &other_end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) if (recow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) key.offset = other_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) del_slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) del_nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) ret = btrfs_free_extent(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) if (del_nr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) btrfs_set_file_extent_type(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) BTRFS_FILE_EXTENT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) btrfs_set_file_extent_generation(leaf, fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) fi = btrfs_item_ptr(leaf, del_slot - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) btrfs_set_file_extent_type(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) BTRFS_FILE_EXTENT_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) btrfs_set_file_extent_generation(leaf, fi, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) btrfs_set_file_extent_num_bytes(leaf, fi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) extent_end - key.offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) * on error we return an unlocked page and the error value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) * on success we return a locked page and 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) static int prepare_uptodate_page(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) struct page *page, u64 pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) bool force_uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (((pos & (PAGE_SIZE - 1)) || force_uptodate) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) !PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) ret = btrfs_readpage(NULL, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (page->mapping != inode->i_mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) * this just gets pages into the page cache and locks them down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) static noinline int prepare_pages(struct inode *inode, struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) size_t num_pages, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) size_t write_bytes, bool force_uptodate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) unsigned long index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) int faili;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) pages[i] = find_or_create_page(inode->i_mapping, index + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) mask | __GFP_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (!pages[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) faili = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) err = prepare_uptodate_page(inode, pages[i], pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) force_uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) if (!err && i == num_pages - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) err = prepare_uptodate_page(inode, pages[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) pos + write_bytes, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) if (err == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) faili = i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) wait_on_page_writeback(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) while (faili >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) unlock_page(pages[faili]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) put_page(pages[faili]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) faili--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * This function locks the extent and properly waits for data=ordered extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * to finish before allowing the pages to be modified if need.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) * The return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) * 1 - the extent is locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) * 0 - the extent is not locked, and everything is OK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) * -EAGAIN - need re-prepare the pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) * the other < 0 number - Something wrong happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) static noinline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) lock_and_cleanup_extent_if_need(struct btrfs_inode *inode, struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) size_t num_pages, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) size_t write_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) u64 *lockstart, u64 *lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) u64 start_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) u64 last_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) start_pos = round_down(pos, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) last_pos = round_up(pos + write_bytes, fs_info->sectorsize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (start_pos < inode->vfs_inode.i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) lock_extent_bits(&inode->io_tree, start_pos, last_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) ordered = btrfs_lookup_ordered_range(inode, start_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) last_pos - start_pos + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (ordered &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) ordered->file_offset + ordered->num_bytes > start_pos &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) ordered->file_offset <= last_pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) unlock_extent_cached(&inode->io_tree, start_pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) last_pos, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) btrfs_start_ordered_extent(ordered, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) if (ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) *lockstart = start_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) *lockend = last_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) * It's possible the pages are dirty right now, but we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) * to clean them yet because copy_from_user may catch a page fault
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) * and we might have to fall back to one page at a time. If that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) * happens, we'll unlock these pages and we'd have a window where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) * reclaim could sneak in and drop the once-dirty page on the floor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) * without writing it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * We have the pages locked and the extent range locked, so there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * no way someone can start IO on any dirty pages in this range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) * We'll call btrfs_dirty_pages() later on, and that will flip around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) * delalloc bits and dirty the pages as required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) set_page_extent_mapped(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) WARN_ON(!PageLocked(pages[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) static int check_can_nocow(struct btrfs_inode *inode, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) size_t *write_bytes, bool nowait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) u64 lockstart, lockend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) if (!(inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (!nowait && !btrfs_drew_try_write_lock(&root->snapshot_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) lockstart = round_down(pos, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) lockend = round_up(pos + *write_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) fs_info->sectorsize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) num_bytes = lockend - lockstart + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) if (nowait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) if (!try_lock_extent(&inode->io_tree, lockstart, lockend))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) ordered = btrfs_lookup_ordered_range(inode, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) if (ordered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) btrfs_lock_and_flush_ordered_range(inode, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) lockend, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) ret = can_nocow_extent(&inode->vfs_inode, lockstart, &num_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) NULL, NULL, NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (!nowait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) btrfs_drew_write_unlock(&root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) *write_bytes = min_t(size_t, *write_bytes ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) num_bytes - pos + lockstart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) unlock_extent(&inode->io_tree, lockstart, lockend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) static int check_nocow_nolock(struct btrfs_inode *inode, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) size_t *write_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) return check_can_nocow(inode, pos, write_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) * Check if we can do nocow write into the range [@pos, @pos + @write_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) * @pos: File offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) * @write_bytes: The length to write, will be updated to the nocow writeable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) * range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) * This function will flush ordered extents in the range to ensure proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) * nocow checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) * >0 and update @write_bytes if we can do nocow write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) * 0 if we can't do nocow write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) * -EAGAIN if we can't get the needed lock or there are ordered extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) * for * (nowait == true) case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) * <0 if other error happened
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * NOTE: Callers need to release the lock by btrfs_check_nocow_unlock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) int btrfs_check_nocow_lock(struct btrfs_inode *inode, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) size_t *write_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) return check_can_nocow(inode, pos, write_bytes, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) void btrfs_check_nocow_unlock(struct btrfs_inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) btrfs_drew_write_unlock(&inode->root->snapshot_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) static noinline ssize_t btrfs_buffered_write(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) struct iov_iter *i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) loff_t pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) struct page **pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) u64 release_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) u64 lockstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) u64 lockend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) size_t num_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) int nrptrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) bool only_release_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) bool force_page_uptodate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) nrptrs = min(DIV_ROUND_UP(iov_iter_count(i), PAGE_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) PAGE_SIZE / (sizeof(struct page *)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) nrptrs = max(nrptrs, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) pages = kmalloc_array(nrptrs, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) if (!pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) while (iov_iter_count(i) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) size_t offset = offset_in_page(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) size_t sector_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) size_t write_bytes = min(iov_iter_count(i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) nrptrs * (size_t)PAGE_SIZE -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) size_t num_pages = DIV_ROUND_UP(write_bytes + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) size_t reserve_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) size_t dirty_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) size_t copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) size_t dirty_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) size_t num_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) int extents_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) WARN_ON(num_pages > nrptrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) * Fault pages before locking them in prepare_pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) * to avoid recursive lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) only_release_metadata = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) sector_offset = pos & (fs_info->sectorsize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) reserve_bytes = round_up(write_bytes + sector_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) extent_changeset_release(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) ret = btrfs_check_data_free_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) &data_reserved, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) write_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (btrfs_check_nocow_lock(BTRFS_I(inode), pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) &write_bytes) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) * For nodata cow case, no need to reserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) * data space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) only_release_metadata = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) * our prealloc extent may be smaller than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) * write_bytes, so scale down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) num_pages = DIV_ROUND_UP(write_bytes + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) reserve_bytes = round_up(write_bytes +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) sector_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) WARN_ON(reserve_bytes == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) reserve_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) if (!only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) btrfs_free_reserved_data_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) data_reserved, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) write_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) btrfs_check_nocow_unlock(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) release_bytes = reserve_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) * This is going to setup the pages array with the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) * pages we want, so we don't really need to worry about the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) * contents of pages from loop to loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) ret = prepare_pages(inode, pages, num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) pos, write_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) force_page_uptodate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) btrfs_delalloc_release_extents(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) reserve_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) extents_locked = lock_and_cleanup_extent_if_need(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) BTRFS_I(inode), pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) num_pages, pos, write_bytes, &lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) &lockend, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (extents_locked < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) if (extents_locked == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) btrfs_delalloc_release_extents(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) reserve_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) ret = extents_locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) dirty_sectors = round_up(copied + sector_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) * if we have trouble faulting in the pages, fall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) * back to one page at a time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (copied < write_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) nrptrs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) if (copied == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) force_page_uptodate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) dirty_sectors = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) dirty_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) force_page_uptodate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) dirty_pages = DIV_ROUND_UP(copied + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) if (num_sectors > dirty_sectors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) /* release everything except the sectors we dirtied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) release_bytes -= dirty_sectors <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) fs_info->sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (only_release_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) btrfs_delalloc_release_metadata(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) release_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) u64 __pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) __pos = round_down(pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) fs_info->sectorsize) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) (dirty_pages << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) btrfs_delalloc_release_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) data_reserved, __pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) release_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) release_bytes = round_up(copied + sector_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) if (copied > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) ret = btrfs_dirty_pages(BTRFS_I(inode), pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) dirty_pages, pos, copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) * If we have not locked the extent range, because the range's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) * start offset is >= i_size, we might still have a non-NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) * cached extent state, acquired while marking the extent range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) * as delalloc through btrfs_dirty_pages(). Therefore free any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) * possible cached extent state to avoid a memory leak.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) if (extents_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) unlock_extent_cached(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) lockstart, lockend, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) free_extent_state(cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) btrfs_delalloc_release_extents(BTRFS_I(inode), reserve_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) btrfs_drop_pages(pages, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) release_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) if (only_release_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) btrfs_check_nocow_unlock(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) if (only_release_metadata && copied > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) lockstart = round_down(pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) lockend = round_up(pos + copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) fs_info->sectorsize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) lockend, EXTENT_NORESERVE, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) NULL, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) btrfs_drop_pages(pages, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) balance_dirty_pages_ratelimited(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) pos += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) num_written += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (release_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) if (only_release_metadata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) btrfs_check_nocow_unlock(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) btrfs_delalloc_release_metadata(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) release_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) btrfs_delalloc_release_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) round_down(pos, fs_info->sectorsize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) release_bytes, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) return num_written ? num_written : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) static ssize_t __btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) ssize_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) ssize_t written_buffered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) loff_t endbyte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) written = btrfs_direct_IO(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) if (written < 0 || !iov_iter_count(from))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) written_buffered = btrfs_buffered_write(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) if (written_buffered < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) err = written_buffered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) * Ensure all data is persisted. We want the next direct IO read to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) * able to read what was just written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) endbyte = pos + written_buffered - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) err = btrfs_fdatawrite_range(inode, pos, endbyte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) err = filemap_fdatawait_range(inode->i_mapping, pos, endbyte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) written += written_buffered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) iocb->ki_pos = pos + written_buffered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) invalidate_mapping_pages(file->f_mapping, pos >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) endbyte >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) return written ? written : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) static void update_time_for_write(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) struct timespec64 now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) if (IS_NOCMTIME(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) now = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) if (!timespec64_equal(&inode->i_mtime, &now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) inode->i_mtime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) if (!timespec64_equal(&inode->i_ctime, &now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) inode->i_ctime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) if (IS_I_VERSION(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) u64 start_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) u64 end_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) ssize_t num_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) const bool sync = iocb->ki_flags & IOCB_DSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) ssize_t err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) size_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) loff_t oldsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) int clean_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) if (!(iocb->ki_flags & IOCB_DIRECT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) (iocb->ki_flags & IOCB_NOWAIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) if (iocb->ki_flags & IOCB_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) if (!inode_trylock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) err = generic_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (err <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) count = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) if (iocb->ki_flags & IOCB_NOWAIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) size_t nocow_bytes = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) * We will allocate space in case nodatacow is not set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) * so bail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) if (check_nocow_nolock(BTRFS_I(inode), pos, &nocow_bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) * There are holes in the range or parts of the range that must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) * be COWed (shared extents, RO block groups, etc), so just bail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) * out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) if (nocow_bytes < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) current->backing_dev_info = inode_to_bdi(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) err = file_remove_privs(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) * If BTRFS flips readonly due to some impossible error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) * although we have opened a file as writable, we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) * to stop this write operation to ensure FS consistency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) * We reserve space for updating the inode when we reserve space for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) * extent we are going to write, so we will enospc out there. We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) * need to start yet another transaction to update the inode as we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) * update the inode when we finish writing whatever data we write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) update_time_for_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) start_pos = round_down(pos, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) oldsize = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) if (start_pos > oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) /* Expand hole size to cover write data, preventing empty gap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) end_pos = round_up(pos + count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) err = btrfs_cont_expand(inode, oldsize, end_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (start_pos > round_up(oldsize, fs_info->sectorsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) clean_page = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) if (sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) atomic_inc(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) if (iocb->ki_flags & IOCB_DIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) * 1. We must always clear IOCB_DSYNC in order to not deadlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) * in iomap, as it calls generic_write_sync() in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) * 2. If we are async, we can call iomap_dio_complete() either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) * in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) * 2.1. A worker thread from the last bio completed. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) * case we need to mark the btrfs_dio_data that it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) * async in order to call generic_write_sync() properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) * This is handled by setting BTRFS_DIO_SYNC_STUB in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) * current->journal_info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) * 2.2 The submitter context, because all IO completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) * before we exited iomap_dio_rw(). In this case we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) * just re-set the IOCB_DSYNC on the iocb and we'll do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) * the sync below. If our ->end_io() gets called and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) * current->journal_info is set, then we know we're in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) * our current context and we will clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) * current->journal_info to indicate that we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) * sync below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) if (sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) ASSERT(current->journal_info == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) iocb->ki_flags &= ~IOCB_DSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) current->journal_info = BTRFS_DIO_SYNC_STUB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) num_written = __btrfs_direct_write(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) * As stated above, we cleared journal_info, so we need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) * the sync ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) if (sync && current->journal_info == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) iocb->ki_flags |= IOCB_DSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) current->journal_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) num_written = btrfs_buffered_write(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) if (num_written > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) iocb->ki_pos = pos + num_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) if (clean_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) pagecache_isize_extended(inode, oldsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) if (num_written > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) num_written = generic_write_sync(iocb, num_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) if (sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) atomic_dec(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) current->backing_dev_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return num_written ? num_written : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) int btrfs_release_file(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) struct btrfs_file_private *private = filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) if (private && private->filldir_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) kfree(private->filldir_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) kfree(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) filp->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) * Set by setattr when we are about to truncate a file from a non-zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) * size to a zero size. This tries to flush down new bytes that may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) * have been written if the application were using truncate to replace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) * a file in place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) if (test_and_clear_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) static int start_ordered_ops(struct inode *inode, loff_t start, loff_t end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) * This is only called in fsync, which would do synchronous writes, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) * a plug can merge adjacent IOs as much as possible. Esp. in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) * multiple disks using raid profile, a large IO can be split to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) * several segments of stripe length (currently 64K).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) atomic_inc(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) ret = btrfs_fdatawrite_range(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) atomic_dec(&BTRFS_I(inode)->sync_writers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) struct btrfs_inode *inode = BTRFS_I(ctx->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) struct btrfs_fs_info *fs_info = inode->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) if (btrfs_inode_in_log(inode, fs_info->generation) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) list_empty(&ctx->ordered_extents))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) * If we are doing a fast fsync we can not bail out if the inode's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) * last_trans is <= then the last committed transaction, because we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) * update the last_trans of the inode during ordered extent completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) * and for a fast fsync we don't wait for that, we only wait for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) * writeback to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) if (inode->last_trans <= fs_info->last_trans_committed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) list_empty(&ctx->ordered_extents)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) * fsync call for both files and directories. This logs the inode into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) * the tree log instead of forcing full commits whenever possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) * It needs to call filemap_fdatawait so that all ordered extent updates are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) * in the metadata btree are up to date for copying to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) * It drops the inode mutex before doing the tree log commit. This is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) * important optimization for directories because holding the mutex prevents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) * new operations on the dir while we write to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) struct dentry *dentry = file_dentry(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) struct btrfs_log_ctx ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) int ret = 0, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) bool full_sync;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) trace_btrfs_sync_file(file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) btrfs_init_log_ctx(&ctx, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) * Always set the range to a full range, otherwise we can get into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) * several problems, from missing file extent items to represent holes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) * when not using the NO_HOLES feature, to log tree corruption due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) * races between hole detection during logging and completion of ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) * extents outside the range, to missing checksums due to ordered extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) * for which we flushed only a subset of their pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) end = LLONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) len = (u64)LLONG_MAX + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) * We write the dirty pages in the range and wait until they complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) * out of the ->i_mutex. If so, we can flush the dirty pages by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) * multi-task, and make the performance up. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) * btrfs_wait_ordered_range for an explanation of the ASYNC check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) ret = start_ordered_ops(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) * We take the dio_sem here because the tree log stuff can race with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) * lockless dio writes and get an extent map logged for an extent we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) * never waited on. We need it this high up for lockdep reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) down_write(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) atomic_inc(&root->log_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) * Always check for the full sync flag while holding the inode's lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) * to avoid races with other tasks. The flag must be either set all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) * time during logging or always off all the time while logging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) * Before we acquired the inode's lock, someone may have dirtied more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) * pages in the target range. We need to make sure that writeback for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) * any such pages does not start while we are logging the inode, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) * if it does, any of the following might happen when we are not doing a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) * full inode sync:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) * 1) We log an extent after its writeback finishes but before its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) * checksums are added to the csum tree, leading to -EIO errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) * when attempting to read the extent after a log replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) * 2) We can end up logging an extent before its writeback finishes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) * Therefore after the log replay we will have a file extent item
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) * pointing to an unwritten extent (and no data checksums as well).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) * So trigger writeback for any eventual new dirty pages and then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) * wait for all ordered extents to complete below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) ret = start_ordered_ops(inode, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) up_write(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) * We have to do this here to avoid the priority inversion of waiting on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) * IO of a lower priority task while holding a transaction open.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) * For a full fsync we wait for the ordered extents to complete while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) * for a fast fsync we wait just for writeback to complete, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) * attach the ordered extents to the transaction so that a transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) * commit waits for their completion, to avoid data loss if we fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) * the current transaction commits before the ordered extents complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) * and a power failure happens right after that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) if (full_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) ret = btrfs_wait_ordered_range(inode, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) * Get our ordered extents as soon as possible to avoid doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) * checksum lookups in the csum tree, and use instead the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) * checksums attached to the ordered extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) btrfs_get_ordered_extents_for_logging(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) &ctx.ordered_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) ret = filemap_fdatawait_range(inode->i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) goto out_release_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) atomic_inc(&root->log_batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) if (skip_inode_logging(&ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) * We've had everything committed since the last time we were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) * modified so clear this flag in case it was set for whatever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) * reason, it's no longer relevant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) * An ordered extent might have started before and completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) * already with io errors, in which case the inode was not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) * updated and we end up here. So check the inode's mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) * for any errors that might have happened since we last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) * checked called fsync.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) ret = filemap_check_wb_err(inode->i_mapping, file->f_wb_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) goto out_release_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) * We use start here because we will need to wait on the IO to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) * in btrfs_sync_log, which could require joining a transaction (for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) * example checking cross references in the nocow path). If we use join
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) * here we could get into a situation where we're waiting on IO to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) * happen that is blocked on a transaction trying to commit. With start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) * we inc the extwriter counter, so we wait for all extwriters to exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) * before we start blocking joiners. This comment is to keep somebody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) * from thinking they are super smart and changing this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) * btrfs_join_transaction *cough*Josef*cough*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) trans = btrfs_start_transaction(root, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) goto out_release_extents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) ret = btrfs_log_dentry_safe(trans, dentry, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) btrfs_release_log_ctx_extents(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) /* Fallthrough and commit/free transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) /* we've logged all the items and now have a consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) * version of the file in the log. It is possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) * someone will come in and modify the file, but that's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) * fine because the log is consistent on disk, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) * have references to all of the file's extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) * It is possible that someone will come in and log the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) * file again, but that will end up using the synchronization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) * inside btrfs_sync_log to keep things safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) up_write(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) if (ret != BTRFS_NO_LOG_SYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) ret = btrfs_sync_log(trans, root, &ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) if (!full_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) ret = btrfs_wait_ordered_range(inode, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) ret = btrfs_commit_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) ret = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) ASSERT(list_empty(&ctx.list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) err = file_check_and_advance_wb_err(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) return ret > 0 ? -EIO : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) out_release_extents:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) btrfs_release_log_ctx_extents(&ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) up_write(&BTRFS_I(inode)->dio_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) static const struct vm_operations_struct btrfs_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) .fault = filemap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) .map_pages = filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) .page_mkwrite = btrfs_page_mkwrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) struct address_space *mapping = filp->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) if (!mapping->a_ops->readpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) file_accessed(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) vma->vm_ops = &btrfs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) static int hole_mergeable(struct btrfs_inode *inode, struct extent_buffer *leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) int slot, u64 start, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) if (slot < 0 || slot >= btrfs_header_nritems(leaf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) btrfs_item_key_to_cpu(leaf, &key, slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) if (key.objectid != btrfs_ino(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) key.type != BTRFS_EXTENT_DATA_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) if (btrfs_file_extent_disk_bytenr(leaf, fi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) if (key.offset == end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) static int fill_holes(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) struct btrfs_path *path, u64 offset, u64 end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) struct btrfs_fs_info *fs_info = trans->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) struct btrfs_root *root = inode->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) struct btrfs_file_extent_item *fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) struct extent_map *hole_em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) struct extent_map_tree *em_tree = &inode->extent_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) if (btrfs_fs_incompat(fs_info, NO_HOLES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) key.objectid = btrfs_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) key.offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) if (ret <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) * We should have dropped this offset, so if we find it then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) * something has gone horribly wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) if (hole_mergeable(inode, leaf, path->slots[0] - 1, offset, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) path->slots[0]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) end - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) btrfs_set_file_extent_offset(leaf, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) if (hole_mergeable(inode, leaf, path->slots[0], offset, end)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) u64 num_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) key.offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) btrfs_set_item_key_safe(fs_info, path, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) fi = btrfs_item_ptr(leaf, path->slots[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) btrfs_set_file_extent_offset(leaf, fi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) offset, 0, 0, end - offset, 0, end - offset, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) hole_em = alloc_extent_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) if (!hole_em) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) btrfs_drop_extent_cache(inode, offset, end - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) hole_em->start = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) hole_em->len = end - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) hole_em->ram_bytes = hole_em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) hole_em->orig_start = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) hole_em->block_start = EXTENT_MAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) hole_em->block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) hole_em->orig_block_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) hole_em->compress_type = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) hole_em->generation = trans->transid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) btrfs_drop_extent_cache(inode, offset, end - 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) write_lock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) ret = add_extent_mapping(em_tree, hole_em, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) write_unlock(&em_tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) } while (ret == -EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) free_extent_map(hole_em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) &inode->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) * Find a hole extent on given inode and change start/len to the end of hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) * extent.(hole/vacuum extent whose em->start <= start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) * em->start + em->len > start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) * When a hole extent is found, return 1 and modify start/len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) em = btrfs_get_extent(BTRFS_I(inode), NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) round_down(*start, fs_info->sectorsize),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) round_up(*len, fs_info->sectorsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) return PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) /* Hole or vacuum extent(only exists in no-hole mode) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) if (em->block_start == EXTENT_MAP_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) *len = em->start + em->len > *start + *len ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 0 : *start + *len - em->start - em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) *start = em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) static int btrfs_punch_hole_lock_range(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) const u64 lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) const u64 lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) struct extent_state **cached_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) truncate_pagecache_range(inode, lockstart, lockend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) lockend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) * We need to make sure we have no ordered extents in this range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) * and nobody raced in and read a page in this range, if we did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) * we need to try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) if ((!ordered ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) (ordered->file_offset + ordered->num_bytes <= lockstart ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) ordered->file_offset > lockend)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) !filemap_range_has_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) lockstart, lockend)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) if (ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) if (ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) lockend, cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) ret = btrfs_wait_ordered_range(inode, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) lockend - lockstart + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) static int btrfs_insert_replace_extent(struct btrfs_trans_handle *trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) struct btrfs_replace_extent_info *extent_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) const u64 replace_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) struct btrfs_file_extent_item *extent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) struct extent_buffer *leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) struct btrfs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) struct btrfs_ref ref = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) if (replace_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) if (extent_info->disk_offset == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) btrfs_fs_incompat(fs_info, NO_HOLES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) key.objectid = btrfs_ino(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) key.type = BTRFS_EXTENT_DATA_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) key.offset = extent_info->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) ret = btrfs_insert_empty_item(trans, root, path, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) sizeof(struct btrfs_file_extent_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) leaf = path->nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) slot = path->slots[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) write_extent_buffer(leaf, extent_info->extent_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) btrfs_item_ptr_offset(leaf, slot),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) sizeof(struct btrfs_file_extent_item));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) extent = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) ASSERT(btrfs_file_extent_type(leaf, extent) != BTRFS_FILE_EXTENT_INLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) btrfs_set_file_extent_offset(leaf, extent, extent_info->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) btrfs_set_file_extent_num_bytes(leaf, extent, replace_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) if (extent_info->is_new_extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) btrfs_set_file_extent_generation(leaf, extent, trans->transid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) btrfs_mark_buffer_dirty(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) btrfs_release_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) extent_info->file_offset, replace_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) /* If it's a hole, nothing more needs to be done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) if (extent_info->disk_offset == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) inode_add_bytes(inode, replace_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) if (extent_info->is_new_extent && extent_info->insertions == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) key.objectid = extent_info->disk_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) key.type = BTRFS_EXTENT_ITEM_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) key.offset = extent_info->disk_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) ret = btrfs_alloc_reserved_file_extent(trans, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) btrfs_ino(BTRFS_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) extent_info->file_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) extent_info->qgroup_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) u64 ref_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) extent_info->disk_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) extent_info->disk_len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) ref_offset = extent_info->file_offset - extent_info->data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) btrfs_init_data_ref(&ref, root->root_key.objectid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) btrfs_ino(BTRFS_I(inode)), ref_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) ret = btrfs_inc_extent_ref(trans, &ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) extent_info->insertions++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) * The respective range must have been previously locked, as well as the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) * The end offset is inclusive (last byte of the range).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) * @extent_info is NULL for fallocate's hole punching and non-NULL when replacing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) * the file range with an extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) * When not punching a hole, we don't want to end up in a state where we dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) * extents without inserting a new one, so we must abort the transaction to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) * a corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) int btrfs_replace_file_extents(struct inode *inode, struct btrfs_path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) const u64 start, const u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) struct btrfs_replace_extent_info *extent_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) struct btrfs_trans_handle **trans_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) u64 min_size = btrfs_calc_insert_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) u64 ino_size = round_up(inode->i_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) struct btrfs_block_rsv *rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) unsigned int rsv_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) u64 cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) u64 drop_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) u64 len = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) if (end <= start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) if (!rsv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) rsv->size = btrfs_calc_insert_metadata_size(fs_info, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) rsv->failfast = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) * 1 - update the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) * 1 - removing the extents in the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) * 1 - adding the hole extent if no_holes isn't set or if we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) * replacing the range with a new extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) if (!btrfs_fs_incompat(fs_info, NO_HOLES) || extent_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) rsv_count = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) rsv_count = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) trans = btrfs_start_transaction(root, rsv_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) min_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) cur_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) while (cur_offset < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) cur_offset, end + 1, &drop_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 1, 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) if (ret != -ENOSPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) * The only time we don't want to abort is if we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) * attempting to clone a partial inline extent, in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) * case we'll get EOPNOTSUPP. However if we aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) * clone we need to abort no matter what, because if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) * got EOPNOTSUPP via prealloc then we messed up and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) * need to abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) if (ret &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) (ret != -EOPNOTSUPP ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) (extent_info && extent_info->is_new_extent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) if (!extent_info && cur_offset < drop_end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) cur_offset < ino_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) ret = fill_holes(trans, BTRFS_I(inode), path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) cur_offset, drop_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) * If we failed then we didn't insert our hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) * entries for the area we dropped, so now the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) * fs is corrupted, so we must abort the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) * transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) } else if (!extent_info && cur_offset < drop_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) * We are past the i_size here, but since we didn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) * insert holes we need to clear the mapped area so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) * know to not set disk_i_size in this area until a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) * file extent is inserted here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) cur_offset, drop_end - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) * We couldn't clear our area, so we could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) * presumably adjust up and corrupt the fs, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) * we need to abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) if (extent_info && drop_end > extent_info->file_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) u64 replace_len = drop_end - extent_info->file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) ret = btrfs_insert_replace_extent(trans, inode, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) extent_info, replace_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) extent_info->data_len -= replace_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) extent_info->data_offset += replace_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) extent_info->file_offset += replace_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) cur_offset = drop_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) trans = btrfs_start_transaction(root, rsv_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) rsv, min_size, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) BUG_ON(ret); /* shouldn't happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) trans->block_rsv = rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) if (!extent_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) ret = find_first_non_hole(inode, &cur_offset, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) if (unlikely(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) if (ret && !len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) * If we were cloning, force the next fsync to be a full one since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) * we replaced (or just dropped in the case of cloning holes when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) * NO_HOLES is enabled) extents and extent maps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) * This is for the sake of simplicity, and cloning into files larger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) * than 16Mb would force the full fsync any way (when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) * try_release_extent_mapping() is invoked during page cache truncation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) if (extent_info && !extent_info->is_new_extent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) &BTRFS_I(inode)->runtime_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) * If we are using the NO_HOLES feature we might have had already an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) * hole that overlaps a part of the region [lockstart, lockend] and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) * ends at (or beyond) lockend. Since we have no file extent items to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) * represent holes, drop_end can be less than lockend and so we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) * make sure we have an extent map representing the existing hole (the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) * call to __btrfs_drop_extents() might have dropped the existing extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) * map representing the existing hole), otherwise the fast fsync path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) * will not record the existence of the hole region
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) * [existing_hole_start, lockend].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) if (drop_end <= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) drop_end = end + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) * Don't insert file hole extent item if it's for a range beyond eof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) * (because it's useless) or if it represents a 0 bytes range (when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) * cur_offset == drop_end).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) if (!extent_info && cur_offset < ino_size && cur_offset < drop_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) ret = fill_holes(trans, BTRFS_I(inode), path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) cur_offset, drop_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) /* Same comment as above. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) } else if (!extent_info && cur_offset < drop_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) /* See the comment in the loop above for the reasoning here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) cur_offset, drop_end - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) if (extent_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) ret = btrfs_insert_replace_extent(trans, inode, path, extent_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) extent_info->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) btrfs_abort_transaction(trans, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) goto out_trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) out_trans:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) if (!trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) trans->block_rsv = &fs_info->trans_block_rsv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) *trans_out = trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) btrfs_free_block_rsv(fs_info, rsv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) struct btrfs_path *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) struct btrfs_trans_handle *trans = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) u64 lockstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) u64 lockend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) u64 tail_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) u64 tail_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) u64 orig_start = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) bool same_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) u64 ino_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) bool truncated_block = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) bool updated_inode = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) ret = btrfs_wait_ordered_range(inode, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) ino_size = round_up(inode->i_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) ret = find_first_non_hole(inode, &offset, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) if (ret && !len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) /* Already in a large hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) lockend = round_down(offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * We needn't truncate any block which is beyond the end of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * because we are sure there is no data there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) * Only do this if we are in the same block and we aren't doing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) * entire block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) if (same_block && len < fs_info->sectorsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) if (offset < ino_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) truncated_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) ret = btrfs_truncate_block(inode, offset, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) /* zero back part of the first block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) if (offset < ino_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) truncated_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) ret = btrfs_truncate_block(inode, offset, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) /* Check the aligned pages after the first unaligned page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) * if offset != orig_start, which means the first unaligned page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) * including several following pages are already in holes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) * the extra check can be skipped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) if (offset == orig_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) /* after truncate page, check hole again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) len = offset + len - lockstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) offset = lockstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) ret = find_first_non_hole(inode, &offset, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) if (ret && !len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) lockstart = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) /* Check the tail unaligned part is in a hole */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) tail_start = lockend + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) tail_len = offset + len - tail_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) if (tail_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) ret = find_first_non_hole(inode, &tail_start, &tail_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) if (unlikely(ret < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) /* zero the front end of the last page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) if (tail_start + tail_len < ino_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) truncated_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) ret = btrfs_truncate_block(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) tail_start + tail_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) if (lockend < lockstart) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) goto out_only_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) path = btrfs_alloc_path();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) if (!path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) ret = btrfs_replace_file_extents(inode, path, lockstart, lockend, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) &trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) btrfs_free_path(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) ASSERT(trans != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) updated_inode = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) btrfs_btree_balance_dirty(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) out_only_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) if (!updated_inode && truncated_block && !ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) * If we only end up zeroing part of a page, we still need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) * update the inode item, so that all the time fields are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) * updated as well as the necessary btrfs inode in memory fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) * for detecting, at fsync time, if the inode isn't yet in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) * log tree or it's there but not up to date.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) struct timespec64 now = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) inode->i_mtime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) inode->i_ctime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) if (IS_ERR(trans)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) ret = PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) ret2 = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) /* Helper structure to record which range is already reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) struct falloc_range {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) * Helper function to add falloc range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) * Caller should have locked the larger range of extent containing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) * [start, len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) static int add_falloc_range(struct list_head *head, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) struct falloc_range *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) struct falloc_range *range = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) if (list_empty(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) goto insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) * As fallocate iterate by bytenr order, we only need to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) * the last range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) prev = list_entry(head->prev, struct falloc_range, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) if (prev->start + prev->len == start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) prev->len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) insert:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) range = kmalloc(sizeof(*range), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) if (!range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) range->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) range->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) list_add_tail(&range->list, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) static int btrfs_fallocate_update_isize(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) const u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) const int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) struct btrfs_trans_handle *trans;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) struct btrfs_root *root = BTRFS_I(inode)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) int ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) trans = btrfs_start_transaction(root, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) if (IS_ERR(trans))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) return PTR_ERR(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) i_size_write(inode, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) btrfs_inode_safe_disk_i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) ret = btrfs_update_inode(trans, root, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) ret2 = btrfs_end_transaction(trans);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) return ret ? ret : ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) RANGE_BOUNDARY_WRITTEN_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) RANGE_BOUNDARY_PREALLOC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) RANGE_BOUNDARY_HOLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) static int btrfs_zero_range_check_range_boundary(struct btrfs_inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) u64 offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) const u64 sectorsize = btrfs_inode_sectorsize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) offset = round_down(offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) em = btrfs_get_extent(inode, NULL, 0, offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) if (IS_ERR(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) return PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) if (em->block_start == EXTENT_MAP_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) ret = RANGE_BOUNDARY_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) else if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) ret = RANGE_BOUNDARY_PREALLOC_EXTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) ret = RANGE_BOUNDARY_WRITTEN_EXTENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) static int btrfs_zero_range(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) loff_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) const int mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) u64 alloc_hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) const u64 sectorsize = btrfs_inode_sectorsize(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) u64 alloc_start = round_down(offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) u64 alloc_end = round_up(offset + len, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) u64 bytes_to_reserve = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) bool space_reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) alloc_end - alloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) * Avoid hole punching and extent allocation for some cases. More cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) * could be considered, but these are unlikely common and we keep things
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) * as simple as possible for now. Also, intentionally, if the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) * range contains one or more prealloc extents together with regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) * extents and holes, we drop all the existing extents and allocate a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) * new prealloc extent, so that we get a larger contiguous disk extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) if (em->start <= alloc_start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) const u64 em_end = em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) if (em_end >= offset + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) * The whole range is already a prealloc extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) * do nothing except updating the inode's i_size if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) * needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) ret = btrfs_fallocate_update_isize(inode, offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) * Part of the range is already a prealloc extent, so operate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) * only on the remaining part of the range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) alloc_start = em_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) ASSERT(IS_ALIGNED(alloc_start, sectorsize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) len = offset + len - alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) offset = alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) alloc_hint = em->block_start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) if (BTRFS_BYTES_TO_BLKS(fs_info, offset) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) ret = btrfs_fallocate_update_isize(inode, offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) if (len < sectorsize && em->block_start != EXTENT_MAP_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) ret = btrfs_truncate_block(inode, offset, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) ret = btrfs_fallocate_update_isize(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) alloc_start = round_down(offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) alloc_end = alloc_start + sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) goto reserve_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) alloc_start = round_up(offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) alloc_end = round_down(offset + len, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) * For unaligned ranges, check the pages at the boundaries, they might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) * map to an extent, in which case we need to partially zero them, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) * they might map to a hole, in which case we need our allocation range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) * to cover them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) if (!IS_ALIGNED(offset, sectorsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) if (ret == RANGE_BOUNDARY_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) alloc_start = round_down(offset, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) ret = btrfs_truncate_block(inode, offset, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) if (!IS_ALIGNED(offset + len, sectorsize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) ret = btrfs_zero_range_check_range_boundary(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) if (ret == RANGE_BOUNDARY_HOLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) alloc_end = round_up(offset + len, sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) } else if (ret == RANGE_BOUNDARY_WRITTEN_EXTENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) ret = btrfs_truncate_block(inode, offset + len, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) reserve_space:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) if (alloc_start < alloc_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) const u64 lockstart = alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) const u64 lockend = alloc_end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) bytes_to_reserve = alloc_end - alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) bytes_to_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) space_reserved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) ret = btrfs_punch_hole_lock_range(inode, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) ret = btrfs_qgroup_reserve_data(BTRFS_I(inode), &data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) alloc_start, bytes_to_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) lockend, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) ret = btrfs_prealloc_file_range(inode, mode, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) alloc_end - alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) i_blocksize(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) offset + len, &alloc_hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) lockend, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) /* btrfs_prealloc_file_range releases reserved space on error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) space_reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) ret = btrfs_fallocate_update_isize(inode, offset + len, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) if (ret && space_reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) alloc_start, bytes_to_reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) static long btrfs_fallocate(struct file *file, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) loff_t offset, loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) struct extent_changeset *data_reserved = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) struct falloc_range *range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) struct falloc_range *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) struct list_head reserve_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) u64 cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) u64 last_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) u64 alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) u64 alloc_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) u64 alloc_hint = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) u64 locked_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) u64 actual_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) int blocksize = btrfs_inode_sectorsize(BTRFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) alloc_start = round_down(offset, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) alloc_end = round_up(offset + len, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) cur_offset = alloc_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) /* Make sure we aren't being give some crap mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) FALLOC_FL_ZERO_RANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) if (mode & FALLOC_FL_PUNCH_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) return btrfs_punch_hole(inode, offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) * Only trigger disk allocation, don't trigger qgroup reserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) * For qgroup space, it will be checked later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) if (!(mode & FALLOC_FL_ZERO_RANGE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) alloc_end - alloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) ret = inode_newsize_ok(inode, offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) * TODO: Move these two operations after we have checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) * accurate reserved space, or fallocate can still fail but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) * with page truncated or size expanded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) * But that's a minor problem and won't do much harm BTW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) if (alloc_start > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) ret = btrfs_cont_expand(inode, i_size_read(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) alloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) } else if (offset + len > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) * If we are fallocating from the end of the file onward we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) * need to zero out the end of the block if i_size lands in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) * middle of a block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) * wait for ordered IO before we have any locks. We'll loop again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) * below with the locks held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) ret = btrfs_wait_ordered_range(inode, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) alloc_end - alloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) if (mode & FALLOC_FL_ZERO_RANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) ret = btrfs_zero_range(inode, offset, len, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) locked_end = alloc_end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) struct btrfs_ordered_extent *ordered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) /* the extent lock is ordered inside the running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) * transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) locked_end, &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) locked_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) if (ordered &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) ordered->file_offset + ordered->num_bytes > alloc_start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) ordered->file_offset < alloc_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) unlock_extent_cached(&BTRFS_I(inode)->io_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) alloc_start, locked_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) * we can't wait on the range with the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) * running or with the extent lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) ret = btrfs_wait_ordered_range(inode, alloc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) alloc_end - alloc_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) if (ordered)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) btrfs_put_ordered_extent(ordered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) /* First, check if we exceed the qgroup limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) INIT_LIST_HEAD(&reserve_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) while (cur_offset < alloc_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) alloc_end - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) last_byte = min(extent_map_end(em), alloc_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) actual_end = min_t(u64, extent_map_end(em), offset + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) last_byte = ALIGN(last_byte, blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) if (em->block_start == EXTENT_MAP_HOLE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) (cur_offset >= inode->i_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) ret = add_falloc_range(&reserve_list, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) last_byte - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) ret = btrfs_qgroup_reserve_data(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) &data_reserved, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) last_byte - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) cur_offset = last_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) * Do not need to reserve unwritten extent for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) * range, free reserved data space first, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) * it'll result in false ENOSPC error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) btrfs_free_reserved_data_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) data_reserved, cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) last_byte - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) cur_offset = last_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) * If ret is still 0, means we're OK to fallocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) * Or just cleanup the list and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) list_for_each_entry_safe(range, tmp, &reserve_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) ret = btrfs_prealloc_file_range(inode, mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) range->len, i_blocksize(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) offset + len, &alloc_hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) btrfs_free_reserved_data_space(BTRFS_I(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) data_reserved, range->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) range->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) list_del(&range->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) kfree(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) * We didn't need to allocate any more space, but we still extended the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) * size of the file so we need to update i_size and the inode item.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) ret = btrfs_fallocate_update_isize(inode, actual_end, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) /* Let go of our reservation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) if (ret != 0 && !(mode & FALLOC_FL_ZERO_RANGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) btrfs_free_reserved_data_space(BTRFS_I(inode), data_reserved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) cur_offset, alloc_end - cur_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) extent_changeset_free(data_reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) static loff_t find_desired_extent(struct inode *inode, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) struct extent_map *em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) struct extent_state *cached_state = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) loff_t i_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) u64 lockstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) u64 lockend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) u64 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) if (i_size == 0 || offset >= i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) * offset can be negative, in this case we start finding DATA/HOLE from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) * the very start of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) start = max_t(loff_t, 0, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) lockstart = round_down(start, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) lockend = round_up(i_size, fs_info->sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) if (lockend <= lockstart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) lockend = lockstart + fs_info->sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) lockend--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) len = lockend - lockstart + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) while (start < i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) em = btrfs_get_extent_fiemap(BTRFS_I(inode), start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) if (IS_ERR(em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) ret = PTR_ERR(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) if (whence == SEEK_HOLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) (em->block_start == EXTENT_MAP_HOLE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) else if (whence == SEEK_DATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) (em->block_start != EXTENT_MAP_HOLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) !test_bit(EXTENT_FLAG_PREALLOC, &em->flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) start = em->start + em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) em = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) &cached_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) offset = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) if (whence == SEEK_DATA && start >= i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) offset = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) offset = min_t(loff_t, start, i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int whence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) switch (whence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) return generic_file_llseek(file, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) case SEEK_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) case SEEK_HOLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) offset = find_desired_extent(inode, offset, whence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) if (offset < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) static int btrfs_file_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) return generic_file_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) static ssize_t btrfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) ssize_t ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) if (iocb->ki_flags & IOCB_DIRECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) ret = btrfs_direct_IO(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) if (ret < 0 || !iov_iter_count(to) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) iocb->ki_pos >= i_size_read(file_inode(iocb->ki_filp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) return generic_file_buffered_read(iocb, to, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) const struct file_operations btrfs_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) .llseek = btrfs_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) .read_iter = btrfs_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) .splice_read = generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) .write_iter = btrfs_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) .splice_write = iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) .mmap = btrfs_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) .open = btrfs_file_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) .release = btrfs_release_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) .fsync = btrfs_sync_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) .fallocate = btrfs_fallocate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) .unlocked_ioctl = btrfs_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) .compat_ioctl = btrfs_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) .remap_file_range = btrfs_remap_file_range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) void __cold btrfs_auto_defrag_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) kmem_cache_destroy(btrfs_inode_defrag_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) int __init btrfs_auto_defrag_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) btrfs_inode_defrag_cachep = kmem_cache_create("btrfs_inode_defrag",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) sizeof(struct inode_defrag), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) SLAB_MEM_SPREAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) if (!btrfs_inode_defrag_cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) * So with compression we will find and lock a dirty page and clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) * first one as dirty, setup an async extent, and immediately return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) * with the entire range locked but with nobody actually marked with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) * writeback. So we can't just filemap_write_and_wait_range() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) * expect it to work since it will just kick off a thread to do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) * actual work. So we need to call filemap_fdatawrite_range _again_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) * since it will wait on the page lock, which won't be unlocked until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) * after the pages have been marked as writeback and so we're good to go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) * from there. We have to do this otherwise we'll miss the ordered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) * extents and that results in badness. Please Josef, do not think you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) * know better and pull this out at some point in the future, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) * right and you are wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) &BTRFS_I(inode)->runtime_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) }