inode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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_array(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. page = grab_cache_page_write_begin(mapping, pos >> PAGE_SHIFT,
  719. flags);
  720. if (!page) {
  721. EXOFS_DBGMSG("grab_cache_page_write_begin failed\n");
  722. return -ENOMEM;
  723. }
  724. *pagep = page;
  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. if (page->index > end_index) {
  731. clear_highpage(page);
  732. SetPageUptodate(page);
  733. } else {
  734. ret = _readpage(page, true);
  735. if (ret) {
  736. unlock_page(page);
  737. EXOFS_DBGMSG("__readpage failed\n");
  738. }
  739. }
  740. }
  741. return ret;
  742. }
  743. static int exofs_write_begin_export(struct file *file,
  744. struct address_space *mapping,
  745. loff_t pos, unsigned len, unsigned flags,
  746. struct page **pagep, void **fsdata)
  747. {
  748. *pagep = NULL;
  749. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  750. fsdata);
  751. }
  752. static int exofs_write_end(struct file *file, struct address_space *mapping,
  753. loff_t pos, unsigned len, unsigned copied,
  754. struct page *page, void *fsdata)
  755. {
  756. struct inode *inode = mapping->host;
  757. loff_t last_pos = pos + copied;
  758. if (!PageUptodate(page)) {
  759. if (copied < len) {
  760. _write_failed(inode, pos + len);
  761. copied = 0;
  762. goto out;
  763. }
  764. SetPageUptodate(page);
  765. }
  766. if (last_pos > inode->i_size) {
  767. i_size_write(inode, last_pos);
  768. mark_inode_dirty(inode);
  769. }
  770. set_page_dirty(page);
  771. out:
  772. unlock_page(page);
  773. put_page(page);
  774. return copied;
  775. }
  776. static int exofs_releasepage(struct page *page, gfp_t gfp)
  777. {
  778. EXOFS_DBGMSG("page 0x%lx\n", page->index);
  779. WARN_ON(1);
  780. return 0;
  781. }
  782. static void exofs_invalidatepage(struct page *page, unsigned int offset,
  783. unsigned int length)
  784. {
  785. EXOFS_DBGMSG("page 0x%lx offset 0x%x length 0x%x\n",
  786. page->index, offset, length);
  787. WARN_ON(1);
  788. }
  789. /* TODO: Should be easy enough to do proprly */
  790. static ssize_t exofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  791. {
  792. return 0;
  793. }
  794. const struct address_space_operations exofs_aops = {
  795. .readpage = exofs_readpage,
  796. .readpages = exofs_readpages,
  797. .writepage = NULL,
  798. .writepages = exofs_writepages,
  799. .write_begin = exofs_write_begin_export,
  800. .write_end = exofs_write_end,
  801. .releasepage = exofs_releasepage,
  802. .set_page_dirty = __set_page_dirty_nobuffers,
  803. .invalidatepage = exofs_invalidatepage,
  804. /* Not implemented Yet */
  805. .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
  806. .direct_IO = exofs_direct_IO,
  807. /* With these NULL has special meaning or default is not exported */
  808. .migratepage = NULL,
  809. .launder_page = NULL,
  810. .is_partially_uptodate = NULL,
  811. .error_remove_page = NULL,
  812. };
  813. /******************************************************************************
  814. * INODE OPERATIONS
  815. *****************************************************************************/
  816. /*
  817. * Test whether an inode is a fast symlink.
  818. */
  819. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  820. {
  821. struct exofs_i_info *oi = exofs_i(inode);
  822. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  823. }
  824. static int _do_truncate(struct inode *inode, loff_t newsize)
  825. {
  826. struct exofs_i_info *oi = exofs_i(inode);
  827. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  828. int ret;
  829. inode->i_mtime = inode->i_ctime = current_time(inode);
  830. ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
  831. if (likely(!ret))
  832. truncate_setsize(inode, newsize);
  833. EXOFS_DBGMSG2("(0x%lx) size=0x%llx ret=>%d\n",
  834. inode->i_ino, newsize, ret);
  835. return ret;
  836. }
  837. /*
  838. * Set inode attributes - update size attribute on OSD if needed,
  839. * otherwise just call generic functions.
  840. */
  841. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  842. {
  843. struct inode *inode = d_inode(dentry);
  844. int error;
  845. /* if we are about to modify an object, and it hasn't been
  846. * created yet, wait
  847. */
  848. error = wait_obj_created(exofs_i(inode));
  849. if (unlikely(error))
  850. return error;
  851. error = setattr_prepare(dentry, iattr);
  852. if (unlikely(error))
  853. return error;
  854. if ((iattr->ia_valid & ATTR_SIZE) &&
  855. iattr->ia_size != i_size_read(inode)) {
  856. error = _do_truncate(inode, iattr->ia_size);
  857. if (unlikely(error))
  858. return error;
  859. }
  860. setattr_copy(inode, iattr);
  861. mark_inode_dirty(inode);
  862. return 0;
  863. }
  864. static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
  865. EXOFS_APAGE_FS_DATA,
  866. EXOFS_ATTR_INODE_FILE_LAYOUT,
  867. 0);
  868. static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
  869. EXOFS_APAGE_FS_DATA,
  870. EXOFS_ATTR_INODE_DIR_LAYOUT,
  871. 0);
  872. /*
  873. * Read the Linux inode info from the OSD, and return it as is. In exofs the
  874. * inode info is in an application specific page/attribute of the osd-object.
  875. */
  876. static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
  877. struct exofs_fcb *inode)
  878. {
  879. struct exofs_sb_info *sbi = sb->s_fs_info;
  880. struct osd_attr attrs[] = {
  881. [0] = g_attr_inode_data,
  882. [1] = g_attr_inode_file_layout,
  883. [2] = g_attr_inode_dir_layout,
  884. };
  885. struct ore_io_state *ios;
  886. struct exofs_on_disk_inode_layout *layout;
  887. int ret;
  888. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  889. if (unlikely(ret)) {
  890. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  891. return ret;
  892. }
  893. attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  894. attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  895. ios->in_attr = attrs;
  896. ios->in_attr_len = ARRAY_SIZE(attrs);
  897. ret = ore_read(ios);
  898. if (unlikely(ret)) {
  899. EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
  900. _LLU(oi->one_comp.obj.id), ret);
  901. memset(inode, 0, sizeof(*inode));
  902. inode->i_mode = 0040000 | (0777 & ~022);
  903. /* If object is lost on target we might as well enable it's
  904. * delete.
  905. */
  906. ret = 0;
  907. goto out;
  908. }
  909. ret = extract_attr_from_ios(ios, &attrs[0]);
  910. if (ret) {
  911. EXOFS_ERR("%s: extract_attr 0 of inode failed\n", __func__);
  912. goto out;
  913. }
  914. WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
  915. memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
  916. ret = extract_attr_from_ios(ios, &attrs[1]);
  917. if (ret) {
  918. EXOFS_ERR("%s: extract_attr 1 of inode failed\n", __func__);
  919. goto out;
  920. }
  921. if (attrs[1].len) {
  922. layout = attrs[1].val_ptr;
  923. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  924. EXOFS_ERR("%s: unsupported files layout %d\n",
  925. __func__, layout->gen_func);
  926. ret = -ENOTSUPP;
  927. goto out;
  928. }
  929. }
  930. ret = extract_attr_from_ios(ios, &attrs[2]);
  931. if (ret) {
  932. EXOFS_ERR("%s: extract_attr 2 of inode failed\n", __func__);
  933. goto out;
  934. }
  935. if (attrs[2].len) {
  936. layout = attrs[2].val_ptr;
  937. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  938. EXOFS_ERR("%s: unsupported meta-data layout %d\n",
  939. __func__, layout->gen_func);
  940. ret = -ENOTSUPP;
  941. goto out;
  942. }
  943. }
  944. out:
  945. ore_put_io_state(ios);
  946. return ret;
  947. }
  948. static void __oi_init(struct exofs_i_info *oi)
  949. {
  950. init_waitqueue_head(&oi->i_wq);
  951. oi->i_flags = 0;
  952. }
  953. /*
  954. * Fill in an inode read from the OSD and set it up for use
  955. */
  956. struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
  957. {
  958. struct exofs_i_info *oi;
  959. struct exofs_fcb fcb;
  960. struct inode *inode;
  961. int ret;
  962. inode = iget_locked(sb, ino);
  963. if (!inode)
  964. return ERR_PTR(-ENOMEM);
  965. if (!(inode->i_state & I_NEW))
  966. return inode;
  967. oi = exofs_i(inode);
  968. __oi_init(oi);
  969. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  970. exofs_oi_objno(oi));
  971. /* read the inode from the osd */
  972. ret = exofs_get_inode(sb, oi, &fcb);
  973. if (ret)
  974. goto bad_inode;
  975. set_obj_created(oi);
  976. /* copy stuff from on-disk struct to in-memory struct */
  977. inode->i_mode = le16_to_cpu(fcb.i_mode);
  978. i_uid_write(inode, le32_to_cpu(fcb.i_uid));
  979. i_gid_write(inode, le32_to_cpu(fcb.i_gid));
  980. set_nlink(inode, le16_to_cpu(fcb.i_links_count));
  981. inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
  982. inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
  983. inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
  984. inode->i_ctime.tv_nsec =
  985. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  986. oi->i_commit_size = le64_to_cpu(fcb.i_size);
  987. i_size_write(inode, oi->i_commit_size);
  988. inode->i_blkbits = EXOFS_BLKSHIFT;
  989. inode->i_generation = le32_to_cpu(fcb.i_generation);
  990. oi->i_dir_start_lookup = 0;
  991. if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
  992. ret = -ESTALE;
  993. goto bad_inode;
  994. }
  995. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  996. if (fcb.i_data[0])
  997. inode->i_rdev =
  998. old_decode_dev(le32_to_cpu(fcb.i_data[0]));
  999. else
  1000. inode->i_rdev =
  1001. new_decode_dev(le32_to_cpu(fcb.i_data[1]));
  1002. } else {
  1003. memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
  1004. }
  1005. if (S_ISREG(inode->i_mode)) {
  1006. inode->i_op = &exofs_file_inode_operations;
  1007. inode->i_fop = &exofs_file_operations;
  1008. inode->i_mapping->a_ops = &exofs_aops;
  1009. } else if (S_ISDIR(inode->i_mode)) {
  1010. inode->i_op = &exofs_dir_inode_operations;
  1011. inode->i_fop = &exofs_dir_operations;
  1012. inode->i_mapping->a_ops = &exofs_aops;
  1013. } else if (S_ISLNK(inode->i_mode)) {
  1014. if (exofs_inode_is_fast_symlink(inode)) {
  1015. inode->i_op = &simple_symlink_inode_operations;
  1016. inode->i_link = (char *)oi->i_data;
  1017. } else {
  1018. inode->i_op = &page_symlink_inode_operations;
  1019. inode_nohighmem(inode);
  1020. inode->i_mapping->a_ops = &exofs_aops;
  1021. }
  1022. } else {
  1023. inode->i_op = &exofs_special_inode_operations;
  1024. if (fcb.i_data[0])
  1025. init_special_inode(inode, inode->i_mode,
  1026. old_decode_dev(le32_to_cpu(fcb.i_data[0])));
  1027. else
  1028. init_special_inode(inode, inode->i_mode,
  1029. new_decode_dev(le32_to_cpu(fcb.i_data[1])));
  1030. }
  1031. unlock_new_inode(inode);
  1032. return inode;
  1033. bad_inode:
  1034. iget_failed(inode);
  1035. return ERR_PTR(ret);
  1036. }
  1037. int __exofs_wait_obj_created(struct exofs_i_info *oi)
  1038. {
  1039. if (!obj_created(oi)) {
  1040. EXOFS_DBGMSG("!obj_created\n");
  1041. BUG_ON(!obj_2bcreated(oi));
  1042. wait_event(oi->i_wq, obj_created(oi));
  1043. EXOFS_DBGMSG("wait_event done\n");
  1044. }
  1045. return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
  1046. }
  1047. /*
  1048. * Callback function from exofs_new_inode(). The important thing is that we
  1049. * set the obj_created flag so that other methods know that the object exists on
  1050. * the OSD.
  1051. */
  1052. static void create_done(struct ore_io_state *ios, void *p)
  1053. {
  1054. struct inode *inode = p;
  1055. struct exofs_i_info *oi = exofs_i(inode);
  1056. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  1057. int ret;
  1058. ret = ore_check_io(ios, NULL);
  1059. ore_put_io_state(ios);
  1060. atomic_dec(&sbi->s_curr_pending);
  1061. if (unlikely(ret)) {
  1062. EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
  1063. _LLU(exofs_oi_objno(oi)),
  1064. _LLU(oi->one_comp.obj.partition));
  1065. /*TODO: When FS is corrupted creation can fail, object already
  1066. * exist. Get rid of this asynchronous creation, if exist
  1067. * increment the obj counter and try the next object. Until we
  1068. * succeed. All these dangling objects will be made into lost
  1069. * files by chkfs.exofs
  1070. */
  1071. }
  1072. set_obj_created(oi);
  1073. wake_up(&oi->i_wq);
  1074. }
  1075. /*
  1076. * Set up a new inode and create an object for it on the OSD
  1077. */
  1078. struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
  1079. {
  1080. struct super_block *sb = dir->i_sb;
  1081. struct exofs_sb_info *sbi = sb->s_fs_info;
  1082. struct inode *inode;
  1083. struct exofs_i_info *oi;
  1084. struct ore_io_state *ios;
  1085. int ret;
  1086. inode = new_inode(sb);
  1087. if (!inode)
  1088. return ERR_PTR(-ENOMEM);
  1089. oi = exofs_i(inode);
  1090. __oi_init(oi);
  1091. set_obj_2bcreated(oi);
  1092. inode_init_owner(inode, dir, mode);
  1093. inode->i_ino = sbi->s_nextid++;
  1094. inode->i_blkbits = EXOFS_BLKSHIFT;
  1095. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  1096. oi->i_commit_size = inode->i_size = 0;
  1097. spin_lock(&sbi->s_next_gen_lock);
  1098. inode->i_generation = sbi->s_next_generation++;
  1099. spin_unlock(&sbi->s_next_gen_lock);
  1100. insert_inode_hash(inode);
  1101. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  1102. exofs_oi_objno(oi));
  1103. exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
  1104. mark_inode_dirty(inode);
  1105. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1106. if (unlikely(ret)) {
  1107. EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
  1108. return ERR_PTR(ret);
  1109. }
  1110. ios->done = create_done;
  1111. ios->private = inode;
  1112. ret = ore_create(ios);
  1113. if (ret) {
  1114. ore_put_io_state(ios);
  1115. return ERR_PTR(ret);
  1116. }
  1117. atomic_inc(&sbi->s_curr_pending);
  1118. return inode;
  1119. }
  1120. /*
  1121. * struct to pass two arguments to update_inode's callback
  1122. */
  1123. struct updatei_args {
  1124. struct exofs_sb_info *sbi;
  1125. struct exofs_fcb fcb;
  1126. };
  1127. /*
  1128. * Callback function from exofs_update_inode().
  1129. */
  1130. static void updatei_done(struct ore_io_state *ios, void *p)
  1131. {
  1132. struct updatei_args *args = p;
  1133. ore_put_io_state(ios);
  1134. atomic_dec(&args->sbi->s_curr_pending);
  1135. kfree(args);
  1136. }
  1137. /*
  1138. * Write the inode to the OSD. Just fill up the struct, and set the attribute
  1139. * synchronously or asynchronously depending on the do_sync flag.
  1140. */
  1141. static int exofs_update_inode(struct inode *inode, int do_sync)
  1142. {
  1143. struct exofs_i_info *oi = exofs_i(inode);
  1144. struct super_block *sb = inode->i_sb;
  1145. struct exofs_sb_info *sbi = sb->s_fs_info;
  1146. struct ore_io_state *ios;
  1147. struct osd_attr attr;
  1148. struct exofs_fcb *fcb;
  1149. struct updatei_args *args;
  1150. int ret;
  1151. args = kzalloc(sizeof(*args), GFP_KERNEL);
  1152. if (!args) {
  1153. EXOFS_DBGMSG("Failed kzalloc of args\n");
  1154. return -ENOMEM;
  1155. }
  1156. fcb = &args->fcb;
  1157. fcb->i_mode = cpu_to_le16(inode->i_mode);
  1158. fcb->i_uid = cpu_to_le32(i_uid_read(inode));
  1159. fcb->i_gid = cpu_to_le32(i_gid_read(inode));
  1160. fcb->i_links_count = cpu_to_le16(inode->i_nlink);
  1161. fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1162. fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1163. fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1164. oi->i_commit_size = i_size_read(inode);
  1165. fcb->i_size = cpu_to_le64(oi->i_commit_size);
  1166. fcb->i_generation = cpu_to_le32(inode->i_generation);
  1167. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1168. if (old_valid_dev(inode->i_rdev)) {
  1169. fcb->i_data[0] =
  1170. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1171. fcb->i_data[1] = 0;
  1172. } else {
  1173. fcb->i_data[0] = 0;
  1174. fcb->i_data[1] =
  1175. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1176. fcb->i_data[2] = 0;
  1177. }
  1178. } else
  1179. memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
  1180. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1181. if (unlikely(ret)) {
  1182. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  1183. goto free_args;
  1184. }
  1185. attr = g_attr_inode_data;
  1186. attr.val_ptr = fcb;
  1187. ios->out_attr_len = 1;
  1188. ios->out_attr = &attr;
  1189. wait_obj_created(oi);
  1190. if (!do_sync) {
  1191. args->sbi = sbi;
  1192. ios->done = updatei_done;
  1193. ios->private = args;
  1194. }
  1195. ret = ore_write(ios);
  1196. if (!do_sync && !ret) {
  1197. atomic_inc(&sbi->s_curr_pending);
  1198. goto out; /* deallocation in updatei_done */
  1199. }
  1200. ore_put_io_state(ios);
  1201. free_args:
  1202. kfree(args);
  1203. out:
  1204. EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
  1205. inode->i_ino, do_sync, ret);
  1206. return ret;
  1207. }
  1208. int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1209. {
  1210. /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
  1211. return exofs_update_inode(inode, 1);
  1212. }
  1213. /*
  1214. * Callback function from exofs_delete_inode() - don't have much cleaning up to
  1215. * do.
  1216. */
  1217. static void delete_done(struct ore_io_state *ios, void *p)
  1218. {
  1219. struct exofs_sb_info *sbi = p;
  1220. ore_put_io_state(ios);
  1221. atomic_dec(&sbi->s_curr_pending);
  1222. }
  1223. /*
  1224. * Called when the refcount of an inode reaches zero. We remove the object
  1225. * from the OSD here. We make sure the object was created before we try and
  1226. * delete it.
  1227. */
  1228. void exofs_evict_inode(struct inode *inode)
  1229. {
  1230. struct exofs_i_info *oi = exofs_i(inode);
  1231. struct super_block *sb = inode->i_sb;
  1232. struct exofs_sb_info *sbi = sb->s_fs_info;
  1233. struct ore_io_state *ios;
  1234. int ret;
  1235. truncate_inode_pages_final(&inode->i_data);
  1236. /* TODO: should do better here */
  1237. if (inode->i_nlink || is_bad_inode(inode))
  1238. goto no_delete;
  1239. inode->i_size = 0;
  1240. clear_inode(inode);
  1241. /* if we are deleting an obj that hasn't been created yet, wait.
  1242. * This also makes sure that create_done cannot be called with an
  1243. * already evicted inode.
  1244. */
  1245. wait_obj_created(oi);
  1246. /* ignore the error, attempt a remove anyway */
  1247. /* Now Remove the OSD objects */
  1248. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1249. if (unlikely(ret)) {
  1250. EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
  1251. return;
  1252. }
  1253. ios->done = delete_done;
  1254. ios->private = sbi;
  1255. ret = ore_remove(ios);
  1256. if (ret) {
  1257. EXOFS_ERR("%s: ore_remove failed\n", __func__);
  1258. ore_put_io_state(ios);
  1259. return;
  1260. }
  1261. atomic_inc(&sbi->s_curr_pending);
  1262. return;
  1263. no_delete:
  1264. clear_inode(inode);
  1265. }