journal.c 42 KB

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