journal.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation.
  6. *
  7. * Authors: Artem Bityutskiy (Битюцкий Артём)
  8. * Adrian Hunter
  9. */
  10. /*
  11. * This file implements UBIFS journal.
  12. *
  13. * The journal consists of 2 parts - the log and bud LEBs. The log has fixed
  14. * length and position, while a bud logical eraseblock is any LEB in the main
  15. * area. Buds contain file system data - data nodes, inode nodes, etc. The log
  16. * contains only references to buds and some other stuff like commit
  17. * start node. The idea is that when we commit the journal, we do
  18. * not copy the data, the buds just become indexed. Since after the commit the
  19. * nodes in bud eraseblocks become leaf nodes of the file system index tree, we
  20. * use term "bud". Analogy is obvious, bud eraseblocks contain nodes which will
  21. * become leafs in the future.
  22. *
  23. * The journal is multi-headed because we want to write data to the journal as
  24. * optimally as possible. It is nice to have nodes belonging to the same inode
  25. * in one LEB, so we may write data owned by different inodes to different
  26. * journal heads, although at present only one data head is used.
  27. *
  28. * For recovery reasons, the base head contains all inode nodes, all directory
  29. * entry nodes and all truncate nodes. This means that the other heads contain
  30. * only data nodes.
  31. *
  32. * Bud LEBs may be half-indexed. For example, if the bud was not full at the
  33. * time of commit, the bud is retained to continue to be used in the journal,
  34. * even though the "front" of the LEB is now indexed. In that case, the log
  35. * reference contains the offset where the bud starts for the purposes of the
  36. * journal.
  37. *
  38. * The journal size has to be limited, because the larger is the journal, the
  39. * longer it takes to mount UBIFS (scanning the journal) and the more memory it
  40. * takes (indexing in the TNC).
  41. *
  42. * All the journal write operations like 'ubifs_jnl_update()' here, which write
  43. * multiple UBIFS nodes to the journal at one go, are atomic with respect to
  44. * unclean reboots. Should the unclean reboot happen, the recovery code drops
  45. * all the nodes.
  46. */
  47. #include "ubifs.h"
  48. /**
  49. * zero_ino_node_unused - zero out unused fields of an on-flash inode node.
  50. * @ino: the inode to zero out
  51. */
  52. static inline void zero_ino_node_unused(struct ubifs_ino_node *ino)
  53. {
  54. memset(ino->padding1, 0, 4);
  55. memset(ino->padding2, 0, 26);
  56. }
  57. /**
  58. * zero_dent_node_unused - zero out unused fields of an on-flash directory
  59. * entry node.
  60. * @dent: the directory entry to zero out
  61. */
  62. static inline void zero_dent_node_unused(struct ubifs_dent_node *dent)
  63. {
  64. dent->padding1 = 0;
  65. }
  66. /**
  67. * zero_trun_node_unused - zero out unused fields of an on-flash truncation
  68. * node.
  69. * @trun: the truncation node to zero out
  70. */
  71. static inline void zero_trun_node_unused(struct ubifs_trun_node *trun)
  72. {
  73. memset(trun->padding, 0, 12);
  74. }
  75. static void ubifs_add_auth_dirt(struct ubifs_info *c, int lnum)
  76. {
  77. if (ubifs_authenticated(c))
  78. ubifs_add_dirt(c, lnum, ubifs_auth_node_sz(c));
  79. }
  80. /**
  81. * reserve_space - reserve space in the journal.
  82. * @c: UBIFS file-system description object
  83. * @jhead: journal head number
  84. * @len: node length
  85. *
  86. * This function reserves space in journal head @head. If the reservation
  87. * succeeded, the journal head stays locked and later has to be unlocked using
  88. * 'release_head()'. Returns zero in case of success, %-EAGAIN if commit has to
  89. * be done, and other negative error codes in case of other failures.
  90. */
  91. static int reserve_space(struct ubifs_info *c, int jhead, int len)
  92. {
  93. int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
  94. struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
  95. /*
  96. * Typically, the base head has smaller nodes written to it, so it is
  97. * better to try to allocate space at the ends of eraseblocks. This is
  98. * what the squeeze parameter does.
  99. */
  100. ubifs_assert(c, !c->ro_media && !c->ro_mount);
  101. squeeze = (jhead == BASEHD);
  102. again:
  103. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  104. if (c->ro_error) {
  105. err = -EROFS;
  106. goto out_unlock;
  107. }
  108. avail = c->leb_size - wbuf->offs - wbuf->used;
  109. if (wbuf->lnum != -1 && avail >= len)
  110. return 0;
  111. /*
  112. * Write buffer wasn't seek'ed or there is no enough space - look for an
  113. * LEB with some empty space.
  114. */
  115. lnum = ubifs_find_free_space(c, len, &offs, squeeze);
  116. if (lnum >= 0)
  117. goto out;
  118. err = lnum;
  119. if (err != -ENOSPC)
  120. goto out_unlock;
  121. /*
  122. * No free space, we have to run garbage collector to make
  123. * some. But the write-buffer mutex has to be unlocked because
  124. * GC also takes it.
  125. */
  126. dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
  127. mutex_unlock(&wbuf->io_mutex);
  128. lnum = ubifs_garbage_collect(c, 0);
  129. if (lnum < 0) {
  130. err = lnum;
  131. if (err != -ENOSPC)
  132. return err;
  133. /*
  134. * GC could not make a free LEB. But someone else may
  135. * have allocated new bud for this journal head,
  136. * because we dropped @wbuf->io_mutex, so try once
  137. * again.
  138. */
  139. dbg_jnl("GC couldn't make a free LEB for jhead %s",
  140. dbg_jhead(jhead));
  141. if (retries++ < 2) {
  142. dbg_jnl("retry (%d)", retries);
  143. goto again;
  144. }
  145. dbg_jnl("return -ENOSPC");
  146. return err;
  147. }
  148. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  149. dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
  150. avail = c->leb_size - wbuf->offs - wbuf->used;
  151. if (wbuf->lnum != -1 && avail >= len) {
  152. /*
  153. * Someone else has switched the journal head and we have
  154. * enough space now. This happens when more than one process is
  155. * trying to write to the same journal head at the same time.
  156. */
  157. dbg_jnl("return LEB %d back, already have LEB %d:%d",
  158. lnum, wbuf->lnum, wbuf->offs + wbuf->used);
  159. err = ubifs_return_leb(c, lnum);
  160. if (err)
  161. goto out_unlock;
  162. return 0;
  163. }
  164. offs = 0;
  165. out:
  166. /*
  167. * Make sure we synchronize the write-buffer before we add the new bud
  168. * to the log. Otherwise we may have a power cut after the log
  169. * reference node for the last bud (@lnum) is written but before the
  170. * write-buffer data are written to the next-to-last bud
  171. * (@wbuf->lnum). And the effect would be that the recovery would see
  172. * that there is corruption in the next-to-last bud.
  173. */
  174. err = ubifs_wbuf_sync_nolock(wbuf);
  175. if (err)
  176. goto out_return;
  177. err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
  178. if (err)
  179. goto out_return;
  180. err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
  181. if (err)
  182. goto out_unlock;
  183. return 0;
  184. out_unlock:
  185. mutex_unlock(&wbuf->io_mutex);
  186. return err;
  187. out_return:
  188. /* An error occurred and the LEB has to be returned to lprops */
  189. ubifs_assert(c, err < 0);
  190. err1 = ubifs_return_leb(c, lnum);
  191. if (err1 && err == -EAGAIN)
  192. /*
  193. * Return original error code only if it is not %-EAGAIN,
  194. * which is not really an error. Otherwise, return the error
  195. * code of 'ubifs_return_leb()'.
  196. */
  197. err = err1;
  198. mutex_unlock(&wbuf->io_mutex);
  199. return err;
  200. }
  201. static int ubifs_hash_nodes(struct ubifs_info *c, void *node,
  202. int len, struct shash_desc *hash)
  203. {
  204. int auth_node_size = ubifs_auth_node_sz(c);
  205. int err;
  206. while (1) {
  207. const struct ubifs_ch *ch = node;
  208. int nodelen = le32_to_cpu(ch->len);
  209. ubifs_assert(c, len >= auth_node_size);
  210. if (len == auth_node_size)
  211. break;
  212. ubifs_assert(c, len > nodelen);
  213. ubifs_assert(c, ch->magic == cpu_to_le32(UBIFS_NODE_MAGIC));
  214. err = ubifs_shash_update(c, hash, (void *)node, nodelen);
  215. if (err)
  216. return err;
  217. node += ALIGN(nodelen, 8);
  218. len -= ALIGN(nodelen, 8);
  219. }
  220. return ubifs_prepare_auth_node(c, node, hash);
  221. }
  222. /**
  223. * write_head - write data to a journal head.
  224. * @c: UBIFS file-system description object
  225. * @jhead: journal head
  226. * @buf: buffer to write
  227. * @len: length to write
  228. * @lnum: LEB number written is returned here
  229. * @offs: offset written is returned here
  230. * @sync: non-zero if the write-buffer has to by synchronized
  231. *
  232. * This function writes data to the reserved space of journal head @jhead.
  233. * Returns zero in case of success and a negative error code in case of
  234. * failure.
  235. */
  236. static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
  237. int *lnum, int *offs, int sync)
  238. {
  239. int err;
  240. struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
  241. ubifs_assert(c, jhead != GCHD);
  242. *lnum = c->jheads[jhead].wbuf.lnum;
  243. *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
  244. dbg_jnl("jhead %s, LEB %d:%d, len %d",
  245. dbg_jhead(jhead), *lnum, *offs, len);
  246. if (ubifs_authenticated(c)) {
  247. err = ubifs_hash_nodes(c, buf, len, c->jheads[jhead].log_hash);
  248. if (err)
  249. return err;
  250. }
  251. err = ubifs_wbuf_write_nolock(wbuf, buf, len);
  252. if (err)
  253. return err;
  254. if (sync)
  255. err = ubifs_wbuf_sync_nolock(wbuf);
  256. return err;
  257. }
  258. /**
  259. * __queue_and_wait - queue a task and wait until the task is waked up.
  260. * @c: UBIFS file-system description object
  261. *
  262. * This function adds current task in queue and waits until the task is waked
  263. * up. This function should be called with @c->reserve_space_wq locked.
  264. */
  265. static void __queue_and_wait(struct ubifs_info *c)
  266. {
  267. DEFINE_WAIT(wait);
  268. __add_wait_queue_entry_tail_exclusive(&c->reserve_space_wq, &wait);
  269. set_current_state(TASK_UNINTERRUPTIBLE);
  270. spin_unlock(&c->reserve_space_wq.lock);
  271. schedule();
  272. finish_wait(&c->reserve_space_wq, &wait);
  273. }
  274. /**
  275. * wait_for_reservation - try queuing current task to wait until waked up.
  276. * @c: UBIFS file-system description object
  277. *
  278. * This function queues current task to wait until waked up, if queuing is
  279. * started(@c->need_wait_space is not %0). Returns %true if current task is
  280. * added in queue, otherwise %false is returned.
  281. */
  282. static bool wait_for_reservation(struct ubifs_info *c)
  283. {
  284. if (likely(atomic_read(&c->need_wait_space) == 0))
  285. /* Quick path to check whether queuing is started. */
  286. return false;
  287. spin_lock(&c->reserve_space_wq.lock);
  288. if (atomic_read(&c->need_wait_space) == 0) {
  289. /* Queuing is not started, don't queue current task. */
  290. spin_unlock(&c->reserve_space_wq.lock);
  291. return false;
  292. }
  293. __queue_and_wait(c);
  294. return true;
  295. }
  296. /**
  297. * wake_up_reservation - wake up first task in queue or stop queuing.
  298. * @c: UBIFS file-system description object
  299. *
  300. * This function wakes up the first task in queue if it exists, or stops
  301. * queuing if no tasks in queue.
  302. */
  303. static void wake_up_reservation(struct ubifs_info *c)
  304. {
  305. spin_lock(&c->reserve_space_wq.lock);
  306. if (waitqueue_active(&c->reserve_space_wq))
  307. wake_up_locked(&c->reserve_space_wq);
  308. else
  309. /*
  310. * Compared with wait_for_reservation(), set @c->need_wait_space
  311. * under the protection of wait queue lock, which can avoid that
  312. * @c->need_wait_space is set to 0 after new task queued.
  313. */
  314. atomic_set(&c->need_wait_space, 0);
  315. spin_unlock(&c->reserve_space_wq.lock);
  316. }
  317. /**
  318. * add_or_start_queue - add current task in queue or start queuing.
  319. * @c: UBIFS file-system description object
  320. *
  321. * This function starts queuing if queuing is not started, otherwise adds
  322. * current task in queue.
  323. */
  324. static void add_or_start_queue(struct ubifs_info *c)
  325. {
  326. spin_lock(&c->reserve_space_wq.lock);
  327. if (atomic_cmpxchg(&c->need_wait_space, 0, 1) == 0) {
  328. /* Starts queuing, task can go on directly. */
  329. spin_unlock(&c->reserve_space_wq.lock);
  330. return;
  331. }
  332. /*
  333. * There are at least two tasks have retried more than 32 times
  334. * at certain point, first task has started queuing, just queue
  335. * the left tasks.
  336. */
  337. __queue_and_wait(c);
  338. }
  339. /**
  340. * make_reservation - reserve journal space.
  341. * @c: UBIFS file-system description object
  342. * @jhead: journal head
  343. * @len: how many bytes to reserve
  344. *
  345. * This function makes space reservation in journal head @jhead. The function
  346. * takes the commit lock and locks the journal head, and the caller has to
  347. * unlock the head and finish the reservation with 'finish_reservation()'.
  348. * Returns zero in case of success and a negative error code in case of
  349. * failure.
  350. *
  351. * Note, the journal head may be unlocked as soon as the data is written, while
  352. * the commit lock has to be released after the data has been added to the
  353. * TNC.
  354. */
  355. static int make_reservation(struct ubifs_info *c, int jhead, int len)
  356. {
  357. int err, cmt_retries = 0, nospc_retries = 0;
  358. bool blocked = wait_for_reservation(c);
  359. again:
  360. down_read(&c->commit_sem);
  361. err = reserve_space(c, jhead, len);
  362. if (!err) {
  363. /* c->commit_sem will get released via finish_reservation(). */
  364. goto out_wake_up;
  365. }
  366. up_read(&c->commit_sem);
  367. if (err == -ENOSPC) {
  368. /*
  369. * GC could not make any progress. We should try to commit
  370. * because it could make some dirty space and GC would make
  371. * progress, so make the error -EAGAIN so that the below
  372. * will commit and re-try.
  373. */
  374. nospc_retries++;
  375. dbg_jnl("no space, retry");
  376. err = -EAGAIN;
  377. }
  378. if (err != -EAGAIN)
  379. goto out;
  380. /*
  381. * -EAGAIN means that the journal is full or too large, or the above
  382. * code wants to do one commit. Do this and re-try.
  383. */
  384. if (cmt_retries > 128) {
  385. /*
  386. * This should not happen unless:
  387. * 1. The journal size limitations are too tough.
  388. * 2. The budgeting is incorrect. We always have to be able to
  389. * write to the media, because all operations are budgeted.
  390. * Deletions are not budgeted, though, but we reserve an
  391. * extra LEB for them.
  392. */
  393. ubifs_err(c, "stuck in space allocation, nospc_retries %d",
  394. nospc_retries);
  395. err = -ENOSPC;
  396. goto out;
  397. } else if (cmt_retries > 32) {
  398. /*
  399. * It's almost impossible to happen, unless there are many tasks
  400. * making reservation concurrently and someone task has retried
  401. * gc + commit for many times, generated available space during
  402. * this period are grabbed by other tasks.
  403. * But if it happens, start queuing up all tasks that will make
  404. * space reservation, then there is only one task making space
  405. * reservation at any time, and it can always make success under
  406. * the premise of correct budgeting.
  407. */
  408. ubifs_warn(c, "too many space allocation cmt_retries (%d) "
  409. "nospc_retries (%d), start queuing tasks",
  410. cmt_retries, nospc_retries);
  411. if (!blocked) {
  412. blocked = true;
  413. add_or_start_queue(c);
  414. }
  415. }
  416. dbg_jnl("-EAGAIN, commit and retry (retried %d times)",
  417. cmt_retries);
  418. cmt_retries += 1;
  419. err = ubifs_run_commit(c);
  420. if (err)
  421. goto out_wake_up;
  422. goto again;
  423. out:
  424. ubifs_err(c, "cannot reserve %d bytes in jhead %d, error %d",
  425. len, jhead, err);
  426. if (err == -ENOSPC) {
  427. /* This are some budgeting problems, print useful information */
  428. down_write(&c->commit_sem);
  429. dump_stack();
  430. ubifs_dump_budg(c, &c->bi);
  431. ubifs_dump_lprops(c);
  432. cmt_retries = dbg_check_lprops(c);
  433. up_write(&c->commit_sem);
  434. }
  435. out_wake_up:
  436. if (blocked) {
  437. /*
  438. * Only tasks that have ever started queuing or ever been queued
  439. * can wake up other queued tasks, which can make sure that
  440. * there is only one task waked up to make space reservation.
  441. * For example:
  442. * task A task B task C
  443. * make_reservation make_reservation
  444. * reserve_space // 0
  445. * wake_up_reservation
  446. * atomic_cmpxchg // 0, start queuing
  447. * reserve_space
  448. * wait_for_reservation
  449. * __queue_and_wait
  450. * add_wait_queue
  451. * if (blocked) // false
  452. * // So that task C won't be waked up to race with task B
  453. */
  454. wake_up_reservation(c);
  455. }
  456. return err;
  457. }
  458. /**
  459. * release_head - release a journal head.
  460. * @c: UBIFS file-system description object
  461. * @jhead: journal head
  462. *
  463. * This function releases journal head @jhead which was locked by
  464. * the 'make_reservation()' function. It has to be called after each successful
  465. * 'make_reservation()' invocation.
  466. */
  467. static inline void release_head(struct ubifs_info *c, int jhead)
  468. {
  469. mutex_unlock(&c->jheads[jhead].wbuf.io_mutex);
  470. }
  471. /**
  472. * finish_reservation - finish a reservation.
  473. * @c: UBIFS file-system description object
  474. *
  475. * This function finishes journal space reservation. It must be called after
  476. * 'make_reservation()'.
  477. */
  478. static void finish_reservation(struct ubifs_info *c)
  479. {
  480. up_read(&c->commit_sem);
  481. }
  482. /**
  483. * get_dent_type - translate VFS inode mode to UBIFS directory entry type.
  484. * @mode: inode mode
  485. */
  486. static int get_dent_type(int mode)
  487. {
  488. switch (mode & S_IFMT) {
  489. case S_IFREG:
  490. return UBIFS_ITYPE_REG;
  491. case S_IFDIR:
  492. return UBIFS_ITYPE_DIR;
  493. case S_IFLNK:
  494. return UBIFS_ITYPE_LNK;
  495. case S_IFBLK:
  496. return UBIFS_ITYPE_BLK;
  497. case S_IFCHR:
  498. return UBIFS_ITYPE_CHR;
  499. case S_IFIFO:
  500. return UBIFS_ITYPE_FIFO;
  501. case S_IFSOCK:
  502. return UBIFS_ITYPE_SOCK;
  503. default:
  504. BUG();
  505. }
  506. return 0;
  507. }
  508. /**
  509. * pack_inode - pack an inode node.
  510. * @c: UBIFS file-system description object
  511. * @ino: buffer in which to pack inode node
  512. * @inode: inode to pack
  513. * @last: indicates the last node of the group
  514. */
  515. static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
  516. const struct inode *inode, int last)
  517. {
  518. int data_len = 0, last_reference = !inode->i_nlink;
  519. struct ubifs_inode *ui = ubifs_inode(inode);
  520. ino->ch.node_type = UBIFS_INO_NODE;
  521. ino_key_init_flash(c, &ino->key, inode->i_ino);
  522. ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
  523. ino->atime_sec = cpu_to_le64(inode_get_atime_sec(inode));
  524. ino->atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
  525. ino->ctime_sec = cpu_to_le64(inode_get_ctime_sec(inode));
  526. ino->ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
  527. ino->mtime_sec = cpu_to_le64(inode_get_mtime_sec(inode));
  528. ino->mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
  529. ino->uid = cpu_to_le32(i_uid_read(inode));
  530. ino->gid = cpu_to_le32(i_gid_read(inode));
  531. ino->mode = cpu_to_le32(inode->i_mode);
  532. ino->flags = cpu_to_le32(ui->flags);
  533. ino->size = cpu_to_le64(ui->ui_size);
  534. ino->nlink = cpu_to_le32(inode->i_nlink);
  535. ino->compr_type = cpu_to_le16(ui->compr_type);
  536. ino->data_len = cpu_to_le32(ui->data_len);
  537. ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
  538. ino->xattr_size = cpu_to_le32(ui->xattr_size);
  539. ino->xattr_names = cpu_to_le32(ui->xattr_names);
  540. zero_ino_node_unused(ino);
  541. /*
  542. * Drop the attached data if this is a deletion inode, the data is not
  543. * needed anymore.
  544. */
  545. if (!last_reference) {
  546. memcpy(ino->data, ui->data, ui->data_len);
  547. data_len = ui->data_len;
  548. }
  549. ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
  550. }
  551. /**
  552. * mark_inode_clean - mark UBIFS inode as clean.
  553. * @c: UBIFS file-system description object
  554. * @ui: UBIFS inode to mark as clean
  555. *
  556. * This helper function marks UBIFS inode @ui as clean by cleaning the
  557. * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
  558. * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
  559. * just do nothing.
  560. */
  561. static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
  562. {
  563. if (ui->dirty)
  564. ubifs_release_dirty_inode_budget(c, ui);
  565. ui->dirty = 0;
  566. }
  567. static void set_dent_cookie(struct ubifs_info *c, struct ubifs_dent_node *dent)
  568. {
  569. if (c->double_hash)
  570. dent->cookie = (__force __le32) get_random_u32();
  571. else
  572. dent->cookie = 0;
  573. }
  574. /**
  575. * ubifs_jnl_update - update inode.
  576. * @c: UBIFS file-system description object
  577. * @dir: parent inode or host inode in case of extended attributes
  578. * @nm: directory entry name
  579. * @inode: inode to update
  580. * @deletion: indicates a directory entry deletion i.e unlink or rmdir
  581. * @xent: non-zero if the directory entry is an extended attribute entry
  582. * @in_orphan: indicates whether the @inode is in orphan list
  583. *
  584. * This function updates an inode by writing a directory entry (or extended
  585. * attribute entry), the inode itself, and the parent directory inode (or the
  586. * host inode) to the journal.
  587. *
  588. * The function writes the host inode @dir last, which is important in case of
  589. * extended attributes. Indeed, then we guarantee that if the host inode gets
  590. * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
  591. * the extended attribute inode gets flushed too. And this is exactly what the
  592. * user expects - synchronizing the host inode synchronizes its extended
  593. * attributes. Similarly, this guarantees that if @dir is synchronized, its
  594. * directory entry corresponding to @nm gets synchronized too.
  595. *
  596. * If the inode (@inode) or the parent directory (@dir) are synchronous, this
  597. * function synchronizes the write-buffer.
  598. *
  599. * This function marks the @dir and @inode inodes as clean and returns zero on
  600. * success. In case of failure, a negative error code is returned.
  601. */
  602. int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
  603. const struct fscrypt_name *nm, const struct inode *inode,
  604. int deletion, int xent, int in_orphan)
  605. {
  606. int err, dlen, ilen, len, lnum, ino_offs, dent_offs, orphan_added = 0;
  607. int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
  608. int last_reference = !!(deletion && inode->i_nlink == 0);
  609. struct ubifs_inode *ui = ubifs_inode(inode);
  610. struct ubifs_inode *host_ui = ubifs_inode(dir);
  611. struct ubifs_dent_node *dent;
  612. struct ubifs_ino_node *ino;
  613. union ubifs_key dent_key, ino_key;
  614. u8 hash_dent[UBIFS_HASH_ARR_SZ];
  615. u8 hash_ino[UBIFS_HASH_ARR_SZ];
  616. u8 hash_ino_host[UBIFS_HASH_ARR_SZ];
  617. ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
  618. dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  619. ilen = UBIFS_INO_NODE_SZ;
  620. /*
  621. * If the last reference to the inode is being deleted, then there is
  622. * no need to attach and write inode data, it is being deleted anyway.
  623. * And if the inode is being deleted, no need to synchronize
  624. * write-buffer even if the inode is synchronous.
  625. */
  626. if (!last_reference) {
  627. ilen += ui->data_len;
  628. sync |= IS_SYNC(inode);
  629. }
  630. aligned_dlen = ALIGN(dlen, 8);
  631. aligned_ilen = ALIGN(ilen, 8);
  632. len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
  633. /* Make sure to also account for extended attributes */
  634. if (ubifs_authenticated(c))
  635. len += ALIGN(host_ui->data_len, 8) + ubifs_auth_node_sz(c);
  636. else
  637. len += host_ui->data_len;
  638. dent = kzalloc(len, GFP_NOFS);
  639. if (!dent)
  640. return -ENOMEM;
  641. /* Make reservation before allocating sequence numbers */
  642. err = make_reservation(c, BASEHD, len);
  643. if (err)
  644. goto out_free;
  645. if (!xent) {
  646. dent->ch.node_type = UBIFS_DENT_NODE;
  647. if (fname_name(nm) == NULL)
  648. dent_key_init_hash(c, &dent_key, dir->i_ino, nm->hash);
  649. else
  650. dent_key_init(c, &dent_key, dir->i_ino, nm);
  651. } else {
  652. dent->ch.node_type = UBIFS_XENT_NODE;
  653. xent_key_init(c, &dent_key, dir->i_ino, nm);
  654. }
  655. key_write(c, &dent_key, dent->key);
  656. dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
  657. dent->type = get_dent_type(inode->i_mode);
  658. dent->nlen = cpu_to_le16(fname_len(nm));
  659. memcpy(dent->name, fname_name(nm), fname_len(nm));
  660. dent->name[fname_len(nm)] = '\0';
  661. set_dent_cookie(c, dent);
  662. zero_dent_node_unused(dent);
  663. ubifs_prep_grp_node(c, dent, dlen, 0);
  664. err = ubifs_node_calc_hash(c, dent, hash_dent);
  665. if (err)
  666. goto out_release;
  667. ino = (void *)dent + aligned_dlen;
  668. pack_inode(c, ino, inode, 0);
  669. err = ubifs_node_calc_hash(c, ino, hash_ino);
  670. if (err)
  671. goto out_release;
  672. ino = (void *)ino + aligned_ilen;
  673. pack_inode(c, ino, dir, 1);
  674. err = ubifs_node_calc_hash(c, ino, hash_ino_host);
  675. if (err)
  676. goto out_release;
  677. if (last_reference && !in_orphan) {
  678. err = ubifs_add_orphan(c, inode->i_ino);
  679. if (err) {
  680. release_head(c, BASEHD);
  681. goto out_finish;
  682. }
  683. ui->del_cmtno = c->cmt_no;
  684. orphan_added = 1;
  685. }
  686. err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
  687. if (err)
  688. goto out_release;
  689. if (!sync) {
  690. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  691. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  692. ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
  693. }
  694. release_head(c, BASEHD);
  695. kfree(dent);
  696. ubifs_add_auth_dirt(c, lnum);
  697. if (deletion) {
  698. if (fname_name(nm) == NULL)
  699. err = ubifs_tnc_remove_dh(c, &dent_key, nm->minor_hash);
  700. else
  701. err = ubifs_tnc_remove_nm(c, &dent_key, nm);
  702. if (err)
  703. goto out_ro;
  704. err = ubifs_add_dirt(c, lnum, dlen);
  705. } else
  706. err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen,
  707. hash_dent, nm);
  708. if (err)
  709. goto out_ro;
  710. /*
  711. * Note, we do not remove the inode from TNC even if the last reference
  712. * to it has just been deleted, because the inode may still be opened.
  713. * Instead, the inode has been added to orphan lists and the orphan
  714. * subsystem will take further care about it.
  715. */
  716. ino_key_init(c, &ino_key, inode->i_ino);
  717. ino_offs = dent_offs + aligned_dlen;
  718. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen, hash_ino);
  719. if (err)
  720. goto out_ro;
  721. ino_key_init(c, &ino_key, dir->i_ino);
  722. ino_offs += aligned_ilen;
  723. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
  724. UBIFS_INO_NODE_SZ + host_ui->data_len, hash_ino_host);
  725. if (err)
  726. goto out_ro;
  727. if (in_orphan && inode->i_nlink)
  728. ubifs_delete_orphan(c, inode->i_ino);
  729. finish_reservation(c);
  730. spin_lock(&ui->ui_lock);
  731. ui->synced_i_size = ui->ui_size;
  732. spin_unlock(&ui->ui_lock);
  733. if (xent) {
  734. spin_lock(&host_ui->ui_lock);
  735. host_ui->synced_i_size = host_ui->ui_size;
  736. spin_unlock(&host_ui->ui_lock);
  737. }
  738. mark_inode_clean(c, ui);
  739. mark_inode_clean(c, host_ui);
  740. return 0;
  741. out_finish:
  742. finish_reservation(c);
  743. out_free:
  744. kfree(dent);
  745. return err;
  746. out_release:
  747. release_head(c, BASEHD);
  748. kfree(dent);
  749. out_ro:
  750. ubifs_ro_mode(c, err);
  751. if (orphan_added)
  752. ubifs_delete_orphan(c, inode->i_ino);
  753. finish_reservation(c);
  754. return err;
  755. }
  756. /**
  757. * ubifs_jnl_write_data - write a data node to the journal.
  758. * @c: UBIFS file-system description object
  759. * @inode: inode the data node belongs to
  760. * @key: node key
  761. * @buf: buffer to write
  762. * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
  763. *
  764. * This function writes a data node to the journal. Returns %0 if the data node
  765. * was successfully written, and a negative error code in case of failure.
  766. */
  767. int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
  768. const union ubifs_key *key, const void *buf, int len)
  769. {
  770. struct ubifs_data_node *data;
  771. int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
  772. int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
  773. int write_len;
  774. struct ubifs_inode *ui = ubifs_inode(inode);
  775. bool encrypted = IS_ENCRYPTED(inode);
  776. u8 hash[UBIFS_HASH_ARR_SZ];
  777. dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
  778. (unsigned long)key_inum(c, key), key_block(c, key), len);
  779. ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
  780. if (encrypted)
  781. dlen += UBIFS_CIPHER_BLOCK_SIZE;
  782. auth_len = ubifs_auth_node_sz(c);
  783. data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
  784. if (!data) {
  785. /*
  786. * Fall-back to the write reserve buffer. Note, we might be
  787. * currently on the memory reclaim path, when the kernel is
  788. * trying to free some memory by writing out dirty pages. The
  789. * write reserve buffer helps us to guarantee that we are
  790. * always able to write the data.
  791. */
  792. allocated = 0;
  793. mutex_lock(&c->write_reserve_mutex);
  794. data = c->write_reserve_buf;
  795. }
  796. data->ch.node_type = UBIFS_DATA_NODE;
  797. key_write(c, key, &data->key);
  798. data->size = cpu_to_le32(len);
  799. if (!(ui->flags & UBIFS_COMPR_FL))
  800. /* Compression is disabled for this inode */
  801. compr_type = UBIFS_COMPR_NONE;
  802. else
  803. compr_type = ui->compr_type;
  804. out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
  805. ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
  806. ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
  807. if (encrypted) {
  808. err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
  809. if (err)
  810. goto out_free;
  811. } else {
  812. data->compr_size = 0;
  813. out_len = compr_len;
  814. }
  815. dlen = UBIFS_DATA_NODE_SZ + out_len;
  816. if (ubifs_authenticated(c))
  817. write_len = ALIGN(dlen, 8) + auth_len;
  818. else
  819. write_len = dlen;
  820. data->compr_type = cpu_to_le16(compr_type);
  821. /* Make reservation before allocating sequence numbers */
  822. err = make_reservation(c, DATAHD, write_len);
  823. if (err)
  824. goto out_free;
  825. ubifs_prepare_node(c, data, dlen, 0);
  826. err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
  827. if (err)
  828. goto out_release;
  829. err = ubifs_node_calc_hash(c, data, hash);
  830. if (err)
  831. goto out_release;
  832. ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
  833. release_head(c, DATAHD);
  834. ubifs_add_auth_dirt(c, lnum);
  835. err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
  836. if (err)
  837. goto out_ro;
  838. finish_reservation(c);
  839. if (!allocated)
  840. mutex_unlock(&c->write_reserve_mutex);
  841. else
  842. kfree(data);
  843. return 0;
  844. out_release:
  845. release_head(c, DATAHD);
  846. out_ro:
  847. ubifs_ro_mode(c, err);
  848. finish_reservation(c);
  849. out_free:
  850. if (!allocated)
  851. mutex_unlock(&c->write_reserve_mutex);
  852. else
  853. kfree(data);
  854. return err;
  855. }
  856. /**
  857. * ubifs_jnl_write_inode - flush inode to the journal.
  858. * @c: UBIFS file-system description object
  859. * @inode: inode to flush
  860. *
  861. * This function writes inode @inode to the journal. If the inode is
  862. * synchronous, it also synchronizes the write-buffer. Returns zero in case of
  863. * success and a negative error code in case of failure.
  864. */
  865. int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
  866. {
  867. int err, lnum, offs;
  868. struct ubifs_ino_node *ino, *ino_start;
  869. struct ubifs_inode *ui = ubifs_inode(inode);
  870. int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
  871. int last_reference = !inode->i_nlink;
  872. int kill_xattrs = ui->xattr_cnt && last_reference;
  873. u8 hash[UBIFS_HASH_ARR_SZ];
  874. dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
  875. /*
  876. * If the inode is being deleted, do not write the attached data. No
  877. * need to synchronize the write-buffer either.
  878. */
  879. if (!last_reference) {
  880. ilen += ui->data_len;
  881. sync = IS_SYNC(inode);
  882. } else if (kill_xattrs) {
  883. write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
  884. }
  885. if (ubifs_authenticated(c))
  886. write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
  887. else
  888. write_len += ilen;
  889. ino_start = ino = kmalloc(write_len, GFP_NOFS);
  890. if (!ino)
  891. return -ENOMEM;
  892. /* Make reservation before allocating sequence numbers */
  893. err = make_reservation(c, BASEHD, write_len);
  894. if (err)
  895. goto out_free;
  896. if (kill_xattrs) {
  897. union ubifs_key key;
  898. struct fscrypt_name nm = {0};
  899. struct inode *xino;
  900. struct ubifs_dent_node *xent, *pxent = NULL;
  901. if (ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
  902. err = -EPERM;
  903. ubifs_err(c, "Cannot delete inode, it has too much xattrs!");
  904. goto out_release;
  905. }
  906. lowest_xent_key(c, &key, inode->i_ino);
  907. while (1) {
  908. xent = ubifs_tnc_next_ent(c, &key, &nm);
  909. if (IS_ERR(xent)) {
  910. err = PTR_ERR(xent);
  911. if (err == -ENOENT)
  912. break;
  913. kfree(pxent);
  914. goto out_release;
  915. }
  916. fname_name(&nm) = xent->name;
  917. fname_len(&nm) = le16_to_cpu(xent->nlen);
  918. xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
  919. if (IS_ERR(xino)) {
  920. err = PTR_ERR(xino);
  921. ubifs_err(c, "dead directory entry '%s', error %d",
  922. xent->name, err);
  923. ubifs_ro_mode(c, err);
  924. kfree(pxent);
  925. kfree(xent);
  926. goto out_release;
  927. }
  928. ubifs_assert(c, ubifs_inode(xino)->xattr);
  929. clear_nlink(xino);
  930. pack_inode(c, ino, xino, 0);
  931. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  932. iput(xino);
  933. kfree(pxent);
  934. pxent = xent;
  935. key_read(c, &xent->key, &key);
  936. }
  937. kfree(pxent);
  938. }
  939. pack_inode(c, ino, inode, 1);
  940. err = ubifs_node_calc_hash(c, ino, hash);
  941. if (err)
  942. goto out_release;
  943. err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
  944. if (err)
  945. goto out_release;
  946. if (!sync)
  947. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  948. inode->i_ino);
  949. release_head(c, BASEHD);
  950. if (last_reference) {
  951. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  952. if (err)
  953. goto out_ro;
  954. ubifs_delete_orphan(c, inode->i_ino);
  955. err = ubifs_add_dirt(c, lnum, write_len);
  956. } else {
  957. union ubifs_key key;
  958. ubifs_add_auth_dirt(c, lnum);
  959. ino_key_init(c, &key, inode->i_ino);
  960. err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
  961. }
  962. if (err)
  963. goto out_ro;
  964. finish_reservation(c);
  965. spin_lock(&ui->ui_lock);
  966. ui->synced_i_size = ui->ui_size;
  967. spin_unlock(&ui->ui_lock);
  968. kfree(ino_start);
  969. return 0;
  970. out_release:
  971. release_head(c, BASEHD);
  972. out_ro:
  973. ubifs_ro_mode(c, err);
  974. finish_reservation(c);
  975. out_free:
  976. kfree(ino_start);
  977. return err;
  978. }
  979. /**
  980. * ubifs_jnl_delete_inode - delete an inode.
  981. * @c: UBIFS file-system description object
  982. * @inode: inode to delete
  983. *
  984. * This function deletes inode @inode which includes removing it from orphans,
  985. * deleting it from TNC and, in some cases, writing a deletion inode to the
  986. * journal.
  987. *
  988. * When regular file inodes are unlinked or a directory inode is removed, the
  989. * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
  990. * direntry to the media, and adds the inode to orphans. After this, when the
  991. * last reference to this inode has been dropped, this function is called. In
  992. * general, it has to write one more deletion inode to the media, because if
  993. * a commit happened between 'ubifs_jnl_update()' and
  994. * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
  995. * anymore, and in fact it might not be on the flash anymore, because it might
  996. * have been garbage-collected already. And for optimization reasons UBIFS does
  997. * not read the orphan area if it has been unmounted cleanly, so it would have
  998. * no indication in the journal that there is a deleted inode which has to be
  999. * removed from TNC.
  1000. *
  1001. * However, if there was no commit between 'ubifs_jnl_update()' and
  1002. * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
  1003. * inode to the media for the second time. And this is quite a typical case.
  1004. *
  1005. * This function returns zero in case of success and a negative error code in
  1006. * case of failure.
  1007. */
  1008. int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
  1009. {
  1010. int err;
  1011. struct ubifs_inode *ui = ubifs_inode(inode);
  1012. ubifs_assert(c, inode->i_nlink == 0);
  1013. if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
  1014. /* A commit happened for sure or inode hosts xattrs */
  1015. return ubifs_jnl_write_inode(c, inode);
  1016. down_read(&c->commit_sem);
  1017. /*
  1018. * Check commit number again, because the first test has been done
  1019. * without @c->commit_sem, so a commit might have happened.
  1020. */
  1021. if (ui->del_cmtno != c->cmt_no) {
  1022. up_read(&c->commit_sem);
  1023. return ubifs_jnl_write_inode(c, inode);
  1024. }
  1025. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  1026. if (err)
  1027. ubifs_ro_mode(c, err);
  1028. else
  1029. ubifs_delete_orphan(c, inode->i_ino);
  1030. up_read(&c->commit_sem);
  1031. return err;
  1032. }
  1033. /**
  1034. * ubifs_jnl_xrename - cross rename two directory entries.
  1035. * @c: UBIFS file-system description object
  1036. * @fst_dir: parent inode of 1st directory entry to exchange
  1037. * @fst_inode: 1st inode to exchange
  1038. * @fst_nm: name of 1st inode to exchange
  1039. * @snd_dir: parent inode of 2nd directory entry to exchange
  1040. * @snd_inode: 2nd inode to exchange
  1041. * @snd_nm: name of 2nd inode to exchange
  1042. * @sync: non-zero if the write-buffer has to be synchronized
  1043. *
  1044. * This function implements the cross rename operation which may involve
  1045. * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
  1046. * and returns zero on success. In case of failure, a negative error code is
  1047. * returned.
  1048. */
  1049. int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  1050. const struct inode *fst_inode,
  1051. const struct fscrypt_name *fst_nm,
  1052. const struct inode *snd_dir,
  1053. const struct inode *snd_inode,
  1054. const struct fscrypt_name *snd_nm, int sync)
  1055. {
  1056. union ubifs_key key;
  1057. struct ubifs_dent_node *dent1, *dent2;
  1058. int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
  1059. int aligned_dlen1, aligned_dlen2;
  1060. int twoparents = (fst_dir != snd_dir);
  1061. void *p;
  1062. u8 hash_dent1[UBIFS_HASH_ARR_SZ];
  1063. u8 hash_dent2[UBIFS_HASH_ARR_SZ];
  1064. u8 hash_p1[UBIFS_HASH_ARR_SZ];
  1065. u8 hash_p2[UBIFS_HASH_ARR_SZ];
  1066. ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
  1067. ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
  1068. ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
  1069. ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
  1070. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
  1071. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
  1072. aligned_dlen1 = ALIGN(dlen1, 8);
  1073. aligned_dlen2 = ALIGN(dlen2, 8);
  1074. len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
  1075. if (twoparents)
  1076. len += plen;
  1077. len += ubifs_auth_node_sz(c);
  1078. dent1 = kzalloc(len, GFP_NOFS);
  1079. if (!dent1)
  1080. return -ENOMEM;
  1081. /* Make reservation before allocating sequence numbers */
  1082. err = make_reservation(c, BASEHD, len);
  1083. if (err)
  1084. goto out_free;
  1085. /* Make new dent for 1st entry */
  1086. dent1->ch.node_type = UBIFS_DENT_NODE;
  1087. dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
  1088. dent1->inum = cpu_to_le64(fst_inode->i_ino);
  1089. dent1->type = get_dent_type(fst_inode->i_mode);
  1090. dent1->nlen = cpu_to_le16(fname_len(snd_nm));
  1091. memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
  1092. dent1->name[fname_len(snd_nm)] = '\0';
  1093. set_dent_cookie(c, dent1);
  1094. zero_dent_node_unused(dent1);
  1095. ubifs_prep_grp_node(c, dent1, dlen1, 0);
  1096. err = ubifs_node_calc_hash(c, dent1, hash_dent1);
  1097. if (err)
  1098. goto out_release;
  1099. /* Make new dent for 2nd entry */
  1100. dent2 = (void *)dent1 + aligned_dlen1;
  1101. dent2->ch.node_type = UBIFS_DENT_NODE;
  1102. dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
  1103. dent2->inum = cpu_to_le64(snd_inode->i_ino);
  1104. dent2->type = get_dent_type(snd_inode->i_mode);
  1105. dent2->nlen = cpu_to_le16(fname_len(fst_nm));
  1106. memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
  1107. dent2->name[fname_len(fst_nm)] = '\0';
  1108. set_dent_cookie(c, dent2);
  1109. zero_dent_node_unused(dent2);
  1110. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1111. err = ubifs_node_calc_hash(c, dent2, hash_dent2);
  1112. if (err)
  1113. goto out_release;
  1114. p = (void *)dent2 + aligned_dlen2;
  1115. if (!twoparents) {
  1116. pack_inode(c, p, fst_dir, 1);
  1117. err = ubifs_node_calc_hash(c, p, hash_p1);
  1118. if (err)
  1119. goto out_release;
  1120. } else {
  1121. pack_inode(c, p, fst_dir, 0);
  1122. err = ubifs_node_calc_hash(c, p, hash_p1);
  1123. if (err)
  1124. goto out_release;
  1125. p += ALIGN(plen, 8);
  1126. pack_inode(c, p, snd_dir, 1);
  1127. err = ubifs_node_calc_hash(c, p, hash_p2);
  1128. if (err)
  1129. goto out_release;
  1130. }
  1131. err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
  1132. if (err)
  1133. goto out_release;
  1134. if (!sync) {
  1135. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1136. ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
  1137. ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
  1138. }
  1139. release_head(c, BASEHD);
  1140. ubifs_add_auth_dirt(c, lnum);
  1141. dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
  1142. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
  1143. if (err)
  1144. goto out_ro;
  1145. offs += aligned_dlen1;
  1146. dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
  1147. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
  1148. if (err)
  1149. goto out_ro;
  1150. offs += aligned_dlen2;
  1151. ino_key_init(c, &key, fst_dir->i_ino);
  1152. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
  1153. if (err)
  1154. goto out_ro;
  1155. if (twoparents) {
  1156. offs += ALIGN(plen, 8);
  1157. ino_key_init(c, &key, snd_dir->i_ino);
  1158. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
  1159. if (err)
  1160. goto out_ro;
  1161. }
  1162. finish_reservation(c);
  1163. mark_inode_clean(c, ubifs_inode(fst_dir));
  1164. if (twoparents)
  1165. mark_inode_clean(c, ubifs_inode(snd_dir));
  1166. kfree(dent1);
  1167. return 0;
  1168. out_release:
  1169. release_head(c, BASEHD);
  1170. out_ro:
  1171. ubifs_ro_mode(c, err);
  1172. finish_reservation(c);
  1173. out_free:
  1174. kfree(dent1);
  1175. return err;
  1176. }
  1177. /**
  1178. * ubifs_jnl_rename - rename a directory entry.
  1179. * @c: UBIFS file-system description object
  1180. * @old_dir: parent inode of directory entry to rename
  1181. * @old_inode: directory entry's inode to rename
  1182. * @old_nm: name of the old directory entry to rename
  1183. * @new_dir: parent inode of directory entry to rename
  1184. * @new_inode: new directory entry's inode (or directory entry's inode to
  1185. * replace)
  1186. * @new_nm: new name of the new directory entry
  1187. * @whiteout: whiteout inode
  1188. * @sync: non-zero if the write-buffer has to be synchronized
  1189. * @delete_orphan: indicates an orphan entry deletion for @whiteout
  1190. *
  1191. * This function implements the re-name operation which may involve writing up
  1192. * to 4 inodes(new inode, whiteout inode, old and new parent directory inodes)
  1193. * and 2 directory entries. It marks the written inodes as clean and returns
  1194. * zero on success. In case of failure, a negative error code is returned.
  1195. */
  1196. int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
  1197. const struct inode *old_inode,
  1198. const struct fscrypt_name *old_nm,
  1199. const struct inode *new_dir,
  1200. const struct inode *new_inode,
  1201. const struct fscrypt_name *new_nm,
  1202. const struct inode *whiteout, int sync, int delete_orphan)
  1203. {
  1204. void *p;
  1205. union ubifs_key key;
  1206. struct ubifs_dent_node *dent, *dent2;
  1207. int err, dlen1, dlen2, ilen, wlen, lnum, offs, len, orphan_added = 0;
  1208. int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
  1209. int last_reference = !!(new_inode && new_inode->i_nlink == 0);
  1210. int move = (old_dir != new_dir);
  1211. struct ubifs_inode *new_ui, *whiteout_ui;
  1212. u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
  1213. u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
  1214. u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
  1215. u8 hash_whiteout_inode[UBIFS_HASH_ARR_SZ];
  1216. u8 hash_dent1[UBIFS_HASH_ARR_SZ];
  1217. u8 hash_dent2[UBIFS_HASH_ARR_SZ];
  1218. ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
  1219. ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
  1220. ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
  1221. ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
  1222. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
  1223. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
  1224. if (new_inode) {
  1225. new_ui = ubifs_inode(new_inode);
  1226. ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
  1227. ilen = UBIFS_INO_NODE_SZ;
  1228. if (!last_reference)
  1229. ilen += new_ui->data_len;
  1230. } else
  1231. ilen = 0;
  1232. if (whiteout) {
  1233. whiteout_ui = ubifs_inode(whiteout);
  1234. ubifs_assert(c, mutex_is_locked(&whiteout_ui->ui_mutex));
  1235. ubifs_assert(c, whiteout->i_nlink == 1);
  1236. ubifs_assert(c, !whiteout_ui->dirty);
  1237. wlen = UBIFS_INO_NODE_SZ;
  1238. wlen += whiteout_ui->data_len;
  1239. } else
  1240. wlen = 0;
  1241. aligned_dlen1 = ALIGN(dlen1, 8);
  1242. aligned_dlen2 = ALIGN(dlen2, 8);
  1243. len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) +
  1244. ALIGN(wlen, 8) + ALIGN(plen, 8);
  1245. if (move)
  1246. len += plen;
  1247. len += ubifs_auth_node_sz(c);
  1248. dent = kzalloc(len, GFP_NOFS);
  1249. if (!dent)
  1250. return -ENOMEM;
  1251. /* Make reservation before allocating sequence numbers */
  1252. err = make_reservation(c, BASEHD, len);
  1253. if (err)
  1254. goto out_free;
  1255. /* Make new dent */
  1256. dent->ch.node_type = UBIFS_DENT_NODE;
  1257. dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
  1258. dent->inum = cpu_to_le64(old_inode->i_ino);
  1259. dent->type = get_dent_type(old_inode->i_mode);
  1260. dent->nlen = cpu_to_le16(fname_len(new_nm));
  1261. memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
  1262. dent->name[fname_len(new_nm)] = '\0';
  1263. set_dent_cookie(c, dent);
  1264. zero_dent_node_unused(dent);
  1265. ubifs_prep_grp_node(c, dent, dlen1, 0);
  1266. err = ubifs_node_calc_hash(c, dent, hash_dent1);
  1267. if (err)
  1268. goto out_release;
  1269. dent2 = (void *)dent + aligned_dlen1;
  1270. dent2->ch.node_type = UBIFS_DENT_NODE;
  1271. dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
  1272. if (whiteout) {
  1273. dent2->inum = cpu_to_le64(whiteout->i_ino);
  1274. dent2->type = get_dent_type(whiteout->i_mode);
  1275. } else {
  1276. /* Make deletion dent */
  1277. dent2->inum = 0;
  1278. dent2->type = DT_UNKNOWN;
  1279. }
  1280. dent2->nlen = cpu_to_le16(fname_len(old_nm));
  1281. memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
  1282. dent2->name[fname_len(old_nm)] = '\0';
  1283. set_dent_cookie(c, dent2);
  1284. zero_dent_node_unused(dent2);
  1285. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1286. err = ubifs_node_calc_hash(c, dent2, hash_dent2);
  1287. if (err)
  1288. goto out_release;
  1289. p = (void *)dent2 + aligned_dlen2;
  1290. if (new_inode) {
  1291. pack_inode(c, p, new_inode, 0);
  1292. err = ubifs_node_calc_hash(c, p, hash_new_inode);
  1293. if (err)
  1294. goto out_release;
  1295. p += ALIGN(ilen, 8);
  1296. }
  1297. if (whiteout) {
  1298. pack_inode(c, p, whiteout, 0);
  1299. err = ubifs_node_calc_hash(c, p, hash_whiteout_inode);
  1300. if (err)
  1301. goto out_release;
  1302. p += ALIGN(wlen, 8);
  1303. }
  1304. if (!move) {
  1305. pack_inode(c, p, old_dir, 1);
  1306. err = ubifs_node_calc_hash(c, p, hash_old_dir);
  1307. if (err)
  1308. goto out_release;
  1309. } else {
  1310. pack_inode(c, p, old_dir, 0);
  1311. err = ubifs_node_calc_hash(c, p, hash_old_dir);
  1312. if (err)
  1313. goto out_release;
  1314. p += ALIGN(plen, 8);
  1315. pack_inode(c, p, new_dir, 1);
  1316. err = ubifs_node_calc_hash(c, p, hash_new_dir);
  1317. if (err)
  1318. goto out_release;
  1319. }
  1320. if (last_reference) {
  1321. err = ubifs_add_orphan(c, new_inode->i_ino);
  1322. if (err) {
  1323. release_head(c, BASEHD);
  1324. goto out_finish;
  1325. }
  1326. new_ui->del_cmtno = c->cmt_no;
  1327. orphan_added = 1;
  1328. }
  1329. err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
  1330. if (err)
  1331. goto out_release;
  1332. if (!sync) {
  1333. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1334. ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
  1335. ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
  1336. if (new_inode)
  1337. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1338. new_inode->i_ino);
  1339. if (whiteout)
  1340. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1341. whiteout->i_ino);
  1342. }
  1343. release_head(c, BASEHD);
  1344. ubifs_add_auth_dirt(c, lnum);
  1345. dent_key_init(c, &key, new_dir->i_ino, new_nm);
  1346. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
  1347. if (err)
  1348. goto out_ro;
  1349. offs += aligned_dlen1;
  1350. if (whiteout) {
  1351. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1352. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
  1353. if (err)
  1354. goto out_ro;
  1355. } else {
  1356. err = ubifs_add_dirt(c, lnum, dlen2);
  1357. if (err)
  1358. goto out_ro;
  1359. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1360. err = ubifs_tnc_remove_nm(c, &key, old_nm);
  1361. if (err)
  1362. goto out_ro;
  1363. }
  1364. offs += aligned_dlen2;
  1365. if (new_inode) {
  1366. ino_key_init(c, &key, new_inode->i_ino);
  1367. err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
  1368. if (err)
  1369. goto out_ro;
  1370. offs += ALIGN(ilen, 8);
  1371. }
  1372. if (whiteout) {
  1373. ino_key_init(c, &key, whiteout->i_ino);
  1374. err = ubifs_tnc_add(c, &key, lnum, offs, wlen,
  1375. hash_whiteout_inode);
  1376. if (err)
  1377. goto out_ro;
  1378. offs += ALIGN(wlen, 8);
  1379. }
  1380. ino_key_init(c, &key, old_dir->i_ino);
  1381. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
  1382. if (err)
  1383. goto out_ro;
  1384. if (move) {
  1385. offs += ALIGN(plen, 8);
  1386. ino_key_init(c, &key, new_dir->i_ino);
  1387. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
  1388. if (err)
  1389. goto out_ro;
  1390. }
  1391. if (delete_orphan)
  1392. ubifs_delete_orphan(c, whiteout->i_ino);
  1393. finish_reservation(c);
  1394. if (new_inode) {
  1395. mark_inode_clean(c, new_ui);
  1396. spin_lock(&new_ui->ui_lock);
  1397. new_ui->synced_i_size = new_ui->ui_size;
  1398. spin_unlock(&new_ui->ui_lock);
  1399. }
  1400. /*
  1401. * No need to mark whiteout inode clean.
  1402. * Whiteout doesn't have non-zero size, no need to update
  1403. * synced_i_size for whiteout_ui.
  1404. */
  1405. mark_inode_clean(c, ubifs_inode(old_dir));
  1406. if (move)
  1407. mark_inode_clean(c, ubifs_inode(new_dir));
  1408. kfree(dent);
  1409. return 0;
  1410. out_release:
  1411. release_head(c, BASEHD);
  1412. out_ro:
  1413. ubifs_ro_mode(c, err);
  1414. if (orphan_added)
  1415. ubifs_delete_orphan(c, new_inode->i_ino);
  1416. out_finish:
  1417. finish_reservation(c);
  1418. out_free:
  1419. kfree(dent);
  1420. return err;
  1421. }
  1422. /**
  1423. * truncate_data_node - re-compress/encrypt a truncated data node.
  1424. * @c: UBIFS file-system description object
  1425. * @inode: inode which refers to the data node
  1426. * @block: data block number
  1427. * @dn: data node to re-compress
  1428. * @new_len: new length
  1429. * @dn_size: size of the data node @dn in memory
  1430. *
  1431. * This function is used when an inode is truncated and the last data node of
  1432. * the inode has to be re-compressed/encrypted and re-written.
  1433. */
  1434. static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
  1435. unsigned int block, struct ubifs_data_node *dn,
  1436. int *new_len, int dn_size)
  1437. {
  1438. void *buf;
  1439. int err, dlen, compr_type, out_len, data_size;
  1440. out_len = le32_to_cpu(dn->size);
  1441. buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
  1442. if (!buf)
  1443. return -ENOMEM;
  1444. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  1445. data_size = dn_size - UBIFS_DATA_NODE_SZ;
  1446. compr_type = le16_to_cpu(dn->compr_type);
  1447. if (IS_ENCRYPTED(inode)) {
  1448. err = ubifs_decrypt(inode, dn, &dlen, block);
  1449. if (err)
  1450. goto out;
  1451. }
  1452. if (compr_type == UBIFS_COMPR_NONE) {
  1453. out_len = *new_len;
  1454. } else {
  1455. err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
  1456. if (err)
  1457. goto out;
  1458. ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
  1459. }
  1460. if (IS_ENCRYPTED(inode)) {
  1461. err = ubifs_encrypt(inode, dn, out_len, &data_size, block);
  1462. if (err)
  1463. goto out;
  1464. out_len = data_size;
  1465. } else {
  1466. dn->compr_size = 0;
  1467. }
  1468. ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
  1469. dn->compr_type = cpu_to_le16(compr_type);
  1470. dn->size = cpu_to_le32(*new_len);
  1471. *new_len = UBIFS_DATA_NODE_SZ + out_len;
  1472. err = 0;
  1473. out:
  1474. kfree(buf);
  1475. return err;
  1476. }
  1477. /**
  1478. * ubifs_jnl_truncate - update the journal for a truncation.
  1479. * @c: UBIFS file-system description object
  1480. * @inode: inode to truncate
  1481. * @old_size: old size
  1482. * @new_size: new size
  1483. *
  1484. * When the size of a file decreases due to truncation, a truncation node is
  1485. * written, the journal tree is updated, and the last data block is re-written
  1486. * if it has been affected. The inode is also updated in order to synchronize
  1487. * the new inode size.
  1488. *
  1489. * This function marks the inode as clean and returns zero on success. In case
  1490. * of failure, a negative error code is returned.
  1491. */
  1492. int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
  1493. loff_t old_size, loff_t new_size)
  1494. {
  1495. union ubifs_key key, to_key;
  1496. struct ubifs_ino_node *ino;
  1497. struct ubifs_trun_node *trun;
  1498. struct ubifs_data_node *dn;
  1499. int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
  1500. int dn_size;
  1501. struct ubifs_inode *ui = ubifs_inode(inode);
  1502. ino_t inum = inode->i_ino;
  1503. unsigned int blk;
  1504. u8 hash_ino[UBIFS_HASH_ARR_SZ];
  1505. u8 hash_dn[UBIFS_HASH_ARR_SZ];
  1506. dbg_jnl("ino %lu, size %lld -> %lld",
  1507. (unsigned long)inum, old_size, new_size);
  1508. ubifs_assert(c, !ui->data_len);
  1509. ubifs_assert(c, S_ISREG(inode->i_mode));
  1510. ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
  1511. dn_size = COMPRESSED_DATA_NODE_BUF_SZ;
  1512. if (IS_ENCRYPTED(inode))
  1513. dn_size += UBIFS_CIPHER_BLOCK_SIZE;
  1514. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1515. dn_size + ubifs_auth_node_sz(c);
  1516. ino = kmalloc(sz, GFP_NOFS);
  1517. if (!ino)
  1518. return -ENOMEM;
  1519. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1520. trun->ch.node_type = UBIFS_TRUN_NODE;
  1521. trun->inum = cpu_to_le32(inum);
  1522. trun->old_size = cpu_to_le64(old_size);
  1523. trun->new_size = cpu_to_le64(new_size);
  1524. zero_trun_node_unused(trun);
  1525. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1526. if (dlen) {
  1527. /* Get last data block so it can be truncated */
  1528. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1529. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1530. data_key_init(c, &key, inum, blk);
  1531. dbg_jnlk(&key, "last block key ");
  1532. err = ubifs_tnc_lookup(c, &key, dn);
  1533. if (err == -ENOENT)
  1534. dlen = 0; /* Not found (so it is a hole) */
  1535. else if (err)
  1536. goto out_free;
  1537. else {
  1538. int dn_len = le32_to_cpu(dn->size);
  1539. if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
  1540. ubifs_err(c, "bad data node (block %u, inode %lu)",
  1541. blk, inode->i_ino);
  1542. ubifs_dump_node(c, dn, dn_size);
  1543. err = -EUCLEAN;
  1544. goto out_free;
  1545. }
  1546. if (dn_len <= dlen)
  1547. dlen = 0; /* Nothing to do */
  1548. else {
  1549. err = truncate_data_node(c, inode, blk, dn,
  1550. &dlen, dn_size);
  1551. if (err)
  1552. goto out_free;
  1553. }
  1554. }
  1555. }
  1556. /* Must make reservation before allocating sequence numbers */
  1557. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1558. if (ubifs_authenticated(c))
  1559. len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
  1560. else
  1561. len += dlen;
  1562. err = make_reservation(c, BASEHD, len);
  1563. if (err)
  1564. goto out_free;
  1565. pack_inode(c, ino, inode, 0);
  1566. err = ubifs_node_calc_hash(c, ino, hash_ino);
  1567. if (err)
  1568. goto out_release;
  1569. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1570. if (dlen) {
  1571. ubifs_prep_grp_node(c, dn, dlen, 1);
  1572. err = ubifs_node_calc_hash(c, dn, hash_dn);
  1573. if (err)
  1574. goto out_release;
  1575. }
  1576. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1577. if (err)
  1578. goto out_release;
  1579. if (!sync)
  1580. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1581. release_head(c, BASEHD);
  1582. ubifs_add_auth_dirt(c, lnum);
  1583. if (dlen) {
  1584. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1585. err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
  1586. if (err)
  1587. goto out_ro;
  1588. }
  1589. ino_key_init(c, &key, inum);
  1590. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
  1591. if (err)
  1592. goto out_ro;
  1593. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1594. if (err)
  1595. goto out_ro;
  1596. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1597. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1598. data_key_init(c, &key, inum, blk);
  1599. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1600. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
  1601. data_key_init(c, &to_key, inum, blk);
  1602. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1603. if (err)
  1604. goto out_ro;
  1605. finish_reservation(c);
  1606. spin_lock(&ui->ui_lock);
  1607. ui->synced_i_size = ui->ui_size;
  1608. spin_unlock(&ui->ui_lock);
  1609. mark_inode_clean(c, ui);
  1610. kfree(ino);
  1611. return 0;
  1612. out_release:
  1613. release_head(c, BASEHD);
  1614. out_ro:
  1615. ubifs_ro_mode(c, err);
  1616. finish_reservation(c);
  1617. out_free:
  1618. kfree(ino);
  1619. return err;
  1620. }
  1621. /**
  1622. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1623. * @c: UBIFS file-system description object
  1624. * @host: host inode
  1625. * @inode: extended attribute inode
  1626. * @nm: extended attribute entry name
  1627. *
  1628. * This function delete an extended attribute which is very similar to
  1629. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1630. * updates the target inode. Returns zero in case of success and a negative
  1631. * error code in case of failure.
  1632. */
  1633. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1634. const struct inode *inode,
  1635. const struct fscrypt_name *nm)
  1636. {
  1637. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
  1638. struct ubifs_dent_node *xent;
  1639. struct ubifs_ino_node *ino;
  1640. union ubifs_key xent_key, key1, key2;
  1641. int sync = IS_DIRSYNC(host);
  1642. struct ubifs_inode *host_ui = ubifs_inode(host);
  1643. u8 hash[UBIFS_HASH_ARR_SZ];
  1644. ubifs_assert(c, inode->i_nlink == 0);
  1645. ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
  1646. /*
  1647. * Since we are deleting the inode, we do not bother to attach any data
  1648. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1649. */
  1650. xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  1651. aligned_xlen = ALIGN(xlen, 8);
  1652. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1653. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1654. write_len = len + ubifs_auth_node_sz(c);
  1655. xent = kzalloc(write_len, GFP_NOFS);
  1656. if (!xent)
  1657. return -ENOMEM;
  1658. /* Make reservation before allocating sequence numbers */
  1659. err = make_reservation(c, BASEHD, write_len);
  1660. if (err) {
  1661. kfree(xent);
  1662. return err;
  1663. }
  1664. xent->ch.node_type = UBIFS_XENT_NODE;
  1665. xent_key_init(c, &xent_key, host->i_ino, nm);
  1666. key_write(c, &xent_key, xent->key);
  1667. xent->inum = 0;
  1668. xent->type = get_dent_type(inode->i_mode);
  1669. xent->nlen = cpu_to_le16(fname_len(nm));
  1670. memcpy(xent->name, fname_name(nm), fname_len(nm));
  1671. xent->name[fname_len(nm)] = '\0';
  1672. zero_dent_node_unused(xent);
  1673. ubifs_prep_grp_node(c, xent, xlen, 0);
  1674. ino = (void *)xent + aligned_xlen;
  1675. pack_inode(c, ino, inode, 0);
  1676. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1677. pack_inode(c, ino, host, 1);
  1678. err = ubifs_node_calc_hash(c, ino, hash);
  1679. if (err)
  1680. goto out_release;
  1681. err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
  1682. if (!sync && !err)
  1683. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1684. release_head(c, BASEHD);
  1685. ubifs_add_auth_dirt(c, lnum);
  1686. kfree(xent);
  1687. if (err)
  1688. goto out_ro;
  1689. /* Remove the extended attribute entry from TNC */
  1690. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1691. if (err)
  1692. goto out_ro;
  1693. err = ubifs_add_dirt(c, lnum, xlen);
  1694. if (err)
  1695. goto out_ro;
  1696. /*
  1697. * Remove all nodes belonging to the extended attribute inode from TNC.
  1698. * Well, there actually must be only one node - the inode itself.
  1699. */
  1700. lowest_ino_key(c, &key1, inode->i_ino);
  1701. highest_ino_key(c, &key2, inode->i_ino);
  1702. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1703. if (err)
  1704. goto out_ro;
  1705. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1706. if (err)
  1707. goto out_ro;
  1708. /* And update TNC with the new host inode position */
  1709. ino_key_init(c, &key1, host->i_ino);
  1710. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
  1711. if (err)
  1712. goto out_ro;
  1713. finish_reservation(c);
  1714. spin_lock(&host_ui->ui_lock);
  1715. host_ui->synced_i_size = host_ui->ui_size;
  1716. spin_unlock(&host_ui->ui_lock);
  1717. mark_inode_clean(c, host_ui);
  1718. return 0;
  1719. out_release:
  1720. kfree(xent);
  1721. release_head(c, BASEHD);
  1722. out_ro:
  1723. ubifs_ro_mode(c, err);
  1724. finish_reservation(c);
  1725. return err;
  1726. }
  1727. /**
  1728. * ubifs_jnl_change_xattr - change an extended attribute.
  1729. * @c: UBIFS file-system description object
  1730. * @inode: extended attribute inode
  1731. * @host: host inode
  1732. *
  1733. * This function writes the updated version of an extended attribute inode and
  1734. * the host inode to the journal (to the base head). The host inode is written
  1735. * after the extended attribute inode in order to guarantee that the extended
  1736. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1737. * consequently, the write-buffer is synchronized. This function returns zero
  1738. * in case of success and a negative error code in case of failure.
  1739. */
  1740. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1741. const struct inode *host)
  1742. {
  1743. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1744. struct ubifs_inode *host_ui = ubifs_inode(host);
  1745. struct ubifs_ino_node *ino;
  1746. union ubifs_key key;
  1747. int sync = IS_DIRSYNC(host);
  1748. u8 hash_host[UBIFS_HASH_ARR_SZ];
  1749. u8 hash[UBIFS_HASH_ARR_SZ];
  1750. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1751. ubifs_assert(c, inode->i_nlink > 0);
  1752. ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
  1753. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1754. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1755. aligned_len1 = ALIGN(len1, 8);
  1756. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1757. aligned_len += ubifs_auth_node_sz(c);
  1758. ino = kzalloc(aligned_len, GFP_NOFS);
  1759. if (!ino)
  1760. return -ENOMEM;
  1761. /* Make reservation before allocating sequence numbers */
  1762. err = make_reservation(c, BASEHD, aligned_len);
  1763. if (err)
  1764. goto out_free;
  1765. pack_inode(c, ino, host, 0);
  1766. err = ubifs_node_calc_hash(c, ino, hash_host);
  1767. if (err)
  1768. goto out_release;
  1769. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1770. err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
  1771. if (err)
  1772. goto out_release;
  1773. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1774. if (!sync && !err) {
  1775. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1776. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1777. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1778. }
  1779. release_head(c, BASEHD);
  1780. if (err)
  1781. goto out_ro;
  1782. ubifs_add_auth_dirt(c, lnum);
  1783. ino_key_init(c, &key, host->i_ino);
  1784. err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
  1785. if (err)
  1786. goto out_ro;
  1787. ino_key_init(c, &key, inode->i_ino);
  1788. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
  1789. if (err)
  1790. goto out_ro;
  1791. finish_reservation(c);
  1792. spin_lock(&host_ui->ui_lock);
  1793. host_ui->synced_i_size = host_ui->ui_size;
  1794. spin_unlock(&host_ui->ui_lock);
  1795. mark_inode_clean(c, host_ui);
  1796. kfree(ino);
  1797. return 0;
  1798. out_release:
  1799. release_head(c, BASEHD);
  1800. out_ro:
  1801. ubifs_ro_mode(c, err);
  1802. finish_reservation(c);
  1803. out_free:
  1804. kfree(ino);
  1805. return err;
  1806. }