Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Authors: Artem Bityutskiy (Битюцкий Артём)
0008  *          Adrian Hunter
0009  */
0010 
0011 /*
0012  * This file implements UBIFS journal.
0013  *
0014  * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
0015  * length and position, while a bud logical eraseblock is any LEB in the main
0016  * area. Buds contain file system data - data nodes, inode nodes, etc. The log
0017  * contains only references to buds and some other stuff like commit
0018  * start node. The idea is that when we commit the journal, we do
0019  * not copy the data, the buds just become indexed. Since after the commit the
0020  * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
0021  * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
0022  * become leafs in the future.
0023  *
0024  * The journal is multi-headed because we want to write data to the journal as
0025  * optimally as possible. It is nice to have nodes belonging to the same inode
0026  * in one LEB, so we may write data owned by different inodes to different
0027  * journal heads, although at present only one data head is used.
0028  *
0029  * For recovery reasons, the base head contains all inode nodes, all directory
0030  * entry nodes and all truncate nodes. This means that the other heads contain
0031  * only data nodes.
0032  *
0033  * Bud LEBs may be half-indexed. For example, if the bud was not full at the
0034  * time of commit, the bud is retained to continue to be used in the journal,
0035  * even though the "front" of the LEB is now indexed. In that case, the log
0036  * reference contains the offset where the bud starts for the purposes of the
0037  * journal.
0038  *
0039  * The journal size has to be limited, because the larger is the journal, the
0040  * longer it takes to mount UBIFS (scanning the journal) and the more memory it
0041  * takes (indexing in the TNC).
0042  *
0043  * All the journal write operations like 'ubifs_jnl_update()' here, which write
0044  * multiple UBIFS nodes to the journal at one go, are atomic with respect to
0045  * unclean reboots. Should the unclean reboot happen, the recovery code drops
0046  * all the nodes.
0047  */
0048 
0049 #include "ubifs.h"
0050 
0051 /**
0052  * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
0053  * @ino: the inode to zero out
0054  */
0055 static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
0056 {
0057     memset(ino->padding1, 0, 4);
0058     memset(ino->padding2, 0, 26);
0059 }
0060 
0061 /**
0062  * zero_dent_node_unused - zero out unused fields of an on-flash directory
0063  *                         entry node.
0064  * @dent: the directory entry to zero out
0065  */
0066 static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
0067 {
0068     dent->padding1 = 0;
0069 }
0070 
0071 /**
0072  * zero_trun_node_unused - zero out unused fields of an on-flash truncation
0073  *                         node.
0074  * @trun: the truncation node to zero out
0075  */
0076 static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
0077 {
0078     memset(trun->padding, 0, 12);
0079 }
0080 
0081 static void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
0082 {
0083     if (ubifs_authenticated(c))
0084         ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
0085 }
0086 
0087 /**
0088  * reserve_space - reserve space in the journal.
0089  * @c: UBIFS file-system description object
0090  * @jhead: journal head number
0091  * @len: node length
0092  *
0093  * This function reserves space in journal head @head. If the reservation
0094  * succeeded, the journal head stays locked and later has to be unlocked using
0095  * 'release_head()'. Returns zero in case of success, %-EAGAIN if commit has to
0096  * be done, and other negative error codes in case of other failures.
0097  */
0098 static int reserve_space(struct ubifs_info *c, int jhead, int len)
0099 {
0100     int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
0101     struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
0102 
0103     /*
0104      * Typically, the base head has smaller nodes written to it, so it is
0105      * better to try to allocate space at the ends of eraseblocks. This is
0106      * what the squeeze parameter does.
0107      */
0108     ubifs_assert(c, !c->ro_media && !c->ro_mount);
0109     squeeze = (jhead == BASEHD);
0110 again:
0111     mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
0112 
0113     if (c->ro_error) {
0114         err = -EROFS;
0115         goto out_unlock;
0116     }
0117 
0118     avail = c->leb_size - wbuf->offs - wbuf->used;
0119     if (wbuf->lnum != -1 && avail >= len)
0120         return 0;
0121 
0122     /*
0123      * Write buffer wasn't seek'ed or there is no enough space - look for an
0124      * LEB with some empty space.
0125      */
0126     lnum = ubifs_find_free_space(c, len, &offs, squeeze);
0127     if (lnum >= 0)
0128         goto out;
0129 
0130     err = lnum;
0131     if (err != -ENOSPC)
0132         goto out_unlock;
0133 
0134     /*
0135      * No free space, we have to run garbage collector to make
0136      * some. But the write-buffer mutex has to be unlocked because
0137      * GC also takes it.
0138      */
0139     dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
0140     mutex_unlock(&wbuf->io_mutex);
0141 
0142     lnum = ubifs_garbage_collect(c, 0);
0143     if (lnum < 0) {
0144         err = lnum;
0145         if (err != -ENOSPC)
0146             return err;
0147 
0148         /*
0149          * GC could not make a free LEB. But someone else may
0150          * have allocated new bud for this journal head,
0151          * because we dropped @wbuf->io_mutex, so try once
0152          * again.
0153          */
0154         dbg_jnl("GC couldn't make a free LEB for jhead %s",
0155             dbg_jhead(jhead));
0156         if (retries++ < 2) {
0157             dbg_jnl("retry (%d)", retries);
0158             goto again;
0159         }
0160 
0161         dbg_jnl("return -ENOSPC");
0162         return err;
0163     }
0164 
0165     mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
0166     dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
0167     avail = c->leb_size - wbuf->offs - wbuf->used;
0168 
0169     if (wbuf->lnum != -1 && avail >= len) {
0170         /*
0171          * Someone else has switched the journal head and we have
0172          * enough space now. This happens when more than one process is
0173          * trying to write to the same journal head at the same time.
0174          */
0175         dbg_jnl("return LEB %d back, already have LEB %d:%d",
0176             lnum, wbuf->lnum, wbuf->offs + wbuf->used);
0177         err = ubifs_return_leb(c, lnum);
0178         if (err)
0179             goto out_unlock;
0180         return 0;
0181     }
0182 
0183     offs = 0;
0184 
0185 out:
0186     /*
0187      * Make sure we synchronize the write-buffer before we add the new bud
0188      * to the log. Otherwise we may have a power cut after the log
0189      * reference node for the last bud (@lnum) is written but before the
0190      * write-buffer data are written to the next-to-last bud
0191      * (@wbuf->lnum). And the effect would be that the recovery would see
0192      * that there is corruption in the next-to-last bud.
0193      */
0194     err = ubifs_wbuf_sync_nolock(wbuf);
0195     if (err)
0196         goto out_return;
0197     err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
0198     if (err)
0199         goto out_return;
0200     err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
0201     if (err)
0202         goto out_unlock;
0203 
0204     return 0;
0205 
0206 out_unlock:
0207     mutex_unlock(&wbuf->io_mutex);
0208     return err;
0209 
0210 out_return:
0211     /* An error occurred and the LEB has to be returned to lprops */
0212     ubifs_assert(c, err < 0);
0213     err1 = ubifs_return_leb(c, lnum);
0214     if (err1 && err == -EAGAIN)
0215         /*
0216          * Return original error code only if it is not %-EAGAIN,
0217          * which is not really an error. Otherwise, return the error
0218          * code of 'ubifs_return_leb()'.
0219          */
0220         err = err1;
0221     mutex_unlock(&wbuf->io_mutex);
0222     return err;
0223 }
0224 
0225 static int ubifs_hash_nodes(struct ubifs_info *c, void *node,
0226                  int len, struct shash_desc *hash)
0227 {
0228     int auth_node_size = ubifs_auth_node_sz(c);
0229     int err;
0230 
0231     while (1) {
0232         const struct ubifs_ch *ch = node;
0233         int nodelen = le32_to_cpu(ch->len);
0234 
0235         ubifs_assert(c, len >= auth_node_size);
0236 
0237         if (len == auth_node_size)
0238             break;
0239 
0240         ubifs_assert(c, len > nodelen);
0241         ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
0242 
0243         err = ubifs_shash_update(c, hash, (void *)node, nodelen);
0244         if (err)
0245             return err;
0246 
0247         node += ALIGN(nodelen, 8);
0248         len -= ALIGN(nodelen, 8);
0249     }
0250 
0251     return ubifs_prepare_auth_node(c, node, hash);
0252 }
0253 
0254 /**
0255  * write_head - write data to a journal head.
0256  * @c: UBIFS file-system description object
0257  * @jhead: journal head
0258  * @buf: buffer to write
0259  * @len: length to write
0260  * @lnum: LEB number written is returned here
0261  * @offs: offset written is returned here
0262  * @sync: non-zero if the write-buffer has to by synchronized
0263  *
0264  * This function writes data to the reserved space of journal head @jhead.
0265  * Returns zero in case of success and a negative error code in case of
0266  * failure.
0267  */
0268 static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
0269               int *lnum, int *offs, int sync)
0270 {
0271     int err;
0272     struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
0273 
0274     ubifs_assert(c, jhead != GCHD);
0275 
0276     *lnum = c->jheads[jhead].wbuf.lnum;
0277     *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
0278     dbg_jnl("jhead %s, LEB %d:%d, len %d",
0279         dbg_jhead(jhead), *lnum, *offs, len);
0280 
0281     if (ubifs_authenticated(c)) {
0282         err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
0283         if (err)
0284             return err;
0285     }
0286 
0287     err = ubifs_wbuf_write_nolock(wbuf, buf, len);
0288     if (err)
0289         return err;
0290     if (sync)
0291         err = ubifs_wbuf_sync_nolock(wbuf);
0292     return err;
0293 }
0294 
0295 /**
0296  * make_reservation - reserve journal space.
0297  * @c: UBIFS file-system description object
0298  * @jhead: journal head
0299  * @len: how many bytes to reserve
0300  *
0301  * This function makes space reservation in journal head @jhead. The function
0302  * takes the commit lock and locks the journal head, and the caller has to
0303  * unlock the head and finish the reservation with 'finish_reservation()'.
0304  * Returns zero in case of success and a negative error code in case of
0305  * failure.
0306  *
0307  * Note, the journal head may be unlocked as soon as the data is written, while
0308  * the commit lock has to be released after the data has been added to the
0309  * TNC.
0310  */
0311 static int make_reservation(struct ubifs_info *c, int jhead, int len)
0312 {
0313     int err, cmt_retries = 0, nospc_retries = 0;
0314 
0315 again:
0316     down_read(&c->commit_sem);
0317     err = reserve_space(c, jhead, len);
0318     if (!err)
0319         /* c->commit_sem will get released via finish_reservation(). */
0320         return 0;
0321     up_read(&c->commit_sem);
0322 
0323     if (err == -ENOSPC) {
0324         /*
0325          * GC could not make any progress. We should try to commit
0326          * once because it could make some dirty space and GC would
0327          * make progress, so make the error -EAGAIN so that the below
0328          * will commit and re-try.
0329          */
0330         if (nospc_retries++ < 2) {
0331             dbg_jnl("no space, retry");
0332             err = -EAGAIN;
0333         }
0334 
0335         /*
0336          * This means that the budgeting is incorrect. We always have
0337          * to be able to write to the media, because all operations are
0338          * budgeted. Deletions are not budgeted, though, but we reserve
0339          * an extra LEB for them.
0340          */
0341     }
0342 
0343     if (err != -EAGAIN)
0344         goto out;
0345 
0346     /*
0347      * -EAGAIN means that the journal is full or too large, or the above
0348      * code wants to do one commit. Do this and re-try.
0349      */
0350     if (cmt_retries > 128) {
0351         /*
0352          * This should not happen unless the journal size limitations
0353          * are too tough.
0354          */
0355         ubifs_err(c, "stuck in space allocation");
0356         err = -ENOSPC;
0357         goto out;
0358     } else if (cmt_retries > 32)
0359         ubifs_warn(c, "too many space allocation re-tries (%d)",
0360                cmt_retries);
0361 
0362     dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
0363         cmt_retries);
0364     cmt_retries += 1;
0365 
0366     err = ubifs_run_commit(c);
0367     if (err)
0368         return err;
0369     goto again;
0370 
0371 out:
0372     ubifs_err(c, "cannot reserve %d bytes in jhead %d, error %d",
0373           len, jhead, err);
0374     if (err == -ENOSPC) {
0375         /* This are some budgeting problems, print useful information */
0376         down_write(&c->commit_sem);
0377         dump_stack();
0378         ubifs_dump_budg(c, &c->bi);
0379         ubifs_dump_lprops(c);
0380         cmt_retries = dbg_check_lprops(c);
0381         up_write(&c->commit_sem);
0382     }
0383     return err;
0384 }
0385 
0386 /**
0387  * release_head - release a journal head.
0388  * @c: UBIFS file-system description object
0389  * @jhead: journal head
0390  *
0391  * This function releases journal head @jhead which was locked by
0392  * the 'make_reservation()' function. It has to be called after each successful
0393  * 'make_reservation()' invocation.
0394  */
0395 static inline void release_head(struct ubifs_info *c, int jhead)
0396 {
0397     mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
0398 }
0399 
0400 /**
0401  * finish_reservation - finish a reservation.
0402  * @c: UBIFS file-system description object
0403  *
0404  * This function finishes journal space reservation. It must be called after
0405  * 'make_reservation()'.
0406  */
0407 static void finish_reservation(struct ubifs_info *c)
0408 {
0409     up_read(&c->commit_sem);
0410 }
0411 
0412 /**
0413  * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
0414  * @mode: inode mode
0415  */
0416 static int get_dent_type(int mode)
0417 {
0418     switch (mode & S_IFMT) {
0419     case S_IFREG:
0420         return UBIFS_ITYPE_REG;
0421     case S_IFDIR:
0422         return UBIFS_ITYPE_DIR;
0423     case S_IFLNK:
0424         return UBIFS_ITYPE_LNK;
0425     case S_IFBLK:
0426         return UBIFS_ITYPE_BLK;
0427     case S_IFCHR:
0428         return UBIFS_ITYPE_CHR;
0429     case S_IFIFO:
0430         return UBIFS_ITYPE_FIFO;
0431     case S_IFSOCK:
0432         return UBIFS_ITYPE_SOCK;
0433     default:
0434         BUG();
0435     }
0436     return 0;
0437 }
0438 
0439 /**
0440  * pack_inode - pack an inode node.
0441  * @c: UBIFS file-system description object
0442  * @ino: buffer in which to pack inode node
0443  * @inode: inode to pack
0444  * @last: indicates the last node of the group
0445  */
0446 static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
0447                const struct inode *inode, int last)
0448 {
0449     int data_len = 0, last_reference = !inode->i_nlink;
0450     struct ubifs_inode *ui = ubifs_inode(inode);
0451 
0452     ino->ch.node_type = UBIFS_INO_NODE;
0453     ino_key_init_flash(c, &ino->key, inode->i_ino);
0454     ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
0455     ino->atime_sec  = cpu_to_le64(inode->i_atime.tv_sec);
0456     ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
0457     ino->ctime_sec  = cpu_to_le64(inode->i_ctime.tv_sec);
0458     ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
0459     ino->mtime_sec  = cpu_to_le64(inode->i_mtime.tv_sec);
0460     ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
0461     ino->uid   = cpu_to_le32(i_uid_read(inode));
0462     ino->gid   = cpu_to_le32(i_gid_read(inode));
0463     ino->mode  = cpu_to_le32(inode->i_mode);
0464     ino->flags = cpu_to_le32(ui->flags);
0465     ino->size  = cpu_to_le64(ui->ui_size);
0466     ino->nlink = cpu_to_le32(inode->i_nlink);
0467     ino->compr_type  = cpu_to_le16(ui->compr_type);
0468     ino->data_len    = cpu_to_le32(ui->data_len);
0469     ino->xattr_cnt   = cpu_to_le32(ui->xattr_cnt);
0470     ino->xattr_size  = cpu_to_le32(ui->xattr_size);
0471     ino->xattr_names = cpu_to_le32(ui->xattr_names);
0472     zero_ino_node_unused(ino);
0473 
0474     /*
0475      * Drop the attached data if this is a deletion inode, the data is not
0476      * needed anymore.
0477      */
0478     if (!last_reference) {
0479         memcpy(ino->data, ui->data, ui->data_len);
0480         data_len = ui->data_len;
0481     }
0482 
0483     ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
0484 }
0485 
0486 /**
0487  * mark_inode_clean - mark UBIFS inode as clean.
0488  * @c: UBIFS file-system description object
0489  * @ui: UBIFS inode to mark as clean
0490  *
0491  * This helper function marks UBIFS inode @ui as clean by cleaning the
0492  * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
0493  * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
0494  * just do nothing.
0495  */
0496 static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
0497 {
0498     if (ui->dirty)
0499         ubifs_release_dirty_inode_budget(c, ui);
0500     ui->dirty = 0;
0501 }
0502 
0503 static void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
0504 {
0505     if (c->double_hash)
0506         dent->cookie = (__force __le32) prandom_u32();
0507     else
0508         dent->cookie = 0;
0509 }
0510 
0511 /**
0512  * ubifs_jnl_update - update inode.
0513  * @c: UBIFS file-system description object
0514  * @dir: parent inode or host inode in case of extended attributes
0515  * @nm: directory entry name
0516  * @inode: inode to update
0517  * @deletion: indicates a directory entry deletion i.e unlink or rmdir
0518  * @xent: non-zero if the directory entry is an extended attribute entry
0519  *
0520  * This function updates an inode by writing a directory entry (or extended
0521  * attribute entry), the inode itself, and the parent directory inode (or the
0522  * host inode) to the journal.
0523  *
0524  * The function writes the host inode @dir last, which is important in case of
0525  * extended attributes. Indeed, then we guarantee that if the host inode gets
0526  * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
0527  * the extended attribute inode gets flushed too. And this is exactly what the
0528  * user expects - synchronizing the host inode synchronizes its extended
0529  * attributes. Similarly, this guarantees that if @dir is synchronized, its
0530  * directory entry corresponding to @nm gets synchronized too.
0531  *
0532  * If the inode (@inode) or the parent directory (@dir) are synchronous, this
0533  * function synchronizes the write-buffer.
0534  *
0535  * This function marks the @dir and @inode inodes as clean and returns zero on
0536  * success. In case of failure, a negative error code is returned.
0537  */
0538 int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
0539              const struct fscrypt_name *nm, const struct inode *inode,
0540              int deletion, int xent)
0541 {
0542     int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
0543     int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
0544     int last_reference = !!(deletion && inode->i_nlink == 0);
0545     struct ubifs_inode *ui = ubifs_inode(inode);
0546     struct ubifs_inode *host_ui = ubifs_inode(dir);
0547     struct ubifs_dent_node *dent;
0548     struct ubifs_ino_node *ino;
0549     union ubifs_key dent_key, ino_key;
0550     u8 hash_dent[UBIFS_HASH_ARR_SZ];
0551     u8 hash_ino[UBIFS_HASH_ARR_SZ];
0552     u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
0553 
0554     ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
0555 
0556     dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
0557     ilen = UBIFS_INO_NODE_SZ;
0558 
0559     /*
0560      * If the last reference to the inode is being deleted, then there is
0561      * no need to attach and write inode data, it is being deleted anyway.
0562      * And if the inode is being deleted, no need to synchronize
0563      * write-buffer even if the inode is synchronous.
0564      */
0565     if (!last_reference) {
0566         ilen += ui->data_len;
0567         sync |= IS_SYNC(inode);
0568     }
0569 
0570     aligned_dlen = ALIGN(dlen, 8);
0571     aligned_ilen = ALIGN(ilen, 8);
0572 
0573     len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
0574     /* Make sure to also account for extended attributes */
0575     if (ubifs_authenticated(c))
0576         len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
0577     else
0578         len += host_ui->data_len;
0579 
0580     dent = kzalloc(len, GFP_NOFS);
0581     if (!dent)
0582         return -ENOMEM;
0583 
0584     /* Make reservation before allocating sequence numbers */
0585     err = make_reservation(c, BASEHD, len);
0586     if (err)
0587         goto out_free;
0588 
0589     if (!xent) {
0590         dent->ch.node_type = UBIFS_DENT_NODE;
0591         if (fname_name(nm) == NULL)
0592             dent_key_init_hash(c, &dent_key, dir->i_ino, nm->hash);
0593         else
0594             dent_key_init(c, &dent_key, dir->i_ino, nm);
0595     } else {
0596         dent->ch.node_type = UBIFS_XENT_NODE;
0597         xent_key_init(c, &dent_key, dir->i_ino, nm);
0598     }
0599 
0600     key_write(c, &dent_key, dent->key);
0601     dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
0602     dent->type = get_dent_type(inode->i_mode);
0603     dent->nlen = cpu_to_le16(fname_len(nm));
0604     memcpy(dent->name, fname_name(nm), fname_len(nm));
0605     dent->name[fname_len(nm)] = '\0';
0606     set_dent_cookie(c, dent);
0607 
0608     zero_dent_node_unused(dent);
0609     ubifs_prep_grp_node(c, dent, dlen, 0);
0610     err = ubifs_node_calc_hash(c, dent, hash_dent);
0611     if (err)
0612         goto out_release;
0613 
0614     ino = (void *)dent + aligned_dlen;
0615     pack_inode(c, ino, inode, 0);
0616     err = ubifs_node_calc_hash(c, ino, hash_ino);
0617     if (err)
0618         goto out_release;
0619 
0620     ino = (void *)ino + aligned_ilen;
0621     pack_inode(c, ino, dir, 1);
0622     err = ubifs_node_calc_hash(c, ino, hash_ino_host);
0623     if (err)
0624         goto out_release;
0625 
0626     if (last_reference) {
0627         err = ubifs_add_orphan(c, inode->i_ino);
0628         if (err) {
0629             release_head(c, BASEHD);
0630             goto out_finish;
0631         }
0632         ui->del_cmtno = c->cmt_no;
0633         orphan_added = 1;
0634     }
0635 
0636     err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
0637     if (err)
0638         goto out_release;
0639     if (!sync) {
0640         struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
0641 
0642         ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
0643         ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
0644     }
0645     release_head(c, BASEHD);
0646     kfree(dent);
0647     ubifs_add_auth_dirt(c, lnum);
0648 
0649     if (deletion) {
0650         if (fname_name(nm) == NULL)
0651             err = ubifs_tnc_remove_dh(c, &dent_key, nm->minor_hash);
0652         else
0653             err = ubifs_tnc_remove_nm(c, &dent_key, nm);
0654         if (err)
0655             goto out_ro;
0656         err = ubifs_add_dirt(c, lnum, dlen);
0657     } else
0658         err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
0659                        hash_dent, nm);
0660     if (err)
0661         goto out_ro;
0662 
0663     /*
0664      * Note, we do not remove the inode from TNC even if the last reference
0665      * to it has just been deleted, because the inode may still be opened.
0666      * Instead, the inode has been added to orphan lists and the orphan
0667      * subsystem will take further care about it.
0668      */
0669     ino_key_init(c, &ino_key, inode->i_ino);
0670     ino_offs = dent_offs + aligned_dlen;
0671     err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
0672     if (err)
0673         goto out_ro;
0674 
0675     ino_key_init(c, &ino_key, dir->i_ino);
0676     ino_offs += aligned_ilen;
0677     err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
0678                 UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
0679     if (err)
0680         goto out_ro;
0681 
0682     finish_reservation(c);
0683     spin_lock(&ui->ui_lock);
0684     ui->synced_i_size = ui->ui_size;
0685     spin_unlock(&ui->ui_lock);
0686     if (xent) {
0687         spin_lock(&host_ui->ui_lock);
0688         host_ui->synced_i_size = host_ui->ui_size;
0689         spin_unlock(&host_ui->ui_lock);
0690     }
0691     mark_inode_clean(c, ui);
0692     mark_inode_clean(c, host_ui);
0693     return 0;
0694 
0695 out_finish:
0696     finish_reservation(c);
0697 out_free:
0698     kfree(dent);
0699     return err;
0700 
0701 out_release:
0702     release_head(c, BASEHD);
0703     kfree(dent);
0704 out_ro:
0705     ubifs_ro_mode(c, err);
0706     if (orphan_added)
0707         ubifs_delete_orphan(c, inode->i_ino);
0708     finish_reservation(c);
0709     return err;
0710 }
0711 
0712 /**
0713  * ubifs_jnl_write_data - write a data node to the journal.
0714  * @c: UBIFS file-system description object
0715  * @inode: inode the data node belongs to
0716  * @key: node key
0717  * @buf: buffer to write
0718  * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
0719  *
0720  * This function writes a data node to the journal. Returns %0 if the data node
0721  * was successfully written, and a negative error code in case of failure.
0722  */
0723 int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
0724              const union ubifs_key *key, const void *buf, int len)
0725 {
0726     struct ubifs_data_node *data;
0727     int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
0728     int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
0729     int write_len;
0730     struct ubifs_inode *ui = ubifs_inode(inode);
0731     bool encrypted = IS_ENCRYPTED(inode);
0732     u8 hash[UBIFS_HASH_ARR_SZ];
0733 
0734     dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
0735         (unsigned long)key_inum(c, key), key_block(c, key), len);
0736     ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
0737 
0738     if (encrypted)
0739         dlen += UBIFS_CIPHER_BLOCK_SIZE;
0740 
0741     auth_len = ubifs_auth_node_sz(c);
0742 
0743     data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
0744     if (!data) {
0745         /*
0746          * Fall-back to the write reserve buffer. Note, we might be
0747          * currently on the memory reclaim path, when the kernel is
0748          * trying to free some memory by writing out dirty pages. The
0749          * write reserve buffer helps us to guarantee that we are
0750          * always able to write the data.
0751          */
0752         allocated = 0;
0753         mutex_lock(&c->write_reserve_mutex);
0754         data = c->write_reserve_buf;
0755     }
0756 
0757     data->ch.node_type = UBIFS_DATA_NODE;
0758     key_write(c, key, &data->key);
0759     data->size = cpu_to_le32(len);
0760 
0761     if (!(ui->flags & UBIFS_COMPR_FL))
0762         /* Compression is disabled for this inode */
0763         compr_type = UBIFS_COMPR_NONE;
0764     else
0765         compr_type = ui->compr_type;
0766 
0767     out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
0768     ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
0769     ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
0770 
0771     if (encrypted) {
0772         err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
0773         if (err)
0774             goto out_free;
0775 
0776     } else {
0777         data->compr_size = 0;
0778         out_len = compr_len;
0779     }
0780 
0781     dlen = UBIFS_DATA_NODE_SZ + out_len;
0782     if (ubifs_authenticated(c))
0783         write_len = ALIGN(dlen, 8) + auth_len;
0784     else
0785         write_len = dlen;
0786 
0787     data->compr_type = cpu_to_le16(compr_type);
0788 
0789     /* Make reservation before allocating sequence numbers */
0790     err = make_reservation(c, DATAHD, write_len);
0791     if (err)
0792         goto out_free;
0793 
0794     ubifs_prepare_node(c, data, dlen, 0);
0795     err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
0796     if (err)
0797         goto out_release;
0798 
0799     err = ubifs_node_calc_hash(c, data, hash);
0800     if (err)
0801         goto out_release;
0802 
0803     ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
0804     release_head(c, DATAHD);
0805 
0806     ubifs_add_auth_dirt(c, lnum);
0807 
0808     err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
0809     if (err)
0810         goto out_ro;
0811 
0812     finish_reservation(c);
0813     if (!allocated)
0814         mutex_unlock(&c->write_reserve_mutex);
0815     else
0816         kfree(data);
0817     return 0;
0818 
0819 out_release:
0820     release_head(c, DATAHD);
0821 out_ro:
0822     ubifs_ro_mode(c, err);
0823     finish_reservation(c);
0824 out_free:
0825     if (!allocated)
0826         mutex_unlock(&c->write_reserve_mutex);
0827     else
0828         kfree(data);
0829     return err;
0830 }
0831 
0832 /**
0833  * ubifs_jnl_write_inode - flush inode to the journal.
0834  * @c: UBIFS file-system description object
0835  * @inode: inode to flush
0836  *
0837  * This function writes inode @inode to the journal. If the inode is
0838  * synchronous, it also synchronizes the write-buffer. Returns zero in case of
0839  * success and a negative error code in case of failure.
0840  */
0841 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
0842 {
0843     int err, lnum, offs;
0844     struct ubifs_ino_node *ino, *ino_start;
0845     struct ubifs_inode *ui = ubifs_inode(inode);
0846     int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
0847     int last_reference = !inode->i_nlink;
0848     int kill_xattrs = ui->xattr_cnt && last_reference;
0849     u8 hash[UBIFS_HASH_ARR_SZ];
0850 
0851     dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
0852 
0853     /*
0854      * If the inode is being deleted, do not write the attached data. No
0855      * need to synchronize the write-buffer either.
0856      */
0857     if (!last_reference) {
0858         ilen += ui->data_len;
0859         sync = IS_SYNC(inode);
0860     } else if (kill_xattrs) {
0861         write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
0862     }
0863 
0864     if (ubifs_authenticated(c))
0865         write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
0866     else
0867         write_len += ilen;
0868 
0869     ino_start = ino = kmalloc(write_len, GFP_NOFS);
0870     if (!ino)
0871         return -ENOMEM;
0872 
0873     /* Make reservation before allocating sequence numbers */
0874     err = make_reservation(c, BASEHD, write_len);
0875     if (err)
0876         goto out_free;
0877 
0878     if (kill_xattrs) {
0879         union ubifs_key key;
0880         struct fscrypt_name nm = {0};
0881         struct inode *xino;
0882         struct ubifs_dent_node *xent, *pxent = NULL;
0883 
0884         if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
0885             err = -EPERM;
0886             ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
0887             goto out_release;
0888         }
0889 
0890         lowest_xent_key(c, &key, inode->i_ino);
0891         while (1) {
0892             xent = ubifs_tnc_next_ent(c, &key, &nm);
0893             if (IS_ERR(xent)) {
0894                 err = PTR_ERR(xent);
0895                 if (err == -ENOENT)
0896                     break;
0897 
0898                 kfree(pxent);
0899                 goto out_release;
0900             }
0901 
0902             fname_name(&nm) = xent->name;
0903             fname_len(&nm) = le16_to_cpu(xent->nlen);
0904 
0905             xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
0906             if (IS_ERR(xino)) {
0907                 err = PTR_ERR(xino);
0908                 ubifs_err(c, "dead directory entry '%s', error %d",
0909                       xent->name, err);
0910                 ubifs_ro_mode(c, err);
0911                 kfree(pxent);
0912                 kfree(xent);
0913                 goto out_release;
0914             }
0915             ubifs_assert(c, ubifs_inode(xino)->xattr);
0916 
0917             clear_nlink(xino);
0918             pack_inode(c, ino, xino, 0);
0919             ino = (void *)ino + UBIFS_INO_NODE_SZ;
0920             iput(xino);
0921 
0922             kfree(pxent);
0923             pxent = xent;
0924             key_read(c, &xent->key, &key);
0925         }
0926         kfree(pxent);
0927     }
0928 
0929     pack_inode(c, ino, inode, 1);
0930     err = ubifs_node_calc_hash(c, ino, hash);
0931     if (err)
0932         goto out_release;
0933 
0934     err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
0935     if (err)
0936         goto out_release;
0937     if (!sync)
0938         ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
0939                       inode->i_ino);
0940     release_head(c, BASEHD);
0941 
0942     if (last_reference) {
0943         err = ubifs_tnc_remove_ino(c, inode->i_ino);
0944         if (err)
0945             goto out_ro;
0946         ubifs_delete_orphan(c, inode->i_ino);
0947         err = ubifs_add_dirt(c, lnum, write_len);
0948     } else {
0949         union ubifs_key key;
0950 
0951         ubifs_add_auth_dirt(c, lnum);
0952 
0953         ino_key_init(c, &key, inode->i_ino);
0954         err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
0955     }
0956     if (err)
0957         goto out_ro;
0958 
0959     finish_reservation(c);
0960     spin_lock(&ui->ui_lock);
0961     ui->synced_i_size = ui->ui_size;
0962     spin_unlock(&ui->ui_lock);
0963     kfree(ino_start);
0964     return 0;
0965 
0966 out_release:
0967     release_head(c, BASEHD);
0968 out_ro:
0969     ubifs_ro_mode(c, err);
0970     finish_reservation(c);
0971 out_free:
0972     kfree(ino_start);
0973     return err;
0974 }
0975 
0976 /**
0977  * ubifs_jnl_delete_inode - delete an inode.
0978  * @c: UBIFS file-system description object
0979  * @inode: inode to delete
0980  *
0981  * This function deletes inode @inode which includes removing it from orphans,
0982  * deleting it from TNC and, in some cases, writing a deletion inode to the
0983  * journal.
0984  *
0985  * When regular file inodes are unlinked or a directory inode is removed, the
0986  * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
0987  * direntry to the media, and adds the inode to orphans. After this, when the
0988  * last reference to this inode has been dropped, this function is called. In
0989  * general, it has to write one more deletion inode to the media, because if
0990  * a commit happened between 'ubifs_jnl_update()' and
0991  * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
0992  * anymore, and in fact it might not be on the flash anymore, because it might
0993  * have been garbage-collected already. And for optimization reasons UBIFS does
0994  * not read the orphan area if it has been unmounted cleanly, so it would have
0995  * no indication in the journal that there is a deleted inode which has to be
0996  * removed from TNC.
0997  *
0998  * However, if there was no commit between 'ubifs_jnl_update()' and
0999  * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
1000  * inode to the media for the second time. And this is quite a typical case.
1001  *
1002  * This function returns zero in case of success and a negative error code in
1003  * case of failure.
1004  */
1005 int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
1006 {
1007     int err;
1008     struct ubifs_inode *ui = ubifs_inode(inode);
1009 
1010     ubifs_assert(c, inode->i_nlink == 0);
1011 
1012     if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
1013         /* A commit happened for sure or inode hosts xattrs */
1014         return ubifs_jnl_write_inode(c, inode);
1015 
1016     down_read(&c->commit_sem);
1017     /*
1018      * Check commit number again, because the first test has been done
1019      * without @c->commit_sem, so a commit might have happened.
1020      */
1021     if (ui->del_cmtno != c->cmt_no) {
1022         up_read(&c->commit_sem);
1023         return ubifs_jnl_write_inode(c, inode);
1024     }
1025 
1026     err = ubifs_tnc_remove_ino(c, inode->i_ino);
1027     if (err)
1028         ubifs_ro_mode(c, err);
1029     else
1030         ubifs_delete_orphan(c, inode->i_ino);
1031     up_read(&c->commit_sem);
1032     return err;
1033 }
1034 
1035 /**
1036  * ubifs_jnl_xrename - cross rename two directory entries.
1037  * @c: UBIFS file-system description object
1038  * @fst_dir: parent inode of 1st directory entry to exchange
1039  * @fst_inode: 1st inode to exchange
1040  * @fst_nm: name of 1st inode to exchange
1041  * @snd_dir: parent inode of 2nd directory entry to exchange
1042  * @snd_inode: 2nd inode to exchange
1043  * @snd_nm: name of 2nd inode to exchange
1044  * @sync: non-zero if the write-buffer has to be synchronized
1045  *
1046  * This function implements the cross rename operation which may involve
1047  * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
1048  * and returns zero on success. In case of failure, a negative error code is
1049  * returned.
1050  */
1051 int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
1052               const struct inode *fst_inode,
1053               const struct fscrypt_name *fst_nm,
1054               const struct inode *snd_dir,
1055               const struct inode *snd_inode,
1056               const struct fscrypt_name *snd_nm, int sync)
1057 {
1058     union ubifs_key key;
1059     struct ubifs_dent_node *dent1, *dent2;
1060     int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
1061     int aligned_dlen1, aligned_dlen2;
1062     int twoparents = (fst_dir != snd_dir);
1063     void *p;
1064     u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1065     u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1066     u8 hash_p1[UBIFS_HASH_ARR_SZ];
1067     u8 hash_p2[UBIFS_HASH_ARR_SZ];
1068 
1069     ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
1070     ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
1071     ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
1072     ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
1073 
1074     dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
1075     dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
1076     aligned_dlen1 = ALIGN(dlen1, 8);
1077     aligned_dlen2 = ALIGN(dlen2, 8);
1078 
1079     len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
1080     if (twoparents)
1081         len += plen;
1082 
1083     len += ubifs_auth_node_sz(c);
1084 
1085     dent1 = kzalloc(len, GFP_NOFS);
1086     if (!dent1)
1087         return -ENOMEM;
1088 
1089     /* Make reservation before allocating sequence numbers */
1090     err = make_reservation(c, BASEHD, len);
1091     if (err)
1092         goto out_free;
1093 
1094     /* Make new dent for 1st entry */
1095     dent1->ch.node_type = UBIFS_DENT_NODE;
1096     dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
1097     dent1->inum = cpu_to_le64(fst_inode->i_ino);
1098     dent1->type = get_dent_type(fst_inode->i_mode);
1099     dent1->nlen = cpu_to_le16(fname_len(snd_nm));
1100     memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
1101     dent1->name[fname_len(snd_nm)] = '\0';
1102     set_dent_cookie(c, dent1);
1103     zero_dent_node_unused(dent1);
1104     ubifs_prep_grp_node(c, dent1, dlen1, 0);
1105     err = ubifs_node_calc_hash(c, dent1, hash_dent1);
1106     if (err)
1107         goto out_release;
1108 
1109     /* Make new dent for 2nd entry */
1110     dent2 = (void *)dent1 + aligned_dlen1;
1111     dent2->ch.node_type = UBIFS_DENT_NODE;
1112     dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
1113     dent2->inum = cpu_to_le64(snd_inode->i_ino);
1114     dent2->type = get_dent_type(snd_inode->i_mode);
1115     dent2->nlen = cpu_to_le16(fname_len(fst_nm));
1116     memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
1117     dent2->name[fname_len(fst_nm)] = '\0';
1118     set_dent_cookie(c, dent2);
1119     zero_dent_node_unused(dent2);
1120     ubifs_prep_grp_node(c, dent2, dlen2, 0);
1121     err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1122     if (err)
1123         goto out_release;
1124 
1125     p = (void *)dent2 + aligned_dlen2;
1126     if (!twoparents) {
1127         pack_inode(c, p, fst_dir, 1);
1128         err = ubifs_node_calc_hash(c, p, hash_p1);
1129         if (err)
1130             goto out_release;
1131     } else {
1132         pack_inode(c, p, fst_dir, 0);
1133         err = ubifs_node_calc_hash(c, p, hash_p1);
1134         if (err)
1135             goto out_release;
1136         p += ALIGN(plen, 8);
1137         pack_inode(c, p, snd_dir, 1);
1138         err = ubifs_node_calc_hash(c, p, hash_p2);
1139         if (err)
1140             goto out_release;
1141     }
1142 
1143     err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
1144     if (err)
1145         goto out_release;
1146     if (!sync) {
1147         struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1148 
1149         ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
1150         ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
1151     }
1152     release_head(c, BASEHD);
1153 
1154     ubifs_add_auth_dirt(c, lnum);
1155 
1156     dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
1157     err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
1158     if (err)
1159         goto out_ro;
1160 
1161     offs += aligned_dlen1;
1162     dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
1163     err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
1164     if (err)
1165         goto out_ro;
1166 
1167     offs += aligned_dlen2;
1168 
1169     ino_key_init(c, &key, fst_dir->i_ino);
1170     err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
1171     if (err)
1172         goto out_ro;
1173 
1174     if (twoparents) {
1175         offs += ALIGN(plen, 8);
1176         ino_key_init(c, &key, snd_dir->i_ino);
1177         err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
1178         if (err)
1179             goto out_ro;
1180     }
1181 
1182     finish_reservation(c);
1183 
1184     mark_inode_clean(c, ubifs_inode(fst_dir));
1185     if (twoparents)
1186         mark_inode_clean(c, ubifs_inode(snd_dir));
1187     kfree(dent1);
1188     return 0;
1189 
1190 out_release:
1191     release_head(c, BASEHD);
1192 out_ro:
1193     ubifs_ro_mode(c, err);
1194     finish_reservation(c);
1195 out_free:
1196     kfree(dent1);
1197     return err;
1198 }
1199 
1200 /**
1201  * ubifs_jnl_rename - rename a directory entry.
1202  * @c: UBIFS file-system description object
1203  * @old_dir: parent inode of directory entry to rename
1204  * @old_dentry: directory entry to rename
1205  * @new_dir: parent inode of directory entry to rename
1206  * @new_dentry: new directory entry (or directory entry to replace)
1207  * @sync: non-zero if the write-buffer has to be synchronized
1208  *
1209  * This function implements the re-name operation which may involve writing up
1210  * to 4 inodes(new inode, whiteout inode, old and new parent directory inodes)
1211  * and 2 directory entries. It marks the written inodes as clean and returns
1212  * zero on success. In case of failure, a negative error code is returned.
1213  */
1214 int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1215              const struct inode *old_inode,
1216              const struct fscrypt_name *old_nm,
1217              const struct inode *new_dir,
1218              const struct inode *new_inode,
1219              const struct fscrypt_name *new_nm,
1220              const struct inode *whiteout, int sync)
1221 {
1222     void *p;
1223     union ubifs_key key;
1224     struct ubifs_dent_node *dent, *dent2;
1225     int err, dlen1, dlen2, ilen, wlen, lnum, offs, len, orphan_added = 0;
1226     int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
1227     int last_reference = !!(new_inode && new_inode->i_nlink == 0);
1228     int move = (old_dir != new_dir);
1229     struct ubifs_inode *new_ui, *whiteout_ui;
1230     u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
1231     u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
1232     u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
1233     u8 hash_whiteout_inode[UBIFS_HASH_ARR_SZ];
1234     u8 hash_dent1[UBIFS_HASH_ARR_SZ];
1235     u8 hash_dent2[UBIFS_HASH_ARR_SZ];
1236 
1237     ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
1238     ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
1239     ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
1240     ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
1241 
1242     dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
1243     dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
1244     if (new_inode) {
1245         new_ui = ubifs_inode(new_inode);
1246         ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
1247         ilen = UBIFS_INO_NODE_SZ;
1248         if (!last_reference)
1249             ilen += new_ui->data_len;
1250     } else
1251         ilen = 0;
1252 
1253     if (whiteout) {
1254         whiteout_ui = ubifs_inode(whiteout);
1255         ubifs_assert(c, mutex_is_locked(&whiteout_ui->ui_mutex));
1256         ubifs_assert(c, whiteout->i_nlink == 1);
1257         ubifs_assert(c, !whiteout_ui->dirty);
1258         wlen = UBIFS_INO_NODE_SZ;
1259         wlen += whiteout_ui->data_len;
1260     } else
1261         wlen = 0;
1262 
1263     aligned_dlen1 = ALIGN(dlen1, 8);
1264     aligned_dlen2 = ALIGN(dlen2, 8);
1265     len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) +
1266           ALIGN(wlen, 8) + ALIGN(plen, 8);
1267     if (move)
1268         len += plen;
1269 
1270     len += ubifs_auth_node_sz(c);
1271 
1272     dent = kzalloc(len, GFP_NOFS);
1273     if (!dent)
1274         return -ENOMEM;
1275 
1276     /* Make reservation before allocating sequence numbers */
1277     err = make_reservation(c, BASEHD, len);
1278     if (err)
1279         goto out_free;
1280 
1281     /* Make new dent */
1282     dent->ch.node_type = UBIFS_DENT_NODE;
1283     dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
1284     dent->inum = cpu_to_le64(old_inode->i_ino);
1285     dent->type = get_dent_type(old_inode->i_mode);
1286     dent->nlen = cpu_to_le16(fname_len(new_nm));
1287     memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
1288     dent->name[fname_len(new_nm)] = '\0';
1289     set_dent_cookie(c, dent);
1290     zero_dent_node_unused(dent);
1291     ubifs_prep_grp_node(c, dent, dlen1, 0);
1292     err = ubifs_node_calc_hash(c, dent, hash_dent1);
1293     if (err)
1294         goto out_release;
1295 
1296     dent2 = (void *)dent + aligned_dlen1;
1297     dent2->ch.node_type = UBIFS_DENT_NODE;
1298     dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
1299 
1300     if (whiteout) {
1301         dent2->inum = cpu_to_le64(whiteout->i_ino);
1302         dent2->type = get_dent_type(whiteout->i_mode);
1303     } else {
1304         /* Make deletion dent */
1305         dent2->inum = 0;
1306         dent2->type = DT_UNKNOWN;
1307     }
1308     dent2->nlen = cpu_to_le16(fname_len(old_nm));
1309     memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
1310     dent2->name[fname_len(old_nm)] = '\0';
1311     set_dent_cookie(c, dent2);
1312     zero_dent_node_unused(dent2);
1313     ubifs_prep_grp_node(c, dent2, dlen2, 0);
1314     err = ubifs_node_calc_hash(c, dent2, hash_dent2);
1315     if (err)
1316         goto out_release;
1317 
1318     p = (void *)dent2 + aligned_dlen2;
1319     if (new_inode) {
1320         pack_inode(c, p, new_inode, 0);
1321         err = ubifs_node_calc_hash(c, p, hash_new_inode);
1322         if (err)
1323             goto out_release;
1324 
1325         p += ALIGN(ilen, 8);
1326     }
1327 
1328     if (whiteout) {
1329         pack_inode(c, p, whiteout, 0);
1330         err = ubifs_node_calc_hash(c, p, hash_whiteout_inode);
1331         if (err)
1332             goto out_release;
1333 
1334         p += ALIGN(wlen, 8);
1335     }
1336 
1337     if (!move) {
1338         pack_inode(c, p, old_dir, 1);
1339         err = ubifs_node_calc_hash(c, p, hash_old_dir);
1340         if (err)
1341             goto out_release;
1342     } else {
1343         pack_inode(c, p, old_dir, 0);
1344         err = ubifs_node_calc_hash(c, p, hash_old_dir);
1345         if (err)
1346             goto out_release;
1347 
1348         p += ALIGN(plen, 8);
1349         pack_inode(c, p, new_dir, 1);
1350         err = ubifs_node_calc_hash(c, p, hash_new_dir);
1351         if (err)
1352             goto out_release;
1353     }
1354 
1355     if (last_reference) {
1356         err = ubifs_add_orphan(c, new_inode->i_ino);
1357         if (err) {
1358             release_head(c, BASEHD);
1359             goto out_finish;
1360         }
1361         new_ui->del_cmtno = c->cmt_no;
1362         orphan_added = 1;
1363     }
1364 
1365     err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
1366     if (err)
1367         goto out_release;
1368     if (!sync) {
1369         struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1370 
1371         ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
1372         ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
1373         if (new_inode)
1374             ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1375                           new_inode->i_ino);
1376         if (whiteout)
1377             ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1378                           whiteout->i_ino);
1379     }
1380     release_head(c, BASEHD);
1381 
1382     ubifs_add_auth_dirt(c, lnum);
1383 
1384     dent_key_init(c, &key, new_dir->i_ino, new_nm);
1385     err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
1386     if (err)
1387         goto out_ro;
1388 
1389     offs += aligned_dlen1;
1390     if (whiteout) {
1391         dent_key_init(c, &key, old_dir->i_ino, old_nm);
1392         err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
1393         if (err)
1394             goto out_ro;
1395     } else {
1396         err = ubifs_add_dirt(c, lnum, dlen2);
1397         if (err)
1398             goto out_ro;
1399 
1400         dent_key_init(c, &key, old_dir->i_ino, old_nm);
1401         err = ubifs_tnc_remove_nm(c, &key, old_nm);
1402         if (err)
1403             goto out_ro;
1404     }
1405 
1406     offs += aligned_dlen2;
1407     if (new_inode) {
1408         ino_key_init(c, &key, new_inode->i_ino);
1409         err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
1410         if (err)
1411             goto out_ro;
1412         offs += ALIGN(ilen, 8);
1413     }
1414 
1415     if (whiteout) {
1416         ino_key_init(c, &key, whiteout->i_ino);
1417         err = ubifs_tnc_add(c, &key, lnum, offs, wlen,
1418                     hash_whiteout_inode);
1419         if (err)
1420             goto out_ro;
1421         offs += ALIGN(wlen, 8);
1422     }
1423 
1424     ino_key_init(c, &key, old_dir->i_ino);
1425     err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
1426     if (err)
1427         goto out_ro;
1428 
1429     if (move) {
1430         offs += ALIGN(plen, 8);
1431         ino_key_init(c, &key, new_dir->i_ino);
1432         err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
1433         if (err)
1434             goto out_ro;
1435     }
1436 
1437     finish_reservation(c);
1438     if (new_inode) {
1439         mark_inode_clean(c, new_ui);
1440         spin_lock(&new_ui->ui_lock);
1441         new_ui->synced_i_size = new_ui->ui_size;
1442         spin_unlock(&new_ui->ui_lock);
1443     }
1444     /*
1445      * No need to mark whiteout inode clean.
1446      * Whiteout doesn't have non-zero size, no need to update
1447      * synced_i_size for whiteout_ui.
1448      */
1449     mark_inode_clean(c, ubifs_inode(old_dir));
1450     if (move)
1451         mark_inode_clean(c, ubifs_inode(new_dir));
1452     kfree(dent);
1453     return 0;
1454 
1455 out_release:
1456     release_head(c, BASEHD);
1457 out_ro:
1458     ubifs_ro_mode(c, err);
1459     if (orphan_added)
1460         ubifs_delete_orphan(c, new_inode->i_ino);
1461 out_finish:
1462     finish_reservation(c);
1463 out_free:
1464     kfree(dent);
1465     return err;
1466 }
1467 
1468 /**
1469  * truncate_data_node - re-compress/encrypt a truncated data node.
1470  * @c: UBIFS file-system description object
1471  * @inode: inode which refers to the data node
1472  * @block: data block number
1473  * @dn: data node to re-compress
1474  * @new_len: new length
1475  *
1476  * This function is used when an inode is truncated and the last data node of
1477  * the inode has to be re-compressed/encrypted and re-written.
1478  */
1479 static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
1480                   unsigned int block, struct ubifs_data_node *dn,
1481                   int *new_len)
1482 {
1483     void *buf;
1484     int err, dlen, compr_type, out_len, old_dlen;
1485 
1486     out_len = le32_to_cpu(dn->size);
1487     buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
1488     if (!buf)
1489         return -ENOMEM;
1490 
1491     dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
1492     compr_type = le16_to_cpu(dn->compr_type);
1493 
1494     if (IS_ENCRYPTED(inode)) {
1495         err = ubifs_decrypt(inode, dn, &dlen, block);
1496         if (err)
1497             goto out;
1498     }
1499 
1500     if (compr_type == UBIFS_COMPR_NONE) {
1501         out_len = *new_len;
1502     } else {
1503         err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
1504         if (err)
1505             goto out;
1506 
1507         ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
1508     }
1509 
1510     if (IS_ENCRYPTED(inode)) {
1511         err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
1512         if (err)
1513             goto out;
1514 
1515         out_len = old_dlen;
1516     } else {
1517         dn->compr_size = 0;
1518     }
1519 
1520     ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
1521     dn->compr_type = cpu_to_le16(compr_type);
1522     dn->size = cpu_to_le32(*new_len);
1523     *new_len = UBIFS_DATA_NODE_SZ + out_len;
1524     err = 0;
1525 out:
1526     kfree(buf);
1527     return err;
1528 }
1529 
1530 /**
1531  * ubifs_jnl_truncate - update the journal for a truncation.
1532  * @c: UBIFS file-system description object
1533  * @inode: inode to truncate
1534  * @old_size: old size
1535  * @new_size: new size
1536  *
1537  * When the size of a file decreases due to truncation, a truncation node is
1538  * written, the journal tree is updated, and the last data block is re-written
1539  * if it has been affected. The inode is also updated in order to synchronize
1540  * the new inode size.
1541  *
1542  * This function marks the inode as clean and returns zero on success. In case
1543  * of failure, a negative error code is returned.
1544  */
1545 int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1546                loff_t old_size, loff_t new_size)
1547 {
1548     union ubifs_key key, to_key;
1549     struct ubifs_ino_node *ino;
1550     struct ubifs_trun_node *trun;
1551     struct ubifs_data_node *dn;
1552     int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
1553     struct ubifs_inode *ui = ubifs_inode(inode);
1554     ino_t inum = inode->i_ino;
1555     unsigned int blk;
1556     u8 hash_ino[UBIFS_HASH_ARR_SZ];
1557     u8 hash_dn[UBIFS_HASH_ARR_SZ];
1558 
1559     dbg_jnl("ino %lu, size %lld -> %lld",
1560         (unsigned long)inum, old_size, new_size);
1561     ubifs_assert(c, !ui->data_len);
1562     ubifs_assert(c, S_ISREG(inode->i_mode));
1563     ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
1564 
1565     sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
1566          UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
1567 
1568     sz += ubifs_auth_node_sz(c);
1569 
1570     ino = kmalloc(sz, GFP_NOFS);
1571     if (!ino)
1572         return -ENOMEM;
1573 
1574     trun = (void *)ino + UBIFS_INO_NODE_SZ;
1575     trun->ch.node_type = UBIFS_TRUN_NODE;
1576     trun->inum = cpu_to_le32(inum);
1577     trun->old_size = cpu_to_le64(old_size);
1578     trun->new_size = cpu_to_le64(new_size);
1579     zero_trun_node_unused(trun);
1580 
1581     dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
1582     if (dlen) {
1583         /* Get last data block so it can be truncated */
1584         dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
1585         blk = new_size >> UBIFS_BLOCK_SHIFT;
1586         data_key_init(c, &key, inum, blk);
1587         dbg_jnlk(&key, "last block key ");
1588         err = ubifs_tnc_lookup(c, &key, dn);
1589         if (err == -ENOENT)
1590             dlen = 0; /* Not found (so it is a hole) */
1591         else if (err)
1592             goto out_free;
1593         else {
1594             int dn_len = le32_to_cpu(dn->size);
1595 
1596             if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
1597                 ubifs_err(c, "bad data node (block %u, inode %lu)",
1598                       blk, inode->i_ino);
1599                 ubifs_dump_node(c, dn, sz - UBIFS_INO_NODE_SZ -
1600                         UBIFS_TRUN_NODE_SZ);
1601                 goto out_free;
1602             }
1603 
1604             if (dn_len <= dlen)
1605                 dlen = 0; /* Nothing to do */
1606             else {
1607                 err = truncate_data_node(c, inode, blk, dn, &dlen);
1608                 if (err)
1609                     goto out_free;
1610             }
1611         }
1612     }
1613 
1614     /* Must make reservation before allocating sequence numbers */
1615     len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1616 
1617     if (ubifs_authenticated(c))
1618         len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
1619     else
1620         len += dlen;
1621 
1622     err = make_reservation(c, BASEHD, len);
1623     if (err)
1624         goto out_free;
1625 
1626     pack_inode(c, ino, inode, 0);
1627     err = ubifs_node_calc_hash(c, ino, hash_ino);
1628     if (err)
1629         goto out_release;
1630 
1631     ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1632     if (dlen) {
1633         ubifs_prep_grp_node(c, dn, dlen, 1);
1634         err = ubifs_node_calc_hash(c, dn, hash_dn);
1635         if (err)
1636             goto out_release;
1637     }
1638 
1639     err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1640     if (err)
1641         goto out_release;
1642     if (!sync)
1643         ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
1644     release_head(c, BASEHD);
1645 
1646     ubifs_add_auth_dirt(c, lnum);
1647 
1648     if (dlen) {
1649         sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1650         err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
1651         if (err)
1652             goto out_ro;
1653     }
1654 
1655     ino_key_init(c, &key, inum);
1656     err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
1657     if (err)
1658         goto out_ro;
1659 
1660     err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
1661     if (err)
1662         goto out_ro;
1663 
1664     bit = new_size & (UBIFS_BLOCK_SIZE - 1);
1665     blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
1666     data_key_init(c, &key, inum, blk);
1667 
1668     bit = old_size & (UBIFS_BLOCK_SIZE - 1);
1669     blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
1670     data_key_init(c, &to_key, inum, blk);
1671 
1672     err = ubifs_tnc_remove_range(c, &key, &to_key);
1673     if (err)
1674         goto out_ro;
1675 
1676     finish_reservation(c);
1677     spin_lock(&ui->ui_lock);
1678     ui->synced_i_size = ui->ui_size;
1679     spin_unlock(&ui->ui_lock);
1680     mark_inode_clean(c, ui);
1681     kfree(ino);
1682     return 0;
1683 
1684 out_release:
1685     release_head(c, BASEHD);
1686 out_ro:
1687     ubifs_ro_mode(c, err);
1688     finish_reservation(c);
1689 out_free:
1690     kfree(ino);
1691     return err;
1692 }
1693 
1694 
1695 /**
1696  * ubifs_jnl_delete_xattr - delete an extended attribute.
1697  * @c: UBIFS file-system description object
1698  * @host: host inode
1699  * @inode: extended attribute inode
1700  * @nm: extended attribute entry name
1701  *
1702  * This function delete an extended attribute which is very similar to
1703  * un-linking regular files - it writes a deletion xentry, a deletion inode and
1704  * updates the target inode. Returns zero in case of success and a negative
1705  * error code in case of failure.
1706  */
1707 int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1708                const struct inode *inode,
1709                const struct fscrypt_name *nm)
1710 {
1711     int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
1712     struct ubifs_dent_node *xent;
1713     struct ubifs_ino_node *ino;
1714     union ubifs_key xent_key, key1, key2;
1715     int sync = IS_DIRSYNC(host);
1716     struct ubifs_inode *host_ui = ubifs_inode(host);
1717     u8 hash[UBIFS_HASH_ARR_SZ];
1718 
1719     ubifs_assert(c, inode->i_nlink == 0);
1720     ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1721 
1722     /*
1723      * Since we are deleting the inode, we do not bother to attach any data
1724      * to it and assume its length is %UBIFS_INO_NODE_SZ.
1725      */
1726     xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
1727     aligned_xlen = ALIGN(xlen, 8);
1728     hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
1729     len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
1730 
1731     write_len = len + ubifs_auth_node_sz(c);
1732 
1733     xent = kzalloc(write_len, GFP_NOFS);
1734     if (!xent)
1735         return -ENOMEM;
1736 
1737     /* Make reservation before allocating sequence numbers */
1738     err = make_reservation(c, BASEHD, write_len);
1739     if (err) {
1740         kfree(xent);
1741         return err;
1742     }
1743 
1744     xent->ch.node_type = UBIFS_XENT_NODE;
1745     xent_key_init(c, &xent_key, host->i_ino, nm);
1746     key_write(c, &xent_key, xent->key);
1747     xent->inum = 0;
1748     xent->type = get_dent_type(inode->i_mode);
1749     xent->nlen = cpu_to_le16(fname_len(nm));
1750     memcpy(xent->name, fname_name(nm), fname_len(nm));
1751     xent->name[fname_len(nm)] = '\0';
1752     zero_dent_node_unused(xent);
1753     ubifs_prep_grp_node(c, xent, xlen, 0);
1754 
1755     ino = (void *)xent + aligned_xlen;
1756     pack_inode(c, ino, inode, 0);
1757     ino = (void *)ino + UBIFS_INO_NODE_SZ;
1758     pack_inode(c, ino, host, 1);
1759     err = ubifs_node_calc_hash(c, ino, hash);
1760     if (err)
1761         goto out_release;
1762 
1763     err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
1764     if (!sync && !err)
1765         ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
1766     release_head(c, BASEHD);
1767 
1768     ubifs_add_auth_dirt(c, lnum);
1769     kfree(xent);
1770     if (err)
1771         goto out_ro;
1772 
1773     /* Remove the extended attribute entry from TNC */
1774     err = ubifs_tnc_remove_nm(c, &xent_key, nm);
1775     if (err)
1776         goto out_ro;
1777     err = ubifs_add_dirt(c, lnum, xlen);
1778     if (err)
1779         goto out_ro;
1780 
1781     /*
1782      * Remove all nodes belonging to the extended attribute inode from TNC.
1783      * Well, there actually must be only one node - the inode itself.
1784      */
1785     lowest_ino_key(c, &key1, inode->i_ino);
1786     highest_ino_key(c, &key2, inode->i_ino);
1787     err = ubifs_tnc_remove_range(c, &key1, &key2);
1788     if (err)
1789         goto out_ro;
1790     err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
1791     if (err)
1792         goto out_ro;
1793 
1794     /* And update TNC with the new host inode position */
1795     ino_key_init(c, &key1, host->i_ino);
1796     err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
1797     if (err)
1798         goto out_ro;
1799 
1800     finish_reservation(c);
1801     spin_lock(&host_ui->ui_lock);
1802     host_ui->synced_i_size = host_ui->ui_size;
1803     spin_unlock(&host_ui->ui_lock);
1804     mark_inode_clean(c, host_ui);
1805     return 0;
1806 
1807 out_release:
1808     kfree(xent);
1809     release_head(c, BASEHD);
1810 out_ro:
1811     ubifs_ro_mode(c, err);
1812     finish_reservation(c);
1813     return err;
1814 }
1815 
1816 /**
1817  * ubifs_jnl_change_xattr - change an extended attribute.
1818  * @c: UBIFS file-system description object
1819  * @inode: extended attribute inode
1820  * @host: host inode
1821  *
1822  * This function writes the updated version of an extended attribute inode and
1823  * the host inode to the journal (to the base head). The host inode is written
1824  * after the extended attribute inode in order to guarantee that the extended
1825  * attribute will be flushed when the inode is synchronized by 'fsync()' and
1826  * consequently, the write-buffer is synchronized. This function returns zero
1827  * in case of success and a negative error code in case of failure.
1828  */
1829 int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1830                const struct inode *host)
1831 {
1832     int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
1833     struct ubifs_inode *host_ui = ubifs_inode(host);
1834     struct ubifs_ino_node *ino;
1835     union ubifs_key key;
1836     int sync = IS_DIRSYNC(host);
1837     u8 hash_host[UBIFS_HASH_ARR_SZ];
1838     u8 hash[UBIFS_HASH_ARR_SZ];
1839 
1840     dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
1841     ubifs_assert(c, inode->i_nlink > 0);
1842     ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
1843 
1844     len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
1845     len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
1846     aligned_len1 = ALIGN(len1, 8);
1847     aligned_len = aligned_len1 + ALIGN(len2, 8);
1848 
1849     aligned_len += ubifs_auth_node_sz(c);
1850 
1851     ino = kzalloc(aligned_len, GFP_NOFS);
1852     if (!ino)
1853         return -ENOMEM;
1854 
1855     /* Make reservation before allocating sequence numbers */
1856     err = make_reservation(c, BASEHD, aligned_len);
1857     if (err)
1858         goto out_free;
1859 
1860     pack_inode(c, ino, host, 0);
1861     err = ubifs_node_calc_hash(c, ino, hash_host);
1862     if (err)
1863         goto out_release;
1864     pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1865     err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
1866     if (err)
1867         goto out_release;
1868 
1869     err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
1870     if (!sync && !err) {
1871         struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1872 
1873         ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
1874         ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
1875     }
1876     release_head(c, BASEHD);
1877     if (err)
1878         goto out_ro;
1879 
1880     ubifs_add_auth_dirt(c, lnum);
1881 
1882     ino_key_init(c, &key, host->i_ino);
1883     err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
1884     if (err)
1885         goto out_ro;
1886 
1887     ino_key_init(c, &key, inode->i_ino);
1888     err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
1889     if (err)
1890         goto out_ro;
1891 
1892     finish_reservation(c);
1893     spin_lock(&host_ui->ui_lock);
1894     host_ui->synced_i_size = host_ui->ui_size;
1895     spin_unlock(&host_ui->ui_lock);
1896     mark_inode_clean(c, host_ui);
1897     kfree(ino);
1898     return 0;
1899 
1900 out_release:
1901     release_head(c, BASEHD);
1902 out_ro:
1903     ubifs_ro_mode(c, err);
1904     finish_reservation(c);
1905 out_free:
1906     kfree(ino);
1907     return err;
1908 }
1909