18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of UBIFS.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2006-2008 Nokia Corporation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём)
88c2ecf20Sopenharmony_ci *          Adrian Hunter
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file implements UBIFS journal.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
158c2ecf20Sopenharmony_ci * length and position, while a bud logical eraseblock is any LEB in the main
168c2ecf20Sopenharmony_ci * area. Buds contain file system data - data nodes, inode nodes, etc. The log
178c2ecf20Sopenharmony_ci * contains only references to buds and some other stuff like commit
188c2ecf20Sopenharmony_ci * start node. The idea is that when we commit the journal, we do
198c2ecf20Sopenharmony_ci * not copy the data, the buds just become indexed. Since after the commit the
208c2ecf20Sopenharmony_ci * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
218c2ecf20Sopenharmony_ci * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
228c2ecf20Sopenharmony_ci * become leafs in the future.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * The journal is multi-headed because we want to write data to the journal as
258c2ecf20Sopenharmony_ci * optimally as possible. It is nice to have nodes belonging to the same inode
268c2ecf20Sopenharmony_ci * in one LEB, so we may write data owned by different inodes to different
278c2ecf20Sopenharmony_ci * journal heads, although at present only one data head is used.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * For recovery reasons, the base head contains all inode nodes, all directory
308c2ecf20Sopenharmony_ci * entry nodes and all truncate nodes. This means that the other heads contain
318c2ecf20Sopenharmony_ci * only data nodes.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * Bud LEBs may be half-indexed. For example, if the bud was not full at the
348c2ecf20Sopenharmony_ci * time of commit, the bud is retained to continue to be used in the journal,
358c2ecf20Sopenharmony_ci * even though the "front" of the LEB is now indexed. In that case, the log
368c2ecf20Sopenharmony_ci * reference contains the offset where the bud starts for the purposes of the
378c2ecf20Sopenharmony_ci * journal.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * The journal size has to be limited, because the larger is the journal, the
408c2ecf20Sopenharmony_ci * longer it takes to mount UBIFS (scanning the journal) and the more memory it
418c2ecf20Sopenharmony_ci * takes (indexing in the TNC).
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * All the journal write operations like 'ubifs_jnl_update()' here, which write
448c2ecf20Sopenharmony_ci * multiple UBIFS nodes to the journal at one go, are atomic with respect to
458c2ecf20Sopenharmony_ci * unclean reboots. Should the unclean reboot happen, the recovery code drops
468c2ecf20Sopenharmony_ci * all the nodes.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#include "ubifs.h"
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
538c2ecf20Sopenharmony_ci * @ino: the inode to zero out
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_cistatic inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	memset(ino->padding1, 0, 4);
588c2ecf20Sopenharmony_ci	memset(ino->padding2, 0, 26);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci * zero_dent_node_unused - zero out unused fields of an on-flash directory
638c2ecf20Sopenharmony_ci *                         entry node.
648c2ecf20Sopenharmony_ci * @dent: the directory entry to zero out
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistatic inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	dent->padding1 = 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/**
728c2ecf20Sopenharmony_ci * zero_trun_node_unused - zero out unused fields of an on-flash truncation
738c2ecf20Sopenharmony_ci *                         node.
748c2ecf20Sopenharmony_ci * @trun: the truncation node to zero out
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_cistatic inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	memset(trun->padding, 0, 12);
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_cistatic void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c))
848c2ecf20Sopenharmony_ci		ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci * reserve_space - reserve space in the journal.
898c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
908c2ecf20Sopenharmony_ci * @jhead: journal head number
918c2ecf20Sopenharmony_ci * @len: node length
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * This function reserves space in journal head @head. If the reservation
948c2ecf20Sopenharmony_ci * succeeded, the journal head stays locked and later has to be unlocked using
958c2ecf20Sopenharmony_ci * 'release_head()'. Returns zero in case of success, %-EAGAIN if commit has to
968c2ecf20Sopenharmony_ci * be done, and other negative error codes in case of other failures.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_cistatic int reserve_space(struct ubifs_info *c, int jhead, int len)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
1018c2ecf20Sopenharmony_ci	struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/*
1048c2ecf20Sopenharmony_ci	 * Typically, the base head has smaller nodes written to it, so it is
1058c2ecf20Sopenharmony_ci	 * better to try to allocate space at the ends of eraseblocks. This is
1068c2ecf20Sopenharmony_ci	 * what the squeeze parameter does.
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	ubifs_assert(c, !c->ro_media && !c->ro_mount);
1098c2ecf20Sopenharmony_ci	squeeze = (jhead == BASEHD);
1108c2ecf20Sopenharmony_ciagain:
1118c2ecf20Sopenharmony_ci	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (c->ro_error) {
1148c2ecf20Sopenharmony_ci		err = -EROFS;
1158c2ecf20Sopenharmony_ci		goto out_unlock;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	avail = c->leb_size - wbuf->offs - wbuf->used;
1198c2ecf20Sopenharmony_ci	if (wbuf->lnum != -1 && avail >= len)
1208c2ecf20Sopenharmony_ci		return 0;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * Write buffer wasn't seek'ed or there is no enough space - look for an
1248c2ecf20Sopenharmony_ci	 * LEB with some empty space.
1258c2ecf20Sopenharmony_ci	 */
1268c2ecf20Sopenharmony_ci	lnum = ubifs_find_free_space(c, len, &offs, squeeze);
1278c2ecf20Sopenharmony_ci	if (lnum >= 0)
1288c2ecf20Sopenharmony_ci		goto out;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	err = lnum;
1318c2ecf20Sopenharmony_ci	if (err != -ENOSPC)
1328c2ecf20Sopenharmony_ci		goto out_unlock;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/*
1358c2ecf20Sopenharmony_ci	 * No free space, we have to run garbage collector to make
1368c2ecf20Sopenharmony_ci	 * some. But the write-buffer mutex has to be unlocked because
1378c2ecf20Sopenharmony_ci	 * GC also takes it.
1388c2ecf20Sopenharmony_ci	 */
1398c2ecf20Sopenharmony_ci	dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
1408c2ecf20Sopenharmony_ci	mutex_unlock(&wbuf->io_mutex);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	lnum = ubifs_garbage_collect(c, 0);
1438c2ecf20Sopenharmony_ci	if (lnum < 0) {
1448c2ecf20Sopenharmony_ci		err = lnum;
1458c2ecf20Sopenharmony_ci		if (err != -ENOSPC)
1468c2ecf20Sopenharmony_ci			return err;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		/*
1498c2ecf20Sopenharmony_ci		 * GC could not make a free LEB. But someone else may
1508c2ecf20Sopenharmony_ci		 * have allocated new bud for this journal head,
1518c2ecf20Sopenharmony_ci		 * because we dropped @wbuf->io_mutex, so try once
1528c2ecf20Sopenharmony_ci		 * again.
1538c2ecf20Sopenharmony_ci		 */
1548c2ecf20Sopenharmony_ci		dbg_jnl("GC couldn't make a free LEB for jhead %s",
1558c2ecf20Sopenharmony_ci			dbg_jhead(jhead));
1568c2ecf20Sopenharmony_ci		if (retries++ < 2) {
1578c2ecf20Sopenharmony_ci			dbg_jnl("retry (%d)", retries);
1588c2ecf20Sopenharmony_ci			goto again;
1598c2ecf20Sopenharmony_ci		}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		dbg_jnl("return -ENOSPC");
1628c2ecf20Sopenharmony_ci		return err;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1668c2ecf20Sopenharmony_ci	dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
1678c2ecf20Sopenharmony_ci	avail = c->leb_size - wbuf->offs - wbuf->used;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (wbuf->lnum != -1 && avail >= len) {
1708c2ecf20Sopenharmony_ci		/*
1718c2ecf20Sopenharmony_ci		 * Someone else has switched the journal head and we have
1728c2ecf20Sopenharmony_ci		 * enough space now. This happens when more than one process is
1738c2ecf20Sopenharmony_ci		 * trying to write to the same journal head at the same time.
1748c2ecf20Sopenharmony_ci		 */
1758c2ecf20Sopenharmony_ci		dbg_jnl("return LEB %d back, already have LEB %d:%d",
1768c2ecf20Sopenharmony_ci			lnum, wbuf->lnum, wbuf->offs + wbuf->used);
1778c2ecf20Sopenharmony_ci		err = ubifs_return_leb(c, lnum);
1788c2ecf20Sopenharmony_ci		if (err)
1798c2ecf20Sopenharmony_ci			goto out_unlock;
1808c2ecf20Sopenharmony_ci		return 0;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	offs = 0;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciout:
1868c2ecf20Sopenharmony_ci	/*
1878c2ecf20Sopenharmony_ci	 * Make sure we synchronize the write-buffer before we add the new bud
1888c2ecf20Sopenharmony_ci	 * to the log. Otherwise we may have a power cut after the log
1898c2ecf20Sopenharmony_ci	 * reference node for the last bud (@lnum) is written but before the
1908c2ecf20Sopenharmony_ci	 * write-buffer data are written to the next-to-last bud
1918c2ecf20Sopenharmony_ci	 * (@wbuf->lnum). And the effect would be that the recovery would see
1928c2ecf20Sopenharmony_ci	 * that there is corruption in the next-to-last bud.
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	err = ubifs_wbuf_sync_nolock(wbuf);
1958c2ecf20Sopenharmony_ci	if (err)
1968c2ecf20Sopenharmony_ci		goto out_return;
1978c2ecf20Sopenharmony_ci	err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
1988c2ecf20Sopenharmony_ci	if (err)
1998c2ecf20Sopenharmony_ci		goto out_return;
2008c2ecf20Sopenharmony_ci	err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
2018c2ecf20Sopenharmony_ci	if (err)
2028c2ecf20Sopenharmony_ci		goto out_unlock;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	return 0;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciout_unlock:
2078c2ecf20Sopenharmony_ci	mutex_unlock(&wbuf->io_mutex);
2088c2ecf20Sopenharmony_ci	return err;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciout_return:
2118c2ecf20Sopenharmony_ci	/* An error occurred and the LEB has to be returned to lprops */
2128c2ecf20Sopenharmony_ci	ubifs_assert(c, err < 0);
2138c2ecf20Sopenharmony_ci	err1 = ubifs_return_leb(c, lnum);
2148c2ecf20Sopenharmony_ci	if (err1 && err == -EAGAIN)
2158c2ecf20Sopenharmony_ci		/*
2168c2ecf20Sopenharmony_ci		 * Return original error code only if it is not %-EAGAIN,
2178c2ecf20Sopenharmony_ci		 * which is not really an error. Otherwise, return the error
2188c2ecf20Sopenharmony_ci		 * code of 'ubifs_return_leb()'.
2198c2ecf20Sopenharmony_ci		 */
2208c2ecf20Sopenharmony_ci		err = err1;
2218c2ecf20Sopenharmony_ci	mutex_unlock(&wbuf->io_mutex);
2228c2ecf20Sopenharmony_ci	return err;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int ubifs_hash_nodes(struct ubifs_info *c, void *node,
2268c2ecf20Sopenharmony_ci			     int len, struct shash_desc *hash)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	int auth_node_size = ubifs_auth_node_sz(c);
2298c2ecf20Sopenharmony_ci	int err;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	while (1) {
2328c2ecf20Sopenharmony_ci		const struct ubifs_ch *ch = node;
2338c2ecf20Sopenharmony_ci		int nodelen = le32_to_cpu(ch->len);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		ubifs_assert(c, len >= auth_node_size);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci		if (len == auth_node_size)
2388c2ecf20Sopenharmony_ci			break;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		ubifs_assert(c, len > nodelen);
2418c2ecf20Sopenharmony_ci		ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci		err = ubifs_shash_update(c, hash, (void *)node, nodelen);
2448c2ecf20Sopenharmony_ci		if (err)
2458c2ecf20Sopenharmony_ci			return err;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		node += ALIGN(nodelen, 8);
2488c2ecf20Sopenharmony_ci		len -= ALIGN(nodelen, 8);
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return ubifs_prepare_auth_node(c, node, hash);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/**
2558c2ecf20Sopenharmony_ci * write_head - write data to a journal head.
2568c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
2578c2ecf20Sopenharmony_ci * @jhead: journal head
2588c2ecf20Sopenharmony_ci * @buf: buffer to write
2598c2ecf20Sopenharmony_ci * @len: length to write
2608c2ecf20Sopenharmony_ci * @lnum: LEB number written is returned here
2618c2ecf20Sopenharmony_ci * @offs: offset written is returned here
2628c2ecf20Sopenharmony_ci * @sync: non-zero if the write-buffer has to by synchronized
2638c2ecf20Sopenharmony_ci *
2648c2ecf20Sopenharmony_ci * This function writes data to the reserved space of journal head @jhead.
2658c2ecf20Sopenharmony_ci * Returns zero in case of success and a negative error code in case of
2668c2ecf20Sopenharmony_ci * failure.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistatic int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
2698c2ecf20Sopenharmony_ci		      int *lnum, int *offs, int sync)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int err;
2728c2ecf20Sopenharmony_ci	struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	ubifs_assert(c, jhead != GCHD);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	*lnum = c->jheads[jhead].wbuf.lnum;
2778c2ecf20Sopenharmony_ci	*offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
2788c2ecf20Sopenharmony_ci	dbg_jnl("jhead %s, LEB %d:%d, len %d",
2798c2ecf20Sopenharmony_ci		dbg_jhead(jhead), *lnum, *offs, len);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c)) {
2828c2ecf20Sopenharmony_ci		err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
2838c2ecf20Sopenharmony_ci		if (err)
2848c2ecf20Sopenharmony_ci			return err;
2858c2ecf20Sopenharmony_ci	}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	err = ubifs_wbuf_write_nolock(wbuf, buf, len);
2888c2ecf20Sopenharmony_ci	if (err)
2898c2ecf20Sopenharmony_ci		return err;
2908c2ecf20Sopenharmony_ci	if (sync)
2918c2ecf20Sopenharmony_ci		err = ubifs_wbuf_sync_nolock(wbuf);
2928c2ecf20Sopenharmony_ci	return err;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci/**
2968c2ecf20Sopenharmony_ci * make_reservation - reserve journal space.
2978c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
2988c2ecf20Sopenharmony_ci * @jhead: journal head
2998c2ecf20Sopenharmony_ci * @len: how many bytes to reserve
3008c2ecf20Sopenharmony_ci *
3018c2ecf20Sopenharmony_ci * This function makes space reservation in journal head @jhead. The function
3028c2ecf20Sopenharmony_ci * takes the commit lock and locks the journal head, and the caller has to
3038c2ecf20Sopenharmony_ci * unlock the head and finish the reservation with 'finish_reservation()'.
3048c2ecf20Sopenharmony_ci * Returns zero in case of success and a negative error code in case of
3058c2ecf20Sopenharmony_ci * failure.
3068c2ecf20Sopenharmony_ci *
3078c2ecf20Sopenharmony_ci * Note, the journal head may be unlocked as soon as the data is written, while
3088c2ecf20Sopenharmony_ci * the commit lock has to be released after the data has been added to the
3098c2ecf20Sopenharmony_ci * TNC.
3108c2ecf20Sopenharmony_ci */
3118c2ecf20Sopenharmony_cistatic int make_reservation(struct ubifs_info *c, int jhead, int len)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	int err, cmt_retries = 0, nospc_retries = 0;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ciagain:
3168c2ecf20Sopenharmony_ci	down_read(&c->commit_sem);
3178c2ecf20Sopenharmony_ci	err = reserve_space(c, jhead, len);
3188c2ecf20Sopenharmony_ci	if (!err)
3198c2ecf20Sopenharmony_ci		/* c->commit_sem will get released via finish_reservation(). */
3208c2ecf20Sopenharmony_ci		return 0;
3218c2ecf20Sopenharmony_ci	up_read(&c->commit_sem);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (err == -ENOSPC) {
3248c2ecf20Sopenharmony_ci		/*
3258c2ecf20Sopenharmony_ci		 * GC could not make any progress. We should try to commit
3268c2ecf20Sopenharmony_ci		 * once because it could make some dirty space and GC would
3278c2ecf20Sopenharmony_ci		 * make progress, so make the error -EAGAIN so that the below
3288c2ecf20Sopenharmony_ci		 * will commit and re-try.
3298c2ecf20Sopenharmony_ci		 */
3308c2ecf20Sopenharmony_ci		if (nospc_retries++ < 2) {
3318c2ecf20Sopenharmony_ci			dbg_jnl("no space, retry");
3328c2ecf20Sopenharmony_ci			err = -EAGAIN;
3338c2ecf20Sopenharmony_ci		}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		/*
3368c2ecf20Sopenharmony_ci		 * This means that the budgeting is incorrect. We always have
3378c2ecf20Sopenharmony_ci		 * to be able to write to the media, because all operations are
3388c2ecf20Sopenharmony_ci		 * budgeted. Deletions are not budgeted, though, but we reserve
3398c2ecf20Sopenharmony_ci		 * an extra LEB for them.
3408c2ecf20Sopenharmony_ci		 */
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (err != -EAGAIN)
3448c2ecf20Sopenharmony_ci		goto out;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/*
3478c2ecf20Sopenharmony_ci	 * -EAGAIN means that the journal is full or too large, or the above
3488c2ecf20Sopenharmony_ci	 * code wants to do one commit. Do this and re-try.
3498c2ecf20Sopenharmony_ci	 */
3508c2ecf20Sopenharmony_ci	if (cmt_retries > 128) {
3518c2ecf20Sopenharmony_ci		/*
3528c2ecf20Sopenharmony_ci		 * This should not happen unless the journal size limitations
3538c2ecf20Sopenharmony_ci		 * are too tough.
3548c2ecf20Sopenharmony_ci		 */
3558c2ecf20Sopenharmony_ci		ubifs_err(c, "stuck in space allocation");
3568c2ecf20Sopenharmony_ci		err = -ENOSPC;
3578c2ecf20Sopenharmony_ci		goto out;
3588c2ecf20Sopenharmony_ci	} else if (cmt_retries > 32)
3598c2ecf20Sopenharmony_ci		ubifs_warn(c, "too many space allocation re-tries (%d)",
3608c2ecf20Sopenharmony_ci			   cmt_retries);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
3638c2ecf20Sopenharmony_ci		cmt_retries);
3648c2ecf20Sopenharmony_ci	cmt_retries += 1;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	err = ubifs_run_commit(c);
3678c2ecf20Sopenharmony_ci	if (err)
3688c2ecf20Sopenharmony_ci		return err;
3698c2ecf20Sopenharmony_ci	goto again;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ciout:
3728c2ecf20Sopenharmony_ci	ubifs_err(c, "cannot reserve %d bytes in jhead %d, error %d",
3738c2ecf20Sopenharmony_ci		  len, jhead, err);
3748c2ecf20Sopenharmony_ci	if (err == -ENOSPC) {
3758c2ecf20Sopenharmony_ci		/* This are some budgeting problems, print useful information */
3768c2ecf20Sopenharmony_ci		down_write(&c->commit_sem);
3778c2ecf20Sopenharmony_ci		dump_stack();
3788c2ecf20Sopenharmony_ci		ubifs_dump_budg(c, &c->bi);
3798c2ecf20Sopenharmony_ci		ubifs_dump_lprops(c);
3808c2ecf20Sopenharmony_ci		cmt_retries = dbg_check_lprops(c);
3818c2ecf20Sopenharmony_ci		up_write(&c->commit_sem);
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci	return err;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/**
3878c2ecf20Sopenharmony_ci * release_head - release a journal head.
3888c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
3898c2ecf20Sopenharmony_ci * @jhead: journal head
3908c2ecf20Sopenharmony_ci *
3918c2ecf20Sopenharmony_ci * This function releases journal head @jhead which was locked by
3928c2ecf20Sopenharmony_ci * the 'make_reservation()' function. It has to be called after each successful
3938c2ecf20Sopenharmony_ci * 'make_reservation()' invocation.
3948c2ecf20Sopenharmony_ci */
3958c2ecf20Sopenharmony_cistatic inline void release_head(struct ubifs_info *c, int jhead)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/**
4018c2ecf20Sopenharmony_ci * finish_reservation - finish a reservation.
4028c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
4038c2ecf20Sopenharmony_ci *
4048c2ecf20Sopenharmony_ci * This function finishes journal space reservation. It must be called after
4058c2ecf20Sopenharmony_ci * 'make_reservation()'.
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_cistatic void finish_reservation(struct ubifs_info *c)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	up_read(&c->commit_sem);
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/**
4138c2ecf20Sopenharmony_ci * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
4148c2ecf20Sopenharmony_ci * @mode: inode mode
4158c2ecf20Sopenharmony_ci */
4168c2ecf20Sopenharmony_cistatic int get_dent_type(int mode)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	switch (mode & S_IFMT) {
4198c2ecf20Sopenharmony_ci	case S_IFREG:
4208c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_REG;
4218c2ecf20Sopenharmony_ci	case S_IFDIR:
4228c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_DIR;
4238c2ecf20Sopenharmony_ci	case S_IFLNK:
4248c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_LNK;
4258c2ecf20Sopenharmony_ci	case S_IFBLK:
4268c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_BLK;
4278c2ecf20Sopenharmony_ci	case S_IFCHR:
4288c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_CHR;
4298c2ecf20Sopenharmony_ci	case S_IFIFO:
4308c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_FIFO;
4318c2ecf20Sopenharmony_ci	case S_IFSOCK:
4328c2ecf20Sopenharmony_ci		return UBIFS_ITYPE_SOCK;
4338c2ecf20Sopenharmony_ci	default:
4348c2ecf20Sopenharmony_ci		BUG();
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci	return 0;
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci/**
4408c2ecf20Sopenharmony_ci * pack_inode - pack an inode node.
4418c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
4428c2ecf20Sopenharmony_ci * @ino: buffer in which to pack inode node
4438c2ecf20Sopenharmony_ci * @inode: inode to pack
4448c2ecf20Sopenharmony_ci * @last: indicates the last node of the group
4458c2ecf20Sopenharmony_ci */
4468c2ecf20Sopenharmony_cistatic void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
4478c2ecf20Sopenharmony_ci		       const struct inode *inode, int last)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	int data_len = 0, last_reference = !inode->i_nlink;
4508c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	ino->ch.node_type = UBIFS_INO_NODE;
4538c2ecf20Sopenharmony_ci	ino_key_init_flash(c, &ino->key, inode->i_ino);
4548c2ecf20Sopenharmony_ci	ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
4558c2ecf20Sopenharmony_ci	ino->atime_sec  = cpu_to_le64(inode->i_atime.tv_sec);
4568c2ecf20Sopenharmony_ci	ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
4578c2ecf20Sopenharmony_ci	ino->ctime_sec  = cpu_to_le64(inode->i_ctime.tv_sec);
4588c2ecf20Sopenharmony_ci	ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
4598c2ecf20Sopenharmony_ci	ino->mtime_sec  = cpu_to_le64(inode->i_mtime.tv_sec);
4608c2ecf20Sopenharmony_ci	ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
4618c2ecf20Sopenharmony_ci	ino->uid   = cpu_to_le32(i_uid_read(inode));
4628c2ecf20Sopenharmony_ci	ino->gid   = cpu_to_le32(i_gid_read(inode));
4638c2ecf20Sopenharmony_ci	ino->mode  = cpu_to_le32(inode->i_mode);
4648c2ecf20Sopenharmony_ci	ino->flags = cpu_to_le32(ui->flags);
4658c2ecf20Sopenharmony_ci	ino->size  = cpu_to_le64(ui->ui_size);
4668c2ecf20Sopenharmony_ci	ino->nlink = cpu_to_le32(inode->i_nlink);
4678c2ecf20Sopenharmony_ci	ino->compr_type  = cpu_to_le16(ui->compr_type);
4688c2ecf20Sopenharmony_ci	ino->data_len    = cpu_to_le32(ui->data_len);
4698c2ecf20Sopenharmony_ci	ino->xattr_cnt   = cpu_to_le32(ui->xattr_cnt);
4708c2ecf20Sopenharmony_ci	ino->xattr_size  = cpu_to_le32(ui->xattr_size);
4718c2ecf20Sopenharmony_ci	ino->xattr_names = cpu_to_le32(ui->xattr_names);
4728c2ecf20Sopenharmony_ci	zero_ino_node_unused(ino);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/*
4758c2ecf20Sopenharmony_ci	 * Drop the attached data if this is a deletion inode, the data is not
4768c2ecf20Sopenharmony_ci	 * needed anymore.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	if (!last_reference) {
4798c2ecf20Sopenharmony_ci		memcpy(ino->data, ui->data, ui->data_len);
4808c2ecf20Sopenharmony_ci		data_len = ui->data_len;
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/**
4878c2ecf20Sopenharmony_ci * mark_inode_clean - mark UBIFS inode as clean.
4888c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
4898c2ecf20Sopenharmony_ci * @ui: UBIFS inode to mark as clean
4908c2ecf20Sopenharmony_ci *
4918c2ecf20Sopenharmony_ci * This helper function marks UBIFS inode @ui as clean by cleaning the
4928c2ecf20Sopenharmony_ci * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
4938c2ecf20Sopenharmony_ci * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
4948c2ecf20Sopenharmony_ci * just do nothing.
4958c2ecf20Sopenharmony_ci */
4968c2ecf20Sopenharmony_cistatic void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	if (ui->dirty)
4998c2ecf20Sopenharmony_ci		ubifs_release_dirty_inode_budget(c, ui);
5008c2ecf20Sopenharmony_ci	ui->dirty = 0;
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_cistatic void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	if (c->double_hash)
5068c2ecf20Sopenharmony_ci		dent->cookie = (__force __le32) prandom_u32();
5078c2ecf20Sopenharmony_ci	else
5088c2ecf20Sopenharmony_ci		dent->cookie = 0;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci/**
5128c2ecf20Sopenharmony_ci * ubifs_jnl_update - update inode.
5138c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
5148c2ecf20Sopenharmony_ci * @dir: parent inode or host inode in case of extended attributes
5158c2ecf20Sopenharmony_ci * @nm: directory entry name
5168c2ecf20Sopenharmony_ci * @inode: inode to update
5178c2ecf20Sopenharmony_ci * @deletion: indicates a directory entry deletion i.e unlink or rmdir
5188c2ecf20Sopenharmony_ci * @xent: non-zero if the directory entry is an extended attribute entry
5198c2ecf20Sopenharmony_ci *
5208c2ecf20Sopenharmony_ci * This function updates an inode by writing a directory entry (or extended
5218c2ecf20Sopenharmony_ci * attribute entry), the inode itself, and the parent directory inode (or the
5228c2ecf20Sopenharmony_ci * host inode) to the journal.
5238c2ecf20Sopenharmony_ci *
5248c2ecf20Sopenharmony_ci * The function writes the host inode @dir last, which is important in case of
5258c2ecf20Sopenharmony_ci * extended attributes. Indeed, then we guarantee that if the host inode gets
5268c2ecf20Sopenharmony_ci * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
5278c2ecf20Sopenharmony_ci * the extended attribute inode gets flushed too. And this is exactly what the
5288c2ecf20Sopenharmony_ci * user expects - synchronizing the host inode synchronizes its extended
5298c2ecf20Sopenharmony_ci * attributes. Similarly, this guarantees that if @dir is synchronized, its
5308c2ecf20Sopenharmony_ci * directory entry corresponding to @nm gets synchronized too.
5318c2ecf20Sopenharmony_ci *
5328c2ecf20Sopenharmony_ci * If the inode (@inode) or the parent directory (@dir) are synchronous, this
5338c2ecf20Sopenharmony_ci * function synchronizes the write-buffer.
5348c2ecf20Sopenharmony_ci *
5358c2ecf20Sopenharmony_ci * This function marks the @dir and @inode inodes as clean and returns zero on
5368c2ecf20Sopenharmony_ci * success. In case of failure, a negative error code is returned.
5378c2ecf20Sopenharmony_ci */
5388c2ecf20Sopenharmony_ciint ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
5398c2ecf20Sopenharmony_ci		     const struct fscrypt_name *nm, const struct inode *inode,
5408c2ecf20Sopenharmony_ci		     int deletion, int xent)
5418c2ecf20Sopenharmony_ci{
5428c2ecf20Sopenharmony_ci	int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
5438c2ecf20Sopenharmony_ci	int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
5448c2ecf20Sopenharmony_ci	int last_reference = !!(deletion && inode->i_nlink == 0);
5458c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
5468c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(dir);
5478c2ecf20Sopenharmony_ci	struct ubifs_dent_node *dent;
5488c2ecf20Sopenharmony_ci	struct ubifs_ino_node *ino;
5498c2ecf20Sopenharmony_ci	union ubifs_key dent_key, ino_key;
5508c2ecf20Sopenharmony_ci	u8 hash_dent[UBIFS_HASH_ARR_SZ];
5518c2ecf20Sopenharmony_ci	u8 hash_ino[UBIFS_HASH_ARR_SZ];
5528c2ecf20Sopenharmony_ci	u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
5578c2ecf20Sopenharmony_ci	ilen = UBIFS_INO_NODE_SZ;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/*
5608c2ecf20Sopenharmony_ci	 * If the last reference to the inode is being deleted, then there is
5618c2ecf20Sopenharmony_ci	 * no need to attach and write inode data, it is being deleted anyway.
5628c2ecf20Sopenharmony_ci	 * And if the inode is being deleted, no need to synchronize
5638c2ecf20Sopenharmony_ci	 * write-buffer even if the inode is synchronous.
5648c2ecf20Sopenharmony_ci	 */
5658c2ecf20Sopenharmony_ci	if (!last_reference) {
5668c2ecf20Sopenharmony_ci		ilen += ui->data_len;
5678c2ecf20Sopenharmony_ci		sync |= IS_SYNC(inode);
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	aligned_dlen = ALIGN(dlen, 8);
5718c2ecf20Sopenharmony_ci	aligned_ilen = ALIGN(ilen, 8);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
5748c2ecf20Sopenharmony_ci	/* Make sure to also account for extended attributes */
5758c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c))
5768c2ecf20Sopenharmony_ci		len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
5778c2ecf20Sopenharmony_ci	else
5788c2ecf20Sopenharmony_ci		len += host_ui->data_len;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	dent = kzalloc(len, GFP_NOFS);
5818c2ecf20Sopenharmony_ci	if (!dent)
5828c2ecf20Sopenharmony_ci		return -ENOMEM;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
5858c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, len);
5868c2ecf20Sopenharmony_ci	if (err)
5878c2ecf20Sopenharmony_ci		goto out_free;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if (!xent) {
5908c2ecf20Sopenharmony_ci		dent->ch.node_type = UBIFS_DENT_NODE;
5918c2ecf20Sopenharmony_ci		if (fname_name(nm) == NULL)
5928c2ecf20Sopenharmony_ci			dent_key_init_hash(c, &dent_key, dir->i_ino, nm->hash);
5938c2ecf20Sopenharmony_ci		else
5948c2ecf20Sopenharmony_ci			dent_key_init(c, &dent_key, dir->i_ino, nm);
5958c2ecf20Sopenharmony_ci	} else {
5968c2ecf20Sopenharmony_ci		dent->ch.node_type = UBIFS_XENT_NODE;
5978c2ecf20Sopenharmony_ci		xent_key_init(c, &dent_key, dir->i_ino, nm);
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	key_write(c, &dent_key, dent->key);
6018c2ecf20Sopenharmony_ci	dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
6028c2ecf20Sopenharmony_ci	dent->type = get_dent_type(inode->i_mode);
6038c2ecf20Sopenharmony_ci	dent->nlen = cpu_to_le16(fname_len(nm));
6048c2ecf20Sopenharmony_ci	memcpy(dent->name, fname_name(nm), fname_len(nm));
6058c2ecf20Sopenharmony_ci	dent->name[fname_len(nm)] = '\0';
6068c2ecf20Sopenharmony_ci	set_dent_cookie(c, dent);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	zero_dent_node_unused(dent);
6098c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, dent, dlen, 0);
6108c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, dent, hash_dent);
6118c2ecf20Sopenharmony_ci	if (err)
6128c2ecf20Sopenharmony_ci		goto out_release;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	ino = (void *)dent + aligned_dlen;
6158c2ecf20Sopenharmony_ci	pack_inode(c, ino, inode, 0);
6168c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash_ino);
6178c2ecf20Sopenharmony_ci	if (err)
6188c2ecf20Sopenharmony_ci		goto out_release;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	ino = (void *)ino + aligned_ilen;
6218c2ecf20Sopenharmony_ci	pack_inode(c, ino, dir, 1);
6228c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash_ino_host);
6238c2ecf20Sopenharmony_ci	if (err)
6248c2ecf20Sopenharmony_ci		goto out_release;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	if (last_reference) {
6278c2ecf20Sopenharmony_ci		err = ubifs_add_orphan(c, inode->i_ino);
6288c2ecf20Sopenharmony_ci		if (err) {
6298c2ecf20Sopenharmony_ci			release_head(c, BASEHD);
6308c2ecf20Sopenharmony_ci			goto out_finish;
6318c2ecf20Sopenharmony_ci		}
6328c2ecf20Sopenharmony_ci		ui->del_cmtno = c->cmt_no;
6338c2ecf20Sopenharmony_ci		orphan_added = 1;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
6378c2ecf20Sopenharmony_ci	if (err)
6388c2ecf20Sopenharmony_ci		goto out_release;
6398c2ecf20Sopenharmony_ci	if (!sync) {
6408c2ecf20Sopenharmony_ci		struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
6438c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
6468c2ecf20Sopenharmony_ci	kfree(dent);
6478c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	if (deletion) {
6508c2ecf20Sopenharmony_ci		if (fname_name(nm) == NULL)
6518c2ecf20Sopenharmony_ci			err = ubifs_tnc_remove_dh(c, &dent_key, nm->minor_hash);
6528c2ecf20Sopenharmony_ci		else
6538c2ecf20Sopenharmony_ci			err = ubifs_tnc_remove_nm(c, &dent_key, nm);
6548c2ecf20Sopenharmony_ci		if (err)
6558c2ecf20Sopenharmony_ci			goto out_ro;
6568c2ecf20Sopenharmony_ci		err = ubifs_add_dirt(c, lnum, dlen);
6578c2ecf20Sopenharmony_ci	} else
6588c2ecf20Sopenharmony_ci		err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
6598c2ecf20Sopenharmony_ci				       hash_dent, nm);
6608c2ecf20Sopenharmony_ci	if (err)
6618c2ecf20Sopenharmony_ci		goto out_ro;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/*
6648c2ecf20Sopenharmony_ci	 * Note, we do not remove the inode from TNC even if the last reference
6658c2ecf20Sopenharmony_ci	 * to it has just been deleted, because the inode may still be opened.
6668c2ecf20Sopenharmony_ci	 * Instead, the inode has been added to orphan lists and the orphan
6678c2ecf20Sopenharmony_ci	 * subsystem will take further care about it.
6688c2ecf20Sopenharmony_ci	 */
6698c2ecf20Sopenharmony_ci	ino_key_init(c, &ino_key, inode->i_ino);
6708c2ecf20Sopenharmony_ci	ino_offs = dent_offs + aligned_dlen;
6718c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
6728c2ecf20Sopenharmony_ci	if (err)
6738c2ecf20Sopenharmony_ci		goto out_ro;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	ino_key_init(c, &ino_key, dir->i_ino);
6768c2ecf20Sopenharmony_ci	ino_offs += aligned_ilen;
6778c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
6788c2ecf20Sopenharmony_ci			    UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
6798c2ecf20Sopenharmony_ci	if (err)
6808c2ecf20Sopenharmony_ci		goto out_ro;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	finish_reservation(c);
6838c2ecf20Sopenharmony_ci	spin_lock(&ui->ui_lock);
6848c2ecf20Sopenharmony_ci	ui->synced_i_size = ui->ui_size;
6858c2ecf20Sopenharmony_ci	spin_unlock(&ui->ui_lock);
6868c2ecf20Sopenharmony_ci	if (xent) {
6878c2ecf20Sopenharmony_ci		spin_lock(&host_ui->ui_lock);
6888c2ecf20Sopenharmony_ci		host_ui->synced_i_size = host_ui->ui_size;
6898c2ecf20Sopenharmony_ci		spin_unlock(&host_ui->ui_lock);
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci	mark_inode_clean(c, ui);
6928c2ecf20Sopenharmony_ci	mark_inode_clean(c, host_ui);
6938c2ecf20Sopenharmony_ci	return 0;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ciout_finish:
6968c2ecf20Sopenharmony_ci	finish_reservation(c);
6978c2ecf20Sopenharmony_ciout_free:
6988c2ecf20Sopenharmony_ci	kfree(dent);
6998c2ecf20Sopenharmony_ci	return err;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ciout_release:
7028c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
7038c2ecf20Sopenharmony_ci	kfree(dent);
7048c2ecf20Sopenharmony_ciout_ro:
7058c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
7068c2ecf20Sopenharmony_ci	if (orphan_added)
7078c2ecf20Sopenharmony_ci		ubifs_delete_orphan(c, inode->i_ino);
7088c2ecf20Sopenharmony_ci	finish_reservation(c);
7098c2ecf20Sopenharmony_ci	return err;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci/**
7138c2ecf20Sopenharmony_ci * ubifs_jnl_write_data - write a data node to the journal.
7148c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
7158c2ecf20Sopenharmony_ci * @inode: inode the data node belongs to
7168c2ecf20Sopenharmony_ci * @key: node key
7178c2ecf20Sopenharmony_ci * @buf: buffer to write
7188c2ecf20Sopenharmony_ci * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
7198c2ecf20Sopenharmony_ci *
7208c2ecf20Sopenharmony_ci * This function writes a data node to the journal. Returns %0 if the data node
7218c2ecf20Sopenharmony_ci * was successfully written, and a negative error code in case of failure.
7228c2ecf20Sopenharmony_ci */
7238c2ecf20Sopenharmony_ciint ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
7248c2ecf20Sopenharmony_ci			 const union ubifs_key *key, const void *buf, int len)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	struct ubifs_data_node *data;
7278c2ecf20Sopenharmony_ci	int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
7288c2ecf20Sopenharmony_ci	int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
7298c2ecf20Sopenharmony_ci	int write_len;
7308c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
7318c2ecf20Sopenharmony_ci	bool encrypted = IS_ENCRYPTED(inode);
7328c2ecf20Sopenharmony_ci	u8 hash[UBIFS_HASH_ARR_SZ];
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
7358c2ecf20Sopenharmony_ci		(unsigned long)key_inum(c, key), key_block(c, key), len);
7368c2ecf20Sopenharmony_ci	ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (encrypted)
7398c2ecf20Sopenharmony_ci		dlen += UBIFS_CIPHER_BLOCK_SIZE;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	auth_len = ubifs_auth_node_sz(c);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
7448c2ecf20Sopenharmony_ci	if (!data) {
7458c2ecf20Sopenharmony_ci		/*
7468c2ecf20Sopenharmony_ci		 * Fall-back to the write reserve buffer. Note, we might be
7478c2ecf20Sopenharmony_ci		 * currently on the memory reclaim path, when the kernel is
7488c2ecf20Sopenharmony_ci		 * trying to free some memory by writing out dirty pages. The
7498c2ecf20Sopenharmony_ci		 * write reserve buffer helps us to guarantee that we are
7508c2ecf20Sopenharmony_ci		 * always able to write the data.
7518c2ecf20Sopenharmony_ci		 */
7528c2ecf20Sopenharmony_ci		allocated = 0;
7538c2ecf20Sopenharmony_ci		mutex_lock(&c->write_reserve_mutex);
7548c2ecf20Sopenharmony_ci		data = c->write_reserve_buf;
7558c2ecf20Sopenharmony_ci	}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	data->ch.node_type = UBIFS_DATA_NODE;
7588c2ecf20Sopenharmony_ci	key_write(c, key, &data->key);
7598c2ecf20Sopenharmony_ci	data->size = cpu_to_le32(len);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	if (!(ui->flags & UBIFS_COMPR_FL))
7628c2ecf20Sopenharmony_ci		/* Compression is disabled for this inode */
7638c2ecf20Sopenharmony_ci		compr_type = UBIFS_COMPR_NONE;
7648c2ecf20Sopenharmony_ci	else
7658c2ecf20Sopenharmony_ci		compr_type = ui->compr_type;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
7688c2ecf20Sopenharmony_ci	ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
7698c2ecf20Sopenharmony_ci	ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	if (encrypted) {
7728c2ecf20Sopenharmony_ci		err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
7738c2ecf20Sopenharmony_ci		if (err)
7748c2ecf20Sopenharmony_ci			goto out_free;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	} else {
7778c2ecf20Sopenharmony_ci		data->compr_size = 0;
7788c2ecf20Sopenharmony_ci		out_len = compr_len;
7798c2ecf20Sopenharmony_ci	}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	dlen = UBIFS_DATA_NODE_SZ + out_len;
7828c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c))
7838c2ecf20Sopenharmony_ci		write_len = ALIGN(dlen, 8) + auth_len;
7848c2ecf20Sopenharmony_ci	else
7858c2ecf20Sopenharmony_ci		write_len = dlen;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	data->compr_type = cpu_to_le16(compr_type);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
7908c2ecf20Sopenharmony_ci	err = make_reservation(c, DATAHD, write_len);
7918c2ecf20Sopenharmony_ci	if (err)
7928c2ecf20Sopenharmony_ci		goto out_free;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	ubifs_prepare_node(c, data, dlen, 0);
7958c2ecf20Sopenharmony_ci	err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
7968c2ecf20Sopenharmony_ci	if (err)
7978c2ecf20Sopenharmony_ci		goto out_release;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, data, hash);
8008c2ecf20Sopenharmony_ci	if (err)
8018c2ecf20Sopenharmony_ci		goto out_release;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
8048c2ecf20Sopenharmony_ci	release_head(c, DATAHD);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
8098c2ecf20Sopenharmony_ci	if (err)
8108c2ecf20Sopenharmony_ci		goto out_ro;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	finish_reservation(c);
8138c2ecf20Sopenharmony_ci	if (!allocated)
8148c2ecf20Sopenharmony_ci		mutex_unlock(&c->write_reserve_mutex);
8158c2ecf20Sopenharmony_ci	else
8168c2ecf20Sopenharmony_ci		kfree(data);
8178c2ecf20Sopenharmony_ci	return 0;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ciout_release:
8208c2ecf20Sopenharmony_ci	release_head(c, DATAHD);
8218c2ecf20Sopenharmony_ciout_ro:
8228c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
8238c2ecf20Sopenharmony_ci	finish_reservation(c);
8248c2ecf20Sopenharmony_ciout_free:
8258c2ecf20Sopenharmony_ci	if (!allocated)
8268c2ecf20Sopenharmony_ci		mutex_unlock(&c->write_reserve_mutex);
8278c2ecf20Sopenharmony_ci	else
8288c2ecf20Sopenharmony_ci		kfree(data);
8298c2ecf20Sopenharmony_ci	return err;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci/**
8338c2ecf20Sopenharmony_ci * ubifs_jnl_write_inode - flush inode to the journal.
8348c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
8358c2ecf20Sopenharmony_ci * @inode: inode to flush
8368c2ecf20Sopenharmony_ci *
8378c2ecf20Sopenharmony_ci * This function writes inode @inode to the journal. If the inode is
8388c2ecf20Sopenharmony_ci * synchronous, it also synchronizes the write-buffer. Returns zero in case of
8398c2ecf20Sopenharmony_ci * success and a negative error code in case of failure.
8408c2ecf20Sopenharmony_ci */
8418c2ecf20Sopenharmony_ciint ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
8428c2ecf20Sopenharmony_ci{
8438c2ecf20Sopenharmony_ci	int err, lnum, offs;
8448c2ecf20Sopenharmony_ci	struct ubifs_ino_node *ino, *ino_start;
8458c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
8468c2ecf20Sopenharmony_ci	int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
8478c2ecf20Sopenharmony_ci	int last_reference = !inode->i_nlink;
8488c2ecf20Sopenharmony_ci	int kill_xattrs = ui->xattr_cnt && last_reference;
8498c2ecf20Sopenharmony_ci	u8 hash[UBIFS_HASH_ARR_SZ];
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	/*
8548c2ecf20Sopenharmony_ci	 * If the inode is being deleted, do not write the attached data. No
8558c2ecf20Sopenharmony_ci	 * need to synchronize the write-buffer either.
8568c2ecf20Sopenharmony_ci	 */
8578c2ecf20Sopenharmony_ci	if (!last_reference) {
8588c2ecf20Sopenharmony_ci		ilen += ui->data_len;
8598c2ecf20Sopenharmony_ci		sync = IS_SYNC(inode);
8608c2ecf20Sopenharmony_ci	} else if (kill_xattrs) {
8618c2ecf20Sopenharmony_ci		write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
8628c2ecf20Sopenharmony_ci	}
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c))
8658c2ecf20Sopenharmony_ci		write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
8668c2ecf20Sopenharmony_ci	else
8678c2ecf20Sopenharmony_ci		write_len += ilen;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	ino_start = ino = kmalloc(write_len, GFP_NOFS);
8708c2ecf20Sopenharmony_ci	if (!ino)
8718c2ecf20Sopenharmony_ci		return -ENOMEM;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
8748c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, write_len);
8758c2ecf20Sopenharmony_ci	if (err)
8768c2ecf20Sopenharmony_ci		goto out_free;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	if (kill_xattrs) {
8798c2ecf20Sopenharmony_ci		union ubifs_key key;
8808c2ecf20Sopenharmony_ci		struct fscrypt_name nm = {0};
8818c2ecf20Sopenharmony_ci		struct inode *xino;
8828c2ecf20Sopenharmony_ci		struct ubifs_dent_node *xent, *pxent = NULL;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci		if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
8858c2ecf20Sopenharmony_ci			err = -EPERM;
8868c2ecf20Sopenharmony_ci			ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
8878c2ecf20Sopenharmony_ci			goto out_release;
8888c2ecf20Sopenharmony_ci		}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci		lowest_xent_key(c, &key, inode->i_ino);
8918c2ecf20Sopenharmony_ci		while (1) {
8928c2ecf20Sopenharmony_ci			xent = ubifs_tnc_next_ent(c, &key, &nm);
8938c2ecf20Sopenharmony_ci			if (IS_ERR(xent)) {
8948c2ecf20Sopenharmony_ci				err = PTR_ERR(xent);
8958c2ecf20Sopenharmony_ci				if (err == -ENOENT)
8968c2ecf20Sopenharmony_ci					break;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci				kfree(pxent);
8998c2ecf20Sopenharmony_ci				goto out_release;
9008c2ecf20Sopenharmony_ci			}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci			fname_name(&nm) = xent->name;
9038c2ecf20Sopenharmony_ci			fname_len(&nm) = le16_to_cpu(xent->nlen);
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci			xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
9068c2ecf20Sopenharmony_ci			if (IS_ERR(xino)) {
9078c2ecf20Sopenharmony_ci				err = PTR_ERR(xino);
9088c2ecf20Sopenharmony_ci				ubifs_err(c, "dead directory entry '%s', error %d",
9098c2ecf20Sopenharmony_ci					  xent->name, err);
9108c2ecf20Sopenharmony_ci				ubifs_ro_mode(c, err);
9118c2ecf20Sopenharmony_ci				kfree(pxent);
9128c2ecf20Sopenharmony_ci				kfree(xent);
9138c2ecf20Sopenharmony_ci				goto out_release;
9148c2ecf20Sopenharmony_ci			}
9158c2ecf20Sopenharmony_ci			ubifs_assert(c, ubifs_inode(xino)->xattr);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci			clear_nlink(xino);
9188c2ecf20Sopenharmony_ci			pack_inode(c, ino, xino, 0);
9198c2ecf20Sopenharmony_ci			ino = (void *)ino + UBIFS_INO_NODE_SZ;
9208c2ecf20Sopenharmony_ci			iput(xino);
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci			kfree(pxent);
9238c2ecf20Sopenharmony_ci			pxent = xent;
9248c2ecf20Sopenharmony_ci			key_read(c, &xent->key, &key);
9258c2ecf20Sopenharmony_ci		}
9268c2ecf20Sopenharmony_ci		kfree(pxent);
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	pack_inode(c, ino, inode, 1);
9308c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash);
9318c2ecf20Sopenharmony_ci	if (err)
9328c2ecf20Sopenharmony_ci		goto out_release;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
9358c2ecf20Sopenharmony_ci	if (err)
9368c2ecf20Sopenharmony_ci		goto out_release;
9378c2ecf20Sopenharmony_ci	if (!sync)
9388c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
9398c2ecf20Sopenharmony_ci					  inode->i_ino);
9408c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (last_reference) {
9438c2ecf20Sopenharmony_ci		err = ubifs_tnc_remove_ino(c, inode->i_ino);
9448c2ecf20Sopenharmony_ci		if (err)
9458c2ecf20Sopenharmony_ci			goto out_ro;
9468c2ecf20Sopenharmony_ci		ubifs_delete_orphan(c, inode->i_ino);
9478c2ecf20Sopenharmony_ci		err = ubifs_add_dirt(c, lnum, write_len);
9488c2ecf20Sopenharmony_ci	} else {
9498c2ecf20Sopenharmony_ci		union ubifs_key key;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci		ubifs_add_auth_dirt(c, lnum);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci		ino_key_init(c, &key, inode->i_ino);
9548c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
9558c2ecf20Sopenharmony_ci	}
9568c2ecf20Sopenharmony_ci	if (err)
9578c2ecf20Sopenharmony_ci		goto out_ro;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	finish_reservation(c);
9608c2ecf20Sopenharmony_ci	spin_lock(&ui->ui_lock);
9618c2ecf20Sopenharmony_ci	ui->synced_i_size = ui->ui_size;
9628c2ecf20Sopenharmony_ci	spin_unlock(&ui->ui_lock);
9638c2ecf20Sopenharmony_ci	kfree(ino_start);
9648c2ecf20Sopenharmony_ci	return 0;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ciout_release:
9678c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
9688c2ecf20Sopenharmony_ciout_ro:
9698c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
9708c2ecf20Sopenharmony_ci	finish_reservation(c);
9718c2ecf20Sopenharmony_ciout_free:
9728c2ecf20Sopenharmony_ci	kfree(ino_start);
9738c2ecf20Sopenharmony_ci	return err;
9748c2ecf20Sopenharmony_ci}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci/**
9778c2ecf20Sopenharmony_ci * ubifs_jnl_delete_inode - delete an inode.
9788c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
9798c2ecf20Sopenharmony_ci * @inode: inode to delete
9808c2ecf20Sopenharmony_ci *
9818c2ecf20Sopenharmony_ci * This function deletes inode @inode which includes removing it from orphans,
9828c2ecf20Sopenharmony_ci * deleting it from TNC and, in some cases, writing a deletion inode to the
9838c2ecf20Sopenharmony_ci * journal.
9848c2ecf20Sopenharmony_ci *
9858c2ecf20Sopenharmony_ci * When regular file inodes are unlinked or a directory inode is removed, the
9868c2ecf20Sopenharmony_ci * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
9878c2ecf20Sopenharmony_ci * direntry to the media, and adds the inode to orphans. After this, when the
9888c2ecf20Sopenharmony_ci * last reference to this inode has been dropped, this function is called. In
9898c2ecf20Sopenharmony_ci * general, it has to write one more deletion inode to the media, because if
9908c2ecf20Sopenharmony_ci * a commit happened between 'ubifs_jnl_update()' and
9918c2ecf20Sopenharmony_ci * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
9928c2ecf20Sopenharmony_ci * anymore, and in fact it might not be on the flash anymore, because it might
9938c2ecf20Sopenharmony_ci * have been garbage-collected already. And for optimization reasons UBIFS does
9948c2ecf20Sopenharmony_ci * not read the orphan area if it has been unmounted cleanly, so it would have
9958c2ecf20Sopenharmony_ci * no indication in the journal that there is a deleted inode which has to be
9968c2ecf20Sopenharmony_ci * removed from TNC.
9978c2ecf20Sopenharmony_ci *
9988c2ecf20Sopenharmony_ci * However, if there was no commit between 'ubifs_jnl_update()' and
9998c2ecf20Sopenharmony_ci * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
10008c2ecf20Sopenharmony_ci * inode to the media for the second time. And this is quite a typical case.
10018c2ecf20Sopenharmony_ci *
10028c2ecf20Sopenharmony_ci * This function returns zero in case of success and a negative error code in
10038c2ecf20Sopenharmony_ci * case of failure.
10048c2ecf20Sopenharmony_ci */
10058c2ecf20Sopenharmony_ciint ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	int err;
10088c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	ubifs_assert(c, inode->i_nlink == 0);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
10138c2ecf20Sopenharmony_ci		/* A commit happened for sure or inode hosts xattrs */
10148c2ecf20Sopenharmony_ci		return ubifs_jnl_write_inode(c, inode);
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	down_read(&c->commit_sem);
10178c2ecf20Sopenharmony_ci	/*
10188c2ecf20Sopenharmony_ci	 * Check commit number again, because the first test has been done
10198c2ecf20Sopenharmony_ci	 * without @c->commit_sem, so a commit might have happened.
10208c2ecf20Sopenharmony_ci	 */
10218c2ecf20Sopenharmony_ci	if (ui->del_cmtno != c->cmt_no) {
10228c2ecf20Sopenharmony_ci		up_read(&c->commit_sem);
10238c2ecf20Sopenharmony_ci		return ubifs_jnl_write_inode(c, inode);
10248c2ecf20Sopenharmony_ci	}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	err = ubifs_tnc_remove_ino(c, inode->i_ino);
10278c2ecf20Sopenharmony_ci	if (err)
10288c2ecf20Sopenharmony_ci		ubifs_ro_mode(c, err);
10298c2ecf20Sopenharmony_ci	else
10308c2ecf20Sopenharmony_ci		ubifs_delete_orphan(c, inode->i_ino);
10318c2ecf20Sopenharmony_ci	up_read(&c->commit_sem);
10328c2ecf20Sopenharmony_ci	return err;
10338c2ecf20Sopenharmony_ci}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci/**
10368c2ecf20Sopenharmony_ci * ubifs_jnl_xrename - cross rename two directory entries.
10378c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
10388c2ecf20Sopenharmony_ci * @fst_dir: parent inode of 1st directory entry to exchange
10398c2ecf20Sopenharmony_ci * @fst_inode: 1st inode to exchange
10408c2ecf20Sopenharmony_ci * @fst_nm: name of 1st inode to exchange
10418c2ecf20Sopenharmony_ci * @snd_dir: parent inode of 2nd directory entry to exchange
10428c2ecf20Sopenharmony_ci * @snd_inode: 2nd inode to exchange
10438c2ecf20Sopenharmony_ci * @snd_nm: name of 2nd inode to exchange
10448c2ecf20Sopenharmony_ci * @sync: non-zero if the write-buffer has to be synchronized
10458c2ecf20Sopenharmony_ci *
10468c2ecf20Sopenharmony_ci * This function implements the cross rename operation which may involve
10478c2ecf20Sopenharmony_ci * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
10488c2ecf20Sopenharmony_ci * and returns zero on success. In case of failure, a negative error code is
10498c2ecf20Sopenharmony_ci * returned.
10508c2ecf20Sopenharmony_ci */
10518c2ecf20Sopenharmony_ciint ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
10528c2ecf20Sopenharmony_ci		      const struct inode *fst_inode,
10538c2ecf20Sopenharmony_ci		      const struct fscrypt_name *fst_nm,
10548c2ecf20Sopenharmony_ci		      const struct inode *snd_dir,
10558c2ecf20Sopenharmony_ci		      const struct inode *snd_inode,
10568c2ecf20Sopenharmony_ci		      const struct fscrypt_name *snd_nm, int sync)
10578c2ecf20Sopenharmony_ci{
10588c2ecf20Sopenharmony_ci	union ubifs_key key;
10598c2ecf20Sopenharmony_ci	struct ubifs_dent_node *dent1, *dent2;
10608c2ecf20Sopenharmony_ci	int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
10618c2ecf20Sopenharmony_ci	int aligned_dlen1, aligned_dlen2;
10628c2ecf20Sopenharmony_ci	int twoparents = (fst_dir != snd_dir);
10638c2ecf20Sopenharmony_ci	void *p;
10648c2ecf20Sopenharmony_ci	u8 hash_dent1[UBIFS_HASH_ARR_SZ];
10658c2ecf20Sopenharmony_ci	u8 hash_dent2[UBIFS_HASH_ARR_SZ];
10668c2ecf20Sopenharmony_ci	u8 hash_p1[UBIFS_HASH_ARR_SZ];
10678c2ecf20Sopenharmony_ci	u8 hash_p2[UBIFS_HASH_ARR_SZ];
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
10708c2ecf20Sopenharmony_ci	ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
10718c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
10728c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
10758c2ecf20Sopenharmony_ci	dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
10768c2ecf20Sopenharmony_ci	aligned_dlen1 = ALIGN(dlen1, 8);
10778c2ecf20Sopenharmony_ci	aligned_dlen2 = ALIGN(dlen2, 8);
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
10808c2ecf20Sopenharmony_ci	if (twoparents)
10818c2ecf20Sopenharmony_ci		len += plen;
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	len += ubifs_auth_node_sz(c);
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	dent1 = kzalloc(len, GFP_NOFS);
10868c2ecf20Sopenharmony_ci	if (!dent1)
10878c2ecf20Sopenharmony_ci		return -ENOMEM;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
10908c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, len);
10918c2ecf20Sopenharmony_ci	if (err)
10928c2ecf20Sopenharmony_ci		goto out_free;
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	/* Make new dent for 1st entry */
10958c2ecf20Sopenharmony_ci	dent1->ch.node_type = UBIFS_DENT_NODE;
10968c2ecf20Sopenharmony_ci	dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
10978c2ecf20Sopenharmony_ci	dent1->inum = cpu_to_le64(fst_inode->i_ino);
10988c2ecf20Sopenharmony_ci	dent1->type = get_dent_type(fst_inode->i_mode);
10998c2ecf20Sopenharmony_ci	dent1->nlen = cpu_to_le16(fname_len(snd_nm));
11008c2ecf20Sopenharmony_ci	memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
11018c2ecf20Sopenharmony_ci	dent1->name[fname_len(snd_nm)] = '\0';
11028c2ecf20Sopenharmony_ci	set_dent_cookie(c, dent1);
11038c2ecf20Sopenharmony_ci	zero_dent_node_unused(dent1);
11048c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, dent1, dlen1, 0);
11058c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, dent1, hash_dent1);
11068c2ecf20Sopenharmony_ci	if (err)
11078c2ecf20Sopenharmony_ci		goto out_release;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	/* Make new dent for 2nd entry */
11108c2ecf20Sopenharmony_ci	dent2 = (void *)dent1 + aligned_dlen1;
11118c2ecf20Sopenharmony_ci	dent2->ch.node_type = UBIFS_DENT_NODE;
11128c2ecf20Sopenharmony_ci	dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
11138c2ecf20Sopenharmony_ci	dent2->inum = cpu_to_le64(snd_inode->i_ino);
11148c2ecf20Sopenharmony_ci	dent2->type = get_dent_type(snd_inode->i_mode);
11158c2ecf20Sopenharmony_ci	dent2->nlen = cpu_to_le16(fname_len(fst_nm));
11168c2ecf20Sopenharmony_ci	memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
11178c2ecf20Sopenharmony_ci	dent2->name[fname_len(fst_nm)] = '\0';
11188c2ecf20Sopenharmony_ci	set_dent_cookie(c, dent2);
11198c2ecf20Sopenharmony_ci	zero_dent_node_unused(dent2);
11208c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, dent2, dlen2, 0);
11218c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, dent2, hash_dent2);
11228c2ecf20Sopenharmony_ci	if (err)
11238c2ecf20Sopenharmony_ci		goto out_release;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	p = (void *)dent2 + aligned_dlen2;
11268c2ecf20Sopenharmony_ci	if (!twoparents) {
11278c2ecf20Sopenharmony_ci		pack_inode(c, p, fst_dir, 1);
11288c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_p1);
11298c2ecf20Sopenharmony_ci		if (err)
11308c2ecf20Sopenharmony_ci			goto out_release;
11318c2ecf20Sopenharmony_ci	} else {
11328c2ecf20Sopenharmony_ci		pack_inode(c, p, fst_dir, 0);
11338c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_p1);
11348c2ecf20Sopenharmony_ci		if (err)
11358c2ecf20Sopenharmony_ci			goto out_release;
11368c2ecf20Sopenharmony_ci		p += ALIGN(plen, 8);
11378c2ecf20Sopenharmony_ci		pack_inode(c, p, snd_dir, 1);
11388c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_p2);
11398c2ecf20Sopenharmony_ci		if (err)
11408c2ecf20Sopenharmony_ci			goto out_release;
11418c2ecf20Sopenharmony_ci	}
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
11448c2ecf20Sopenharmony_ci	if (err)
11458c2ecf20Sopenharmony_ci		goto out_release;
11468c2ecf20Sopenharmony_ci	if (!sync) {
11478c2ecf20Sopenharmony_ci		struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
11508c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
11518c2ecf20Sopenharmony_ci	}
11528c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
11578c2ecf20Sopenharmony_ci	err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
11588c2ecf20Sopenharmony_ci	if (err)
11598c2ecf20Sopenharmony_ci		goto out_ro;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	offs += aligned_dlen1;
11628c2ecf20Sopenharmony_ci	dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
11638c2ecf20Sopenharmony_ci	err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
11648c2ecf20Sopenharmony_ci	if (err)
11658c2ecf20Sopenharmony_ci		goto out_ro;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	offs += aligned_dlen2;
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	ino_key_init(c, &key, fst_dir->i_ino);
11708c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
11718c2ecf20Sopenharmony_ci	if (err)
11728c2ecf20Sopenharmony_ci		goto out_ro;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	if (twoparents) {
11758c2ecf20Sopenharmony_ci		offs += ALIGN(plen, 8);
11768c2ecf20Sopenharmony_ci		ino_key_init(c, &key, snd_dir->i_ino);
11778c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
11788c2ecf20Sopenharmony_ci		if (err)
11798c2ecf20Sopenharmony_ci			goto out_ro;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	finish_reservation(c);
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	mark_inode_clean(c, ubifs_inode(fst_dir));
11858c2ecf20Sopenharmony_ci	if (twoparents)
11868c2ecf20Sopenharmony_ci		mark_inode_clean(c, ubifs_inode(snd_dir));
11878c2ecf20Sopenharmony_ci	kfree(dent1);
11888c2ecf20Sopenharmony_ci	return 0;
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ciout_release:
11918c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
11928c2ecf20Sopenharmony_ciout_ro:
11938c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
11948c2ecf20Sopenharmony_ci	finish_reservation(c);
11958c2ecf20Sopenharmony_ciout_free:
11968c2ecf20Sopenharmony_ci	kfree(dent1);
11978c2ecf20Sopenharmony_ci	return err;
11988c2ecf20Sopenharmony_ci}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci/**
12018c2ecf20Sopenharmony_ci * ubifs_jnl_rename - rename a directory entry.
12028c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
12038c2ecf20Sopenharmony_ci * @old_dir: parent inode of directory entry to rename
12048c2ecf20Sopenharmony_ci * @old_dentry: directory entry to rename
12058c2ecf20Sopenharmony_ci * @new_dir: parent inode of directory entry to rename
12068c2ecf20Sopenharmony_ci * @new_dentry: new directory entry (or directory entry to replace)
12078c2ecf20Sopenharmony_ci * @sync: non-zero if the write-buffer has to be synchronized
12088c2ecf20Sopenharmony_ci *
12098c2ecf20Sopenharmony_ci * This function implements the re-name operation which may involve writing up
12108c2ecf20Sopenharmony_ci * to 4 inodes(new inode, whiteout inode, old and new parent directory inodes)
12118c2ecf20Sopenharmony_ci * and 2 directory entries. It marks the written inodes as clean and returns
12128c2ecf20Sopenharmony_ci * zero on success. In case of failure, a negative error code is returned.
12138c2ecf20Sopenharmony_ci */
12148c2ecf20Sopenharmony_ciint ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
12158c2ecf20Sopenharmony_ci		     const struct inode *old_inode,
12168c2ecf20Sopenharmony_ci		     const struct fscrypt_name *old_nm,
12178c2ecf20Sopenharmony_ci		     const struct inode *new_dir,
12188c2ecf20Sopenharmony_ci		     const struct inode *new_inode,
12198c2ecf20Sopenharmony_ci		     const struct fscrypt_name *new_nm,
12208c2ecf20Sopenharmony_ci		     const struct inode *whiteout, int sync)
12218c2ecf20Sopenharmony_ci{
12228c2ecf20Sopenharmony_ci	void *p;
12238c2ecf20Sopenharmony_ci	union ubifs_key key;
12248c2ecf20Sopenharmony_ci	struct ubifs_dent_node *dent, *dent2;
12258c2ecf20Sopenharmony_ci	int err, dlen1, dlen2, ilen, wlen, lnum, offs, len, orphan_added = 0;
12268c2ecf20Sopenharmony_ci	int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
12278c2ecf20Sopenharmony_ci	int last_reference = !!(new_inode && new_inode->i_nlink == 0);
12288c2ecf20Sopenharmony_ci	int move = (old_dir != new_dir);
12298c2ecf20Sopenharmony_ci	struct ubifs_inode *new_ui, *whiteout_ui;
12308c2ecf20Sopenharmony_ci	u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
12318c2ecf20Sopenharmony_ci	u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
12328c2ecf20Sopenharmony_ci	u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
12338c2ecf20Sopenharmony_ci	u8 hash_whiteout_inode[UBIFS_HASH_ARR_SZ];
12348c2ecf20Sopenharmony_ci	u8 hash_dent1[UBIFS_HASH_ARR_SZ];
12358c2ecf20Sopenharmony_ci	u8 hash_dent2[UBIFS_HASH_ARR_SZ];
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
12388c2ecf20Sopenharmony_ci	ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
12398c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
12408c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
12438c2ecf20Sopenharmony_ci	dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
12448c2ecf20Sopenharmony_ci	if (new_inode) {
12458c2ecf20Sopenharmony_ci		new_ui = ubifs_inode(new_inode);
12468c2ecf20Sopenharmony_ci		ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
12478c2ecf20Sopenharmony_ci		ilen = UBIFS_INO_NODE_SZ;
12488c2ecf20Sopenharmony_ci		if (!last_reference)
12498c2ecf20Sopenharmony_ci			ilen += new_ui->data_len;
12508c2ecf20Sopenharmony_ci	} else
12518c2ecf20Sopenharmony_ci		ilen = 0;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (whiteout) {
12548c2ecf20Sopenharmony_ci		whiteout_ui = ubifs_inode(whiteout);
12558c2ecf20Sopenharmony_ci		ubifs_assert(c, mutex_is_locked(&whiteout_ui->ui_mutex));
12568c2ecf20Sopenharmony_ci		ubifs_assert(c, whiteout->i_nlink == 1);
12578c2ecf20Sopenharmony_ci		ubifs_assert(c, !whiteout_ui->dirty);
12588c2ecf20Sopenharmony_ci		wlen = UBIFS_INO_NODE_SZ;
12598c2ecf20Sopenharmony_ci		wlen += whiteout_ui->data_len;
12608c2ecf20Sopenharmony_ci	} else
12618c2ecf20Sopenharmony_ci		wlen = 0;
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	aligned_dlen1 = ALIGN(dlen1, 8);
12648c2ecf20Sopenharmony_ci	aligned_dlen2 = ALIGN(dlen2, 8);
12658c2ecf20Sopenharmony_ci	len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) +
12668c2ecf20Sopenharmony_ci	      ALIGN(wlen, 8) + ALIGN(plen, 8);
12678c2ecf20Sopenharmony_ci	if (move)
12688c2ecf20Sopenharmony_ci		len += plen;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	len += ubifs_auth_node_sz(c);
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	dent = kzalloc(len, GFP_NOFS);
12738c2ecf20Sopenharmony_ci	if (!dent)
12748c2ecf20Sopenharmony_ci		return -ENOMEM;
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
12778c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, len);
12788c2ecf20Sopenharmony_ci	if (err)
12798c2ecf20Sopenharmony_ci		goto out_free;
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci	/* Make new dent */
12828c2ecf20Sopenharmony_ci	dent->ch.node_type = UBIFS_DENT_NODE;
12838c2ecf20Sopenharmony_ci	dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
12848c2ecf20Sopenharmony_ci	dent->inum = cpu_to_le64(old_inode->i_ino);
12858c2ecf20Sopenharmony_ci	dent->type = get_dent_type(old_inode->i_mode);
12868c2ecf20Sopenharmony_ci	dent->nlen = cpu_to_le16(fname_len(new_nm));
12878c2ecf20Sopenharmony_ci	memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
12888c2ecf20Sopenharmony_ci	dent->name[fname_len(new_nm)] = '\0';
12898c2ecf20Sopenharmony_ci	set_dent_cookie(c, dent);
12908c2ecf20Sopenharmony_ci	zero_dent_node_unused(dent);
12918c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, dent, dlen1, 0);
12928c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, dent, hash_dent1);
12938c2ecf20Sopenharmony_ci	if (err)
12948c2ecf20Sopenharmony_ci		goto out_release;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	dent2 = (void *)dent + aligned_dlen1;
12978c2ecf20Sopenharmony_ci	dent2->ch.node_type = UBIFS_DENT_NODE;
12988c2ecf20Sopenharmony_ci	dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	if (whiteout) {
13018c2ecf20Sopenharmony_ci		dent2->inum = cpu_to_le64(whiteout->i_ino);
13028c2ecf20Sopenharmony_ci		dent2->type = get_dent_type(whiteout->i_mode);
13038c2ecf20Sopenharmony_ci	} else {
13048c2ecf20Sopenharmony_ci		/* Make deletion dent */
13058c2ecf20Sopenharmony_ci		dent2->inum = 0;
13068c2ecf20Sopenharmony_ci		dent2->type = DT_UNKNOWN;
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci	dent2->nlen = cpu_to_le16(fname_len(old_nm));
13098c2ecf20Sopenharmony_ci	memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
13108c2ecf20Sopenharmony_ci	dent2->name[fname_len(old_nm)] = '\0';
13118c2ecf20Sopenharmony_ci	set_dent_cookie(c, dent2);
13128c2ecf20Sopenharmony_ci	zero_dent_node_unused(dent2);
13138c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, dent2, dlen2, 0);
13148c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, dent2, hash_dent2);
13158c2ecf20Sopenharmony_ci	if (err)
13168c2ecf20Sopenharmony_ci		goto out_release;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	p = (void *)dent2 + aligned_dlen2;
13198c2ecf20Sopenharmony_ci	if (new_inode) {
13208c2ecf20Sopenharmony_ci		pack_inode(c, p, new_inode, 0);
13218c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_new_inode);
13228c2ecf20Sopenharmony_ci		if (err)
13238c2ecf20Sopenharmony_ci			goto out_release;
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci		p += ALIGN(ilen, 8);
13268c2ecf20Sopenharmony_ci	}
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	if (whiteout) {
13298c2ecf20Sopenharmony_ci		pack_inode(c, p, whiteout, 0);
13308c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_whiteout_inode);
13318c2ecf20Sopenharmony_ci		if (err)
13328c2ecf20Sopenharmony_ci			goto out_release;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci		p += ALIGN(wlen, 8);
13358c2ecf20Sopenharmony_ci	}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (!move) {
13388c2ecf20Sopenharmony_ci		pack_inode(c, p, old_dir, 1);
13398c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_old_dir);
13408c2ecf20Sopenharmony_ci		if (err)
13418c2ecf20Sopenharmony_ci			goto out_release;
13428c2ecf20Sopenharmony_ci	} else {
13438c2ecf20Sopenharmony_ci		pack_inode(c, p, old_dir, 0);
13448c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_old_dir);
13458c2ecf20Sopenharmony_ci		if (err)
13468c2ecf20Sopenharmony_ci			goto out_release;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci		p += ALIGN(plen, 8);
13498c2ecf20Sopenharmony_ci		pack_inode(c, p, new_dir, 1);
13508c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, p, hash_new_dir);
13518c2ecf20Sopenharmony_ci		if (err)
13528c2ecf20Sopenharmony_ci			goto out_release;
13538c2ecf20Sopenharmony_ci	}
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	if (last_reference) {
13568c2ecf20Sopenharmony_ci		err = ubifs_add_orphan(c, new_inode->i_ino);
13578c2ecf20Sopenharmony_ci		if (err) {
13588c2ecf20Sopenharmony_ci			release_head(c, BASEHD);
13598c2ecf20Sopenharmony_ci			goto out_finish;
13608c2ecf20Sopenharmony_ci		}
13618c2ecf20Sopenharmony_ci		new_ui->del_cmtno = c->cmt_no;
13628c2ecf20Sopenharmony_ci		orphan_added = 1;
13638c2ecf20Sopenharmony_ci	}
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
13668c2ecf20Sopenharmony_ci	if (err)
13678c2ecf20Sopenharmony_ci		goto out_release;
13688c2ecf20Sopenharmony_ci	if (!sync) {
13698c2ecf20Sopenharmony_ci		struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
13728c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
13738c2ecf20Sopenharmony_ci		if (new_inode)
13748c2ecf20Sopenharmony_ci			ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
13758c2ecf20Sopenharmony_ci						  new_inode->i_ino);
13768c2ecf20Sopenharmony_ci		if (whiteout)
13778c2ecf20Sopenharmony_ci			ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
13788c2ecf20Sopenharmony_ci						  whiteout->i_ino);
13798c2ecf20Sopenharmony_ci	}
13808c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	dent_key_init(c, &key, new_dir->i_ino, new_nm);
13858c2ecf20Sopenharmony_ci	err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
13868c2ecf20Sopenharmony_ci	if (err)
13878c2ecf20Sopenharmony_ci		goto out_ro;
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	offs += aligned_dlen1;
13908c2ecf20Sopenharmony_ci	if (whiteout) {
13918c2ecf20Sopenharmony_ci		dent_key_init(c, &key, old_dir->i_ino, old_nm);
13928c2ecf20Sopenharmony_ci		err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
13938c2ecf20Sopenharmony_ci		if (err)
13948c2ecf20Sopenharmony_ci			goto out_ro;
13958c2ecf20Sopenharmony_ci	} else {
13968c2ecf20Sopenharmony_ci		err = ubifs_add_dirt(c, lnum, dlen2);
13978c2ecf20Sopenharmony_ci		if (err)
13988c2ecf20Sopenharmony_ci			goto out_ro;
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci		dent_key_init(c, &key, old_dir->i_ino, old_nm);
14018c2ecf20Sopenharmony_ci		err = ubifs_tnc_remove_nm(c, &key, old_nm);
14028c2ecf20Sopenharmony_ci		if (err)
14038c2ecf20Sopenharmony_ci			goto out_ro;
14048c2ecf20Sopenharmony_ci	}
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	offs += aligned_dlen2;
14078c2ecf20Sopenharmony_ci	if (new_inode) {
14088c2ecf20Sopenharmony_ci		ino_key_init(c, &key, new_inode->i_ino);
14098c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
14108c2ecf20Sopenharmony_ci		if (err)
14118c2ecf20Sopenharmony_ci			goto out_ro;
14128c2ecf20Sopenharmony_ci		offs += ALIGN(ilen, 8);
14138c2ecf20Sopenharmony_ci	}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	if (whiteout) {
14168c2ecf20Sopenharmony_ci		ino_key_init(c, &key, whiteout->i_ino);
14178c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, offs, wlen,
14188c2ecf20Sopenharmony_ci				    hash_whiteout_inode);
14198c2ecf20Sopenharmony_ci		if (err)
14208c2ecf20Sopenharmony_ci			goto out_ro;
14218c2ecf20Sopenharmony_ci		offs += ALIGN(wlen, 8);
14228c2ecf20Sopenharmony_ci	}
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	ino_key_init(c, &key, old_dir->i_ino);
14258c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
14268c2ecf20Sopenharmony_ci	if (err)
14278c2ecf20Sopenharmony_ci		goto out_ro;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	if (move) {
14308c2ecf20Sopenharmony_ci		offs += ALIGN(plen, 8);
14318c2ecf20Sopenharmony_ci		ino_key_init(c, &key, new_dir->i_ino);
14328c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
14338c2ecf20Sopenharmony_ci		if (err)
14348c2ecf20Sopenharmony_ci			goto out_ro;
14358c2ecf20Sopenharmony_ci	}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	finish_reservation(c);
14388c2ecf20Sopenharmony_ci	if (new_inode) {
14398c2ecf20Sopenharmony_ci		mark_inode_clean(c, new_ui);
14408c2ecf20Sopenharmony_ci		spin_lock(&new_ui->ui_lock);
14418c2ecf20Sopenharmony_ci		new_ui->synced_i_size = new_ui->ui_size;
14428c2ecf20Sopenharmony_ci		spin_unlock(&new_ui->ui_lock);
14438c2ecf20Sopenharmony_ci	}
14448c2ecf20Sopenharmony_ci	/*
14458c2ecf20Sopenharmony_ci	 * No need to mark whiteout inode clean.
14468c2ecf20Sopenharmony_ci	 * Whiteout don't have non-zero size, no need to update
14478c2ecf20Sopenharmony_ci	 * synced_i_size for whiteout_ui.
14488c2ecf20Sopenharmony_ci	 */
14498c2ecf20Sopenharmony_ci	mark_inode_clean(c, ubifs_inode(old_dir));
14508c2ecf20Sopenharmony_ci	if (move)
14518c2ecf20Sopenharmony_ci		mark_inode_clean(c, ubifs_inode(new_dir));
14528c2ecf20Sopenharmony_ci	kfree(dent);
14538c2ecf20Sopenharmony_ci	return 0;
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ciout_release:
14568c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
14578c2ecf20Sopenharmony_ciout_ro:
14588c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
14598c2ecf20Sopenharmony_ci	if (orphan_added)
14608c2ecf20Sopenharmony_ci		ubifs_delete_orphan(c, new_inode->i_ino);
14618c2ecf20Sopenharmony_ciout_finish:
14628c2ecf20Sopenharmony_ci	finish_reservation(c);
14638c2ecf20Sopenharmony_ciout_free:
14648c2ecf20Sopenharmony_ci	kfree(dent);
14658c2ecf20Sopenharmony_ci	return err;
14668c2ecf20Sopenharmony_ci}
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci/**
14698c2ecf20Sopenharmony_ci * truncate_data_node - re-compress/encrypt a truncated data node.
14708c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
14718c2ecf20Sopenharmony_ci * @inode: inode which referes to the data node
14728c2ecf20Sopenharmony_ci * @block: data block number
14738c2ecf20Sopenharmony_ci * @dn: data node to re-compress
14748c2ecf20Sopenharmony_ci * @new_len: new length
14758c2ecf20Sopenharmony_ci *
14768c2ecf20Sopenharmony_ci * This function is used when an inode is truncated and the last data node of
14778c2ecf20Sopenharmony_ci * the inode has to be re-compressed/encrypted and re-written.
14788c2ecf20Sopenharmony_ci */
14798c2ecf20Sopenharmony_cistatic int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
14808c2ecf20Sopenharmony_ci			      unsigned int block, struct ubifs_data_node *dn,
14818c2ecf20Sopenharmony_ci			      int *new_len)
14828c2ecf20Sopenharmony_ci{
14838c2ecf20Sopenharmony_ci	void *buf;
14848c2ecf20Sopenharmony_ci	int err, dlen, compr_type, out_len, old_dlen;
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	out_len = le32_to_cpu(dn->size);
14878c2ecf20Sopenharmony_ci	buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
14888c2ecf20Sopenharmony_ci	if (!buf)
14898c2ecf20Sopenharmony_ci		return -ENOMEM;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
14928c2ecf20Sopenharmony_ci	compr_type = le16_to_cpu(dn->compr_type);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	if (IS_ENCRYPTED(inode)) {
14958c2ecf20Sopenharmony_ci		err = ubifs_decrypt(inode, dn, &dlen, block);
14968c2ecf20Sopenharmony_ci		if (err)
14978c2ecf20Sopenharmony_ci			goto out;
14988c2ecf20Sopenharmony_ci	}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci	if (compr_type == UBIFS_COMPR_NONE) {
15018c2ecf20Sopenharmony_ci		out_len = *new_len;
15028c2ecf20Sopenharmony_ci	} else {
15038c2ecf20Sopenharmony_ci		err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
15048c2ecf20Sopenharmony_ci		if (err)
15058c2ecf20Sopenharmony_ci			goto out;
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci		ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
15088c2ecf20Sopenharmony_ci	}
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	if (IS_ENCRYPTED(inode)) {
15118c2ecf20Sopenharmony_ci		err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
15128c2ecf20Sopenharmony_ci		if (err)
15138c2ecf20Sopenharmony_ci			goto out;
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci		out_len = old_dlen;
15168c2ecf20Sopenharmony_ci	} else {
15178c2ecf20Sopenharmony_ci		dn->compr_size = 0;
15188c2ecf20Sopenharmony_ci	}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
15218c2ecf20Sopenharmony_ci	dn->compr_type = cpu_to_le16(compr_type);
15228c2ecf20Sopenharmony_ci	dn->size = cpu_to_le32(*new_len);
15238c2ecf20Sopenharmony_ci	*new_len = UBIFS_DATA_NODE_SZ + out_len;
15248c2ecf20Sopenharmony_ci	err = 0;
15258c2ecf20Sopenharmony_ciout:
15268c2ecf20Sopenharmony_ci	kfree(buf);
15278c2ecf20Sopenharmony_ci	return err;
15288c2ecf20Sopenharmony_ci}
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci/**
15318c2ecf20Sopenharmony_ci * ubifs_jnl_truncate - update the journal for a truncation.
15328c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
15338c2ecf20Sopenharmony_ci * @inode: inode to truncate
15348c2ecf20Sopenharmony_ci * @old_size: old size
15358c2ecf20Sopenharmony_ci * @new_size: new size
15368c2ecf20Sopenharmony_ci *
15378c2ecf20Sopenharmony_ci * When the size of a file decreases due to truncation, a truncation node is
15388c2ecf20Sopenharmony_ci * written, the journal tree is updated, and the last data block is re-written
15398c2ecf20Sopenharmony_ci * if it has been affected. The inode is also updated in order to synchronize
15408c2ecf20Sopenharmony_ci * the new inode size.
15418c2ecf20Sopenharmony_ci *
15428c2ecf20Sopenharmony_ci * This function marks the inode as clean and returns zero on success. In case
15438c2ecf20Sopenharmony_ci * of failure, a negative error code is returned.
15448c2ecf20Sopenharmony_ci */
15458c2ecf20Sopenharmony_ciint ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
15468c2ecf20Sopenharmony_ci		       loff_t old_size, loff_t new_size)
15478c2ecf20Sopenharmony_ci{
15488c2ecf20Sopenharmony_ci	union ubifs_key key, to_key;
15498c2ecf20Sopenharmony_ci	struct ubifs_ino_node *ino;
15508c2ecf20Sopenharmony_ci	struct ubifs_trun_node *trun;
15518c2ecf20Sopenharmony_ci	struct ubifs_data_node *dn;
15528c2ecf20Sopenharmony_ci	int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
15538c2ecf20Sopenharmony_ci	struct ubifs_inode *ui = ubifs_inode(inode);
15548c2ecf20Sopenharmony_ci	ino_t inum = inode->i_ino;
15558c2ecf20Sopenharmony_ci	unsigned int blk;
15568c2ecf20Sopenharmony_ci	u8 hash_ino[UBIFS_HASH_ARR_SZ];
15578c2ecf20Sopenharmony_ci	u8 hash_dn[UBIFS_HASH_ARR_SZ];
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	dbg_jnl("ino %lu, size %lld -> %lld",
15608c2ecf20Sopenharmony_ci		(unsigned long)inum, old_size, new_size);
15618c2ecf20Sopenharmony_ci	ubifs_assert(c, !ui->data_len);
15628c2ecf20Sopenharmony_ci	ubifs_assert(c, S_ISREG(inode->i_mode));
15638c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
15668c2ecf20Sopenharmony_ci	     UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	sz += ubifs_auth_node_sz(c);
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	ino = kmalloc(sz, GFP_NOFS);
15718c2ecf20Sopenharmony_ci	if (!ino)
15728c2ecf20Sopenharmony_ci		return -ENOMEM;
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	trun = (void *)ino + UBIFS_INO_NODE_SZ;
15758c2ecf20Sopenharmony_ci	trun->ch.node_type = UBIFS_TRUN_NODE;
15768c2ecf20Sopenharmony_ci	trun->inum = cpu_to_le32(inum);
15778c2ecf20Sopenharmony_ci	trun->old_size = cpu_to_le64(old_size);
15788c2ecf20Sopenharmony_ci	trun->new_size = cpu_to_le64(new_size);
15798c2ecf20Sopenharmony_ci	zero_trun_node_unused(trun);
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci	dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
15828c2ecf20Sopenharmony_ci	if (dlen) {
15838c2ecf20Sopenharmony_ci		/* Get last data block so it can be truncated */
15848c2ecf20Sopenharmony_ci		dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
15858c2ecf20Sopenharmony_ci		blk = new_size >> UBIFS_BLOCK_SHIFT;
15868c2ecf20Sopenharmony_ci		data_key_init(c, &key, inum, blk);
15878c2ecf20Sopenharmony_ci		dbg_jnlk(&key, "last block key ");
15888c2ecf20Sopenharmony_ci		err = ubifs_tnc_lookup(c, &key, dn);
15898c2ecf20Sopenharmony_ci		if (err == -ENOENT)
15908c2ecf20Sopenharmony_ci			dlen = 0; /* Not found (so it is a hole) */
15918c2ecf20Sopenharmony_ci		else if (err)
15928c2ecf20Sopenharmony_ci			goto out_free;
15938c2ecf20Sopenharmony_ci		else {
15948c2ecf20Sopenharmony_ci			int dn_len = le32_to_cpu(dn->size);
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci			if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
15978c2ecf20Sopenharmony_ci				ubifs_err(c, "bad data node (block %u, inode %lu)",
15988c2ecf20Sopenharmony_ci					  blk, inode->i_ino);
15998c2ecf20Sopenharmony_ci				ubifs_dump_node(c, dn, sz - UBIFS_INO_NODE_SZ -
16008c2ecf20Sopenharmony_ci						UBIFS_TRUN_NODE_SZ);
16018c2ecf20Sopenharmony_ci				goto out_free;
16028c2ecf20Sopenharmony_ci			}
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci			if (dn_len <= dlen)
16058c2ecf20Sopenharmony_ci				dlen = 0; /* Nothing to do */
16068c2ecf20Sopenharmony_ci			else {
16078c2ecf20Sopenharmony_ci				err = truncate_data_node(c, inode, blk, dn, &dlen);
16088c2ecf20Sopenharmony_ci				if (err)
16098c2ecf20Sopenharmony_ci					goto out_free;
16108c2ecf20Sopenharmony_ci			}
16118c2ecf20Sopenharmony_ci		}
16128c2ecf20Sopenharmony_ci	}
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	/* Must make reservation before allocating sequence numbers */
16158c2ecf20Sopenharmony_ci	len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci	if (ubifs_authenticated(c))
16188c2ecf20Sopenharmony_ci		len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
16198c2ecf20Sopenharmony_ci	else
16208c2ecf20Sopenharmony_ci		len += dlen;
16218c2ecf20Sopenharmony_ci
16228c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, len);
16238c2ecf20Sopenharmony_ci	if (err)
16248c2ecf20Sopenharmony_ci		goto out_free;
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	pack_inode(c, ino, inode, 0);
16278c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash_ino);
16288c2ecf20Sopenharmony_ci	if (err)
16298c2ecf20Sopenharmony_ci		goto out_release;
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
16328c2ecf20Sopenharmony_ci	if (dlen) {
16338c2ecf20Sopenharmony_ci		ubifs_prep_grp_node(c, dn, dlen, 1);
16348c2ecf20Sopenharmony_ci		err = ubifs_node_calc_hash(c, dn, hash_dn);
16358c2ecf20Sopenharmony_ci		if (err)
16368c2ecf20Sopenharmony_ci			goto out_release;
16378c2ecf20Sopenharmony_ci	}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
16408c2ecf20Sopenharmony_ci	if (err)
16418c2ecf20Sopenharmony_ci		goto out_release;
16428c2ecf20Sopenharmony_ci	if (!sync)
16438c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
16448c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci	if (dlen) {
16498c2ecf20Sopenharmony_ci		sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
16508c2ecf20Sopenharmony_ci		err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
16518c2ecf20Sopenharmony_ci		if (err)
16528c2ecf20Sopenharmony_ci			goto out_ro;
16538c2ecf20Sopenharmony_ci	}
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci	ino_key_init(c, &key, inum);
16568c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
16578c2ecf20Sopenharmony_ci	if (err)
16588c2ecf20Sopenharmony_ci		goto out_ro;
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_ci	err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
16618c2ecf20Sopenharmony_ci	if (err)
16628c2ecf20Sopenharmony_ci		goto out_ro;
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ci	bit = new_size & (UBIFS_BLOCK_SIZE - 1);
16658c2ecf20Sopenharmony_ci	blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
16668c2ecf20Sopenharmony_ci	data_key_init(c, &key, inum, blk);
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci	bit = old_size & (UBIFS_BLOCK_SIZE - 1);
16698c2ecf20Sopenharmony_ci	blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
16708c2ecf20Sopenharmony_ci	data_key_init(c, &to_key, inum, blk);
16718c2ecf20Sopenharmony_ci
16728c2ecf20Sopenharmony_ci	err = ubifs_tnc_remove_range(c, &key, &to_key);
16738c2ecf20Sopenharmony_ci	if (err)
16748c2ecf20Sopenharmony_ci		goto out_ro;
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_ci	finish_reservation(c);
16778c2ecf20Sopenharmony_ci	spin_lock(&ui->ui_lock);
16788c2ecf20Sopenharmony_ci	ui->synced_i_size = ui->ui_size;
16798c2ecf20Sopenharmony_ci	spin_unlock(&ui->ui_lock);
16808c2ecf20Sopenharmony_ci	mark_inode_clean(c, ui);
16818c2ecf20Sopenharmony_ci	kfree(ino);
16828c2ecf20Sopenharmony_ci	return 0;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ciout_release:
16858c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
16868c2ecf20Sopenharmony_ciout_ro:
16878c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
16888c2ecf20Sopenharmony_ci	finish_reservation(c);
16898c2ecf20Sopenharmony_ciout_free:
16908c2ecf20Sopenharmony_ci	kfree(ino);
16918c2ecf20Sopenharmony_ci	return err;
16928c2ecf20Sopenharmony_ci}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci/**
16968c2ecf20Sopenharmony_ci * ubifs_jnl_delete_xattr - delete an extended attribute.
16978c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
16988c2ecf20Sopenharmony_ci * @host: host inode
16998c2ecf20Sopenharmony_ci * @inode: extended attribute inode
17008c2ecf20Sopenharmony_ci * @nm: extended attribute entry name
17018c2ecf20Sopenharmony_ci *
17028c2ecf20Sopenharmony_ci * This function delete an extended attribute which is very similar to
17038c2ecf20Sopenharmony_ci * un-linking regular files - it writes a deletion xentry, a deletion inode and
17048c2ecf20Sopenharmony_ci * updates the target inode. Returns zero in case of success and a negative
17058c2ecf20Sopenharmony_ci * error code in case of failure.
17068c2ecf20Sopenharmony_ci */
17078c2ecf20Sopenharmony_ciint ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
17088c2ecf20Sopenharmony_ci			   const struct inode *inode,
17098c2ecf20Sopenharmony_ci			   const struct fscrypt_name *nm)
17108c2ecf20Sopenharmony_ci{
17118c2ecf20Sopenharmony_ci	int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
17128c2ecf20Sopenharmony_ci	struct ubifs_dent_node *xent;
17138c2ecf20Sopenharmony_ci	struct ubifs_ino_node *ino;
17148c2ecf20Sopenharmony_ci	union ubifs_key xent_key, key1, key2;
17158c2ecf20Sopenharmony_ci	int sync = IS_DIRSYNC(host);
17168c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(host);
17178c2ecf20Sopenharmony_ci	u8 hash[UBIFS_HASH_ARR_SZ];
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	ubifs_assert(c, inode->i_nlink == 0);
17208c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ci	/*
17238c2ecf20Sopenharmony_ci	 * Since we are deleting the inode, we do not bother to attach any data
17248c2ecf20Sopenharmony_ci	 * to it and assume its length is %UBIFS_INO_NODE_SZ.
17258c2ecf20Sopenharmony_ci	 */
17268c2ecf20Sopenharmony_ci	xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
17278c2ecf20Sopenharmony_ci	aligned_xlen = ALIGN(xlen, 8);
17288c2ecf20Sopenharmony_ci	hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
17298c2ecf20Sopenharmony_ci	len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci	write_len = len + ubifs_auth_node_sz(c);
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	xent = kzalloc(write_len, GFP_NOFS);
17348c2ecf20Sopenharmony_ci	if (!xent)
17358c2ecf20Sopenharmony_ci		return -ENOMEM;
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
17388c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, write_len);
17398c2ecf20Sopenharmony_ci	if (err) {
17408c2ecf20Sopenharmony_ci		kfree(xent);
17418c2ecf20Sopenharmony_ci		return err;
17428c2ecf20Sopenharmony_ci	}
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	xent->ch.node_type = UBIFS_XENT_NODE;
17458c2ecf20Sopenharmony_ci	xent_key_init(c, &xent_key, host->i_ino, nm);
17468c2ecf20Sopenharmony_ci	key_write(c, &xent_key, xent->key);
17478c2ecf20Sopenharmony_ci	xent->inum = 0;
17488c2ecf20Sopenharmony_ci	xent->type = get_dent_type(inode->i_mode);
17498c2ecf20Sopenharmony_ci	xent->nlen = cpu_to_le16(fname_len(nm));
17508c2ecf20Sopenharmony_ci	memcpy(xent->name, fname_name(nm), fname_len(nm));
17518c2ecf20Sopenharmony_ci	xent->name[fname_len(nm)] = '\0';
17528c2ecf20Sopenharmony_ci	zero_dent_node_unused(xent);
17538c2ecf20Sopenharmony_ci	ubifs_prep_grp_node(c, xent, xlen, 0);
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	ino = (void *)xent + aligned_xlen;
17568c2ecf20Sopenharmony_ci	pack_inode(c, ino, inode, 0);
17578c2ecf20Sopenharmony_ci	ino = (void *)ino + UBIFS_INO_NODE_SZ;
17588c2ecf20Sopenharmony_ci	pack_inode(c, ino, host, 1);
17598c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash);
17608c2ecf20Sopenharmony_ci	if (err)
17618c2ecf20Sopenharmony_ci		goto out_release;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
17648c2ecf20Sopenharmony_ci	if (!sync && !err)
17658c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
17668c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
17698c2ecf20Sopenharmony_ci	kfree(xent);
17708c2ecf20Sopenharmony_ci	if (err)
17718c2ecf20Sopenharmony_ci		goto out_ro;
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	/* Remove the extended attribute entry from TNC */
17748c2ecf20Sopenharmony_ci	err = ubifs_tnc_remove_nm(c, &xent_key, nm);
17758c2ecf20Sopenharmony_ci	if (err)
17768c2ecf20Sopenharmony_ci		goto out_ro;
17778c2ecf20Sopenharmony_ci	err = ubifs_add_dirt(c, lnum, xlen);
17788c2ecf20Sopenharmony_ci	if (err)
17798c2ecf20Sopenharmony_ci		goto out_ro;
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	/*
17828c2ecf20Sopenharmony_ci	 * Remove all nodes belonging to the extended attribute inode from TNC.
17838c2ecf20Sopenharmony_ci	 * Well, there actually must be only one node - the inode itself.
17848c2ecf20Sopenharmony_ci	 */
17858c2ecf20Sopenharmony_ci	lowest_ino_key(c, &key1, inode->i_ino);
17868c2ecf20Sopenharmony_ci	highest_ino_key(c, &key2, inode->i_ino);
17878c2ecf20Sopenharmony_ci	err = ubifs_tnc_remove_range(c, &key1, &key2);
17888c2ecf20Sopenharmony_ci	if (err)
17898c2ecf20Sopenharmony_ci		goto out_ro;
17908c2ecf20Sopenharmony_ci	err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
17918c2ecf20Sopenharmony_ci	if (err)
17928c2ecf20Sopenharmony_ci		goto out_ro;
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	/* And update TNC with the new host inode position */
17958c2ecf20Sopenharmony_ci	ino_key_init(c, &key1, host->i_ino);
17968c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
17978c2ecf20Sopenharmony_ci	if (err)
17988c2ecf20Sopenharmony_ci		goto out_ro;
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci	finish_reservation(c);
18018c2ecf20Sopenharmony_ci	spin_lock(&host_ui->ui_lock);
18028c2ecf20Sopenharmony_ci	host_ui->synced_i_size = host_ui->ui_size;
18038c2ecf20Sopenharmony_ci	spin_unlock(&host_ui->ui_lock);
18048c2ecf20Sopenharmony_ci	mark_inode_clean(c, host_ui);
18058c2ecf20Sopenharmony_ci	return 0;
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ciout_release:
18088c2ecf20Sopenharmony_ci	kfree(xent);
18098c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
18108c2ecf20Sopenharmony_ciout_ro:
18118c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
18128c2ecf20Sopenharmony_ci	finish_reservation(c);
18138c2ecf20Sopenharmony_ci	return err;
18148c2ecf20Sopenharmony_ci}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci/**
18178c2ecf20Sopenharmony_ci * ubifs_jnl_change_xattr - change an extended attribute.
18188c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object
18198c2ecf20Sopenharmony_ci * @inode: extended attribute inode
18208c2ecf20Sopenharmony_ci * @host: host inode
18218c2ecf20Sopenharmony_ci *
18228c2ecf20Sopenharmony_ci * This function writes the updated version of an extended attribute inode and
18238c2ecf20Sopenharmony_ci * the host inode to the journal (to the base head). The host inode is written
18248c2ecf20Sopenharmony_ci * after the extended attribute inode in order to guarantee that the extended
18258c2ecf20Sopenharmony_ci * attribute will be flushed when the inode is synchronized by 'fsync()' and
18268c2ecf20Sopenharmony_ci * consequently, the write-buffer is synchronized. This function returns zero
18278c2ecf20Sopenharmony_ci * in case of success and a negative error code in case of failure.
18288c2ecf20Sopenharmony_ci */
18298c2ecf20Sopenharmony_ciint ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
18308c2ecf20Sopenharmony_ci			   const struct inode *host)
18318c2ecf20Sopenharmony_ci{
18328c2ecf20Sopenharmony_ci	int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
18338c2ecf20Sopenharmony_ci	struct ubifs_inode *host_ui = ubifs_inode(host);
18348c2ecf20Sopenharmony_ci	struct ubifs_ino_node *ino;
18358c2ecf20Sopenharmony_ci	union ubifs_key key;
18368c2ecf20Sopenharmony_ci	int sync = IS_DIRSYNC(host);
18378c2ecf20Sopenharmony_ci	u8 hash_host[UBIFS_HASH_ARR_SZ];
18388c2ecf20Sopenharmony_ci	u8 hash[UBIFS_HASH_ARR_SZ];
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
18418c2ecf20Sopenharmony_ci	ubifs_assert(c, inode->i_nlink > 0);
18428c2ecf20Sopenharmony_ci	ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
18438c2ecf20Sopenharmony_ci
18448c2ecf20Sopenharmony_ci	len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
18458c2ecf20Sopenharmony_ci	len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
18468c2ecf20Sopenharmony_ci	aligned_len1 = ALIGN(len1, 8);
18478c2ecf20Sopenharmony_ci	aligned_len = aligned_len1 + ALIGN(len2, 8);
18488c2ecf20Sopenharmony_ci
18498c2ecf20Sopenharmony_ci	aligned_len += ubifs_auth_node_sz(c);
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_ci	ino = kzalloc(aligned_len, GFP_NOFS);
18528c2ecf20Sopenharmony_ci	if (!ino)
18538c2ecf20Sopenharmony_ci		return -ENOMEM;
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	/* Make reservation before allocating sequence numbers */
18568c2ecf20Sopenharmony_ci	err = make_reservation(c, BASEHD, aligned_len);
18578c2ecf20Sopenharmony_ci	if (err)
18588c2ecf20Sopenharmony_ci		goto out_free;
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	pack_inode(c, ino, host, 0);
18618c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, ino, hash_host);
18628c2ecf20Sopenharmony_ci	if (err)
18638c2ecf20Sopenharmony_ci		goto out_release;
18648c2ecf20Sopenharmony_ci	pack_inode(c, (void *)ino + aligned_len1, inode, 1);
18658c2ecf20Sopenharmony_ci	err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
18668c2ecf20Sopenharmony_ci	if (err)
18678c2ecf20Sopenharmony_ci		goto out_release;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
18708c2ecf20Sopenharmony_ci	if (!sync && !err) {
18718c2ecf20Sopenharmony_ci		struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
18748c2ecf20Sopenharmony_ci		ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
18758c2ecf20Sopenharmony_ci	}
18768c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
18778c2ecf20Sopenharmony_ci	if (err)
18788c2ecf20Sopenharmony_ci		goto out_ro;
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_ci	ubifs_add_auth_dirt(c, lnum);
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ci	ino_key_init(c, &key, host->i_ino);
18838c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
18848c2ecf20Sopenharmony_ci	if (err)
18858c2ecf20Sopenharmony_ci		goto out_ro;
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci	ino_key_init(c, &key, inode->i_ino);
18888c2ecf20Sopenharmony_ci	err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
18898c2ecf20Sopenharmony_ci	if (err)
18908c2ecf20Sopenharmony_ci		goto out_ro;
18918c2ecf20Sopenharmony_ci
18928c2ecf20Sopenharmony_ci	finish_reservation(c);
18938c2ecf20Sopenharmony_ci	spin_lock(&host_ui->ui_lock);
18948c2ecf20Sopenharmony_ci	host_ui->synced_i_size = host_ui->ui_size;
18958c2ecf20Sopenharmony_ci	spin_unlock(&host_ui->ui_lock);
18968c2ecf20Sopenharmony_ci	mark_inode_clean(c, host_ui);
18978c2ecf20Sopenharmony_ci	kfree(ino);
18988c2ecf20Sopenharmony_ci	return 0;
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_ciout_release:
19018c2ecf20Sopenharmony_ci	release_head(c, BASEHD);
19028c2ecf20Sopenharmony_ciout_ro:
19038c2ecf20Sopenharmony_ci	ubifs_ro_mode(c, err);
19048c2ecf20Sopenharmony_ci	finish_reservation(c);
19058c2ecf20Sopenharmony_ciout_free:
19068c2ecf20Sopenharmony_ci	kfree(ino);
19078c2ecf20Sopenharmony_ci	return err;
19088c2ecf20Sopenharmony_ci}
19098c2ecf20Sopenharmony_ci
1910