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