journal.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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()'. 'write_node()' and 'write_head()' functions also unlock
  96. * it. Returns zero in case of success, %-EAGAIN if commit has to be done, and
  97. * other negative error codes in case of other failures.
  98. */
  99. static int reserve_space(struct ubifs_info *c, int jhead, int len)
  100. {
  101. int err = 0, err1, retries = 0, avail, lnum, offs, squeeze;
  102. struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
  103. /*
  104. * Typically, the base head has smaller nodes written to it, so it is
  105. * better to try to allocate space at the ends of eraseblocks. This is
  106. * what the squeeze parameter does.
  107. */
  108. ubifs_assert(!c->ro_media && !c->ro_mount);
  109. squeeze = (jhead == BASEHD);
  110. again:
  111. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  112. if (c->ro_error) {
  113. err = -EROFS;
  114. goto out_unlock;
  115. }
  116. avail = c->leb_size - wbuf->offs - wbuf->used;
  117. if (wbuf->lnum != -1 && avail >= len)
  118. return 0;
  119. /*
  120. * Write buffer wasn't seek'ed or there is no enough space - look for an
  121. * LEB with some empty space.
  122. */
  123. lnum = ubifs_find_free_space(c, len, &offs, squeeze);
  124. if (lnum >= 0)
  125. goto out;
  126. err = lnum;
  127. if (err != -ENOSPC)
  128. goto out_unlock;
  129. /*
  130. * No free space, we have to run garbage collector to make
  131. * some. But the write-buffer mutex has to be unlocked because
  132. * GC also takes it.
  133. */
  134. dbg_jnl("no free space in jhead %s, run GC", dbg_jhead(jhead));
  135. mutex_unlock(&wbuf->io_mutex);
  136. lnum = ubifs_garbage_collect(c, 0);
  137. if (lnum < 0) {
  138. err = lnum;
  139. if (err != -ENOSPC)
  140. return err;
  141. /*
  142. * GC could not make a free LEB. But someone else may
  143. * have allocated new bud for this journal head,
  144. * because we dropped @wbuf->io_mutex, so try once
  145. * again.
  146. */
  147. dbg_jnl("GC couldn't make a free LEB for jhead %s",
  148. dbg_jhead(jhead));
  149. if (retries++ < 2) {
  150. dbg_jnl("retry (%d)", retries);
  151. goto again;
  152. }
  153. dbg_jnl("return -ENOSPC");
  154. return err;
  155. }
  156. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  157. dbg_jnl("got LEB %d for jhead %s", lnum, dbg_jhead(jhead));
  158. avail = c->leb_size - wbuf->offs - wbuf->used;
  159. if (wbuf->lnum != -1 && avail >= len) {
  160. /*
  161. * Someone else has switched the journal head and we have
  162. * enough space now. This happens when more than one process is
  163. * trying to write to the same journal head at the same time.
  164. */
  165. dbg_jnl("return LEB %d back, already have LEB %d:%d",
  166. lnum, wbuf->lnum, wbuf->offs + wbuf->used);
  167. err = ubifs_return_leb(c, lnum);
  168. if (err)
  169. goto out_unlock;
  170. return 0;
  171. }
  172. offs = 0;
  173. out:
  174. /*
  175. * Make sure we synchronize the write-buffer before we add the new bud
  176. * to the log. Otherwise we may have a power cut after the log
  177. * reference node for the last bud (@lnum) is written but before the
  178. * write-buffer data are written to the next-to-last bud
  179. * (@wbuf->lnum). And the effect would be that the recovery would see
  180. * that there is corruption in the next-to-last bud.
  181. */
  182. err = ubifs_wbuf_sync_nolock(wbuf);
  183. if (err)
  184. goto out_return;
  185. err = ubifs_add_bud_to_log(c, jhead, lnum, offs);
  186. if (err)
  187. goto out_return;
  188. err = ubifs_wbuf_seek_nolock(wbuf, lnum, offs);
  189. if (err)
  190. goto out_unlock;
  191. return 0;
  192. out_unlock:
  193. mutex_unlock(&wbuf->io_mutex);
  194. return err;
  195. out_return:
  196. /* An error occurred and the LEB has to be returned to lprops */
  197. ubifs_assert(err < 0);
  198. err1 = ubifs_return_leb(c, lnum);
  199. if (err1 && err == -EAGAIN)
  200. /*
  201. * Return original error code only if it is not %-EAGAIN,
  202. * which is not really an error. Otherwise, return the error
  203. * code of 'ubifs_return_leb()'.
  204. */
  205. err = err1;
  206. mutex_unlock(&wbuf->io_mutex);
  207. return err;
  208. }
  209. /**
  210. * write_node - write node to a journal head.
  211. * @c: UBIFS file-system description object
  212. * @jhead: journal head
  213. * @node: node to write
  214. * @len: node length
  215. * @lnum: LEB number written is returned here
  216. * @offs: offset written is returned here
  217. *
  218. * This function writes a node to reserved space of journal head @jhead.
  219. * Returns zero in case of success and a negative error code in case of
  220. * failure.
  221. */
  222. static int write_node(struct ubifs_info *c, int jhead, void *node, int len,
  223. int *lnum, int *offs)
  224. {
  225. struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
  226. ubifs_assert(jhead != GCHD);
  227. *lnum = c->jheads[jhead].wbuf.lnum;
  228. *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
  229. dbg_jnl("jhead %s, LEB %d:%d, len %d",
  230. dbg_jhead(jhead), *lnum, *offs, len);
  231. ubifs_prepare_node(c, node, len, 0);
  232. return ubifs_wbuf_write_nolock(wbuf, node, len);
  233. }
  234. /**
  235. * write_head - write data to a journal head.
  236. * @c: UBIFS file-system description object
  237. * @jhead: journal head
  238. * @buf: buffer to write
  239. * @len: length to write
  240. * @lnum: LEB number written is returned here
  241. * @offs: offset written is returned here
  242. * @sync: non-zero if the write-buffer has to by synchronized
  243. *
  244. * This function is the same as 'write_node()' but it does not assume the
  245. * buffer it is writing is a node, so it does not prepare it (which means
  246. * initializing common header and calculating CRC).
  247. */
  248. static int write_head(struct ubifs_info *c, int jhead, void *buf, int len,
  249. int *lnum, int *offs, int sync)
  250. {
  251. int err;
  252. struct ubifs_wbuf *wbuf = &c->jheads[jhead].wbuf;
  253. ubifs_assert(jhead != GCHD);
  254. *lnum = c->jheads[jhead].wbuf.lnum;
  255. *offs = c->jheads[jhead].wbuf.offs + c->jheads[jhead].wbuf.used;
  256. dbg_jnl("jhead %s, LEB %d:%d, len %d",
  257. dbg_jhead(jhead), *lnum, *offs, len);
  258. err = ubifs_wbuf_write_nolock(wbuf, buf, len);
  259. if (err)
  260. return err;
  261. if (sync)
  262. err = ubifs_wbuf_sync_nolock(wbuf);
  263. return err;
  264. }
  265. /**
  266. * make_reservation - reserve journal space.
  267. * @c: UBIFS file-system description object
  268. * @jhead: journal head
  269. * @len: how many bytes to reserve
  270. *
  271. * This function makes space reservation in journal head @jhead. The function
  272. * takes the commit lock and locks the journal head, and the caller has to
  273. * unlock the head and finish the reservation with 'finish_reservation()'.
  274. * Returns zero in case of success and a negative error code in case of
  275. * failure.
  276. *
  277. * Note, the journal head may be unlocked as soon as the data is written, while
  278. * the commit lock has to be released after the data has been added to the
  279. * TNC.
  280. */
  281. static int make_reservation(struct ubifs_info *c, int jhead, int len)
  282. {
  283. int err, cmt_retries = 0, nospc_retries = 0;
  284. again:
  285. down_read(&c->commit_sem);
  286. err = reserve_space(c, jhead, len);
  287. if (!err)
  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(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. mark_inode_clean(c, ui);
  601. mark_inode_clean(c, host_ui);
  602. return 0;
  603. out_finish:
  604. finish_reservation(c);
  605. out_free:
  606. kfree(dent);
  607. return err;
  608. out_release:
  609. release_head(c, BASEHD);
  610. kfree(dent);
  611. out_ro:
  612. ubifs_ro_mode(c, err);
  613. if (last_reference)
  614. ubifs_delete_orphan(c, inode->i_ino);
  615. finish_reservation(c);
  616. return err;
  617. }
  618. /**
  619. * ubifs_jnl_write_data - write a data node to the journal.
  620. * @c: UBIFS file-system description object
  621. * @inode: inode the data node belongs to
  622. * @key: node key
  623. * @buf: buffer to write
  624. * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
  625. *
  626. * This function writes a data node to the journal. Returns %0 if the data node
  627. * was successfully written, and a negative error code in case of failure.
  628. */
  629. int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
  630. const union ubifs_key *key, const void *buf, int len)
  631. {
  632. struct ubifs_data_node *data;
  633. int err, lnum, offs, compr_type, out_len, compr_len;
  634. int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
  635. struct ubifs_inode *ui = ubifs_inode(inode);
  636. bool encrypted = ubifs_crypt_is_encrypted(inode);
  637. dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
  638. (unsigned long)key_inum(c, key), key_block(c, key), len);
  639. ubifs_assert(len <= UBIFS_BLOCK_SIZE);
  640. if (encrypted)
  641. dlen += UBIFS_CIPHER_BLOCK_SIZE;
  642. data = kmalloc(dlen, GFP_NOFS | __GFP_NOWARN);
  643. if (!data) {
  644. /*
  645. * Fall-back to the write reserve buffer. Note, we might be
  646. * currently on the memory reclaim path, when the kernel is
  647. * trying to free some memory by writing out dirty pages. The
  648. * write reserve buffer helps us to guarantee that we are
  649. * always able to write the data.
  650. */
  651. allocated = 0;
  652. mutex_lock(&c->write_reserve_mutex);
  653. data = c->write_reserve_buf;
  654. }
  655. data->ch.node_type = UBIFS_DATA_NODE;
  656. key_write(c, key, &data->key);
  657. data->size = cpu_to_le32(len);
  658. if (!(ui->flags & UBIFS_COMPR_FL))
  659. /* Compression is disabled for this inode */
  660. compr_type = UBIFS_COMPR_NONE;
  661. else
  662. compr_type = ui->compr_type;
  663. out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
  664. ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
  665. ubifs_assert(compr_len <= UBIFS_BLOCK_SIZE);
  666. if (encrypted) {
  667. err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
  668. if (err)
  669. goto out_free;
  670. } else {
  671. data->compr_size = 0;
  672. out_len = compr_len;
  673. }
  674. dlen = UBIFS_DATA_NODE_SZ + out_len;
  675. data->compr_type = cpu_to_le16(compr_type);
  676. /* Make reservation before allocating sequence numbers */
  677. err = make_reservation(c, DATAHD, dlen);
  678. if (err)
  679. goto out_free;
  680. err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
  681. if (err)
  682. goto out_release;
  683. ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
  684. release_head(c, DATAHD);
  685. err = ubifs_tnc_add(c, key, lnum, offs, dlen);
  686. if (err)
  687. goto out_ro;
  688. finish_reservation(c);
  689. if (!allocated)
  690. mutex_unlock(&c->write_reserve_mutex);
  691. else
  692. kfree(data);
  693. return 0;
  694. out_release:
  695. release_head(c, DATAHD);
  696. out_ro:
  697. ubifs_ro_mode(c, err);
  698. finish_reservation(c);
  699. out_free:
  700. if (!allocated)
  701. mutex_unlock(&c->write_reserve_mutex);
  702. else
  703. kfree(data);
  704. return err;
  705. }
  706. /**
  707. * ubifs_jnl_write_inode - flush inode to the journal.
  708. * @c: UBIFS file-system description object
  709. * @inode: inode to flush
  710. *
  711. * This function writes inode @inode to the journal. If the inode is
  712. * synchronous, it also synchronizes the write-buffer. Returns zero in case of
  713. * success and a negative error code in case of failure.
  714. */
  715. int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
  716. {
  717. int err, lnum, offs;
  718. struct ubifs_ino_node *ino;
  719. struct ubifs_inode *ui = ubifs_inode(inode);
  720. int sync = 0, len = UBIFS_INO_NODE_SZ, last_reference = !inode->i_nlink;
  721. dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
  722. /*
  723. * If the inode is being deleted, do not write the attached data. No
  724. * need to synchronize the write-buffer either.
  725. */
  726. if (!last_reference) {
  727. len += ui->data_len;
  728. sync = IS_SYNC(inode);
  729. }
  730. ino = kmalloc(len, GFP_NOFS);
  731. if (!ino)
  732. return -ENOMEM;
  733. /* Make reservation before allocating sequence numbers */
  734. err = make_reservation(c, BASEHD, len);
  735. if (err)
  736. goto out_free;
  737. pack_inode(c, ino, inode, 1);
  738. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  739. if (err)
  740. goto out_release;
  741. if (!sync)
  742. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  743. inode->i_ino);
  744. release_head(c, BASEHD);
  745. if (last_reference) {
  746. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  747. if (err)
  748. goto out_ro;
  749. ubifs_delete_orphan(c, inode->i_ino);
  750. err = ubifs_add_dirt(c, lnum, len);
  751. } else {
  752. union ubifs_key key;
  753. ino_key_init(c, &key, inode->i_ino);
  754. err = ubifs_tnc_add(c, &key, lnum, offs, len);
  755. }
  756. if (err)
  757. goto out_ro;
  758. finish_reservation(c);
  759. spin_lock(&ui->ui_lock);
  760. ui->synced_i_size = ui->ui_size;
  761. spin_unlock(&ui->ui_lock);
  762. kfree(ino);
  763. return 0;
  764. out_release:
  765. release_head(c, BASEHD);
  766. out_ro:
  767. ubifs_ro_mode(c, err);
  768. finish_reservation(c);
  769. out_free:
  770. kfree(ino);
  771. return err;
  772. }
  773. /**
  774. * ubifs_jnl_delete_inode - delete an inode.
  775. * @c: UBIFS file-system description object
  776. * @inode: inode to delete
  777. *
  778. * This function deletes inode @inode which includes removing it from orphans,
  779. * deleting it from TNC and, in some cases, writing a deletion inode to the
  780. * journal.
  781. *
  782. * When regular file inodes are unlinked or a directory inode is removed, the
  783. * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
  784. * direntry to the media, and adds the inode to orphans. After this, when the
  785. * last reference to this inode has been dropped, this function is called. In
  786. * general, it has to write one more deletion inode to the media, because if
  787. * a commit happened between 'ubifs_jnl_update()' and
  788. * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
  789. * anymore, and in fact it might not be on the flash anymore, because it might
  790. * have been garbage-collected already. And for optimization reasons UBIFS does
  791. * not read the orphan area if it has been unmounted cleanly, so it would have
  792. * no indication in the journal that there is a deleted inode which has to be
  793. * removed from TNC.
  794. *
  795. * However, if there was no commit between 'ubifs_jnl_update()' and
  796. * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
  797. * inode to the media for the second time. And this is quite a typical case.
  798. *
  799. * This function returns zero in case of success and a negative error code in
  800. * case of failure.
  801. */
  802. int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
  803. {
  804. int err;
  805. struct ubifs_inode *ui = ubifs_inode(inode);
  806. ubifs_assert(inode->i_nlink == 0);
  807. if (ui->del_cmtno != c->cmt_no)
  808. /* A commit happened for sure */
  809. return ubifs_jnl_write_inode(c, inode);
  810. down_read(&c->commit_sem);
  811. /*
  812. * Check commit number again, because the first test has been done
  813. * without @c->commit_sem, so a commit might have happened.
  814. */
  815. if (ui->del_cmtno != c->cmt_no) {
  816. up_read(&c->commit_sem);
  817. return ubifs_jnl_write_inode(c, inode);
  818. }
  819. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  820. if (err)
  821. ubifs_ro_mode(c, err);
  822. else
  823. ubifs_delete_orphan(c, inode->i_ino);
  824. up_read(&c->commit_sem);
  825. return err;
  826. }
  827. /**
  828. * ubifs_jnl_xrename - cross rename two directory entries.
  829. * @c: UBIFS file-system description object
  830. * @fst_dir: parent inode of 1st directory entry to exchange
  831. * @fst_inode: 1st inode to exchange
  832. * @fst_nm: name of 1st inode to exchange
  833. * @snd_dir: parent inode of 2nd directory entry to exchange
  834. * @snd_inode: 2nd inode to exchange
  835. * @snd_nm: name of 2nd inode to exchange
  836. * @sync: non-zero if the write-buffer has to be synchronized
  837. *
  838. * This function implements the cross rename operation which may involve
  839. * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
  840. * and returns zero on success. In case of failure, a negative error code is
  841. * returned.
  842. */
  843. int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  844. const struct inode *fst_inode,
  845. const struct fscrypt_name *fst_nm,
  846. const struct inode *snd_dir,
  847. const struct inode *snd_inode,
  848. const struct fscrypt_name *snd_nm, int sync)
  849. {
  850. union ubifs_key key;
  851. struct ubifs_dent_node *dent1, *dent2;
  852. int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
  853. int aligned_dlen1, aligned_dlen2;
  854. int twoparents = (fst_dir != snd_dir);
  855. void *p;
  856. ubifs_assert(ubifs_inode(fst_dir)->data_len == 0);
  857. ubifs_assert(ubifs_inode(snd_dir)->data_len == 0);
  858. ubifs_assert(mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
  859. ubifs_assert(mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
  860. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
  861. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
  862. aligned_dlen1 = ALIGN(dlen1, 8);
  863. aligned_dlen2 = ALIGN(dlen2, 8);
  864. len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
  865. if (twoparents)
  866. len += plen;
  867. dent1 = kzalloc(len, GFP_NOFS);
  868. if (!dent1)
  869. return -ENOMEM;
  870. /* Make reservation before allocating sequence numbers */
  871. err = make_reservation(c, BASEHD, len);
  872. if (err)
  873. goto out_free;
  874. /* Make new dent for 1st entry */
  875. dent1->ch.node_type = UBIFS_DENT_NODE;
  876. dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
  877. dent1->inum = cpu_to_le64(fst_inode->i_ino);
  878. dent1->type = get_dent_type(fst_inode->i_mode);
  879. dent1->nlen = cpu_to_le16(fname_len(snd_nm));
  880. memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
  881. dent1->name[fname_len(snd_nm)] = '\0';
  882. set_dent_cookie(c, dent1);
  883. zero_dent_node_unused(dent1);
  884. ubifs_prep_grp_node(c, dent1, dlen1, 0);
  885. /* Make new dent for 2nd entry */
  886. dent2 = (void *)dent1 + aligned_dlen1;
  887. dent2->ch.node_type = UBIFS_DENT_NODE;
  888. dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
  889. dent2->inum = cpu_to_le64(snd_inode->i_ino);
  890. dent2->type = get_dent_type(snd_inode->i_mode);
  891. dent2->nlen = cpu_to_le16(fname_len(fst_nm));
  892. memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
  893. dent2->name[fname_len(fst_nm)] = '\0';
  894. set_dent_cookie(c, dent2);
  895. zero_dent_node_unused(dent2);
  896. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  897. p = (void *)dent2 + aligned_dlen2;
  898. if (!twoparents)
  899. pack_inode(c, p, fst_dir, 1);
  900. else {
  901. pack_inode(c, p, fst_dir, 0);
  902. p += ALIGN(plen, 8);
  903. pack_inode(c, p, snd_dir, 1);
  904. }
  905. err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
  906. if (err)
  907. goto out_release;
  908. if (!sync) {
  909. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  910. ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
  911. ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
  912. }
  913. release_head(c, BASEHD);
  914. dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
  915. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, snd_nm);
  916. if (err)
  917. goto out_ro;
  918. offs += aligned_dlen1;
  919. dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
  920. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, fst_nm);
  921. if (err)
  922. goto out_ro;
  923. offs += aligned_dlen2;
  924. ino_key_init(c, &key, fst_dir->i_ino);
  925. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  926. if (err)
  927. goto out_ro;
  928. if (twoparents) {
  929. offs += ALIGN(plen, 8);
  930. ino_key_init(c, &key, snd_dir->i_ino);
  931. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  932. if (err)
  933. goto out_ro;
  934. }
  935. finish_reservation(c);
  936. mark_inode_clean(c, ubifs_inode(fst_dir));
  937. if (twoparents)
  938. mark_inode_clean(c, ubifs_inode(snd_dir));
  939. kfree(dent1);
  940. return 0;
  941. out_release:
  942. release_head(c, BASEHD);
  943. out_ro:
  944. ubifs_ro_mode(c, err);
  945. finish_reservation(c);
  946. out_free:
  947. kfree(dent1);
  948. return err;
  949. }
  950. /**
  951. * ubifs_jnl_rename - rename a directory entry.
  952. * @c: UBIFS file-system description object
  953. * @old_dir: parent inode of directory entry to rename
  954. * @old_dentry: directory entry to rename
  955. * @new_dir: parent inode of directory entry to rename
  956. * @new_dentry: new directory entry (or directory entry to replace)
  957. * @sync: non-zero if the write-buffer has to be synchronized
  958. *
  959. * This function implements the re-name operation which may involve writing up
  960. * to 4 inodes and 2 directory entries. It marks the written inodes as clean
  961. * and returns zero on success. In case of failure, a negative error code is
  962. * returned.
  963. */
  964. int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
  965. const struct inode *old_inode,
  966. const struct fscrypt_name *old_nm,
  967. const struct inode *new_dir,
  968. const struct inode *new_inode,
  969. const struct fscrypt_name *new_nm,
  970. const struct inode *whiteout, int sync)
  971. {
  972. void *p;
  973. union ubifs_key key;
  974. struct ubifs_dent_node *dent, *dent2;
  975. int err, dlen1, dlen2, ilen, lnum, offs, len;
  976. int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
  977. int last_reference = !!(new_inode && new_inode->i_nlink == 0);
  978. int move = (old_dir != new_dir);
  979. struct ubifs_inode *uninitialized_var(new_ui);
  980. ubifs_assert(ubifs_inode(old_dir)->data_len == 0);
  981. ubifs_assert(ubifs_inode(new_dir)->data_len == 0);
  982. ubifs_assert(mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
  983. ubifs_assert(mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
  984. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
  985. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
  986. if (new_inode) {
  987. new_ui = ubifs_inode(new_inode);
  988. ubifs_assert(mutex_is_locked(&new_ui->ui_mutex));
  989. ilen = UBIFS_INO_NODE_SZ;
  990. if (!last_reference)
  991. ilen += new_ui->data_len;
  992. } else
  993. ilen = 0;
  994. aligned_dlen1 = ALIGN(dlen1, 8);
  995. aligned_dlen2 = ALIGN(dlen2, 8);
  996. len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
  997. if (move)
  998. len += plen;
  999. dent = kzalloc(len, GFP_NOFS);
  1000. if (!dent)
  1001. return -ENOMEM;
  1002. /* Make reservation before allocating sequence numbers */
  1003. err = make_reservation(c, BASEHD, len);
  1004. if (err)
  1005. goto out_free;
  1006. /* Make new dent */
  1007. dent->ch.node_type = UBIFS_DENT_NODE;
  1008. dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
  1009. dent->inum = cpu_to_le64(old_inode->i_ino);
  1010. dent->type = get_dent_type(old_inode->i_mode);
  1011. dent->nlen = cpu_to_le16(fname_len(new_nm));
  1012. memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
  1013. dent->name[fname_len(new_nm)] = '\0';
  1014. set_dent_cookie(c, dent);
  1015. zero_dent_node_unused(dent);
  1016. ubifs_prep_grp_node(c, dent, dlen1, 0);
  1017. dent2 = (void *)dent + aligned_dlen1;
  1018. dent2->ch.node_type = UBIFS_DENT_NODE;
  1019. dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
  1020. if (whiteout) {
  1021. dent2->inum = cpu_to_le64(whiteout->i_ino);
  1022. dent2->type = get_dent_type(whiteout->i_mode);
  1023. } else {
  1024. /* Make deletion dent */
  1025. dent2->inum = 0;
  1026. dent2->type = DT_UNKNOWN;
  1027. }
  1028. dent2->nlen = cpu_to_le16(fname_len(old_nm));
  1029. memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
  1030. dent2->name[fname_len(old_nm)] = '\0';
  1031. set_dent_cookie(c, dent2);
  1032. zero_dent_node_unused(dent2);
  1033. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1034. p = (void *)dent2 + aligned_dlen2;
  1035. if (new_inode) {
  1036. pack_inode(c, p, new_inode, 0);
  1037. p += ALIGN(ilen, 8);
  1038. }
  1039. if (!move)
  1040. pack_inode(c, p, old_dir, 1);
  1041. else {
  1042. pack_inode(c, p, old_dir, 0);
  1043. p += ALIGN(plen, 8);
  1044. pack_inode(c, p, new_dir, 1);
  1045. }
  1046. if (last_reference) {
  1047. err = ubifs_add_orphan(c, new_inode->i_ino);
  1048. if (err) {
  1049. release_head(c, BASEHD);
  1050. goto out_finish;
  1051. }
  1052. new_ui->del_cmtno = c->cmt_no;
  1053. }
  1054. err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
  1055. if (err)
  1056. goto out_release;
  1057. if (!sync) {
  1058. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1059. ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
  1060. ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
  1061. if (new_inode)
  1062. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1063. new_inode->i_ino);
  1064. }
  1065. release_head(c, BASEHD);
  1066. dent_key_init(c, &key, new_dir->i_ino, new_nm);
  1067. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, new_nm);
  1068. if (err)
  1069. goto out_ro;
  1070. offs += aligned_dlen1;
  1071. if (whiteout) {
  1072. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1073. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, old_nm);
  1074. if (err)
  1075. goto out_ro;
  1076. ubifs_delete_orphan(c, whiteout->i_ino);
  1077. } else {
  1078. err = ubifs_add_dirt(c, lnum, dlen2);
  1079. if (err)
  1080. goto out_ro;
  1081. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1082. err = ubifs_tnc_remove_nm(c, &key, old_nm);
  1083. if (err)
  1084. goto out_ro;
  1085. }
  1086. offs += aligned_dlen2;
  1087. if (new_inode) {
  1088. ino_key_init(c, &key, new_inode->i_ino);
  1089. err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
  1090. if (err)
  1091. goto out_ro;
  1092. offs += ALIGN(ilen, 8);
  1093. }
  1094. ino_key_init(c, &key, old_dir->i_ino);
  1095. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  1096. if (err)
  1097. goto out_ro;
  1098. if (move) {
  1099. offs += ALIGN(plen, 8);
  1100. ino_key_init(c, &key, new_dir->i_ino);
  1101. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  1102. if (err)
  1103. goto out_ro;
  1104. }
  1105. finish_reservation(c);
  1106. if (new_inode) {
  1107. mark_inode_clean(c, new_ui);
  1108. spin_lock(&new_ui->ui_lock);
  1109. new_ui->synced_i_size = new_ui->ui_size;
  1110. spin_unlock(&new_ui->ui_lock);
  1111. }
  1112. mark_inode_clean(c, ubifs_inode(old_dir));
  1113. if (move)
  1114. mark_inode_clean(c, ubifs_inode(new_dir));
  1115. kfree(dent);
  1116. return 0;
  1117. out_release:
  1118. release_head(c, BASEHD);
  1119. out_ro:
  1120. ubifs_ro_mode(c, err);
  1121. if (last_reference)
  1122. ubifs_delete_orphan(c, new_inode->i_ino);
  1123. out_finish:
  1124. finish_reservation(c);
  1125. out_free:
  1126. kfree(dent);
  1127. return err;
  1128. }
  1129. /**
  1130. * truncate_data_node - re-compress/encrypt a truncated data node.
  1131. * @c: UBIFS file-system description object
  1132. * @inode: inode which referes to the data node
  1133. * @block: data block number
  1134. * @dn: data node to re-compress
  1135. * @new_len: new length
  1136. *
  1137. * This function is used when an inode is truncated and the last data node of
  1138. * the inode has to be re-compressed/encrypted and re-written.
  1139. */
  1140. static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
  1141. unsigned int block, struct ubifs_data_node *dn,
  1142. int *new_len)
  1143. {
  1144. void *buf;
  1145. int err, dlen, compr_type, out_len, old_dlen;
  1146. out_len = le32_to_cpu(dn->size);
  1147. buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
  1148. if (!buf)
  1149. return -ENOMEM;
  1150. dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  1151. compr_type = le16_to_cpu(dn->compr_type);
  1152. if (ubifs_crypt_is_encrypted(inode)) {
  1153. err = ubifs_decrypt(inode, dn, &dlen, block);
  1154. if (err)
  1155. goto out;
  1156. }
  1157. if (compr_type == UBIFS_COMPR_NONE) {
  1158. out_len = *new_len;
  1159. } else {
  1160. err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
  1161. if (err)
  1162. goto out;
  1163. ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
  1164. }
  1165. if (ubifs_crypt_is_encrypted(inode)) {
  1166. err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
  1167. if (err)
  1168. goto out;
  1169. out_len = old_dlen;
  1170. } else {
  1171. dn->compr_size = 0;
  1172. }
  1173. ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
  1174. dn->compr_type = cpu_to_le16(compr_type);
  1175. dn->size = cpu_to_le32(*new_len);
  1176. *new_len = UBIFS_DATA_NODE_SZ + out_len;
  1177. err = 0;
  1178. out:
  1179. kfree(buf);
  1180. return err;
  1181. }
  1182. /**
  1183. * ubifs_jnl_truncate - update the journal for a truncation.
  1184. * @c: UBIFS file-system description object
  1185. * @inode: inode to truncate
  1186. * @old_size: old size
  1187. * @new_size: new size
  1188. *
  1189. * When the size of a file decreases due to truncation, a truncation node is
  1190. * written, the journal tree is updated, and the last data block is re-written
  1191. * if it has been affected. The inode is also updated in order to synchronize
  1192. * the new inode size.
  1193. *
  1194. * This function marks the inode as clean and returns zero on success. In case
  1195. * of failure, a negative error code is returned.
  1196. */
  1197. int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
  1198. loff_t old_size, loff_t new_size)
  1199. {
  1200. union ubifs_key key, to_key;
  1201. struct ubifs_ino_node *ino;
  1202. struct ubifs_trun_node *trun;
  1203. struct ubifs_data_node *uninitialized_var(dn);
  1204. int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
  1205. struct ubifs_inode *ui = ubifs_inode(inode);
  1206. ino_t inum = inode->i_ino;
  1207. unsigned int blk;
  1208. dbg_jnl("ino %lu, size %lld -> %lld",
  1209. (unsigned long)inum, old_size, new_size);
  1210. ubifs_assert(!ui->data_len);
  1211. ubifs_assert(S_ISREG(inode->i_mode));
  1212. ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  1213. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1214. UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
  1215. ino = kmalloc(sz, GFP_NOFS);
  1216. if (!ino)
  1217. return -ENOMEM;
  1218. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1219. trun->ch.node_type = UBIFS_TRUN_NODE;
  1220. trun->inum = cpu_to_le32(inum);
  1221. trun->old_size = cpu_to_le64(old_size);
  1222. trun->new_size = cpu_to_le64(new_size);
  1223. zero_trun_node_unused(trun);
  1224. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1225. if (dlen) {
  1226. /* Get last data block so it can be truncated */
  1227. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1228. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1229. data_key_init(c, &key, inum, blk);
  1230. dbg_jnlk(&key, "last block key ");
  1231. err = ubifs_tnc_lookup(c, &key, dn);
  1232. if (err == -ENOENT)
  1233. dlen = 0; /* Not found (so it is a hole) */
  1234. else if (err)
  1235. goto out_free;
  1236. else {
  1237. if (le32_to_cpu(dn->size) <= dlen)
  1238. dlen = 0; /* Nothing to do */
  1239. else {
  1240. err = truncate_data_node(c, inode, blk, dn, &dlen);
  1241. if (err)
  1242. goto out_free;
  1243. }
  1244. }
  1245. }
  1246. /* Must make reservation before allocating sequence numbers */
  1247. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1248. if (dlen)
  1249. len += dlen;
  1250. err = make_reservation(c, BASEHD, len);
  1251. if (err)
  1252. goto out_free;
  1253. pack_inode(c, ino, inode, 0);
  1254. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1255. if (dlen)
  1256. ubifs_prep_grp_node(c, dn, dlen, 1);
  1257. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1258. if (err)
  1259. goto out_release;
  1260. if (!sync)
  1261. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1262. release_head(c, BASEHD);
  1263. if (dlen) {
  1264. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1265. err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
  1266. if (err)
  1267. goto out_ro;
  1268. }
  1269. ino_key_init(c, &key, inum);
  1270. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
  1271. if (err)
  1272. goto out_ro;
  1273. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1274. if (err)
  1275. goto out_ro;
  1276. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1277. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1278. data_key_init(c, &key, inum, blk);
  1279. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1280. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
  1281. data_key_init(c, &to_key, inum, blk);
  1282. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1283. if (err)
  1284. goto out_ro;
  1285. finish_reservation(c);
  1286. spin_lock(&ui->ui_lock);
  1287. ui->synced_i_size = ui->ui_size;
  1288. spin_unlock(&ui->ui_lock);
  1289. mark_inode_clean(c, ui);
  1290. kfree(ino);
  1291. return 0;
  1292. out_release:
  1293. release_head(c, BASEHD);
  1294. out_ro:
  1295. ubifs_ro_mode(c, err);
  1296. finish_reservation(c);
  1297. out_free:
  1298. kfree(ino);
  1299. return err;
  1300. }
  1301. /**
  1302. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1303. * @c: UBIFS file-system description object
  1304. * @host: host inode
  1305. * @inode: extended attribute inode
  1306. * @nm: extended attribute entry name
  1307. *
  1308. * This function delete an extended attribute which is very similar to
  1309. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1310. * updates the target inode. Returns zero in case of success and a negative
  1311. * error code in case of failure.
  1312. */
  1313. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1314. const struct inode *inode,
  1315. const struct fscrypt_name *nm)
  1316. {
  1317. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
  1318. struct ubifs_dent_node *xent;
  1319. struct ubifs_ino_node *ino;
  1320. union ubifs_key xent_key, key1, key2;
  1321. int sync = IS_DIRSYNC(host);
  1322. struct ubifs_inode *host_ui = ubifs_inode(host);
  1323. ubifs_assert(inode->i_nlink == 0);
  1324. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1325. /*
  1326. * Since we are deleting the inode, we do not bother to attach any data
  1327. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1328. */
  1329. xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  1330. aligned_xlen = ALIGN(xlen, 8);
  1331. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1332. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1333. xent = kzalloc(len, GFP_NOFS);
  1334. if (!xent)
  1335. return -ENOMEM;
  1336. /* Make reservation before allocating sequence numbers */
  1337. err = make_reservation(c, BASEHD, len);
  1338. if (err) {
  1339. kfree(xent);
  1340. return err;
  1341. }
  1342. xent->ch.node_type = UBIFS_XENT_NODE;
  1343. xent_key_init(c, &xent_key, host->i_ino, nm);
  1344. key_write(c, &xent_key, xent->key);
  1345. xent->inum = 0;
  1346. xent->type = get_dent_type(inode->i_mode);
  1347. xent->nlen = cpu_to_le16(fname_len(nm));
  1348. memcpy(xent->name, fname_name(nm), fname_len(nm));
  1349. xent->name[fname_len(nm)] = '\0';
  1350. zero_dent_node_unused(xent);
  1351. ubifs_prep_grp_node(c, xent, xlen, 0);
  1352. ino = (void *)xent + aligned_xlen;
  1353. pack_inode(c, ino, inode, 0);
  1354. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1355. pack_inode(c, ino, host, 1);
  1356. err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
  1357. if (!sync && !err)
  1358. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1359. release_head(c, BASEHD);
  1360. kfree(xent);
  1361. if (err)
  1362. goto out_ro;
  1363. /* Remove the extended attribute entry from TNC */
  1364. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1365. if (err)
  1366. goto out_ro;
  1367. err = ubifs_add_dirt(c, lnum, xlen);
  1368. if (err)
  1369. goto out_ro;
  1370. /*
  1371. * Remove all nodes belonging to the extended attribute inode from TNC.
  1372. * Well, there actually must be only one node - the inode itself.
  1373. */
  1374. lowest_ino_key(c, &key1, inode->i_ino);
  1375. highest_ino_key(c, &key2, inode->i_ino);
  1376. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1377. if (err)
  1378. goto out_ro;
  1379. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1380. if (err)
  1381. goto out_ro;
  1382. /* And update TNC with the new host inode position */
  1383. ino_key_init(c, &key1, host->i_ino);
  1384. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
  1385. if (err)
  1386. goto out_ro;
  1387. finish_reservation(c);
  1388. spin_lock(&host_ui->ui_lock);
  1389. host_ui->synced_i_size = host_ui->ui_size;
  1390. spin_unlock(&host_ui->ui_lock);
  1391. mark_inode_clean(c, host_ui);
  1392. return 0;
  1393. out_ro:
  1394. ubifs_ro_mode(c, err);
  1395. finish_reservation(c);
  1396. return err;
  1397. }
  1398. /**
  1399. * ubifs_jnl_change_xattr - change an extended attribute.
  1400. * @c: UBIFS file-system description object
  1401. * @inode: extended attribute inode
  1402. * @host: host inode
  1403. *
  1404. * This function writes the updated version of an extended attribute inode and
  1405. * the host inode to the journal (to the base head). The host inode is written
  1406. * after the extended attribute inode in order to guarantee that the extended
  1407. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1408. * consequently, the write-buffer is synchronized. This function returns zero
  1409. * in case of success and a negative error code in case of failure.
  1410. */
  1411. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1412. const struct inode *host)
  1413. {
  1414. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1415. struct ubifs_inode *host_ui = ubifs_inode(host);
  1416. struct ubifs_ino_node *ino;
  1417. union ubifs_key key;
  1418. int sync = IS_DIRSYNC(host);
  1419. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1420. ubifs_assert(host->i_nlink > 0);
  1421. ubifs_assert(inode->i_nlink > 0);
  1422. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1423. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1424. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1425. aligned_len1 = ALIGN(len1, 8);
  1426. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1427. ino = kzalloc(aligned_len, GFP_NOFS);
  1428. if (!ino)
  1429. return -ENOMEM;
  1430. /* Make reservation before allocating sequence numbers */
  1431. err = make_reservation(c, BASEHD, aligned_len);
  1432. if (err)
  1433. goto out_free;
  1434. pack_inode(c, ino, host, 0);
  1435. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1436. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1437. if (!sync && !err) {
  1438. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1439. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1440. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1441. }
  1442. release_head(c, BASEHD);
  1443. if (err)
  1444. goto out_ro;
  1445. ino_key_init(c, &key, host->i_ino);
  1446. err = ubifs_tnc_add(c, &key, lnum, offs, len1);
  1447. if (err)
  1448. goto out_ro;
  1449. ino_key_init(c, &key, inode->i_ino);
  1450. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
  1451. if (err)
  1452. goto out_ro;
  1453. finish_reservation(c);
  1454. spin_lock(&host_ui->ui_lock);
  1455. host_ui->synced_i_size = host_ui->ui_size;
  1456. spin_unlock(&host_ui->ui_lock);
  1457. mark_inode_clean(c, host_ui);
  1458. kfree(ino);
  1459. return 0;
  1460. out_ro:
  1461. ubifs_ro_mode(c, err);
  1462. finish_reservation(c);
  1463. out_free:
  1464. kfree(ino);
  1465. return err;
  1466. }