journal.c 42 KB

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