readwrite.c 56 KB

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