journal.c 42 KB

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