journal.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. */
  406. static void pack_inode(struct ubifs_info *c, struct ubifs_ino_node *ino,
  407. const struct inode *inode, int last)
  408. {
  409. int data_len = 0, last_reference = !inode->i_nlink;
  410. struct ubifs_inode *ui = ubifs_inode(inode);
  411. ino->ch.node_type = UBIFS_INO_NODE;
  412. ino_key_init_flash(c, &ino->key, inode->i_ino);
  413. ino->creat_sqnum = cpu_to_le64(ui->creat_sqnum);
  414. ino->atime_sec = cpu_to_le64(inode->i_atime.tv_sec);
  415. ino->atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  416. ino->ctime_sec = cpu_to_le64(inode->i_ctime.tv_sec);
  417. ino->ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  418. ino->mtime_sec = cpu_to_le64(inode->i_mtime.tv_sec);
  419. ino->mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  420. ino->uid = cpu_to_le32(inode->i_uid);
  421. ino->gid = cpu_to_le32(inode->i_gid);
  422. ino->mode = cpu_to_le32(inode->i_mode);
  423. ino->flags = cpu_to_le32(ui->flags);
  424. ino->size = cpu_to_le64(ui->ui_size);
  425. ino->nlink = cpu_to_le32(inode->i_nlink);
  426. ino->compr_type = cpu_to_le16(ui->compr_type);
  427. ino->data_len = cpu_to_le32(ui->data_len);
  428. ino->xattr_cnt = cpu_to_le32(ui->xattr_cnt);
  429. ino->xattr_size = cpu_to_le32(ui->xattr_size);
  430. ino->xattr_names = cpu_to_le32(ui->xattr_names);
  431. zero_ino_node_unused(ino);
  432. /*
  433. * Drop the attached data if this is a deletion inode, the data is not
  434. * needed anymore.
  435. */
  436. if (!last_reference) {
  437. memcpy(ino->data, ui->data, ui->data_len);
  438. data_len = ui->data_len;
  439. }
  440. ubifs_prep_grp_node(c, ino, UBIFS_INO_NODE_SZ + data_len, last);
  441. }
  442. /**
  443. * mark_inode_clean - mark UBIFS inode as clean.
  444. * @c: UBIFS file-system description object
  445. * @ui: UBIFS inode to mark as clean
  446. *
  447. * This helper function marks UBIFS inode @ui as clean by cleaning the
  448. * @ui->dirty flag and releasing its budget. Note, VFS may still treat the
  449. * inode as dirty and try to write it back, but 'ubifs_write_inode()' would
  450. * just do nothing.
  451. */
  452. static void mark_inode_clean(struct ubifs_info *c, struct ubifs_inode *ui)
  453. {
  454. if (ui->dirty)
  455. ubifs_release_dirty_inode_budget(c, ui);
  456. ui->dirty = 0;
  457. }
  458. /**
  459. * ubifs_jnl_update - update inode.
  460. * @c: UBIFS file-system description object
  461. * @dir: parent inode or host inode in case of extended attributes
  462. * @nm: directory entry name
  463. * @inode: inode to update
  464. * @deletion: indicates a directory entry deletion i.e unlink or rmdir
  465. * @xent: non-zero if the directory entry is an extended attribute entry
  466. *
  467. * This function updates an inode by writing a directory entry (or extended
  468. * attribute entry), the inode itself, and the parent directory inode (or the
  469. * host inode) to the journal.
  470. *
  471. * The function writes the host inode @dir last, which is important in case of
  472. * extended attributes. Indeed, then we guarantee that if the host inode gets
  473. * synchronized (with 'fsync()'), and the write-buffer it sits in gets flushed,
  474. * the extended attribute inode gets flushed too. And this is exactly what the
  475. * user expects - synchronizing the host inode synchronizes its extended
  476. * attributes. Similarly, this guarantees that if @dir is synchronized, its
  477. * directory entry corresponding to @nm gets synchronized too.
  478. *
  479. * If the inode (@inode) or the parent directory (@dir) are synchronous, this
  480. * function synchronizes the write-buffer.
  481. *
  482. * This function marks the @dir and @inode inodes as clean and returns zero on
  483. * success. In case of failure, a negative error code is returned.
  484. */
  485. int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
  486. const struct qstr *nm, const struct inode *inode,
  487. int deletion, int xent)
  488. {
  489. int err, dlen, ilen, len, lnum, ino_offs, dent_offs;
  490. int aligned_dlen, aligned_ilen, sync = IS_DIRSYNC(dir);
  491. int last_reference = !!(deletion && inode->i_nlink == 0);
  492. struct ubifs_inode *ui = ubifs_inode(inode);
  493. struct ubifs_inode *dir_ui = ubifs_inode(dir);
  494. struct ubifs_dent_node *dent;
  495. struct ubifs_ino_node *ino;
  496. union ubifs_key dent_key, ino_key;
  497. dbg_jnl("ino %lu, dent '%.*s', data len %d in dir ino %lu",
  498. inode->i_ino, nm->len, nm->name, ui->data_len, dir->i_ino);
  499. ubifs_assert(dir_ui->data_len == 0);
  500. ubifs_assert(mutex_is_locked(&dir_ui->ui_mutex));
  501. dlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
  502. ilen = UBIFS_INO_NODE_SZ;
  503. /*
  504. * If the last reference to the inode is being deleted, then there is
  505. * no need to attach and write inode data, it is being deleted anyway.
  506. * And if the inode is being deleted, no need to synchronize
  507. * write-buffer even if the inode is synchronous.
  508. */
  509. if (!last_reference) {
  510. ilen += ui->data_len;
  511. sync |= IS_SYNC(inode);
  512. }
  513. aligned_dlen = ALIGN(dlen, 8);
  514. aligned_ilen = ALIGN(ilen, 8);
  515. len = aligned_dlen + aligned_ilen + UBIFS_INO_NODE_SZ;
  516. dent = kmalloc(len, GFP_NOFS);
  517. if (!dent)
  518. return -ENOMEM;
  519. /* Make reservation before allocating sequence numbers */
  520. err = make_reservation(c, BASEHD, len);
  521. if (err)
  522. goto out_free;
  523. if (!xent) {
  524. dent->ch.node_type = UBIFS_DENT_NODE;
  525. dent_key_init(c, &dent_key, dir->i_ino, nm);
  526. } else {
  527. dent->ch.node_type = UBIFS_XENT_NODE;
  528. xent_key_init(c, &dent_key, dir->i_ino, nm);
  529. }
  530. key_write(c, &dent_key, dent->key);
  531. dent->inum = deletion ? 0 : cpu_to_le64(inode->i_ino);
  532. dent->type = get_dent_type(inode->i_mode);
  533. dent->nlen = cpu_to_le16(nm->len);
  534. memcpy(dent->name, nm->name, nm->len);
  535. dent->name[nm->len] = '\0';
  536. zero_dent_node_unused(dent);
  537. ubifs_prep_grp_node(c, dent, dlen, 0);
  538. ino = (void *)dent + aligned_dlen;
  539. pack_inode(c, ino, inode, 0);
  540. ino = (void *)ino + aligned_ilen;
  541. pack_inode(c, ino, dir, 1);
  542. if (last_reference) {
  543. err = ubifs_add_orphan(c, inode->i_ino);
  544. if (err) {
  545. release_head(c, BASEHD);
  546. goto out_finish;
  547. }
  548. ui->del_cmtno = c->cmt_no;
  549. }
  550. err = write_head(c, BASEHD, dent, len, &lnum, &dent_offs, sync);
  551. if (err)
  552. goto out_release;
  553. if (!sync) {
  554. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  555. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  556. ubifs_wbuf_add_ino_nolock(wbuf, dir->i_ino);
  557. }
  558. release_head(c, BASEHD);
  559. kfree(dent);
  560. if (deletion) {
  561. err = ubifs_tnc_remove_nm(c, &dent_key, nm);
  562. if (err)
  563. goto out_ro;
  564. err = ubifs_add_dirt(c, lnum, dlen);
  565. } else
  566. err = ubifs_tnc_add_nm(c, &dent_key, lnum, dent_offs, dlen, nm);
  567. if (err)
  568. goto out_ro;
  569. /*
  570. * Note, we do not remove the inode from TNC even if the last reference
  571. * to it has just been deleted, because the inode may still be opened.
  572. * Instead, the inode has been added to orphan lists and the orphan
  573. * subsystem will take further care about it.
  574. */
  575. ino_key_init(c, &ino_key, inode->i_ino);
  576. ino_offs = dent_offs + aligned_dlen;
  577. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, ilen);
  578. if (err)
  579. goto out_ro;
  580. ino_key_init(c, &ino_key, dir->i_ino);
  581. ino_offs += aligned_ilen;
  582. err = ubifs_tnc_add(c, &ino_key, lnum, ino_offs, UBIFS_INO_NODE_SZ);
  583. if (err)
  584. goto out_ro;
  585. finish_reservation(c);
  586. spin_lock(&ui->ui_lock);
  587. ui->synced_i_size = ui->ui_size;
  588. spin_unlock(&ui->ui_lock);
  589. mark_inode_clean(c, ui);
  590. mark_inode_clean(c, dir_ui);
  591. return 0;
  592. out_finish:
  593. finish_reservation(c);
  594. out_free:
  595. kfree(dent);
  596. return err;
  597. out_release:
  598. release_head(c, BASEHD);
  599. out_ro:
  600. ubifs_ro_mode(c, err);
  601. if (last_reference)
  602. ubifs_delete_orphan(c, inode->i_ino);
  603. finish_reservation(c);
  604. return err;
  605. }
  606. /**
  607. * ubifs_jnl_write_data - write a data node to the journal.
  608. * @c: UBIFS file-system description object
  609. * @inode: inode the data node belongs to
  610. * @key: node key
  611. * @buf: buffer to write
  612. * @len: data length (must not exceed %UBIFS_BLOCK_SIZE)
  613. *
  614. * This function writes a data node to the journal. Returns %0 if the data node
  615. * was successfully written, and a negative error code in case of failure.
  616. */
  617. int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
  618. const union ubifs_key *key, const void *buf, int len)
  619. {
  620. struct ubifs_data_node *data;
  621. int err, lnum, offs, compr_type, out_len;
  622. int dlen = UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR;
  623. struct ubifs_inode *ui = ubifs_inode(inode);
  624. dbg_jnl("ino %lu, blk %u, len %d, key %s", key_inum(c, key),
  625. key_block(c, key), len, 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", inum, old_size, new_size);
  1013. ubifs_assert(!ui->data_len);
  1014. ubifs_assert(S_ISREG(inode->i_mode));
  1015. ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  1016. sz = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ +
  1017. UBIFS_MAX_DATA_NODE_SZ * WORST_COMPR_FACTOR;
  1018. ino = kmalloc(sz, GFP_NOFS);
  1019. if (!ino)
  1020. return -ENOMEM;
  1021. trun = (void *)ino + UBIFS_INO_NODE_SZ;
  1022. trun->ch.node_type = UBIFS_TRUN_NODE;
  1023. trun->inum = cpu_to_le32(inum);
  1024. trun->old_size = cpu_to_le64(old_size);
  1025. trun->new_size = cpu_to_le64(new_size);
  1026. zero_trun_node_unused(trun);
  1027. dlen = new_size & (UBIFS_BLOCK_SIZE - 1);
  1028. if (dlen) {
  1029. /* Get last data block so it can be truncated */
  1030. dn = (void *)trun + UBIFS_TRUN_NODE_SZ;
  1031. blk = new_size >> UBIFS_BLOCK_SHIFT;
  1032. data_key_init(c, &key, inum, blk);
  1033. dbg_jnl("last block key %s", DBGKEY(&key));
  1034. err = ubifs_tnc_lookup(c, &key, dn);
  1035. if (err == -ENOENT)
  1036. dlen = 0; /* Not found (so it is a hole) */
  1037. else if (err)
  1038. goto out_free;
  1039. else {
  1040. if (le32_to_cpu(dn->size) <= dlen)
  1041. dlen = 0; /* Nothing to do */
  1042. else {
  1043. int compr_type = le16_to_cpu(dn->compr_type);
  1044. if (compr_type != UBIFS_COMPR_NONE) {
  1045. err = recomp_data_node(dn, &dlen);
  1046. if (err)
  1047. goto out_free;
  1048. } else {
  1049. dn->size = cpu_to_le32(dlen);
  1050. dlen += UBIFS_DATA_NODE_SZ;
  1051. }
  1052. zero_data_node_unused(dn);
  1053. }
  1054. }
  1055. }
  1056. /* Must make reservation before allocating sequence numbers */
  1057. len = UBIFS_TRUN_NODE_SZ + UBIFS_INO_NODE_SZ;
  1058. if (dlen)
  1059. len += dlen;
  1060. err = make_reservation(c, BASEHD, len);
  1061. if (err)
  1062. goto out_free;
  1063. pack_inode(c, ino, inode, 0);
  1064. ubifs_prep_grp_node(c, trun, UBIFS_TRUN_NODE_SZ, dlen ? 0 : 1);
  1065. if (dlen)
  1066. ubifs_prep_grp_node(c, dn, dlen, 1);
  1067. err = write_head(c, BASEHD, ino, len, &lnum, &offs, sync);
  1068. if (err)
  1069. goto out_release;
  1070. if (!sync)
  1071. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, inum);
  1072. release_head(c, BASEHD);
  1073. if (dlen) {
  1074. sz = offs + UBIFS_INO_NODE_SZ + UBIFS_TRUN_NODE_SZ;
  1075. err = ubifs_tnc_add(c, &key, lnum, sz, dlen);
  1076. if (err)
  1077. goto out_ro;
  1078. }
  1079. ino_key_init(c, &key, inum);
  1080. err = ubifs_tnc_add(c, &key, lnum, offs, UBIFS_INO_NODE_SZ);
  1081. if (err)
  1082. goto out_ro;
  1083. err = ubifs_add_dirt(c, lnum, UBIFS_TRUN_NODE_SZ);
  1084. if (err)
  1085. goto out_ro;
  1086. bit = new_size & (UBIFS_BLOCK_SIZE - 1);
  1087. blk = (new_size >> UBIFS_BLOCK_SHIFT) + (bit ? 1 : 0);
  1088. data_key_init(c, &key, inum, blk);
  1089. bit = old_size & (UBIFS_BLOCK_SIZE - 1);
  1090. blk = (old_size >> UBIFS_BLOCK_SHIFT) - (bit ? 0: 1);
  1091. data_key_init(c, &to_key, inum, blk);
  1092. err = ubifs_tnc_remove_range(c, &key, &to_key);
  1093. if (err)
  1094. goto out_ro;
  1095. finish_reservation(c);
  1096. spin_lock(&ui->ui_lock);
  1097. ui->synced_i_size = ui->ui_size;
  1098. spin_unlock(&ui->ui_lock);
  1099. mark_inode_clean(c, ui);
  1100. kfree(ino);
  1101. return 0;
  1102. out_release:
  1103. release_head(c, BASEHD);
  1104. out_ro:
  1105. ubifs_ro_mode(c, err);
  1106. finish_reservation(c);
  1107. out_free:
  1108. kfree(ino);
  1109. return err;
  1110. }
  1111. #ifdef CONFIG_UBIFS_FS_XATTR
  1112. /**
  1113. * ubifs_jnl_delete_xattr - delete an extended attribute.
  1114. * @c: UBIFS file-system description object
  1115. * @host: host inode
  1116. * @inode: extended attribute inode
  1117. * @nm: extended attribute entry name
  1118. *
  1119. * This function delete an extended attribute which is very similar to
  1120. * un-linking regular files - it writes a deletion xentry, a deletion inode and
  1121. * updates the target inode. Returns zero in case of success and a negative
  1122. * error code in case of failure.
  1123. */
  1124. int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
  1125. const struct inode *inode, const struct qstr *nm)
  1126. {
  1127. int err, xlen, hlen, len, lnum, xent_offs, aligned_xlen;
  1128. struct ubifs_dent_node *xent;
  1129. struct ubifs_ino_node *ino;
  1130. union ubifs_key xent_key, key1, key2;
  1131. int sync = IS_DIRSYNC(host);
  1132. struct ubifs_inode *host_ui = ubifs_inode(host);
  1133. dbg_jnl("host %lu, xattr ino %lu, name '%s', data len %d",
  1134. host->i_ino, inode->i_ino, nm->name,
  1135. ubifs_inode(inode)->data_len);
  1136. ubifs_assert(inode->i_nlink == 0);
  1137. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1138. /*
  1139. * Since we are deleting the inode, we do not bother to attach any data
  1140. * to it and assume its length is %UBIFS_INO_NODE_SZ.
  1141. */
  1142. xlen = UBIFS_DENT_NODE_SZ + nm->len + 1;
  1143. aligned_xlen = ALIGN(xlen, 8);
  1144. hlen = host_ui->data_len + UBIFS_INO_NODE_SZ;
  1145. len = aligned_xlen + UBIFS_INO_NODE_SZ + ALIGN(hlen, 8);
  1146. xent = kmalloc(len, GFP_NOFS);
  1147. if (!xent)
  1148. return -ENOMEM;
  1149. /* Make reservation before allocating sequence numbers */
  1150. err = make_reservation(c, BASEHD, len);
  1151. if (err) {
  1152. kfree(xent);
  1153. return err;
  1154. }
  1155. xent->ch.node_type = UBIFS_XENT_NODE;
  1156. xent_key_init(c, &xent_key, host->i_ino, nm);
  1157. key_write(c, &xent_key, xent->key);
  1158. xent->inum = 0;
  1159. xent->type = get_dent_type(inode->i_mode);
  1160. xent->nlen = cpu_to_le16(nm->len);
  1161. memcpy(xent->name, nm->name, nm->len);
  1162. xent->name[nm->len] = '\0';
  1163. zero_dent_node_unused(xent);
  1164. ubifs_prep_grp_node(c, xent, xlen, 0);
  1165. ino = (void *)xent + aligned_xlen;
  1166. pack_inode(c, ino, inode, 0);
  1167. ino = (void *)ino + UBIFS_INO_NODE_SZ;
  1168. pack_inode(c, ino, host, 1);
  1169. err = write_head(c, BASEHD, xent, len, &lnum, &xent_offs, sync);
  1170. if (!sync && !err)
  1171. ubifs_wbuf_add_ino_nolock(&c->jheads[BASEHD].wbuf, host->i_ino);
  1172. release_head(c, BASEHD);
  1173. kfree(xent);
  1174. if (err)
  1175. goto out_ro;
  1176. /* Remove the extended attribute entry from TNC */
  1177. err = ubifs_tnc_remove_nm(c, &xent_key, nm);
  1178. if (err)
  1179. goto out_ro;
  1180. err = ubifs_add_dirt(c, lnum, xlen);
  1181. if (err)
  1182. goto out_ro;
  1183. /*
  1184. * Remove all nodes belonging to the extended attribute inode from TNC.
  1185. * Well, there actually must be only one node - the inode itself.
  1186. */
  1187. lowest_ino_key(c, &key1, inode->i_ino);
  1188. highest_ino_key(c, &key2, inode->i_ino);
  1189. err = ubifs_tnc_remove_range(c, &key1, &key2);
  1190. if (err)
  1191. goto out_ro;
  1192. err = ubifs_add_dirt(c, lnum, UBIFS_INO_NODE_SZ);
  1193. if (err)
  1194. goto out_ro;
  1195. /* And update TNC with the new host inode position */
  1196. ino_key_init(c, &key1, host->i_ino);
  1197. err = ubifs_tnc_add(c, &key1, lnum, xent_offs + len - hlen, hlen);
  1198. if (err)
  1199. goto out_ro;
  1200. finish_reservation(c);
  1201. spin_lock(&host_ui->ui_lock);
  1202. host_ui->synced_i_size = host_ui->ui_size;
  1203. spin_unlock(&host_ui->ui_lock);
  1204. mark_inode_clean(c, host_ui);
  1205. return 0;
  1206. out_ro:
  1207. ubifs_ro_mode(c, err);
  1208. finish_reservation(c);
  1209. return err;
  1210. }
  1211. /**
  1212. * ubifs_jnl_change_xattr - change an extended attribute.
  1213. * @c: UBIFS file-system description object
  1214. * @inode: extended attribute inode
  1215. * @host: host inode
  1216. *
  1217. * This function writes the updated version of an extended attribute inode and
  1218. * the host inode tho the journal (to the base head). The host inode is written
  1219. * after the extended attribute inode in order to guarantee that the extended
  1220. * attribute will be flushed when the inode is synchronized by 'fsync()' and
  1221. * consequently, the write-buffer is synchronized. This function returns zero
  1222. * in case of success and a negative error code in case of failure.
  1223. */
  1224. int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode,
  1225. const struct inode *host)
  1226. {
  1227. int err, len1, len2, aligned_len, aligned_len1, lnum, offs;
  1228. struct ubifs_inode *host_ui = ubifs_inode(host);
  1229. struct ubifs_ino_node *ino;
  1230. union ubifs_key key;
  1231. int sync = IS_DIRSYNC(host);
  1232. dbg_jnl("ino %lu, ino %lu", host->i_ino, inode->i_ino);
  1233. ubifs_assert(host->i_nlink > 0);
  1234. ubifs_assert(inode->i_nlink > 0);
  1235. ubifs_assert(mutex_is_locked(&host_ui->ui_mutex));
  1236. len1 = UBIFS_INO_NODE_SZ + host_ui->data_len;
  1237. len2 = UBIFS_INO_NODE_SZ + ubifs_inode(inode)->data_len;
  1238. aligned_len1 = ALIGN(len1, 8);
  1239. aligned_len = aligned_len1 + ALIGN(len2, 8);
  1240. ino = kmalloc(aligned_len, GFP_NOFS);
  1241. if (!ino)
  1242. return -ENOMEM;
  1243. /* Make reservation before allocating sequence numbers */
  1244. err = make_reservation(c, BASEHD, aligned_len);
  1245. if (err)
  1246. goto out_free;
  1247. pack_inode(c, ino, host, 0);
  1248. pack_inode(c, (void *)ino + aligned_len1, inode, 1);
  1249. err = write_head(c, BASEHD, ino, aligned_len, &lnum, &offs, 0);
  1250. if (!sync && !err) {
  1251. struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  1252. ubifs_wbuf_add_ino_nolock(wbuf, host->i_ino);
  1253. ubifs_wbuf_add_ino_nolock(wbuf, inode->i_ino);
  1254. }
  1255. release_head(c, BASEHD);
  1256. if (err)
  1257. goto out_ro;
  1258. ino_key_init(c, &key, host->i_ino);
  1259. err = ubifs_tnc_add(c, &key, lnum, offs, len1);
  1260. if (err)
  1261. goto out_ro;
  1262. ino_key_init(c, &key, inode->i_ino);
  1263. err = ubifs_tnc_add(c, &key, lnum, offs + aligned_len1, len2);
  1264. if (err)
  1265. goto out_ro;
  1266. finish_reservation(c);
  1267. spin_lock(&host_ui->ui_lock);
  1268. host_ui->synced_i_size = host_ui->ui_size;
  1269. spin_unlock(&host_ui->ui_lock);
  1270. mark_inode_clean(c, host_ui);
  1271. kfree(ino);
  1272. return 0;
  1273. out_ro:
  1274. ubifs_ro_mode(c, err);
  1275. finish_reservation(c);
  1276. out_free:
  1277. kfree(ino);
  1278. return err;
  1279. }
  1280. #endif /* CONFIG_UBIFS_FS_XATTR */