journal.c 48 KB

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