journal.c 47 KB

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