journal.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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 = kmalloc(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_rename - rename a directory entry.
  821. * @c: UBIFS file-system description object
  822. * @old_dir: parent inode of directory entry to rename
  823. * @old_dentry: directory entry to rename
  824. * @new_dir: parent inode of directory entry to rename
  825. * @new_dentry: new directory entry (or directory entry to replace)
  826. * @sync: non-zero if the write-buffer has to be synchronized
  827. *
  828. * This function implements the re-name operation which may involve writing up
  829. * to 3 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_rename(struct ubifs_info *c, const struct inode *old_dir,
  834. const struct dentry *old_dentry,
  835. const struct inode *new_dir,
  836. const struct dentry *new_dentry, int sync)
  837. {
  838. void *p;
  839. union ubifs_key key;
  840. struct ubifs_dent_node *dent, *dent2;
  841. int err, dlen1, dlen2, ilen, lnum, offs, len;
  842. const struct inode *old_inode = d_inode(old_dentry);
  843. const struct inode *new_inode = d_inode(new_dentry);
  844. int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
  845. int last_reference = !!(new_inode && new_inode->i_nlink == 0);
  846. int move = (old_dir != new_dir);
  847. struct ubifs_inode *uninitialized_var(new_ui);
  848. dbg_jnl("dent '%pd' in dir ino %lu to dent '%pd' in dir ino %lu",
  849. old_dentry, old_dir->i_ino, new_dentry, new_dir->i_ino);
  850. ubifs_assert(ubifs_inode(old_dir)->data_len == 0);
  851. ubifs_assert(ubifs_inode(new_dir)->data_len == 0);
  852. ubifs_assert(mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
  853. ubifs_assert(mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
  854. dlen1 = UBIFS_DENT_NODE_SZ + new_dentry->d_name.len + 1;
  855. dlen2 = UBIFS_DENT_NODE_SZ + old_dentry->d_name.len + 1;
  856. if (new_inode) {
  857. new_ui = ubifs_inode(new_inode);
  858. ubifs_assert(mutex_is_locked(&new_ui->ui_mutex));
  859. ilen = UBIFS_INO_NODE_SZ;
  860. if (!last_reference)
  861. ilen += new_ui->data_len;
  862. } else
  863. ilen = 0;
  864. aligned_dlen1 = ALIGN(dlen1, 8);
  865. aligned_dlen2 = ALIGN(dlen2, 8);
  866. len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
  867. if (old_dir != new_dir)
  868. len += plen;
  869. dent = kmalloc(len, GFP_NOFS);
  870. if (!dent)
  871. return -ENOMEM;
  872. /* Make reservation before allocating sequence numbers */
  873. err = make_reservation(c, BASEHD, len);
  874. if (err)
  875. goto out_free;
  876. /* Make new dent */
  877. dent->ch.node_type = UBIFS_DENT_NODE;
  878. dent_key_init_flash(c, &dent->key, new_dir->i_ino, &new_dentry->d_name);
  879. dent->inum = cpu_to_le64(old_inode->i_ino);
  880. dent->type = get_dent_type(old_inode->i_mode);
  881. dent->nlen = cpu_to_le16(new_dentry->d_name.len);
  882. memcpy(dent->name, new_dentry->d_name.name, new_dentry->d_name.len);
  883. dent->name[new_dentry->d_name.len] = '\0';
  884. zero_dent_node_unused(dent);
  885. ubifs_prep_grp_node(c, dent, dlen1, 0);
  886. /* Make deletion dent */
  887. dent2 = (void *)dent + aligned_dlen1;
  888. dent2->ch.node_type = UBIFS_DENT_NODE;
  889. dent_key_init_flash(c, &dent2->key, old_dir->i_ino,
  890. &old_dentry->d_name);
  891. dent2->inum = 0;
  892. dent2->type = DT_UNKNOWN;
  893. dent2->nlen = cpu_to_le16(old_dentry->d_name.len);
  894. memcpy(dent2->name, old_dentry->d_name.name, old_dentry->d_name.len);
  895. dent2->name[old_dentry->d_name.len] = '\0';
  896. zero_dent_node_unused(dent2);
  897. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  898. p = (void *)dent2 + aligned_dlen2;
  899. if (new_inode) {
  900. pack_inode(c, p, new_inode, 0);
  901. p += ALIGN(ilen, 8);
  902. }
  903. if (!move)
  904. pack_inode(c, p, old_dir, 1);
  905. else {
  906. pack_inode(c, p, old_dir, 0);
  907. p += ALIGN(plen, 8);
  908. pack_inode(c, p, new_dir, 1);
  909. }
  910. if (last_reference) {
  911. err = ubifs_add_orphan(c, new_inode->i_ino);
  912. if (err) {
  913. release_head(c, BASEHD);
  914. goto out_finish;
  915. }
  916. new_ui->del_cmtno = c->cmt_no;
  917. }
  918. err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
  919. if (err)
  920. goto out_release;
  921. if (!sync) {
  922. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  923. ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
  924. ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
  925. if (new_inode)
  926. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  927. new_inode->i_ino);
  928. }
  929. release_head(c, BASEHD);
  930. dent_key_init(c, &key, new_dir->i_ino, &new_dentry->d_name);
  931. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, &new_dentry->d_name);
  932. if (err)
  933. goto out_ro;
  934. err = ubifs_add_dirt(c, lnum, dlen2);
  935. if (err)
  936. goto out_ro;
  937. dent_key_init(c, &key, old_dir->i_ino, &old_dentry->d_name);
  938. err = ubifs_tnc_remove_nm(c, &key, &old_dentry->d_name);
  939. if (err)
  940. goto out_ro;
  941. offs += aligned_dlen1 + aligned_dlen2;
  942. if (new_inode) {
  943. ino_key_init(c, &key, new_inode->i_ino);
  944. err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
  945. if (err)
  946. goto out_ro;
  947. offs += ALIGN(ilen, 8);
  948. }
  949. ino_key_init(c, &key, old_dir->i_ino);
  950. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  951. if (err)
  952. goto out_ro;
  953. if (old_dir != new_dir) {
  954. offs += ALIGN(plen, 8);
  955. ino_key_init(c, &key, new_dir->i_ino);
  956. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  957. if (err)
  958. goto out_ro;
  959. }
  960. finish_reservation(c);
  961. if (new_inode) {
  962. mark_inode_clean(c, new_ui);
  963. spin_lock(&new_ui->ui_lock);
  964. new_ui->synced_i_size = new_ui->ui_size;
  965. spin_unlock(&new_ui->ui_lock);
  966. }
  967. mark_inode_clean(c, ubifs_inode(old_dir));
  968. if (move)
  969. mark_inode_clean(c, ubifs_inode(new_dir));
  970. kfree(dent);
  971. return 0;
  972. out_release:
  973. release_head(c, BASEHD);
  974. out_ro:
  975. ubifs_ro_mode(c, err);
  976. if (last_reference)
  977. ubifs_delete_orphan(c, new_inode->i_ino);
  978. out_finish:
  979. finish_reservation(c);
  980. out_free:
  981. kfree(dent);
  982. return err;
  983. }
  984. /**
  985. * recomp_data_node - re-compress a truncated data node.
  986. * @dn: data node to re-compress
  987. * @new_len: new length
  988. *
  989. * This function is used when an inode is truncated and the last data node of
  990. * the inode has to be re-compressed and re-written.
  991. */
  992. static int recomp_data_node(const struct ubifs_info *c,
  993. struct ubifs_data_node *dn, int *new_len)
  994. {
  995. void *buf;
  996. int err, len, compr_type, out_len;
  997. out_len = le32_to_cpu(dn->size);
  998. buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
  999. if (!buf)
  1000. return -ENOMEM;
  1001. len = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  1002. compr_type = le16_to_cpu(dn->compr_type);
  1003. err = ubifs_decompress(c, &dn->data, len, buf, &out_len, compr_type);
  1004. if (err)
  1005. goto out;
  1006. ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
  1007. ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
  1008. dn->compr_type = cpu_to_le16(compr_type);
  1009. dn->size = cpu_to_le32(*new_len);
  1010. *new_len = UBIFS_DATA_NODE_SZ + out_len;
  1011. out:
  1012. kfree(buf);
  1013. return err;
  1014. }
  1015. /**
  1016. * ubifs_jnl_truncate - update the journal for a truncation.
  1017. * @c: UBIFS file-system description object
  1018. * @inode: inode to truncate
  1019. * @old_size: old size
  1020. * @new_size: new size
  1021. *
  1022. * When the size of a file decreases due to truncation, a truncation node is
  1023. * written, the journal tree is updated, and the last data block is re-written
  1024. * if it has been affected. The inode is also updated in order to synchronize
  1025. * the new inode size.
  1026. *
  1027. * This function marks the inode as clean and returns zero on success. In case
  1028. * of failure, a negative error code is returned.
  1029. */
  1030. int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
  1031. loff_t old_size, loff_t new_size)
  1032. {
  1033. union ubifs_key key, to_key;
  1034. struct ubifs_ino_node *ino;
  1035. struct ubifs_trun_node *trun;
  1036. struct ubifs_data_node *uninitialized_var(dn);
  1037. int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
  1038. struct ubifs_inode *ui = ubifs_inode(inode);
  1039. ino_t inum = inode->i_ino;
  1040. unsigned int blk;
  1041. dbg_jnl("ino %lu, size %lld -> %lld",
  1042. (unsigned long)inum, old_size, new_size);
  1043. ubifs_assert(!ui->data_len);
  1044. ubifs_assert(S_ISREG(inode->i_mode));
  1045. ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  1046. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1047. UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
  1048. ino = kmalloc(sz, GFP_NOFS);
  1049. if (!ino)
  1050. return -ENOMEM;
  1051. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1052. trun->ch.node_type = UBIFS_TRUN_NODE;
  1053. trun->inum = cpu_to_le32(inum);
  1054. trun->old_size = cpu_to_le64(old_size);
  1055. trun->new_size = cpu_to_le64(new_size);
  1056. zero_trun_node_unused(trun);
  1057. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1058. if (dlen) {
  1059. /* Get last data block so it can be truncated */
  1060. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1061. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1062. data_key_init(c, &key, inum, blk);
  1063. dbg_jnlk(&key, "last block key ");
  1064. err = ubifs_tnc_lookup(c, &key, dn);
  1065. if (err == -ENOENT)
  1066. dlen = 0; /* Not found (so it is a hole) */
  1067. else if (err)
  1068. goto out_free;
  1069. else {
  1070. int dn_len = le32_to_cpu(dn->size);
  1071. if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
  1072. ubifs_err(c, "bad data node (block %u, inode %lu)",
  1073. blk, inode->i_ino);
  1074. ubifs_dump_node(c, dn);
  1075. goto out_free;
  1076. }
  1077. if (dn_len <= dlen)
  1078. dlen = 0; /* Nothing to do */
  1079. else {
  1080. int compr_type = le16_to_cpu(dn->compr_type);
  1081. if (compr_type != UBIFS_COMPR_NONE) {
  1082. err = recomp_data_node(c, dn, &dlen);
  1083. if (err)
  1084. goto out_free;
  1085. } else {
  1086. dn->size = cpu_to_le32(dlen);
  1087. dlen += UBIFS_DATA_NODE_SZ;
  1088. }
  1089. zero_data_node_unused(dn);
  1090. }
  1091. }
  1092. }
  1093. /* Must make reservation before allocating sequence numbers */
  1094. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1095. if (dlen)
  1096. len += dlen;
  1097. err = make_reservation(c, BASEHD, len);
  1098. if (err)
  1099. goto out_free;
  1100. pack_inode(c, ino, inode, 0);
  1101. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1102. if (dlen)
  1103. ubifs_prep_grp_node(c, dn, dlen, 1);
  1104. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1105. if (err)
  1106. goto out_release;
  1107. if (!sync)
  1108. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1109. release_head(c, BASEHD);
  1110. if (dlen) {
  1111. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1112. err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
  1113. if (err)
  1114. goto out_ro;
  1115. }
  1116. ino_key_init(c, &key, inum);
  1117. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
  1118. if (err)
  1119. goto out_ro;
  1120. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1121. if (err)
  1122. goto out_ro;
  1123. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1124. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1125. data_key_init(c, &key, inum, blk);
  1126. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1127. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
  1128. data_key_init(c, &to_key, inum, blk);
  1129. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1130. if (err)
  1131. goto out_ro;
  1132. finish_reservation(c);
  1133. spin_lock(&ui->ui_lock);
  1134. ui->synced_i_size = ui->ui_size;
  1135. spin_unlock(&ui->ui_lock);
  1136. mark_inode_clean(c, ui);
  1137. kfree(ino);
  1138. return 0;
  1139. out_release:
  1140. release_head(c, BASEHD);
  1141. out_ro:
  1142. ubifs_ro_mode(c, err);
  1143. finish_reservation(c);
  1144. out_free:
  1145. kfree(ino);
  1146. return err;
  1147. }
  1148. /**
  1149. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1150. * @c: UBIFS file-system description object
  1151. * @host: host inode
  1152. * @inode: extended attribute inode
  1153. * @nm: extended attribute entry name
  1154. *
  1155. * This function delete an extended attribute which is very similar to
  1156. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1157. * updates the target inode. Returns zero in case of success and a negative
  1158. * error code in case of failure.
  1159. */
  1160. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1161. const struct inode *inode, const struct qstr *nm)
  1162. {
  1163. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
  1164. struct ubifs_dent_node *xent;
  1165. struct ubifs_ino_node *ino;
  1166. union ubifs_key xent_key, key1, key2;
  1167. int sync = IS_DIRSYNC(host);
  1168. struct ubifs_inode *host_ui = ubifs_inode(host);
  1169. dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
  1170. host->i_ino, inode->i_ino, nm->name,
  1171. ubifs_inode(inode)->data_len);
  1172. ubifs_assert(inode->i_nlink == 0);
  1173. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1174. /*
  1175. * Since we are deleting the inode, we do not bother to attach any data
  1176. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1177. */
  1178. xlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
  1179. aligned_xlen = ALIGN(xlen, 8);
  1180. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1181. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1182. xent = kmalloc(len, GFP_NOFS);
  1183. if (!xent)
  1184. return -ENOMEM;
  1185. /* Make reservation before allocating sequence numbers */
  1186. err = make_reservation(c, BASEHD, len);
  1187. if (err) {
  1188. kfree(xent);
  1189. return err;
  1190. }
  1191. xent->ch.node_type = UBIFS_XENT_NODE;
  1192. xent_key_init(c, &xent_key, host->i_ino, nm);
  1193. key_write(c, &xent_key, xent->key);
  1194. xent->inum = 0;
  1195. xent->type = get_dent_type(inode->i_mode);
  1196. xent->nlen = cpu_to_le16(nm->len);
  1197. memcpy(xent->name, nm->name, nm->len);
  1198. xent->name[nm->len] = '\0';
  1199. zero_dent_node_unused(xent);
  1200. ubifs_prep_grp_node(c, xent, xlen, 0);
  1201. ino = (void *)xent + aligned_xlen;
  1202. pack_inode(c, ino, inode, 0);
  1203. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1204. pack_inode(c, ino, host, 1);
  1205. err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
  1206. if (!sync && !err)
  1207. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1208. release_head(c, BASEHD);
  1209. kfree(xent);
  1210. if (err)
  1211. goto out_ro;
  1212. /* Remove the extended attribute entry from TNC */
  1213. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1214. if (err)
  1215. goto out_ro;
  1216. err = ubifs_add_dirt(c, lnum, xlen);
  1217. if (err)
  1218. goto out_ro;
  1219. /*
  1220. * Remove all nodes belonging to the extended attribute inode from TNC.
  1221. * Well, there actually must be only one node - the inode itself.
  1222. */
  1223. lowest_ino_key(c, &key1, inode->i_ino);
  1224. highest_ino_key(c, &key2, inode->i_ino);
  1225. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1226. if (err)
  1227. goto out_ro;
  1228. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1229. if (err)
  1230. goto out_ro;
  1231. /* And update TNC with the new host inode position */
  1232. ino_key_init(c, &key1, host->i_ino);
  1233. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
  1234. if (err)
  1235. goto out_ro;
  1236. finish_reservation(c);
  1237. spin_lock(&host_ui->ui_lock);
  1238. host_ui->synced_i_size = host_ui->ui_size;
  1239. spin_unlock(&host_ui->ui_lock);
  1240. mark_inode_clean(c, host_ui);
  1241. return 0;
  1242. out_ro:
  1243. ubifs_ro_mode(c, err);
  1244. finish_reservation(c);
  1245. return err;
  1246. }
  1247. /**
  1248. * ubifs_jnl_change_xattr - change an extended attribute.
  1249. * @c: UBIFS file-system description object
  1250. * @inode: extended attribute inode
  1251. * @host: host inode
  1252. *
  1253. * This function writes the updated version of an extended attribute inode and
  1254. * the host inode to the journal (to the base head). The host inode is written
  1255. * after the extended attribute inode in order to guarantee that the extended
  1256. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1257. * consequently, the write-buffer is synchronized. This function returns zero
  1258. * in case of success and a negative error code in case of failure.
  1259. */
  1260. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1261. const struct inode *host)
  1262. {
  1263. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1264. struct ubifs_inode *host_ui = ubifs_inode(host);
  1265. struct ubifs_ino_node *ino;
  1266. union ubifs_key key;
  1267. int sync = IS_DIRSYNC(host);
  1268. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1269. ubifs_assert(host->i_nlink > 0);
  1270. ubifs_assert(inode->i_nlink > 0);
  1271. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1272. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1273. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1274. aligned_len1 = ALIGN(len1, 8);
  1275. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1276. ino = kmalloc(aligned_len, GFP_NOFS);
  1277. if (!ino)
  1278. return -ENOMEM;
  1279. /* Make reservation before allocating sequence numbers */
  1280. err = make_reservation(c, BASEHD, aligned_len);
  1281. if (err)
  1282. goto out_free;
  1283. pack_inode(c, ino, host, 0);
  1284. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1285. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1286. if (!sync && !err) {
  1287. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1288. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1289. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1290. }
  1291. release_head(c, BASEHD);
  1292. if (err)
  1293. goto out_ro;
  1294. ino_key_init(c, &key, host->i_ino);
  1295. err = ubifs_tnc_add(c, &key, lnum, offs, len1);
  1296. if (err)
  1297. goto out_ro;
  1298. ino_key_init(c, &key, inode->i_ino);
  1299. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
  1300. if (err)
  1301. goto out_ro;
  1302. finish_reservation(c);
  1303. spin_lock(&host_ui->ui_lock);
  1304. host_ui->synced_i_size = host_ui->ui_size;
  1305. spin_unlock(&host_ui->ui_lock);
  1306. mark_inode_clean(c, host_ui);
  1307. kfree(ino);
  1308. return 0;
  1309. out_ro:
  1310. ubifs_ro_mode(c, err);
  1311. finish_reservation(c);
  1312. out_free:
  1313. kfree(ino);
  1314. return err;
  1315. }