journal.c 42 KB

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