+static int ldiskfs_ext_new_extent_cb(struct inode *inode,
+ struct ldiskfs_ext_path *path,
+ struct ldiskfs_ext_cache *cex,
+#ifdef HAVE_EXT_PREPARE_CB_EXTENT
+ struct ldiskfs_extent *ex,
+#endif
+ void *cbdata)
+{
+ struct bpointers *bp = cbdata;
+ struct ldiskfs_extent nex;
+ unsigned long pblock;
+ unsigned long tgen;
+ int err, i;
+ unsigned long count;
+ handle_t *handle;
+
+#ifdef LDISKFS_EXT_CACHE_EXTENT /* until kernel 2.6.37 */
+ if (cex->ec_type == LDISKFS_EXT_CACHE_EXTENT) {
+#else
+ if ((cex->ec_len != 0) && (cex->ec_start != 0)) {
+#endif
+ err = EXT_CONTINUE;
+ goto map;
+ }
+
+ if (bp->create == 0) {
+ i = 0;
+ if (cex->ec_block < bp->start)
+ i = bp->start - cex->ec_block;
+ if (i >= cex->ec_len)
+ CERROR("nothing to do?! i = %d, e_num = %u\n",
+ i, cex->ec_len);
+ for (; i < cex->ec_len && bp->num; i++) {
+ *(bp->blocks) = 0;
+ bp->blocks++;
+ bp->num--;
+ bp->start++;
+ }
+
+ return EXT_CONTINUE;
+ }
+
+ tgen = LDISKFS_I(inode)->i_ext_generation;
+ count = ldiskfs_ext_calc_credits_for_insert(inode, path);
+
+ handle = osd_journal_start(inode, LDISKFS_HT_MISC,
+ count + LDISKFS_ALLOC_NEEDED + 1);
+ if (IS_ERR(handle)) {
+ return PTR_ERR(handle);
+ }
+
+ if (tgen != LDISKFS_I(inode)->i_ext_generation) {
+ /* the tree has changed. so path can be invalid at moment */
+ ldiskfs_journal_stop(handle);
+ return EXT_REPEAT;
+ }
+
+ /* In 2.6.32 kernel, ldiskfs_ext_walk_space()'s callback func is not
+ * protected by i_data_sem as whole. so we patch it to store
+ * generation to path and now verify the tree hasn't changed */
+ down_write((&LDISKFS_I(inode)->i_data_sem));
+
+ /* validate extent, make sure the extent tree does not changed */
+ if (LDISKFS_I(inode)->i_ext_generation != path[0].p_generation) {
+ /* cex is invalid, try again */
+ up_write(&LDISKFS_I(inode)->i_data_sem);
+ ldiskfs_journal_stop(handle);
+ return EXT_REPEAT;
+ }
+
+ count = cex->ec_len;
+ pblock = new_blocks(handle, inode, path, cex->ec_block, &count, &err);
+ if (!pblock)
+ goto out;
+ BUG_ON(count > cex->ec_len);
+
+ /* insert new extent */
+ nex.ee_block = cpu_to_le32(cex->ec_block);
+ ldiskfs_ext_store_pblock(&nex, pblock);
+ nex.ee_len = cpu_to_le16(count);
+ err = ldiskfs_ext_insert_extent(handle, inode, path, &nex, 0);
+ if (err) {
+ /* free data blocks we just allocated */
+ /* not a good idea to call discard here directly,
+ * but otherwise we'd need to call it every free() */
+ ldiskfs_discard_preallocations(inode);
+#ifdef HAVE_EXT_FREE_BLOCK_WITH_BUFFER_HEAD /* Introduced in 2.6.32-rc7 */
+ ldiskfs_free_blocks(handle, inode, NULL,
+ ldiskfs_ext_pblock(&nex),
+ le16_to_cpu(nex.ee_len), 0);
+#else
+ ldiskfs_free_blocks(handle, inode, ldiskfs_ext_pblock(&nex),
+ le16_to_cpu(nex.ee_len), 0);
+#endif
+ goto out;
+ }
+
+ /*
+ * Putting len of the actual extent we just inserted,
+ * we are asking ldiskfs_ext_walk_space() to continue
+ * scaning after that block
+ */
+ cex->ec_len = le16_to_cpu(nex.ee_len);
+ cex->ec_start = ldiskfs_ext_pblock(&nex);
+ BUG_ON(le16_to_cpu(nex.ee_len) == 0);
+ BUG_ON(le32_to_cpu(nex.ee_block) != cex->ec_block);
+
+out:
+ up_write((&LDISKFS_I(inode)->i_data_sem));
+ ldiskfs_journal_stop(handle);
+map:
+ if (err >= 0) {
+ /* map blocks */
+ if (bp->num == 0) {
+ CERROR("hmm. why do we find this extent?\n");
+ CERROR("initial space: %lu:%u\n",
+ bp->start, bp->init_num);
+#ifdef LDISKFS_EXT_CACHE_EXTENT /* until kernel 2.6.37 */
+ CERROR("current extent: %u/%u/%llu %d\n",
+ cex->ec_block, cex->ec_len,
+ (unsigned long long)cex->ec_start,
+ cex->ec_type);
+#else
+ CERROR("current extent: %u/%u/%llu\n",
+ cex->ec_block, cex->ec_len,
+ (unsigned long long)cex->ec_start);
+#endif
+ }
+ i = 0;
+ if (cex->ec_block < bp->start)
+ i = bp->start - cex->ec_block;
+ if (i >= cex->ec_len)
+ CERROR("nothing to do?! i = %d, e_num = %u\n",
+ i, cex->ec_len);
+ for (; i < cex->ec_len && bp->num; i++) {
+ *(bp->blocks) = cex->ec_start + i;
+#ifdef LDISKFS_EXT_CACHE_EXTENT /* until kernel 2.6.37 */
+ if (cex->ec_type != LDISKFS_EXT_CACHE_EXTENT) {
+#else
+ if ((cex->ec_len == 0) || (cex->ec_start == 0)) {
+#endif
+ /* unmap any possible underlying metadata from
+ * the block device mapping. bug 6998. */
+ unmap_underlying_metadata(inode->i_sb->s_bdev,
+ *(bp->blocks));
+ }
+ bp->blocks++;
+ bp->num--;
+ bp->start++;
+ }
+ }
+ return err;
+}