journal.c 42 KB

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