journal.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  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. * @folio: buffer to write
  762. * @offset: offset to write at
  763. * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
  764. *
  765. * This function writes a data node to the journal. Returns %0 if the data node
  766. * was successfully written, and a negative error code in case of failure.
  767. */
  768. int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
  769. const union ubifs_key *key, struct folio *folio,
  770. size_t offset, int len)
  771. {
  772. struct ubifs_data_node *data;
  773. int err, lnum, offs, compr_type, out_len, compr_len, auth_len;
  774. int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
  775. int write_len;
  776. struct ubifs_inode *ui = ubifs_inode(inode);
  777. bool encrypted = IS_ENCRYPTED(inode);
  778. u8 hash[UBIFS_HASH_ARR_SZ];
  779. dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
  780. (unsigned long)key_inum(c, key), key_block(c, key), len);
  781. ubifs_assert(c, len <= UBIFS_BLOCK_SIZE);
  782. if (encrypted)
  783. dlen += UBIFS_CIPHER_BLOCK_SIZE;
  784. auth_len = ubifs_auth_node_sz(c);
  785. data = kmalloc(dlen + auth_len, GFP_NOFS | __GFP_NOWARN);
  786. if (!data) {
  787. /*
  788. * Fall-back to the write reserve buffer. Note, we might be
  789. * currently on the memory reclaim path, when the kernel is
  790. * trying to free some memory by writing out dirty pages. The
  791. * write reserve buffer helps us to guarantee that we are
  792. * always able to write the data.
  793. */
  794. allocated = 0;
  795. mutex_lock(&c->write_reserve_mutex);
  796. data = c->write_reserve_buf;
  797. }
  798. data->ch.node_type = UBIFS_DATA_NODE;
  799. key_write(c, key, &data->key);
  800. data->size = cpu_to_le32(len);
  801. if (!(ui->flags & UBIFS_COMPR_FL))
  802. /* Compression is disabled for this inode */
  803. compr_type = UBIFS_COMPR_NONE;
  804. else
  805. compr_type = ui->compr_type;
  806. out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
  807. ubifs_compress_folio(c, folio, offset, len, &data->data, &compr_len,
  808. &compr_type);
  809. ubifs_assert(c, compr_len <= UBIFS_BLOCK_SIZE);
  810. if (encrypted) {
  811. err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
  812. if (err)
  813. goto out_free;
  814. } else {
  815. data->compr_size = 0;
  816. out_len = compr_len;
  817. }
  818. dlen = UBIFS_DATA_NODE_SZ + out_len;
  819. if (ubifs_authenticated(c))
  820. write_len = ALIGN(dlen, 8) + auth_len;
  821. else
  822. write_len = dlen;
  823. data->compr_type = cpu_to_le16(compr_type);
  824. /* Make reservation before allocating sequence numbers */
  825. err = make_reservation(c, DATAHD, write_len);
  826. if (err)
  827. goto out_free;
  828. ubifs_prepare_node(c, data, dlen, 0);
  829. err = write_head(c, DATAHD, data, write_len, &lnum, &offs, 0);
  830. if (err)
  831. goto out_release;
  832. err = ubifs_node_calc_hash(c, data, hash);
  833. if (err)
  834. goto out_release;
  835. ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
  836. release_head(c, DATAHD);
  837. ubifs_add_auth_dirt(c, lnum);
  838. err = ubifs_tnc_add(c, key, lnum, offs, dlen, hash);
  839. if (err)
  840. goto out_ro;
  841. finish_reservation(c);
  842. if (!allocated)
  843. mutex_unlock(&c->write_reserve_mutex);
  844. else
  845. kfree(data);
  846. return 0;
  847. out_release:
  848. release_head(c, DATAHD);
  849. out_ro:
  850. ubifs_ro_mode(c, err);
  851. finish_reservation(c);
  852. out_free:
  853. if (!allocated)
  854. mutex_unlock(&c->write_reserve_mutex);
  855. else
  856. kfree(data);
  857. return err;
  858. }
  859. /**
  860. * ubifs_jnl_write_inode - flush inode to the journal.
  861. * @c: UBIFS file-system description object
  862. * @inode: inode to flush
  863. *
  864. * This function writes inode @inode to the journal. If the inode is
  865. * synchronous, it also synchronizes the write-buffer. Returns zero in case of
  866. * success and a negative error code in case of failure.
  867. */
  868. int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
  869. {
  870. int err, lnum, offs;
  871. struct ubifs_ino_node *ino, *ino_start;
  872. struct ubifs_inode *ui = ubifs_inode(inode);
  873. int sync = 0, write_len = 0, ilen = UBIFS_INO_NODE_SZ;
  874. int last_reference = !inode->i_nlink;
  875. int kill_xattrs = ui->xattr_cnt && last_reference;
  876. u8 hash[UBIFS_HASH_ARR_SZ];
  877. dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
  878. if (kill_xattrs && ui->xattr_cnt > ubifs_xattr_max_cnt(c)) {
  879. ubifs_err(c, "Cannot delete inode, it has too many xattrs!");
  880. err = -EPERM;
  881. ubifs_ro_mode(c, err);
  882. return err;
  883. }
  884. /*
  885. * If the inode is being deleted, do not write the attached data. No
  886. * need to synchronize the write-buffer either.
  887. */
  888. if (!last_reference) {
  889. ilen += ui->data_len;
  890. sync = IS_SYNC(inode);
  891. } else if (kill_xattrs) {
  892. write_len += UBIFS_INO_NODE_SZ * ui->xattr_cnt;
  893. }
  894. if (ubifs_authenticated(c))
  895. write_len += ALIGN(ilen, 8) + ubifs_auth_node_sz(c);
  896. else
  897. write_len += ilen;
  898. ino_start = ino = kmalloc(write_len, GFP_NOFS);
  899. if (!ino)
  900. return -ENOMEM;
  901. /* Make reservation before allocating sequence numbers */
  902. err = make_reservation(c, BASEHD, write_len);
  903. if (err)
  904. goto out_free;
  905. if (kill_xattrs) {
  906. union ubifs_key key;
  907. struct fscrypt_name nm = {0};
  908. struct inode *xino;
  909. struct ubifs_dent_node *xent, *pxent = NULL;
  910. lowest_xent_key(c, &key, inode->i_ino);
  911. while (1) {
  912. xent = ubifs_tnc_next_ent(c, &key, &nm);
  913. if (IS_ERR(xent)) {
  914. err = PTR_ERR(xent);
  915. if (err == -ENOENT)
  916. break;
  917. kfree(pxent);
  918. goto out_release;
  919. }
  920. fname_name(&nm) = xent->name;
  921. fname_len(&nm) = le16_to_cpu(xent->nlen);
  922. xino = ubifs_iget(c->vfs_sb, le64_to_cpu(xent->inum));
  923. if (IS_ERR(xino)) {
  924. err = PTR_ERR(xino);
  925. ubifs_err(c, "dead directory entry '%s', error %d",
  926. xent->name, err);
  927. ubifs_ro_mode(c, err);
  928. kfree(pxent);
  929. kfree(xent);
  930. goto out_release;
  931. }
  932. ubifs_assert(c, ubifs_inode(xino)->xattr);
  933. clear_nlink(xino);
  934. pack_inode(c, ino, xino, 0);
  935. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  936. iput(xino);
  937. kfree(pxent);
  938. pxent = xent;
  939. key_read(c, &xent->key, &key);
  940. }
  941. kfree(pxent);
  942. }
  943. pack_inode(c, ino, inode, 1);
  944. err = ubifs_node_calc_hash(c, ino, hash);
  945. if (err)
  946. goto out_release;
  947. err = write_head(c, BASEHD, ino_start, write_len, &lnum, &offs, sync);
  948. if (err)
  949. goto out_release;
  950. if (!sync)
  951. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  952. inode->i_ino);
  953. release_head(c, BASEHD);
  954. if (last_reference) {
  955. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  956. if (err)
  957. goto out_ro;
  958. ubifs_delete_orphan(c, inode->i_ino);
  959. err = ubifs_add_dirt(c, lnum, write_len);
  960. } else {
  961. union ubifs_key key;
  962. ubifs_add_auth_dirt(c, lnum);
  963. ino_key_init(c, &key, inode->i_ino);
  964. err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash);
  965. }
  966. if (err)
  967. goto out_ro;
  968. finish_reservation(c);
  969. spin_lock(&ui->ui_lock);
  970. ui->synced_i_size = ui->ui_size;
  971. spin_unlock(&ui->ui_lock);
  972. kfree(ino_start);
  973. return 0;
  974. out_release:
  975. release_head(c, BASEHD);
  976. out_ro:
  977. ubifs_ro_mode(c, err);
  978. finish_reservation(c);
  979. out_free:
  980. kfree(ino_start);
  981. return err;
  982. }
  983. /**
  984. * ubifs_jnl_delete_inode - delete an inode.
  985. * @c: UBIFS file-system description object
  986. * @inode: inode to delete
  987. *
  988. * This function deletes inode @inode which includes removing it from orphans,
  989. * deleting it from TNC and, in some cases, writing a deletion inode to the
  990. * journal.
  991. *
  992. * When regular file inodes are unlinked or a directory inode is removed, the
  993. * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
  994. * direntry to the media, and adds the inode to orphans. After this, when the
  995. * last reference to this inode has been dropped, this function is called. In
  996. * general, it has to write one more deletion inode to the media, because if
  997. * a commit happened between 'ubifs_jnl_update()' and
  998. * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
  999. * anymore, and in fact it might not be on the flash anymore, because it might
  1000. * have been garbage-collected already. And for optimization reasons UBIFS does
  1001. * not read the orphan area if it has been unmounted cleanly, so it would have
  1002. * no indication in the journal that there is a deleted inode which has to be
  1003. * removed from TNC.
  1004. *
  1005. * However, if there was no commit between 'ubifs_jnl_update()' and
  1006. * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
  1007. * inode to the media for the second time. And this is quite a typical case.
  1008. *
  1009. * This function returns zero in case of success and a negative error code in
  1010. * case of failure.
  1011. */
  1012. int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
  1013. {
  1014. int err;
  1015. struct ubifs_inode *ui = ubifs_inode(inode);
  1016. ubifs_assert(c, inode->i_nlink == 0);
  1017. if (ui->xattr_cnt || ui->del_cmtno != c->cmt_no)
  1018. /* A commit happened for sure or inode hosts xattrs */
  1019. return ubifs_jnl_write_inode(c, inode);
  1020. down_read(&c->commit_sem);
  1021. /*
  1022. * Check commit number again, because the first test has been done
  1023. * without @c->commit_sem, so a commit might have happened.
  1024. */
  1025. if (ui->del_cmtno != c->cmt_no) {
  1026. up_read(&c->commit_sem);
  1027. return ubifs_jnl_write_inode(c, inode);
  1028. }
  1029. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  1030. if (err)
  1031. ubifs_ro_mode(c, err);
  1032. else
  1033. ubifs_delete_orphan(c, inode->i_ino);
  1034. up_read(&c->commit_sem);
  1035. return err;
  1036. }
  1037. /**
  1038. * ubifs_jnl_xrename - cross rename two directory entries.
  1039. * @c: UBIFS file-system description object
  1040. * @fst_dir: parent inode of 1st directory entry to exchange
  1041. * @fst_inode: 1st inode to exchange
  1042. * @fst_nm: name of 1st inode to exchange
  1043. * @snd_dir: parent inode of 2nd directory entry to exchange
  1044. * @snd_inode: 2nd inode to exchange
  1045. * @snd_nm: name of 2nd inode to exchange
  1046. * @sync: non-zero if the write-buffer has to be synchronized
  1047. *
  1048. * This function implements the cross rename operation which may involve
  1049. * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
  1050. * and returns zero on success. In case of failure, a negative error code is
  1051. * returned.
  1052. */
  1053. int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  1054. const struct inode *fst_inode,
  1055. const struct fscrypt_name *fst_nm,
  1056. const struct inode *snd_dir,
  1057. const struct inode *snd_inode,
  1058. const struct fscrypt_name *snd_nm, int sync)
  1059. {
  1060. union ubifs_key key;
  1061. struct ubifs_dent_node *dent1, *dent2;
  1062. int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
  1063. int aligned_dlen1, aligned_dlen2;
  1064. int twoparents = (fst_dir != snd_dir);
  1065. void *p;
  1066. u8 hash_dent1[UBIFS_HASH_ARR_SZ];
  1067. u8 hash_dent2[UBIFS_HASH_ARR_SZ];
  1068. u8 hash_p1[UBIFS_HASH_ARR_SZ];
  1069. u8 hash_p2[UBIFS_HASH_ARR_SZ];
  1070. ubifs_assert(c, ubifs_inode(fst_dir)->data_len == 0);
  1071. ubifs_assert(c, ubifs_inode(snd_dir)->data_len == 0);
  1072. ubifs_assert(c, mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
  1073. ubifs_assert(c, mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
  1074. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
  1075. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
  1076. aligned_dlen1 = ALIGN(dlen1, 8);
  1077. aligned_dlen2 = ALIGN(dlen2, 8);
  1078. len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
  1079. if (twoparents)
  1080. len += plen;
  1081. len += ubifs_auth_node_sz(c);
  1082. dent1 = kzalloc(len, GFP_NOFS);
  1083. if (!dent1)
  1084. return -ENOMEM;
  1085. /* Make reservation before allocating sequence numbers */
  1086. err = make_reservation(c, BASEHD, len);
  1087. if (err)
  1088. goto out_free;
  1089. /* Make new dent for 1st entry */
  1090. dent1->ch.node_type = UBIFS_DENT_NODE;
  1091. dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
  1092. dent1->inum = cpu_to_le64(fst_inode->i_ino);
  1093. dent1->type = get_dent_type(fst_inode->i_mode);
  1094. dent1->nlen = cpu_to_le16(fname_len(snd_nm));
  1095. memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
  1096. dent1->name[fname_len(snd_nm)] = '\0';
  1097. set_dent_cookie(c, dent1);
  1098. zero_dent_node_unused(dent1);
  1099. ubifs_prep_grp_node(c, dent1, dlen1, 0);
  1100. err = ubifs_node_calc_hash(c, dent1, hash_dent1);
  1101. if (err)
  1102. goto out_release;
  1103. /* Make new dent for 2nd entry */
  1104. dent2 = (void *)dent1 + aligned_dlen1;
  1105. dent2->ch.node_type = UBIFS_DENT_NODE;
  1106. dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
  1107. dent2->inum = cpu_to_le64(snd_inode->i_ino);
  1108. dent2->type = get_dent_type(snd_inode->i_mode);
  1109. dent2->nlen = cpu_to_le16(fname_len(fst_nm));
  1110. memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
  1111. dent2->name[fname_len(fst_nm)] = '\0';
  1112. set_dent_cookie(c, dent2);
  1113. zero_dent_node_unused(dent2);
  1114. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1115. err = ubifs_node_calc_hash(c, dent2, hash_dent2);
  1116. if (err)
  1117. goto out_release;
  1118. p = (void *)dent2 + aligned_dlen2;
  1119. if (!twoparents) {
  1120. pack_inode(c, p, fst_dir, 1);
  1121. err = ubifs_node_calc_hash(c, p, hash_p1);
  1122. if (err)
  1123. goto out_release;
  1124. } else {
  1125. pack_inode(c, p, fst_dir, 0);
  1126. err = ubifs_node_calc_hash(c, p, hash_p1);
  1127. if (err)
  1128. goto out_release;
  1129. p += ALIGN(plen, 8);
  1130. pack_inode(c, p, snd_dir, 1);
  1131. err = ubifs_node_calc_hash(c, p, hash_p2);
  1132. if (err)
  1133. goto out_release;
  1134. }
  1135. err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
  1136. if (err)
  1137. goto out_release;
  1138. if (!sync) {
  1139. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1140. ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
  1141. ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
  1142. }
  1143. release_head(c, BASEHD);
  1144. ubifs_add_auth_dirt(c, lnum);
  1145. dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
  1146. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, snd_nm);
  1147. if (err)
  1148. goto out_ro;
  1149. offs += aligned_dlen1;
  1150. dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
  1151. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, fst_nm);
  1152. if (err)
  1153. goto out_ro;
  1154. offs += aligned_dlen2;
  1155. ino_key_init(c, &key, fst_dir->i_ino);
  1156. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p1);
  1157. if (err)
  1158. goto out_ro;
  1159. if (twoparents) {
  1160. offs += ALIGN(plen, 8);
  1161. ino_key_init(c, &key, snd_dir->i_ino);
  1162. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_p2);
  1163. if (err)
  1164. goto out_ro;
  1165. }
  1166. finish_reservation(c);
  1167. mark_inode_clean(c, ubifs_inode(fst_dir));
  1168. if (twoparents)
  1169. mark_inode_clean(c, ubifs_inode(snd_dir));
  1170. kfree(dent1);
  1171. return 0;
  1172. out_release:
  1173. release_head(c, BASEHD);
  1174. out_ro:
  1175. ubifs_ro_mode(c, err);
  1176. finish_reservation(c);
  1177. out_free:
  1178. kfree(dent1);
  1179. return err;
  1180. }
  1181. /**
  1182. * ubifs_jnl_rename - rename a directory entry.
  1183. * @c: UBIFS file-system description object
  1184. * @old_dir: parent inode of directory entry to rename
  1185. * @old_inode: directory entry's inode to rename
  1186. * @old_nm: name of the old directory entry to rename
  1187. * @new_dir: parent inode of directory entry to rename
  1188. * @new_inode: new directory entry's inode (or directory entry's inode to
  1189. * replace)
  1190. * @new_nm: new name of the new directory entry
  1191. * @whiteout: whiteout inode
  1192. * @sync: non-zero if the write-buffer has to be synchronized
  1193. * @delete_orphan: indicates an orphan entry deletion for @whiteout
  1194. *
  1195. * This function implements the re-name operation which may involve writing up
  1196. * to 4 inodes(new inode, whiteout inode, old and new parent directory inodes)
  1197. * and 2 directory entries. It marks the written inodes as clean and returns
  1198. * zero on success. In case of failure, a negative error code is returned.
  1199. */
  1200. int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
  1201. const struct inode *old_inode,
  1202. const struct fscrypt_name *old_nm,
  1203. const struct inode *new_dir,
  1204. const struct inode *new_inode,
  1205. const struct fscrypt_name *new_nm,
  1206. const struct inode *whiteout, int sync, int delete_orphan)
  1207. {
  1208. void *p;
  1209. union ubifs_key key;
  1210. struct ubifs_dent_node *dent, *dent2;
  1211. int err, dlen1, dlen2, ilen, wlen, lnum, offs, len, orphan_added = 0;
  1212. int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
  1213. int last_reference = !!(new_inode && new_inode->i_nlink == 0);
  1214. int move = (old_dir != new_dir);
  1215. struct ubifs_inode *new_ui, *whiteout_ui;
  1216. u8 hash_old_dir[UBIFS_HASH_ARR_SZ];
  1217. u8 hash_new_dir[UBIFS_HASH_ARR_SZ];
  1218. u8 hash_new_inode[UBIFS_HASH_ARR_SZ];
  1219. u8 hash_whiteout_inode[UBIFS_HASH_ARR_SZ];
  1220. u8 hash_dent1[UBIFS_HASH_ARR_SZ];
  1221. u8 hash_dent2[UBIFS_HASH_ARR_SZ];
  1222. ubifs_assert(c, ubifs_inode(old_dir)->data_len == 0);
  1223. ubifs_assert(c, ubifs_inode(new_dir)->data_len == 0);
  1224. ubifs_assert(c, mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
  1225. ubifs_assert(c, mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
  1226. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
  1227. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
  1228. if (new_inode) {
  1229. new_ui = ubifs_inode(new_inode);
  1230. ubifs_assert(c, mutex_is_locked(&new_ui->ui_mutex));
  1231. ilen = UBIFS_INO_NODE_SZ;
  1232. if (!last_reference)
  1233. ilen += new_ui->data_len;
  1234. } else
  1235. ilen = 0;
  1236. if (whiteout) {
  1237. whiteout_ui = ubifs_inode(whiteout);
  1238. ubifs_assert(c, mutex_is_locked(&whiteout_ui->ui_mutex));
  1239. ubifs_assert(c, whiteout->i_nlink == 1);
  1240. ubifs_assert(c, !whiteout_ui->dirty);
  1241. wlen = UBIFS_INO_NODE_SZ;
  1242. wlen += whiteout_ui->data_len;
  1243. } else
  1244. wlen = 0;
  1245. aligned_dlen1 = ALIGN(dlen1, 8);
  1246. aligned_dlen2 = ALIGN(dlen2, 8);
  1247. len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) +
  1248. ALIGN(wlen, 8) + ALIGN(plen, 8);
  1249. if (move)
  1250. len += plen;
  1251. len += ubifs_auth_node_sz(c);
  1252. dent = kzalloc(len, GFP_NOFS);
  1253. if (!dent)
  1254. return -ENOMEM;
  1255. /* Make reservation before allocating sequence numbers */
  1256. err = make_reservation(c, BASEHD, len);
  1257. if (err)
  1258. goto out_free;
  1259. /* Make new dent */
  1260. dent->ch.node_type = UBIFS_DENT_NODE;
  1261. dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
  1262. dent->inum = cpu_to_le64(old_inode->i_ino);
  1263. dent->type = get_dent_type(old_inode->i_mode);
  1264. dent->nlen = cpu_to_le16(fname_len(new_nm));
  1265. memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
  1266. dent->name[fname_len(new_nm)] = '\0';
  1267. set_dent_cookie(c, dent);
  1268. zero_dent_node_unused(dent);
  1269. ubifs_prep_grp_node(c, dent, dlen1, 0);
  1270. err = ubifs_node_calc_hash(c, dent, hash_dent1);
  1271. if (err)
  1272. goto out_release;
  1273. dent2 = (void *)dent + aligned_dlen1;
  1274. dent2->ch.node_type = UBIFS_DENT_NODE;
  1275. dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
  1276. if (whiteout) {
  1277. dent2->inum = cpu_to_le64(whiteout->i_ino);
  1278. dent2->type = get_dent_type(whiteout->i_mode);
  1279. } else {
  1280. /* Make deletion dent */
  1281. dent2->inum = 0;
  1282. dent2->type = DT_UNKNOWN;
  1283. }
  1284. dent2->nlen = cpu_to_le16(fname_len(old_nm));
  1285. memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
  1286. dent2->name[fname_len(old_nm)] = '\0';
  1287. set_dent_cookie(c, dent2);
  1288. zero_dent_node_unused(dent2);
  1289. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1290. err = ubifs_node_calc_hash(c, dent2, hash_dent2);
  1291. if (err)
  1292. goto out_release;
  1293. p = (void *)dent2 + aligned_dlen2;
  1294. if (new_inode) {
  1295. pack_inode(c, p, new_inode, 0);
  1296. err = ubifs_node_calc_hash(c, p, hash_new_inode);
  1297. if (err)
  1298. goto out_release;
  1299. p += ALIGN(ilen, 8);
  1300. }
  1301. if (whiteout) {
  1302. pack_inode(c, p, whiteout, 0);
  1303. err = ubifs_node_calc_hash(c, p, hash_whiteout_inode);
  1304. if (err)
  1305. goto out_release;
  1306. p += ALIGN(wlen, 8);
  1307. }
  1308. if (!move) {
  1309. pack_inode(c, p, old_dir, 1);
  1310. err = ubifs_node_calc_hash(c, p, hash_old_dir);
  1311. if (err)
  1312. goto out_release;
  1313. } else {
  1314. pack_inode(c, p, old_dir, 0);
  1315. err = ubifs_node_calc_hash(c, p, hash_old_dir);
  1316. if (err)
  1317. goto out_release;
  1318. p += ALIGN(plen, 8);
  1319. pack_inode(c, p, new_dir, 1);
  1320. err = ubifs_node_calc_hash(c, p, hash_new_dir);
  1321. if (err)
  1322. goto out_release;
  1323. }
  1324. if (last_reference) {
  1325. err = ubifs_add_orphan(c, new_inode->i_ino);
  1326. if (err) {
  1327. release_head(c, BASEHD);
  1328. goto out_finish;
  1329. }
  1330. new_ui->del_cmtno = c->cmt_no;
  1331. orphan_added = 1;
  1332. }
  1333. err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
  1334. if (err)
  1335. goto out_release;
  1336. if (!sync) {
  1337. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1338. ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
  1339. ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
  1340. if (new_inode)
  1341. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1342. new_inode->i_ino);
  1343. if (whiteout)
  1344. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1345. whiteout->i_ino);
  1346. }
  1347. release_head(c, BASEHD);
  1348. ubifs_add_auth_dirt(c, lnum);
  1349. dent_key_init(c, &key, new_dir->i_ino, new_nm);
  1350. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, hash_dent1, new_nm);
  1351. if (err)
  1352. goto out_ro;
  1353. offs += aligned_dlen1;
  1354. if (whiteout) {
  1355. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1356. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, hash_dent2, old_nm);
  1357. if (err)
  1358. goto out_ro;
  1359. } else {
  1360. err = ubifs_add_dirt(c, lnum, dlen2);
  1361. if (err)
  1362. goto out_ro;
  1363. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1364. err = ubifs_tnc_remove_nm(c, &key, old_nm);
  1365. if (err)
  1366. goto out_ro;
  1367. }
  1368. offs += aligned_dlen2;
  1369. if (new_inode) {
  1370. ino_key_init(c, &key, new_inode->i_ino);
  1371. err = ubifs_tnc_add(c, &key, lnum, offs, ilen, hash_new_inode);
  1372. if (err)
  1373. goto out_ro;
  1374. offs += ALIGN(ilen, 8);
  1375. }
  1376. if (whiteout) {
  1377. ino_key_init(c, &key, whiteout->i_ino);
  1378. err = ubifs_tnc_add(c, &key, lnum, offs, wlen,
  1379. hash_whiteout_inode);
  1380. if (err)
  1381. goto out_ro;
  1382. offs += ALIGN(wlen, 8);
  1383. }
  1384. ino_key_init(c, &key, old_dir->i_ino);
  1385. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_old_dir);
  1386. if (err)
  1387. goto out_ro;
  1388. if (move) {
  1389. offs += ALIGN(plen, 8);
  1390. ino_key_init(c, &key, new_dir->i_ino);
  1391. err = ubifs_tnc_add(c, &key, lnum, offs, plen, hash_new_dir);
  1392. if (err)
  1393. goto out_ro;
  1394. }
  1395. if (delete_orphan)
  1396. ubifs_delete_orphan(c, whiteout->i_ino);
  1397. finish_reservation(c);
  1398. if (new_inode) {
  1399. mark_inode_clean(c, new_ui);
  1400. spin_lock(&new_ui->ui_lock);
  1401. new_ui->synced_i_size = new_ui->ui_size;
  1402. spin_unlock(&new_ui->ui_lock);
  1403. }
  1404. /*
  1405. * No need to mark whiteout inode clean.
  1406. * Whiteout doesn't have non-zero size, no need to update
  1407. * synced_i_size for whiteout_ui.
  1408. */
  1409. mark_inode_clean(c, ubifs_inode(old_dir));
  1410. if (move)
  1411. mark_inode_clean(c, ubifs_inode(new_dir));
  1412. kfree(dent);
  1413. return 0;
  1414. out_release:
  1415. release_head(c, BASEHD);
  1416. out_ro:
  1417. ubifs_ro_mode(c, err);
  1418. if (orphan_added)
  1419. ubifs_delete_orphan(c, new_inode->i_ino);
  1420. out_finish:
  1421. finish_reservation(c);
  1422. out_free:
  1423. kfree(dent);
  1424. return err;
  1425. }
  1426. /**
  1427. * truncate_data_node - re-compress/encrypt a truncated data node.
  1428. * @c: UBIFS file-system description object
  1429. * @inode: inode which refers to the data node
  1430. * @block: data block number
  1431. * @dn: data node to re-compress
  1432. * @new_len: new length
  1433. * @dn_size: size of the data node @dn in memory
  1434. *
  1435. * This function is used when an inode is truncated and the last data node of
  1436. * the inode has to be re-compressed/encrypted and re-written.
  1437. */
  1438. static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
  1439. unsigned int block, struct ubifs_data_node *dn,
  1440. int *new_len, int dn_size)
  1441. {
  1442. void *buf;
  1443. int err, dlen, compr_type, out_len, data_size;
  1444. out_len = le32_to_cpu(dn->size);
  1445. buf = kmalloc(out_len, GFP_NOFS);
  1446. if (!buf)
  1447. return -ENOMEM;
  1448. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  1449. data_size = dn_size - UBIFS_DATA_NODE_SZ;
  1450. compr_type = le16_to_cpu(dn->compr_type);
  1451. if (IS_ENCRYPTED(inode)) {
  1452. err = ubifs_decrypt(inode, dn, &dlen, block);
  1453. if (err)
  1454. goto out;
  1455. }
  1456. if (compr_type == UBIFS_COMPR_NONE) {
  1457. out_len = *new_len;
  1458. } else {
  1459. err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
  1460. if (err)
  1461. goto out;
  1462. ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
  1463. }
  1464. if (IS_ENCRYPTED(inode)) {
  1465. err = ubifs_encrypt(inode, dn, out_len, &data_size, block);
  1466. if (err)
  1467. goto out;
  1468. out_len = data_size;
  1469. } else {
  1470. dn->compr_size = 0;
  1471. }
  1472. ubifs_assert(c, out_len <= UBIFS_BLOCK_SIZE);
  1473. dn->compr_type = cpu_to_le16(compr_type);
  1474. dn->size = cpu_to_le32(*new_len);
  1475. *new_len = UBIFS_DATA_NODE_SZ + out_len;
  1476. err = 0;
  1477. out:
  1478. kfree(buf);
  1479. return err;
  1480. }
  1481. /**
  1482. * ubifs_jnl_truncate - update the journal for a truncation.
  1483. * @c: UBIFS file-system description object
  1484. * @inode: inode to truncate
  1485. * @old_size: old size
  1486. * @new_size: new size
  1487. *
  1488. * When the size of a file decreases due to truncation, a truncation node is
  1489. * written, the journal tree is updated, and the last data block is re-written
  1490. * if it has been affected. The inode is also updated in order to synchronize
  1491. * the new inode size.
  1492. *
  1493. * This function marks the inode as clean and returns zero on success. In case
  1494. * of failure, a negative error code is returned.
  1495. */
  1496. int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
  1497. loff_t old_size, loff_t new_size)
  1498. {
  1499. union ubifs_key key, to_key;
  1500. struct ubifs_ino_node *ino;
  1501. struct ubifs_trun_node *trun;
  1502. struct ubifs_data_node *dn;
  1503. int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
  1504. int dn_size;
  1505. struct ubifs_inode *ui = ubifs_inode(inode);
  1506. ino_t inum = inode->i_ino;
  1507. unsigned int blk;
  1508. u8 hash_ino[UBIFS_HASH_ARR_SZ];
  1509. u8 hash_dn[UBIFS_HASH_ARR_SZ];
  1510. dbg_jnl("ino %lu, size %lld -> %lld",
  1511. (unsigned long)inum, old_size, new_size);
  1512. ubifs_assert(c, !ui->data_len);
  1513. ubifs_assert(c, S_ISREG(inode->i_mode));
  1514. ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
  1515. dn_size = COMPRESSED_DATA_NODE_BUF_SZ;
  1516. if (IS_ENCRYPTED(inode))
  1517. dn_size += UBIFS_CIPHER_BLOCK_SIZE;
  1518. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1519. dn_size + ubifs_auth_node_sz(c);
  1520. ino = kmalloc(sz, GFP_NOFS);
  1521. if (!ino)
  1522. return -ENOMEM;
  1523. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1524. trun->ch.node_type = UBIFS_TRUN_NODE;
  1525. trun->inum = cpu_to_le32(inum);
  1526. trun->old_size = cpu_to_le64(old_size);
  1527. trun->new_size = cpu_to_le64(new_size);
  1528. zero_trun_node_unused(trun);
  1529. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1530. if (dlen) {
  1531. /* Get last data block so it can be truncated */
  1532. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1533. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1534. data_key_init(c, &key, inum, blk);
  1535. dbg_jnlk(&key, "last block key ");
  1536. err = ubifs_tnc_lookup(c, &key, dn);
  1537. if (err == -ENOENT)
  1538. dlen = 0; /* Not found (so it is a hole) */
  1539. else if (err)
  1540. goto out_free;
  1541. else {
  1542. int dn_len = le32_to_cpu(dn->size);
  1543. if (dn_len <= 0 || dn_len > UBIFS_BLOCK_SIZE) {
  1544. ubifs_err(c, "bad data node (block %u, inode %lu)",
  1545. blk, inode->i_ino);
  1546. ubifs_dump_node(c, dn, dn_size);
  1547. err = -EUCLEAN;
  1548. goto out_free;
  1549. }
  1550. if (dn_len <= dlen)
  1551. dlen = 0; /* Nothing to do */
  1552. else {
  1553. err = truncate_data_node(c, inode, blk, dn,
  1554. &dlen, dn_size);
  1555. if (err)
  1556. goto out_free;
  1557. }
  1558. }
  1559. }
  1560. /* Must make reservation before allocating sequence numbers */
  1561. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1562. if (ubifs_authenticated(c))
  1563. len += ALIGN(dlen, 8) + ubifs_auth_node_sz(c);
  1564. else
  1565. len += dlen;
  1566. err = make_reservation(c, BASEHD, len);
  1567. if (err)
  1568. goto out_free;
  1569. pack_inode(c, ino, inode, 0);
  1570. err = ubifs_node_calc_hash(c, ino, hash_ino);
  1571. if (err)
  1572. goto out_release;
  1573. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1574. if (dlen) {
  1575. ubifs_prep_grp_node(c, dn, dlen, 1);
  1576. err = ubifs_node_calc_hash(c, dn, hash_dn);
  1577. if (err)
  1578. goto out_release;
  1579. }
  1580. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1581. if (err)
  1582. goto out_release;
  1583. if (!sync)
  1584. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1585. release_head(c, BASEHD);
  1586. ubifs_add_auth_dirt(c, lnum);
  1587. if (dlen) {
  1588. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1589. err = ubifs_tnc_add(c, &key, lnum, sz, dlen, hash_dn);
  1590. if (err)
  1591. goto out_ro;
  1592. }
  1593. ino_key_init(c, &key, inum);
  1594. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ, hash_ino);
  1595. if (err)
  1596. goto out_ro;
  1597. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1598. if (err)
  1599. goto out_ro;
  1600. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1601. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1602. data_key_init(c, &key, inum, blk);
  1603. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1604. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
  1605. data_key_init(c, &to_key, inum, blk);
  1606. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1607. if (err)
  1608. goto out_ro;
  1609. finish_reservation(c);
  1610. spin_lock(&ui->ui_lock);
  1611. ui->synced_i_size = ui->ui_size;
  1612. spin_unlock(&ui->ui_lock);
  1613. mark_inode_clean(c, ui);
  1614. kfree(ino);
  1615. return 0;
  1616. out_release:
  1617. release_head(c, BASEHD);
  1618. out_ro:
  1619. ubifs_ro_mode(c, err);
  1620. finish_reservation(c);
  1621. out_free:
  1622. kfree(ino);
  1623. return err;
  1624. }
  1625. /**
  1626. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1627. * @c: UBIFS file-system description object
  1628. * @host: host inode
  1629. * @inode: extended attribute inode
  1630. * @nm: extended attribute entry name
  1631. *
  1632. * This function delete an extended attribute which is very similar to
  1633. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1634. * updates the target inode. Returns zero in case of success and a negative
  1635. * error code in case of failure.
  1636. */
  1637. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1638. const struct inode *inode,
  1639. const struct fscrypt_name *nm)
  1640. {
  1641. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen, write_len;
  1642. struct ubifs_dent_node *xent;
  1643. struct ubifs_ino_node *ino;
  1644. union ubifs_key xent_key, key1, key2;
  1645. int sync = IS_DIRSYNC(host);
  1646. struct ubifs_inode *host_ui = ubifs_inode(host);
  1647. u8 hash[UBIFS_HASH_ARR_SZ];
  1648. ubifs_assert(c, inode->i_nlink == 0);
  1649. ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
  1650. /*
  1651. * Since we are deleting the inode, we do not bother to attach any data
  1652. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1653. */
  1654. xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  1655. aligned_xlen = ALIGN(xlen, 8);
  1656. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1657. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1658. write_len = len + ubifs_auth_node_sz(c);
  1659. xent = kzalloc(write_len, GFP_NOFS);
  1660. if (!xent)
  1661. return -ENOMEM;
  1662. /* Make reservation before allocating sequence numbers */
  1663. err = make_reservation(c, BASEHD, write_len);
  1664. if (err) {
  1665. kfree(xent);
  1666. return err;
  1667. }
  1668. xent->ch.node_type = UBIFS_XENT_NODE;
  1669. xent_key_init(c, &xent_key, host->i_ino, nm);
  1670. key_write(c, &xent_key, xent->key);
  1671. xent->inum = 0;
  1672. xent->type = get_dent_type(inode->i_mode);
  1673. xent->nlen = cpu_to_le16(fname_len(nm));
  1674. memcpy(xent->name, fname_name(nm), fname_len(nm));
  1675. xent->name[fname_len(nm)] = '\0';
  1676. zero_dent_node_unused(xent);
  1677. ubifs_prep_grp_node(c, xent, xlen, 0);
  1678. ino = (void *)xent + aligned_xlen;
  1679. pack_inode(c, ino, inode, 0);
  1680. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1681. pack_inode(c, ino, host, 1);
  1682. err = ubifs_node_calc_hash(c, ino, hash);
  1683. if (err)
  1684. goto out_release;
  1685. err = write_head(c, BASEHD, xent, write_len, &lnum, &xent_offs, sync);
  1686. if (!sync && !err)
  1687. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1688. release_head(c, BASEHD);
  1689. ubifs_add_auth_dirt(c, lnum);
  1690. kfree(xent);
  1691. if (err)
  1692. goto out_ro;
  1693. /* Remove the extended attribute entry from TNC */
  1694. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1695. if (err)
  1696. goto out_ro;
  1697. err = ubifs_add_dirt(c, lnum, xlen);
  1698. if (err)
  1699. goto out_ro;
  1700. /*
  1701. * Remove all nodes belonging to the extended attribute inode from TNC.
  1702. * Well, there actually must be only one node - the inode itself.
  1703. */
  1704. lowest_ino_key(c, &key1, inode->i_ino);
  1705. highest_ino_key(c, &key2, inode->i_ino);
  1706. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1707. if (err)
  1708. goto out_ro;
  1709. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1710. if (err)
  1711. goto out_ro;
  1712. /* And update TNC with the new host inode position */
  1713. ino_key_init(c, &key1, host->i_ino);
  1714. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen, hash);
  1715. if (err)
  1716. goto out_ro;
  1717. finish_reservation(c);
  1718. spin_lock(&host_ui->ui_lock);
  1719. host_ui->synced_i_size = host_ui->ui_size;
  1720. spin_unlock(&host_ui->ui_lock);
  1721. mark_inode_clean(c, host_ui);
  1722. return 0;
  1723. out_release:
  1724. kfree(xent);
  1725. release_head(c, BASEHD);
  1726. out_ro:
  1727. ubifs_ro_mode(c, err);
  1728. finish_reservation(c);
  1729. return err;
  1730. }
  1731. /**
  1732. * ubifs_jnl_change_xattr - change an extended attribute.
  1733. * @c: UBIFS file-system description object
  1734. * @inode: extended attribute inode
  1735. * @host: host inode
  1736. *
  1737. * This function writes the updated version of an extended attribute inode and
  1738. * the host inode to the journal (to the base head). The host inode is written
  1739. * after the extended attribute inode in order to guarantee that the extended
  1740. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1741. * consequently, the write-buffer is synchronized. This function returns zero
  1742. * in case of success and a negative error code in case of failure.
  1743. */
  1744. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1745. const struct inode *host)
  1746. {
  1747. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1748. struct ubifs_inode *host_ui = ubifs_inode(host);
  1749. struct ubifs_ino_node *ino;
  1750. union ubifs_key key;
  1751. int sync = IS_DIRSYNC(host);
  1752. u8 hash_host[UBIFS_HASH_ARR_SZ];
  1753. u8 hash[UBIFS_HASH_ARR_SZ];
  1754. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1755. ubifs_assert(c, inode->i_nlink > 0);
  1756. ubifs_assert(c, mutex_is_locked(&host_ui->ui_mutex));
  1757. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1758. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1759. aligned_len1 = ALIGN(len1, 8);
  1760. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1761. aligned_len += ubifs_auth_node_sz(c);
  1762. ino = kzalloc(aligned_len, GFP_NOFS);
  1763. if (!ino)
  1764. return -ENOMEM;
  1765. /* Make reservation before allocating sequence numbers */
  1766. err = make_reservation(c, BASEHD, aligned_len);
  1767. if (err)
  1768. goto out_free;
  1769. pack_inode(c, ino, host, 0);
  1770. err = ubifs_node_calc_hash(c, ino, hash_host);
  1771. if (err)
  1772. goto out_release;
  1773. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1774. err = ubifs_node_calc_hash(c, (void *)ino + aligned_len1, hash);
  1775. if (err)
  1776. goto out_release;
  1777. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1778. if (!sync && !err) {
  1779. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1780. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1781. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1782. }
  1783. release_head(c, BASEHD);
  1784. if (err)
  1785. goto out_ro;
  1786. ubifs_add_auth_dirt(c, lnum);
  1787. ino_key_init(c, &key, host->i_ino);
  1788. err = ubifs_tnc_add(c, &key, lnum, offs, len1, hash_host);
  1789. if (err)
  1790. goto out_ro;
  1791. ino_key_init(c, &key, inode->i_ino);
  1792. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2, hash);
  1793. if (err)
  1794. goto out_ro;
  1795. finish_reservation(c);
  1796. spin_lock(&host_ui->ui_lock);
  1797. host_ui->synced_i_size = host_ui->ui_size;
  1798. spin_unlock(&host_ui->ui_lock);
  1799. mark_inode_clean(c, host_ui);
  1800. kfree(ino);
  1801. return 0;
  1802. out_release:
  1803. release_head(c, BASEHD);
  1804. out_ro:
  1805. ubifs_ro_mode(c, err);
  1806. finish_reservation(c);
  1807. out_free:
  1808. kfree(ino);
  1809. return err;
  1810. }