// SPDX-License-Identifier: GPL-2.0-only
/*
* This file is part of UBIFS.
*
* Copyright (C) 2006-2008 Nokia Corporation.
*
* Authors: Adrian Hunter
* Artem Bityutskiy (Битюцкий Артём)
*/
/* This file implements TNC functions for committing */
#include <linux/random.h>
#include "ubifs.h"
/**
* make_idx_node - make an index node for fill-the-gaps method of TNC commit.
* @c: UBIFS file-system description object
* @idx: buffer in which to place new index node
* @znode: znode from which to make new index node
* @lnum: LEB number where new index node will be written
* @offs: offset where new index node will be written
* @len: length of new index node
*/
static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
struct ubifs_znode *znode, int lnum, int offs, int len)
{
struct ubifs_znode *zp;
u8 hash[UBIFS_HASH_ARR_SZ];
int i, err;
/* Make index node */
idx->ch.node_type = UBIFS_IDX_NODE;
idx->child_cnt = cpu_to_le16(znode->child_cnt);
idx->level = cpu_to_le16(znode->level);
for (i = 0; i < znode->child_cnt; i++) {
struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
struct ubifs_zbranch *zbr = &znode->zbranch[i];
key_write_idx(c, &zbr->key, &br->key);
br->lnum = cpu_to_le32(zbr->lnum);
br->offs = cpu_to_le32(zbr->offs);
br->len = cpu_to_le32(zbr->len);
ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
if (!zbr->lnum || !zbr->len) {
ubifs_err(c, "bad ref in znode");
ubifs_dump_znode(c, znode);
if (zbr->znode)
ubifs_dump_znode(c, zbr->znode);
return -EINVAL;
}
}
ubifs_prepare_node(c, idx, len, 0);
ubifs_node_calc_hash(c, idx, hash);
znode->lnum = lnum;
znode->offs = offs;
znode->len = len;
err = insert_old_idx_znode(c, znode);
/* Update the parent */
zp = znode->parent;
if (zp) {
struct ubifs_zbranch *zbr;
zbr = &zp->zbranch[znode->iip];
zbr->lnum = lnum;
zbr->offs = offs;
zbr->len = len;
ubifs_copy_hash(c, hash, zbr->hash);
} else {
c->zroot.lnum = lnum;
c->zroot.offs = offs;
c->zroot.len = len;
ubifs_copy_hash(c, hash, c->zroot.hash);
}
c->calc_idx_sz += ALIGN(len, 8);
atomic_long_dec(&c->dirty_zn_cnt);
ubifs_assert(c, ubifs_zn_dirty(znode));
ubifs_assert(c, ubifs_zn_cow(znode));
/*
* Note, unlike 'write_index()' we do not add memory barriers here
* because this function is called with @c->tnc_mutex locked.
*/
__clear_bit(DIRTY_ZNODE, &znode->flags);
__clear_bit(COW_ZNODE, &znode->flags);
return err;
}
/**
* fill_gap - make index nodes in gaps in dirty index LEBs.
* @c: UBIFS file-system description object
* @lnum: LEB number that gap appears in
* @gap_start: offset of start of gap
* @gap_end: offset of end of gap
* @dirt: adds dirty space to this
*
* This function returns the number of index nodes written into the gap.
*/
static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
int *dirt)
{
int len, gap_remains, gap_pos, written, pad_len;
ubifs_assert(c, (gap_start & 7) == 0);
ubifs_assert(c, (gap_end & 7) == 0);
ubifs_assert(c, gap_end >= gap_start);
gap_remains = gap_end - gap_start;
if (!gap_remains)
return 0;
gap_pos = gap_start;
written = 0;
while (c->enext) {
len = ubifs_idx_node_sz(c, c->enext->child_cnt);
if (len < gap_remains) {
struct ubifs_znode *znode = c->enext;
const int alen = ALIGN(len, 8);
int err;
ubifs_assert(c, alen <= gap_remains);
err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
lnum, gap_pos, len);
if (err)
return err;
gap_remains -= alen;
gap_pos += alen;
c->enext = znode->cnext;
if (c->