journal.c 47 KB

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