inode.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <ooo@electrozaur.com>
  6. *
  7. * Copyrights for code taken from ext2:
  8. * Copyright (C) 1992, 1993, 1994, 1995
  9. * Remy Card (card@masi.ibp.fr)
  10. * Laboratoire MASI - Institut Blaise Pascal
  11. * Universite Pierre et Marie Curie (Paris VI)
  12. * from
  13. * linux/fs/minix/inode.c
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * This file is part of exofs.
  17. *
  18. * exofs is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation. Since it is based on ext2, and the only
  21. * valid version of GPL for the Linux kernel is version 2, the only valid
  22. * version of GPL for exofs is version 2.
  23. *
  24. * exofs is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with exofs; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  32. */
  33. #include <linux/slab.h>
  34. #include "exofs.h"
  35. #define EXOFS_DBGMSG2(M...) do {} while (0)
  36. unsigned exofs_max_io_pages(struct ore_layout *layout,
  37. unsigned expected_pages)
  38. {
  39. unsigned pages = min_t(unsigned, expected_pages,
  40. layout->max_io_length / PAGE_SIZE);
  41. return pages;
  42. }
  43. struct page_collect {
  44. struct exofs_sb_info *sbi;
  45. struct inode *inode;
  46. unsigned expected_pages;
  47. struct ore_io_state *ios;
  48. struct page **pages;
  49. unsigned alloc_pages;
  50. unsigned nr_pages;
  51. unsigned long length;
  52. loff_t pg_first; /* keep 64bit also in 32-arches */
  53. bool read_4_write; /* This means two things: that the read is sync
  54. * And the pages should not be unlocked.
  55. */
  56. struct page *that_locked_page;
  57. };
  58. static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
  59. struct inode *inode)
  60. {
  61. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  62. pcol->sbi = sbi;
  63. pcol->inode = inode;
  64. pcol->expected_pages = expected_pages;
  65. pcol->ios = NULL;
  66. pcol->pages = NULL;
  67. pcol->alloc_pages = 0;
  68. pcol->nr_pages = 0;
  69. pcol->length = 0;
  70. pcol->pg_first = -1;
  71. pcol->read_4_write = false;
  72. pcol->that_locked_page = NULL;
  73. }
  74. static void _pcol_reset(struct page_collect *pcol)
  75. {
  76. pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
  77. pcol->pages = NULL;
  78. pcol->alloc_pages = 0;
  79. pcol->nr_pages = 0;
  80. pcol->length = 0;
  81. pcol->pg_first = -1;
  82. pcol->ios = NULL;
  83. pcol->that_locked_page = NULL;
  84. /* this is probably the end of the loop but in writes
  85. * it might not end here. don't be left with nothing
  86. */
  87. if (!pcol->expected_pages)
  88. pcol->expected_pages =
  89. exofs_max_io_pages(&pcol->sbi->layout, ~0);
  90. }
  91. static int pcol_try_alloc(struct page_collect *pcol)
  92. {
  93. unsigned pages;
  94. /* TODO: easily support bio chaining */
  95. pages = exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages);
  96. for (; pages; pages >>= 1) {
  97. pcol->pages = kmalloc(pages * sizeof(struct page *),
  98. GFP_KERNEL);
  99. if (likely(pcol->pages)) {
  100. pcol->alloc_pages = pages;
  101. return 0;
  102. }
  103. }
  104. EXOFS_ERR("Failed to kmalloc expected_pages=%u\n",
  105. pcol->expected_pages);
  106. return -ENOMEM;
  107. }
  108. static void pcol_free(struct page_collect *pcol)
  109. {
  110. kfree(pcol->pages);
  111. pcol->pages = NULL;
  112. if (pcol->ios) {
  113. ore_put_io_state(pcol->ios);
  114. pcol->ios = NULL;
  115. }
  116. }
  117. static int pcol_add_page(struct page_collect *pcol, struct page *page,
  118. unsigned len)
  119. {
  120. if (unlikely(pcol->nr_pages >= pcol->alloc_pages))
  121. return -ENOMEM;
  122. pcol->pages[pcol->nr_pages++] = page;
  123. pcol->length += len;
  124. return 0;
  125. }
  126. enum {PAGE_WAS_NOT_IN_IO = 17};
  127. static int update_read_page(struct page *page, int ret)
  128. {
  129. switch (ret) {
  130. case 0:
  131. /* Everything is OK */
  132. SetPageUptodate(page);
  133. if (PageError(page))
  134. ClearPageError(page);
  135. break;
  136. case -EFAULT:
  137. /* In this case we were trying to read something that wasn't on
  138. * disk yet - return a page full of zeroes. This should be OK,
  139. * because the object should be empty (if there was a write
  140. * before this read, the read would be waiting with the page
  141. * locked */
  142. clear_highpage(page);
  143. SetPageUptodate(page);
  144. if (PageError(page))
  145. ClearPageError(page);
  146. EXOFS_DBGMSG("recovered read error\n");
  147. /* fall through */
  148. case PAGE_WAS_NOT_IN_IO:
  149. ret = 0; /* recovered error */
  150. break;
  151. default:
  152. SetPageError(page);
  153. }
  154. return ret;
  155. }
  156. static void update_write_page(struct page *page, int ret)
  157. {
  158. if (unlikely(ret == PAGE_WAS_NOT_IN_IO))
  159. return; /* don't pass start don't collect $200 */
  160. if (ret) {
  161. mapping_set_error(page->mapping, ret);
  162. SetPageError(page);
  163. }
  164. end_page_writeback(page);
  165. }
  166. /* Called at the end of reads, to optionally unlock pages and update their
  167. * status.
  168. */
  169. static int __readpages_done(struct page_collect *pcol)
  170. {
  171. int i;
  172. u64 good_bytes;
  173. u64 length = 0;
  174. int ret = ore_check_io(pcol->ios, NULL);
  175. if (likely(!ret)) {
  176. good_bytes = pcol->length;
  177. ret = PAGE_WAS_NOT_IN_IO;
  178. } else {
  179. good_bytes = 0;
  180. }
  181. EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
  182. " length=0x%lx nr_pages=%u\n",
  183. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  184. pcol->nr_pages);
  185. for (i = 0; i < pcol->nr_pages; i++) {
  186. struct page *page = pcol->pages[i];
  187. struct inode *inode = page->mapping->host;
  188. int page_stat;
  189. if (inode != pcol->inode)
  190. continue; /* osd might add more pages at end */
  191. if (likely(length < good_bytes))
  192. page_stat = 0;
  193. else
  194. page_stat = ret;
  195. EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n",
  196. inode->i_ino, page->index,
  197. page_stat ? "bad_bytes" : "good_bytes");
  198. ret = update_read_page(page, page_stat);
  199. if (!pcol->read_4_write)
  200. unlock_page(page);
  201. length += PAGE_SIZE;
  202. }
  203. pcol_free(pcol);
  204. EXOFS_DBGMSG2("readpages_done END\n");
  205. return ret;
  206. }
  207. /* callback of async reads */
  208. static void readpages_done(struct ore_io_state *ios, void *p)
  209. {
  210. struct page_collect *pcol = p;
  211. __readpages_done(pcol);
  212. atomic_dec(&pcol->sbi->s_curr_pending);
  213. kfree(pcol);
  214. }
  215. static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
  216. {
  217. int i;
  218. for (i = 0; i < pcol->nr_pages; i++) {
  219. struct page *page = pcol->pages[i];
  220. if (rw == READ)
  221. update_read_page(page, ret);
  222. else
  223. update_write_page(page, ret);
  224. unlock_page(page);
  225. }
  226. }
  227. static int _maybe_not_all_in_one_io(struct ore_io_state *ios,
  228. struct page_collect *pcol_src, struct page_collect *pcol)
  229. {
  230. /* length was wrong or offset was not page aligned */
  231. BUG_ON(pcol_src->nr_pages < ios->nr_pages);
  232. if (pcol_src->nr_pages > ios->nr_pages) {
  233. struct page **src_page;
  234. unsigned pages_less = pcol_src->nr_pages - ios->nr_pages;
  235. unsigned long len_less = pcol_src->length - ios->length;
  236. unsigned i;
  237. int ret;
  238. /* This IO was trimmed */
  239. pcol_src->nr_pages = ios->nr_pages;
  240. pcol_src->length = ios->length;
  241. /* Left over pages are passed to the next io */
  242. pcol->expected_pages += pages_less;
  243. pcol->nr_pages = pages_less;
  244. pcol->length = len_less;
  245. src_page = pcol_src->pages + pcol_src->nr_pages;
  246. pcol->pg_first = (*src_page)->index;
  247. ret = pcol_try_alloc(pcol);
  248. if (unlikely(ret))
  249. return ret;
  250. for (i = 0; i < pages_less; ++i)
  251. pcol->pages[i] = *src_page++;
  252. EXOFS_DBGMSG("Length was adjusted nr_pages=0x%x "
  253. "pages_less=0x%x expected_pages=0x%x "
  254. "next_offset=0x%llx next_len=0x%lx\n",
  255. pcol_src->nr_pages, pages_less, pcol->expected_pages,
  256. pcol->pg_first * PAGE_SIZE, pcol->length);
  257. }
  258. return 0;
  259. }
  260. static int read_exec(struct page_collect *pcol)
  261. {
  262. struct exofs_i_info *oi = exofs_i(pcol->inode);
  263. struct ore_io_state *ios;
  264. struct page_collect *pcol_copy = NULL;
  265. int ret;
  266. if (!pcol->pages)
  267. return 0;
  268. if (!pcol->ios) {
  269. int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true,
  270. pcol->pg_first << PAGE_SHIFT,
  271. pcol->length, &pcol->ios);
  272. if (ret)
  273. return ret;
  274. }
  275. ios = pcol->ios;
  276. ios->pages = pcol->pages;
  277. if (pcol->read_4_write) {
  278. ore_read(pcol->ios);
  279. return __readpages_done(pcol);
  280. }
  281. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  282. if (!pcol_copy) {
  283. ret = -ENOMEM;
  284. goto err;
  285. }
  286. *pcol_copy = *pcol;
  287. ios->done = readpages_done;
  288. ios->private = pcol_copy;
  289. /* pages ownership was passed to pcol_copy */
  290. _pcol_reset(pcol);
  291. ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
  292. if (unlikely(ret))
  293. goto err;
  294. EXOFS_DBGMSG2("read_exec(0x%lx) offset=0x%llx length=0x%llx\n",
  295. pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));
  296. ret = ore_read(ios);
  297. if (unlikely(ret))
  298. goto err;
  299. atomic_inc(&pcol->sbi->s_curr_pending);
  300. return 0;
  301. err:
  302. if (!pcol_copy) /* Failed before ownership transfer */
  303. pcol_copy = pcol;
  304. _unlock_pcol_pages(pcol_copy, ret, READ);
  305. pcol_free(pcol_copy);
  306. kfree(pcol_copy);
  307. return ret;
  308. }
  309. /* readpage_strip is called either directly from readpage() or by the VFS from
  310. * within read_cache_pages(), to add one more page to be read. It will try to
  311. * collect as many contiguous pages as posible. If a discontinuity is
  312. * encountered, or it runs out of resources, it will submit the previous segment
  313. * and will start a new collection. Eventually caller must submit the last
  314. * segment if present.
  315. */
  316. static int readpage_strip(void *data, struct page *page)
  317. {
  318. struct page_collect *pcol = data;
  319. struct inode *inode = pcol->inode;
  320. struct exofs_i_info *oi = exofs_i(inode);
  321. loff_t i_size = i_size_read(inode);
  322. pgoff_t end_index = i_size >> PAGE_SHIFT;
  323. size_t len;
  324. int ret;
  325. BUG_ON(!PageLocked(page));
  326. /* FIXME: Just for debugging, will be removed */
  327. if (PageUptodate(page))
  328. EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
  329. page->index);
  330. pcol->that_locked_page = page;
  331. if (page->index < end_index)
  332. len = PAGE_SIZE;
  333. else if (page->index == end_index)
  334. len = i_size & ~PAGE_MASK;
  335. else
  336. len = 0;
  337. if (!len || !obj_created(oi)) {
  338. /* this will be out of bounds, or doesn't exist yet.
  339. * Current page is cleared and the request is split
  340. */
  341. clear_highpage(page);
  342. SetPageUptodate(page);
  343. if (PageError(page))
  344. ClearPageError(page);
  345. if (!pcol->read_4_write)
  346. unlock_page(page);
  347. EXOFS_DBGMSG("readpage_strip(0x%lx) empty page len=%zx "
  348. "read_4_write=%d index=0x%lx end_index=0x%lx "
  349. "splitting\n", inode->i_ino, len,
  350. pcol->read_4_write, page->index, end_index);
  351. return read_exec(pcol);
  352. }
  353. try_again:
  354. if (unlikely(pcol->pg_first == -1)) {
  355. pcol->pg_first = page->index;
  356. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  357. page->index)) {
  358. /* Discontinuity detected, split the request */
  359. ret = read_exec(pcol);
  360. if (unlikely(ret))
  361. goto fail;
  362. goto try_again;
  363. }
  364. if (!pcol->pages) {
  365. ret = pcol_try_alloc(pcol);
  366. if (unlikely(ret))
  367. goto fail;
  368. }
  369. if (len != PAGE_SIZE)
  370. zero_user(page, len, PAGE_SIZE - len);
  371. EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  372. inode->i_ino, page->index, len);
  373. ret = pcol_add_page(pcol, page, len);
  374. if (ret) {
  375. EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
  376. "this_len=0x%zx nr_pages=%u length=0x%lx\n",
  377. page, len, pcol->nr_pages, pcol->length);
  378. /* split the request, and start again with current page */
  379. ret = read_exec(pcol);
  380. if (unlikely(ret))
  381. goto fail;
  382. goto try_again;
  383. }
  384. return 0;
  385. fail:
  386. /* SetPageError(page); ??? */
  387. unlock_page(page);
  388. return ret;
  389. }
  390. static int exofs_readpages(struct file *file, struct address_space *mapping,
  391. struct list_head *pages, unsigned nr_pages)
  392. {
  393. struct page_collect pcol;
  394. int ret;
  395. _pcol_init(&pcol, nr_pages, mapping->host);
  396. ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
  397. if (ret) {
  398. EXOFS_ERR("read_cache_pages => %d\n", ret);
  399. return ret;
  400. }
  401. ret = read_exec(&pcol);
  402. if (unlikely(ret))
  403. return ret;
  404. return read_exec(&pcol);
  405. }
  406. static int _readpage(struct page *page, bool read_4_write)
  407. {
  408. struct page_collect pcol;
  409. int ret;
  410. _pcol_init(&pcol, 1, page->mapping->host);
  411. pcol.read_4_write = read_4_write;
  412. ret = readpage_strip(&pcol, page);
  413. if (ret) {
  414. EXOFS_ERR("_readpage => %d\n", ret);
  415. return ret;
  416. }
  417. return read_exec(&pcol);
  418. }
  419. /*
  420. * We don't need the file
  421. */
  422. static int exofs_readpage(struct file *file, struct page *page)
  423. {
  424. return _readpage(page, false);
  425. }
  426. /* Callback for osd_write. All writes are asynchronous */
  427. static void writepages_done(struct ore_io_state *ios, void *p)
  428. {
  429. struct page_collect *pcol = p;
  430. int i;
  431. u64 good_bytes;
  432. u64 length = 0;
  433. int ret = ore_check_io(ios, NULL);
  434. atomic_dec(&pcol->sbi->s_curr_pending);
  435. if (likely(!ret)) {
  436. good_bytes = pcol->length;
  437. ret = PAGE_WAS_NOT_IN_IO;
  438. } else {
  439. good_bytes = 0;
  440. }
  441. EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
  442. " length=0x%lx nr_pages=%u\n",
  443. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  444. pcol->nr_pages);
  445. for (i = 0; i < pcol->nr_pages; i++) {
  446. struct page *page = pcol->pages[i];
  447. struct inode *inode = page->mapping->host;
  448. int page_stat;
  449. if (inode != pcol->inode)
  450. continue; /* osd might add more pages to a bio */
  451. if (likely(length < good_bytes))
  452. page_stat = 0;
  453. else
  454. page_stat = ret;
  455. update_write_page(page, page_stat);
  456. unlock_page(page);
  457. EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n",
  458. inode->i_ino, page->index, page_stat);
  459. length += PAGE_SIZE;
  460. }
  461. pcol_free(pcol);
  462. kfree(pcol);
  463. EXOFS_DBGMSG2("writepages_done END\n");
  464. }
  465. static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
  466. {
  467. struct page_collect *pcol = priv;
  468. pgoff_t index = offset / PAGE_SIZE;
  469. if (!pcol->that_locked_page ||
  470. (pcol->that_locked_page->index != index)) {
  471. struct page *page;
  472. loff_t i_size = i_size_read(pcol->inode);
  473. if (offset >= i_size) {
  474. *uptodate = true;
  475. EXOFS_DBGMSG2("offset >= i_size index=0x%lx\n", index);
  476. return ZERO_PAGE(0);
  477. }
  478. page = find_get_page(pcol->inode->i_mapping, index);
  479. if (!page) {
  480. page = find_or_create_page(pcol->inode->i_mapping,
  481. index, GFP_NOFS);
  482. if (unlikely(!page)) {
  483. EXOFS_DBGMSG("grab_cache_page Failed "
  484. "index=0x%llx\n", _LLU(index));
  485. return NULL;
  486. }
  487. unlock_page(page);
  488. }
  489. *uptodate = PageUptodate(page);
  490. EXOFS_DBGMSG2("index=0x%lx uptodate=%d\n", index, *uptodate);
  491. return page;
  492. } else {
  493. EXOFS_DBGMSG2("YES that_locked_page index=0x%lx\n",
  494. pcol->that_locked_page->index);
  495. *uptodate = true;
  496. return pcol->that_locked_page;
  497. }
  498. }
  499. static void __r4w_put_page(void *priv, struct page *page)
  500. {
  501. struct page_collect *pcol = priv;
  502. if ((pcol->that_locked_page != page) && (ZERO_PAGE(0) != page)) {
  503. EXOFS_DBGMSG2("index=0x%lx\n", page->index);
  504. put_page(page);
  505. return;
  506. }
  507. EXOFS_DBGMSG2("that_locked_page index=0x%lx\n",
  508. ZERO_PAGE(0) == page ? -1 : page->index);
  509. }
  510. static const struct _ore_r4w_op _r4w_op = {
  511. .get_page = &__r4w_get_page,
  512. .put_page = &__r4w_put_page,
  513. };
  514. static int write_exec(struct page_collect *pcol)
  515. {
  516. struct exofs_i_info *oi = exofs_i(pcol->inode);
  517. struct ore_io_state *ios;
  518. struct page_collect *pcol_copy = NULL;
  519. int ret;
  520. if (!pcol->pages)
  521. return 0;
  522. BUG_ON(pcol->ios);
  523. ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false,
  524. pcol->pg_first << PAGE_SHIFT,
  525. pcol->length, &pcol->ios);
  526. if (unlikely(ret))
  527. goto err;
  528. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  529. if (!pcol_copy) {
  530. EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n");
  531. ret = -ENOMEM;
  532. goto err;
  533. }
  534. *pcol_copy = *pcol;
  535. ios = pcol->ios;
  536. ios->pages = pcol_copy->pages;
  537. ios->done = writepages_done;
  538. ios->r4w = &_r4w_op;
  539. ios->private = pcol_copy;
  540. /* pages ownership was passed to pcol_copy */
  541. _pcol_reset(pcol);
  542. ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
  543. if (unlikely(ret))
  544. goto err;
  545. EXOFS_DBGMSG2("write_exec(0x%lx) offset=0x%llx length=0x%llx\n",
  546. pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));
  547. ret = ore_write(ios);
  548. if (unlikely(ret)) {
  549. EXOFS_ERR("write_exec: ore_write() Failed\n");
  550. goto err;
  551. }
  552. atomic_inc(&pcol->sbi->s_curr_pending);
  553. return 0;
  554. err:
  555. if (!pcol_copy) /* Failed before ownership transfer */
  556. pcol_copy = pcol;
  557. _unlock_pcol_pages(pcol_copy, ret, WRITE);
  558. pcol_free(pcol_copy);
  559. kfree(pcol_copy);
  560. return ret;
  561. }
  562. /* writepage_strip is called either directly from writepage() or by the VFS from
  563. * within write_cache_pages(), to add one more page to be written to storage.
  564. * It will try to collect as many contiguous pages as possible. If a
  565. * discontinuity is encountered or it runs out of resources it will submit the
  566. * previous segment and will start a new collection.
  567. * Eventually caller must submit the last segment if present.
  568. */
  569. static int writepage_strip(struct page *page,
  570. struct writeback_control *wbc_unused, void *data)
  571. {
  572. struct page_collect *pcol = data;
  573. struct inode *inode = pcol->inode;
  574. struct exofs_i_info *oi = exofs_i(inode);
  575. loff_t i_size = i_size_read(inode);
  576. pgoff_t end_index = i_size >> PAGE_SHIFT;
  577. size_t len;
  578. int ret;
  579. BUG_ON(!PageLocked(page));
  580. ret = wait_obj_created(oi);
  581. if (unlikely(ret))
  582. goto fail;
  583. if (page->index < end_index)
  584. /* in this case, the page is within the limits of the file */
  585. len = PAGE_SIZE;
  586. else {
  587. len = i_size & ~PAGE_MASK;
  588. if (page->index > end_index || !len) {
  589. /* in this case, the page is outside the limits
  590. * (truncate in progress)
  591. */
  592. ret = write_exec(pcol);
  593. if (unlikely(ret))
  594. goto fail;
  595. if (PageError(page))
  596. ClearPageError(page);
  597. unlock_page(page);
  598. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
  599. "outside the limits\n",
  600. inode->i_ino, page->index);
  601. return 0;
  602. }
  603. }
  604. try_again:
  605. if (unlikely(pcol->pg_first == -1)) {
  606. pcol->pg_first = page->index;
  607. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  608. page->index)) {
  609. /* Discontinuity detected, split the request */
  610. ret = write_exec(pcol);
  611. if (unlikely(ret))
  612. goto fail;
  613. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
  614. inode->i_ino, page->index);
  615. goto try_again;
  616. }
  617. if (!pcol->pages) {
  618. ret = pcol_try_alloc(pcol);
  619. if (unlikely(ret))
  620. goto fail;
  621. }
  622. EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  623. inode->i_ino, page->index, len);
  624. ret = pcol_add_page(pcol, page, len);
  625. if (unlikely(ret)) {
  626. EXOFS_DBGMSG2("Failed pcol_add_page "
  627. "nr_pages=%u total_length=0x%lx\n",
  628. pcol->nr_pages, pcol->length);
  629. /* split the request, next loop will start again */
  630. ret = write_exec(pcol);
  631. if (unlikely(ret)) {
  632. EXOFS_DBGMSG("write_exec failed => %d", ret);
  633. goto fail;
  634. }
  635. goto try_again;
  636. }
  637. BUG_ON(PageWriteback(page));
  638. set_page_writeback(page);
  639. return 0;
  640. fail:
  641. EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
  642. inode->i_ino, page->index, ret);
  643. mapping_set_error(page->mapping, -EIO);
  644. unlock_page(page);
  645. return ret;
  646. }
  647. static int exofs_writepages(struct address_space *mapping,
  648. struct writeback_control *wbc)
  649. {
  650. struct page_collect pcol;
  651. long start, end, expected_pages;
  652. int ret;
  653. start = wbc->range_start >> PAGE_SHIFT;
  654. end = (wbc->range_end == LLONG_MAX) ?
  655. start + mapping->nrpages :
  656. wbc->range_end >> PAGE_SHIFT;
  657. if (start || end)
  658. expected_pages = end - start + 1;
  659. else
  660. expected_pages = mapping->nrpages;
  661. if (expected_pages < 32L)
  662. expected_pages = 32L;
  663. EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
  664. "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
  665. mapping->host->i_ino, wbc->range_start, wbc->range_end,
  666. mapping->nrpages, start, end, expected_pages);
  667. _pcol_init(&pcol, expected_pages, mapping->host);
  668. ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
  669. if (unlikely(ret)) {
  670. EXOFS_ERR("write_cache_pages => %d\n", ret);
  671. return ret;
  672. }
  673. ret = write_exec(&pcol);
  674. if (unlikely(ret))
  675. return ret;
  676. if (wbc->sync_mode == WB_SYNC_ALL) {
  677. return write_exec(&pcol); /* pump the last reminder */
  678. } else if (pcol.nr_pages) {
  679. /* not SYNC let the reminder join the next writeout */
  680. unsigned i;
  681. for (i = 0; i < pcol.nr_pages; i++) {
  682. struct page *page = pcol.pages[i];
  683. end_page_writeback(page);
  684. set_page_dirty(page);
  685. unlock_page(page);
  686. }
  687. }
  688. return 0;
  689. }
  690. /*
  691. static int exofs_writepage(struct page *page, struct writeback_control *wbc)
  692. {
  693. struct page_collect pcol;
  694. int ret;
  695. _pcol_init(&pcol, 1, page->mapping->host);
  696. ret = writepage_strip(page, NULL, &pcol);
  697. if (ret) {
  698. EXOFS_ERR("exofs_writepage => %d\n", ret);
  699. return ret;
  700. }
  701. return write_exec(&pcol);
  702. }
  703. */
  704. /* i_mutex held using inode->i_size directly */
  705. static void _write_failed(struct inode *inode, loff_t to)
  706. {
  707. if (to > inode->i_size)
  708. truncate_pagecache(inode, inode->i_size);
  709. }
  710. int exofs_write_begin(struct file *file, struct address_space *mapping,
  711. loff_t pos, unsigned len, unsigned flags,
  712. struct page **pagep, void **fsdata)
  713. {
  714. int ret = 0;
  715. struct page *page;
  716. page = *pagep;
  717. if (page == NULL) {
  718. ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
  719. fsdata);
  720. if (ret) {
  721. EXOFS_DBGMSG("simple_write_begin failed\n");
  722. goto out;
  723. }
  724. page = *pagep;
  725. }
  726. /* read modify write */
  727. if (!PageUptodate(page) && (len != PAGE_SIZE)) {
  728. loff_t i_size = i_size_read(mapping->host);
  729. pgoff_t end_index = i_size >> PAGE_SHIFT;
  730. size_t rlen;
  731. if (page->index < end_index)
  732. rlen = PAGE_SIZE;
  733. else if (page->index == end_index)
  734. rlen = i_size & ~PAGE_MASK;
  735. else
  736. rlen = 0;
  737. if (!rlen) {
  738. clear_highpage(page);
  739. SetPageUptodate(page);
  740. goto out;
  741. }
  742. ret = _readpage(page, true);
  743. if (ret) {
  744. /*SetPageError was done by _readpage. Is it ok?*/
  745. unlock_page(page);
  746. EXOFS_DBGMSG("__readpage failed\n");
  747. }
  748. }
  749. out:
  750. if (unlikely(ret))
  751. _write_failed(mapping->host, pos + len);
  752. return ret;
  753. }
  754. static int exofs_write_begin_export(struct file *file,
  755. struct address_space *mapping,
  756. loff_t pos, unsigned len, unsigned flags,
  757. struct page **pagep, void **fsdata)
  758. {
  759. *pagep = NULL;
  760. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  761. fsdata);
  762. }
  763. static int exofs_write_end(struct file *file, struct address_space *mapping,
  764. loff_t pos, unsigned len, unsigned copied,
  765. struct page *page, void *fsdata)
  766. {
  767. struct inode *inode = mapping->host;
  768. /* According to comment in simple_write_end i_mutex is held */
  769. loff_t i_size = inode->i_size;
  770. int ret;
  771. ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
  772. if (unlikely(ret))
  773. _write_failed(inode, pos + len);
  774. /* TODO: once simple_write_end marks inode dirty remove */
  775. if (i_size != inode->i_size)
  776. mark_inode_dirty(inode);
  777. return ret;
  778. }
  779. static int exofs_releasepage(struct page *page, gfp_t gfp)
  780. {
  781. EXOFS_DBGMSG("page 0x%lx\n", page->index);
  782. WARN_ON(1);
  783. return 0;
  784. }
  785. static void exofs_invalidatepage(struct page *page, unsigned int offset,
  786. unsigned int length)
  787. {
  788. EXOFS_DBGMSG("page 0x%lx offset 0x%x length 0x%x\n",
  789. page->index, offset, length);
  790. WARN_ON(1);
  791. }
  792. /* TODO: Should be easy enough to do proprly */
  793. static ssize_t exofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  794. {
  795. return 0;
  796. }
  797. const struct address_space_operations exofs_aops = {
  798. .readpage = exofs_readpage,
  799. .readpages = exofs_readpages,
  800. .writepage = NULL,
  801. .writepages = exofs_writepages,
  802. .write_begin = exofs_write_begin_export,
  803. .write_end = exofs_write_end,
  804. .releasepage = exofs_releasepage,
  805. .set_page_dirty = __set_page_dirty_nobuffers,
  806. .invalidatepage = exofs_invalidatepage,
  807. /* Not implemented Yet */
  808. .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
  809. .direct_IO = exofs_direct_IO,
  810. /* With these NULL has special meaning or default is not exported */
  811. .migratepage = NULL,
  812. .launder_page = NULL,
  813. .is_partially_uptodate = NULL,
  814. .error_remove_page = NULL,
  815. };
  816. /******************************************************************************
  817. * INODE OPERATIONS
  818. *****************************************************************************/
  819. /*
  820. * Test whether an inode is a fast symlink.
  821. */
  822. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  823. {
  824. struct exofs_i_info *oi = exofs_i(inode);
  825. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  826. }
  827. static int _do_truncate(struct inode *inode, loff_t newsize)
  828. {
  829. struct exofs_i_info *oi = exofs_i(inode);
  830. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  831. int ret;
  832. inode->i_mtime = inode->i_ctime = current_time(inode);
  833. ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
  834. if (likely(!ret))
  835. truncate_setsize(inode, newsize);
  836. EXOFS_DBGMSG2("(0x%lx) size=0x%llx ret=>%d\n",
  837. inode->i_ino, newsize, ret);
  838. return ret;
  839. }
  840. /*
  841. * Set inode attributes - update size attribute on OSD if needed,
  842. * otherwise just call generic functions.
  843. */
  844. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  845. {
  846. struct inode *inode = d_inode(dentry);
  847. int error;
  848. /* if we are about to modify an object, and it hasn't been
  849. * created yet, wait
  850. */
  851. error = wait_obj_created(exofs_i(inode));
  852. if (unlikely(error))
  853. return error;
  854. error = setattr_prepare(dentry, iattr);
  855. if (unlikely(error))
  856. return error;
  857. if ((iattr->ia_valid & ATTR_SIZE) &&
  858. iattr->ia_size != i_size_read(inode)) {
  859. error = _do_truncate(inode, iattr->ia_size);
  860. if (unlikely(error))
  861. return error;
  862. }
  863. setattr_copy(inode, iattr);
  864. mark_inode_dirty(inode);
  865. return 0;
  866. }
  867. static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
  868. EXOFS_APAGE_FS_DATA,
  869. EXOFS_ATTR_INODE_FILE_LAYOUT,
  870. 0);
  871. static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
  872. EXOFS_APAGE_FS_DATA,
  873. EXOFS_ATTR_INODE_DIR_LAYOUT,
  874. 0);
  875. /*
  876. * Read the Linux inode info from the OSD, and return it as is. In exofs the
  877. * inode info is in an application specific page/attribute of the osd-object.
  878. */
  879. static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
  880. struct exofs_fcb *inode)
  881. {
  882. struct exofs_sb_info *sbi = sb->s_fs_info;
  883. struct osd_attr attrs[] = {
  884. [0] = g_attr_inode_data,
  885. [1] = g_attr_inode_file_layout,
  886. [2] = g_attr_inode_dir_layout,
  887. };
  888. struct ore_io_state *ios;
  889. struct exofs_on_disk_inode_layout *layout;
  890. int ret;
  891. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  892. if (unlikely(ret)) {
  893. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  894. return ret;
  895. }
  896. attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  897. attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  898. ios->in_attr = attrs;
  899. ios->in_attr_len = ARRAY_SIZE(attrs);
  900. ret = ore_read(ios);
  901. if (unlikely(ret)) {
  902. EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
  903. _LLU(oi->one_comp.obj.id), ret);
  904. memset(inode, 0, sizeof(*inode));
  905. inode->i_mode = 0040000 | (0777 & ~022);
  906. /* If object is lost on target we might as well enable it's
  907. * delete.
  908. */
  909. ret = 0;
  910. goto out;
  911. }
  912. ret = extract_attr_from_ios(ios, &attrs[0]);
  913. if (ret) {
  914. EXOFS_ERR("%s: extract_attr 0 of inode failed\n", __func__);
  915. goto out;
  916. }
  917. WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
  918. memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
  919. ret = extract_attr_from_ios(ios, &attrs[1]);
  920. if (ret) {
  921. EXOFS_ERR("%s: extract_attr 1 of inode failed\n", __func__);
  922. goto out;
  923. }
  924. if (attrs[1].len) {
  925. layout = attrs[1].val_ptr;
  926. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  927. EXOFS_ERR("%s: unsupported files layout %d\n",
  928. __func__, layout->gen_func);
  929. ret = -ENOTSUPP;
  930. goto out;
  931. }
  932. }
  933. ret = extract_attr_from_ios(ios, &attrs[2]);
  934. if (ret) {
  935. EXOFS_ERR("%s: extract_attr 2 of inode failed\n", __func__);
  936. goto out;
  937. }
  938. if (attrs[2].len) {
  939. layout = attrs[2].val_ptr;
  940. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  941. EXOFS_ERR("%s: unsupported meta-data layout %d\n",
  942. __func__, layout->gen_func);
  943. ret = -ENOTSUPP;
  944. goto out;
  945. }
  946. }
  947. out:
  948. ore_put_io_state(ios);
  949. return ret;
  950. }
  951. static void __oi_init(struct exofs_i_info *oi)
  952. {
  953. init_waitqueue_head(&oi->i_wq);
  954. oi->i_flags = 0;
  955. }
  956. /*
  957. * Fill in an inode read from the OSD and set it up for use
  958. */
  959. struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
  960. {
  961. struct exofs_i_info *oi;
  962. struct exofs_fcb fcb;
  963. struct inode *inode;
  964. int ret;
  965. inode = iget_locked(sb, ino);
  966. if (!inode)
  967. return ERR_PTR(-ENOMEM);
  968. if (!(inode->i_state & I_NEW))
  969. return inode;
  970. oi = exofs_i(inode);
  971. __oi_init(oi);
  972. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  973. exofs_oi_objno(oi));
  974. /* read the inode from the osd */
  975. ret = exofs_get_inode(sb, oi, &fcb);
  976. if (ret)
  977. goto bad_inode;
  978. set_obj_created(oi);
  979. /* copy stuff from on-disk struct to in-memory struct */
  980. inode->i_mode = le16_to_cpu(fcb.i_mode);
  981. i_uid_write(inode, le32_to_cpu(fcb.i_uid));
  982. i_gid_write(inode, le32_to_cpu(fcb.i_gid));
  983. set_nlink(inode, le16_to_cpu(fcb.i_links_count));
  984. inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
  985. inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
  986. inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
  987. inode->i_ctime.tv_nsec =
  988. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  989. oi->i_commit_size = le64_to_cpu(fcb.i_size);
  990. i_size_write(inode, oi->i_commit_size);
  991. inode->i_blkbits = EXOFS_BLKSHIFT;
  992. inode->i_generation = le32_to_cpu(fcb.i_generation);
  993. oi->i_dir_start_lookup = 0;
  994. if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
  995. ret = -ESTALE;
  996. goto bad_inode;
  997. }
  998. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  999. if (fcb.i_data[0])
  1000. inode->i_rdev =
  1001. old_decode_dev(le32_to_cpu(fcb.i_data[0]));
  1002. else
  1003. inode->i_rdev =
  1004. new_decode_dev(le32_to_cpu(fcb.i_data[1]));
  1005. } else {
  1006. memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
  1007. }
  1008. if (S_ISREG(inode->i_mode)) {
  1009. inode->i_op = &exofs_file_inode_operations;
  1010. inode->i_fop = &exofs_file_operations;
  1011. inode->i_mapping->a_ops = &exofs_aops;
  1012. } else if (S_ISDIR(inode->i_mode)) {
  1013. inode->i_op = &exofs_dir_inode_operations;
  1014. inode->i_fop = &exofs_dir_operations;
  1015. inode->i_mapping->a_ops = &exofs_aops;
  1016. } else if (S_ISLNK(inode->i_mode)) {
  1017. if (exofs_inode_is_fast_symlink(inode)) {
  1018. inode->i_op = &simple_symlink_inode_operations;
  1019. inode->i_link = (char *)oi->i_data;
  1020. } else {
  1021. inode->i_op = &page_symlink_inode_operations;
  1022. inode_nohighmem(inode);
  1023. inode->i_mapping->a_ops = &exofs_aops;
  1024. }
  1025. } else {
  1026. inode->i_op = &exofs_special_inode_operations;
  1027. if (fcb.i_data[0])
  1028. init_special_inode(inode, inode->i_mode,
  1029. old_decode_dev(le32_to_cpu(fcb.i_data[0])));
  1030. else
  1031. init_special_inode(inode, inode->i_mode,
  1032. new_decode_dev(le32_to_cpu(fcb.i_data[1])));
  1033. }
  1034. unlock_new_inode(inode);
  1035. return inode;
  1036. bad_inode:
  1037. iget_failed(inode);
  1038. return ERR_PTR(ret);
  1039. }
  1040. int __exofs_wait_obj_created(struct exofs_i_info *oi)
  1041. {
  1042. if (!obj_created(oi)) {
  1043. EXOFS_DBGMSG("!obj_created\n");
  1044. BUG_ON(!obj_2bcreated(oi));
  1045. wait_event(oi->i_wq, obj_created(oi));
  1046. EXOFS_DBGMSG("wait_event done\n");
  1047. }
  1048. return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
  1049. }
  1050. /*
  1051. * Callback function from exofs_new_inode(). The important thing is that we
  1052. * set the obj_created flag so that other methods know that the object exists on
  1053. * the OSD.
  1054. */
  1055. static void create_done(struct ore_io_state *ios, void *p)
  1056. {
  1057. struct inode *inode = p;
  1058. struct exofs_i_info *oi = exofs_i(inode);
  1059. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  1060. int ret;
  1061. ret = ore_check_io(ios, NULL);
  1062. ore_put_io_state(ios);
  1063. atomic_dec(&sbi->s_curr_pending);
  1064. if (unlikely(ret)) {
  1065. EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
  1066. _LLU(exofs_oi_objno(oi)),
  1067. _LLU(oi->one_comp.obj.partition));
  1068. /*TODO: When FS is corrupted creation can fail, object already
  1069. * exist. Get rid of this asynchronous creation, if exist
  1070. * increment the obj counter and try the next object. Until we
  1071. * succeed. All these dangling objects will be made into lost
  1072. * files by chkfs.exofs
  1073. */
  1074. }
  1075. set_obj_created(oi);
  1076. wake_up(&oi->i_wq);
  1077. }
  1078. /*
  1079. * Set up a new inode and create an object for it on the OSD
  1080. */
  1081. struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
  1082. {
  1083. struct super_block *sb = dir->i_sb;
  1084. struct exofs_sb_info *sbi = sb->s_fs_info;
  1085. struct inode *inode;
  1086. struct exofs_i_info *oi;
  1087. struct ore_io_state *ios;
  1088. int ret;
  1089. inode = new_inode(sb);
  1090. if (!inode)
  1091. return ERR_PTR(-ENOMEM);
  1092. oi = exofs_i(inode);
  1093. __oi_init(oi);
  1094. set_obj_2bcreated(oi);
  1095. inode_init_owner(inode, dir, mode);
  1096. inode->i_ino = sbi->s_nextid++;
  1097. inode->i_blkbits = EXOFS_BLKSHIFT;
  1098. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  1099. oi->i_commit_size = inode->i_size = 0;
  1100. spin_lock(&sbi->s_next_gen_lock);
  1101. inode->i_generation = sbi->s_next_generation++;
  1102. spin_unlock(&sbi->s_next_gen_lock);
  1103. insert_inode_hash(inode);
  1104. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  1105. exofs_oi_objno(oi));
  1106. exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
  1107. mark_inode_dirty(inode);
  1108. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1109. if (unlikely(ret)) {
  1110. EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
  1111. return ERR_PTR(ret);
  1112. }
  1113. ios->done = create_done;
  1114. ios->private = inode;
  1115. ret = ore_create(ios);
  1116. if (ret) {
  1117. ore_put_io_state(ios);
  1118. return ERR_PTR(ret);
  1119. }
  1120. atomic_inc(&sbi->s_curr_pending);
  1121. return inode;
  1122. }
  1123. /*
  1124. * struct to pass two arguments to update_inode's callback
  1125. */
  1126. struct updatei_args {
  1127. struct exofs_sb_info *sbi;
  1128. struct exofs_fcb fcb;
  1129. };
  1130. /*
  1131. * Callback function from exofs_update_inode().
  1132. */
  1133. static void updatei_done(struct ore_io_state *ios, void *p)
  1134. {
  1135. struct updatei_args *args = p;
  1136. ore_put_io_state(ios);
  1137. atomic_dec(&args->sbi->s_curr_pending);
  1138. kfree(args);
  1139. }
  1140. /*
  1141. * Write the inode to the OSD. Just fill up the struct, and set the attribute
  1142. * synchronously or asynchronously depending on the do_sync flag.
  1143. */
  1144. static int exofs_update_inode(struct inode *inode, int do_sync)
  1145. {
  1146. struct exofs_i_info *oi = exofs_i(inode);
  1147. struct super_block *sb = inode->i_sb;
  1148. struct exofs_sb_info *sbi = sb->s_fs_info;
  1149. struct ore_io_state *ios;
  1150. struct osd_attr attr;
  1151. struct exofs_fcb *fcb;
  1152. struct updatei_args *args;
  1153. int ret;
  1154. args = kzalloc(sizeof(*args), GFP_KERNEL);
  1155. if (!args) {
  1156. EXOFS_DBGMSG("Failed kzalloc of args\n");
  1157. return -ENOMEM;
  1158. }
  1159. fcb = &args->fcb;
  1160. fcb->i_mode = cpu_to_le16(inode->i_mode);
  1161. fcb->i_uid = cpu_to_le32(i_uid_read(inode));
  1162. fcb->i_gid = cpu_to_le32(i_gid_read(inode));
  1163. fcb->i_links_count = cpu_to_le16(inode->i_nlink);
  1164. fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1165. fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1166. fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1167. oi->i_commit_size = i_size_read(inode);
  1168. fcb->i_size = cpu_to_le64(oi->i_commit_size);
  1169. fcb->i_generation = cpu_to_le32(inode->i_generation);
  1170. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1171. if (old_valid_dev(inode->i_rdev)) {
  1172. fcb->i_data[0] =
  1173. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1174. fcb->i_data[1] = 0;
  1175. } else {
  1176. fcb->i_data[0] = 0;
  1177. fcb->i_data[1] =
  1178. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1179. fcb->i_data[2] = 0;
  1180. }
  1181. } else
  1182. memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
  1183. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1184. if (unlikely(ret)) {
  1185. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  1186. goto free_args;
  1187. }
  1188. attr = g_attr_inode_data;
  1189. attr.val_ptr = fcb;
  1190. ios->out_attr_len = 1;
  1191. ios->out_attr = &attr;
  1192. wait_obj_created(oi);
  1193. if (!do_sync) {
  1194. args->sbi = sbi;
  1195. ios->done = updatei_done;
  1196. ios->private = args;
  1197. }
  1198. ret = ore_write(ios);
  1199. if (!do_sync && !ret) {
  1200. atomic_inc(&sbi->s_curr_pending);
  1201. goto out; /* deallocation in updatei_done */
  1202. }
  1203. ore_put_io_state(ios);
  1204. free_args:
  1205. kfree(args);
  1206. out:
  1207. EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
  1208. inode->i_ino, do_sync, ret);
  1209. return ret;
  1210. }
  1211. int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1212. {
  1213. /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
  1214. return exofs_update_inode(inode, 1);
  1215. }
  1216. /*
  1217. * Callback function from exofs_delete_inode() - don't have much cleaning up to
  1218. * do.
  1219. */
  1220. static void delete_done(struct ore_io_state *ios, void *p)
  1221. {
  1222. struct exofs_sb_info *sbi = p;
  1223. ore_put_io_state(ios);
  1224. atomic_dec(&sbi->s_curr_pending);
  1225. }
  1226. /*
  1227. * Called when the refcount of an inode reaches zero. We remove the object
  1228. * from the OSD here. We make sure the object was created before we try and
  1229. * delete it.
  1230. */
  1231. void exofs_evict_inode(struct inode *inode)
  1232. {
  1233. struct exofs_i_info *oi = exofs_i(inode);
  1234. struct super_block *sb = inode->i_sb;
  1235. struct exofs_sb_info *sbi = sb->s_fs_info;
  1236. struct ore_io_state *ios;
  1237. int ret;
  1238. truncate_inode_pages_final(&inode->i_data);
  1239. /* TODO: should do better here */
  1240. if (inode->i_nlink || is_bad_inode(inode))
  1241. goto no_delete;
  1242. inode->i_size = 0;
  1243. clear_inode(inode);
  1244. /* if we are deleting an obj that hasn't been created yet, wait.
  1245. * This also makes sure that create_done cannot be called with an
  1246. * already evicted inode.
  1247. */
  1248. wait_obj_created(oi);
  1249. /* ignore the error, attempt a remove anyway */
  1250. /* Now Remove the OSD objects */
  1251. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1252. if (unlikely(ret)) {
  1253. EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
  1254. return;
  1255. }
  1256. ios->done = delete_done;
  1257. ios->private = sbi;
  1258. ret = ore_remove(ios);
  1259. if (ret) {
  1260. EXOFS_ERR("%s: ore_remove failed\n", __func__);
  1261. ore_put_io_state(ios);
  1262. return;
  1263. }
  1264. atomic_inc(&sbi->s_curr_pending);
  1265. return;
  1266. no_delete:
  1267. clear_inode(inode);
  1268. }