1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * This file is part of UBIFS.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2006-2008 Nokia Corporation.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Authors: Artem Bityutskiy (Битюцкий Артём)
8*4882a593Smuzhiyun * Adrian Hunter
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * This file implements UBIFS journal.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
15*4882a593Smuzhiyun * length and position, while a bud logical eraseblock is any LEB in the main
16*4882a593Smuzhiyun * area. Buds contain file system data - data nodes, inode nodes, etc. The log
17*4882a593Smuzhiyun * contains only references to buds and some other stuff like commit
18*4882a593Smuzhiyun * start node. The idea is that when we commit the journal, we do
19*4882a593Smuzhiyun * not copy the data, the buds just become indexed. Since after the commit the
20*4882a593Smuzhiyun * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
21*4882a593Smuzhiyun * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
22*4882a593Smuzhiyun * become leafs in the future.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * The journal is multi-headed because we want to write data to the journal as
25*4882a593Smuzhiyun * optimally as possible. It is nice to have nodes belonging to the same inode
26*4882a593Smuzhiyun * in one LEB, so we may write data owned by different inodes to different
27*4882a593Smuzhiyun * journal heads, although at present only one data head is used.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * For recovery reasons, the base head contains all inode nodes, all directory
30*4882a593Smuzhiyun * entry nodes and all truncate nodes. This means that the other heads contain
31*4882a593Smuzhiyun * only data nodes.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * Bud LEBs may be half-indexed. For example, if the bud was not full at the
34*4882a593Smuzhiyun * time of commit, the bud is retained to continue to be used in the journal,
35*4882a593Smuzhiyun * even though the "front" of the LEB is now indexed. In that case, the log
36*4882a593Smuzhiyun * reference contains the offset where the bud starts for the purposes of the
37*4882a593Smuzhiyun * journal.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * The journal size has to be limited, because the larger is the journal, the
40*4882a593Smuzhiyun * longer it takes to mount UBIFS (scanning the journal) and the more memory it
41*4882a593Smuzhiyun * takes (indexing in the TNC).
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * All the journal write operations like 'ubifs_jnl_update()' here, which write
44*4882a593Smuzhiyun * multiple UBIFS nodes to the journal at one go, are atomic with respect to
45*4882a593Smuzhiyun * unclean reboots. Should the unclean reboot happen, the recovery code drops
46*4882a593Smuzhiyun * all the nodes.
47*4882a593Smuzhiyun */
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #include "ubifs.h"
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /**
52*4882a593Smuzhiyun * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
53*4882a593Smuzhiyun * @ino: the inode to zero out
54*4882a593Smuzhiyun */
zero_ino_node_unused(struct ubifs_ino_node * ino)55*4882a593Smuzhiyun static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun memset(ino->padding1, 0, 4);
58*4882a593Smuzhiyun memset(ino->padding2, 0, 26);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun * zero_dent_node_unused - zero out unused fields of an on-flash directory
63*4882a593Smuzhiyun * entry node.
64*4882a593Smuzhiyun * @dent: the directory entry to zero out
65*4882a593Smuzhiyun */
zero_dent_node_unused(struct ubifs_dent_node * dent)66*4882a593Smuzhiyun static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun dent->padding1 = 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * zero_trun_node_unused - zero out unused fields of an on-flash truncation
73*4882a593Smuzhiyun * node.
74*4882a593Smuzhiyun * @trun: the truncation node to zero out
75*4882a593Smuzhiyun */
zero_trun_node_unused(struct ubifs_trun_node * trun)76*4882a593Smuzhiyun static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun memset(trun->padding, 0, 12);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
ubifs_add_auth_dirt(struct ubifs_info * c,int lnum)81*4882a593Smuzhiyun static void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun if (ubifs_authenticated(c))
84*4882a593Smuzhiyun ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /**
88*4882a593Smuzhiyun * reserve_space - reserve space in the journal.
89*4882a593Smuzhiyun * @c: UBIFS file-system description object
90*4882a593Smuzhiyun * @jhead: journal head number
91*4882a593Smuzhiyun * @len: node length
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * This function reserves space in journal head @head. If the reservation
94*4882a593Smuzhiyun * succeeded, the journal head stays locked and later has to be unlocked using
95*4882a593Smuzhiyun * 'release_head()'. Returns zero in case of success, %-EAGAIN if commit has to
96*4882a593Smuzhiyun * be done, and other negative error codes in case of other failures.
97*4882a593Smuzhiyun */
reserve_space(struct ubifs_info * c,int jhead,int len)98*4882a593Smuzhiyun static int reserve_space(struct ubifs_info *c, int jhead, int len)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
101*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Typically, the base head has smaller nodes written to it, so it is
105*4882a593Smuzhiyun * better to try to allocate space at the ends of eraseblocks. This is
106*4882a593Smuzhiyun * what the squeeze parameter does.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun ubifs_assert(c, !c->ro_media && !c->ro_mount);
109*4882a593Smuzhiyun squeeze = (jhead == BASEHD);
110*4882a593Smuzhiyun again:
111*4882a593Smuzhiyun mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (c->ro_error) {
114*4882a593Smuzhiyun err = -EROFS;
115*4882a593Smuzhiyun goto out_unlock;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun avail = c->leb_size - wbuf->offs - wbuf->used;
119*4882a593Smuzhiyun if (wbuf->lnum != -1 && avail >= len)
120*4882a593Smuzhiyun return 0;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * Write buffer wasn't seek'ed or there is no enough space - look for an
124*4882a593Smuzhiyun * LEB with some empty space.
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun lnum = ubifs_find_free_space(c, len, &offs, squeeze);
127*4882a593Smuzhiyun if (lnum >= 0)
128*4882a593Smuzhiyun goto out;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun err = lnum;
131*4882a593Smuzhiyun if (err != -ENOSPC)
132*4882a593Smuzhiyun goto out_unlock;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * No free space, we have to run garbage collector to make
136*4882a593Smuzhiyun * some. But the write-buffer mutex has to be unlocked because
137*4882a593Smuzhiyun * GC also takes it.
138*4882a593Smuzhiyun */
139*4882a593Smuzhiyun dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
140*4882a593Smuzhiyun mutex_unlock(&wbuf->io_mutex);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun lnum = ubifs_garbage_collect(c, 0);
143*4882a593Smuzhiyun if (lnum < 0) {
144*4882a593Smuzhiyun err = lnum;
145*4882a593Smuzhiyun if (err != -ENOSPC)
146*4882a593Smuzhiyun return err;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * GC could not make a free LEB. But someone else may
150*4882a593Smuzhiyun * have allocated new bud for this journal head,
151*4882a593Smuzhiyun * because we dropped @wbuf->io_mutex, so try once
152*4882a593Smuzhiyun * again.
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun dbg_jnl("GC couldn't make a free LEB for jhead %s",
155*4882a593Smuzhiyun dbg_jhead(jhead));
156*4882a593Smuzhiyun if (retries++ < 2) {
157*4882a593Smuzhiyun dbg_jnl("retry (%d)", retries);
158*4882a593Smuzhiyun goto again;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun dbg_jnl("return -ENOSPC");
162*4882a593Smuzhiyun return err;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
166*4882a593Smuzhiyun dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
167*4882a593Smuzhiyun avail = c->leb_size - wbuf->offs - wbuf->used;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (wbuf->lnum != -1 && avail >= len) {
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * Someone else has switched the journal head and we have
172*4882a593Smuzhiyun * enough space now. This happens when more than one process is
173*4882a593Smuzhiyun * trying to write to the same journal head at the same time.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun dbg_jnl("return LEB %d back, already have LEB %d:%d",
176*4882a593Smuzhiyun lnum, wbuf->lnum, wbuf->offs + wbuf->used);
177*4882a593Smuzhiyun err = ubifs_return_leb(c, lnum);
178*4882a593Smuzhiyun if (err)
179*4882a593Smuzhiyun goto out_unlock;
180*4882a593Smuzhiyun return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun offs = 0;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun out:
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * Make sure we synchronize the write-buffer before we add the new bud
188*4882a593Smuzhiyun * to the log. Otherwise we may have a power cut after the log
189*4882a593Smuzhiyun * reference node for the last bud (@lnum) is written but before the
190*4882a593Smuzhiyun * write-buffer data are written to the next-to-last bud
191*4882a593Smuzhiyun * (@wbuf->lnum). And the effect would be that the recovery would see
192*4882a593Smuzhiyun * that there is corruption in the next-to-last bud.
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun err = ubifs_wbuf_sync_nolock(wbuf);
195*4882a593Smuzhiyun if (err)
196*4882a593Smuzhiyun goto out_return;
197*4882a593Smuzhiyun err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
198*4882a593Smuzhiyun if (err)
199*4882a593Smuzhiyun goto out_return;
200*4882a593Smuzhiyun err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
201*4882a593Smuzhiyun if (err)
202*4882a593Smuzhiyun goto out_unlock;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return 0;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun out_unlock:
207*4882a593Smuzhiyun mutex_unlock(&wbuf->io_mutex);
208*4882a593Smuzhiyun return err;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun out_return:
211*4882a593Smuzhiyun /* An error occurred and the LEB has to be returned to lprops */
212*4882a593Smuzhiyun ubifs_assert(c, err < 0);
213*4882a593Smuzhiyun err1 = ubifs_return_leb(c, lnum);
214*4882a593Smuzhiyun if (err1 && err == -EAGAIN)
215*4882a593Smuzhiyun /*
216*4882a593Smuzhiyun * Return original error code only if it is not %-EAGAIN,
217*4882a593Smuzhiyun * which is not really an error. Otherwise, return the error
218*4882a593Smuzhiyun * code of 'ubifs_return_leb()'.
219*4882a593Smuzhiyun */
220*4882a593Smuzhiyun err = err1;
221*4882a593Smuzhiyun mutex_unlock(&wbuf->io_mutex);
222*4882a593Smuzhiyun return err;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
ubifs_hash_nodes(struct ubifs_info * c,void * node,int len,struct shash_desc * hash)225*4882a593Smuzhiyun static int ubifs_hash_nodes(struct ubifs_info *c, void *node,
226*4882a593Smuzhiyun int len, struct shash_desc *hash)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun int auth_node_size = ubifs_auth_node_sz(c);
229*4882a593Smuzhiyun int err;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun while (1) {
232*4882a593Smuzhiyun const struct ubifs_ch *ch = node;
233*4882a593Smuzhiyun int nodelen = le32_to_cpu(ch->len);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun ubifs_assert(c, len >= auth_node_size);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (len == auth_node_size)
238*4882a593Smuzhiyun break;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun ubifs_assert(c, len > nodelen);
241*4882a593Smuzhiyun ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun err = ubifs_shash_update(c, hash, (void *)node, nodelen);
244*4882a593Smuzhiyun if (err)
245*4882a593Smuzhiyun return err;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun node += ALIGN(nodelen, 8);
248*4882a593Smuzhiyun len -= ALIGN(nodelen, 8);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return ubifs_prepare_auth_node(c, node, hash);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /**
255*4882a593Smuzhiyun * write_head - write data to a journal head.
256*4882a593Smuzhiyun * @c: UBIFS file-system description object
257*4882a593Smuzhiyun * @jhead: journal head
258*4882a593Smuzhiyun * @buf: buffer to write
259*4882a593Smuzhiyun * @len: length to write
260*4882a593Smuzhiyun * @lnum: LEB number written is returned here
261*4882a593Smuzhiyun * @offs: offset written is returned here
262*4882a593Smuzhiyun * @sync: non-zero if the write-buffer has to by synchronized
263*4882a593Smuzhiyun *
264*4882a593Smuzhiyun * This function writes data to the reserved space of journal head @jhead.
265*4882a593Smuzhiyun * Returns zero in case of success and a negative error code in case of
266*4882a593Smuzhiyun * failure.
267*4882a593Smuzhiyun */
write_head(struct ubifs_info * c,int jhead,void * buf,int len,int * lnum,int * offs,int sync)268*4882a593Smuzhiyun static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
269*4882a593Smuzhiyun int *lnum, int *offs, int sync)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun int err;
272*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun ubifs_assert(c, jhead != GCHD);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun *lnum = c->jheads[jhead].wbuf.lnum;
277*4882a593Smuzhiyun *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
278*4882a593Smuzhiyun dbg_jnl("jhead %s, LEB %d:%d, len %d",
279*4882a593Smuzhiyun dbg_jhead(jhead), *lnum, *offs, len);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if (ubifs_authenticated(c)) {
282*4882a593Smuzhiyun err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
283*4882a593Smuzhiyun if (err)
284*4882a593Smuzhiyun return err;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun err = ubifs_wbuf_write_nolock(wbuf, buf, len);
288*4882a593Smuzhiyun if (err)
289*4882a593Smuzhiyun return err;
290*4882a593Smuzhiyun if (sync)
291*4882a593Smuzhiyun err = ubifs_wbuf_sync_nolock(wbuf);
292*4882a593Smuzhiyun return err;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * make_reservation - reserve journal space.
297*4882a593Smuzhiyun * @c: UBIFS file-system description object
298*4882a593Smuzhiyun * @jhead: journal head
299*4882a593Smuzhiyun * @len: how many bytes to reserve
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * This function makes space reservation in journal head @jhead. The function
302*4882a593Smuzhiyun * takes the commit lock and locks the journal head, and the caller has to
303*4882a593Smuzhiyun * unlock the head and finish the reservation with 'finish_reservation()'.
304*4882a593Smuzhiyun * Returns zero in case of success and a negative error code in case of
305*4882a593Smuzhiyun * failure.
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * Note, the journal head may be unlocked as soon as the data is written, while
308*4882a593Smuzhiyun * the commit lock has to be released after the data has been added to the
309*4882a593Smuzhiyun * TNC.
310*4882a593Smuzhiyun */
make_reservation(struct ubifs_info * c,int jhead,int len)311*4882a593Smuzhiyun static int make_reservation(struct ubifs_info *c, int jhead, int len)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun int err, cmt_retries = 0, nospc_retries = 0;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun again:
316*4882a593Smuzhiyun down_read(&c->commit_sem);
317*4882a593Smuzhiyun err = reserve_space(c, jhead, len);
318*4882a593Smuzhiyun if (!err)
319*4882a593Smuzhiyun /* c->commit_sem will get released via finish_reservation(). */
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun up_read(&c->commit_sem);
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (err == -ENOSPC) {
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun * GC could not make any progress. We should try to commit
326*4882a593Smuzhiyun * once because it could make some dirty space and GC would
327*4882a593Smuzhiyun * make progress, so make the error -EAGAIN so that the below
328*4882a593Smuzhiyun * will commit and re-try.
329*4882a593Smuzhiyun */
330*4882a593Smuzhiyun if (nospc_retries++ < 2) {
331*4882a593Smuzhiyun dbg_jnl("no space, retry");
332*4882a593Smuzhiyun err = -EAGAIN;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * This means that the budgeting is incorrect. We always have
337*4882a593Smuzhiyun * to be able to write to the media, because all operations are
338*4882a593Smuzhiyun * budgeted. Deletions are not budgeted, though, but we reserve
339*4882a593Smuzhiyun * an extra LEB for them.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun if (err != -EAGAIN)
344*4882a593Smuzhiyun goto out;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /*
347*4882a593Smuzhiyun * -EAGAIN means that the journal is full or too large, or the above
348*4882a593Smuzhiyun * code wants to do one commit. Do this and re-try.
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun if (cmt_retries > 128) {
351*4882a593Smuzhiyun /*
352*4882a593Smuzhiyun * This should not happen unless the journal size limitations
353*4882a593Smuzhiyun * are too tough.
354*4882a593Smuzhiyun */
355*4882a593Smuzhiyun ubifs_err(c, "stuck in space allocation");
356*4882a593Smuzhiyun err = -ENOSPC;
357*4882a593Smuzhiyun goto out;
358*4882a593Smuzhiyun } else if (cmt_retries > 32)
359*4882a593Smuzhiyun ubifs_warn(c, "too many space allocation re-tries (%d)",
360*4882a593Smuzhiyun cmt_retries);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
363*4882a593Smuzhiyun cmt_retries);
364*4882a593Smuzhiyun cmt_retries += 1;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun err = ubifs_run_commit(c);
367*4882a593Smuzhiyun if (err)
368*4882a593Smuzhiyun return err;
369*4882a593Smuzhiyun goto again;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun out:
372*4882a593Smuzhiyun ubifs_err(c, "cannot reserve %d bytes in jhead %d, error %d",
373*4882a593Smuzhiyun len, jhead, err);
374*4882a593Smuzhiyun if (err == -ENOSPC) {
375*4882a593Smuzhiyun /* This are some budgeting problems, print useful information */
376*4882a593Smuzhiyun down_write(&c->commit_sem);
377*4882a593Smuzhiyun dump_stack();
378*4882a593Smuzhiyun ubifs_dump_budg(c, &c->bi);
379*4882a593Smuzhiyun ubifs_dump_lprops(c);
380*4882a593Smuzhiyun cmt_retries = dbg_check_lprops(c);
381*4882a593Smuzhiyun up_write(&c->commit_sem);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun return err;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /**
387*4882a593Smuzhiyun * release_head - release a journal head.
388*4882a593Smuzhiyun * @c: UBIFS file-system description object
389*4882a593Smuzhiyun * @jhead: journal head
390*4882a593Smuzhiyun *
391*4882a593Smuzhiyun * This function releases journal head @jhead which was locked by
392*4882a593Smuzhiyun * the 'make_reservation()' function. It has to be called after each successful
393*4882a593Smuzhiyun * 'make_reservation()' invocation.
394*4882a593Smuzhiyun */
release_head(struct ubifs_info * c,int jhead)395*4882a593Smuzhiyun static inline void release_head(struct ubifs_info *c, int jhead)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /**
401*4882a593Smuzhiyun * finish_reservation - finish a reservation.
402*4882a593Smuzhiyun * @c: UBIFS file-system description object
403*4882a593Smuzhiyun *
404*4882a593Smuzhiyun * This function finishes journal space reservation. It must be called after
405*4882a593Smuzhiyun * 'make_reservation()'.
406*4882a593Smuzhiyun */
finish_reservation(struct ubifs_info * c)407*4882a593Smuzhiyun static void finish_reservation(struct ubifs_info *c)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun up_read(&c->commit_sem);
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun /**
413*4882a593Smuzhiyun * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
414*4882a593Smuzhiyun * @mode: inode mode
415*4882a593Smuzhiyun */
get_dent_type(int mode)416*4882a593Smuzhiyun static int get_dent_type(int mode)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun switch (mode & S_IFMT) {
419*4882a593Smuzhiyun case S_IFREG:
420*4882a593Smuzhiyun return UBIFS_ITYPE_REG;
421*4882a593Smuzhiyun case S_IFDIR:
422*4882a593Smuzhiyun return UBIFS_ITYPE_DIR;
423*4882a593Smuzhiyun case S_IFLNK:
424*4882a593Smuzhiyun return UBIFS_ITYPE_LNK;
425*4882a593Smuzhiyun case S_IFBLK:
426*4882a593Smuzhiyun return UBIFS_ITYPE_BLK;
427*4882a593Smuzhiyun case S_IFCHR:
428*4882a593Smuzhiyun return UBIFS_ITYPE_CHR;
429*4882a593Smuzhiyun case S_IFIFO:
430*4882a593Smuzhiyun return UBIFS_ITYPE_FIFO;
431*4882a593Smuzhiyun case S_IFSOCK:
432*4882a593Smuzhiyun return UBIFS_ITYPE_SOCK;
433*4882a593Smuzhiyun default:
434*4882a593Smuzhiyun BUG();
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun return 0;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun * pack_inode - pack an inode node.
441*4882a593Smuzhiyun * @c: UBIFS file-system description object
442*4882a593Smuzhiyun * @ino: buffer in which to pack inode node
443*4882a593Smuzhiyun * @inode: inode to pack
444*4882a593Smuzhiyun * @last: indicates the last node of the group
445*4882a593Smuzhiyun */
pack_inode(struct ubifs_info * c,struct ubifs_ino_node * ino,const struct inode * inode,int last)446*4882a593Smuzhiyun static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
447*4882a593Smuzhiyun const struct inode *inode, int last)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun int data_len = 0, last_reference = !inode->i_nlink;
450*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun ino->ch.node_type = UBIFS_INO_NODE;
453*4882a593Smuzhiyun ino_key_init_flash(c, &ino->key, inode->i_ino);
454*4882a593Smuzhiyun ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
455*4882a593Smuzhiyun ino->atime_sec = cpu_to_le64(inode->i_atime.tv_sec);
456*4882a593Smuzhiyun ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
457*4882a593Smuzhiyun ino->ctime_sec = cpu_to_le64(inode->i_ctime.tv_sec);
458*4882a593Smuzhiyun ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
459*4882a593Smuzhiyun ino->mtime_sec = cpu_to_le64(inode->i_mtime.tv_sec);
460*4882a593Smuzhiyun ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
461*4882a593Smuzhiyun ino->uid = cpu_to_le32(i_uid_read(inode));
462*4882a593Smuzhiyun ino->gid = cpu_to_le32(i_gid_read(inode));
463*4882a593Smuzhiyun ino->mode = cpu_to_le32(inode->i_mode);
464*4882a593Smuzhiyun ino->flags = cpu_to_le32(ui->flags);
465*4882a593Smuzhiyun ino->size = cpu_to_le64(ui->ui_size);
466*4882a593Smuzhiyun ino->nlink = cpu_to_le32(inode->i_nlink);
467*4882a593Smuzhiyun ino->compr_type = cpu_to_le16(ui->compr_type);
468*4882a593Smuzhiyun ino->data_len = cpu_to_le32(ui->data_len);
469*4882a593Smuzhiyun ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
470*4882a593Smuzhiyun ino->xattr_size = cpu_to_le32(ui->xattr_size);
471*4882a593Smuzhiyun ino->xattr_names = cpu_to_le32(ui->xattr_names);
472*4882a593Smuzhiyun zero_ino_node_unused(ino);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun * Drop the attached data if this is a deletion inode, the data is not
476*4882a593Smuzhiyun * needed anymore.
477*4882a593Smuzhiyun */
478*4882a593Smuzhiyun if (!last_reference) {
479*4882a593Smuzhiyun memcpy(ino->data, ui->data, ui->data_len);
480*4882a593Smuzhiyun data_len = ui->data_len;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /**
487*4882a593Smuzhiyun * mark_inode_clean - mark UBIFS inode as clean.
488*4882a593Smuzhiyun * @c: UBIFS file-system description object
489*4882a593Smuzhiyun * @ui: UBIFS inode to mark as clean
490*4882a593Smuzhiyun *
491*4882a593Smuzhiyun * This helper function marks UBIFS inode @ui as clean by cleaning the
492*4882a593Smuzhiyun * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
493*4882a593Smuzhiyun * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
494*4882a593Smuzhiyun * just do nothing.
495*4882a593Smuzhiyun */
mark_inode_clean(struct ubifs_info * c,struct ubifs_inode * ui)496*4882a593Smuzhiyun static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun if (ui->dirty)
499*4882a593Smuzhiyun ubifs_release_dirty_inode_budget(c, ui);
500*4882a593Smuzhiyun ui->dirty = 0;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
set_dent_cookie(struct ubifs_info * c,struct ubifs_dent_node * dent)503*4882a593Smuzhiyun static void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun if (c->double_hash)
506*4882a593Smuzhiyun dent->cookie = (__force __le32) prandom_u32();
507*4882a593Smuzhiyun else
508*4882a593Smuzhiyun dent->cookie = 0;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * ubifs_jnl_update - update inode.
513*4882a593Smuzhiyun * @c: UBIFS file-system description object
514*4882a593Smuzhiyun * @dir: parent inode or host inode in case of extended attributes
515*4882a593Smuzhiyun * @nm: directory entry name
516*4882a593Smuzhiyun * @inode: inode to update
517*4882a593Smuzhiyun * @deletion: indicates a directory entry deletion i.e unlink or rmdir
518*4882a593Smuzhiyun * @xent: non-zero if the directory entry is an extended attribute entry
519*4882a593Smuzhiyun *
520*4882a593Smuzhiyun * This function updates an inode by writing a directory entry (or extended
521*4882a593Smuzhiyun * attribute entry), the inode itself, and the parent directory inode (or the
522*4882a593Smuzhiyun * host inode) to the journal.
523*4882a593Smuzhiyun *
524*4882a593Smuzhiyun * The function writes the host inode @dir last, which is important in case of
525*4882a593Smuzhiyun * extended attributes. Indeed, then we guarantee that if the host inode gets
526*4882a593Smuzhiyun * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
527*4882a593Smuzhiyun * the extended attribute inode gets flushed too. And this is exactly what the
528*4882a593Smuzhiyun * user expects - synchronizing the host inode synchronizes its extended
529*4882a593Smuzhiyun * attributes. Similarly, this guarantees that if @dir is synchronized, its
530*4882a593Smuzhiyun * directory entry corresponding to @nm gets synchronized too.
531*4882a593Smuzhiyun *
532*4882a593Smuzhiyun * If the inode (@inode) or the parent directory (@dir) are synchronous, this
533*4882a593Smuzhiyun * function synchronizes the write-buffer.
534*4882a593Smuzhiyun *
535*4882a593Smuzhiyun * This function marks the @dir and @inode inodes as clean and returns zero on
536*4882a593Smuzhiyun * success. In case of failure, a negative error code is returned.
537*4882a593Smuzhiyun */
ubifs_jnl_update(struct ubifs_info * c,const struct inode * dir,const struct fscrypt_name * nm,const struct inode * inode,int deletion,int xent)538*4882a593Smuzhiyun int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
539*4882a593Smuzhiyun const struct fscrypt_name *nm, const struct inode *inode,
540*4882a593Smuzhiyun int deletion, int xent)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
543*4882a593Smuzhiyun int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
544*4882a593Smuzhiyun int last_reference = !!(deletion && inode->i_nlink == 0);
545*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
546*4882a593Smuzhiyun struct ubifs_inode *host_ui = ubifs_inode(dir);
547*4882a593Smuzhiyun struct ubifs_dent_node *dent;
548*4882a593Smuzhiyun struct ubifs_ino_node *ino;
549*4882a593Smuzhiyun union ubifs_key dent_key, ino_key;
550*4882a593Smuzhiyun u8 hash_dent[UBIFS_HASH_ARR_SZ];
551*4882a593Smuzhiyun u8 hash_ino[UBIFS_HASH_ARR_SZ];
552*4882a593Smuzhiyun u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
557*4882a593Smuzhiyun ilen = UBIFS_INO_NODE_SZ;
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /*
560*4882a593Smuzhiyun * If the last reference to the inode is being deleted, then there is
561*4882a593Smuzhiyun * no need to attach and write inode data, it is being deleted anyway.
562*4882a593Smuzhiyun * And if the inode is being deleted, no need to synchronize
563*4882a593Smuzhiyun * write-buffer even if the inode is synchronous.
564*4882a593Smuzhiyun */
565*4882a593Smuzhiyun if (!last_reference) {
566*4882a593Smuzhiyun ilen += ui->data_len;
567*4882a593Smuzhiyun sync |= IS_SYNC(inode);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun aligned_dlen = ALIGN(dlen, 8);
571*4882a593Smuzhiyun aligned_ilen = ALIGN(ilen, 8);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
574*4882a593Smuzhiyun /* Make sure to also account for extended attributes */
575*4882a593Smuzhiyun if (ubifs_authenticated(c))
576*4882a593Smuzhiyun len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
577*4882a593Smuzhiyun else
578*4882a593Smuzhiyun len += host_ui->data_len;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun dent = kzalloc(len, GFP_NOFS);
581*4882a593Smuzhiyun if (!dent)
582*4882a593Smuzhiyun return -ENOMEM;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
585*4882a593Smuzhiyun err = make_reservation(c, BASEHD, len);
586*4882a593Smuzhiyun if (err)
587*4882a593Smuzhiyun goto out_free;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun if (!xent) {
590*4882a593Smuzhiyun dent->ch.node_type = UBIFS_DENT_NODE;
591*4882a593Smuzhiyun if (fname_name(nm) == NULL)
592*4882a593Smuzhiyun dent_key_init_hash(c, &dent_key, dir->i_ino, nm->hash);
593*4882a593Smuzhiyun else
594*4882a593Smuzhiyun dent_key_init(c, &dent_key, dir->i_ino, nm);
595*4882a593Smuzhiyun } else {
596*4882a593Smuzhiyun dent->ch.node_type = UBIFS_XENT_NODE;
597*4882a593Smuzhiyun xent_key_init(c, &dent_key, dir->i_ino, nm);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun key_write(c, &dent_key, dent->key);
601*4882a593Smuzhiyun dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
602*4882a593Smuzhiyun dent->type = get_dent_type(inode->i_mode);
603*4882a593Smuzhiyun dent->nlen = cpu_to_le16(fname_len(nm));
604*4882a593Smuzhiyun memcpy(dent->name, fname_name(nm), fname_len(nm));
605*4882a593Smuzhiyun dent->name[fname_len(nm)] = '\0';
606*4882a593Smuzhiyun set_dent_cookie(c, dent);
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun zero_dent_node_unused(dent);
609*4882a593Smuzhiyun ubifs_prep_grp_node(c, dent, dlen, 0);
610*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dent, hash_dent);
611*4882a593Smuzhiyun if (err)
612*4882a593Smuzhiyun goto out_release;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun ino = (void *)dent + aligned_dlen;
615*4882a593Smuzhiyun pack_inode(c, ino, inode, 0);
616*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash_ino);
617*4882a593Smuzhiyun if (err)
618*4882a593Smuzhiyun goto out_release;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun ino = (void *)ino + aligned_ilen;
621*4882a593Smuzhiyun pack_inode(c, ino, dir, 1);
622*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash_ino_host);
623*4882a593Smuzhiyun if (err)
624*4882a593Smuzhiyun goto out_release;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun if (last_reference) {
627*4882a593Smuzhiyun err = ubifs_add_orphan(c, inode->i_ino);
628*4882a593Smuzhiyun if (err) {
629*4882a593Smuzhiyun release_head(c, BASEHD);
630*4882a593Smuzhiyun goto out_finish;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun ui->del_cmtno = c->cmt_no;
633*4882a593Smuzhiyun orphan_added = 1;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
637*4882a593Smuzhiyun if (err)
638*4882a593Smuzhiyun goto out_release;
639*4882a593Smuzhiyun if (!sync) {
640*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
643*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun release_head(c, BASEHD);
646*4882a593Smuzhiyun kfree(dent);
647*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun if (deletion) {
650*4882a593Smuzhiyun if (fname_name(nm) == NULL)
651*4882a593Smuzhiyun err = ubifs_tnc_remove_dh(c, &dent_key, nm->minor_hash);
652*4882a593Smuzhiyun else
653*4882a593Smuzhiyun err = ubifs_tnc_remove_nm(c, &dent_key, nm);
654*4882a593Smuzhiyun if (err)
655*4882a593Smuzhiyun goto out_ro;
656*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, dlen);
657*4882a593Smuzhiyun } else
658*4882a593Smuzhiyun err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
659*4882a593Smuzhiyun hash_dent, nm);
660*4882a593Smuzhiyun if (err)
661*4882a593Smuzhiyun goto out_ro;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /*
664*4882a593Smuzhiyun * Note, we do not remove the inode from TNC even if the last reference
665*4882a593Smuzhiyun * to it has just been deleted, because the inode may still be opened.
666*4882a593Smuzhiyun * Instead, the inode has been added to orphan lists and the orphan
667*4882a593Smuzhiyun * subsystem will take further care about it.
668*4882a593Smuzhiyun */
669*4882a593Smuzhiyun ino_key_init(c, &ino_key, inode->i_ino);
670*4882a593Smuzhiyun ino_offs = dent_offs + aligned_dlen;
671*4882a593Smuzhiyun err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
672*4882a593Smuzhiyun if (err)
673*4882a593Smuzhiyun goto out_ro;
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun ino_key_init(c, &ino_key, dir->i_ino);
676*4882a593Smuzhiyun ino_offs += aligned_ilen;
677*4882a593Smuzhiyun err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
678*4882a593Smuzhiyun UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
679*4882a593Smuzhiyun if (err)
680*4882a593Smuzhiyun goto out_ro;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun finish_reservation(c);
683*4882a593Smuzhiyun spin_lock(&ui->ui_lock);
684*4882a593Smuzhiyun ui->synced_i_size = ui->ui_size;
685*4882a593Smuzhiyun spin_unlock(&ui->ui_lock);
686*4882a593Smuzhiyun if (xent) {
687*4882a593Smuzhiyun spin_lock(&host_ui->ui_lock);
688*4882a593Smuzhiyun host_ui->synced_i_size = host_ui->ui_size;
689*4882a593Smuzhiyun spin_unlock(&host_ui->ui_lock);
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun mark_inode_clean(c, ui);
692*4882a593Smuzhiyun mark_inode_clean(c, host_ui);
693*4882a593Smuzhiyun return 0;
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun out_finish:
696*4882a593Smuzhiyun finish_reservation(c);
697*4882a593Smuzhiyun out_free:
698*4882a593Smuzhiyun kfree(dent);
699*4882a593Smuzhiyun return err;
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun out_release:
702*4882a593Smuzhiyun release_head(c, BASEHD);
703*4882a593Smuzhiyun kfree(dent);
704*4882a593Smuzhiyun out_ro:
705*4882a593Smuzhiyun ubifs_ro_mode(c, err);
706*4882a593Smuzhiyun if (orphan_added)
707*4882a593Smuzhiyun ubifs_delete_orphan(c, inode->i_ino);
708*4882a593Smuzhiyun finish_reservation(c);
709*4882a593Smuzhiyun return err;
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun /**
713*4882a593Smuzhiyun * ubifs_jnl_write_data - write a data node to the journal.
714*4882a593Smuzhiyun * @c: UBIFS file-system description object
715*4882a593Smuzhiyun * @inode: inode the data node belongs to
716*4882a593Smuzhiyun * @key: node key
717*4882a593Smuzhiyun * @buf: buffer to write
718*4882a593Smuzhiyun * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
719*4882a593Smuzhiyun *
720*4882a593Smuzhiyun * This function writes a data node to the journal. Returns %0 if the data node
721*4882a593Smuzhiyun * was successfully written, and a negative error code in case of failure.
722*4882a593Smuzhiyun */
ubifs_jnl_write_data(struct ubifs_info * c,const struct inode * inode,const union ubifs_key * key,const void * buf,int len)723*4882a593Smuzhiyun int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
724*4882a593Smuzhiyun const union ubifs_key *key, const void *buf, int len)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun struct ubifs_data_node *data;
727*4882a593Smuzhiyun int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
728*4882a593Smuzhiyun int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
729*4882a593Smuzhiyun int write_len;
730*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
731*4882a593Smuzhiyun bool encrypted = IS_ENCRYPTED(inode);
732*4882a593Smuzhiyun u8 hash[UBIFS_HASH_ARR_SZ];
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
735*4882a593Smuzhiyun (unsigned long)key_inum(c, key), key_block(c, key), len);
736*4882a593Smuzhiyun ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun if (encrypted)
739*4882a593Smuzhiyun dlen += UBIFS_CIPHER_BLOCK_SIZE;
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun auth_len = ubifs_auth_node_sz(c);
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
744*4882a593Smuzhiyun if (!data) {
745*4882a593Smuzhiyun /*
746*4882a593Smuzhiyun * Fall-back to the write reserve buffer. Note, we might be
747*4882a593Smuzhiyun * currently on the memory reclaim path, when the kernel is
748*4882a593Smuzhiyun * trying to free some memory by writing out dirty pages. The
749*4882a593Smuzhiyun * write reserve buffer helps us to guarantee that we are
750*4882a593Smuzhiyun * always able to write the data.
751*4882a593Smuzhiyun */
752*4882a593Smuzhiyun allocated = 0;
753*4882a593Smuzhiyun mutex_lock(&c->write_reserve_mutex);
754*4882a593Smuzhiyun data = c->write_reserve_buf;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun data->ch.node_type = UBIFS_DATA_NODE;
758*4882a593Smuzhiyun key_write(c, key, &data->key);
759*4882a593Smuzhiyun data->size = cpu_to_le32(len);
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (!(ui->flags & UBIFS_COMPR_FL))
762*4882a593Smuzhiyun /* Compression is disabled for this inode */
763*4882a593Smuzhiyun compr_type = UBIFS_COMPR_NONE;
764*4882a593Smuzhiyun else
765*4882a593Smuzhiyun compr_type = ui->compr_type;
766*4882a593Smuzhiyun
767*4882a593Smuzhiyun out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
768*4882a593Smuzhiyun ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
769*4882a593Smuzhiyun ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun if (encrypted) {
772*4882a593Smuzhiyun err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
773*4882a593Smuzhiyun if (err)
774*4882a593Smuzhiyun goto out_free;
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun } else {
777*4882a593Smuzhiyun data->compr_size = 0;
778*4882a593Smuzhiyun out_len = compr_len;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun dlen = UBIFS_DATA_NODE_SZ + out_len;
782*4882a593Smuzhiyun if (ubifs_authenticated(c))
783*4882a593Smuzhiyun write_len = ALIGN(dlen, 8) + auth_len;
784*4882a593Smuzhiyun else
785*4882a593Smuzhiyun write_len = dlen;
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun data->compr_type = cpu_to_le16(compr_type);
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
790*4882a593Smuzhiyun err = make_reservation(c, DATAHD, write_len);
791*4882a593Smuzhiyun if (err)
792*4882a593Smuzhiyun goto out_free;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun ubifs_prepare_node(c, data, dlen, 0);
795*4882a593Smuzhiyun err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
796*4882a593Smuzhiyun if (err)
797*4882a593Smuzhiyun goto out_release;
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, data, hash);
800*4882a593Smuzhiyun if (err)
801*4882a593Smuzhiyun goto out_release;
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
804*4882a593Smuzhiyun release_head(c, DATAHD);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
809*4882a593Smuzhiyun if (err)
810*4882a593Smuzhiyun goto out_ro;
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun finish_reservation(c);
813*4882a593Smuzhiyun if (!allocated)
814*4882a593Smuzhiyun mutex_unlock(&c->write_reserve_mutex);
815*4882a593Smuzhiyun else
816*4882a593Smuzhiyun kfree(data);
817*4882a593Smuzhiyun return 0;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun out_release:
820*4882a593Smuzhiyun release_head(c, DATAHD);
821*4882a593Smuzhiyun out_ro:
822*4882a593Smuzhiyun ubifs_ro_mode(c, err);
823*4882a593Smuzhiyun finish_reservation(c);
824*4882a593Smuzhiyun out_free:
825*4882a593Smuzhiyun if (!allocated)
826*4882a593Smuzhiyun mutex_unlock(&c->write_reserve_mutex);
827*4882a593Smuzhiyun else
828*4882a593Smuzhiyun kfree(data);
829*4882a593Smuzhiyun return err;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /**
833*4882a593Smuzhiyun * ubifs_jnl_write_inode - flush inode to the journal.
834*4882a593Smuzhiyun * @c: UBIFS file-system description object
835*4882a593Smuzhiyun * @inode: inode to flush
836*4882a593Smuzhiyun *
837*4882a593Smuzhiyun * This function writes inode @inode to the journal. If the inode is
838*4882a593Smuzhiyun * synchronous, it also synchronizes the write-buffer. Returns zero in case of
839*4882a593Smuzhiyun * success and a negative error code in case of failure.
840*4882a593Smuzhiyun */
ubifs_jnl_write_inode(struct ubifs_info * c,const struct inode * inode)841*4882a593Smuzhiyun int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun int err, lnum, offs;
844*4882a593Smuzhiyun struct ubifs_ino_node *ino, *ino_start;
845*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
846*4882a593Smuzhiyun int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
847*4882a593Smuzhiyun int last_reference = !inode->i_nlink;
848*4882a593Smuzhiyun int kill_xattrs = ui->xattr_cnt && last_reference;
849*4882a593Smuzhiyun u8 hash[UBIFS_HASH_ARR_SZ];
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun /*
854*4882a593Smuzhiyun * If the inode is being deleted, do not write the attached data. No
855*4882a593Smuzhiyun * need to synchronize the write-buffer either.
856*4882a593Smuzhiyun */
857*4882a593Smuzhiyun if (!last_reference) {
858*4882a593Smuzhiyun ilen += ui->data_len;
859*4882a593Smuzhiyun sync = IS_SYNC(inode);
860*4882a593Smuzhiyun } else if (kill_xattrs) {
861*4882a593Smuzhiyun write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun if (ubifs_authenticated(c))
865*4882a593Smuzhiyun write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
866*4882a593Smuzhiyun else
867*4882a593Smuzhiyun write_len += ilen;
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun ino_start = ino = kmalloc(write_len, GFP_NOFS);
870*4882a593Smuzhiyun if (!ino)
871*4882a593Smuzhiyun return -ENOMEM;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
874*4882a593Smuzhiyun err = make_reservation(c, BASEHD, write_len);
875*4882a593Smuzhiyun if (err)
876*4882a593Smuzhiyun goto out_free;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun if (kill_xattrs) {
879*4882a593Smuzhiyun union ubifs_key key;
880*4882a593Smuzhiyun struct fscrypt_name nm = {0};
881*4882a593Smuzhiyun struct inode *xino;
882*4882a593Smuzhiyun struct ubifs_dent_node *xent, *pxent = NULL;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
885*4882a593Smuzhiyun err = -EPERM;
886*4882a593Smuzhiyun ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
887*4882a593Smuzhiyun goto out_release;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun lowest_xent_key(c, &key, inode->i_ino);
891*4882a593Smuzhiyun while (1) {
892*4882a593Smuzhiyun xent = ubifs_tnc_next_ent(c, &key, &nm);
893*4882a593Smuzhiyun if (IS_ERR(xent)) {
894*4882a593Smuzhiyun err = PTR_ERR(xent);
895*4882a593Smuzhiyun if (err == -ENOENT)
896*4882a593Smuzhiyun break;
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun kfree(pxent);
899*4882a593Smuzhiyun goto out_release;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun fname_name(&nm) = xent->name;
903*4882a593Smuzhiyun fname_len(&nm) = le16_to_cpu(xent->nlen);
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
906*4882a593Smuzhiyun if (IS_ERR(xino)) {
907*4882a593Smuzhiyun err = PTR_ERR(xino);
908*4882a593Smuzhiyun ubifs_err(c, "dead directory entry '%s', error %d",
909*4882a593Smuzhiyun xent->name, err);
910*4882a593Smuzhiyun ubifs_ro_mode(c, err);
911*4882a593Smuzhiyun kfree(pxent);
912*4882a593Smuzhiyun kfree(xent);
913*4882a593Smuzhiyun goto out_release;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun ubifs_assert(c, ubifs_inode(xino)->xattr);
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun clear_nlink(xino);
918*4882a593Smuzhiyun pack_inode(c, ino, xino, 0);
919*4882a593Smuzhiyun ino = (void *)ino + UBIFS_INO_NODE_SZ;
920*4882a593Smuzhiyun iput(xino);
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun kfree(pxent);
923*4882a593Smuzhiyun pxent = xent;
924*4882a593Smuzhiyun key_read(c, &xent->key, &key);
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun kfree(pxent);
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun pack_inode(c, ino, inode, 1);
930*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash);
931*4882a593Smuzhiyun if (err)
932*4882a593Smuzhiyun goto out_release;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
935*4882a593Smuzhiyun if (err)
936*4882a593Smuzhiyun goto out_release;
937*4882a593Smuzhiyun if (!sync)
938*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
939*4882a593Smuzhiyun inode->i_ino);
940*4882a593Smuzhiyun release_head(c, BASEHD);
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun if (last_reference) {
943*4882a593Smuzhiyun err = ubifs_tnc_remove_ino(c, inode->i_ino);
944*4882a593Smuzhiyun if (err)
945*4882a593Smuzhiyun goto out_ro;
946*4882a593Smuzhiyun ubifs_delete_orphan(c, inode->i_ino);
947*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, write_len);
948*4882a593Smuzhiyun } else {
949*4882a593Smuzhiyun union ubifs_key key;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun ino_key_init(c, &key, inode->i_ino);
954*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun if (err)
957*4882a593Smuzhiyun goto out_ro;
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun finish_reservation(c);
960*4882a593Smuzhiyun spin_lock(&ui->ui_lock);
961*4882a593Smuzhiyun ui->synced_i_size = ui->ui_size;
962*4882a593Smuzhiyun spin_unlock(&ui->ui_lock);
963*4882a593Smuzhiyun kfree(ino_start);
964*4882a593Smuzhiyun return 0;
965*4882a593Smuzhiyun
966*4882a593Smuzhiyun out_release:
967*4882a593Smuzhiyun release_head(c, BASEHD);
968*4882a593Smuzhiyun out_ro:
969*4882a593Smuzhiyun ubifs_ro_mode(c, err);
970*4882a593Smuzhiyun finish_reservation(c);
971*4882a593Smuzhiyun out_free:
972*4882a593Smuzhiyun kfree(ino_start);
973*4882a593Smuzhiyun return err;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun /**
977*4882a593Smuzhiyun * ubifs_jnl_delete_inode - delete an inode.
978*4882a593Smuzhiyun * @c: UBIFS file-system description object
979*4882a593Smuzhiyun * @inode: inode to delete
980*4882a593Smuzhiyun *
981*4882a593Smuzhiyun * This function deletes inode @inode which includes removing it from orphans,
982*4882a593Smuzhiyun * deleting it from TNC and, in some cases, writing a deletion inode to the
983*4882a593Smuzhiyun * journal.
984*4882a593Smuzhiyun *
985*4882a593Smuzhiyun * When regular file inodes are unlinked or a directory inode is removed, the
986*4882a593Smuzhiyun * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
987*4882a593Smuzhiyun * direntry to the media, and adds the inode to orphans. After this, when the
988*4882a593Smuzhiyun * last reference to this inode has been dropped, this function is called. In
989*4882a593Smuzhiyun * general, it has to write one more deletion inode to the media, because if
990*4882a593Smuzhiyun * a commit happened between 'ubifs_jnl_update()' and
991*4882a593Smuzhiyun * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
992*4882a593Smuzhiyun * anymore, and in fact it might not be on the flash anymore, because it might
993*4882a593Smuzhiyun * have been garbage-collected already. And for optimization reasons UBIFS does
994*4882a593Smuzhiyun * not read the orphan area if it has been unmounted cleanly, so it would have
995*4882a593Smuzhiyun * no indication in the journal that there is a deleted inode which has to be
996*4882a593Smuzhiyun * removed from TNC.
997*4882a593Smuzhiyun *
998*4882a593Smuzhiyun * However, if there was no commit between 'ubifs_jnl_update()' and
999*4882a593Smuzhiyun * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
1000*4882a593Smuzhiyun * inode to the media for the second time. And this is quite a typical case.
1001*4882a593Smuzhiyun *
1002*4882a593Smuzhiyun * This function returns zero in case of success and a negative error code in
1003*4882a593Smuzhiyun * case of failure.
1004*4882a593Smuzhiyun */
ubifs_jnl_delete_inode(struct ubifs_info * c,const struct inode * inode)1005*4882a593Smuzhiyun int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun int err;
1008*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun ubifs_assert(c, inode->i_nlink == 0);
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
1013*4882a593Smuzhiyun /* A commit happened for sure or inode hosts xattrs */
1014*4882a593Smuzhiyun return ubifs_jnl_write_inode(c, inode);
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun down_read(&c->commit_sem);
1017*4882a593Smuzhiyun /*
1018*4882a593Smuzhiyun * Check commit number again, because the first test has been done
1019*4882a593Smuzhiyun * without @c->commit_sem, so a commit might have happened.
1020*4882a593Smuzhiyun */
1021*4882a593Smuzhiyun if (ui->del_cmtno != c->cmt_no) {
1022*4882a593Smuzhiyun up_read(&c->commit_sem);
1023*4882a593Smuzhiyun return ubifs_jnl_write_inode(c, inode);
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun err = ubifs_tnc_remove_ino(c, inode->i_ino);
1027*4882a593Smuzhiyun if (err)
1028*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1029*4882a593Smuzhiyun else
1030*4882a593Smuzhiyun ubifs_delete_orphan(c, inode->i_ino);
1031*4882a593Smuzhiyun up_read(&c->commit_sem);
1032*4882a593Smuzhiyun return err;
1033*4882a593Smuzhiyun }
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun /**
1036*4882a593Smuzhiyun * ubifs_jnl_xrename - cross rename two directory entries.
1037*4882a593Smuzhiyun * @c: UBIFS file-system description object
1038*4882a593Smuzhiyun * @fst_dir: parent inode of 1st directory entry to exchange
1039*4882a593Smuzhiyun * @fst_inode: 1st inode to exchange
1040*4882a593Smuzhiyun * @fst_nm: name of 1st inode to exchange
1041*4882a593Smuzhiyun * @snd_dir: parent inode of 2nd directory entry to exchange
1042*4882a593Smuzhiyun * @snd_inode: 2nd inode to exchange
1043*4882a593Smuzhiyun * @snd_nm: name of 2nd inode to exchange
1044*4882a593Smuzhiyun * @sync: non-zero if the write-buffer has to be synchronized
1045*4882a593Smuzhiyun *
1046*4882a593Smuzhiyun * This function implements the cross rename operation which may involve
1047*4882a593Smuzhiyun * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
1048*4882a593Smuzhiyun * and returns zero on success. In case of failure, a negative error code is
1049*4882a593Smuzhiyun * returned.
1050*4882a593Smuzhiyun */
ubifs_jnl_xrename(struct ubifs_info * c,const struct inode * fst_dir,const struct inode * fst_inode,const struct fscrypt_name * fst_nm,const struct inode * snd_dir,const struct inode * snd_inode,const struct fscrypt_name * snd_nm,int sync)1051*4882a593Smuzhiyun int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
1052*4882a593Smuzhiyun const struct inode *fst_inode,
1053*4882a593Smuzhiyun const struct fscrypt_name *fst_nm,
1054*4882a593Smuzhiyun const struct inode *snd_dir,
1055*4882a593Smuzhiyun const struct inode *snd_inode,
1056*4882a593Smuzhiyun const struct fscrypt_name *snd_nm, int sync)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun union ubifs_key key;
1059*4882a593Smuzhiyun struct ubifs_dent_node *dent1, *dent2;
1060*4882a593Smuzhiyun int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
1061*4882a593Smuzhiyun int aligned_dlen1, aligned_dlen2;
1062*4882a593Smuzhiyun int twoparents = (fst_dir != snd_dir);
1063*4882a593Smuzhiyun void *p;
1064*4882a593Smuzhiyun u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1065*4882a593Smuzhiyun u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1066*4882a593Smuzhiyun u8 hash_p1[UBIFS_HASH_ARR_SZ];
1067*4882a593Smuzhiyun u8 hash_p2[UBIFS_HASH_ARR_SZ];
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
1070*4882a593Smuzhiyun ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
1071*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
1072*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
1075*4882a593Smuzhiyun dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
1076*4882a593Smuzhiyun aligned_dlen1 = ALIGN(dlen1, 8);
1077*4882a593Smuzhiyun aligned_dlen2 = ALIGN(dlen2, 8);
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
1080*4882a593Smuzhiyun if (twoparents)
1081*4882a593Smuzhiyun len += plen;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun len += ubifs_auth_node_sz(c);
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun dent1 = kzalloc(len, GFP_NOFS);
1086*4882a593Smuzhiyun if (!dent1)
1087*4882a593Smuzhiyun return -ENOMEM;
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
1090*4882a593Smuzhiyun err = make_reservation(c, BASEHD, len);
1091*4882a593Smuzhiyun if (err)
1092*4882a593Smuzhiyun goto out_free;
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun /* Make new dent for 1st entry */
1095*4882a593Smuzhiyun dent1->ch.node_type = UBIFS_DENT_NODE;
1096*4882a593Smuzhiyun dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
1097*4882a593Smuzhiyun dent1->inum = cpu_to_le64(fst_inode->i_ino);
1098*4882a593Smuzhiyun dent1->type = get_dent_type(fst_inode->i_mode);
1099*4882a593Smuzhiyun dent1->nlen = cpu_to_le16(fname_len(snd_nm));
1100*4882a593Smuzhiyun memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
1101*4882a593Smuzhiyun dent1->name[fname_len(snd_nm)] = '\0';
1102*4882a593Smuzhiyun set_dent_cookie(c, dent1);
1103*4882a593Smuzhiyun zero_dent_node_unused(dent1);
1104*4882a593Smuzhiyun ubifs_prep_grp_node(c, dent1, dlen1, 0);
1105*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dent1, hash_dent1);
1106*4882a593Smuzhiyun if (err)
1107*4882a593Smuzhiyun goto out_release;
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun /* Make new dent for 2nd entry */
1110*4882a593Smuzhiyun dent2 = (void *)dent1 + aligned_dlen1;
1111*4882a593Smuzhiyun dent2->ch.node_type = UBIFS_DENT_NODE;
1112*4882a593Smuzhiyun dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
1113*4882a593Smuzhiyun dent2->inum = cpu_to_le64(snd_inode->i_ino);
1114*4882a593Smuzhiyun dent2->type = get_dent_type(snd_inode->i_mode);
1115*4882a593Smuzhiyun dent2->nlen = cpu_to_le16(fname_len(fst_nm));
1116*4882a593Smuzhiyun memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
1117*4882a593Smuzhiyun dent2->name[fname_len(fst_nm)] = '\0';
1118*4882a593Smuzhiyun set_dent_cookie(c, dent2);
1119*4882a593Smuzhiyun zero_dent_node_unused(dent2);
1120*4882a593Smuzhiyun ubifs_prep_grp_node(c, dent2, dlen2, 0);
1121*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1122*4882a593Smuzhiyun if (err)
1123*4882a593Smuzhiyun goto out_release;
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun p = (void *)dent2 + aligned_dlen2;
1126*4882a593Smuzhiyun if (!twoparents) {
1127*4882a593Smuzhiyun pack_inode(c, p, fst_dir, 1);
1128*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_p1);
1129*4882a593Smuzhiyun if (err)
1130*4882a593Smuzhiyun goto out_release;
1131*4882a593Smuzhiyun } else {
1132*4882a593Smuzhiyun pack_inode(c, p, fst_dir, 0);
1133*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_p1);
1134*4882a593Smuzhiyun if (err)
1135*4882a593Smuzhiyun goto out_release;
1136*4882a593Smuzhiyun p += ALIGN(plen, 8);
1137*4882a593Smuzhiyun pack_inode(c, p, snd_dir, 1);
1138*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_p2);
1139*4882a593Smuzhiyun if (err)
1140*4882a593Smuzhiyun goto out_release;
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
1144*4882a593Smuzhiyun if (err)
1145*4882a593Smuzhiyun goto out_release;
1146*4882a593Smuzhiyun if (!sync) {
1147*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
1150*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun release_head(c, BASEHD);
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
1157*4882a593Smuzhiyun err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
1158*4882a593Smuzhiyun if (err)
1159*4882a593Smuzhiyun goto out_ro;
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun offs += aligned_dlen1;
1162*4882a593Smuzhiyun dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
1163*4882a593Smuzhiyun err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
1164*4882a593Smuzhiyun if (err)
1165*4882a593Smuzhiyun goto out_ro;
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun offs += aligned_dlen2;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun ino_key_init(c, &key, fst_dir->i_ino);
1170*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
1171*4882a593Smuzhiyun if (err)
1172*4882a593Smuzhiyun goto out_ro;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun if (twoparents) {
1175*4882a593Smuzhiyun offs += ALIGN(plen, 8);
1176*4882a593Smuzhiyun ino_key_init(c, &key, snd_dir->i_ino);
1177*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
1178*4882a593Smuzhiyun if (err)
1179*4882a593Smuzhiyun goto out_ro;
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun finish_reservation(c);
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun mark_inode_clean(c, ubifs_inode(fst_dir));
1185*4882a593Smuzhiyun if (twoparents)
1186*4882a593Smuzhiyun mark_inode_clean(c, ubifs_inode(snd_dir));
1187*4882a593Smuzhiyun kfree(dent1);
1188*4882a593Smuzhiyun return 0;
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun out_release:
1191*4882a593Smuzhiyun release_head(c, BASEHD);
1192*4882a593Smuzhiyun out_ro:
1193*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1194*4882a593Smuzhiyun finish_reservation(c);
1195*4882a593Smuzhiyun out_free:
1196*4882a593Smuzhiyun kfree(dent1);
1197*4882a593Smuzhiyun return err;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun
1200*4882a593Smuzhiyun /**
1201*4882a593Smuzhiyun * ubifs_jnl_rename - rename a directory entry.
1202*4882a593Smuzhiyun * @c: UBIFS file-system description object
1203*4882a593Smuzhiyun * @old_dir: parent inode of directory entry to rename
1204*4882a593Smuzhiyun * @old_dentry: directory entry to rename
1205*4882a593Smuzhiyun * @new_dir: parent inode of directory entry to rename
1206*4882a593Smuzhiyun * @new_dentry: new directory entry (or directory entry to replace)
1207*4882a593Smuzhiyun * @sync: non-zero if the write-buffer has to be synchronized
1208*4882a593Smuzhiyun *
1209*4882a593Smuzhiyun * This function implements the re-name operation which may involve writing up
1210*4882a593Smuzhiyun * to 4 inodes and 2 directory entries. It marks the written inodes as clean
1211*4882a593Smuzhiyun * and returns zero on success. In case of failure, a negative error code is
1212*4882a593Smuzhiyun * returned.
1213*4882a593Smuzhiyun */
ubifs_jnl_rename(struct ubifs_info * c,const struct inode * old_dir,const struct inode * old_inode,const struct fscrypt_name * old_nm,const struct inode * new_dir,const struct inode * new_inode,const struct fscrypt_name * new_nm,const struct inode * whiteout,int sync)1214*4882a593Smuzhiyun int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1215*4882a593Smuzhiyun const struct inode *old_inode,
1216*4882a593Smuzhiyun const struct fscrypt_name *old_nm,
1217*4882a593Smuzhiyun const struct inode *new_dir,
1218*4882a593Smuzhiyun const struct inode *new_inode,
1219*4882a593Smuzhiyun const struct fscrypt_name *new_nm,
1220*4882a593Smuzhiyun const struct inode *whiteout, int sync)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun void *p;
1223*4882a593Smuzhiyun union ubifs_key key;
1224*4882a593Smuzhiyun struct ubifs_dent_node *dent, *dent2;
1225*4882a593Smuzhiyun int err, dlen1, dlen2, ilen, lnum, offs, len, orphan_added = 0;
1226*4882a593Smuzhiyun int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
1227*4882a593Smuzhiyun int last_reference = !!(new_inode && new_inode->i_nlink == 0);
1228*4882a593Smuzhiyun int move = (old_dir != new_dir);
1229*4882a593Smuzhiyun struct ubifs_inode *new_ui;
1230*4882a593Smuzhiyun u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
1231*4882a593Smuzhiyun u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
1232*4882a593Smuzhiyun u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
1233*4882a593Smuzhiyun u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1234*4882a593Smuzhiyun u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
1237*4882a593Smuzhiyun ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
1238*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
1239*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
1242*4882a593Smuzhiyun dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
1243*4882a593Smuzhiyun if (new_inode) {
1244*4882a593Smuzhiyun new_ui = ubifs_inode(new_inode);
1245*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
1246*4882a593Smuzhiyun ilen = UBIFS_INO_NODE_SZ;
1247*4882a593Smuzhiyun if (!last_reference)
1248*4882a593Smuzhiyun ilen += new_ui->data_len;
1249*4882a593Smuzhiyun } else
1250*4882a593Smuzhiyun ilen = 0;
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun aligned_dlen1 = ALIGN(dlen1, 8);
1253*4882a593Smuzhiyun aligned_dlen2 = ALIGN(dlen2, 8);
1254*4882a593Smuzhiyun len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
1255*4882a593Smuzhiyun if (move)
1256*4882a593Smuzhiyun len += plen;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun len += ubifs_auth_node_sz(c);
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun dent = kzalloc(len, GFP_NOFS);
1261*4882a593Smuzhiyun if (!dent)
1262*4882a593Smuzhiyun return -ENOMEM;
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
1265*4882a593Smuzhiyun err = make_reservation(c, BASEHD, len);
1266*4882a593Smuzhiyun if (err)
1267*4882a593Smuzhiyun goto out_free;
1268*4882a593Smuzhiyun
1269*4882a593Smuzhiyun /* Make new dent */
1270*4882a593Smuzhiyun dent->ch.node_type = UBIFS_DENT_NODE;
1271*4882a593Smuzhiyun dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
1272*4882a593Smuzhiyun dent->inum = cpu_to_le64(old_inode->i_ino);
1273*4882a593Smuzhiyun dent->type = get_dent_type(old_inode->i_mode);
1274*4882a593Smuzhiyun dent->nlen = cpu_to_le16(fname_len(new_nm));
1275*4882a593Smuzhiyun memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
1276*4882a593Smuzhiyun dent->name[fname_len(new_nm)] = '\0';
1277*4882a593Smuzhiyun set_dent_cookie(c, dent);
1278*4882a593Smuzhiyun zero_dent_node_unused(dent);
1279*4882a593Smuzhiyun ubifs_prep_grp_node(c, dent, dlen1, 0);
1280*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dent, hash_dent1);
1281*4882a593Smuzhiyun if (err)
1282*4882a593Smuzhiyun goto out_release;
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun dent2 = (void *)dent + aligned_dlen1;
1285*4882a593Smuzhiyun dent2->ch.node_type = UBIFS_DENT_NODE;
1286*4882a593Smuzhiyun dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun if (whiteout) {
1289*4882a593Smuzhiyun dent2->inum = cpu_to_le64(whiteout->i_ino);
1290*4882a593Smuzhiyun dent2->type = get_dent_type(whiteout->i_mode);
1291*4882a593Smuzhiyun } else {
1292*4882a593Smuzhiyun /* Make deletion dent */
1293*4882a593Smuzhiyun dent2->inum = 0;
1294*4882a593Smuzhiyun dent2->type = DT_UNKNOWN;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun dent2->nlen = cpu_to_le16(fname_len(old_nm));
1297*4882a593Smuzhiyun memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
1298*4882a593Smuzhiyun dent2->name[fname_len(old_nm)] = '\0';
1299*4882a593Smuzhiyun set_dent_cookie(c, dent2);
1300*4882a593Smuzhiyun zero_dent_node_unused(dent2);
1301*4882a593Smuzhiyun ubifs_prep_grp_node(c, dent2, dlen2, 0);
1302*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1303*4882a593Smuzhiyun if (err)
1304*4882a593Smuzhiyun goto out_release;
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun p = (void *)dent2 + aligned_dlen2;
1307*4882a593Smuzhiyun if (new_inode) {
1308*4882a593Smuzhiyun pack_inode(c, p, new_inode, 0);
1309*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_new_inode);
1310*4882a593Smuzhiyun if (err)
1311*4882a593Smuzhiyun goto out_release;
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun p += ALIGN(ilen, 8);
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun if (!move) {
1317*4882a593Smuzhiyun pack_inode(c, p, old_dir, 1);
1318*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_old_dir);
1319*4882a593Smuzhiyun if (err)
1320*4882a593Smuzhiyun goto out_release;
1321*4882a593Smuzhiyun } else {
1322*4882a593Smuzhiyun pack_inode(c, p, old_dir, 0);
1323*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_old_dir);
1324*4882a593Smuzhiyun if (err)
1325*4882a593Smuzhiyun goto out_release;
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun p += ALIGN(plen, 8);
1328*4882a593Smuzhiyun pack_inode(c, p, new_dir, 1);
1329*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, p, hash_new_dir);
1330*4882a593Smuzhiyun if (err)
1331*4882a593Smuzhiyun goto out_release;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun if (last_reference) {
1335*4882a593Smuzhiyun err = ubifs_add_orphan(c, new_inode->i_ino);
1336*4882a593Smuzhiyun if (err) {
1337*4882a593Smuzhiyun release_head(c, BASEHD);
1338*4882a593Smuzhiyun goto out_finish;
1339*4882a593Smuzhiyun }
1340*4882a593Smuzhiyun new_ui->del_cmtno = c->cmt_no;
1341*4882a593Smuzhiyun orphan_added = 1;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
1345*4882a593Smuzhiyun if (err)
1346*4882a593Smuzhiyun goto out_release;
1347*4882a593Smuzhiyun if (!sync) {
1348*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
1351*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
1352*4882a593Smuzhiyun if (new_inode)
1353*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1354*4882a593Smuzhiyun new_inode->i_ino);
1355*4882a593Smuzhiyun }
1356*4882a593Smuzhiyun release_head(c, BASEHD);
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
1359*4882a593Smuzhiyun
1360*4882a593Smuzhiyun dent_key_init(c, &key, new_dir->i_ino, new_nm);
1361*4882a593Smuzhiyun err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
1362*4882a593Smuzhiyun if (err)
1363*4882a593Smuzhiyun goto out_ro;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun offs += aligned_dlen1;
1366*4882a593Smuzhiyun if (whiteout) {
1367*4882a593Smuzhiyun dent_key_init(c, &key, old_dir->i_ino, old_nm);
1368*4882a593Smuzhiyun err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
1369*4882a593Smuzhiyun if (err)
1370*4882a593Smuzhiyun goto out_ro;
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun ubifs_delete_orphan(c, whiteout->i_ino);
1373*4882a593Smuzhiyun } else {
1374*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, dlen2);
1375*4882a593Smuzhiyun if (err)
1376*4882a593Smuzhiyun goto out_ro;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun dent_key_init(c, &key, old_dir->i_ino, old_nm);
1379*4882a593Smuzhiyun err = ubifs_tnc_remove_nm(c, &key, old_nm);
1380*4882a593Smuzhiyun if (err)
1381*4882a593Smuzhiyun goto out_ro;
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun offs += aligned_dlen2;
1385*4882a593Smuzhiyun if (new_inode) {
1386*4882a593Smuzhiyun ino_key_init(c, &key, new_inode->i_ino);
1387*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
1388*4882a593Smuzhiyun if (err)
1389*4882a593Smuzhiyun goto out_ro;
1390*4882a593Smuzhiyun offs += ALIGN(ilen, 8);
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun ino_key_init(c, &key, old_dir->i_ino);
1394*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
1395*4882a593Smuzhiyun if (err)
1396*4882a593Smuzhiyun goto out_ro;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun if (move) {
1399*4882a593Smuzhiyun offs += ALIGN(plen, 8);
1400*4882a593Smuzhiyun ino_key_init(c, &key, new_dir->i_ino);
1401*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
1402*4882a593Smuzhiyun if (err)
1403*4882a593Smuzhiyun goto out_ro;
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun finish_reservation(c);
1407*4882a593Smuzhiyun if (new_inode) {
1408*4882a593Smuzhiyun mark_inode_clean(c, new_ui);
1409*4882a593Smuzhiyun spin_lock(&new_ui->ui_lock);
1410*4882a593Smuzhiyun new_ui->synced_i_size = new_ui->ui_size;
1411*4882a593Smuzhiyun spin_unlock(&new_ui->ui_lock);
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun mark_inode_clean(c, ubifs_inode(old_dir));
1414*4882a593Smuzhiyun if (move)
1415*4882a593Smuzhiyun mark_inode_clean(c, ubifs_inode(new_dir));
1416*4882a593Smuzhiyun kfree(dent);
1417*4882a593Smuzhiyun return 0;
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun out_release:
1420*4882a593Smuzhiyun release_head(c, BASEHD);
1421*4882a593Smuzhiyun out_ro:
1422*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1423*4882a593Smuzhiyun if (orphan_added)
1424*4882a593Smuzhiyun ubifs_delete_orphan(c, new_inode->i_ino);
1425*4882a593Smuzhiyun out_finish:
1426*4882a593Smuzhiyun finish_reservation(c);
1427*4882a593Smuzhiyun out_free:
1428*4882a593Smuzhiyun kfree(dent);
1429*4882a593Smuzhiyun return err;
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun /**
1433*4882a593Smuzhiyun * truncate_data_node - re-compress/encrypt a truncated data node.
1434*4882a593Smuzhiyun * @c: UBIFS file-system description object
1435*4882a593Smuzhiyun * @inode: inode which referes to the data node
1436*4882a593Smuzhiyun * @block: data block number
1437*4882a593Smuzhiyun * @dn: data node to re-compress
1438*4882a593Smuzhiyun * @new_len: new length
1439*4882a593Smuzhiyun *
1440*4882a593Smuzhiyun * This function is used when an inode is truncated and the last data node of
1441*4882a593Smuzhiyun * the inode has to be re-compressed/encrypted and re-written.
1442*4882a593Smuzhiyun */
truncate_data_node(const struct ubifs_info * c,const struct inode * inode,unsigned int block,struct ubifs_data_node * dn,int * new_len)1443*4882a593Smuzhiyun static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
1444*4882a593Smuzhiyun unsigned int block, struct ubifs_data_node *dn,
1445*4882a593Smuzhiyun int *new_len)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun void *buf;
1448*4882a593Smuzhiyun int err, dlen, compr_type, out_len, old_dlen;
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun out_len = le32_to_cpu(dn->size);
1451*4882a593Smuzhiyun buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
1452*4882a593Smuzhiyun if (!buf)
1453*4882a593Smuzhiyun return -ENOMEM;
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
1456*4882a593Smuzhiyun compr_type = le16_to_cpu(dn->compr_type);
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun if (IS_ENCRYPTED(inode)) {
1459*4882a593Smuzhiyun err = ubifs_decrypt(inode, dn, &dlen, block);
1460*4882a593Smuzhiyun if (err)
1461*4882a593Smuzhiyun goto out;
1462*4882a593Smuzhiyun }
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun if (compr_type == UBIFS_COMPR_NONE) {
1465*4882a593Smuzhiyun out_len = *new_len;
1466*4882a593Smuzhiyun } else {
1467*4882a593Smuzhiyun err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
1468*4882a593Smuzhiyun if (err)
1469*4882a593Smuzhiyun goto out;
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun if (IS_ENCRYPTED(inode)) {
1475*4882a593Smuzhiyun err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
1476*4882a593Smuzhiyun if (err)
1477*4882a593Smuzhiyun goto out;
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun out_len = old_dlen;
1480*4882a593Smuzhiyun } else {
1481*4882a593Smuzhiyun dn->compr_size = 0;
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
1485*4882a593Smuzhiyun dn->compr_type = cpu_to_le16(compr_type);
1486*4882a593Smuzhiyun dn->size = cpu_to_le32(*new_len);
1487*4882a593Smuzhiyun *new_len = UBIFS_DATA_NODE_SZ + out_len;
1488*4882a593Smuzhiyun err = 0;
1489*4882a593Smuzhiyun out:
1490*4882a593Smuzhiyun kfree(buf);
1491*4882a593Smuzhiyun return err;
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun /**
1495*4882a593Smuzhiyun * ubifs_jnl_truncate - update the journal for a truncation.
1496*4882a593Smuzhiyun * @c: UBIFS file-system description object
1497*4882a593Smuzhiyun * @inode: inode to truncate
1498*4882a593Smuzhiyun * @old_size: old size
1499*4882a593Smuzhiyun * @new_size: new size
1500*4882a593Smuzhiyun *
1501*4882a593Smuzhiyun * When the size of a file decreases due to truncation, a truncation node is
1502*4882a593Smuzhiyun * written, the journal tree is updated, and the last data block is re-written
1503*4882a593Smuzhiyun * if it has been affected. The inode is also updated in order to synchronize
1504*4882a593Smuzhiyun * the new inode size.
1505*4882a593Smuzhiyun *
1506*4882a593Smuzhiyun * This function marks the inode as clean and returns zero on success. In case
1507*4882a593Smuzhiyun * of failure, a negative error code is returned.
1508*4882a593Smuzhiyun */
ubifs_jnl_truncate(struct ubifs_info * c,const struct inode * inode,loff_t old_size,loff_t new_size)1509*4882a593Smuzhiyun int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1510*4882a593Smuzhiyun loff_t old_size, loff_t new_size)
1511*4882a593Smuzhiyun {
1512*4882a593Smuzhiyun union ubifs_key key, to_key;
1513*4882a593Smuzhiyun struct ubifs_ino_node *ino;
1514*4882a593Smuzhiyun struct ubifs_trun_node *trun;
1515*4882a593Smuzhiyun struct ubifs_data_node *dn;
1516*4882a593Smuzhiyun int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
1517*4882a593Smuzhiyun struct ubifs_inode *ui = ubifs_inode(inode);
1518*4882a593Smuzhiyun ino_t inum = inode->i_ino;
1519*4882a593Smuzhiyun unsigned int blk;
1520*4882a593Smuzhiyun u8 hash_ino[UBIFS_HASH_ARR_SZ];
1521*4882a593Smuzhiyun u8 hash_dn[UBIFS_HASH_ARR_SZ];
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun dbg_jnl("ino %lu, size %lld -> %lld",
1524*4882a593Smuzhiyun (unsigned long)inum, old_size, new_size);
1525*4882a593Smuzhiyun ubifs_assert(c, !ui->data_len);
1526*4882a593Smuzhiyun ubifs_assert(c, S_ISREG(inode->i_mode));
1527*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
1528*4882a593Smuzhiyun
1529*4882a593Smuzhiyun sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
1530*4882a593Smuzhiyun UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun sz += ubifs_auth_node_sz(c);
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun ino = kmalloc(sz, GFP_NOFS);
1535*4882a593Smuzhiyun if (!ino)
1536*4882a593Smuzhiyun return -ENOMEM;
1537*4882a593Smuzhiyun
1538*4882a593Smuzhiyun trun = (void *)ino + UBIFS_INO_NODE_SZ;
1539*4882a593Smuzhiyun trun->ch.node_type = UBIFS_TRUN_NODE;
1540*4882a593Smuzhiyun trun->inum = cpu_to_le32(inum);
1541*4882a593Smuzhiyun trun->old_size = cpu_to_le64(old_size);
1542*4882a593Smuzhiyun trun->new_size = cpu_to_le64(new_size);
1543*4882a593Smuzhiyun zero_trun_node_unused(trun);
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
1546*4882a593Smuzhiyun if (dlen) {
1547*4882a593Smuzhiyun /* Get last data block so it can be truncated */
1548*4882a593Smuzhiyun dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
1549*4882a593Smuzhiyun blk = new_size >> UBIFS_BLOCK_SHIFT;
1550*4882a593Smuzhiyun data_key_init(c, &key, inum, blk);
1551*4882a593Smuzhiyun dbg_jnlk(&key, "last block key ");
1552*4882a593Smuzhiyun err = ubifs_tnc_lookup(c, &key, dn);
1553*4882a593Smuzhiyun if (err == -ENOENT)
1554*4882a593Smuzhiyun dlen = 0; /* Not found (so it is a hole) */
1555*4882a593Smuzhiyun else if (err)
1556*4882a593Smuzhiyun goto out_free;
1557*4882a593Smuzhiyun else {
1558*4882a593Smuzhiyun int dn_len = le32_to_cpu(dn->size);
1559*4882a593Smuzhiyun
1560*4882a593Smuzhiyun if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
1561*4882a593Smuzhiyun ubifs_err(c, "bad data node (block %u, inode %lu)",
1562*4882a593Smuzhiyun blk, inode->i_ino);
1563*4882a593Smuzhiyun ubifs_dump_node(c, dn);
1564*4882a593Smuzhiyun goto out_free;
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun if (dn_len <= dlen)
1568*4882a593Smuzhiyun dlen = 0; /* Nothing to do */
1569*4882a593Smuzhiyun else {
1570*4882a593Smuzhiyun err = truncate_data_node(c, inode, blk, dn, &dlen);
1571*4882a593Smuzhiyun if (err)
1572*4882a593Smuzhiyun goto out_free;
1573*4882a593Smuzhiyun }
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun }
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun /* Must make reservation before allocating sequence numbers */
1578*4882a593Smuzhiyun len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun if (ubifs_authenticated(c))
1581*4882a593Smuzhiyun len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
1582*4882a593Smuzhiyun else
1583*4882a593Smuzhiyun len += dlen;
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun err = make_reservation(c, BASEHD, len);
1586*4882a593Smuzhiyun if (err)
1587*4882a593Smuzhiyun goto out_free;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun pack_inode(c, ino, inode, 0);
1590*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash_ino);
1591*4882a593Smuzhiyun if (err)
1592*4882a593Smuzhiyun goto out_release;
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1595*4882a593Smuzhiyun if (dlen) {
1596*4882a593Smuzhiyun ubifs_prep_grp_node(c, dn, dlen, 1);
1597*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, dn, hash_dn);
1598*4882a593Smuzhiyun if (err)
1599*4882a593Smuzhiyun goto out_release;
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1603*4882a593Smuzhiyun if (err)
1604*4882a593Smuzhiyun goto out_release;
1605*4882a593Smuzhiyun if (!sync)
1606*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
1607*4882a593Smuzhiyun release_head(c, BASEHD);
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun if (dlen) {
1612*4882a593Smuzhiyun sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1613*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
1614*4882a593Smuzhiyun if (err)
1615*4882a593Smuzhiyun goto out_ro;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun ino_key_init(c, &key, inum);
1619*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
1620*4882a593Smuzhiyun if (err)
1621*4882a593Smuzhiyun goto out_ro;
1622*4882a593Smuzhiyun
1623*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
1624*4882a593Smuzhiyun if (err)
1625*4882a593Smuzhiyun goto out_ro;
1626*4882a593Smuzhiyun
1627*4882a593Smuzhiyun bit = new_size & (UBIFS_BLOCK_SIZE - 1);
1628*4882a593Smuzhiyun blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
1629*4882a593Smuzhiyun data_key_init(c, &key, inum, blk);
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun bit = old_size & (UBIFS_BLOCK_SIZE - 1);
1632*4882a593Smuzhiyun blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
1633*4882a593Smuzhiyun data_key_init(c, &to_key, inum, blk);
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun err = ubifs_tnc_remove_range(c, &key, &to_key);
1636*4882a593Smuzhiyun if (err)
1637*4882a593Smuzhiyun goto out_ro;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun finish_reservation(c);
1640*4882a593Smuzhiyun spin_lock(&ui->ui_lock);
1641*4882a593Smuzhiyun ui->synced_i_size = ui->ui_size;
1642*4882a593Smuzhiyun spin_unlock(&ui->ui_lock);
1643*4882a593Smuzhiyun mark_inode_clean(c, ui);
1644*4882a593Smuzhiyun kfree(ino);
1645*4882a593Smuzhiyun return 0;
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun out_release:
1648*4882a593Smuzhiyun release_head(c, BASEHD);
1649*4882a593Smuzhiyun out_ro:
1650*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1651*4882a593Smuzhiyun finish_reservation(c);
1652*4882a593Smuzhiyun out_free:
1653*4882a593Smuzhiyun kfree(ino);
1654*4882a593Smuzhiyun return err;
1655*4882a593Smuzhiyun }
1656*4882a593Smuzhiyun
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun /**
1659*4882a593Smuzhiyun * ubifs_jnl_delete_xattr - delete an extended attribute.
1660*4882a593Smuzhiyun * @c: UBIFS file-system description object
1661*4882a593Smuzhiyun * @host: host inode
1662*4882a593Smuzhiyun * @inode: extended attribute inode
1663*4882a593Smuzhiyun * @nm: extended attribute entry name
1664*4882a593Smuzhiyun *
1665*4882a593Smuzhiyun * This function delete an extended attribute which is very similar to
1666*4882a593Smuzhiyun * un-linking regular files - it writes a deletion xentry, a deletion inode and
1667*4882a593Smuzhiyun * updates the target inode. Returns zero in case of success and a negative
1668*4882a593Smuzhiyun * error code in case of failure.
1669*4882a593Smuzhiyun */
ubifs_jnl_delete_xattr(struct ubifs_info * c,const struct inode * host,const struct inode * inode,const struct fscrypt_name * nm)1670*4882a593Smuzhiyun int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1671*4882a593Smuzhiyun const struct inode *inode,
1672*4882a593Smuzhiyun const struct fscrypt_name *nm)
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
1675*4882a593Smuzhiyun struct ubifs_dent_node *xent;
1676*4882a593Smuzhiyun struct ubifs_ino_node *ino;
1677*4882a593Smuzhiyun union ubifs_key xent_key, key1, key2;
1678*4882a593Smuzhiyun int sync = IS_DIRSYNC(host);
1679*4882a593Smuzhiyun struct ubifs_inode *host_ui = ubifs_inode(host);
1680*4882a593Smuzhiyun u8 hash[UBIFS_HASH_ARR_SZ];
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun ubifs_assert(c, inode->i_nlink == 0);
1683*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun /*
1686*4882a593Smuzhiyun * Since we are deleting the inode, we do not bother to attach any data
1687*4882a593Smuzhiyun * to it and assume its length is %UBIFS_INO_NODE_SZ.
1688*4882a593Smuzhiyun */
1689*4882a593Smuzhiyun xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
1690*4882a593Smuzhiyun aligned_xlen = ALIGN(xlen, 8);
1691*4882a593Smuzhiyun hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
1692*4882a593Smuzhiyun len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun write_len = len + ubifs_auth_node_sz(c);
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun xent = kzalloc(write_len, GFP_NOFS);
1697*4882a593Smuzhiyun if (!xent)
1698*4882a593Smuzhiyun return -ENOMEM;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
1701*4882a593Smuzhiyun err = make_reservation(c, BASEHD, write_len);
1702*4882a593Smuzhiyun if (err) {
1703*4882a593Smuzhiyun kfree(xent);
1704*4882a593Smuzhiyun return err;
1705*4882a593Smuzhiyun }
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun xent->ch.node_type = UBIFS_XENT_NODE;
1708*4882a593Smuzhiyun xent_key_init(c, &xent_key, host->i_ino, nm);
1709*4882a593Smuzhiyun key_write(c, &xent_key, xent->key);
1710*4882a593Smuzhiyun xent->inum = 0;
1711*4882a593Smuzhiyun xent->type = get_dent_type(inode->i_mode);
1712*4882a593Smuzhiyun xent->nlen = cpu_to_le16(fname_len(nm));
1713*4882a593Smuzhiyun memcpy(xent->name, fname_name(nm), fname_len(nm));
1714*4882a593Smuzhiyun xent->name[fname_len(nm)] = '\0';
1715*4882a593Smuzhiyun zero_dent_node_unused(xent);
1716*4882a593Smuzhiyun ubifs_prep_grp_node(c, xent, xlen, 0);
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun ino = (void *)xent + aligned_xlen;
1719*4882a593Smuzhiyun pack_inode(c, ino, inode, 0);
1720*4882a593Smuzhiyun ino = (void *)ino + UBIFS_INO_NODE_SZ;
1721*4882a593Smuzhiyun pack_inode(c, ino, host, 1);
1722*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash);
1723*4882a593Smuzhiyun if (err)
1724*4882a593Smuzhiyun goto out_release;
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
1727*4882a593Smuzhiyun if (!sync && !err)
1728*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
1729*4882a593Smuzhiyun release_head(c, BASEHD);
1730*4882a593Smuzhiyun
1731*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
1732*4882a593Smuzhiyun kfree(xent);
1733*4882a593Smuzhiyun if (err)
1734*4882a593Smuzhiyun goto out_ro;
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun /* Remove the extended attribute entry from TNC */
1737*4882a593Smuzhiyun err = ubifs_tnc_remove_nm(c, &xent_key, nm);
1738*4882a593Smuzhiyun if (err)
1739*4882a593Smuzhiyun goto out_ro;
1740*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, xlen);
1741*4882a593Smuzhiyun if (err)
1742*4882a593Smuzhiyun goto out_ro;
1743*4882a593Smuzhiyun
1744*4882a593Smuzhiyun /*
1745*4882a593Smuzhiyun * Remove all nodes belonging to the extended attribute inode from TNC.
1746*4882a593Smuzhiyun * Well, there actually must be only one node - the inode itself.
1747*4882a593Smuzhiyun */
1748*4882a593Smuzhiyun lowest_ino_key(c, &key1, inode->i_ino);
1749*4882a593Smuzhiyun highest_ino_key(c, &key2, inode->i_ino);
1750*4882a593Smuzhiyun err = ubifs_tnc_remove_range(c, &key1, &key2);
1751*4882a593Smuzhiyun if (err)
1752*4882a593Smuzhiyun goto out_ro;
1753*4882a593Smuzhiyun err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
1754*4882a593Smuzhiyun if (err)
1755*4882a593Smuzhiyun goto out_ro;
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun /* And update TNC with the new host inode position */
1758*4882a593Smuzhiyun ino_key_init(c, &key1, host->i_ino);
1759*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
1760*4882a593Smuzhiyun if (err)
1761*4882a593Smuzhiyun goto out_ro;
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun finish_reservation(c);
1764*4882a593Smuzhiyun spin_lock(&host_ui->ui_lock);
1765*4882a593Smuzhiyun host_ui->synced_i_size = host_ui->ui_size;
1766*4882a593Smuzhiyun spin_unlock(&host_ui->ui_lock);
1767*4882a593Smuzhiyun mark_inode_clean(c, host_ui);
1768*4882a593Smuzhiyun return 0;
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun out_release:
1771*4882a593Smuzhiyun kfree(xent);
1772*4882a593Smuzhiyun release_head(c, BASEHD);
1773*4882a593Smuzhiyun out_ro:
1774*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1775*4882a593Smuzhiyun finish_reservation(c);
1776*4882a593Smuzhiyun return err;
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun /**
1780*4882a593Smuzhiyun * ubifs_jnl_change_xattr - change an extended attribute.
1781*4882a593Smuzhiyun * @c: UBIFS file-system description object
1782*4882a593Smuzhiyun * @inode: extended attribute inode
1783*4882a593Smuzhiyun * @host: host inode
1784*4882a593Smuzhiyun *
1785*4882a593Smuzhiyun * This function writes the updated version of an extended attribute inode and
1786*4882a593Smuzhiyun * the host inode to the journal (to the base head). The host inode is written
1787*4882a593Smuzhiyun * after the extended attribute inode in order to guarantee that the extended
1788*4882a593Smuzhiyun * attribute will be flushed when the inode is synchronized by 'fsync()' and
1789*4882a593Smuzhiyun * consequently, the write-buffer is synchronized. This function returns zero
1790*4882a593Smuzhiyun * in case of success and a negative error code in case of failure.
1791*4882a593Smuzhiyun */
ubifs_jnl_change_xattr(struct ubifs_info * c,const struct inode * inode,const struct inode * host)1792*4882a593Smuzhiyun int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1793*4882a593Smuzhiyun const struct inode *host)
1794*4882a593Smuzhiyun {
1795*4882a593Smuzhiyun int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
1796*4882a593Smuzhiyun struct ubifs_inode *host_ui = ubifs_inode(host);
1797*4882a593Smuzhiyun struct ubifs_ino_node *ino;
1798*4882a593Smuzhiyun union ubifs_key key;
1799*4882a593Smuzhiyun int sync = IS_DIRSYNC(host);
1800*4882a593Smuzhiyun u8 hash_host[UBIFS_HASH_ARR_SZ];
1801*4882a593Smuzhiyun u8 hash[UBIFS_HASH_ARR_SZ];
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
1804*4882a593Smuzhiyun ubifs_assert(c, inode->i_nlink > 0);
1805*4882a593Smuzhiyun ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
1808*4882a593Smuzhiyun len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
1809*4882a593Smuzhiyun aligned_len1 = ALIGN(len1, 8);
1810*4882a593Smuzhiyun aligned_len = aligned_len1 + ALIGN(len2, 8);
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun aligned_len += ubifs_auth_node_sz(c);
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun ino = kzalloc(aligned_len, GFP_NOFS);
1815*4882a593Smuzhiyun if (!ino)
1816*4882a593Smuzhiyun return -ENOMEM;
1817*4882a593Smuzhiyun
1818*4882a593Smuzhiyun /* Make reservation before allocating sequence numbers */
1819*4882a593Smuzhiyun err = make_reservation(c, BASEHD, aligned_len);
1820*4882a593Smuzhiyun if (err)
1821*4882a593Smuzhiyun goto out_free;
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun pack_inode(c, ino, host, 0);
1824*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, ino, hash_host);
1825*4882a593Smuzhiyun if (err)
1826*4882a593Smuzhiyun goto out_release;
1827*4882a593Smuzhiyun pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1828*4882a593Smuzhiyun err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
1829*4882a593Smuzhiyun if (err)
1830*4882a593Smuzhiyun goto out_release;
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
1833*4882a593Smuzhiyun if (!sync && !err) {
1834*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
1837*4882a593Smuzhiyun ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun release_head(c, BASEHD);
1840*4882a593Smuzhiyun if (err)
1841*4882a593Smuzhiyun goto out_ro;
1842*4882a593Smuzhiyun
1843*4882a593Smuzhiyun ubifs_add_auth_dirt(c, lnum);
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun ino_key_init(c, &key, host->i_ino);
1846*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
1847*4882a593Smuzhiyun if (err)
1848*4882a593Smuzhiyun goto out_ro;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun ino_key_init(c, &key, inode->i_ino);
1851*4882a593Smuzhiyun err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
1852*4882a593Smuzhiyun if (err)
1853*4882a593Smuzhiyun goto out_ro;
1854*4882a593Smuzhiyun
1855*4882a593Smuzhiyun finish_reservation(c);
1856*4882a593Smuzhiyun spin_lock(&host_ui->ui_lock);
1857*4882a593Smuzhiyun host_ui->synced_i_size = host_ui->ui_size;
1858*4882a593Smuzhiyun spin_unlock(&host_ui->ui_lock);
1859*4882a593Smuzhiyun mark_inode_clean(c, host_ui);
1860*4882a593Smuzhiyun kfree(ino);
1861*4882a593Smuzhiyun return 0;
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun out_release:
1864*4882a593Smuzhiyun release_head(c, BASEHD);
1865*4882a593Smuzhiyun out_ro:
1866*4882a593Smuzhiyun ubifs_ro_mode(c, err);
1867*4882a593Smuzhiyun finish_reservation(c);
1868*4882a593Smuzhiyun out_free:
1869*4882a593Smuzhiyun kfree(ino);
1870*4882a593Smuzhiyun return err;
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun
1873