journal.c 40 KB

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