journal.c 42 KB

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