journal.c 48 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. //dbg_jnl("ino %lu, dent '%.*s', data len %d in dir ino %lu",
  502. // inode->i_ino, nm->len, nm->name, ui->data_len, dir->i_ino);
  503. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  504. dlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  505. ilen = UBIFS_INO_NODE_SZ;
  506. /*
  507. * If the last reference to the inode is being deleted, then there is
  508. * no need to attach and write inode data, it is being deleted anyway.
  509. * And if the inode is being deleted, no need to synchronize
  510. * write-buffer even if the inode is synchronous.
  511. */
  512. if (!last_reference) {
  513. ilen += ui->data_len;
  514. sync |= IS_SYNC(inode);
  515. }
  516. aligned_dlen = ALIGN(dlen, 8);
  517. aligned_ilen = ALIGN(ilen, 8);
  518. len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
  519. /* Make sure to also account for extended attributes */
  520. len += host_ui->data_len;
  521. dent = kmalloc(len, GFP_NOFS);
  522. if (!dent)
  523. return -ENOMEM;
  524. /* Make reservation before allocating sequence numbers */
  525. err = make_reservation(c, BASEHD, len);
  526. if (err)
  527. goto out_free;
  528. if (!xent) {
  529. dent->ch.node_type = UBIFS_DENT_NODE;
  530. dent_key_init(c, &dent_key, dir->i_ino, nm);
  531. } else {
  532. dent->ch.node_type = UBIFS_XENT_NODE;
  533. xent_key_init(c, &dent_key, dir->i_ino, nm);
  534. }
  535. key_write(c, &dent_key, dent->key);
  536. dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
  537. dent->type = get_dent_type(inode->i_mode);
  538. dent->nlen = cpu_to_le16(fname_len(nm));
  539. memcpy(dent->name, fname_name(nm), fname_len(nm));
  540. dent->name[fname_len(nm)] = '\0';
  541. set_dent_cookie(c, dent);
  542. zero_dent_node_unused(dent);
  543. ubifs_prep_grp_node(c, dent, dlen, 0);
  544. ino = (void *)dent + aligned_dlen;
  545. pack_inode(c, ino, inode, 0);
  546. ino = (void *)ino + aligned_ilen;
  547. pack_inode(c, ino, dir, 1);
  548. if (last_reference) {
  549. err = ubifs_add_orphan(c, inode->i_ino);
  550. if (err) {
  551. release_head(c, BASEHD);
  552. goto out_finish;
  553. }
  554. ui->del_cmtno = c->cmt_no;
  555. }
  556. err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
  557. if (err)
  558. goto out_release;
  559. if (!sync) {
  560. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  561. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  562. ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
  563. }
  564. release_head(c, BASEHD);
  565. kfree(dent);
  566. if (deletion) {
  567. err = ubifs_tnc_remove_nm(c, &dent_key, nm);
  568. if (err)
  569. goto out_ro;
  570. err = ubifs_add_dirt(c, lnum, dlen);
  571. } else
  572. err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
  573. if (err)
  574. goto out_ro;
  575. /*
  576. * Note, we do not remove the inode from TNC even if the last reference
  577. * to it has just been deleted, because the inode may still be opened.
  578. * Instead, the inode has been added to orphan lists and the orphan
  579. * subsystem will take further care about it.
  580. */
  581. ino_key_init(c, &ino_key, inode->i_ino);
  582. ino_offs = dent_offs + aligned_dlen;
  583. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen);
  584. if (err)
  585. goto out_ro;
  586. ino_key_init(c, &ino_key, dir->i_ino);
  587. ino_offs += aligned_ilen;
  588. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs,
  589. UBIFS_INO_NODE_SZ + host_ui->data_len);
  590. if (err)
  591. goto out_ro;
  592. finish_reservation(c);
  593. spin_lock(&ui->ui_lock);
  594. ui->synced_i_size = ui->ui_size;
  595. spin_unlock(&ui->ui_lock);
  596. mark_inode_clean(c, ui);
  597. mark_inode_clean(c, host_ui);
  598. return 0;
  599. out_finish:
  600. finish_reservation(c);
  601. out_free:
  602. kfree(dent);
  603. return err;
  604. out_release:
  605. release_head(c, BASEHD);
  606. kfree(dent);
  607. out_ro:
  608. ubifs_ro_mode(c, err);
  609. if (last_reference)
  610. ubifs_delete_orphan(c, inode->i_ino);
  611. finish_reservation(c);
  612. return err;
  613. }
  614. /**
  615. * ubifs_jnl_write_data - write a data node to the journal.
  616. * @c: UBIFS file-system description object
  617. * @inode: inode the data node belongs to
  618. * @key: node key
  619. * @buf: buffer to write
  620. * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
  621. *
  622. * This function writes a data node to the journal. Returns %0 if the data node
  623. * was successfully written, and a negative error code in case of failure.
  624. */
  625. int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
  626. const union ubifs_key *key, const void *buf, int len)
  627. {
  628. struct ubifs_data_node *data;
  629. int err, lnum, offs, compr_type, out_len, compr_len;
  630. int dlen = COMPRESSED_DATA_NODE_BUF_SZ, allocated = 1;
  631. struct ubifs_inode *ui = ubifs_inode(inode);
  632. bool encrypted = ubifs_crypt_is_encrypted(inode);
  633. dbg_jnlk(key, "ino %lu, blk %u, len %d, key ",
  634. (unsigned long)key_inum(c, key), key_block(c, key), len);
  635. ubifs_assert(len <= UBIFS_BLOCK_SIZE);
  636. if (encrypted)
  637. dlen += UBIFS_CIPHER_BLOCK_SIZE;
  638. data = kmalloc(dlen, GFP_NOFS | __GFP_NOWARN);
  639. if (!data) {
  640. /*
  641. * Fall-back to the write reserve buffer. Note, we might be
  642. * currently on the memory reclaim path, when the kernel is
  643. * trying to free some memory by writing out dirty pages. The
  644. * write reserve buffer helps us to guarantee that we are
  645. * always able to write the data.
  646. */
  647. allocated = 0;
  648. mutex_lock(&c->write_reserve_mutex);
  649. data = c->write_reserve_buf;
  650. }
  651. data->ch.node_type = UBIFS_DATA_NODE;
  652. key_write(c, key, &data->key);
  653. data->size = cpu_to_le32(len);
  654. if (!(ui->flags & UBIFS_COMPR_FL))
  655. /* Compression is disabled for this inode */
  656. compr_type = UBIFS_COMPR_NONE;
  657. else
  658. compr_type = ui->compr_type;
  659. out_len = compr_len = dlen - UBIFS_DATA_NODE_SZ;
  660. ubifs_compress(c, buf, len, &data->data, &compr_len, &compr_type);
  661. ubifs_assert(compr_len <= UBIFS_BLOCK_SIZE);
  662. if (encrypted) {
  663. err = ubifs_encrypt(inode, data, compr_len, &out_len, key_block(c, key));
  664. if (err)
  665. goto out_free;
  666. } else {
  667. data->compr_size = 0;
  668. out_len = compr_len;
  669. }
  670. dlen = UBIFS_DATA_NODE_SZ + out_len;
  671. data->compr_type = cpu_to_le16(compr_type);
  672. /* Make reservation before allocating sequence numbers */
  673. err = make_reservation(c, DATAHD, dlen);
  674. if (err)
  675. goto out_free;
  676. err = write_node(c, DATAHD, data, dlen, &lnum, &offs);
  677. if (err)
  678. goto out_release;
  679. ubifs_wbuf_add_ino_nolock(&c->jheads[DATAHD].wbuf, key_inum(c, key));
  680. release_head(c, DATAHD);
  681. err = ubifs_tnc_add(c, key, lnum, offs, dlen);
  682. if (err)
  683. goto out_ro;
  684. finish_reservation(c);
  685. if (!allocated)
  686. mutex_unlock(&c->write_reserve_mutex);
  687. else
  688. kfree(data);
  689. return 0;
  690. out_release:
  691. release_head(c, DATAHD);
  692. out_ro:
  693. ubifs_ro_mode(c, err);
  694. finish_reservation(c);
  695. out_free:
  696. if (!allocated)
  697. mutex_unlock(&c->write_reserve_mutex);
  698. else
  699. kfree(data);
  700. return err;
  701. }
  702. /**
  703. * ubifs_jnl_write_inode - flush inode to the journal.
  704. * @c: UBIFS file-system description object
  705. * @inode: inode to flush
  706. *
  707. * This function writes inode @inode to the journal. If the inode is
  708. * synchronous, it also synchronizes the write-buffer. Returns zero in case of
  709. * success and a negative error code in case of failure.
  710. */
  711. int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode)
  712. {
  713. int err, lnum, offs;
  714. struct ubifs_ino_node *ino;
  715. struct ubifs_inode *ui = ubifs_inode(inode);
  716. int sync = 0, len = UBIFS_INO_NODE_SZ, last_reference = !inode->i_nlink;
  717. dbg_jnl("ino %lu, nlink %u", inode->i_ino, inode->i_nlink);
  718. /*
  719. * If the inode is being deleted, do not write the attached data. No
  720. * need to synchronize the write-buffer either.
  721. */
  722. if (!last_reference) {
  723. len += ui->data_len;
  724. sync = IS_SYNC(inode);
  725. }
  726. ino = kmalloc(len, GFP_NOFS);
  727. if (!ino)
  728. return -ENOMEM;
  729. /* Make reservation before allocating sequence numbers */
  730. err = make_reservation(c, BASEHD, len);
  731. if (err)
  732. goto out_free;
  733. pack_inode(c, ino, inode, 1);
  734. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  735. if (err)
  736. goto out_release;
  737. if (!sync)
  738. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  739. inode->i_ino);
  740. release_head(c, BASEHD);
  741. if (last_reference) {
  742. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  743. if (err)
  744. goto out_ro;
  745. ubifs_delete_orphan(c, inode->i_ino);
  746. err = ubifs_add_dirt(c, lnum, len);
  747. } else {
  748. union ubifs_key key;
  749. ino_key_init(c, &key, inode->i_ino);
  750. err = ubifs_tnc_add(c, &key, lnum, offs, len);
  751. }
  752. if (err)
  753. goto out_ro;
  754. finish_reservation(c);
  755. spin_lock(&ui->ui_lock);
  756. ui->synced_i_size = ui->ui_size;
  757. spin_unlock(&ui->ui_lock);
  758. kfree(ino);
  759. return 0;
  760. out_release:
  761. release_head(c, BASEHD);
  762. out_ro:
  763. ubifs_ro_mode(c, err);
  764. finish_reservation(c);
  765. out_free:
  766. kfree(ino);
  767. return err;
  768. }
  769. /**
  770. * ubifs_jnl_delete_inode - delete an inode.
  771. * @c: UBIFS file-system description object
  772. * @inode: inode to delete
  773. *
  774. * This function deletes inode @inode which includes removing it from orphans,
  775. * deleting it from TNC and, in some cases, writing a deletion inode to the
  776. * journal.
  777. *
  778. * When regular file inodes are unlinked or a directory inode is removed, the
  779. * 'ubifs_jnl_update()' function writes a corresponding deletion inode and
  780. * direntry to the media, and adds the inode to orphans. After this, when the
  781. * last reference to this inode has been dropped, this function is called. In
  782. * general, it has to write one more deletion inode to the media, because if
  783. * a commit happened between 'ubifs_jnl_update()' and
  784. * 'ubifs_jnl_delete_inode()', the deletion inode is not in the journal
  785. * anymore, and in fact it might not be on the flash anymore, because it might
  786. * have been garbage-collected already. And for optimization reasons UBIFS does
  787. * not read the orphan area if it has been unmounted cleanly, so it would have
  788. * no indication in the journal that there is a deleted inode which has to be
  789. * removed from TNC.
  790. *
  791. * However, if there was no commit between 'ubifs_jnl_update()' and
  792. * 'ubifs_jnl_delete_inode()', then there is no need to write the deletion
  793. * inode to the media for the second time. And this is quite a typical case.
  794. *
  795. * This function returns zero in case of success and a negative error code in
  796. * case of failure.
  797. */
  798. int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode)
  799. {
  800. int err;
  801. struct ubifs_inode *ui = ubifs_inode(inode);
  802. ubifs_assert(inode->i_nlink == 0);
  803. if (ui->del_cmtno != c->cmt_no)
  804. /* A commit happened for sure */
  805. return ubifs_jnl_write_inode(c, inode);
  806. down_read(&c->commit_sem);
  807. /*
  808. * Check commit number again, because the first test has been done
  809. * without @c->commit_sem, so a commit might have happened.
  810. */
  811. if (ui->del_cmtno != c->cmt_no) {
  812. up_read(&c->commit_sem);
  813. return ubifs_jnl_write_inode(c, inode);
  814. }
  815. err = ubifs_tnc_remove_ino(c, inode->i_ino);
  816. if (err)
  817. ubifs_ro_mode(c, err);
  818. else
  819. ubifs_delete_orphan(c, inode->i_ino);
  820. up_read(&c->commit_sem);
  821. return err;
  822. }
  823. /**
  824. * ubifs_jnl_xrename - cross rename two directory entries.
  825. * @c: UBIFS file-system description object
  826. * @fst_dir: parent inode of 1st directory entry to exchange
  827. * @fst_inode: 1st inode to exchange
  828. * @fst_nm: name of 1st inode to exchange
  829. * @snd_dir: parent inode of 2nd directory entry to exchange
  830. * @snd_inode: 2nd inode to exchange
  831. * @snd_nm: name of 2nd inode to exchange
  832. * @sync: non-zero if the write-buffer has to be synchronized
  833. *
  834. * This function implements the cross rename operation which may involve
  835. * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
  836. * and returns zero on success. In case of failure, a negative error code is
  837. * returned.
  838. */
  839. int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  840. const struct inode *fst_inode,
  841. const struct fscrypt_name *fst_nm,
  842. const struct inode *snd_dir,
  843. const struct inode *snd_inode,
  844. const struct fscrypt_name *snd_nm, int sync)
  845. {
  846. union ubifs_key key;
  847. struct ubifs_dent_node *dent1, *dent2;
  848. int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
  849. int aligned_dlen1, aligned_dlen2;
  850. int twoparents = (fst_dir != snd_dir);
  851. void *p;
  852. //dbg_jnl("dent '%pd' in dir ino %lu between dent '%pd' in dir ino %lu",
  853. // fst_dentry, fst_dir->i_ino, snd_dentry, snd_dir->i_ino);
  854. ubifs_assert(ubifs_inode(fst_dir)->data_len == 0);
  855. ubifs_assert(ubifs_inode(snd_dir)->data_len == 0);
  856. ubifs_assert(mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
  857. ubifs_assert(mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
  858. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(snd_nm) + 1;
  859. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(fst_nm) + 1;
  860. aligned_dlen1 = ALIGN(dlen1, 8);
  861. aligned_dlen2 = ALIGN(dlen2, 8);
  862. len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
  863. if (twoparents)
  864. len += plen;
  865. dent1 = kmalloc(len, GFP_NOFS);
  866. if (!dent1)
  867. return -ENOMEM;
  868. /* Make reservation before allocating sequence numbers */
  869. err = make_reservation(c, BASEHD, len);
  870. if (err)
  871. goto out_free;
  872. /* Make new dent for 1st entry */
  873. dent1->ch.node_type = UBIFS_DENT_NODE;
  874. dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, snd_nm);
  875. dent1->inum = cpu_to_le64(fst_inode->i_ino);
  876. dent1->type = get_dent_type(fst_inode->i_mode);
  877. dent1->nlen = cpu_to_le16(fname_len(snd_nm));
  878. memcpy(dent1->name, fname_name(snd_nm), fname_len(snd_nm));
  879. dent1->name[fname_len(snd_nm)] = '\0';
  880. zero_dent_node_unused(dent1);
  881. ubifs_prep_grp_node(c, dent1, dlen1, 0);
  882. /* Make new dent for 2nd entry */
  883. dent2 = (void *)dent1 + aligned_dlen1;
  884. dent2->ch.node_type = UBIFS_DENT_NODE;
  885. dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, fst_nm);
  886. dent2->inum = cpu_to_le64(snd_inode->i_ino);
  887. dent2->type = get_dent_type(snd_inode->i_mode);
  888. dent2->nlen = cpu_to_le16(fname_len(fst_nm));
  889. memcpy(dent2->name, fname_name(fst_nm), fname_len(fst_nm));
  890. dent2->name[fname_len(fst_nm)] = '\0';
  891. zero_dent_node_unused(dent2);
  892. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  893. p = (void *)dent2 + aligned_dlen2;
  894. if (!twoparents)
  895. pack_inode(c, p, fst_dir, 1);
  896. else {
  897. pack_inode(c, p, fst_dir, 0);
  898. p += ALIGN(plen, 8);
  899. pack_inode(c, p, snd_dir, 1);
  900. }
  901. err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
  902. if (err)
  903. goto out_release;
  904. if (!sync) {
  905. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  906. ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
  907. ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
  908. }
  909. release_head(c, BASEHD);
  910. dent_key_init(c, &key, snd_dir->i_ino, snd_nm);
  911. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, snd_nm);
  912. if (err)
  913. goto out_ro;
  914. offs += aligned_dlen1;
  915. dent_key_init(c, &key, fst_dir->i_ino, fst_nm);
  916. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, fst_nm);
  917. if (err)
  918. goto out_ro;
  919. offs += aligned_dlen2;
  920. ino_key_init(c, &key, fst_dir->i_ino);
  921. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  922. if (err)
  923. goto out_ro;
  924. if (twoparents) {
  925. offs += ALIGN(plen, 8);
  926. ino_key_init(c, &key, snd_dir->i_ino);
  927. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  928. if (err)
  929. goto out_ro;
  930. }
  931. finish_reservation(c);
  932. mark_inode_clean(c, ubifs_inode(fst_dir));
  933. if (twoparents)
  934. mark_inode_clean(c, ubifs_inode(snd_dir));
  935. kfree(dent1);
  936. return 0;
  937. out_release:
  938. release_head(c, BASEHD);
  939. out_ro:
  940. ubifs_ro_mode(c, err);
  941. finish_reservation(c);
  942. out_free:
  943. kfree(dent1);
  944. return err;
  945. }
  946. /**
  947. * ubifs_jnl_rename - rename a directory entry.
  948. * @c: UBIFS file-system description object
  949. * @old_dir: parent inode of directory entry to rename
  950. * @old_dentry: directory entry to rename
  951. * @new_dir: parent inode of directory entry to rename
  952. * @new_dentry: new directory entry (or directory entry to replace)
  953. * @sync: non-zero if the write-buffer has to be synchronized
  954. *
  955. * This function implements the re-name operation which may involve writing up
  956. * to 4 inodes and 2 directory entries. It marks the written inodes as clean
  957. * and returns zero on success. In case of failure, a negative error code is
  958. * returned.
  959. */
  960. int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
  961. const struct inode *old_inode,
  962. const struct fscrypt_name *old_nm,
  963. const struct inode *new_dir,
  964. const struct inode *new_inode,
  965. const struct fscrypt_name *new_nm,
  966. const struct inode *whiteout, int sync)
  967. {
  968. void *p;
  969. union ubifs_key key;
  970. struct ubifs_dent_node *dent, *dent2;
  971. int err, dlen1, dlen2, ilen, lnum, offs, len;
  972. int aligned_dlen1, aligned_dlen2, plen = UBIFS_INO_NODE_SZ;
  973. int last_reference = !!(new_inode && new_inode->i_nlink == 0);
  974. int move = (old_dir != new_dir);
  975. struct ubifs_inode *uninitialized_var(new_ui);
  976. //dbg_jnl("dent '%pd' in dir ino %lu to dent '%pd' in dir ino %lu",
  977. // old_dentry, old_dir->i_ino, new_dentry, new_dir->i_ino);
  978. ubifs_assert(ubifs_inode(old_dir)->data_len == 0);
  979. ubifs_assert(ubifs_inode(new_dir)->data_len == 0);
  980. ubifs_assert(mutex_is_locked(&ubifs_inode(old_dir)->ui_mutex));
  981. ubifs_assert(mutex_is_locked(&ubifs_inode(new_dir)->ui_mutex));
  982. dlen1 = UBIFS_DENT_NODE_SZ + fname_len(new_nm) + 1;
  983. dlen2 = UBIFS_DENT_NODE_SZ + fname_len(old_nm) + 1;
  984. if (new_inode) {
  985. new_ui = ubifs_inode(new_inode);
  986. ubifs_assert(mutex_is_locked(&new_ui->ui_mutex));
  987. ilen = UBIFS_INO_NODE_SZ;
  988. if (!last_reference)
  989. ilen += new_ui->data_len;
  990. } else
  991. ilen = 0;
  992. aligned_dlen1 = ALIGN(dlen1, 8);
  993. aligned_dlen2 = ALIGN(dlen2, 8);
  994. len = aligned_dlen1 + aligned_dlen2 + ALIGN(ilen, 8) + ALIGN(plen, 8);
  995. if (move)
  996. len += plen;
  997. dent = kmalloc(len, GFP_NOFS);
  998. if (!dent)
  999. return -ENOMEM;
  1000. /* Make reservation before allocating sequence numbers */
  1001. err = make_reservation(c, BASEHD, len);
  1002. if (err)
  1003. goto out_free;
  1004. /* Make new dent */
  1005. dent->ch.node_type = UBIFS_DENT_NODE;
  1006. dent_key_init_flash(c, &dent->key, new_dir->i_ino, new_nm);
  1007. dent->inum = cpu_to_le64(old_inode->i_ino);
  1008. dent->type = get_dent_type(old_inode->i_mode);
  1009. dent->nlen = cpu_to_le16(fname_len(new_nm));
  1010. memcpy(dent->name, fname_name(new_nm), fname_len(new_nm));
  1011. dent->name[fname_len(new_nm)] = '\0';
  1012. set_dent_cookie(c, dent);
  1013. zero_dent_node_unused(dent);
  1014. ubifs_prep_grp_node(c, dent, dlen1, 0);
  1015. dent2 = (void *)dent + aligned_dlen1;
  1016. dent2->ch.node_type = UBIFS_DENT_NODE;
  1017. dent_key_init_flash(c, &dent2->key, old_dir->i_ino, old_nm);
  1018. if (whiteout) {
  1019. dent2->inum = cpu_to_le64(whiteout->i_ino);
  1020. dent2->type = get_dent_type(whiteout->i_mode);
  1021. } else {
  1022. /* Make deletion dent */
  1023. dent2->inum = 0;
  1024. dent2->type = DT_UNKNOWN;
  1025. }
  1026. dent2->nlen = cpu_to_le16(fname_len(old_nm));
  1027. memcpy(dent2->name, fname_name(old_nm), fname_len(old_nm));
  1028. dent2->name[fname_len(old_nm)] = '\0';
  1029. set_dent_cookie(c, dent2);
  1030. zero_dent_node_unused(dent2);
  1031. ubifs_prep_grp_node(c, dent2, dlen2, 0);
  1032. p = (void *)dent2 + aligned_dlen2;
  1033. if (new_inode) {
  1034. pack_inode(c, p, new_inode, 0);
  1035. p += ALIGN(ilen, 8);
  1036. }
  1037. if (!move)
  1038. pack_inode(c, p, old_dir, 1);
  1039. else {
  1040. pack_inode(c, p, old_dir, 0);
  1041. p += ALIGN(plen, 8);
  1042. pack_inode(c, p, new_dir, 1);
  1043. }
  1044. if (last_reference) {
  1045. err = ubifs_add_orphan(c, new_inode->i_ino);
  1046. if (err) {
  1047. release_head(c, BASEHD);
  1048. goto out_finish;
  1049. }
  1050. new_ui->del_cmtno = c->cmt_no;
  1051. }
  1052. err = write_head(c, BASEHD, dent, len, &lnum, &offs, sync);
  1053. if (err)
  1054. goto out_release;
  1055. if (!sync) {
  1056. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1057. ubifs_wbuf_add_ino_nolock(wbuf, new_dir->i_ino);
  1058. ubifs_wbuf_add_ino_nolock(wbuf, old_dir->i_ino);
  1059. if (new_inode)
  1060. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf,
  1061. new_inode->i_ino);
  1062. }
  1063. release_head(c, BASEHD);
  1064. dent_key_init(c, &key, new_dir->i_ino, new_nm);
  1065. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, new_nm);
  1066. if (err)
  1067. goto out_ro;
  1068. offs += aligned_dlen1;
  1069. if (whiteout) {
  1070. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1071. err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, old_nm);
  1072. if (err)
  1073. goto out_ro;
  1074. ubifs_delete_orphan(c, whiteout->i_ino);
  1075. } else {
  1076. err = ubifs_add_dirt(c, lnum, dlen2);
  1077. if (err)
  1078. goto out_ro;
  1079. dent_key_init(c, &key, old_dir->i_ino, old_nm);
  1080. err = ubifs_tnc_remove_nm(c, &key, old_nm);
  1081. if (err)
  1082. goto out_ro;
  1083. }
  1084. offs += aligned_dlen2;
  1085. if (new_inode) {
  1086. ino_key_init(c, &key, new_inode->i_ino);
  1087. err = ubifs_tnc_add(c, &key, lnum, offs, ilen);
  1088. if (err)
  1089. goto out_ro;
  1090. offs += ALIGN(ilen, 8);
  1091. }
  1092. ino_key_init(c, &key, old_dir->i_ino);
  1093. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  1094. if (err)
  1095. goto out_ro;
  1096. if (move) {
  1097. offs += ALIGN(plen, 8);
  1098. ino_key_init(c, &key, new_dir->i_ino);
  1099. err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  1100. if (err)
  1101. goto out_ro;
  1102. }
  1103. finish_reservation(c);
  1104. if (new_inode) {
  1105. mark_inode_clean(c, new_ui);
  1106. spin_lock(&new_ui->ui_lock);
  1107. new_ui->synced_i_size = new_ui->ui_size;
  1108. spin_unlock(&new_ui->ui_lock);
  1109. }
  1110. mark_inode_clean(c, ubifs_inode(old_dir));
  1111. if (move)
  1112. mark_inode_clean(c, ubifs_inode(new_dir));
  1113. kfree(dent);
  1114. return 0;
  1115. out_release:
  1116. release_head(c, BASEHD);
  1117. out_ro:
  1118. ubifs_ro_mode(c, err);
  1119. if (last_reference)
  1120. ubifs_delete_orphan(c, new_inode->i_ino);
  1121. out_finish:
  1122. finish_reservation(c);
  1123. out_free:
  1124. kfree(dent);
  1125. return err;
  1126. }
  1127. /**
  1128. * truncate_data_node - re-compress/encrypt a truncated data node.
  1129. * @c: UBIFS file-system description object
  1130. * @inode: inode which referes to the data node
  1131. * @block: data block number
  1132. * @dn: data node to re-compress
  1133. * @new_len: new length
  1134. *
  1135. * This function is used when an inode is truncated and the last data node of
  1136. * the inode has to be re-compressed/encrypted and re-written.
  1137. */
  1138. static int truncate_data_node(const struct ubifs_info *c, const struct inode *inode,
  1139. unsigned int block, struct ubifs_data_node *dn,
  1140. int *new_len)
  1141. {
  1142. void *buf;
  1143. int err, dlen, compr_type, out_len, old_dlen;
  1144. out_len = le32_to_cpu(dn->size);
  1145. buf = kmalloc(out_len * WORST_COMPR_FACTOR, GFP_NOFS);
  1146. if (!buf)
  1147. return -ENOMEM;
  1148. dlen = old_dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  1149. compr_type = le16_to_cpu(dn->compr_type);
  1150. if (ubifs_crypt_is_encrypted(inode)) {
  1151. err = ubifs_decrypt(inode, dn, &dlen, block);
  1152. if (err)
  1153. goto out;
  1154. }
  1155. if (compr_type != UBIFS_COMPR_NONE) {
  1156. err = ubifs_decompress(c, &dn->data, dlen, buf, &out_len, compr_type);
  1157. if (err)
  1158. goto out;
  1159. ubifs_compress(c, buf, *new_len, &dn->data, &out_len, &compr_type);
  1160. }
  1161. if (ubifs_crypt_is_encrypted(inode)) {
  1162. err = ubifs_encrypt(inode, dn, out_len, &old_dlen, block);
  1163. if (err)
  1164. goto out;
  1165. out_len = old_dlen;
  1166. } else {
  1167. dn->compr_size = 0;
  1168. }
  1169. ubifs_assert(out_len <= UBIFS_BLOCK_SIZE);
  1170. dn->compr_type = cpu_to_le16(compr_type);
  1171. dn->size = cpu_to_le32(*new_len);
  1172. *new_len = UBIFS_DATA_NODE_SZ + out_len;
  1173. err = 0;
  1174. out:
  1175. kfree(buf);
  1176. return err;
  1177. }
  1178. /**
  1179. * ubifs_jnl_truncate - update the journal for a truncation.
  1180. * @c: UBIFS file-system description object
  1181. * @inode: inode to truncate
  1182. * @old_size: old size
  1183. * @new_size: new size
  1184. *
  1185. * When the size of a file decreases due to truncation, a truncation node is
  1186. * written, the journal tree is updated, and the last data block is re-written
  1187. * if it has been affected. The inode is also updated in order to synchronize
  1188. * the new inode size.
  1189. *
  1190. * This function marks the inode as clean and returns zero on success. In case
  1191. * of failure, a negative error code is returned.
  1192. */
  1193. int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
  1194. loff_t old_size, loff_t new_size)
  1195. {
  1196. union ubifs_key key, to_key;
  1197. struct ubifs_ino_node *ino;
  1198. struct ubifs_trun_node *trun;
  1199. struct ubifs_data_node *uninitialized_var(dn);
  1200. int err, dlen, len, lnum, offs, bit, sz, sync = IS_SYNC(inode);
  1201. struct ubifs_inode *ui = ubifs_inode(inode);
  1202. ino_t inum = inode->i_ino;
  1203. unsigned int blk;
  1204. dbg_jnl("ino %lu, size %lld -> %lld",
  1205. (unsigned long)inum, old_size, new_size);
  1206. ubifs_assert(!ui->data_len);
  1207. ubifs_assert(S_ISREG(inode->i_mode));
  1208. ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  1209. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1210. UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
  1211. ino = kmalloc(sz, GFP_NOFS);
  1212. if (!ino)
  1213. return -ENOMEM;
  1214. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1215. trun->ch.node_type = UBIFS_TRUN_NODE;
  1216. trun->inum = cpu_to_le32(inum);
  1217. trun->old_size = cpu_to_le64(old_size);
  1218. trun->new_size = cpu_to_le64(new_size);
  1219. zero_trun_node_unused(trun);
  1220. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1221. if (dlen) {
  1222. /* Get last data block so it can be truncated */
  1223. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1224. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1225. data_key_init(c, &key, inum, blk);
  1226. dbg_jnlk(&key, "last block key ");
  1227. err = ubifs_tnc_lookup(c, &key, dn);
  1228. if (err == -ENOENT)
  1229. dlen = 0; /* Not found (so it is a hole) */
  1230. else if (err)
  1231. goto out_free;
  1232. else {
  1233. if (le32_to_cpu(dn->size) <= dlen)
  1234. dlen = 0; /* Nothing to do */
  1235. else {
  1236. err = truncate_data_node(c, inode, blk, dn, &dlen);
  1237. if (err)
  1238. goto out_free;
  1239. }
  1240. }
  1241. }
  1242. /* Must make reservation before allocating sequence numbers */
  1243. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1244. if (dlen)
  1245. len += dlen;
  1246. err = make_reservation(c, BASEHD, len);
  1247. if (err)
  1248. goto out_free;
  1249. pack_inode(c, ino, inode, 0);
  1250. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1251. if (dlen)
  1252. ubifs_prep_grp_node(c, dn, dlen, 1);
  1253. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1254. if (err)
  1255. goto out_release;
  1256. if (!sync)
  1257. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1258. release_head(c, BASEHD);
  1259. if (dlen) {
  1260. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1261. err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
  1262. if (err)
  1263. goto out_ro;
  1264. }
  1265. ino_key_init(c, &key, inum);
  1266. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
  1267. if (err)
  1268. goto out_ro;
  1269. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1270. if (err)
  1271. goto out_ro;
  1272. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1273. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1274. data_key_init(c, &key, inum, blk);
  1275. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1276. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0 : 1);
  1277. data_key_init(c, &to_key, inum, blk);
  1278. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1279. if (err)
  1280. goto out_ro;
  1281. finish_reservation(c);
  1282. spin_lock(&ui->ui_lock);
  1283. ui->synced_i_size = ui->ui_size;
  1284. spin_unlock(&ui->ui_lock);
  1285. mark_inode_clean(c, ui);
  1286. kfree(ino);
  1287. return 0;
  1288. out_release:
  1289. release_head(c, BASEHD);
  1290. out_ro:
  1291. ubifs_ro_mode(c, err);
  1292. finish_reservation(c);
  1293. out_free:
  1294. kfree(ino);
  1295. return err;
  1296. }
  1297. /**
  1298. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1299. * @c: UBIFS file-system description object
  1300. * @host: host inode
  1301. * @inode: extended attribute inode
  1302. * @nm: extended attribute entry name
  1303. *
  1304. * This function delete an extended attribute which is very similar to
  1305. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1306. * updates the target inode. Returns zero in case of success and a negative
  1307. * error code in case of failure.
  1308. */
  1309. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1310. const struct inode *inode,
  1311. const struct fscrypt_name *nm)
  1312. {
  1313. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
  1314. struct ubifs_dent_node *xent;
  1315. struct ubifs_ino_node *ino;
  1316. union ubifs_key xent_key, key1, key2;
  1317. int sync = IS_DIRSYNC(host);
  1318. struct ubifs_inode *host_ui = ubifs_inode(host);
  1319. //dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
  1320. // host->i_ino, inode->i_ino, nm->name,
  1321. // ubifs_inode(inode)->data_len);
  1322. ubifs_assert(inode->i_nlink == 0);
  1323. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1324. /*
  1325. * Since we are deleting the inode, we do not bother to attach any data
  1326. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1327. */
  1328. xlen = UBIFS_DENT_NODE_SZ + fname_len(nm) + 1;
  1329. aligned_xlen = ALIGN(xlen, 8);
  1330. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1331. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1332. xent = kmalloc(len, GFP_NOFS);
  1333. if (!xent)
  1334. return -ENOMEM;
  1335. /* Make reservation before allocating sequence numbers */
  1336. err = make_reservation(c, BASEHD, len);
  1337. if (err) {
  1338. kfree(xent);
  1339. return err;
  1340. }
  1341. xent->ch.node_type = UBIFS_XENT_NODE;
  1342. xent_key_init(c, &xent_key, host->i_ino, nm);
  1343. key_write(c, &xent_key, xent->key);
  1344. xent->inum = 0;
  1345. xent->type = get_dent_type(inode->i_mode);
  1346. xent->nlen = cpu_to_le16(fname_len(nm));
  1347. memcpy(xent->name, fname_name(nm), fname_len(nm));
  1348. xent->name[fname_len(nm)] = '\0';
  1349. zero_dent_node_unused(xent);
  1350. ubifs_prep_grp_node(c, xent, xlen, 0);
  1351. ino = (void *)xent + aligned_xlen;
  1352. pack_inode(c, ino, inode, 0);
  1353. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1354. pack_inode(c, ino, host, 1);
  1355. err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
  1356. if (!sync && !err)
  1357. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1358. release_head(c, BASEHD);
  1359. kfree(xent);
  1360. if (err)
  1361. goto out_ro;
  1362. /* Remove the extended attribute entry from TNC */
  1363. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1364. if (err)
  1365. goto out_ro;
  1366. err = ubifs_add_dirt(c, lnum, xlen);
  1367. if (err)
  1368. goto out_ro;
  1369. /*
  1370. * Remove all nodes belonging to the extended attribute inode from TNC.
  1371. * Well, there actually must be only one node - the inode itself.
  1372. */
  1373. lowest_ino_key(c, &key1, inode->i_ino);
  1374. highest_ino_key(c, &key2, inode->i_ino);
  1375. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1376. if (err)
  1377. goto out_ro;
  1378. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1379. if (err)
  1380. goto out_ro;
  1381. /* And update TNC with the new host inode position */
  1382. ino_key_init(c, &key1, host->i_ino);
  1383. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
  1384. if (err)
  1385. goto out_ro;
  1386. finish_reservation(c);
  1387. spin_lock(&host_ui->ui_lock);
  1388. host_ui->synced_i_size = host_ui->ui_size;
  1389. spin_unlock(&host_ui->ui_lock);
  1390. mark_inode_clean(c, host_ui);
  1391. return 0;
  1392. out_ro:
  1393. ubifs_ro_mode(c, err);
  1394. finish_reservation(c);
  1395. return err;
  1396. }
  1397. /**
  1398. * ubifs_jnl_change_xattr - change an extended attribute.
  1399. * @c: UBIFS file-system description object
  1400. * @inode: extended attribute inode
  1401. * @host: host inode
  1402. *
  1403. * This function writes the updated version of an extended attribute inode and
  1404. * the host inode to the journal (to the base head). The host inode is written
  1405. * after the extended attribute inode in order to guarantee that the extended
  1406. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1407. * consequently, the write-buffer is synchronized. This function returns zero
  1408. * in case of success and a negative error code in case of failure.
  1409. */
  1410. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1411. const struct inode *host)
  1412. {
  1413. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1414. struct ubifs_inode *host_ui = ubifs_inode(host);
  1415. struct ubifs_ino_node *ino;
  1416. union ubifs_key key;
  1417. int sync = IS_DIRSYNC(host);
  1418. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1419. ubifs_assert(host->i_nlink > 0);
  1420. ubifs_assert(inode->i_nlink > 0);
  1421. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1422. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1423. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1424. aligned_len1 = ALIGN(len1, 8);
  1425. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1426. ino = kmalloc(aligned_len, GFP_NOFS);
  1427. if (!ino)
  1428. return -ENOMEM;
  1429. /* Make reservation before allocating sequence numbers */
  1430. err = make_reservation(c, BASEHD, aligned_len);
  1431. if (err)
  1432. goto out_free;
  1433. pack_inode(c, ino, host, 0);
  1434. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1435. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1436. if (!sync && !err) {
  1437. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1438. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1439. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1440. }
  1441. release_head(c, BASEHD);
  1442. if (err)
  1443. goto out_ro;
  1444. ino_key_init(c, &key, host->i_ino);
  1445. err = ubifs_tnc_add(c, &key, lnum, offs, len1);
  1446. if (err)
  1447. goto out_ro;
  1448. ino_key_init(c, &key, inode->i_ino);
  1449. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
  1450. if (err)
  1451. goto out_ro;
  1452. finish_reservation(c);
  1453. spin_lock(&host_ui->ui_lock);
  1454. host_ui->synced_i_size = host_ui->ui_size;
  1455. spin_unlock(&host_ui->ui_lock);
  1456. mark_inode_clean(c, host_ui);
  1457. kfree(ino);
  1458. return 0;
  1459. out_ro:
  1460. ubifs_ro_mode(c, err);
  1461. finish_reservation(c);
  1462. out_free:
  1463. kfree(ino);
  1464. return err;
  1465. }