2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This file implements UBIFS journal.
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.
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.
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
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
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).
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
63 #if defined(FEATURE_UBIFS_PERF_INDEX)
64 #include <linux/xlog.h>
65 #include <asm/div64.h>
67 #define PRT_TIME_PERIOD 500000000
68 #define PRT_TIME_EXPIRE 5000000000
71 static int ubifs_perf_init
= 0;
74 unsigned long long tag_t1
[ID_CNT
];
75 unsigned long long usage
[ID_CNT
];
76 unsigned int count
[ID_CNT
];
77 unsigned int size
[ID_CNT
];
79 static struct ubifs_perf org_write
, comp_write
, low_write
, low_read
;
81 static void g_var_clear(struct ubifs_perf
*perf
, unsigned int idx
)
88 static void g_var_init(void)
91 for(i
=0;i
<ID_CNT
;i
++) {
93 org_write
.tag_t1
[i
] = 0;
94 g_var_clear(&org_write
, i
);
95 comp_write
.pid
[i
] = 0;
96 comp_write
.tag_t1
[i
] = 0;
97 g_var_clear(&comp_write
, i
);
99 low_write
.tag_t1
[i
] = 0;
100 g_var_clear(&low_write
, i
);
102 low_read
.tag_t1
[i
] = 0;
103 g_var_clear(&low_read
, i
);
107 static void ubifs_pref_output(struct ubifs_perf
*perf
, int idx
)
109 do_div(perf
->usage
[idx
], 1000000);
110 if(perf
->usage
[idx
]) {
111 unsigned int perf_meter
= (perf
->size
[idx
])/((unsigned int)perf
->usage
[idx
]); //kb/s
112 if(perf
== &org_write
) {
113 unsigned int comp_perf_meter
= (comp_write
.size
[idx
])/((unsigned int)perf
->usage
[idx
]); //kb/s
114 xlog_printk(ANDROID_LOG_DEBUG
, "UBIFS_TAG", "[%d] pid:%4d WP:[%5d/%4d]kB/s, size:[%d/%d]bytes, time:%lld ms\n",
115 idx
, perf
->pid
[idx
], perf_meter
, comp_perf_meter
, perf
->size
[idx
], comp_write
.size
[idx
], perf
->usage
[idx
]);
116 } else if(perf
== &low_write
) {
117 xlog_printk(ANDROID_LOG_DEBUG
, "UBIFS_TAG", "[%d] pid:%4d LWP=%5d kB/s, size: %d bytes, time:%lld ms\n",
118 idx
, perf
->pid
[idx
], perf_meter
, perf
->size
[idx
], perf
->usage
[idx
]);
120 } else if(perf
== &low_read
) {
121 xlog_printk(ANDROID_LOG_DEBUG
, "UBIFS_TAG", "[%d] pid:%4d LRP=%5d kB/s, size: %d bytes, time:%lld ms\n",
122 idx
, perf
->pid
[idx
], perf_meter
, perf
->size
[idx
], perf
->usage
[idx
]);
126 static unsigned int find_ubifs_index(struct ubifs_perf
*perf
)
132 unsigned long long t_period
=0;
133 unsigned long long time1
;
136 pid
= task_pid_nr(current
);
142 if(ubifs_pid
[0] == 0)
148 for(i
=0;i
<ID_CNT
;i
++)
150 if(pid
== ubifs_pid
[i
])
155 if (ubifs_pid
[i
] == 0)
164 for(i
=0;i
<ID_CNT
;i
++)
166 t_period
= time1
- ubifs_tag_t1
[i
];
167 if(t_period
>= (unsigned long long )PRT_TIME_EXPIRE
)
169 ubifs_pref_output(i
);
176 for(i
=0;i
<ID_CNT
;i
++)
178 if(pid
== ubifs_pid
[i
])
183 if (ubifs_pid
[i
] == 0)
192 ubifs_pid
[i
-1] = pid
;
193 ubifs_tag_t1
[i
-1] = 0;
201 void ubifs_perf_show(struct ubifs_perf
*perf
)
203 unsigned long long t_period
=0;
204 int idx
=find_ubifs_index(perf
);
205 unsigned long long time1
= sched_clock();
207 if(perf
->tag_t1
[idx
]==0)
208 perf
->tag_t1
[idx
] = time1
;
209 t_period
= time1
- perf
->tag_t1
[idx
];
210 if(t_period
>= (unsigned long long )PRT_TIME_PERIOD
)
212 ubifs_pref_output(perf
, idx
);
213 perf
->tag_t1
[idx
]=time1
;
214 g_var_clear(perf
, idx
);
215 if(perf
== &org_write
) {
216 g_var_clear(&comp_write
, idx
);
220 int ubifs_perf_count(struct ubifs_perf
*perf
, unsigned long long usage
, unsigned int len
, int group_idx
)
223 if(ubifs_perf_init
== 0) {
227 if(group_idx
== -1) {
228 idx
=find_ubifs_index(perf
);
232 perf
->usage
[idx
] += usage
;
234 perf
->size
[idx
] += len
;
237 void ubifs_perf_wcount(unsigned long long usage
, unsigned int len
, unsigned int comp_len
)
240 idx
= ubifs_perf_count(&org_write
, usage
, len
, -1);
241 ubifs_perf_count(&comp_write
, usage
, comp_len
, idx
);
242 ubifs_perf_show(&org_write
);
245 void ubifs_perf_lwcount(unsigned long long usage
, unsigned int len
)
247 ubifs_perf_count(&low_write
, usage
, len
, -1);
248 ubifs_perf_show(&low_write
);
251 void ubifs_perf_lrcount(unsigned long long usage
, unsigned int len
)
253 ubifs_perf_count(&low_read
, usage
, len
, -1);
254 ubifs_perf_show(&low_read
);
259 * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
260 * @ino: the inode to zero out
262 static inline void zero_ino_node_unused(struct ubifs_ino_node
*ino
)
264 memset(ino
->padding1
, 0, 4);
265 memset(ino
->padding2
, 0, 26);
269 * zero_dent_node_unused - zero out unused fields of an on-flash directory
271 * @dent: the directory entry to zero out
273 static inline void zero_dent_node_unused(struct ubifs_dent_node
*dent
)
276 memset(dent
->padding2
, 0, 4);
280 * zero_data_node_unused - zero out unused fields of an on-flash data node.
281 * @data: the data node to zero out
283 static inline void zero_data_node_unused(struct ubifs_data_node
*data
)
285 memset(data
->padding
, 0, 2);
289 * zero_trun_node_unused - zero out unused fields of an on-flash truncation
291 * @trun: the truncation node to zero out
293 static inline void zero_trun_node_unused(struct ubifs_trun_node
*trun
)
295 memset(trun
->padding
, 0, 12);
299 * reserve_space - reserve space in the journal.
300 * @c: UBIFS file-system description object
301 * @jhead: journal head number
304 * This function reserves space in journal head @head. If the reservation
305 * succeeded, the journal head stays locked and later has to be unlocked using
306 * 'release_head()'. 'write_node()' and 'write_head()' functions also unlock
307 * it. Returns zero in case of success, %-EAGAIN if commit has to be done, and
308 * other negative error codes in case of other failures.
310 static int reserve_space(struct ubifs_info
*c
, int jhead
, int len
)
312 int err
= 0, err1
, retries
= 0, avail
, lnum
, offs
, squeeze
;
313 struct ubifs_wbuf
*wbuf
= &c
->jheads
[jhead
].wbuf
;
316 * Typically, the base head has smaller nodes written to it, so it is
317 * better to try to allocate space at the ends of eraseblocks. This is
318 * what the squeeze parameter does.
320 ubifs_assert(!c
->ro_media
&& !c
->ro_mount
);
321 squeeze
= (jhead
== BASEHD
);
323 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
330 avail
= c
->leb_size
- wbuf
->offs
- wbuf
->used
;
331 if (wbuf
->lnum
!= -1 && avail
>= len
)
335 * Write buffer wasn't seek'ed or there is no enough space - look for an
336 * LEB with some empty space.
338 lnum
= ubifs_find_free_space(c
, len
, &offs
, squeeze
);
347 * No free space, we have to run garbage collector to make
348 * some. But the write-buffer mutex has to be unlocked because
351 dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead
));
352 mutex_unlock(&wbuf
->io_mutex
);
354 lnum
= ubifs_garbage_collect(c
, 0);
361 * GC could not make a free LEB. But someone else may
362 * have allocated new bud for this journal head,
363 * because we dropped @wbuf->io_mutex, so try once
366 dbg_jnl("GC couldn't make a free LEB for jhead %s",
369 dbg_jnl("retry (%d)", retries
);
373 dbg_jnl("return -ENOSPC");
377 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
378 dbg_jnl("got LEB %d for jhead %s", lnum
, dbg_jhead(jhead
));
379 avail
= c
->leb_size
- wbuf
->offs
- wbuf
->used
;
381 if (wbuf
->lnum
!= -1 && avail
>= len
) {
383 * Someone else has switched the journal head and we have
384 * enough space now. This happens when more than one process is
385 * trying to write to the same journal head at the same time.
387 dbg_jnl("return LEB %d back, already have LEB %d:%d",
388 lnum
, wbuf
->lnum
, wbuf
->offs
+ wbuf
->used
);
389 err
= ubifs_return_leb(c
, lnum
);
399 * Make sure we synchronize the write-buffer before we add the new bud
400 * to the log. Otherwise we may have a power cut after the log
401 * reference node for the last bud (@lnum) is written but before the
402 * write-buffer data are written to the next-to-last bud
403 * (@wbuf->lnum). And the effect would be that the recovery would see
404 * that there is corruption in the next-to-last bud.
406 err
= ubifs_wbuf_sync_nolock(wbuf
);
409 err
= ubifs_add_bud_to_log(c
, jhead
, lnum
, offs
);
412 err
= ubifs_wbuf_seek_nolock(wbuf
, lnum
, offs
);
419 mutex_unlock(&wbuf
->io_mutex
);
423 /* An error occurred and the LEB has to be returned to lprops */
424 ubifs_assert(err
< 0);
425 err1
= ubifs_return_leb(c
, lnum
);
426 if (err1
&& err
== -EAGAIN
)
428 * Return original error code only if it is not %-EAGAIN,
429 * which is not really an error. Otherwise, return the error
430 * code of 'ubifs_return_leb()'.
433 mutex_unlock(&wbuf
->io_mutex
);
438 * write_node - write node to a journal head.
439 * @c: UBIFS file-system description object
440 * @jhead: journal head
441 * @node: node to write
443 * @lnum: LEB number written is returned here
444 * @offs: offset written is returned here
446 * This function writes a node to reserved space of journal head @jhead.
447 * Returns zero in case of success and a negative error code in case of
450 static int write_node(struct ubifs_info
*c
, int jhead
, void *node
, int len
,
451 int *lnum
, int *offs
)
453 struct ubifs_wbuf
*wbuf
= &c
->jheads
[jhead
].wbuf
;
455 ubifs_assert(jhead
!= GCHD
);
457 *lnum
= c
->jheads
[jhead
].wbuf
.lnum
;
458 *offs
= c
->jheads
[jhead
].wbuf
.offs
+ c
->jheads
[jhead
].wbuf
.used
;
460 dbg_jnl("jhead %s, LEB %d:%d, len %d",
461 dbg_jhead(jhead
), *lnum
, *offs
, len
);
462 ubifs_prepare_node(c
, node
, len
, 0);
464 return ubifs_wbuf_write_nolock(wbuf
, node
, len
);
468 * write_head - write data to a journal head.
469 * @c: UBIFS file-system description object
470 * @jhead: journal head
471 * @buf: buffer to write
472 * @len: length to write
473 * @lnum: LEB number written is returned here
474 * @offs: offset written is returned here
475 * @sync: non-zero if the write-buffer has to by synchronized
477 * This function is the same as 'write_node()' but it does not assume the
478 * buffer it is writing is a node, so it does not prepare it (which means
479 * initializing common header and calculating CRC).
481 static int write_head(struct ubifs_info
*c
, int jhead
, void *buf
, int len
,
482 int *lnum
, int *offs
, int sync
)
485 struct ubifs_wbuf
*wbuf
= &c
->jheads
[jhead
].wbuf
;
487 ubifs_assert(jhead
!= GCHD
);
489 *lnum
= c
->jheads
[jhead
].wbuf
.lnum
;
490 *offs
= c
->jheads
[jhead
].wbuf
.offs
+ c
->jheads
[jhead
].wbuf
.used
;
491 dbg_jnl("jhead %s, LEB %d:%d, len %d",
492 dbg_jhead(jhead
), *lnum
, *offs
, len
);
494 err
= ubifs_wbuf_write_nolock(wbuf
, buf
, len
);
498 err
= ubifs_wbuf_sync_nolock(wbuf
);
503 * make_reservation - reserve journal space.
504 * @c: UBIFS file-system description object
505 * @jhead: journal head
506 * @len: how many bytes to reserve
508 * This function makes space reservation in journal head @jhead. The function
509 * takes the commit lock and locks the journal head, and the caller has to
510 * unlock the head and finish the reservation with 'finish_reservation()'.
511 * Returns zero in case of success and a negative error code in case of
514 * Note, the journal head may be unlocked as soon as the data is written, while
515 * the commit lock has to be released after the data has been added to the
518 static int make_reservation(struct ubifs_info
*c
, int jhead
, int len
)
520 int err
, cmt_retries
= 0, nospc_retries
= 0;
523 down_read(&c
->commit_sem
);
524 err
= reserve_space(c
, jhead
, len
);
527 up_read(&c
->commit_sem
);
529 if (err
== -ENOSPC
) {
531 * GC could not make any progress. We should try to commit
532 * once because it could make some dirty space and GC would
533 * make progress, so make the error -EAGAIN so that the below
534 * will commit and re-try.
536 if (nospc_retries
++ < 2) {
537 dbg_jnl("no space, retry");
542 * This means that the budgeting is incorrect. We always have
543 * to be able to write to the media, because all operations are
544 * budgeted. Deletions are not budgeted, though, but we reserve
545 * an extra LEB for them.
553 * -EAGAIN means that the journal is full or too large, or the above
554 * code wants to do one commit. Do this and re-try.
556 if (cmt_retries
> 128) {
558 * This should not happen unless the journal size limitations
561 ubifs_err("stuck in space allocation");
564 } else if (cmt_retries
> 32)
565 ubifs_warn("too many space allocation re-tries (%d)",
568 dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
572 err
= ubifs_run_commit(c
);
578 ubifs_err("cannot reserve %d bytes in jhead %d, error %d",
580 if (err
== -ENOSPC
) {
581 /* This are some budgeting problems, print useful information */
582 down_write(&c
->commit_sem
);
584 ubifs_dump_budg(c
, &c
->bi
);
585 ubifs_dump_lprops(c
);
586 cmt_retries
= dbg_check_lprops(c
);
587 up_write(&c
->commit_sem
);
593 * release_head - release a journal head.
594 * @c: UBIFS file-system description object
595 * @jhead: journal head
597 * This function releases journal head @jhead which was locked by
598 * the 'make_reservation()' function. It has to be called after each successful
599 * 'make_reservation()' invocation.
601 static inline void release_head(struct ubifs_info
*c
, int jhead
)
603 mutex_unlock(&c
->jheads
[jhead
].wbuf
.io_mutex
);
607 * finish_reservation - finish a reservation.
608 * @c: UBIFS file-system description object
610 * This function finishes journal space reservation. It must be called after
611 * 'make_reservation()'.
613 static void finish_reservation(struct ubifs_info
*c
)
615 up_read(&c
->commit_sem
);
619 * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
622 static int get_dent_type(int mode
)
624 switch (mode
& S_IFMT
) {
626 return UBIFS_ITYPE_REG
;
628 return UBIFS_ITYPE_DIR
;
630 return UBIFS_ITYPE_LNK
;
632 return UBIFS_ITYPE_BLK
;
634 return UBIFS_ITYPE_CHR
;
636 return UBIFS_ITYPE_FIFO
;
638 return UBIFS_ITYPE_SOCK
;
646 * pack_inode - pack an inode node.
647 * @c: UBIFS file-system description object
648 * @ino: buffer in which to pack inode node
649 * @inode: inode to pack
650 * @last: indicates the last node of the group
652 static void pack_inode(struct ubifs_info
*c
, struct ubifs_ino_node
*ino
,
653 const struct inode
*inode
, int last
)
655 int data_len
= 0, last_reference
= !inode
->i_nlink
;
656 struct ubifs_inode
*ui
= ubifs_inode(inode
);
658 ino
->ch
.node_type
= UBIFS_INO_NODE
;
659 ino_key_init_flash(c
, &ino
->key
, inode
->i_ino
);
660 ino
->creat_sqnum
= cpu_to_le64(ui
->creat_sqnum
);
661 ino
->atime_sec
= cpu_to_le64(inode
->i_atime
.tv_sec
);
662 ino
->atime_nsec
= cpu_to_le32(inode
->i_atime
.tv_nsec
);
663 ino
->ctime_sec
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
664 ino
->ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
665 ino
->mtime_sec
= cpu_to_le64(inode
->i_mtime
.tv_sec
);
666 ino
->mtime_nsec
= cpu_to_le32(inode
->i_mtime
.tv_nsec
);
667 ino
->uid
= cpu_to_le32(i_uid_read(inode
));
668 ino
->gid
= cpu_to_le32(i_gid_read(inode
));
669 ino
->mode
= cpu_to_le32(inode
->i_mode
);
670 ino
->flags
= cpu_to_le32(ui
->flags
);
671 ino
->size
= cpu_to_le64(ui
->ui_size
);
672 ino
->nlink
= cpu_to_le32(inode
->i_nlink
);
673 ino
->compr_type
= cpu_to_le16(ui
->compr_type
);
674 ino
->data_len
= cpu_to_le32(ui
->data_len
);
675 ino
->xattr_cnt
= cpu_to_le32(ui
->xattr_cnt
);
676 ino
->xattr_size
= cpu_to_le32(ui
->xattr_size
);
677 ino
->xattr_names
= cpu_to_le32(ui
->xattr_names
);
678 zero_ino_node_unused(ino
);
681 * Drop the attached data if this is a deletion inode, the data is not
684 if (!last_reference
) {
685 memcpy(ino
->data
, ui
->data
, ui
->data_len
);
686 data_len
= ui
->data_len
;
689 ubifs_prep_grp_node(c
, ino
, UBIFS_INO_NODE_SZ
+ data_len
, last
);
693 * mark_inode_clean - mark UBIFS inode as clean.
694 * @c: UBIFS file-system description object
695 * @ui: UBIFS inode to mark as clean
697 * This helper function marks UBIFS inode @ui as clean by cleaning the
698 * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
699 * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
702 static void mark_inode_clean(struct ubifs_info
*c
, struct ubifs_inode
*ui
)
705 ubifs_release_dirty_inode_budget(c
, ui
);
710 * ubifs_jnl_update - update inode.
711 * @c: UBIFS file-system description object
712 * @dir: parent inode or host inode in case of extended attributes
713 * @nm: directory entry name
714 * @inode: inode to update
715 * @deletion: indicates a directory entry deletion i.e unlink or rmdir
716 * @xent: non-zero if the directory entry is an extended attribute entry
718 * This function updates an inode by writing a directory entry (or extended
719 * attribute entry), the inode itself, and the parent directory inode (or the
720 * host inode) to the journal.
722 * The function writes the host inode @dir last, which is important in case of
723 * extended attributes. Indeed, then we guarantee that if the host inode gets
724 * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
725 * the extended attribute inode gets flushed too. And this is exactly what the
726 * user expects - synchronizing the host inode synchronizes its extended
727 * attributes. Similarly, this guarantees that if @dir is synchronized, its
728 * directory entry corresponding to @nm gets synchronized too.
730 * If the inode (@inode) or the parent directory (@dir) are synchronous, this
731 * function synchronizes the write-buffer.
733 * This function marks the @dir and @inode inodes as clean and returns zero on
734 * success. In case of failure, a negative error code is returned.
736 int ubifs_jnl_update(struct ubifs_info
*c
, const struct inode
*dir
,
737 const struct qstr
*nm
, const struct inode
*inode
,
738 int deletion
, int xent
)
740 int err
, dlen
, ilen
, len
, lnum
, ino_offs
, dent_offs
;
741 int aligned_dlen
, aligned_ilen
, sync
= IS_DIRSYNC(dir
);
742 int last_reference
= !!(deletion
&& inode
->i_nlink
== 0);
743 struct ubifs_inode
*ui
= ubifs_inode(inode
);
744 struct ubifs_inode
*dir_ui
= ubifs_inode(dir
);
745 struct ubifs_dent_node
*dent
;
746 struct ubifs_ino_node
*ino
;
747 union ubifs_key dent_key
, ino_key
;
749 dbg_jnl("ino %lu, dent '%.*s', data len %d in dir ino %lu",
750 inode
->i_ino
, nm
->len
, nm
->name
, ui
->data_len
, dir
->i_ino
);
752 ubifs_assert(dir_ui
->data_len
== 0);
753 ubifs_assert(mutex_is_locked(&dir_ui
->ui_mutex
));
755 dlen
= UBIFS_DENT_NODE_SZ
+ nm
->len
+ 1;
756 ilen
= UBIFS_INO_NODE_SZ
;
759 * If the last reference to the inode is being deleted, then there is
760 * no need to attach and write inode data, it is being deleted anyway.
761 * And if the inode is being deleted, no need to synchronize
762 * write-buffer even if the inode is synchronous.
764 if (!last_reference
) {
765 ilen
+= ui
->data_len
;
766 sync
|= IS_SYNC(inode
);
769 aligned_dlen
= ALIGN(dlen
, 8);
770 aligned_ilen
= ALIGN(ilen
, 8);
771 /* Make sure to account for dir_ui+data_len in length calculation
772 * in case there is extended attribute.
774 len
= aligned_dlen
+ aligned_ilen
+
775 UBIFS_INO_NODE_SZ
+ dir_ui
->data_len
;
776 dent
= kmalloc(len
, GFP_NOFS
);
780 /* Make reservation before allocating sequence numbers */
781 err
= make_reservation(c
, BASEHD
, len
);
786 dent
->ch
.node_type
= UBIFS_DENT_NODE
;
787 dent_key_init(c
, &dent_key
, dir
->i_ino
, nm
);
789 dent
->ch
.node_type
= UBIFS_XENT_NODE
;
790 xent_key_init(c
, &dent_key
, dir
->i_ino
, nm
);
793 key_write(c
, &dent_key
, dent
->key
);
794 dent
->inum
= deletion
? 0 : cpu_to_le64(inode
->i_ino
);
795 dent
->type
= get_dent_type(inode
->i_mode
);
796 dent
->nlen
= cpu_to_le16(nm
->len
);
797 memcpy(dent
->name
, nm
->name
, nm
->len
);
798 dent
->name
[nm
->len
] = '\0';
799 zero_dent_node_unused(dent
);
800 ubifs_prep_grp_node(c
, dent
, dlen
, 0);
802 ino
= (void *)dent
+ aligned_dlen
;
803 pack_inode(c
, ino
, inode
, 0);
804 ino
= (void *)ino
+ aligned_ilen
;
805 pack_inode(c
, ino
, dir
, 1);
807 if (last_reference
) {
808 err
= ubifs_add_orphan(c
, inode
->i_ino
);
810 release_head(c
, BASEHD
);
813 ui
->del_cmtno
= c
->cmt_no
;
816 err
= write_head(c
, BASEHD
, dent
, len
, &lnum
, &dent_offs
, sync
);
820 struct ubifs_wbuf
*wbuf
= &c
->jheads
[BASEHD
].wbuf
;
822 ubifs_wbuf_add_ino_nolock(wbuf
, inode
->i_ino
);
823 ubifs_wbuf_add_ino_nolock(wbuf
, dir
->i_ino
);
825 release_head(c
, BASEHD
);
829 err
= ubifs_tnc_remove_nm(c
, &dent_key
, nm
);
832 err
= ubifs_add_dirt(c
, lnum
, dlen
);
834 err
= ubifs_tnc_add_nm(c
, &dent_key
, lnum
, dent_offs
, dlen
, nm
);
839 * Note, we do not remove the inode from TNC even if the last reference
840 * to it has just been deleted, because the inode may still be opened.
841 * Instead, the inode has been added to orphan lists and the orphan
842 * subsystem will take further care about it.
844 ino_key_init(c
, &ino_key
, inode
->i_ino
);
845 ino_offs
= dent_offs
+ aligned_dlen
;
846 err
= ubifs_tnc_add(c
, &ino_key
, lnum
, ino_offs
, ilen
);
850 ino_key_init(c
, &ino_key
, dir
->i_ino
);
851 ino_offs
+= aligned_ilen
;
852 err
= ubifs_tnc_add(c
, &ino_key
, lnum
, ino_offs
,
853 UBIFS_INO_NODE_SZ
+ dir_ui
->data_len
);
857 finish_reservation(c
);
858 spin_lock(&ui
->ui_lock
);
859 ui
->synced_i_size
= ui
->ui_size
;
860 spin_unlock(&ui
->ui_lock
);
861 mark_inode_clean(c
, ui
);
862 mark_inode_clean(c
, dir_ui
);
866 finish_reservation(c
);
872 release_head(c
, BASEHD
);
875 ubifs_ro_mode(c
, err
);
877 ubifs_delete_orphan(c
, inode
->i_ino
);
878 finish_reservation(c
);
883 * ubifs_jnl_write_data - write a data node to the journal.
884 * @c: UBIFS file-system description object
885 * @inode: inode the data node belongs to
887 * @buf: buffer to write
888 * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
890 * This function writes a data node to the journal. Returns %0 if the data node
891 * was successfully written, and a negative error code in case of failure.
893 int ubifs_jnl_write_data(struct ubifs_info
*c
, const struct inode
*inode
,
894 const union ubifs_key
*key
, const void *buf
, int len
)
896 struct ubifs_data_node
*data
;
897 int err
, lnum
, offs
, compr_type
, out_len
;
898 int dlen
= COMPRESSED_DATA_NODE_BUF_SZ
, allocated
= 1;
899 struct ubifs_inode
*ui
= ubifs_inode(inode
);
900 #if defined(FEATURE_UBIFS_PERF_INDEX)
901 unsigned long long time1
= sched_clock();
904 dbg_jnlk(key
, "ino %lu, blk %u, len %d, key ",
905 (unsigned long)key_inum(c
, key
), key_block(c
, key
), len
);
906 ubifs_assert(len
<= UBIFS_BLOCK_SIZE
);
908 //data = kmalloc(dlen, GFP_NOFS | __GFP_NOWARN);
912 * Fall-back to the write reserve buffer. Note, we might be
913 * currently on the memory reclaim path, when the kernel is
914 * trying to free some memory by writing out dirty pages. The
915 * write reserve buffer helps us to guarantee that we are
916 * always able to write the data.
919 mutex_lock(&c
->write_reserve_mutex
);
920 data
= c
->write_reserve_buf
;
923 data
->ch
.node_type
= UBIFS_DATA_NODE
;
924 key_write(c
, key
, &data
->key
);
925 data
->size
= cpu_to_le32(len
);
926 zero_data_node_unused(data
);
928 if (!(ui
->flags
& UBIFS_COMPR_FL
))
929 /* Compression is disabled for this inode */
930 compr_type
= UBIFS_COMPR_NONE
;
932 compr_type
= ui
->compr_type
;
934 out_len
= dlen
- UBIFS_DATA_NODE_SZ
;
935 c
->host_wcount
+= len
;
936 ubifs_compress(buf
, len
, &data
->data
, &out_len
, &compr_type
);
937 ubifs_assert(out_len
<= UBIFS_BLOCK_SIZE
);
939 dlen
= UBIFS_DATA_NODE_SZ
+ out_len
;
940 data
->compr_type
= cpu_to_le16(compr_type
);
942 /* Make reservation before allocating sequence numbers */
943 err
= make_reservation(c
, DATAHD
, dlen
);
947 err
= write_node(c
, DATAHD
, data
, dlen
, &lnum
, &offs
);
950 ubifs_wbuf_add_ino_nolock(&c
->jheads
[DATAHD
].wbuf
, key_inum(c
, key
));
951 release_head(c
, DATAHD
);
953 err
= ubifs_tnc_add(c
, key
, lnum
, offs
, dlen
);
957 finish_reservation(c
);
959 mutex_unlock(&c
->write_reserve_mutex
);
962 #if defined(FEATURE_UBIFS_PERF_INDEX)
963 ubifs_perf_wcount(sched_clock() - time1
, len
, dlen
);
968 release_head(c
, DATAHD
);
970 ubifs_ro_mode(c
, err
);
971 finish_reservation(c
);
974 mutex_unlock(&c
->write_reserve_mutex
);
981 * ubifs_jnl_write_inode - flush inode to the journal.
982 * @c: UBIFS file-system description object
983 * @inode: inode to flush
985 * This function writes inode @inode to the journal. If the inode is
986 * synchronous, it also synchronizes the write-buffer. Returns zero in case of
987 * success and a negative error code in case of failure.
989 int ubifs_jnl_write_inode(struct ubifs_info
*c
, const struct inode
*inode
)
992 struct ubifs_ino_node
*ino
;
993 struct ubifs_inode
*ui
= ubifs_inode(inode
);
994 int sync
= 0, len
= UBIFS_INO_NODE_SZ
, last_reference
= !inode
->i_nlink
;
996 dbg_jnl("ino %lu, nlink %u", inode
->i_ino
, inode
->i_nlink
);
999 * If the inode is being deleted, do not write the attached data. No
1000 * need to synchronize the write-buffer either.
1002 if (!last_reference
) {
1003 len
+= ui
->data_len
;
1004 sync
= IS_SYNC(inode
);
1006 ino
= kmalloc(len
, GFP_NOFS
);
1010 /* Make reservation before allocating sequence numbers */
1011 err
= make_reservation(c
, BASEHD
, len
);
1015 pack_inode(c
, ino
, inode
, 1);
1016 err
= write_head(c
, BASEHD
, ino
, len
, &lnum
, &offs
, sync
);
1020 ubifs_wbuf_add_ino_nolock(&c
->jheads
[BASEHD
].wbuf
,
1022 release_head(c
, BASEHD
);
1024 if (last_reference
) {
1025 err
= ubifs_tnc_remove_ino(c
, inode
->i_ino
);
1028 ubifs_delete_orphan(c
, inode
->i_ino
);
1029 err
= ubifs_add_dirt(c
, lnum
, len
);
1031 union ubifs_key key
;
1033 ino_key_init(c
, &key
, inode
->i_ino
);
1034 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, len
);
1039 finish_reservation(c
);
1040 spin_lock(&ui
->ui_lock
);
1041 ui
->synced_i_size
= ui
->ui_size
;
1042 spin_unlock(&ui
->ui_lock
);
1047 release_head(c
, BASEHD
);
1049 ubifs_ro_mode(c
, err
);
1050 finish_reservation(c
);
1057 * ubifs_jnl_delete_inode - delete an inode.
1058 * @c: UBIFS file-system description object
1059 * @inode: inode to delete
1061 * This function deletes inode @inode which includes removing it from orphans,
1062 * deleting it from TNC and, in some cases, writing a deletion inode to the
1065 * When regular file inodes are unlinked or a directory inode is removed, the
1066 * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
1067 * direntry to the media, and adds the inode to orphans. After this, when the
1068 * last reference to this inode has been dropped, this function is called. In
1069 * general, it has to write one more deletion inode to the media, because if
1070 * a commit happened between 'ubifs_jnl_update()' and
1071 * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
1072 * anymore, and in fact it might not be on the flash anymore, because it might
1073 * have been garbage-collected already. And for optimization reasons UBIFS does
1074 * not read the orphan area if it has been unmounted cleanly, so it would have
1075 * no indication in the journal that there is a deleted inode which has to be
1078 * However, if there was no commit between 'ubifs_jnl_update()' and
1079 * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
1080 * inode to the media for the second time. And this is quite a typical case.
1082 * This function returns zero in case of success and a negative error code in
1085 int ubifs_jnl_delete_inode(struct ubifs_info
*c
, const struct inode
*inode
)
1088 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1090 ubifs_assert(inode
->i_nlink
== 0);
1092 if (ui
->del_cmtno
!= c
->cmt_no
)
1093 /* A commit happened for sure */
1094 return ubifs_jnl_write_inode(c
, inode
);
1096 down_read(&c
->commit_sem
);
1098 * Check commit number again, because the first test has been done
1099 * without @c->commit_sem, so a commit might have happened.
1101 if (ui
->del_cmtno
!= c
->cmt_no
) {
1102 up_read(&c
->commit_sem
);
1103 return ubifs_jnl_write_inode(c
, inode
);
1106 err
= ubifs_tnc_remove_ino(c
, inode
->i_ino
);
1108 ubifs_ro_mode(c
, err
);
1110 ubifs_delete_orphan(c
, inode
->i_ino
);
1111 up_read(&c
->commit_sem
);
1116 * ubifs_jnl_rename - rename a directory entry.
1117 * @c: UBIFS file-system description object
1118 * @old_dir: parent inode of directory entry to rename
1119 * @old_dentry: directory entry to rename
1120 * @new_dir: parent inode of directory entry to rename
1121 * @new_dentry: new directory entry (or directory entry to replace)
1122 * @sync: non-zero if the write-buffer has to be synchronized
1124 * This function implements the re-name operation which may involve writing up
1125 * to 3 inodes and 2 directory entries. It marks the written inodes as clean
1126 * and returns zero on success. In case of failure, a negative error code is
1129 int ubifs_jnl_rename(struct ubifs_info
*c
, const struct inode
*old_dir
,
1130 const struct dentry
*old_dentry
,
1131 const struct inode
*new_dir
,
1132 const struct dentry
*new_dentry
, int sync
)
1135 union ubifs_key key
;
1136 struct ubifs_dent_node
*dent
, *dent2
;
1137 int err
, dlen1
, dlen2
, ilen
, lnum
, offs
, len
;
1138 const struct inode
*old_inode
= old_dentry
->d_inode
;
1139 const struct inode
*new_inode
= new_dentry
->d_inode
;
1140 int aligned_dlen1
, aligned_dlen2
, plen
= UBIFS_INO_NODE_SZ
;
1141 int last_reference
= !!(new_inode
&& new_inode
->i_nlink
== 0);
1142 int move
= (old_dir
!= new_dir
);
1143 struct ubifs_inode
*uninitialized_var(new_ui
);
1145 dbg_jnl("dent '%.*s' in dir ino %lu to dent '%.*s' in dir ino %lu",
1146 old_dentry
->d_name
.len
, old_dentry
->d_name
.name
,
1147 old_dir
->i_ino
, new_dentry
->d_name
.len
,
1148 new_dentry
->d_name
.name
, new_dir
->i_ino
);
1149 ubifs_assert(ubifs_inode(old_dir
)->data_len
== 0);
1150 ubifs_assert(ubifs_inode(new_dir
)->data_len
== 0);
1151 ubifs_assert(mutex_is_locked(&ubifs_inode(old_dir
)->ui_mutex
));
1152 ubifs_assert(mutex_is_locked(&ubifs_inode(new_dir
)->ui_mutex
));
1154 dlen1
= UBIFS_DENT_NODE_SZ
+ new_dentry
->d_name
.len
+ 1;
1155 dlen2
= UBIFS_DENT_NODE_SZ
+ old_dentry
->d_name
.len
+ 1;
1157 new_ui
= ubifs_inode(new_inode
);
1158 ubifs_assert(mutex_is_locked(&new_ui
->ui_mutex
));
1159 ilen
= UBIFS_INO_NODE_SZ
;
1160 if (!last_reference
)
1161 ilen
+= new_ui
->data_len
;
1165 aligned_dlen1
= ALIGN(dlen1
, 8);
1166 aligned_dlen2
= ALIGN(dlen2
, 8);
1167 len
= aligned_dlen1
+ aligned_dlen2
+ ALIGN(ilen
, 8) + ALIGN(plen
, 8);
1168 if (old_dir
!= new_dir
)
1170 dent
= kmalloc(len
, GFP_NOFS
);
1174 /* Make reservation before allocating sequence numbers */
1175 err
= make_reservation(c
, BASEHD
, len
);
1180 dent
->ch
.node_type
= UBIFS_DENT_NODE
;
1181 dent_key_init_flash(c
, &dent
->key
, new_dir
->i_ino
, &new_dentry
->d_name
);
1182 dent
->inum
= cpu_to_le64(old_inode
->i_ino
);
1183 dent
->type
= get_dent_type(old_inode
->i_mode
);
1184 dent
->nlen
= cpu_to_le16(new_dentry
->d_name
.len
);
1185 memcpy(dent
->name
, new_dentry
->d_name
.name
, new_dentry
->d_name
.len
);
1186 dent
->name
[new_dentry
->d_name
.len
] = '\0';
1187 zero_dent_node_unused(dent
);
1188 ubifs_prep_grp_node(c
, dent
, dlen1
, 0);
1190 /* Make deletion dent */
1191 dent2
= (void *)dent
+ aligned_dlen1
;
1192 dent2
->ch
.node_type
= UBIFS_DENT_NODE
;
1193 dent_key_init_flash(c
, &dent2
->key
, old_dir
->i_ino
,
1194 &old_dentry
->d_name
);
1196 dent2
->type
= DT_UNKNOWN
;
1197 dent2
->nlen
= cpu_to_le16(old_dentry
->d_name
.len
);
1198 memcpy(dent2
->name
, old_dentry
->d_name
.name
, old_dentry
->d_name
.len
);
1199 dent2
->name
[old_dentry
->d_name
.len
] = '\0';
1200 zero_dent_node_unused(dent2
);
1201 ubifs_prep_grp_node(c
, dent2
, dlen2
, 0);
1203 p
= (void *)dent2
+ aligned_dlen2
;
1205 pack_inode(c
, p
, new_inode
, 0);
1206 p
+= ALIGN(ilen
, 8);
1210 pack_inode(c
, p
, old_dir
, 1);
1212 pack_inode(c
, p
, old_dir
, 0);
1213 p
+= ALIGN(plen
, 8);
1214 pack_inode(c
, p
, new_dir
, 1);
1217 if (last_reference
) {
1218 err
= ubifs_add_orphan(c
, new_inode
->i_ino
);
1220 release_head(c
, BASEHD
);
1223 new_ui
->del_cmtno
= c
->cmt_no
;
1226 err
= write_head(c
, BASEHD
, dent
, len
, &lnum
, &offs
, sync
);
1230 struct ubifs_wbuf
*wbuf
= &c
->jheads
[BASEHD
].wbuf
;
1232 ubifs_wbuf_add_ino_nolock(wbuf
, new_dir
->i_ino
);
1233 ubifs_wbuf_add_ino_nolock(wbuf
, old_dir
->i_ino
);
1235 ubifs_wbuf_add_ino_nolock(&c
->jheads
[BASEHD
].wbuf
,
1238 release_head(c
, BASEHD
);
1240 dent_key_init(c
, &key
, new_dir
->i_ino
, &new_dentry
->d_name
);
1241 err
= ubifs_tnc_add_nm(c
, &key
, lnum
, offs
, dlen1
, &new_dentry
->d_name
);
1245 err
= ubifs_add_dirt(c
, lnum
, dlen2
);
1249 dent_key_init(c
, &key
, old_dir
->i_ino
, &old_dentry
->d_name
);
1250 err
= ubifs_tnc_remove_nm(c
, &key
, &old_dentry
->d_name
);
1254 offs
+= aligned_dlen1
+ aligned_dlen2
;
1256 ino_key_init(c
, &key
, new_inode
->i_ino
);
1257 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, ilen
);
1260 offs
+= ALIGN(ilen
, 8);
1263 ino_key_init(c
, &key
, old_dir
->i_ino
);
1264 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, plen
);
1268 if (old_dir
!= new_dir
) {
1269 offs
+= ALIGN(plen
, 8);
1270 ino_key_init(c
, &key
, new_dir
->i_ino
);
1271 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, plen
);
1276 finish_reservation(c
);
1278 mark_inode_clean(c
, new_ui
);
1279 spin_lock(&new_ui
->ui_lock
);
1280 new_ui
->synced_i_size
= new_ui
->ui_size
;
1281 spin_unlock(&new_ui
->ui_lock
);
1283 mark_inode_clean(c
, ubifs_inode(old_dir
));
1285 mark_inode_clean(c
, ubifs_inode(new_dir
));
1290 release_head(c
, BASEHD
);
1292 ubifs_ro_mode(c
, err
);
1294 ubifs_delete_orphan(c
, new_inode
->i_ino
);
1296 finish_reservation(c
);
1303 * recomp_data_node - re-compress a truncated data node.
1304 * @dn: data node to re-compress
1305 * @new_len: new length
1307 * This function is used when an inode is truncated and the last data node of
1308 * the inode has to be re-compressed and re-written.
1310 static int recomp_data_node(struct ubifs_data_node
*dn
, int *new_len
)
1313 int err
, len
, compr_type
, out_len
;
1315 out_len
= le32_to_cpu(dn
->size
);
1316 buf
= kmalloc(out_len
* WORST_COMPR_FACTOR
, GFP_NOFS
);
1320 len
= le32_to_cpu(dn
->ch
.len
) - UBIFS_DATA_NODE_SZ
;
1321 compr_type
= le16_to_cpu(dn
->compr_type
);
1322 err
= ubifs_decompress(&dn
->data
, len
, buf
, &out_len
, compr_type
);
1326 ubifs_compress(buf
, *new_len
, &dn
->data
, &out_len
, &compr_type
);
1327 ubifs_assert(out_len
<= UBIFS_BLOCK_SIZE
);
1328 dn
->compr_type
= cpu_to_le16(compr_type
);
1329 dn
->size
= cpu_to_le32(*new_len
);
1330 *new_len
= UBIFS_DATA_NODE_SZ
+ out_len
;
1337 * ubifs_jnl_truncate - update the journal for a truncation.
1338 * @c: UBIFS file-system description object
1339 * @inode: inode to truncate
1340 * @old_size: old size
1341 * @new_size: new size
1343 * When the size of a file decreases due to truncation, a truncation node is
1344 * written, the journal tree is updated, and the last data block is re-written
1345 * if it has been affected. The inode is also updated in order to synchronize
1346 * the new inode size.
1348 * This function marks the inode as clean and returns zero on success. In case
1349 * of failure, a negative error code is returned.
1351 int ubifs_jnl_truncate(struct ubifs_info
*c
, const struct inode
*inode
,
1352 loff_t old_size
, loff_t new_size
)
1354 union ubifs_key key
, to_key
;
1355 struct ubifs_ino_node
*ino
;
1356 struct ubifs_trun_node
*trun
;
1357 struct ubifs_data_node
*uninitialized_var(dn
);
1358 int err
, dlen
, len
, lnum
, offs
, bit
, sz
, sync
= IS_SYNC(inode
);
1359 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1360 ino_t inum
= inode
->i_ino
;
1363 dbg_jnl("ino %lu, size %lld -> %lld",
1364 (unsigned long)inum
, old_size
, new_size
);
1365 ubifs_assert(!ui
->data_len
);
1366 ubifs_assert(S_ISREG(inode
->i_mode
));
1367 ubifs_assert(mutex_is_locked(&ui
->ui_mutex
));
1369 sz
= UBIFS_TRUN_NODE_SZ
+ UBIFS_INO_NODE_SZ
+
1370 UBIFS_MAX_DATA_NODE_SZ
* WORST_COMPR_FACTOR
;
1371 ino
= kmalloc(sz
, GFP_NOFS
);
1375 trun
= (void *)ino
+ UBIFS_INO_NODE_SZ
;
1376 trun
->ch
.node_type
= UBIFS_TRUN_NODE
;
1377 trun
->inum
= cpu_to_le32(inum
);
1378 trun
->old_size
= cpu_to_le64(old_size
);
1379 trun
->new_size
= cpu_to_le64(new_size
);
1380 zero_trun_node_unused(trun
);
1382 dlen
= new_size
& (UBIFS_BLOCK_SIZE
- 1);
1384 /* Get last data block so it can be truncated */
1385 dn
= (void *)trun
+ UBIFS_TRUN_NODE_SZ
;
1386 blk
= new_size
>> UBIFS_BLOCK_SHIFT
;
1387 data_key_init(c
, &key
, inum
, blk
);
1388 dbg_jnlk(&key
, "last block key ");
1389 err
= ubifs_tnc_lookup(c
, &key
, dn
);
1391 dlen
= 0; /* Not found (so it is a hole) */
1395 if (le32_to_cpu(dn
->size
) <= dlen
)
1396 dlen
= 0; /* Nothing to do */
1398 int compr_type
= le16_to_cpu(dn
->compr_type
);
1400 if (compr_type
!= UBIFS_COMPR_NONE
) {
1401 err
= recomp_data_node(dn
, &dlen
);
1405 dn
->size
= cpu_to_le32(dlen
);
1406 dlen
+= UBIFS_DATA_NODE_SZ
;
1408 zero_data_node_unused(dn
);
1413 /* Must make reservation before allocating sequence numbers */
1414 len
= UBIFS_TRUN_NODE_SZ
+ UBIFS_INO_NODE_SZ
;
1417 err
= make_reservation(c
, BASEHD
, len
);
1421 pack_inode(c
, ino
, inode
, 0);
1422 ubifs_prep_grp_node(c
, trun
, UBIFS_TRUN_NODE_SZ
, dlen
? 0 : 1);
1424 ubifs_prep_grp_node(c
, dn
, dlen
, 1);
1426 err
= write_head(c
, BASEHD
, ino
, len
, &lnum
, &offs
, sync
);
1430 ubifs_wbuf_add_ino_nolock(&c
->jheads
[BASEHD
].wbuf
, inum
);
1431 release_head(c
, BASEHD
);
1434 sz
= offs
+ UBIFS_INO_NODE_SZ
+ UBIFS_TRUN_NODE_SZ
;
1435 err
= ubifs_tnc_add(c
, &key
, lnum
, sz
, dlen
);
1440 ino_key_init(c
, &key
, inum
);
1441 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, UBIFS_INO_NODE_SZ
);
1445 err
= ubifs_add_dirt(c
, lnum
, UBIFS_TRUN_NODE_SZ
);
1449 bit
= new_size
& (UBIFS_BLOCK_SIZE
- 1);
1450 blk
= (new_size
>> UBIFS_BLOCK_SHIFT
) + (bit
? 1 : 0);
1451 data_key_init(c
, &key
, inum
, blk
);
1453 bit
= old_size
& (UBIFS_BLOCK_SIZE
- 1);
1454 blk
= (old_size
>> UBIFS_BLOCK_SHIFT
) - (bit
? 0 : 1);
1455 data_key_init(c
, &to_key
, inum
, blk
);
1457 err
= ubifs_tnc_remove_range(c
, &key
, &to_key
);
1461 finish_reservation(c
);
1462 spin_lock(&ui
->ui_lock
);
1463 ui
->synced_i_size
= ui
->ui_size
;
1464 spin_unlock(&ui
->ui_lock
);
1465 mark_inode_clean(c
, ui
);
1470 release_head(c
, BASEHD
);
1472 ubifs_ro_mode(c
, err
);
1473 finish_reservation(c
);
1481 * ubifs_jnl_delete_xattr - delete an extended attribute.
1482 * @c: UBIFS file-system description object
1484 * @inode: extended attribute inode
1485 * @nm: extended attribute entry name
1487 * This function delete an extended attribute which is very similar to
1488 * un-linking regular files - it writes a deletion xentry, a deletion inode and
1489 * updates the target inode. Returns zero in case of success and a negative
1490 * error code in case of failure.
1492 int ubifs_jnl_delete_xattr(struct ubifs_info
*c
, const struct inode
*host
,
1493 const struct inode
*inode
, const struct qstr
*nm
)
1495 int err
, xlen
, hlen
, len
, lnum
, xent_offs
, aligned_xlen
;
1496 struct ubifs_dent_node
*xent
;
1497 struct ubifs_ino_node
*ino
;
1498 union ubifs_key xent_key
, key1
, key2
;
1499 int sync
= IS_DIRSYNC(host
);
1500 struct ubifs_inode
*host_ui
= ubifs_inode(host
);
1502 dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
1503 host
->i_ino
, inode
->i_ino
, nm
->name
,
1504 ubifs_inode(inode
)->data_len
);
1505 ubifs_assert(inode
->i_nlink
== 0);
1506 ubifs_assert(mutex_is_locked(&host_ui
->ui_mutex
));
1509 * Since we are deleting the inode, we do not bother to attach any data
1510 * to it and assume its length is %UBIFS_INO_NODE_SZ.
1512 xlen
= UBIFS_DENT_NODE_SZ
+ nm
->len
+ 1;
1513 aligned_xlen
= ALIGN(xlen
, 8);
1514 hlen
= host_ui
->data_len
+ UBIFS_INO_NODE_SZ
;
1515 len
= aligned_xlen
+ UBIFS_INO_NODE_SZ
+ ALIGN(hlen
, 8);
1517 xent
= kmalloc(len
, GFP_NOFS
);
1521 /* Make reservation before allocating sequence numbers */
1522 err
= make_reservation(c
, BASEHD
, len
);
1528 xent
->ch
.node_type
= UBIFS_XENT_NODE
;
1529 xent_key_init(c
, &xent_key
, host
->i_ino
, nm
);
1530 key_write(c
, &xent_key
, xent
->key
);
1532 xent
->type
= get_dent_type(inode
->i_mode
);
1533 xent
->nlen
= cpu_to_le16(nm
->len
);
1534 memcpy(xent
->name
, nm
->name
, nm
->len
);
1535 xent
->name
[nm
->len
] = '\0';
1536 zero_dent_node_unused(xent
);
1537 ubifs_prep_grp_node(c
, xent
, xlen
, 0);
1539 ino
= (void *)xent
+ aligned_xlen
;
1540 pack_inode(c
, ino
, inode
, 0);
1541 ino
= (void *)ino
+ UBIFS_INO_NODE_SZ
;
1542 pack_inode(c
, ino
, host
, 1);
1544 err
= write_head(c
, BASEHD
, xent
, len
, &lnum
, &xent_offs
, sync
);
1546 ubifs_wbuf_add_ino_nolock(&c
->jheads
[BASEHD
].wbuf
, host
->i_ino
);
1547 release_head(c
, BASEHD
);
1552 /* Remove the extended attribute entry from TNC */
1553 err
= ubifs_tnc_remove_nm(c
, &xent_key
, nm
);
1556 err
= ubifs_add_dirt(c
, lnum
, xlen
);
1561 * Remove all nodes belonging to the extended attribute inode from TNC.
1562 * Well, there actually must be only one node - the inode itself.
1564 lowest_ino_key(c
, &key1
, inode
->i_ino
);
1565 highest_ino_key(c
, &key2
, inode
->i_ino
);
1566 err
= ubifs_tnc_remove_range(c
, &key1
, &key2
);
1569 err
= ubifs_add_dirt(c
, lnum
, UBIFS_INO_NODE_SZ
);
1573 /* And update TNC with the new host inode position */
1574 ino_key_init(c
, &key1
, host
->i_ino
);
1575 err
= ubifs_tnc_add(c
, &key1
, lnum
, xent_offs
+ len
- hlen
, hlen
);
1579 finish_reservation(c
);
1580 spin_lock(&host_ui
->ui_lock
);
1581 host_ui
->synced_i_size
= host_ui
->ui_size
;
1582 spin_unlock(&host_ui
->ui_lock
);
1583 mark_inode_clean(c
, host_ui
);
1587 ubifs_ro_mode(c
, err
);
1588 finish_reservation(c
);
1593 * ubifs_jnl_change_xattr - change an extended attribute.
1594 * @c: UBIFS file-system description object
1595 * @inode: extended attribute inode
1598 * This function writes the updated version of an extended attribute inode and
1599 * the host inode to the journal (to the base head). The host inode is written
1600 * after the extended attribute inode in order to guarantee that the extended
1601 * attribute will be flushed when the inode is synchronized by 'fsync()' and
1602 * consequently, the write-buffer is synchronized. This function returns zero
1603 * in case of success and a negative error code in case of failure.
1605 int ubifs_jnl_change_xattr(struct ubifs_info
*c
, const struct inode
*inode
,
1606 const struct inode
*host
)
1608 int err
, len1
, len2
, aligned_len
, aligned_len1
, lnum
, offs
;
1609 struct ubifs_inode
*host_ui
= ubifs_inode(host
);
1610 struct ubifs_ino_node
*ino
;
1611 union ubifs_key key
;
1612 int sync
= IS_DIRSYNC(host
);
1614 dbg_jnl("ino %lu, ino %lu", host
->i_ino
, inode
->i_ino
);
1615 ubifs_assert(host
->i_nlink
> 0);
1616 ubifs_assert(inode
->i_nlink
> 0);
1617 ubifs_assert(mutex_is_locked(&host_ui
->ui_mutex
));
1619 len1
= UBIFS_INO_NODE_SZ
+ host_ui
->data_len
;
1620 len2
= UBIFS_INO_NODE_SZ
+ ubifs_inode(inode
)->data_len
;
1621 aligned_len1
= ALIGN(len1
, 8);
1622 aligned_len
= aligned_len1
+ ALIGN(len2
, 8);
1624 ino
= kmalloc(aligned_len
, GFP_NOFS
);
1628 /* Make reservation before allocating sequence numbers */
1629 err
= make_reservation(c
, BASEHD
, aligned_len
);
1633 pack_inode(c
, ino
, host
, 0);
1634 pack_inode(c
, (void *)ino
+ aligned_len1
, inode
, 1);
1636 err
= write_head(c
, BASEHD
, ino
, aligned_len
, &lnum
, &offs
, 0);
1637 if (!sync
&& !err
) {
1638 struct ubifs_wbuf
*wbuf
= &c
->jheads
[BASEHD
].wbuf
;
1640 ubifs_wbuf_add_ino_nolock(wbuf
, host
->i_ino
);
1641 ubifs_wbuf_add_ino_nolock(wbuf
, inode
->i_ino
);
1643 release_head(c
, BASEHD
);
1647 ino_key_init(c
, &key
, host
->i_ino
);
1648 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
, len1
);
1652 ino_key_init(c
, &key
, inode
->i_ino
);
1653 err
= ubifs_tnc_add(c
, &key
, lnum
, offs
+ aligned_len1
, len2
);
1657 finish_reservation(c
);
1658 spin_lock(&host_ui
->ui_lock
);
1659 host_ui
->synced_i_size
= host_ui
->ui_size
;
1660 spin_unlock(&host_ui
->ui_lock
);
1661 mark_inode_clean(c
, host_ui
);
1666 ubifs_ro_mode(c
, err
);
1667 finish_reservation(c
);