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