drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ubifs / journal.c
1 /*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23 /*
24 * This file implements UBIFS journal.
25 *
26 * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
27 * length and position, while a bud logical eraseblock is any LEB in the main
28 * area. Buds contain file system data - data nodes, inode nodes, etc. The log
29 * contains only references to buds and some other stuff like commit
30 * start node. The idea is that when we commit the journal, we do
31 * not copy the data, the buds just become indexed. Since after the commit the
32 * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
33 * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
34 * become leafs in the future.
35 *
36 * The journal is multi-headed because we want to write data to the journal as
37 * optimally as possible. It is nice to have nodes belonging to the same inode
38 * in one LEB, so we may write data owned by different inodes to different
39 * journal heads, although at present only one data head is used.
40 *
41 * For recovery reasons, the base head contains all inode nodes, all directory
42 * entry nodes and all truncate nodes. This means that the other heads contain
43 * only data nodes.
44 *
45 * Bud LEBs may be half-indexed. For example, if the bud was not full at the
46 * time of commit, the bud is retained to continue to be used in the journal,
47 * even though the "front" of the LEB is now indexed. In that case, the log
48 * reference contains the offset where the bud starts for the purposes of the
49 * journal.
50 *
51 * The journal size has to be limited, because the larger is the journal, the
52 * longer it takes to mount UBIFS (scanning the journal) and the more memory it
53 * takes (indexing in the TNC).
54 *
55 * All the journal write operations like 'ubifs_jnl_update()' here, which write
56 * multiple UBIFS nodes to the journal at one go, are atomic with respect to
57 * unclean reboots. Should the unclean reboot happen, the recovery code drops
58 * all the nodes.
59 */
60
61 #include "ubifs.h"
62
63 #if defined(FEATURE_UBIFS_PERF_INDEX)
64 #include <linux/xlog.h>
65 #include <asm/div64.h>
66
67 #define PRT_TIME_PERIOD 500000000
68 #define PRT_TIME_EXPIRE 5000000000
69 #define ID_CNT 20
70
71 static int ubifs_perf_init = 0;
72 struct ubifs_perf {
73 pid_t pid[ID_CNT];
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];
78 };
79 static struct ubifs_perf org_write, comp_write, low_write, low_read;
80
81 static void g_var_clear(struct ubifs_perf *perf, unsigned int idx)
82 {
83 perf->usage[idx] = 0;
84 perf->count[idx]= 0;
85 perf->size[idx] = 0;
86 }
87
88 static void g_var_init(void)
89 {
90 int i;
91 for(i=0;i<ID_CNT;i++) {
92 org_write.pid[i] = 0;
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);
98 low_write.pid[i] = 0;
99 low_write.tag_t1[i] = 0;
100 g_var_clear(&low_write, i);
101 low_read.pid[i] = 0;
102 low_read.tag_t1[i] = 0;
103 g_var_clear(&low_read, i);
104 }
105 }
106
107 static void ubifs_pref_output(struct ubifs_perf *perf, int idx)
108 {
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]);
119
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]);
123 }
124 }
125 }
126 static unsigned int find_ubifs_index(struct ubifs_perf *perf)
127 {
128 pid_t pid=0;
129 #if 0
130 unsigned int idx=0;
131 unsigned char i=0;
132 unsigned long long t_period=0;
133 unsigned long long time1;
134 #endif
135
136 pid = task_pid_nr(current);
137 #if 1
138 perf->pid[0] = pid;
139 return 0;
140 #else
141
142 if(ubifs_pid[0] == 0)
143 {
144 ubifs_pid[0] = pid;
145 return 0;
146 }
147
148 for(i=0;i<ID_CNT;i++)
149 {
150 if(pid == ubifs_pid[i])
151 {
152 idx=i;
153 break;
154 }
155 if (ubifs_pid[i] == 0)
156 {
157 ubifs_pid[i]=pid;
158 idx=i;
159 break;
160 }
161
162 }
163 if(i==ID_CNT) {
164 for(i=0;i<ID_CNT;i++)
165 {
166 t_period = time1 - ubifs_tag_t1[i];
167 if(t_period >= (unsigned long long )PRT_TIME_EXPIRE)
168 {
169 ubifs_pref_output(i);
170 ubifs_pid[i]=0;
171 ubifs_tag_t1[i] = 0;
172 g_var_clear(i);
173 }
174 }
175 }
176 for(i=0;i<ID_CNT;i++)
177 {
178 if(pid == ubifs_pid[i])
179 {
180 idx=i;
181 break;
182 }
183 if (ubifs_pid[i] == 0)
184 {
185 ubifs_pid[i]=pid;
186 idx=i;
187 break;
188 }
189
190 }
191 if(i==ID_CNT) {
192 ubifs_pid[i-1] = pid;
193 ubifs_tag_t1[i-1] = 0;
194 g_var_clear(i-1);
195 }
196 return idx;
197 #endif
198 }
199
200
201 void ubifs_perf_show(struct ubifs_perf *perf)
202 {
203 unsigned long long t_period=0;
204 int idx=find_ubifs_index(perf);
205 unsigned long long time1 = sched_clock();
206
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)
211 {
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);
217 }
218 }
219 }
220 int ubifs_perf_count(struct ubifs_perf *perf, unsigned long long usage, unsigned int len, int group_idx)
221 {
222 int idx;
223 if(ubifs_perf_init == 0) {
224 g_var_init();
225 ubifs_perf_init = 1;
226 }
227 if(group_idx == -1) {
228 idx=find_ubifs_index(perf);
229 }else {
230 idx = group_idx;
231 }
232 perf->usage[idx] += usage;
233 perf->count[idx] ++;
234 perf->size[idx] += len;
235 return idx;
236 }
237 void ubifs_perf_wcount(unsigned long long usage, unsigned int len, unsigned int comp_len)
238 {
239 int idx;
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);
243
244 }
245 void ubifs_perf_lwcount(unsigned long long usage, unsigned int len)
246 {
247 ubifs_perf_count(&low_write, usage, len, -1);
248 ubifs_perf_show(&low_write);
249 }
250
251 void ubifs_perf_lrcount(unsigned long long usage, unsigned int len)
252 {
253 ubifs_perf_count(&low_read, usage, len, -1);
254 ubifs_perf_show(&low_read);
255 }
256 #endif
257
258 /**
259 * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
260 * @ino: the inode to zero out
261 */
262 static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
263 {
264 memset(ino->padding1, 0, 4);
265 memset(ino->padding2, 0, 26);
266 }
267
268 /**
269 * zero_dent_node_unused - zero out unused fields of an on-flash directory
270 * entry node.
271 * @dent: the directory entry to zero out
272 */
273 static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
274 {
275 dent->padding1 = 0;
276 memset(dent->padding2, 0, 4);
277 }
278
279 /**
280 * zero_data_node_unused - zero out unused fields of an on-flash data node.
281 * @data: the data node to zero out
282 */
283 static inline void zero_data_node_unused(struct ubifs_data_node *data)
284 {
285 memset(data->padding, 0, 2);
286 }
287
288 /**
289 * zero_trun_node_unused - zero out unused fields of an on-flash truncation
290 * node.
291 * @trun: the truncation node to zero out
292 */
293 static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
294 {
295 memset(trun->padding, 0, 12);
296 }
297
298 /**
299 * reserve_space - reserve space in the journal.
300 * @c: UBIFS file-system description object
301 * @jhead: journal head number
302 * @len: node length
303 *
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.
309 */
310 static int reserve_space(struct ubifs_info *c, int jhead, int len)
311 {
312 int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
313 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
314
315 /*
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.
319 */
320 ubifs_assert(!c->ro_media && !c->ro_mount);
321 squeeze = (jhead == BASEHD);
322 again:
323 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
324
325 if (c->ro_error) {
326 err = -EROFS;
327 goto out_unlock;
328 }
329
330 avail = c->leb_size - wbuf->offs - wbuf->used;
331 if (wbuf->lnum != -1 && avail >= len)
332 return 0;
333
334 /*
335 * Write buffer wasn't seek'ed or there is no enough space - look for an
336 * LEB with some empty space.
337 */
338 lnum = ubifs_find_free_space(c, len, &offs, squeeze);
339 if (lnum >= 0)
340 goto out;
341
342 err = lnum;
343 if (err != -ENOSPC)
344 goto out_unlock;
345
346 /*
347 * No free space, we have to run garbage collector to make
348 * some. But the write-buffer mutex has to be unlocked because
349 * GC also takes it.
350 */
351 dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
352 mutex_unlock(&wbuf->io_mutex);
353
354 lnum = ubifs_garbage_collect(c, 0);
355 if (lnum < 0) {
356 err = lnum;
357 if (err != -ENOSPC)
358 return err;
359
360 /*
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
364 * again.
365 */
366 dbg_jnl("GC couldn't make a free LEB for jhead %s",
367 dbg_jhead(jhead));
368 if (retries++ < 2) {
369 dbg_jnl("retry (%d)", retries);
370 goto again;
371 }
372
373 dbg_jnl("return -ENOSPC");
374 return err;
375 }
376
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;
380
381 if (wbuf->lnum != -1 && avail >= len) {
382 /*
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.
386 */
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);
390 if (err)
391 goto out_unlock;
392 return 0;
393 }
394
395 offs = 0;
396
397 out:
398 /*
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.
405 */
406 err = ubifs_wbuf_sync_nolock(wbuf);
407 if (err)
408 goto out_return;
409 err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
410 if (err)
411 goto out_return;
412 err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
413 if (err)
414 goto out_unlock;
415
416 return 0;
417
418 out_unlock:
419 mutex_unlock(&wbuf->io_mutex);
420 return err;
421
422 out_return:
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)
427 /*
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()'.
431 */
432 err = err1;
433 mutex_unlock(&wbuf->io_mutex);
434 return err;
435 }
436
437 /**
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
442 * @len: node length
443 * @lnum: LEB number written is returned here
444 * @offs: offset written is returned here
445 *
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
448 * failure.
449 */
450 static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
451 int *lnum, int *offs)
452 {
453 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
454
455 ubifs_assert(jhead != GCHD);
456
457 *lnum = c->jheads[jhead].wbuf.lnum;
458 *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
459
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);
463
464 return ubifs_wbuf_write_nolock(wbuf, node, len);
465 }
466
467 /**
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
476 *
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).
480 */
481 static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
482 int *lnum, int *offs, int sync)
483 {
484 int err;
485 struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
486
487 ubifs_assert(jhead != GCHD);
488
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);
493
494 err = ubifs_wbuf_write_nolock(wbuf, buf, len);
495 if (err)
496 return err;
497 if (sync)
498 err = ubifs_wbuf_sync_nolock(wbuf);
499 return err;
500 }
501
502 /**
503 * make_reservation - reserve journal space.
504 * @c: UBIFS file-system description object
505 * @jhead: journal head
506 * @len: how many bytes to reserve
507 *
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
512 * failure.
513 *
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
516 * TNC.
517 */
518 static int make_reservation(struct ubifs_info *c, int jhead, int len)
519 {
520 int err, cmt_retries = 0, nospc_retries = 0;
521
522 again:
523 down_read(&c->commit_sem);
524 err = reserve_space(c, jhead, len);
525 if (!err)
526 return 0;
527 up_read(&c->commit_sem);
528
529 if (err == -ENOSPC) {
530 /*
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.
535 */
536 if (nospc_retries++ < 2) {
537 dbg_jnl("no space, retry");
538 err = -EAGAIN;
539 }
540
541 /*
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.
546 */
547 }
548
549 if (err != -EAGAIN)
550 goto out;
551
552 /*
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.
555 */
556 if (cmt_retries > 128) {
557 /*
558 * This should not happen unless the journal size limitations
559 * are too tough.
560 */
561 ubifs_err("stuck in space allocation");
562 err = -ENOSPC;
563 goto out;
564 } else if (cmt_retries > 32)
565 ubifs_warn("too many space allocation re-tries (%d)",
566 cmt_retries);
567
568 dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
569 cmt_retries);
570 cmt_retries += 1;
571
572 err = ubifs_run_commit(c);
573 if (err)
574 return err;
575 goto again;
576
577 out:
578 ubifs_err("cannot reserve %d bytes in jhead %d, error %d",
579 len, jhead, err);
580 if (err == -ENOSPC) {
581 /* This are some budgeting problems, print useful information */
582 down_write(&c->commit_sem);
583 dump_stack();
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);
588 }
589 return err;
590 }
591
592 /**
593 * release_head - release a journal head.
594 * @c: UBIFS file-system description object
595 * @jhead: journal head
596 *
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.
600 */
601 static inline void release_head(struct ubifs_info *c, int jhead)
602 {
603 mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
604 }
605
606 /**
607 * finish_reservation - finish a reservation.
608 * @c: UBIFS file-system description object
609 *
610 * This function finishes journal space reservation. It must be called after
611 * 'make_reservation()'.
612 */
613 static void finish_reservation(struct ubifs_info *c)
614 {
615 up_read(&c->commit_sem);
616 }
617
618 /**
619 * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
620 * @mode: inode mode
621 */
622 static int get_dent_type(int mode)
623 {
624 switch (mode & S_IFMT) {
625 case S_IFREG:
626 return UBIFS_ITYPE_REG;
627 case S_IFDIR:
628 return UBIFS_ITYPE_DIR;
629 case S_IFLNK:
630 return UBIFS_ITYPE_LNK;
631 case S_IFBLK:
632 return UBIFS_ITYPE_BLK;
633 case S_IFCHR:
634 return UBIFS_ITYPE_CHR;
635 case S_IFIFO:
636 return UBIFS_ITYPE_FIFO;
637 case S_IFSOCK:
638 return UBIFS_ITYPE_SOCK;
639 default:
640 BUG();
641 }
642 return 0;
643 }
644
645 /**
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
651 */
652 static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
653 const struct inode *inode, int last)
654 {
655 int data_len = 0, last_reference = !inode->i_nlink;
656 struct ubifs_inode *ui = ubifs_inode(inode);
657
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);
679
680 /*
681 * Drop the attached data if this is a deletion inode, the data is not
682 * needed anymore.
683 */
684 if (!last_reference) {
685 memcpy(ino->data, ui->data, ui->data_len);
686 data_len = ui->data_len;
687 }
688
689 ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
690 }
691
692 /**
693 * mark_inode_clean - mark UBIFS inode as clean.
694 * @c: UBIFS file-system description object
695 * @ui: UBIFS inode to mark as clean
696 *
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
700 * just do nothing.
701 */
702 static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
703 {
704 if (ui->dirty)
705 ubifs_release_dirty_inode_budget(c, ui);
706 ui->dirty = 0;
707 }
708
709 /**
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
717 *
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.
721 *
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.
729 *
730 * If the inode (@inode) or the parent directory (@dir) are synchronous, this
731 * function synchronizes the write-buffer.
732 *
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.
735 */
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)
739 {
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;
748
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);
751 if (!xent)
752 ubifs_assert(dir_ui->data_len == 0);
753 ubifs_assert(mutex_is_locked(&dir_ui->ui_mutex));
754
755 dlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
756 ilen = UBIFS_INO_NODE_SZ;
757
758 /*
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.
763 */
764 if (!last_reference) {
765 ilen += ui->data_len;
766 sync |= IS_SYNC(inode);
767 }
768
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.
773 */
774 len = aligned_dlen + aligned_ilen +
775 UBIFS_INO_NODE_SZ + dir_ui->data_len;
776 dent = kmalloc(len, GFP_NOFS);
777 if (!dent)
778 return -ENOMEM;
779
780 /* Make reservation before allocating sequence numbers */
781 err = make_reservation(c, BASEHD, len);
782 if (err)
783 goto out_free;
784
785 if (!xent) {
786 dent->ch.node_type = UBIFS_DENT_NODE;
787 dent_key_init(c, &dent_key, dir->i_ino, nm);
788 } else {
789 dent->ch.node_type = UBIFS_XENT_NODE;
790 xent_key_init(c, &dent_key, dir->i_ino, nm);
791 }
792
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);
801
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);
806
807 if (last_reference) {
808 err = ubifs_add_orphan(c, inode->i_ino);
809 if (err) {
810 release_head(c, BASEHD);
811 goto out_finish;
812 }
813 ui->del_cmtno = c->cmt_no;
814 }
815
816 err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
817 if (err)
818 goto out_release;
819 if (!sync) {
820 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
821
822 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
823 ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
824 }
825 release_head(c, BASEHD);
826 kfree(dent);
827
828 if (deletion) {
829 err = ubifs_tnc_remove_nm(c, &dent_key, nm);
830 if (err)
831 goto out_ro;
832 err = ubifs_add_dirt(c, lnum, dlen);
833 } else
834 err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
835 if (err)
836 goto out_ro;
837
838 /*
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.
843 */
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);
847 if (err)
848 goto out_ro;
849
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);
854 if (err)
855 goto out_ro;
856
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);
863 return 0;
864
865 out_finish:
866 finish_reservation(c);
867 out_free:
868 kfree(dent);
869 return err;
870
871 out_release:
872 release_head(c, BASEHD);
873 kfree(dent);
874 out_ro:
875 ubifs_ro_mode(c, err);
876 if (last_reference)
877 ubifs_delete_orphan(c, inode->i_ino);
878 finish_reservation(c);
879 return err;
880 }
881
882 /**
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
886 * @key: node key
887 * @buf: buffer to write
888 * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
889 *
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.
892 */
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)
895 {
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();
902 #endif
903
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);
907
908 //data = kmalloc(dlen, GFP_NOFS | __GFP_NOWARN);
909 data = NULL;
910 if (!data) {
911 /*
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.
917 */
918 allocated = 0;
919 mutex_lock(&c->write_reserve_mutex);
920 data = c->write_reserve_buf;
921 }
922
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);
927
928 if (!(ui->flags & UBIFS_COMPR_FL))
929 /* Compression is disabled for this inode */
930 compr_type = UBIFS_COMPR_NONE;
931 else
932 compr_type = ui->compr_type;
933
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);
938
939 dlen = UBIFS_DATA_NODE_SZ + out_len;
940 data->compr_type = cpu_to_le16(compr_type);
941
942 /* Make reservation before allocating sequence numbers */
943 err = make_reservation(c, DATAHD, dlen);
944 if (err)
945 goto out_free;
946
947 err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
948 if (err)
949 goto out_release;
950 ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
951 release_head(c, DATAHD);
952
953 err = ubifs_tnc_add(c, key, lnum, offs, dlen);
954 if (err)
955 goto out_ro;
956
957 finish_reservation(c);
958 if (!allocated)
959 mutex_unlock(&c->write_reserve_mutex);
960 else
961 kfree(data);
962 #if defined(FEATURE_UBIFS_PERF_INDEX)
963 ubifs_perf_wcount(sched_clock() - time1, len, dlen);
964 #endif
965 return 0;
966
967 out_release:
968 release_head(c, DATAHD);
969 out_ro:
970 ubifs_ro_mode(c, err);
971 finish_reservation(c);
972 out_free:
973 if (!allocated)
974 mutex_unlock(&c->write_reserve_mutex);
975 else
976 kfree(data);
977 return err;
978 }
979
980 /**
981 * ubifs_jnl_write_inode - flush inode to the journal.
982 * @c: UBIFS file-system description object
983 * @inode: inode to flush
984 *
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.
988 */
989 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
990 {
991 int err, lnum, offs;
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;
995
996 dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
997
998 /*
999 * If the inode is being deleted, do not write the attached data. No
1000 * need to synchronize the write-buffer either.
1001 */
1002 if (!last_reference) {
1003 len += ui->data_len;
1004 sync = IS_SYNC(inode);
1005 }
1006 ino = kmalloc(len, GFP_NOFS);
1007 if (!ino)
1008 return -ENOMEM;
1009
1010 /* Make reservation before allocating sequence numbers */
1011 err = make_reservation(c, BASEHD, len);
1012 if (err)
1013 goto out_free;
1014
1015 pack_inode(c, ino, inode, 1);
1016 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1017 if (err)
1018 goto out_release;
1019 if (!sync)
1020 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1021 inode->i_ino);
1022 release_head(c, BASEHD);
1023
1024 if (last_reference) {
1025 err = ubifs_tnc_remove_ino(c, inode->i_ino);
1026 if (err)
1027 goto out_ro;
1028 ubifs_delete_orphan(c, inode->i_ino);
1029 err = ubifs_add_dirt(c, lnum, len);
1030 } else {
1031 union ubifs_key key;
1032
1033 ino_key_init(c, &key, inode->i_ino);
1034 err = ubifs_tnc_add(c, &key, lnum, offs, len);
1035 }
1036 if (err)
1037 goto out_ro;
1038
1039 finish_reservation(c);
1040 spin_lock(&ui->ui_lock);
1041 ui->synced_i_size = ui->ui_size;
1042 spin_unlock(&ui->ui_lock);
1043 kfree(ino);
1044 return 0;
1045
1046 out_release:
1047 release_head(c, BASEHD);
1048 out_ro:
1049 ubifs_ro_mode(c, err);
1050 finish_reservation(c);
1051 out_free:
1052 kfree(ino);
1053 return err;
1054 }
1055
1056 /**
1057 * ubifs_jnl_delete_inode - delete an inode.
1058 * @c: UBIFS file-system description object
1059 * @inode: inode to delete
1060 *
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
1063 * journal.
1064 *
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
1076 * removed from TNC.
1077 *
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.
1081 *
1082 * This function returns zero in case of success and a negative error code in
1083 * case of failure.
1084 */
1085 int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
1086 {
1087 int err;
1088 struct ubifs_inode *ui = ubifs_inode(inode);
1089
1090 ubifs_assert(inode->i_nlink == 0);
1091
1092 if (ui->del_cmtno != c->cmt_no)
1093 /* A commit happened for sure */
1094 return ubifs_jnl_write_inode(c, inode);
1095
1096 down_read(&c->commit_sem);
1097 /*
1098 * Check commit number again, because the first test has been done
1099 * without @c->commit_sem, so a commit might have happened.
1100 */
1101 if (ui->del_cmtno != c->cmt_no) {
1102 up_read(&c->commit_sem);
1103 return ubifs_jnl_write_inode(c, inode);
1104 }
1105
1106 err = ubifs_tnc_remove_ino(c, inode->i_ino);
1107 if (err)
1108 ubifs_ro_mode(c, err);
1109 else
1110 ubifs_delete_orphan(c, inode->i_ino);
1111 up_read(&c->commit_sem);
1112 return err;
1113 }
1114
1115 /**
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
1123 *
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
1127 * returned.
1128 */
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)
1133 {
1134 void *p;
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);
1144
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));
1153
1154 dlen1 = UBIFS_DENT_NODE_SZ + new_dentry->d_name.len + 1;
1155 dlen2 = UBIFS_DENT_NODE_SZ + old_dentry->d_name.len + 1;
1156 if (new_inode) {
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;
1162 } else
1163 ilen = 0;
1164
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)
1169 len += plen;
1170 dent = kmalloc(len, GFP_NOFS);
1171 if (!dent)
1172 return -ENOMEM;
1173
1174 /* Make reservation before allocating sequence numbers */
1175 err = make_reservation(c, BASEHD, len);
1176 if (err)
1177 goto out_free;
1178
1179 /* Make new dent */
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);
1189
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);
1195 dent2->inum = 0;
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);
1202
1203 p = (void *)dent2 + aligned_dlen2;
1204 if (new_inode) {
1205 pack_inode(c, p, new_inode, 0);
1206 p += ALIGN(ilen, 8);
1207 }
1208
1209 if (!move)
1210 pack_inode(c, p, old_dir, 1);
1211 else {
1212 pack_inode(c, p, old_dir, 0);
1213 p += ALIGN(plen, 8);
1214 pack_inode(c, p, new_dir, 1);
1215 }
1216
1217 if (last_reference) {
1218 err = ubifs_add_orphan(c, new_inode->i_ino);
1219 if (err) {
1220 release_head(c, BASEHD);
1221 goto out_finish;
1222 }
1223 new_ui->del_cmtno = c->cmt_no;
1224 }
1225
1226 err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
1227 if (err)
1228 goto out_release;
1229 if (!sync) {
1230 struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
1231
1232 ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
1233 ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
1234 if (new_inode)
1235 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
1236 new_inode->i_ino);
1237 }
1238 release_head(c, BASEHD);
1239
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);
1242 if (err)
1243 goto out_ro;
1244
1245 err = ubifs_add_dirt(c, lnum, dlen2);
1246 if (err)
1247 goto out_ro;
1248
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);
1251 if (err)
1252 goto out_ro;
1253
1254 offs += aligned_dlen1 + aligned_dlen2;
1255 if (new_inode) {
1256 ino_key_init(c, &key, new_inode->i_ino);
1257 err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
1258 if (err)
1259 goto out_ro;
1260 offs += ALIGN(ilen, 8);
1261 }
1262
1263 ino_key_init(c, &key, old_dir->i_ino);
1264 err = ubifs_tnc_add(c, &key, lnum, offs, plen);
1265 if (err)
1266 goto out_ro;
1267
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);
1272 if (err)
1273 goto out_ro;
1274 }
1275
1276 finish_reservation(c);
1277 if (new_inode) {
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);
1282 }
1283 mark_inode_clean(c, ubifs_inode(old_dir));
1284 if (move)
1285 mark_inode_clean(c, ubifs_inode(new_dir));
1286 kfree(dent);
1287 return 0;
1288
1289 out_release:
1290 release_head(c, BASEHD);
1291 out_ro:
1292 ubifs_ro_mode(c, err);
1293 if (last_reference)
1294 ubifs_delete_orphan(c, new_inode->i_ino);
1295 out_finish:
1296 finish_reservation(c);
1297 out_free:
1298 kfree(dent);
1299 return err;
1300 }
1301
1302 /**
1303 * recomp_data_node - re-compress a truncated data node.
1304 * @dn: data node to re-compress
1305 * @new_len: new length
1306 *
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.
1309 */
1310 static int recomp_data_node(struct ubifs_data_node *dn, int *new_len)
1311 {
1312 void *buf;
1313 int err, len, compr_type, out_len;
1314
1315 out_len = le32_to_cpu(dn->size);
1316 buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
1317 if (!buf)
1318 return -ENOMEM;
1319
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);
1323 if (err)
1324 goto out;
1325
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;
1331 out:
1332 kfree(buf);
1333 return err;
1334 }
1335
1336 /**
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
1342 *
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.
1347 *
1348 * This function marks the inode as clean and returns zero on success. In case
1349 * of failure, a negative error code is returned.
1350 */
1351 int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1352 loff_t old_size, loff_t new_size)
1353 {
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;
1361 unsigned int blk;
1362
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));
1368
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);
1372 if (!ino)
1373 return -ENOMEM;
1374
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);
1381
1382 dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
1383 if (dlen) {
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);
1390 if (err == -ENOENT)
1391 dlen = 0; /* Not found (so it is a hole) */
1392 else if (err)
1393 goto out_free;
1394 else {
1395 if (le32_to_cpu(dn->size) <= dlen)
1396 dlen = 0; /* Nothing to do */
1397 else {
1398 int compr_type = le16_to_cpu(dn->compr_type);
1399
1400 if (compr_type != UBIFS_COMPR_NONE) {
1401 err = recomp_data_node(dn, &dlen);
1402 if (err)
1403 goto out_free;
1404 } else {
1405 dn->size = cpu_to_le32(dlen);
1406 dlen += UBIFS_DATA_NODE_SZ;
1407 }
1408 zero_data_node_unused(dn);
1409 }
1410 }
1411 }
1412
1413 /* Must make reservation before allocating sequence numbers */
1414 len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
1415 if (dlen)
1416 len += dlen;
1417 err = make_reservation(c, BASEHD, len);
1418 if (err)
1419 goto out_free;
1420
1421 pack_inode(c, ino, inode, 0);
1422 ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
1423 if (dlen)
1424 ubifs_prep_grp_node(c, dn, dlen, 1);
1425
1426 err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
1427 if (err)
1428 goto out_release;
1429 if (!sync)
1430 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
1431 release_head(c, BASEHD);
1432
1433 if (dlen) {
1434 sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
1435 err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
1436 if (err)
1437 goto out_ro;
1438 }
1439
1440 ino_key_init(c, &key, inum);
1441 err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
1442 if (err)
1443 goto out_ro;
1444
1445 err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
1446 if (err)
1447 goto out_ro;
1448
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);
1452
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);
1456
1457 err = ubifs_tnc_remove_range(c, &key, &to_key);
1458 if (err)
1459 goto out_ro;
1460
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);
1466 kfree(ino);
1467 return 0;
1468
1469 out_release:
1470 release_head(c, BASEHD);
1471 out_ro:
1472 ubifs_ro_mode(c, err);
1473 finish_reservation(c);
1474 out_free:
1475 kfree(ino);
1476 return err;
1477 }
1478
1479
1480 /**
1481 * ubifs_jnl_delete_xattr - delete an extended attribute.
1482 * @c: UBIFS file-system description object
1483 * @host: host inode
1484 * @inode: extended attribute inode
1485 * @nm: extended attribute entry name
1486 *
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.
1491 */
1492 int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1493 const struct inode *inode, const struct qstr *nm)
1494 {
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);
1501
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));
1507
1508 /*
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.
1511 */
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);
1516
1517 xent = kmalloc(len, GFP_NOFS);
1518 if (!xent)
1519 return -ENOMEM;
1520
1521 /* Make reservation before allocating sequence numbers */
1522 err = make_reservation(c, BASEHD, len);
1523 if (err) {
1524 kfree(xent);
1525 return err;
1526 }
1527
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);
1531 xent->inum = 0;
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);
1538
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);
1543
1544 err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
1545 if (!sync && !err)
1546 ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
1547 release_head(c, BASEHD);
1548 kfree(xent);
1549 if (err)
1550 goto out_ro;
1551
1552 /* Remove the extended attribute entry from TNC */
1553 err = ubifs_tnc_remove_nm(c, &xent_key, nm);
1554 if (err)
1555 goto out_ro;
1556 err = ubifs_add_dirt(c, lnum, xlen);
1557 if (err)
1558 goto out_ro;
1559
1560 /*
1561 * Remove all nodes belonging to the extended attribute inode from TNC.
1562 * Well, there actually must be only one node - the inode itself.
1563 */
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);
1567 if (err)
1568 goto out_ro;
1569 err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
1570 if (err)
1571 goto out_ro;
1572
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);
1576 if (err)
1577 goto out_ro;
1578
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);
1584 return 0;
1585
1586 out_ro:
1587 ubifs_ro_mode(c, err);
1588 finish_reservation(c);
1589 return err;
1590 }
1591
1592 /**
1593 * ubifs_jnl_change_xattr - change an extended attribute.
1594 * @c: UBIFS file-system description object
1595 * @inode: extended attribute inode
1596 * @host: host inode
1597 *
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.
1604 */
1605 int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
1606 const struct inode *host)
1607 {
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);
1613
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));
1618
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);
1623
1624 ino = kmalloc(aligned_len, GFP_NOFS);
1625 if (!ino)
1626 return -ENOMEM;
1627
1628 /* Make reservation before allocating sequence numbers */
1629 err = make_reservation(c, BASEHD, aligned_len);
1630 if (err)
1631 goto out_free;
1632
1633 pack_inode(c, ino, host, 0);
1634 pack_inode(c, (void *)ino + aligned_len1, inode, 1);
1635
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;
1639
1640 ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
1641 ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
1642 }
1643 release_head(c, BASEHD);
1644 if (err)
1645 goto out_ro;
1646
1647 ino_key_init(c, &key, host->i_ino);
1648 err = ubifs_tnc_add(c, &key, lnum, offs, len1);
1649 if (err)
1650 goto out_ro;
1651
1652 ino_key_init(c, &key, inode->i_ino);
1653 err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
1654 if (err)
1655 goto out_ro;
1656
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);
1662 kfree(ino);
1663 return 0;
1664
1665 out_ro:
1666 ubifs_ro_mode(c, err);
1667 finish_reservation(c);
1668 out_free:
1669 kfree(ino);
1670 return err;
1671 }
1672