journal.c 47 KB

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