readwrite.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  1. /*
  2. * fs/logfs/readwrite.c
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. *
  8. *
  9. * Actually contains five sets of very similar functions:
  10. * read read blocks from a file
  11. * seek_hole find next hole
  12. * seek_data find next data block
  13. * valid check whether a block still belongs to a file
  14. * write write blocks to a file
  15. * delete delete a block (for directories and ifile)
  16. * rewrite move existing blocks of a file to a new location (gc helper)
  17. * truncate truncate a file
  18. */
  19. #include "logfs.h"
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. static u64 adjust_bix(u64 bix, level_t level)
  23. {
  24. switch (level) {
  25. case 0:
  26. return bix;
  27. case LEVEL(1):
  28. return max_t(u64, bix, I0_BLOCKS);
  29. case LEVEL(2):
  30. return max_t(u64, bix, I1_BLOCKS);
  31. case LEVEL(3):
  32. return max_t(u64, bix, I2_BLOCKS);
  33. case LEVEL(4):
  34. return max_t(u64, bix, I3_BLOCKS);
  35. case LEVEL(5):
  36. return max_t(u64, bix, I4_BLOCKS);
  37. default:
  38. WARN_ON(1);
  39. return bix;
  40. }
  41. }
  42. static inline u64 maxbix(u8 height)
  43. {
  44. return 1ULL << (LOGFS_BLOCK_BITS * height);
  45. }
  46. /**
  47. * The inode address space is cut in two halves. Lower half belongs to data
  48. * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
  49. * set, the actual block index (bix) and level can be derived from the page
  50. * index.
  51. *
  52. * The lowest three bits of the block index are set to 0 after packing and
  53. * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
  54. * anyway this is harmless.
  55. */
  56. #define ARCH_SHIFT (BITS_PER_LONG - 32)
  57. #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
  58. #define LEVEL_SHIFT (28 + ARCH_SHIFT)
  59. static inline pgoff_t first_indirect_block(void)
  60. {
  61. return INDIRECT_BIT | (1ULL << LEVEL_SHIFT);
  62. }
  63. pgoff_t logfs_pack_index(u64 bix, level_t level)
  64. {
  65. pgoff_t index;
  66. BUG_ON(bix >= INDIRECT_BIT);
  67. if (level == 0)
  68. return bix;
  69. index = INDIRECT_BIT;
  70. index |= (__force long)level << LEVEL_SHIFT;
  71. index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS);
  72. return index;
  73. }
  74. void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level)
  75. {
  76. u8 __level;
  77. if (!(index & INDIRECT_BIT)) {
  78. *bix = index;
  79. *level = 0;
  80. return;
  81. }
  82. __level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT;
  83. *level = LEVEL(__level);
  84. *bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT;
  85. *bix = adjust_bix(*bix, *level);
  86. return;
  87. }
  88. #undef ARCH_SHIFT
  89. #undef INDIRECT_BIT
  90. #undef LEVEL_SHIFT
  91. /*
  92. * Time is stored as nanoseconds since the epoch.
  93. */
  94. static struct timespec be64_to_timespec(__be64 betime)
  95. {
  96. return ns_to_timespec(be64_to_cpu(betime));
  97. }
  98. static __be64 timespec_to_be64(struct timespec tsp)
  99. {
  100. return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec);
  101. }
  102. static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode)
  103. {
  104. struct logfs_inode *li = logfs_inode(inode);
  105. int i;
  106. inode->i_mode = be16_to_cpu(di->di_mode);
  107. li->li_height = di->di_height;
  108. li->li_flags = be32_to_cpu(di->di_flags);
  109. i_uid_write(inode, be32_to_cpu(di->di_uid));
  110. i_gid_write(inode, be32_to_cpu(di->di_gid));
  111. inode->i_size = be64_to_cpu(di->di_size);
  112. logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes));
  113. inode->i_atime = be64_to_timespec(di->di_atime);
  114. inode->i_ctime = be64_to_timespec(di->di_ctime);
  115. inode->i_mtime = be64_to_timespec(di->di_mtime);
  116. set_nlink(inode, be32_to_cpu(di->di_refcount));
  117. inode->i_generation = be32_to_cpu(di->di_generation);
  118. switch (inode->i_mode & S_IFMT) {
  119. case S_IFSOCK: /* fall through */
  120. case S_IFBLK: /* fall through */
  121. case S_IFCHR: /* fall through */
  122. case S_IFIFO:
  123. inode->i_rdev = be64_to_cpu(di->di_data[0]);
  124. break;
  125. case S_IFDIR: /* fall through */
  126. case S_IFREG: /* fall through */
  127. case S_IFLNK:
  128. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  129. li->li_data[i] = be64_to_cpu(di->di_data[i]);
  130. break;
  131. default:
  132. BUG();
  133. }
  134. }
  135. static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di)
  136. {
  137. struct logfs_inode *li = logfs_inode(inode);
  138. int i;
  139. di->di_mode = cpu_to_be16(inode->i_mode);
  140. di->di_height = li->li_height;
  141. di->di_pad = 0;
  142. di->di_flags = cpu_to_be32(li->li_flags);
  143. di->di_uid = cpu_to_be32(i_uid_read(inode));
  144. di->di_gid = cpu_to_be32(i_gid_read(inode));
  145. di->di_size = cpu_to_be64(i_size_read(inode));
  146. di->di_used_bytes = cpu_to_be64(li->li_used_bytes);
  147. di->di_atime = timespec_to_be64(inode->i_atime);
  148. di->di_ctime = timespec_to_be64(inode->i_ctime);
  149. di->di_mtime = timespec_to_be64(inode->i_mtime);
  150. di->di_refcount = cpu_to_be32(inode->i_nlink);
  151. di->di_generation = cpu_to_be32(inode->i_generation);
  152. switch (inode->i_mode & S_IFMT) {
  153. case S_IFSOCK: /* fall through */
  154. case S_IFBLK: /* fall through */
  155. case S_IFCHR: /* fall through */
  156. case S_IFIFO:
  157. di->di_data[0] = cpu_to_be64(inode->i_rdev);
  158. break;
  159. case S_IFDIR: /* fall through */
  160. case S_IFREG: /* fall through */
  161. case S_IFLNK:
  162. for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
  163. di->di_data[i] = cpu_to_be64(li->li_data[i]);
  164. break;
  165. default:
  166. BUG();
  167. }
  168. }
  169. static void __logfs_set_blocks(struct inode *inode)
  170. {
  171. struct super_block *sb = inode->i_sb;
  172. struct logfs_inode *li = logfs_inode(inode);
  173. inode->i_blocks = ULONG_MAX;
  174. if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX)
  175. inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9;
  176. }
  177. void logfs_set_blocks(struct inode *inode, u64 bytes)
  178. {
  179. struct logfs_inode *li = logfs_inode(inode);
  180. li->li_used_bytes = bytes;
  181. __logfs_set_blocks(inode);
  182. }
  183. static void prelock_page(struct super_block *sb, struct page *page, int lock)
  184. {
  185. struct logfs_super *super = logfs_super(sb);
  186. BUG_ON(!PageLocked(page));
  187. if (lock) {
  188. BUG_ON(PagePreLocked(page));
  189. SetPagePreLocked(page);
  190. } else {
  191. /* We are in GC path. */
  192. if (PagePreLocked(page))
  193. super->s_lock_count++;
  194. else
  195. SetPagePreLocked(page);
  196. }
  197. }
  198. static void preunlock_page(struct super_block *sb, struct page *page, int lock)
  199. {
  200. struct logfs_super *super = logfs_super(sb);
  201. BUG_ON(!PageLocked(page));
  202. if (lock)
  203. ClearPagePreLocked(page);
  204. else {
  205. /* We are in GC path. */
  206. BUG_ON(!PagePreLocked(page));
  207. if (super->s_lock_count)
  208. super->s_lock_count--;
  209. else
  210. ClearPagePreLocked(page);
  211. }
  212. }
  213. /*
  214. * Logfs is prone to an AB-BA deadlock where one task tries to acquire
  215. * s_write_mutex with a locked page and GC tries to get that page while holding
  216. * s_write_mutex.
  217. * To solve this issue logfs will ignore the page lock iff the page in question
  218. * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
  219. * in addition to PG_locked.
  220. */
  221. void logfs_get_wblocks(struct super_block *sb, struct page *page, int lock)
  222. {
  223. struct logfs_super *super = logfs_super(sb);
  224. if (page)
  225. prelock_page(sb, page, lock);
  226. if (lock) {
  227. mutex_lock(&super->s_write_mutex);
  228. logfs_gc_pass(sb);
  229. /* FIXME: We also have to check for shadowed space
  230. * and mempool fill grade */
  231. }
  232. }
  233. void logfs_put_wblocks(struct super_block *sb, struct page *page, int lock)
  234. {
  235. struct logfs_super *super = logfs_super(sb);
  236. if (page)
  237. preunlock_page(sb, page, lock);
  238. /* Order matters - we must clear PG_pre_locked before releasing
  239. * s_write_mutex or we could race against another task. */
  240. if (lock)
  241. mutex_unlock(&super->s_write_mutex);
  242. }
  243. static struct page *logfs_get_read_page(struct inode *inode, u64 bix,
  244. level_t level)
  245. {
  246. return find_or_create_page(inode->i_mapping,
  247. logfs_pack_index(bix, level), GFP_NOFS);
  248. }
  249. static void logfs_put_read_page(struct page *page)
  250. {
  251. unlock_page(page);
  252. page_cache_release(page);
  253. }
  254. static void logfs_lock_write_page(struct page *page)
  255. {
  256. int loop = 0;
  257. while (unlikely(!trylock_page(page))) {
  258. if (loop++ > 0x1000) {
  259. /* Has been observed once so far... */
  260. printk(KERN_ERR "stack at %p\n", &loop);
  261. BUG();
  262. }
  263. if (PagePreLocked(page)) {
  264. /* Holder of page lock is waiting for us, it
  265. * is safe to use this page. */
  266. break;
  267. }
  268. /* Some other process has this page locked and has
  269. * nothing to do with us. Wait for it to finish.
  270. */
  271. schedule();
  272. }
  273. BUG_ON(!PageLocked(page));
  274. }
  275. static struct page *logfs_get_write_page(struct inode *inode, u64 bix,
  276. level_t level)
  277. {
  278. struct address_space *mapping = inode->i_mapping;
  279. pgoff_t index = logfs_pack_index(bix, level);
  280. struct page *page;
  281. int err;
  282. repeat:
  283. page = find_get_page(mapping, index);
  284. if (!page) {
  285. page = __page_cache_alloc(GFP_NOFS);
  286. if (!page)
  287. return NULL;
  288. err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS);
  289. if (unlikely(err)) {
  290. page_cache_release(page);
  291. if (err == -EEXIST)
  292. goto repeat;
  293. return NULL;
  294. }
  295. } else logfs_lock_write_page(page);
  296. BUG_ON(!PageLocked(page));
  297. return page;
  298. }
  299. static void logfs_unlock_write_page(struct page *page)
  300. {
  301. if (!PagePreLocked(page))
  302. unlock_page(page);
  303. }
  304. static void logfs_put_write_page(struct page *page)
  305. {
  306. logfs_unlock_write_page(page);
  307. page_cache_release(page);
  308. }
  309. static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level,
  310. int rw)
  311. {
  312. if (rw == READ)
  313. return logfs_get_read_page(inode, bix, level);
  314. else
  315. return logfs_get_write_page(inode, bix, level);
  316. }
  317. static void logfs_put_page(struct page *page, int rw)
  318. {
  319. if (rw == READ)
  320. logfs_put_read_page(page);
  321. else
  322. logfs_put_write_page(page);
  323. }
  324. static unsigned long __get_bits(u64 val, int skip, int no)
  325. {
  326. u64 ret = val;
  327. ret >>= skip * no;
  328. ret <<= 64 - no;
  329. ret >>= 64 - no;
  330. return ret;
  331. }
  332. static unsigned long get_bits(u64 val, level_t skip)
  333. {
  334. return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS);
  335. }
  336. static inline void init_shadow_tree(struct super_block *sb,
  337. struct shadow_tree *tree)
  338. {
  339. struct logfs_super *super = logfs_super(sb);
  340. btree_init_mempool64(&tree->new, super->s_btree_pool);
  341. btree_init_mempool64(&tree->old, super->s_btree_pool);
  342. }
  343. static void indirect_write_block(struct logfs_block *block)
  344. {
  345. struct page *page;
  346. struct inode *inode;
  347. int ret;
  348. page = block->page;
  349. inode = page->mapping->host;
  350. logfs_lock_write_page(page);
  351. ret = logfs_write_buf(inode, page, 0);
  352. logfs_unlock_write_page(page);
  353. /*
  354. * This needs some rework. Unless you want your filesystem to run
  355. * completely synchronously (you don't), the filesystem will always
  356. * report writes as 'successful' before the actual work has been
  357. * done. The actual work gets done here and this is where any errors
  358. * will show up. And there isn't much we can do about it, really.
  359. *
  360. * Some attempts to fix the errors (move from bad blocks, retry io,...)
  361. * have already been done, so anything left should be either a broken
  362. * device or a bug somewhere in logfs itself. Being relatively new,
  363. * the odds currently favor a bug, so for now the line below isn't
  364. * entirely tasteles.
  365. */
  366. BUG_ON(ret);
  367. }
  368. static void inode_write_block(struct logfs_block *block)
  369. {
  370. struct inode *inode;
  371. int ret;
  372. inode = block->inode;
  373. if (inode->i_ino == LOGFS_INO_MASTER)
  374. logfs_write_anchor(inode->i_sb);
  375. else {
  376. ret = __logfs_write_inode(inode, NULL, 0);
  377. /* see indirect_write_block comment */
  378. BUG_ON(ret);
  379. }
  380. }
  381. /*
  382. * This silences a false, yet annoying gcc warning. I hate it when my editor
  383. * jumps into bitops.h each time I recompile this file.
  384. * TODO: Complain to gcc folks about this and upgrade compiler.
  385. */
  386. static unsigned long fnb(const unsigned long *addr,
  387. unsigned long size, unsigned long offset)
  388. {
  389. return find_next_bit(addr, size, offset);
  390. }
  391. static __be64 inode_val0(struct inode *inode)
  392. {
  393. struct logfs_inode *li = logfs_inode(inode);
  394. u64 val;
  395. /*
  396. * Explicit shifting generates good code, but must match the format
  397. * of the structure. Add some paranoia just in case.
  398. */
  399. BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0);
  400. BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2);
  401. BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4);
  402. val = (u64)inode->i_mode << 48 |
  403. (u64)li->li_height << 40 |
  404. (u64)li->li_flags;
  405. return cpu_to_be64(val);
  406. }
  407. static int inode_write_alias(struct super_block *sb,
  408. struct logfs_block *block, write_alias_t *write_one_alias)
  409. {
  410. struct inode *inode = block->inode;
  411. struct logfs_inode *li = logfs_inode(inode);
  412. unsigned long pos;
  413. u64 ino , bix;
  414. __be64 val;
  415. level_t level;
  416. int err;
  417. for (pos = 0; ; pos++) {
  418. pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
  419. if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS)
  420. return 0;
  421. switch (pos) {
  422. case INODE_HEIGHT_OFS:
  423. val = inode_val0(inode);
  424. break;
  425. case INODE_USED_OFS:
  426. val = cpu_to_be64(li->li_used_bytes);
  427. break;
  428. case INODE_SIZE_OFS:
  429. val = cpu_to_be64(i_size_read(inode));
  430. break;
  431. case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1:
  432. val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]);
  433. break;
  434. default:
  435. BUG();
  436. }
  437. ino = LOGFS_INO_MASTER;
  438. bix = inode->i_ino;
  439. level = LEVEL(0);
  440. err = write_one_alias(sb, ino, bix, level, pos, val);
  441. if (err)
  442. return err;
  443. }
  444. }
  445. static int indirect_write_alias(struct super_block *sb,
  446. struct logfs_block *block, write_alias_t *write_one_alias)
  447. {
  448. unsigned long pos;
  449. struct page *page = block->page;
  450. u64 ino , bix;
  451. __be64 *child, val;
  452. level_t level;
  453. int err;
  454. for (pos = 0; ; pos++) {
  455. pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
  456. if (pos >= LOGFS_BLOCK_FACTOR)
  457. return 0;
  458. ino = page->mapping->host->i_ino;
  459. logfs_unpack_index(page->index, &bix, &level);
  460. child = kmap_atomic(page);
  461. val = child[pos];
  462. kunmap_atomic(child);
  463. err = write_one_alias(sb, ino, bix, level, pos, val);
  464. if (err)
  465. return err;
  466. }
  467. }
  468. int logfs_write_obj_aliases_pagecache(struct super_block *sb)
  469. {
  470. struct logfs_super *super = logfs_super(sb);
  471. struct logfs_block *block;
  472. int err;
  473. list_for_each_entry(block, &super->s_object_alias, alias_list) {
  474. err = block->ops->write_alias(sb, block, write_alias_journal);
  475. if (err)
  476. return err;
  477. }
  478. return 0;
  479. }
  480. void __free_block(struct super_block *sb, struct logfs_block *block)
  481. {
  482. BUG_ON(!list_empty(&block->item_list));
  483. list_del(&block->alias_list);
  484. mempool_free(block, logfs_super(sb)->s_block_pool);
  485. }
  486. static void inode_free_block(struct super_block *sb, struct logfs_block *block)
  487. {
  488. struct inode *inode = block->inode;
  489. logfs_inode(inode)->li_block = NULL;
  490. __free_block(sb, block);
  491. }
  492. static void indirect_free_block(struct super_block *sb,
  493. struct logfs_block *block)
  494. {
  495. struct page *page = block->page;
  496. if (PagePrivate(page)) {
  497. ClearPagePrivate(page);
  498. page_cache_release(page);
  499. set_page_private(page, 0);
  500. }
  501. __free_block(sb, block);
  502. }
  503. static struct logfs_block_ops inode_block_ops = {
  504. .write_block = inode_write_block,
  505. .free_block = inode_free_block,
  506. .write_alias = inode_write_alias,
  507. };
  508. struct logfs_block_ops indirect_block_ops = {
  509. .write_block = indirect_write_block,
  510. .free_block = indirect_free_block,
  511. .write_alias = indirect_write_alias,
  512. };
  513. struct logfs_block *__alloc_block(struct super_block *sb,
  514. u64 ino, u64 bix, level_t level)
  515. {
  516. struct logfs_super *super = logfs_super(sb);
  517. struct logfs_block *block;
  518. block = mempool_alloc(super->s_block_pool, GFP_NOFS);
  519. memset(block, 0, sizeof(*block));
  520. INIT_LIST_HEAD(&block->alias_list);
  521. INIT_LIST_HEAD(&block->item_list);
  522. block->sb = sb;
  523. block->ino = ino;
  524. block->bix = bix;
  525. block->level = level;
  526. return block;
  527. }
  528. static void alloc_inode_block(struct inode *inode)
  529. {
  530. struct logfs_inode *li = logfs_inode(inode);
  531. struct logfs_block *block;
  532. if (li->li_block)
  533. return;
  534. block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0);
  535. block->inode = inode;
  536. li->li_block = block;
  537. block->ops = &inode_block_ops;
  538. }
  539. void initialize_block_counters(struct page *page, struct logfs_block *block,
  540. __be64 *array, int page_is_empty)
  541. {
  542. u64 ptr;
  543. int i, start;
  544. block->partial = 0;
  545. block->full = 0;
  546. start = 0;
  547. if (page->index < first_indirect_block()) {
  548. /* Counters are pointless on level 0 */
  549. return;
  550. }
  551. if (page->index == first_indirect_block()) {
  552. /* Skip unused pointers */
  553. start = I0_BLOCKS;
  554. block->full = I0_BLOCKS;
  555. }
  556. if (!page_is_empty) {
  557. for (i = start; i < LOGFS_BLOCK_FACTOR; i++) {
  558. ptr = be64_to_cpu(array[i]);
  559. if (ptr)
  560. block->partial++;
  561. if (ptr & LOGFS_FULLY_POPULATED)
  562. block->full++;
  563. }
  564. }
  565. }
  566. static void alloc_data_block(struct inode *inode, struct page *page)
  567. {
  568. struct logfs_block *block;
  569. u64 bix;
  570. level_t level;
  571. if (PagePrivate(page))
  572. return;
  573. logfs_unpack_index(page->index, &bix, &level);
  574. block = __alloc_block(inode->i_sb, inode->i_ino, bix, level);
  575. block->page = page;
  576. SetPagePrivate(page);
  577. page_cache_get(page);
  578. set_page_private(page, (unsigned long) block);
  579. block->ops = &indirect_block_ops;
  580. }
  581. static void alloc_indirect_block(struct inode *inode, struct page *page,
  582. int page_is_empty)
  583. {
  584. struct logfs_block *block;
  585. __be64 *array;
  586. if (PagePrivate(page))
  587. return;
  588. alloc_data_block(inode, page);
  589. block = logfs_block(page);
  590. array = kmap_atomic(page);
  591. initialize_block_counters(page, block, array, page_is_empty);
  592. kunmap_atomic(array);
  593. }
  594. static void block_set_pointer(struct page *page, int index, u64 ptr)
  595. {
  596. struct logfs_block *block = logfs_block(page);
  597. __be64 *array;
  598. u64 oldptr;
  599. BUG_ON(!block);
  600. array = kmap_atomic(page);
  601. oldptr = be64_to_cpu(array[index]);
  602. array[index] = cpu_to_be64(ptr);
  603. kunmap_atomic(array);
  604. SetPageUptodate(page);
  605. block->full += !!(ptr & LOGFS_FULLY_POPULATED)
  606. - !!(oldptr & LOGFS_FULLY_POPULATED);
  607. block->partial += !!ptr - !!oldptr;
  608. }
  609. static u64 block_get_pointer(struct page *page, int index)
  610. {
  611. __be64 *block;
  612. u64 ptr;
  613. block = kmap_atomic(page);
  614. ptr = be64_to_cpu(block[index]);
  615. kunmap_atomic(block);
  616. return ptr;
  617. }
  618. static int logfs_read_empty(struct page *page)
  619. {
  620. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  621. return 0;
  622. }
  623. static int logfs_read_direct(struct inode *inode, struct page *page)
  624. {
  625. struct logfs_inode *li = logfs_inode(inode);
  626. pgoff_t index = page->index;
  627. u64 block;
  628. block = li->li_data[index];
  629. if (!block)
  630. return logfs_read_empty(page);
  631. return logfs_segment_read(inode, page, block, index, 0);
  632. }
  633. static int logfs_read_loop(struct inode *inode, struct page *page,
  634. int rw_context)
  635. {
  636. struct logfs_inode *li = logfs_inode(inode);
  637. u64 bix, bofs = li->li_data[INDIRECT_INDEX];
  638. level_t level, target_level;
  639. int ret;
  640. struct page *ipage;
  641. logfs_unpack_index(page->index, &bix, &target_level);
  642. if (!bofs)
  643. return logfs_read_empty(page);
  644. if (bix >= maxbix(li->li_height))
  645. return logfs_read_empty(page);
  646. for (level = LEVEL(li->li_height);
  647. (__force u8)level > (__force u8)target_level;
  648. level = SUBLEVEL(level)){
  649. ipage = logfs_get_page(inode, bix, level, rw_context);
  650. if (!ipage)
  651. return -ENOMEM;
  652. ret = logfs_segment_read(inode, ipage, bofs, bix, level);
  653. if (ret) {
  654. logfs_put_read_page(ipage);
  655. return ret;
  656. }
  657. bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
  658. logfs_put_page(ipage, rw_context);
  659. if (!bofs)
  660. return logfs_read_empty(page);
  661. }
  662. return logfs_segment_read(inode, page, bofs, bix, 0);
  663. }
  664. static int logfs_read_block(struct inode *inode, struct page *page,
  665. int rw_context)
  666. {
  667. pgoff_t index = page->index;
  668. if (index < I0_BLOCKS)
  669. return logfs_read_direct(inode, page);
  670. return logfs_read_loop(inode, page, rw_context);
  671. }
  672. static int logfs_exist_loop(struct inode *inode, u64 bix)
  673. {
  674. struct logfs_inode *li = logfs_inode(inode);
  675. u64 bofs = li->li_data[INDIRECT_INDEX];
  676. level_t level;
  677. int ret;
  678. struct page *ipage;
  679. if (!bofs)
  680. return 0;
  681. if (bix >= maxbix(li->li_height))
  682. return 0;
  683. for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
  684. ipage = logfs_get_read_page(inode, bix, level);
  685. if (!ipage)
  686. return -ENOMEM;
  687. ret = logfs_segment_read(inode, ipage, bofs, bix, level);
  688. if (ret) {
  689. logfs_put_read_page(ipage);
  690. return ret;
  691. }
  692. bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
  693. logfs_put_read_page(ipage);
  694. if (!bofs)
  695. return 0;
  696. }
  697. return 1;
  698. }
  699. int logfs_exist_block(struct inode *inode, u64 bix)
  700. {
  701. struct logfs_inode *li = logfs_inode(inode);
  702. if (bix < I0_BLOCKS)
  703. return !!li->li_data[bix];
  704. return logfs_exist_loop(inode, bix);
  705. }
  706. static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data)
  707. {
  708. struct logfs_inode *li = logfs_inode(inode);
  709. for (; bix < I0_BLOCKS; bix++)
  710. if (data ^ (li->li_data[bix] == 0))
  711. return bix;
  712. return I0_BLOCKS;
  713. }
  714. static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data)
  715. {
  716. struct logfs_inode *li = logfs_inode(inode);
  717. __be64 *rblock;
  718. u64 increment, bofs = li->li_data[INDIRECT_INDEX];
  719. level_t level;
  720. int ret, slot;
  721. struct page *page;
  722. BUG_ON(!bofs);
  723. for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
  724. increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1));
  725. page = logfs_get_read_page(inode, bix, level);
  726. if (!page)
  727. return bix;
  728. ret = logfs_segment_read(inode, page, bofs, bix, level);
  729. if (ret) {
  730. logfs_put_read_page(page);
  731. return bix;
  732. }
  733. slot = get_bits(bix, SUBLEVEL(level));
  734. rblock = kmap_atomic(page);
  735. while (slot < LOGFS_BLOCK_FACTOR) {
  736. if (data && (rblock[slot] != 0))
  737. break;
  738. if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED))
  739. break;
  740. slot++;
  741. bix += increment;
  742. bix &= ~(increment - 1);
  743. }
  744. if (slot >= LOGFS_BLOCK_FACTOR) {
  745. kunmap_atomic(rblock);
  746. logfs_put_read_page(page);
  747. return bix;
  748. }
  749. bofs = be64_to_cpu(rblock[slot]);
  750. kunmap_atomic(rblock);
  751. logfs_put_read_page(page);
  752. if (!bofs) {
  753. BUG_ON(data);
  754. return bix;
  755. }
  756. }
  757. return bix;
  758. }
  759. /**
  760. * logfs_seek_hole - find next hole starting at a given block index
  761. * @inode: inode to search in
  762. * @bix: block index to start searching
  763. *
  764. * Returns next hole. If the file doesn't contain any further holes, the
  765. * block address next to eof is returned instead.
  766. */
  767. u64 logfs_seek_hole(struct inode *inode, u64 bix)
  768. {
  769. struct logfs_inode *li = logfs_inode(inode);
  770. if (bix < I0_BLOCKS) {
  771. bix = seek_holedata_direct(inode, bix, 0);
  772. if (bix < I0_BLOCKS)
  773. return bix;
  774. }
  775. if (!li->li_data[INDIRECT_INDEX])
  776. return bix;
  777. else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED)
  778. bix = maxbix(li->li_height);
  779. else if (bix >= maxbix(li->li_height))
  780. return bix;
  781. else {
  782. bix = seek_holedata_loop(inode, bix, 0);
  783. if (bix < maxbix(li->li_height))
  784. return bix;
  785. /* Should not happen anymore. But if some port writes semi-
  786. * corrupt images (as this one used to) we might run into it.
  787. */
  788. WARN_ON_ONCE(bix == maxbix(li->li_height));
  789. }
  790. return bix;
  791. }
  792. static u64 __logfs_seek_data(struct inode *inode, u64 bix)
  793. {
  794. struct logfs_inode *li = logfs_inode(inode);
  795. if (bix < I0_BLOCKS) {
  796. bix = seek_holedata_direct(inode, bix, 1);
  797. if (bix < I0_BLOCKS)
  798. return bix;
  799. }
  800. if (bix < maxbix(li->li_height)) {
  801. if (!li->li_data[INDIRECT_INDEX])
  802. bix = maxbix(li->li_height);
  803. else
  804. return seek_holedata_loop(inode, bix, 1);
  805. }
  806. return bix;
  807. }
  808. /**
  809. * logfs_seek_data - find next data block after a given block index
  810. * @inode: inode to search in
  811. * @bix: block index to start searching
  812. *
  813. * Returns next data block. If the file doesn't contain any further data
  814. * blocks, the last block in the file is returned instead.
  815. */
  816. u64 logfs_seek_data(struct inode *inode, u64 bix)
  817. {
  818. struct super_block *sb = inode->i_sb;
  819. u64 ret, end;
  820. ret = __logfs_seek_data(inode, bix);
  821. end = i_size_read(inode) >> sb->s_blocksize_bits;
  822. if (ret >= end)
  823. ret = max(bix, end);
  824. return ret;
  825. }
  826. static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs)
  827. {
  828. return pure_ofs(li->li_data[bix]) == ofs;
  829. }
  830. static int __logfs_is_valid_loop(struct inode *inode, u64 bix,
  831. u64 ofs, u64 bofs)
  832. {
  833. struct logfs_inode *li = logfs_inode(inode);
  834. level_t level;
  835. int ret;
  836. struct page *page;
  837. for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){
  838. page = logfs_get_write_page(inode, bix, level);
  839. BUG_ON(!page);
  840. ret = logfs_segment_read(inode, page, bofs, bix, level);
  841. if (ret) {
  842. logfs_put_write_page(page);
  843. return 0;
  844. }
  845. bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level)));
  846. logfs_put_write_page(page);
  847. if (!bofs)
  848. return 0;
  849. if (pure_ofs(bofs) == ofs)
  850. return 1;
  851. }
  852. return 0;
  853. }
  854. static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs)
  855. {
  856. struct logfs_inode *li = logfs_inode(inode);
  857. u64 bofs = li->li_data[INDIRECT_INDEX];
  858. if (!bofs)
  859. return 0;
  860. if (bix >= maxbix(li->li_height))
  861. return 0;
  862. if (pure_ofs(bofs) == ofs)
  863. return 1;
  864. return __logfs_is_valid_loop(inode, bix, ofs, bofs);
  865. }
  866. static int __logfs_is_valid_block(struct inode *inode, u64 bix, u64 ofs)
  867. {
  868. struct logfs_inode *li = logfs_inode(inode);
  869. if ((inode->i_nlink == 0) && atomic_read(&inode->i_count) == 1)
  870. return 0;
  871. if (bix < I0_BLOCKS)
  872. return logfs_is_valid_direct(li, bix, ofs);
  873. return logfs_is_valid_loop(inode, bix, ofs);
  874. }
  875. /**
  876. * logfs_is_valid_block - check whether this block is still valid
  877. *
  878. * @sb: superblock
  879. * @ofs: block physical offset
  880. * @ino: block inode number
  881. * @bix: block index
  882. * @gc_level: block level
  883. *
  884. * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
  885. * become invalid once the journal is written.
  886. */
  887. int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
  888. gc_level_t gc_level)
  889. {
  890. struct logfs_super *super = logfs_super(sb);
  891. struct inode *inode;
  892. int ret, cookie;
  893. /* Umount closes a segment with free blocks remaining. Those
  894. * blocks are by definition invalid. */
  895. if (ino == -1)
  896. return 0;
  897. LOGFS_BUG_ON((u64)(u_long)ino != ino, sb);
  898. inode = logfs_safe_iget(sb, ino, &cookie);
  899. if (IS_ERR(inode))
  900. goto invalid;
  901. ret = __logfs_is_valid_block(inode, bix, ofs);
  902. logfs_safe_iput(inode, cookie);
  903. if (ret)
  904. return ret;
  905. invalid:
  906. /* Block is nominally invalid, but may still sit in the shadow tree,
  907. * waiting for a journal commit.
  908. */
  909. if (btree_lookup64(&super->s_shadow_tree.old, ofs))
  910. return 2;
  911. return 0;
  912. }
  913. int logfs_readpage_nolock(struct page *page)
  914. {
  915. struct inode *inode = page->mapping->host;
  916. int ret = -EIO;
  917. ret = logfs_read_block(inode, page, READ);
  918. if (ret) {
  919. ClearPageUptodate(page);
  920. SetPageError(page);
  921. } else {
  922. SetPageUptodate(page);
  923. ClearPageError(page);
  924. }
  925. flush_dcache_page(page);
  926. return ret;
  927. }
  928. static int logfs_reserve_bytes(struct inode *inode, int bytes)
  929. {
  930. struct logfs_super *super = logfs_super(inode->i_sb);
  931. u64 available = super->s_free_bytes + super->s_dirty_free_bytes
  932. - super->s_dirty_used_bytes - super->s_dirty_pages;
  933. if (!bytes)
  934. return 0;
  935. if (available < bytes)
  936. return -ENOSPC;
  937. if (available < bytes + super->s_root_reserve &&
  938. !capable(CAP_SYS_RESOURCE))
  939. return -ENOSPC;
  940. return 0;
  941. }
  942. int get_page_reserve(struct inode *inode, struct page *page)
  943. {
  944. struct logfs_super *super = logfs_super(inode->i_sb);
  945. struct logfs_block *block = logfs_block(page);
  946. int ret;
  947. if (block && block->reserved_bytes)
  948. return 0;
  949. logfs_get_wblocks(inode->i_sb, page, WF_LOCK);
  950. while ((ret = logfs_reserve_bytes(inode, 6 * LOGFS_MAX_OBJECTSIZE)) &&
  951. !list_empty(&super->s_writeback_list)) {
  952. block = list_entry(super->s_writeback_list.next,
  953. struct logfs_block, alias_list);
  954. block->ops->write_block(block);
  955. }
  956. if (!ret) {
  957. alloc_data_block(inode, page);
  958. block = logfs_block(page);
  959. block->reserved_bytes += 6 * LOGFS_MAX_OBJECTSIZE;
  960. super->s_dirty_pages += 6 * LOGFS_MAX_OBJECTSIZE;
  961. list_move_tail(&block->alias_list, &super->s_writeback_list);
  962. }
  963. logfs_put_wblocks(inode->i_sb, page, WF_LOCK);
  964. return ret;
  965. }
  966. /*
  967. * We are protected by write lock. Push victims up to superblock level
  968. * and release transaction when appropriate.
  969. */
  970. /* FIXME: This is currently called from the wrong spots. */
  971. static void logfs_handle_transaction(struct inode *inode,
  972. struct logfs_transaction *ta)
  973. {
  974. struct logfs_super *super = logfs_super(inode->i_sb);
  975. if (!ta)
  976. return;
  977. logfs_inode(inode)->li_block->ta = NULL;
  978. if (inode->i_ino != LOGFS_INO_MASTER) {
  979. BUG(); /* FIXME: Yes, this needs more thought */
  980. /* just remember the transaction until inode is written */
  981. //BUG_ON(logfs_inode(inode)->li_transaction);
  982. //logfs_inode(inode)->li_transaction = ta;
  983. return;
  984. }
  985. switch (ta->state) {
  986. case CREATE_1: /* fall through */
  987. case UNLINK_1:
  988. BUG_ON(super->s_victim_ino);
  989. super->s_victim_ino = ta->ino;
  990. break;
  991. case CREATE_2: /* fall through */
  992. case UNLINK_2:
  993. BUG_ON(super->s_victim_ino != ta->ino);
  994. super->s_victim_ino = 0;
  995. /* transaction ends here - free it */
  996. kfree(ta);
  997. break;
  998. case CROSS_RENAME_1:
  999. BUG_ON(super->s_rename_dir);
  1000. BUG_ON(super->s_rename_pos);
  1001. super->s_rename_dir = ta->dir;
  1002. super->s_rename_pos = ta->pos;
  1003. break;
  1004. case CROSS_RENAME_2:
  1005. BUG_ON(super->s_rename_dir != ta->dir);
  1006. BUG_ON(super->s_rename_pos != ta->pos);
  1007. super->s_rename_dir = 0;
  1008. super->s_rename_pos = 0;
  1009. kfree(ta);
  1010. break;
  1011. case TARGET_RENAME_1:
  1012. BUG_ON(super->s_rename_dir);
  1013. BUG_ON(super->s_rename_pos);
  1014. BUG_ON(super->s_victim_ino);
  1015. super->s_rename_dir = ta->dir;
  1016. super->s_rename_pos = ta->pos;
  1017. super->s_victim_ino = ta->ino;
  1018. break;
  1019. case TARGET_RENAME_2:
  1020. BUG_ON(super->s_rename_dir != ta->dir);
  1021. BUG_ON(super->s_rename_pos != ta->pos);
  1022. BUG_ON(super->s_victim_ino != ta->ino);
  1023. super->s_rename_dir = 0;
  1024. super->s_rename_pos = 0;
  1025. break;
  1026. case TARGET_RENAME_3:
  1027. BUG_ON(super->s_rename_dir);
  1028. BUG_ON(super->s_rename_pos);
  1029. BUG_ON(super->s_victim_ino != ta->ino);
  1030. super->s_victim_ino = 0;
  1031. kfree(ta);
  1032. break;
  1033. default:
  1034. BUG();
  1035. }
  1036. }
  1037. /*
  1038. * Not strictly a reservation, but rather a check that we still have enough
  1039. * space to satisfy the write.
  1040. */
  1041. static int logfs_reserve_blocks(struct inode *inode, int blocks)
  1042. {
  1043. return logfs_reserve_bytes(inode, blocks * LOGFS_MAX_OBJECTSIZE);
  1044. }
  1045. struct write_control {
  1046. u64 ofs;
  1047. long flags;
  1048. };
  1049. static struct logfs_shadow *alloc_shadow(struct inode *inode, u64 bix,
  1050. level_t level, u64 old_ofs)
  1051. {
  1052. struct logfs_super *super = logfs_super(inode->i_sb);
  1053. struct logfs_shadow *shadow;
  1054. shadow = mempool_alloc(super->s_shadow_pool, GFP_NOFS);
  1055. memset(shadow, 0, sizeof(*shadow));
  1056. shadow->ino = inode->i_ino;
  1057. shadow->bix = bix;
  1058. shadow->gc_level = expand_level(inode->i_ino, level);
  1059. shadow->old_ofs = old_ofs & ~LOGFS_FULLY_POPULATED;
  1060. return shadow;
  1061. }
  1062. static void free_shadow(struct inode *inode, struct logfs_shadow *shadow)
  1063. {
  1064. struct logfs_super *super = logfs_super(inode->i_sb);
  1065. mempool_free(shadow, super->s_shadow_pool);
  1066. }
  1067. static void mark_segment(struct shadow_tree *tree, u32 segno)
  1068. {
  1069. int err;
  1070. if (!btree_lookup32(&tree->segment_map, segno)) {
  1071. err = btree_insert32(&tree->segment_map, segno, (void *)1,
  1072. GFP_NOFS);
  1073. BUG_ON(err);
  1074. tree->no_shadowed_segments++;
  1075. }
  1076. }
  1077. /**
  1078. * fill_shadow_tree - Propagate shadow tree changes due to a write
  1079. * @inode: Inode owning the page
  1080. * @page: Struct page that was written
  1081. * @shadow: Shadow for the current write
  1082. *
  1083. * Writes in logfs can result in two semi-valid objects. The old object
  1084. * is still valid as long as it can be reached by following pointers on
  1085. * the medium. Only when writes propagate all the way up to the journal
  1086. * has the new object safely replaced the old one.
  1087. *
  1088. * To handle this problem, a struct logfs_shadow is used to represent
  1089. * every single write. It is attached to the indirect block, which is
  1090. * marked dirty. When the indirect block is written, its shadows are
  1091. * handed up to the next indirect block (or inode). Untimately they
  1092. * will reach the master inode and be freed upon journal commit.
  1093. *
  1094. * This function handles a single step in the propagation. It adds the
  1095. * shadow for the current write to the tree, along with any shadows in
  1096. * the page's tree, in case it was an indirect block. If a page is
  1097. * written, the inode parameter is left NULL, if an inode is written,
  1098. * the page parameter is left NULL.
  1099. */
  1100. static void fill_shadow_tree(struct inode *inode, struct page *page,
  1101. struct logfs_shadow *shadow)
  1102. {
  1103. struct logfs_super *super = logfs_super(inode->i_sb);
  1104. struct logfs_block *block = logfs_block(page);
  1105. struct shadow_tree *tree = &super->s_shadow_tree;
  1106. if (PagePrivate(page)) {
  1107. if (block->alias_map)
  1108. super->s_no_object_aliases -= bitmap_weight(
  1109. block->alias_map, LOGFS_BLOCK_FACTOR);
  1110. logfs_handle_transaction(inode, block->ta);
  1111. block->ops->free_block(inode->i_sb, block);
  1112. }
  1113. if (shadow) {
  1114. if (shadow->old_ofs)
  1115. btree_insert64(&tree->old, shadow->old_ofs, shadow,
  1116. GFP_NOFS);
  1117. else
  1118. btree_insert64(&tree->new, shadow->new_ofs, shadow,
  1119. GFP_NOFS);
  1120. super->s_dirty_used_bytes += shadow->new_len;
  1121. super->s_dirty_free_bytes += shadow->old_len;
  1122. mark_segment(tree, shadow->old_ofs >> super->s_segshift);
  1123. mark_segment(tree, shadow->new_ofs >> super->s_segshift);
  1124. }
  1125. }
  1126. static void logfs_set_alias(struct super_block *sb, struct logfs_block *block,
  1127. long child_no)
  1128. {
  1129. struct logfs_super *super = logfs_super(sb);
  1130. if (block->inode && block->inode->i_ino == LOGFS_INO_MASTER) {
  1131. /* Aliases in the master inode are pointless. */
  1132. return;
  1133. }
  1134. if (!test_bit(child_no, block->alias_map)) {
  1135. set_bit(child_no, block->alias_map);
  1136. super->s_no_object_aliases++;
  1137. }
  1138. list_move_tail(&block->alias_list, &super->s_object_alias);
  1139. }
  1140. /*
  1141. * Object aliases can and often do change the size and occupied space of a
  1142. * file. So not only do we have to change the pointers, we also have to
  1143. * change inode->i_size and li->li_used_bytes. Which is done by setting
  1144. * another two object aliases for the inode itself.
  1145. */
  1146. static void set_iused(struct inode *inode, struct logfs_shadow *shadow)
  1147. {
  1148. struct logfs_inode *li = logfs_inode(inode);
  1149. if (shadow->new_len == shadow->old_len)
  1150. return;
  1151. alloc_inode_block(inode);
  1152. li->li_used_bytes += shadow->new_len - shadow->old_len;
  1153. __logfs_set_blocks(inode);
  1154. logfs_set_alias(inode->i_sb, li->li_block, INODE_USED_OFS);
  1155. logfs_set_alias(inode->i_sb, li->li_block, INODE_SIZE_OFS);
  1156. }
  1157. static int logfs_write_i0(struct inode *inode, struct page *page,
  1158. struct write_control *wc)
  1159. {
  1160. struct logfs_shadow *shadow;
  1161. u64 bix;
  1162. level_t level;
  1163. int full, err = 0;
  1164. logfs_unpack_index(page->index, &bix, &level);
  1165. if (wc->ofs == 0)
  1166. if (logfs_reserve_blocks(inode, 1))
  1167. return -ENOSPC;
  1168. shadow = alloc_shadow(inode, bix, level, wc->ofs);
  1169. if (wc->flags & WF_WRITE)
  1170. err = logfs_segment_write(inode, page, shadow);
  1171. if (wc->flags & WF_DELETE)
  1172. logfs_segment_delete(inode, shadow);
  1173. if (err) {
  1174. free_shadow(inode, shadow);
  1175. return err;
  1176. }
  1177. set_iused(inode, shadow);
  1178. full = 1;
  1179. if (level != 0) {
  1180. alloc_indirect_block(inode, page, 0);
  1181. full = logfs_block(page)->full == LOGFS_BLOCK_FACTOR;
  1182. }
  1183. fill_shadow_tree(inode, page, shadow);
  1184. wc->ofs = shadow->new_ofs;
  1185. if (wc->ofs && full)
  1186. wc->ofs |= LOGFS_FULLY_POPULATED;
  1187. return 0;
  1188. }
  1189. static int logfs_write_direct(struct inode *inode, struct page *page,
  1190. long flags)
  1191. {
  1192. struct logfs_inode *li = logfs_inode(inode);
  1193. struct write_control wc = {
  1194. .ofs = li->li_data[page->index],
  1195. .flags = flags,
  1196. };
  1197. int err;
  1198. alloc_inode_block(inode);
  1199. err = logfs_write_i0(inode, page, &wc);
  1200. if (err)
  1201. return err;
  1202. li->li_data[page->index] = wc.ofs;
  1203. logfs_set_alias(inode->i_sb, li->li_block,
  1204. page->index + INODE_POINTER_OFS);
  1205. return 0;
  1206. }
  1207. static int ptr_change(u64 ofs, struct page *page)
  1208. {
  1209. struct logfs_block *block = logfs_block(page);
  1210. int empty0, empty1, full0, full1;
  1211. empty0 = ofs == 0;
  1212. empty1 = block->partial == 0;
  1213. if (empty0 != empty1)
  1214. return 1;
  1215. /* The !! is necessary to shrink result to int */
  1216. full0 = !!(ofs & LOGFS_FULLY_POPULATED);
  1217. full1 = block->full == LOGFS_BLOCK_FACTOR;
  1218. if (full0 != full1)
  1219. return 1;
  1220. return 0;
  1221. }
  1222. static int __logfs_write_rec(struct inode *inode, struct page *page,
  1223. struct write_control *this_wc,
  1224. pgoff_t bix, level_t target_level, level_t level)
  1225. {
  1226. int ret, page_empty = 0;
  1227. int child_no = get_bits(bix, SUBLEVEL(level));
  1228. struct page *ipage;
  1229. struct write_control child_wc = {
  1230. .flags = this_wc->flags,
  1231. };
  1232. ipage = logfs_get_write_page(inode, bix, level);
  1233. if (!ipage)
  1234. return -ENOMEM;
  1235. if (this_wc->ofs) {
  1236. ret = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
  1237. if (ret)
  1238. goto out;
  1239. } else if (!PageUptodate(ipage)) {
  1240. page_empty = 1;
  1241. logfs_read_empty(ipage);
  1242. }
  1243. child_wc.ofs = block_get_pointer(ipage, child_no);
  1244. if ((__force u8)level-1 > (__force u8)target_level)
  1245. ret = __logfs_write_rec(inode, page, &child_wc, bix,
  1246. target_level, SUBLEVEL(level));
  1247. else
  1248. ret = logfs_write_i0(inode, page, &child_wc);
  1249. if (ret)
  1250. goto out;
  1251. alloc_indirect_block(inode, ipage, page_empty);
  1252. block_set_pointer(ipage, child_no, child_wc.ofs);
  1253. /* FIXME: first condition seems superfluous */
  1254. if (child_wc.ofs || logfs_block(ipage)->partial)
  1255. this_wc->flags |= WF_WRITE;
  1256. /* the condition on this_wc->ofs ensures that we won't consume extra
  1257. * space for indirect blocks in the future, which we cannot reserve */
  1258. if (!this_wc->ofs || ptr_change(this_wc->ofs, ipage))
  1259. ret = logfs_write_i0(inode, ipage, this_wc);
  1260. else
  1261. logfs_set_alias(inode->i_sb, logfs_block(ipage), child_no);
  1262. out:
  1263. logfs_put_write_page(ipage);
  1264. return ret;
  1265. }
  1266. static int logfs_write_rec(struct inode *inode, struct page *page,
  1267. pgoff_t bix, level_t target_level, long flags)
  1268. {
  1269. struct logfs_inode *li = logfs_inode(inode);
  1270. struct write_control wc = {
  1271. .ofs = li->li_data[INDIRECT_INDEX],
  1272. .flags = flags,
  1273. };
  1274. int ret;
  1275. alloc_inode_block(inode);
  1276. if (li->li_height > (__force u8)target_level)
  1277. ret = __logfs_write_rec(inode, page, &wc, bix, target_level,
  1278. LEVEL(li->li_height));
  1279. else
  1280. ret = logfs_write_i0(inode, page, &wc);
  1281. if (ret)
  1282. return ret;
  1283. if (li->li_data[INDIRECT_INDEX] != wc.ofs) {
  1284. li->li_data[INDIRECT_INDEX] = wc.ofs;
  1285. logfs_set_alias(inode->i_sb, li->li_block,
  1286. INDIRECT_INDEX + INODE_POINTER_OFS);
  1287. }
  1288. return ret;
  1289. }
  1290. void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta)
  1291. {
  1292. alloc_inode_block(inode);
  1293. logfs_inode(inode)->li_block->ta = ta;
  1294. }
  1295. void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta)
  1296. {
  1297. struct logfs_block *block = logfs_inode(inode)->li_block;
  1298. if (block && block->ta)
  1299. block->ta = NULL;
  1300. }
  1301. static int grow_inode(struct inode *inode, u64 bix, level_t level)
  1302. {
  1303. struct logfs_inode *li = logfs_inode(inode);
  1304. u8 height = (__force u8)level;
  1305. struct page *page;
  1306. struct write_control wc = {
  1307. .flags = WF_WRITE,
  1308. };
  1309. int err;
  1310. BUG_ON(height > 5 || li->li_height > 5);
  1311. while (height > li->li_height || bix >= maxbix(li->li_height)) {
  1312. page = logfs_get_write_page(inode, I0_BLOCKS + 1,
  1313. LEVEL(li->li_height + 1));
  1314. if (!page)
  1315. return -ENOMEM;
  1316. logfs_read_empty(page);
  1317. alloc_indirect_block(inode, page, 1);
  1318. block_set_pointer(page, 0, li->li_data[INDIRECT_INDEX]);
  1319. err = logfs_write_i0(inode, page, &wc);
  1320. logfs_put_write_page(page);
  1321. if (err)
  1322. return err;
  1323. li->li_data[INDIRECT_INDEX] = wc.ofs;
  1324. wc.ofs = 0;
  1325. li->li_height++;
  1326. logfs_set_alias(inode->i_sb, li->li_block, INODE_HEIGHT_OFS);
  1327. }
  1328. return 0;
  1329. }
  1330. static int __logfs_write_buf(struct inode *inode, struct page *page, long flags)
  1331. {
  1332. struct logfs_super *super = logfs_super(inode->i_sb);
  1333. pgoff_t index = page->index;
  1334. u64 bix;
  1335. level_t level;
  1336. int err;
  1337. flags |= WF_WRITE | WF_DELETE;
  1338. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1339. logfs_unpack_index(index, &bix, &level);
  1340. if (logfs_block(page) && logfs_block(page)->reserved_bytes)
  1341. super->s_dirty_pages -= logfs_block(page)->reserved_bytes;
  1342. if (index < I0_BLOCKS)
  1343. return logfs_write_direct(inode, page, flags);
  1344. bix = adjust_bix(bix, level);
  1345. err = grow_inode(inode, bix, level);
  1346. if (err)
  1347. return err;
  1348. return logfs_write_rec(inode, page, bix, level, flags);
  1349. }
  1350. int logfs_write_buf(struct inode *inode, struct page *page, long flags)
  1351. {
  1352. struct super_block *sb = inode->i_sb;
  1353. int ret;
  1354. logfs_get_wblocks(sb, page, flags & WF_LOCK);
  1355. ret = __logfs_write_buf(inode, page, flags);
  1356. logfs_put_wblocks(sb, page, flags & WF_LOCK);
  1357. return ret;
  1358. }
  1359. static int __logfs_delete(struct inode *inode, struct page *page)
  1360. {
  1361. long flags = WF_DELETE;
  1362. int err;
  1363. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1364. if (page->index < I0_BLOCKS)
  1365. return logfs_write_direct(inode, page, flags);
  1366. err = grow_inode(inode, page->index, 0);
  1367. if (err)
  1368. return err;
  1369. return logfs_write_rec(inode, page, page->index, 0, flags);
  1370. }
  1371. int logfs_delete(struct inode *inode, pgoff_t index,
  1372. struct shadow_tree *shadow_tree)
  1373. {
  1374. struct super_block *sb = inode->i_sb;
  1375. struct page *page;
  1376. int ret;
  1377. page = logfs_get_read_page(inode, index, 0);
  1378. if (!page)
  1379. return -ENOMEM;
  1380. logfs_get_wblocks(sb, page, 1);
  1381. ret = __logfs_delete(inode, page);
  1382. logfs_put_wblocks(sb, page, 1);
  1383. logfs_put_read_page(page);
  1384. return ret;
  1385. }
  1386. int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
  1387. gc_level_t gc_level, long flags)
  1388. {
  1389. level_t level = shrink_level(gc_level);
  1390. struct page *page;
  1391. int err;
  1392. page = logfs_get_write_page(inode, bix, level);
  1393. if (!page)
  1394. return -ENOMEM;
  1395. err = logfs_segment_read(inode, page, ofs, bix, level);
  1396. if (!err) {
  1397. if (level != 0)
  1398. alloc_indirect_block(inode, page, 0);
  1399. err = logfs_write_buf(inode, page, flags);
  1400. if (!err && shrink_level(gc_level) == 0) {
  1401. /* Rewrite cannot mark the inode dirty but has to
  1402. * write it immediately.
  1403. * Q: Can't we just create an alias for the inode
  1404. * instead? And if not, why not?
  1405. */
  1406. if (inode->i_ino == LOGFS_INO_MASTER)
  1407. logfs_write_anchor(inode->i_sb);
  1408. else {
  1409. err = __logfs_write_inode(inode, page, flags);
  1410. }
  1411. }
  1412. }
  1413. logfs_put_write_page(page);
  1414. return err;
  1415. }
  1416. static int truncate_data_block(struct inode *inode, struct page *page,
  1417. u64 ofs, struct logfs_shadow *shadow, u64 size)
  1418. {
  1419. loff_t pageofs = page->index << inode->i_sb->s_blocksize_bits;
  1420. u64 bix;
  1421. level_t level;
  1422. int err;
  1423. /* Does truncation happen within this page? */
  1424. if (size <= pageofs || size - pageofs >= PAGE_SIZE)
  1425. return 0;
  1426. logfs_unpack_index(page->index, &bix, &level);
  1427. BUG_ON(level != 0);
  1428. err = logfs_segment_read(inode, page, ofs, bix, level);
  1429. if (err)
  1430. return err;
  1431. zero_user_segment(page, size - pageofs, PAGE_CACHE_SIZE);
  1432. return logfs_segment_write(inode, page, shadow);
  1433. }
  1434. static int logfs_truncate_i0(struct inode *inode, struct page *page,
  1435. struct write_control *wc, u64 size)
  1436. {
  1437. struct logfs_shadow *shadow;
  1438. u64 bix;
  1439. level_t level;
  1440. int err = 0;
  1441. logfs_unpack_index(page->index, &bix, &level);
  1442. BUG_ON(level != 0);
  1443. shadow = alloc_shadow(inode, bix, level, wc->ofs);
  1444. err = truncate_data_block(inode, page, wc->ofs, shadow, size);
  1445. if (err) {
  1446. free_shadow(inode, shadow);
  1447. return err;
  1448. }
  1449. logfs_segment_delete(inode, shadow);
  1450. set_iused(inode, shadow);
  1451. fill_shadow_tree(inode, page, shadow);
  1452. wc->ofs = shadow->new_ofs;
  1453. return 0;
  1454. }
  1455. static int logfs_truncate_direct(struct inode *inode, u64 size)
  1456. {
  1457. struct logfs_inode *li = logfs_inode(inode);
  1458. struct write_control wc;
  1459. struct page *page;
  1460. int e;
  1461. int err;
  1462. alloc_inode_block(inode);
  1463. for (e = I0_BLOCKS - 1; e >= 0; e--) {
  1464. if (size > (e+1) * LOGFS_BLOCKSIZE)
  1465. break;
  1466. wc.ofs = li->li_data[e];
  1467. if (!wc.ofs)
  1468. continue;
  1469. page = logfs_get_write_page(inode, e, 0);
  1470. if (!page)
  1471. return -ENOMEM;
  1472. err = logfs_segment_read(inode, page, wc.ofs, e, 0);
  1473. if (err) {
  1474. logfs_put_write_page(page);
  1475. return err;
  1476. }
  1477. err = logfs_truncate_i0(inode, page, &wc, size);
  1478. logfs_put_write_page(page);
  1479. if (err)
  1480. return err;
  1481. li->li_data[e] = wc.ofs;
  1482. }
  1483. return 0;
  1484. }
  1485. /* FIXME: these need to become per-sb once we support different blocksizes */
  1486. static u64 __logfs_step[] = {
  1487. 1,
  1488. I1_BLOCKS,
  1489. I2_BLOCKS,
  1490. I3_BLOCKS,
  1491. };
  1492. static u64 __logfs_start_index[] = {
  1493. I0_BLOCKS,
  1494. I1_BLOCKS,
  1495. I2_BLOCKS,
  1496. I3_BLOCKS
  1497. };
  1498. static inline u64 logfs_step(level_t level)
  1499. {
  1500. return __logfs_step[(__force u8)level];
  1501. }
  1502. static inline u64 logfs_factor(u8 level)
  1503. {
  1504. return __logfs_step[level] * LOGFS_BLOCKSIZE;
  1505. }
  1506. static inline u64 logfs_start_index(level_t level)
  1507. {
  1508. return __logfs_start_index[(__force u8)level];
  1509. }
  1510. static void logfs_unpack_raw_index(pgoff_t index, u64 *bix, level_t *level)
  1511. {
  1512. logfs_unpack_index(index, bix, level);
  1513. if (*bix <= logfs_start_index(SUBLEVEL(*level)))
  1514. *bix = 0;
  1515. }
  1516. static int __logfs_truncate_rec(struct inode *inode, struct page *ipage,
  1517. struct write_control *this_wc, u64 size)
  1518. {
  1519. int truncate_happened = 0;
  1520. int e, err = 0;
  1521. u64 bix, child_bix, next_bix;
  1522. level_t level;
  1523. struct page *page;
  1524. struct write_control child_wc = { /* FIXME: flags */ };
  1525. logfs_unpack_raw_index(ipage->index, &bix, &level);
  1526. err = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
  1527. if (err)
  1528. return err;
  1529. for (e = LOGFS_BLOCK_FACTOR - 1; e >= 0; e--) {
  1530. child_bix = bix + e * logfs_step(SUBLEVEL(level));
  1531. next_bix = child_bix + logfs_step(SUBLEVEL(level));
  1532. if (size > next_bix * LOGFS_BLOCKSIZE)
  1533. break;
  1534. child_wc.ofs = pure_ofs(block_get_pointer(ipage, e));
  1535. if (!child_wc.ofs)
  1536. continue;
  1537. page = logfs_get_write_page(inode, child_bix, SUBLEVEL(level));
  1538. if (!page)
  1539. return -ENOMEM;
  1540. if ((__force u8)level > 1)
  1541. err = __logfs_truncate_rec(inode, page, &child_wc, size);
  1542. else
  1543. err = logfs_truncate_i0(inode, page, &child_wc, size);
  1544. logfs_put_write_page(page);
  1545. if (err)
  1546. return err;
  1547. truncate_happened = 1;
  1548. alloc_indirect_block(inode, ipage, 0);
  1549. block_set_pointer(ipage, e, child_wc.ofs);
  1550. }
  1551. if (!truncate_happened) {
  1552. printk("ineffectual truncate (%lx, %lx, %llx)\n", inode->i_ino, ipage->index, size);
  1553. return 0;
  1554. }
  1555. this_wc->flags = WF_DELETE;
  1556. if (logfs_block(ipage)->partial)
  1557. this_wc->flags |= WF_WRITE;
  1558. return logfs_write_i0(inode, ipage, this_wc);
  1559. }
  1560. static int logfs_truncate_rec(struct inode *inode, u64 size)
  1561. {
  1562. struct logfs_inode *li = logfs_inode(inode);
  1563. struct write_control wc = {
  1564. .ofs = li->li_data[INDIRECT_INDEX],
  1565. };
  1566. struct page *page;
  1567. int err;
  1568. alloc_inode_block(inode);
  1569. if (!wc.ofs)
  1570. return 0;
  1571. page = logfs_get_write_page(inode, 0, LEVEL(li->li_height));
  1572. if (!page)
  1573. return -ENOMEM;
  1574. err = __logfs_truncate_rec(inode, page, &wc, size);
  1575. logfs_put_write_page(page);
  1576. if (err)
  1577. return err;
  1578. if (li->li_data[INDIRECT_INDEX] != wc.ofs)
  1579. li->li_data[INDIRECT_INDEX] = wc.ofs;
  1580. return 0;
  1581. }
  1582. static int __logfs_truncate(struct inode *inode, u64 size)
  1583. {
  1584. int ret;
  1585. if (size >= logfs_factor(logfs_inode(inode)->li_height))
  1586. return 0;
  1587. ret = logfs_truncate_rec(inode, size);
  1588. if (ret)
  1589. return ret;
  1590. return logfs_truncate_direct(inode, size);
  1591. }
  1592. /*
  1593. * Truncate, by changing the segment file, can consume a fair amount
  1594. * of resources. So back off from time to time and do some GC.
  1595. * 8 or 2048 blocks should be well within safety limits even if
  1596. * every single block resided in a different segment.
  1597. */
  1598. #define TRUNCATE_STEP (8 * 1024 * 1024)
  1599. int logfs_truncate(struct inode *inode, u64 target)
  1600. {
  1601. struct super_block *sb = inode->i_sb;
  1602. u64 size = i_size_read(inode);
  1603. int err = 0;
  1604. size = ALIGN(size, TRUNCATE_STEP);
  1605. while (size > target) {
  1606. if (size > TRUNCATE_STEP)
  1607. size -= TRUNCATE_STEP;
  1608. else
  1609. size = 0;
  1610. if (size < target)
  1611. size = target;
  1612. logfs_get_wblocks(sb, NULL, 1);
  1613. err = __logfs_truncate(inode, size);
  1614. if (!err)
  1615. err = __logfs_write_inode(inode, NULL, 0);
  1616. logfs_put_wblocks(sb, NULL, 1);
  1617. }
  1618. if (!err) {
  1619. err = inode_newsize_ok(inode, target);
  1620. if (err)
  1621. goto out;
  1622. truncate_setsize(inode, target);
  1623. }
  1624. out:
  1625. /* I don't trust error recovery yet. */
  1626. WARN_ON(err);
  1627. return err;
  1628. }
  1629. static void move_page_to_inode(struct inode *inode, struct page *page)
  1630. {
  1631. struct logfs_inode *li = logfs_inode(inode);
  1632. struct logfs_block *block = logfs_block(page);
  1633. if (!block)
  1634. return;
  1635. log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
  1636. block->ino, block->bix, block->level);
  1637. BUG_ON(li->li_block);
  1638. block->ops = &inode_block_ops;
  1639. block->inode = inode;
  1640. li->li_block = block;
  1641. block->page = NULL;
  1642. if (PagePrivate(page)) {
  1643. ClearPagePrivate(page);
  1644. page_cache_release(page);
  1645. set_page_private(page, 0);
  1646. }
  1647. }
  1648. static void move_inode_to_page(struct page *page, struct inode *inode)
  1649. {
  1650. struct logfs_inode *li = logfs_inode(inode);
  1651. struct logfs_block *block = li->li_block;
  1652. if (!block)
  1653. return;
  1654. log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
  1655. block->ino, block->bix, block->level);
  1656. BUG_ON(PagePrivate(page));
  1657. block->ops = &indirect_block_ops;
  1658. block->page = page;
  1659. if (!PagePrivate(page)) {
  1660. SetPagePrivate(page);
  1661. page_cache_get(page);
  1662. set_page_private(page, (unsigned long) block);
  1663. }
  1664. block->inode = NULL;
  1665. li->li_block = NULL;
  1666. }
  1667. int logfs_read_inode(struct inode *inode)
  1668. {
  1669. struct super_block *sb = inode->i_sb;
  1670. struct logfs_super *super = logfs_super(sb);
  1671. struct inode *master_inode = super->s_master_inode;
  1672. struct page *page;
  1673. struct logfs_disk_inode *di;
  1674. u64 ino = inode->i_ino;
  1675. if (ino << sb->s_blocksize_bits > i_size_read(master_inode))
  1676. return -ENODATA;
  1677. if (!logfs_exist_block(master_inode, ino))
  1678. return -ENODATA;
  1679. page = read_cache_page(master_inode->i_mapping, ino,
  1680. (filler_t *)logfs_readpage, NULL);
  1681. if (IS_ERR(page))
  1682. return PTR_ERR(page);
  1683. di = kmap_atomic(page);
  1684. logfs_disk_to_inode(di, inode);
  1685. kunmap_atomic(di);
  1686. move_page_to_inode(inode, page);
  1687. page_cache_release(page);
  1688. return 0;
  1689. }
  1690. /* Caller must logfs_put_write_page(page); */
  1691. static struct page *inode_to_page(struct inode *inode)
  1692. {
  1693. struct inode *master_inode = logfs_super(inode->i_sb)->s_master_inode;
  1694. struct logfs_disk_inode *di;
  1695. struct page *page;
  1696. BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
  1697. page = logfs_get_write_page(master_inode, inode->i_ino, 0);
  1698. if (!page)
  1699. return NULL;
  1700. di = kmap_atomic(page);
  1701. logfs_inode_to_disk(inode, di);
  1702. kunmap_atomic(di);
  1703. move_inode_to_page(page, inode);
  1704. return page;
  1705. }
  1706. static int do_write_inode(struct inode *inode)
  1707. {
  1708. struct super_block *sb = inode->i_sb;
  1709. struct inode *master_inode = logfs_super(sb)->s_master_inode;
  1710. loff_t size = (inode->i_ino + 1) << inode->i_sb->s_blocksize_bits;
  1711. struct page *page;
  1712. int err;
  1713. BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
  1714. /* FIXME: lock inode */
  1715. if (i_size_read(master_inode) < size)
  1716. i_size_write(master_inode, size);
  1717. /* TODO: Tell vfs this inode is clean now */
  1718. page = inode_to_page(inode);
  1719. if (!page)
  1720. return -ENOMEM;
  1721. /* FIXME: transaction is part of logfs_block now. Is that enough? */
  1722. err = logfs_write_buf(master_inode, page, 0);
  1723. if (err)
  1724. move_page_to_inode(inode, page);
  1725. logfs_put_write_page(page);
  1726. return err;
  1727. }
  1728. static void logfs_mod_segment_entry(struct super_block *sb, u32 segno,
  1729. int write,
  1730. void (*change_se)(struct logfs_segment_entry *, long),
  1731. long arg)
  1732. {
  1733. struct logfs_super *super = logfs_super(sb);
  1734. struct inode *inode;
  1735. struct page *page;
  1736. struct logfs_segment_entry *se;
  1737. pgoff_t page_no;
  1738. int child_no;
  1739. page_no = segno >> (sb->s_blocksize_bits - 3);
  1740. child_no = segno & ((sb->s_blocksize >> 3) - 1);
  1741. inode = super->s_segfile_inode;
  1742. page = logfs_get_write_page(inode, page_no, 0);
  1743. BUG_ON(!page); /* FIXME: We need some reserve page for this case */
  1744. if (!PageUptodate(page))
  1745. logfs_read_block(inode, page, WRITE);
  1746. if (write)
  1747. alloc_indirect_block(inode, page, 0);
  1748. se = kmap_atomic(page);
  1749. change_se(se + child_no, arg);
  1750. if (write) {
  1751. logfs_set_alias(sb, logfs_block(page), child_no);
  1752. BUG_ON((int)be32_to_cpu(se[child_no].valid) > super->s_segsize);
  1753. }
  1754. kunmap_atomic(se);
  1755. logfs_put_write_page(page);
  1756. }
  1757. static void __get_segment_entry(struct logfs_segment_entry *se, long _target)
  1758. {
  1759. struct logfs_segment_entry *target = (void *)_target;
  1760. *target = *se;
  1761. }
  1762. void logfs_get_segment_entry(struct super_block *sb, u32 segno,
  1763. struct logfs_segment_entry *se)
  1764. {
  1765. logfs_mod_segment_entry(sb, segno, 0, __get_segment_entry, (long)se);
  1766. }
  1767. static void __set_segment_used(struct logfs_segment_entry *se, long increment)
  1768. {
  1769. u32 valid;
  1770. valid = be32_to_cpu(se->valid);
  1771. valid += increment;
  1772. se->valid = cpu_to_be32(valid);
  1773. }
  1774. void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment)
  1775. {
  1776. struct logfs_super *super = logfs_super(sb);
  1777. u32 segno = ofs >> super->s_segshift;
  1778. if (!increment)
  1779. return;
  1780. logfs_mod_segment_entry(sb, segno, 1, __set_segment_used, increment);
  1781. }
  1782. static void __set_segment_erased(struct logfs_segment_entry *se, long ec_level)
  1783. {
  1784. se->ec_level = cpu_to_be32(ec_level);
  1785. }
  1786. void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
  1787. gc_level_t gc_level)
  1788. {
  1789. u32 ec_level = ec << 4 | (__force u8)gc_level;
  1790. logfs_mod_segment_entry(sb, segno, 1, __set_segment_erased, ec_level);
  1791. }
  1792. static void __set_segment_reserved(struct logfs_segment_entry *se, long ignore)
  1793. {
  1794. se->valid = cpu_to_be32(RESERVED);
  1795. }
  1796. void logfs_set_segment_reserved(struct super_block *sb, u32 segno)
  1797. {
  1798. logfs_mod_segment_entry(sb, segno, 1, __set_segment_reserved, 0);
  1799. }
  1800. static void __set_segment_unreserved(struct logfs_segment_entry *se,
  1801. long ec_level)
  1802. {
  1803. se->valid = 0;
  1804. se->ec_level = cpu_to_be32(ec_level);
  1805. }
  1806. void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec)
  1807. {
  1808. u32 ec_level = ec << 4;
  1809. logfs_mod_segment_entry(sb, segno, 1, __set_segment_unreserved,
  1810. ec_level);
  1811. }
  1812. int __logfs_write_inode(struct inode *inode, struct page *page, long flags)
  1813. {
  1814. struct super_block *sb = inode->i_sb;
  1815. int ret;
  1816. logfs_get_wblocks(sb, page, flags & WF_LOCK);
  1817. ret = do_write_inode(inode);
  1818. logfs_put_wblocks(sb, page, flags & WF_LOCK);
  1819. return ret;
  1820. }
  1821. static int do_delete_inode(struct inode *inode)
  1822. {
  1823. struct super_block *sb = inode->i_sb;
  1824. struct inode *master_inode = logfs_super(sb)->s_master_inode;
  1825. struct page *page;
  1826. int ret;
  1827. page = logfs_get_write_page(master_inode, inode->i_ino, 0);
  1828. if (!page)
  1829. return -ENOMEM;
  1830. move_inode_to_page(page, inode);
  1831. logfs_get_wblocks(sb, page, 1);
  1832. ret = __logfs_delete(master_inode, page);
  1833. logfs_put_wblocks(sb, page, 1);
  1834. logfs_put_write_page(page);
  1835. return ret;
  1836. }
  1837. /*
  1838. * ZOMBIE inodes have already been deleted before and should remain dead,
  1839. * if it weren't for valid checking. No need to kill them again here.
  1840. */
  1841. void logfs_evict_inode(struct inode *inode)
  1842. {
  1843. struct super_block *sb = inode->i_sb;
  1844. struct logfs_inode *li = logfs_inode(inode);
  1845. struct logfs_block *block = li->li_block;
  1846. struct page *page;
  1847. if (!inode->i_nlink) {
  1848. if (!(li->li_flags & LOGFS_IF_ZOMBIE)) {
  1849. li->li_flags |= LOGFS_IF_ZOMBIE;
  1850. if (i_size_read(inode) > 0)
  1851. logfs_truncate(inode, 0);
  1852. do_delete_inode(inode);
  1853. }
  1854. }
  1855. truncate_inode_pages_final(&inode->i_data);
  1856. clear_inode(inode);
  1857. /* Cheaper version of write_inode. All changes are concealed in
  1858. * aliases, which are moved back. No write to the medium happens.
  1859. */
  1860. /* Only deleted files may be dirty at this point */
  1861. BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink);
  1862. if (!block)
  1863. return;
  1864. if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) {
  1865. block->ops->free_block(inode->i_sb, block);
  1866. return;
  1867. }
  1868. page = inode_to_page(inode);
  1869. BUG_ON(!page); /* FIXME: Use emergency page */
  1870. logfs_put_write_page(page);
  1871. }
  1872. void btree_write_block(struct logfs_block *block)
  1873. {
  1874. struct inode *inode;
  1875. struct page *page;
  1876. int err, cookie;
  1877. inode = logfs_safe_iget(block->sb, block->ino, &cookie);
  1878. page = logfs_get_write_page(inode, block->bix, block->level);
  1879. err = logfs_readpage_nolock(page);
  1880. BUG_ON(err);
  1881. BUG_ON(!PagePrivate(page));
  1882. BUG_ON(logfs_block(page) != block);
  1883. err = __logfs_write_buf(inode, page, 0);
  1884. BUG_ON(err);
  1885. BUG_ON(PagePrivate(page) || page->private);
  1886. logfs_put_write_page(page);
  1887. logfs_safe_iput(inode, cookie);
  1888. }
  1889. /**
  1890. * logfs_inode_write - write inode or dentry objects
  1891. *
  1892. * @inode: parent inode (ifile or directory)
  1893. * @buf: object to write (inode or dentry)
  1894. * @count: object size
  1895. * @bix: block index
  1896. * @flags: write flags
  1897. * @shadow_tree: shadow below this inode
  1898. *
  1899. * FIXME: All caller of this put a 200-300 byte variable on the stack,
  1900. * only to call here and do a memcpy from that stack variable. A good
  1901. * example of wasted performance and stack space.
  1902. */
  1903. int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
  1904. loff_t bix, long flags, struct shadow_tree *shadow_tree)
  1905. {
  1906. loff_t pos = bix << inode->i_sb->s_blocksize_bits;
  1907. int err;
  1908. struct page *page;
  1909. void *pagebuf;
  1910. BUG_ON(pos & (LOGFS_BLOCKSIZE-1));
  1911. BUG_ON(count > LOGFS_BLOCKSIZE);
  1912. page = logfs_get_write_page(inode, bix, 0);
  1913. if (!page)
  1914. return -ENOMEM;
  1915. pagebuf = kmap_atomic(page);
  1916. memcpy(pagebuf, buf, count);
  1917. flush_dcache_page(page);
  1918. kunmap_atomic(pagebuf);
  1919. if (i_size_read(inode) < pos + LOGFS_BLOCKSIZE)
  1920. i_size_write(inode, pos + LOGFS_BLOCKSIZE);
  1921. err = logfs_write_buf(inode, page, flags);
  1922. logfs_put_write_page(page);
  1923. return err;
  1924. }
  1925. int logfs_open_segfile(struct super_block *sb)
  1926. {
  1927. struct logfs_super *super = logfs_super(sb);
  1928. struct inode *inode;
  1929. inode = logfs_read_meta_inode(sb, LOGFS_INO_SEGFILE);
  1930. if (IS_ERR(inode))
  1931. return PTR_ERR(inode);
  1932. super->s_segfile_inode = inode;
  1933. return 0;
  1934. }
  1935. int logfs_init_rw(struct super_block *sb)
  1936. {
  1937. struct logfs_super *super = logfs_super(sb);
  1938. int min_fill = 3 * super->s_no_blocks;
  1939. INIT_LIST_HEAD(&super->s_object_alias);
  1940. INIT_LIST_HEAD(&super->s_writeback_list);
  1941. mutex_init(&super->s_write_mutex);
  1942. super->s_block_pool = mempool_create_kmalloc_pool(min_fill,
  1943. sizeof(struct logfs_block));
  1944. super->s_shadow_pool = mempool_create_kmalloc_pool(min_fill,
  1945. sizeof(struct logfs_shadow));
  1946. return 0;
  1947. }
  1948. void logfs_cleanup_rw(struct super_block *sb)
  1949. {
  1950. struct logfs_super *super = logfs_super(sb);
  1951. logfs_mempool_destroy(super->s_block_pool);
  1952. logfs_mempool_destroy(super->s_shadow_pool);
  1953. }