inode.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger ([email protected])
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <[email protected]>
  6. *
  7. * Copyrights for code taken from ext2:
  8. * Copyright (C) 1992, 1993, 1994, 1995
  9. * Remy Card ([email protected])
  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(struct page_collect *pcol, struct page *page)
  317. {
  318. struct inode *inode = pcol->inode;
  319. struct exofs_i_info *oi = exofs_i(inode);
  320. loff_t i_size = i_size_read(inode);
  321. pgoff_t end_index = i_size >> PAGE_SHIFT;
  322. size_t len;
  323. int ret;
  324. BUG_ON(!PageLocked(page));
  325. /* FIXME: Just for debugging, will be removed */
  326. if (PageUptodate(page))
  327. EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
  328. page->index);
  329. pcol->that_locked_page = page;
  330. if (page->index < end_index)
  331. len = PAGE_SIZE;
  332. else if (page->index == end_index)
  333. len = i_size & ~PAGE_MASK;
  334. else
  335. len = 0;
  336. if (!len || !obj_created(oi)) {
  337. /* this will be out of bounds, or doesn't exist yet.
  338. * Current page is cleared and the request is split
  339. */
  340. clear_highpage(page);
  341. SetPageUptodate(page);
  342. if (PageError(page))
  343. ClearPageError(page);
  344. if (!pcol->read_4_write)
  345. unlock_page(page);
  346. EXOFS_DBGMSG("readpage_strip(0x%lx) empty page len=%zx "
  347. "read_4_write=%d index=0x%lx end_index=0x%lx "
  348. "splitting\n", inode->i_ino, len,
  349. pcol->read_4_write, page->index, end_index);
  350. return read_exec(pcol);
  351. }
  352. try_again:
  353. if (unlikely(pcol->pg_first == -1)) {
  354. pcol->pg_first = page->index;
  355. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  356. page->index)) {
  357. /* Discontinuity detected, split the request */
  358. ret = read_exec(pcol);
  359. if (unlikely(ret))
  360. goto fail;
  361. goto try_again;
  362. }
  363. if (!pcol->pages) {
  364. ret = pcol_try_alloc(pcol);
  365. if (unlikely(ret))
  366. goto fail;
  367. }
  368. if (len != PAGE_SIZE)
  369. zero_user(page, len, PAGE_SIZE - len);
  370. EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  371. inode->i_ino, page->index, len);
  372. ret = pcol_add_page(pcol, page, len);
  373. if (ret) {
  374. EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
  375. "this_len=0x%zx nr_pages=%u length=0x%lx\n",
  376. page, len, pcol->nr_pages, pcol->length);
  377. /* split the request, and start again with current page */
  378. ret = read_exec(pcol);
  379. if (unlikely(ret))
  380. goto fail;
  381. goto try_again;
  382. }
  383. return 0;
  384. fail:
  385. /* SetPageError(page); ??? */
  386. unlock_page(page);
  387. return ret;
  388. }
  389. static int readpage_strip(struct file *data, struct page *page)
  390. {
  391. struct page_collect *pcol = (struct page_collect *)data;
  392. return __readpage_strip(pcol, page);
  393. }
  394. static int exofs_readpages(struct file *file, struct address_space *mapping,
  395. struct list_head *pages, unsigned nr_pages)
  396. {
  397. struct page_collect pcol;
  398. int ret;
  399. _pcol_init(&pcol, nr_pages, mapping->host);
  400. ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
  401. if (ret) {
  402. EXOFS_ERR("read_cache_pages => %d\n", ret);
  403. return ret;
  404. }
  405. ret = read_exec(&pcol);
  406. if (unlikely(ret))
  407. return ret;
  408. return read_exec(&pcol);
  409. }
  410. static int _readpage(struct page *page, bool read_4_write)
  411. {
  412. struct page_collect pcol;
  413. int ret;
  414. _pcol_init(&pcol, 1, page->mapping->host);
  415. pcol.read_4_write = read_4_write;
  416. ret = __readpage_strip(&pcol, page);
  417. if (ret) {
  418. EXOFS_ERR("_readpage => %d\n", ret);
  419. return ret;
  420. }
  421. return read_exec(&pcol);
  422. }
  423. /*
  424. * We don't need the file
  425. */
  426. static int exofs_readpage(struct file *file, struct page *page)
  427. {
  428. return _readpage(page, false);
  429. }
  430. /* Callback for osd_write. All writes are asynchronous */
  431. static void writepages_done(struct ore_io_state *ios, void *p)
  432. {
  433. struct page_collect *pcol = p;
  434. int i;
  435. u64 good_bytes;
  436. u64 length = 0;
  437. int ret = ore_check_io(ios, NULL);
  438. atomic_dec(&pcol->sbi->s_curr_pending);
  439. if (likely(!ret)) {
  440. good_bytes = pcol->length;
  441. ret = PAGE_WAS_NOT_IN_IO;
  442. } else {
  443. good_bytes = 0;
  444. }
  445. EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
  446. " length=0x%lx nr_pages=%u\n",
  447. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  448. pcol->nr_pages);
  449. for (i = 0; i < pcol->nr_pages; i++) {
  450. struct page *page = pcol->pages[i];
  451. struct inode *inode = page->mapping->host;
  452. int page_stat;
  453. if (inode != pcol->inode)
  454. continue; /* osd might add more pages to a bio */
  455. if (likely(length < good_bytes))
  456. page_stat = 0;
  457. else
  458. page_stat = ret;
  459. update_write_page(page, page_stat);
  460. unlock_page(page);
  461. EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n",
  462. inode->i_ino, page->index, page_stat);
  463. length += PAGE_SIZE;
  464. }
  465. pcol_free(pcol);
  466. kfree(pcol);
  467. EXOFS_DBGMSG2("writepages_done END\n");
  468. }
  469. static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
  470. {
  471. struct page_collect *pcol = priv;
  472. pgoff_t index = offset / PAGE_SIZE;
  473. if (!pcol->that_locked_page ||
  474. (pcol->that_locked_page->index != index)) {
  475. struct page *page;
  476. loff_t i_size = i_size_read(pcol->inode);
  477. if (offset >= i_size) {
  478. *uptodate = true;
  479. EXOFS_DBGMSG2("offset >= i_size index=0x%lx\n", index);
  480. return ZERO_PAGE(0);
  481. }
  482. page = find_get_page(pcol->inode->i_mapping, index);
  483. if (!page) {
  484. page = find_or_create_page(pcol->inode->i_mapping,
  485. index, GFP_NOFS);
  486. if (unlikely(!page)) {
  487. EXOFS_DBGMSG("grab_cache_page Failed "
  488. "index=0x%llx\n", _LLU(index));
  489. return NULL;
  490. }
  491. unlock_page(page);
  492. }
  493. *uptodate = PageUptodate(page);
  494. EXOFS_DBGMSG2("index=0x%lx uptodate=%d\n", index, *uptodate);
  495. return page;
  496. } else {
  497. EXOFS_DBGMSG2("YES that_locked_page index=0x%lx\n",
  498. pcol->that_locked_page->index);
  499. *uptodate = true;
  500. return pcol->that_locked_page;
  501. }
  502. }
  503. static void __r4w_put_page(void *priv, struct page *page)
  504. {
  505. struct page_collect *pcol = priv;
  506. if ((pcol->that_locked_page != page) && (ZERO_PAGE(0) != page)) {
  507. EXOFS_DBGMSG2("index=0x%lx\n", page->index);
  508. put_page(page);
  509. return;
  510. }
  511. EXOFS_DBGMSG2("that_locked_page index=0x%lx\n",
  512. ZERO_PAGE(0) == page ? -1 : page->index);
  513. }
  514. static const struct _ore_r4w_op _r4w_op = {
  515. .get_page = &__r4w_get_page,
  516. .put_page = &__r4w_put_page,
  517. };
  518. static int write_exec(struct page_collect *pcol)
  519. {
  520. struct exofs_i_info *oi = exofs_i(pcol->inode);
  521. struct ore_io_state *ios;
  522. struct page_collect *pcol_copy = NULL;
  523. int ret;
  524. if (!pcol->pages)
  525. return 0;
  526. BUG_ON(pcol->ios);
  527. ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, false,
  528. pcol->pg_first << PAGE_SHIFT,
  529. pcol->length, &pcol->ios);
  530. if (unlikely(ret))
  531. goto err;
  532. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  533. if (!pcol_copy) {
  534. EXOFS_ERR("write_exec: Failed to kmalloc(pcol)\n");
  535. ret = -ENOMEM;
  536. goto err;
  537. }
  538. *pcol_copy = *pcol;
  539. ios = pcol->ios;
  540. ios->pages = pcol_copy->pages;
  541. ios->done = writepages_done;
  542. ios->r4w = &_r4w_op;
  543. ios->private = pcol_copy;
  544. /* pages ownership was passed to pcol_copy */
  545. _pcol_reset(pcol);
  546. ret = _maybe_not_all_in_one_io(ios, pcol_copy, pcol);
  547. if (unlikely(ret))
  548. goto err;
  549. EXOFS_DBGMSG2("write_exec(0x%lx) offset=0x%llx length=0x%llx\n",
  550. pcol->inode->i_ino, _LLU(ios->offset), _LLU(ios->length));
  551. ret = ore_write(ios);
  552. if (unlikely(ret)) {
  553. EXOFS_ERR("write_exec: ore_write() Failed\n");
  554. goto err;
  555. }
  556. atomic_inc(&pcol->sbi->s_curr_pending);
  557. return 0;
  558. err:
  559. if (!pcol_copy) /* Failed before ownership transfer */
  560. pcol_copy = pcol;
  561. _unlock_pcol_pages(pcol_copy, ret, WRITE);
  562. pcol_free(pcol_copy);
  563. kfree(pcol_copy);
  564. return ret;
  565. }
  566. /* writepage_strip is called either directly from writepage() or by the VFS from
  567. * within write_cache_pages(), to add one more page to be written to storage.
  568. * It will try to collect as many contiguous pages as possible. If a
  569. * discontinuity is encountered or it runs out of resources it will submit the
  570. * previous segment and will start a new collection.
  571. * Eventually caller must submit the last segment if present.
  572. */
  573. static int writepage_strip(struct page *page,
  574. struct writeback_control *wbc_unused, void *data)
  575. {
  576. struct page_collect *pcol = data;
  577. struct inode *inode = pcol->inode;
  578. struct exofs_i_info *oi = exofs_i(inode);
  579. loff_t i_size = i_size_read(inode);
  580. pgoff_t end_index = i_size >> PAGE_SHIFT;
  581. size_t len;
  582. int ret;
  583. BUG_ON(!PageLocked(page));
  584. ret = wait_obj_created(oi);
  585. if (unlikely(ret))
  586. goto fail;
  587. if (page->index < end_index)
  588. /* in this case, the page is within the limits of the file */
  589. len = PAGE_SIZE;
  590. else {
  591. len = i_size & ~PAGE_MASK;
  592. if (page->index > end_index || !len) {
  593. /* in this case, the page is outside the limits
  594. * (truncate in progress)
  595. */
  596. ret = write_exec(pcol);
  597. if (unlikely(ret))
  598. goto fail;
  599. if (PageError(page))
  600. ClearPageError(page);
  601. unlock_page(page);
  602. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
  603. "outside the limits\n",
  604. inode->i_ino, page->index);
  605. return 0;
  606. }
  607. }
  608. try_again:
  609. if (unlikely(pcol->pg_first == -1)) {
  610. pcol->pg_first = page->index;
  611. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  612. page->index)) {
  613. /* Discontinuity detected, split the request */
  614. ret = write_exec(pcol);
  615. if (unlikely(ret))
  616. goto fail;
  617. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
  618. inode->i_ino, page->index);
  619. goto try_again;
  620. }
  621. if (!pcol->pages) {
  622. ret = pcol_try_alloc(pcol);
  623. if (unlikely(ret))
  624. goto fail;
  625. }
  626. EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  627. inode->i_ino, page->index, len);
  628. ret = pcol_add_page(pcol, page, len);
  629. if (unlikely(ret)) {
  630. EXOFS_DBGMSG2("Failed pcol_add_page "
  631. "nr_pages=%u total_length=0x%lx\n",
  632. pcol->nr_pages, pcol->length);
  633. /* split the request, next loop will start again */
  634. ret = write_exec(pcol);
  635. if (unlikely(ret)) {
  636. EXOFS_DBGMSG("write_exec failed => %d", ret);
  637. goto fail;
  638. }
  639. goto try_again;
  640. }
  641. BUG_ON(PageWriteback(page));
  642. set_page_writeback(page);
  643. return 0;
  644. fail:
  645. EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
  646. inode->i_ino, page->index, ret);
  647. mapping_set_error(page->mapping, -EIO);
  648. unlock_page(page);
  649. return ret;
  650. }
  651. static int exofs_writepages(struct address_space *mapping,
  652. struct writeback_control *wbc)
  653. {
  654. struct page_collect pcol;
  655. long start, end, expected_pages;
  656. int ret;
  657. start = wbc->range_start >> PAGE_SHIFT;
  658. end = (wbc->range_end == LLONG_MAX) ?
  659. start + mapping->nrpages :
  660. wbc->range_end >> PAGE_SHIFT;
  661. if (start || end)
  662. expected_pages = end - start + 1;
  663. else
  664. expected_pages = mapping->nrpages;
  665. if (expected_pages < 32L)
  666. expected_pages = 32L;
  667. EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
  668. "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
  669. mapping->host->i_ino, wbc->range_start, wbc->range_end,
  670. mapping->nrpages, start, end, expected_pages);
  671. _pcol_init(&pcol, expected_pages, mapping->host);
  672. ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
  673. if (unlikely(ret)) {
  674. EXOFS_ERR("write_cache_pages => %d\n", ret);
  675. return ret;
  676. }
  677. ret = write_exec(&pcol);
  678. if (unlikely(ret))
  679. return ret;
  680. if (wbc->sync_mode == WB_SYNC_ALL) {
  681. return write_exec(&pcol); /* pump the last reminder */
  682. } else if (pcol.nr_pages) {
  683. /* not SYNC let the reminder join the next writeout */
  684. unsigned i;
  685. for (i = 0; i < pcol.nr_pages; i++) {
  686. struct page *page = pcol.pages[i];
  687. end_page_writeback(page);
  688. set_page_dirty(page);
  689. unlock_page(page);
  690. }
  691. }
  692. return 0;
  693. }
  694. /*
  695. static int exofs_writepage(struct page *page, struct writeback_control *wbc)
  696. {
  697. struct page_collect pcol;
  698. int ret;
  699. _pcol_init(&pcol, 1, page->mapping->host);
  700. ret = writepage_strip(page, NULL, &pcol);
  701. if (ret) {
  702. EXOFS_ERR("exofs_writepage => %d\n", ret);
  703. return ret;
  704. }
  705. return write_exec(&pcol);
  706. }
  707. */
  708. /* i_mutex held using inode->i_size directly */
  709. static void _write_failed(struct inode *inode, loff_t to)
  710. {
  711. if (to > inode->i_size)
  712. truncate_pagecache(inode, inode->i_size);
  713. }
  714. int exofs_write_begin(struct file *file, struct address_space *mapping,
  715. loff_t pos, unsigned len, unsigned flags,
  716. struct page **pagep, void **fsdata)
  717. {
  718. int ret = 0;
  719. struct page *page;
  720. page = *pagep;
  721. if (page == NULL) {
  722. ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
  723. fsdata);
  724. if (ret) {
  725. EXOFS_DBGMSG("simple_write_begin failed\n");
  726. goto out;
  727. }
  728. page = *pagep;
  729. }
  730. /* read modify write */
  731. if (!PageUptodate(page) && (len != PAGE_SIZE)) {
  732. loff_t i_size = i_size_read(mapping->host);
  733. pgoff_t end_index = i_size >> PAGE_SHIFT;
  734. size_t rlen;
  735. if (page->index < end_index)
  736. rlen = PAGE_SIZE;
  737. else if (page->index == end_index)
  738. rlen = i_size & ~PAGE_MASK;
  739. else
  740. rlen = 0;
  741. if (!rlen) {
  742. clear_highpage(page);
  743. SetPageUptodate(page);
  744. goto out;
  745. }
  746. ret = _readpage(page, true);
  747. if (ret) {
  748. /*SetPageError was done by _readpage. Is it ok?*/
  749. unlock_page(page);
  750. EXOFS_DBGMSG("__readpage failed\n");
  751. }
  752. }
  753. out:
  754. if (unlikely(ret))
  755. _write_failed(mapping->host, pos + len);
  756. return ret;
  757. }
  758. static int exofs_write_begin_export(struct file *file,
  759. struct address_space *mapping,
  760. loff_t pos, unsigned len, unsigned flags,
  761. struct page **pagep, void **fsdata)
  762. {
  763. *pagep = NULL;
  764. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  765. fsdata);
  766. }
  767. static int exofs_write_end(struct file *file, struct address_space *mapping,
  768. loff_t pos, unsigned len, unsigned copied,
  769. struct page *page, void *fsdata)
  770. {
  771. struct inode *inode = mapping->host;
  772. /* According to comment in simple_write_end i_mutex is held */
  773. loff_t i_size = inode->i_size;
  774. int ret;
  775. ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
  776. if (unlikely(ret))
  777. _write_failed(inode, pos + len);
  778. /* TODO: once simple_write_end marks inode dirty remove */
  779. if (i_size != inode->i_size)
  780. mark_inode_dirty(inode);
  781. return ret;
  782. }
  783. static int exofs_releasepage(struct page *page, gfp_t gfp)
  784. {
  785. EXOFS_DBGMSG("page 0x%lx\n", page->index);
  786. WARN_ON(1);
  787. return 0;
  788. }
  789. static void exofs_invalidatepage(struct page *page, unsigned int offset,
  790. unsigned int length)
  791. {
  792. EXOFS_DBGMSG("page 0x%lx offset 0x%x length 0x%x\n",
  793. page->index, offset, length);
  794. WARN_ON(1);
  795. }
  796. /* TODO: Should be easy enough to do proprly */
  797. static ssize_t exofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  798. {
  799. return 0;
  800. }
  801. const struct address_space_operations exofs_aops = {
  802. .readpage = exofs_readpage,
  803. .readpages = exofs_readpages,
  804. .writepage = NULL,
  805. .writepages = exofs_writepages,
  806. .write_begin = exofs_write_begin_export,
  807. .write_end = exofs_write_end,
  808. .releasepage = exofs_releasepage,
  809. .set_page_dirty = __set_page_dirty_nobuffers,
  810. .invalidatepage = exofs_invalidatepage,
  811. /* Not implemented Yet */
  812. .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
  813. .direct_IO = exofs_direct_IO,
  814. /* With these NULL has special meaning or default is not exported */
  815. .migratepage = NULL,
  816. .launder_page = NULL,
  817. .is_partially_uptodate = NULL,
  818. .error_remove_page = NULL,
  819. };
  820. /******************************************************************************
  821. * INODE OPERATIONS
  822. *****************************************************************************/
  823. /*
  824. * Test whether an inode is a fast symlink.
  825. */
  826. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  827. {
  828. struct exofs_i_info *oi = exofs_i(inode);
  829. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  830. }
  831. static int _do_truncate(struct inode *inode, loff_t newsize)
  832. {
  833. struct exofs_i_info *oi = exofs_i(inode);
  834. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  835. int ret;
  836. inode->i_mtime = inode->i_ctime = current_time(inode);
  837. ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
  838. if (likely(!ret))
  839. truncate_setsize(inode, newsize);
  840. EXOFS_DBGMSG2("(0x%lx) size=0x%llx ret=>%d\n",
  841. inode->i_ino, newsize, ret);
  842. return ret;
  843. }
  844. /*
  845. * Set inode attributes - update size attribute on OSD if needed,
  846. * otherwise just call generic functions.
  847. */
  848. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  849. {
  850. struct inode *inode = d_inode(dentry);
  851. int error;
  852. /* if we are about to modify an object, and it hasn't been
  853. * created yet, wait
  854. */
  855. error = wait_obj_created(exofs_i(inode));
  856. if (unlikely(error))
  857. return error;
  858. error = setattr_prepare(dentry, iattr);
  859. if (unlikely(error))
  860. return error;
  861. if ((iattr->ia_valid & ATTR_SIZE) &&
  862. iattr->ia_size != i_size_read(inode)) {
  863. error = _do_truncate(inode, iattr->ia_size);
  864. if (unlikely(error))
  865. return error;
  866. }
  867. setattr_copy(inode, iattr);
  868. mark_inode_dirty(inode);
  869. return 0;
  870. }
  871. static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
  872. EXOFS_APAGE_FS_DATA,
  873. EXOFS_ATTR_INODE_FILE_LAYOUT,
  874. 0);
  875. static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
  876. EXOFS_APAGE_FS_DATA,
  877. EXOFS_ATTR_INODE_DIR_LAYOUT,
  878. 0);
  879. /*
  880. * Read the Linux inode info from the OSD, and return it as is. In exofs the
  881. * inode info is in an application specific page/attribute of the osd-object.
  882. */
  883. static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
  884. struct exofs_fcb *inode)
  885. {
  886. struct exofs_sb_info *sbi = sb->s_fs_info;
  887. struct osd_attr attrs[] = {
  888. [0] = g_attr_inode_data,
  889. [1] = g_attr_inode_file_layout,
  890. [2] = g_attr_inode_dir_layout,
  891. };
  892. struct ore_io_state *ios;
  893. struct exofs_on_disk_inode_layout *layout;
  894. int ret;
  895. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  896. if (unlikely(ret)) {
  897. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  898. return ret;
  899. }
  900. attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  901. attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  902. ios->in_attr = attrs;
  903. ios->in_attr_len = ARRAY_SIZE(attrs);
  904. ret = ore_read(ios);
  905. if (unlikely(ret)) {
  906. EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
  907. _LLU(oi->one_comp.obj.id), ret);
  908. memset(inode, 0, sizeof(*inode));
  909. inode->i_mode = 0040000 | (0777 & ~022);
  910. /* If object is lost on target we might as well enable it's
  911. * delete.
  912. */
  913. ret = 0;
  914. goto out;
  915. }
  916. ret = extract_attr_from_ios(ios, &attrs[0]);
  917. if (ret) {
  918. EXOFS_ERR("%s: extract_attr 0 of inode failed\n", __func__);
  919. goto out;
  920. }
  921. WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
  922. memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
  923. ret = extract_attr_from_ios(ios, &attrs[1]);
  924. if (ret) {
  925. EXOFS_ERR("%s: extract_attr 1 of inode failed\n", __func__);
  926. goto out;
  927. }
  928. if (attrs[1].len) {
  929. layout = attrs[1].val_ptr;
  930. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  931. EXOFS_ERR("%s: unsupported files layout %d\n",
  932. __func__, layout->gen_func);
  933. ret = -ENOTSUPP;
  934. goto out;
  935. }
  936. }
  937. ret = extract_attr_from_ios(ios, &attrs[2]);
  938. if (ret) {
  939. EXOFS_ERR("%s: extract_attr 2 of inode failed\n", __func__);
  940. goto out;
  941. }
  942. if (attrs[2].len) {
  943. layout = attrs[2].val_ptr;
  944. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  945. EXOFS_ERR("%s: unsupported meta-data layout %d\n",
  946. __func__, layout->gen_func);
  947. ret = -ENOTSUPP;
  948. goto out;
  949. }
  950. }
  951. out:
  952. ore_put_io_state(ios);
  953. return ret;
  954. }
  955. static void __oi_init(struct exofs_i_info *oi)
  956. {
  957. init_waitqueue_head(&oi->i_wq);
  958. oi->i_flags = 0;
  959. }
  960. /*
  961. * Fill in an inode read from the OSD and set it up for use
  962. */
  963. struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
  964. {
  965. struct exofs_i_info *oi;
  966. struct exofs_fcb fcb;
  967. struct inode *inode;
  968. int ret;
  969. inode = iget_locked(sb, ino);
  970. if (!inode)
  971. return ERR_PTR(-ENOMEM);
  972. if (!(inode->i_state & I_NEW))
  973. return inode;
  974. oi = exofs_i(inode);
  975. __oi_init(oi);
  976. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  977. exofs_oi_objno(oi));
  978. /* read the inode from the osd */
  979. ret = exofs_get_inode(sb, oi, &fcb);
  980. if (ret)
  981. goto bad_inode;
  982. set_obj_created(oi);
  983. /* copy stuff from on-disk struct to in-memory struct */
  984. inode->i_mode = le16_to_cpu(fcb.i_mode);
  985. i_uid_write(inode, le32_to_cpu(fcb.i_uid));
  986. i_gid_write(inode, le32_to_cpu(fcb.i_gid));
  987. set_nlink(inode, le16_to_cpu(fcb.i_links_count));
  988. inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
  989. inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
  990. inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
  991. inode->i_ctime.tv_nsec =
  992. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  993. oi->i_commit_size = le64_to_cpu(fcb.i_size);
  994. i_size_write(inode, oi->i_commit_size);
  995. inode->i_blkbits = EXOFS_BLKSHIFT;
  996. inode->i_generation = le32_to_cpu(fcb.i_generation);
  997. oi->i_dir_start_lookup = 0;
  998. if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
  999. ret = -ESTALE;
  1000. goto bad_inode;
  1001. }
  1002. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1003. if (fcb.i_data[0])
  1004. inode->i_rdev =
  1005. old_decode_dev(le32_to_cpu(fcb.i_data[0]));
  1006. else
  1007. inode->i_rdev =
  1008. new_decode_dev(le32_to_cpu(fcb.i_data[1]));
  1009. } else {
  1010. memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
  1011. }
  1012. if (S_ISREG(inode->i_mode)) {
  1013. inode->i_op = &exofs_file_inode_operations;
  1014. inode->i_fop = &exofs_file_operations;
  1015. inode->i_mapping->a_ops = &exofs_aops;
  1016. } else if (S_ISDIR(inode->i_mode)) {
  1017. inode->i_op = &exofs_dir_inode_operations;
  1018. inode->i_fop = &exofs_dir_operations;
  1019. inode->i_mapping->a_ops = &exofs_aops;
  1020. } else if (S_ISLNK(inode->i_mode)) {
  1021. if (exofs_inode_is_fast_symlink(inode)) {
  1022. inode->i_op = &simple_symlink_inode_operations;
  1023. inode->i_link = (char *)oi->i_data;
  1024. } else {
  1025. inode->i_op = &page_symlink_inode_operations;
  1026. inode_nohighmem(inode);
  1027. inode->i_mapping->a_ops = &exofs_aops;
  1028. }
  1029. } else {
  1030. inode->i_op = &exofs_special_inode_operations;
  1031. if (fcb.i_data[0])
  1032. init_special_inode(inode, inode->i_mode,
  1033. old_decode_dev(le32_to_cpu(fcb.i_data[0])));
  1034. else
  1035. init_special_inode(inode, inode->i_mode,
  1036. new_decode_dev(le32_to_cpu(fcb.i_data[1])));
  1037. }
  1038. unlock_new_inode(inode);
  1039. return inode;
  1040. bad_inode:
  1041. iget_failed(inode);
  1042. return ERR_PTR(ret);
  1043. }
  1044. int __exofs_wait_obj_created(struct exofs_i_info *oi)
  1045. {
  1046. if (!obj_created(oi)) {
  1047. EXOFS_DBGMSG("!obj_created\n");
  1048. BUG_ON(!obj_2bcreated(oi));
  1049. wait_event(oi->i_wq, obj_created(oi));
  1050. EXOFS_DBGMSG("wait_event done\n");
  1051. }
  1052. return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
  1053. }
  1054. /*
  1055. * Callback function from exofs_new_inode(). The important thing is that we
  1056. * set the obj_created flag so that other methods know that the object exists on
  1057. * the OSD.
  1058. */
  1059. static void create_done(struct ore_io_state *ios, void *p)
  1060. {
  1061. struct inode *inode = p;
  1062. struct exofs_i_info *oi = exofs_i(inode);
  1063. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  1064. int ret;
  1065. ret = ore_check_io(ios, NULL);
  1066. ore_put_io_state(ios);
  1067. atomic_dec(&sbi->s_curr_pending);
  1068. if (unlikely(ret)) {
  1069. EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
  1070. _LLU(exofs_oi_objno(oi)),
  1071. _LLU(oi->one_comp.obj.partition));
  1072. /*TODO: When FS is corrupted creation can fail, object already
  1073. * exist. Get rid of this asynchronous creation, if exist
  1074. * increment the obj counter and try the next object. Until we
  1075. * succeed. All these dangling objects will be made into lost
  1076. * files by chkfs.exofs
  1077. */
  1078. }
  1079. set_obj_created(oi);
  1080. wake_up(&oi->i_wq);
  1081. }
  1082. /*
  1083. * Set up a new inode and create an object for it on the OSD
  1084. */
  1085. struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
  1086. {
  1087. struct super_block *sb = dir->i_sb;
  1088. struct exofs_sb_info *sbi = sb->s_fs_info;
  1089. struct inode *inode;
  1090. struct exofs_i_info *oi;
  1091. struct ore_io_state *ios;
  1092. int ret;
  1093. inode = new_inode(sb);
  1094. if (!inode)
  1095. return ERR_PTR(-ENOMEM);
  1096. oi = exofs_i(inode);
  1097. __oi_init(oi);
  1098. set_obj_2bcreated(oi);
  1099. inode_init_owner(inode, dir, mode);
  1100. inode->i_ino = sbi->s_nextid++;
  1101. inode->i_blkbits = EXOFS_BLKSHIFT;
  1102. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  1103. oi->i_commit_size = inode->i_size = 0;
  1104. spin_lock(&sbi->s_next_gen_lock);
  1105. inode->i_generation = sbi->s_next_generation++;
  1106. spin_unlock(&sbi->s_next_gen_lock);
  1107. insert_inode_hash(inode);
  1108. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  1109. exofs_oi_objno(oi));
  1110. exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
  1111. mark_inode_dirty(inode);
  1112. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1113. if (unlikely(ret)) {
  1114. EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
  1115. return ERR_PTR(ret);
  1116. }
  1117. ios->done = create_done;
  1118. ios->private = inode;
  1119. ret = ore_create(ios);
  1120. if (ret) {
  1121. ore_put_io_state(ios);
  1122. return ERR_PTR(ret);
  1123. }
  1124. atomic_inc(&sbi->s_curr_pending);
  1125. return inode;
  1126. }
  1127. /*
  1128. * struct to pass two arguments to update_inode's callback
  1129. */
  1130. struct updatei_args {
  1131. struct exofs_sb_info *sbi;
  1132. struct exofs_fcb fcb;
  1133. };
  1134. /*
  1135. * Callback function from exofs_update_inode().
  1136. */
  1137. static void updatei_done(struct ore_io_state *ios, void *p)
  1138. {
  1139. struct updatei_args *args = p;
  1140. ore_put_io_state(ios);
  1141. atomic_dec(&args->sbi->s_curr_pending);
  1142. kfree(args);
  1143. }
  1144. /*
  1145. * Write the inode to the OSD. Just fill up the struct, and set the attribute
  1146. * synchronously or asynchronously depending on the do_sync flag.
  1147. */
  1148. static int exofs_update_inode(struct inode *inode, int do_sync)
  1149. {
  1150. struct exofs_i_info *oi = exofs_i(inode);
  1151. struct super_block *sb = inode->i_sb;
  1152. struct exofs_sb_info *sbi = sb->s_fs_info;
  1153. struct ore_io_state *ios;
  1154. struct osd_attr attr;
  1155. struct exofs_fcb *fcb;
  1156. struct updatei_args *args;
  1157. int ret;
  1158. args = kzalloc(sizeof(*args), GFP_KERNEL);
  1159. if (!args) {
  1160. EXOFS_DBGMSG("Failed kzalloc of args\n");
  1161. return -ENOMEM;
  1162. }
  1163. fcb = &args->fcb;
  1164. fcb->i_mode = cpu_to_le16(inode->i_mode);
  1165. fcb->i_uid = cpu_to_le32(i_uid_read(inode));
  1166. fcb->i_gid = cpu_to_le32(i_gid_read(inode));
  1167. fcb->i_links_count = cpu_to_le16(inode->i_nlink);
  1168. fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1169. fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1170. fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1171. oi->i_commit_size = i_size_read(inode);
  1172. fcb->i_size = cpu_to_le64(oi->i_commit_size);
  1173. fcb->i_generation = cpu_to_le32(inode->i_generation);
  1174. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1175. if (old_valid_dev(inode->i_rdev)) {
  1176. fcb->i_data[0] =
  1177. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1178. fcb->i_data[1] = 0;
  1179. } else {
  1180. fcb->i_data[0] = 0;
  1181. fcb->i_data[1] =
  1182. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1183. fcb->i_data[2] = 0;
  1184. }
  1185. } else
  1186. memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
  1187. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1188. if (unlikely(ret)) {
  1189. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  1190. goto free_args;
  1191. }
  1192. attr = g_attr_inode_data;
  1193. attr.val_ptr = fcb;
  1194. ios->out_attr_len = 1;
  1195. ios->out_attr = &attr;
  1196. wait_obj_created(oi);
  1197. if (!do_sync) {
  1198. args->sbi = sbi;
  1199. ios->done = updatei_done;
  1200. ios->private = args;
  1201. }
  1202. ret = ore_write(ios);
  1203. if (!do_sync && !ret) {
  1204. atomic_inc(&sbi->s_curr_pending);
  1205. goto out; /* deallocation in updatei_done */
  1206. }
  1207. ore_put_io_state(ios);
  1208. free_args:
  1209. kfree(args);
  1210. out:
  1211. EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
  1212. inode->i_ino, do_sync, ret);
  1213. return ret;
  1214. }
  1215. int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1216. {
  1217. /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
  1218. return exofs_update_inode(inode, 1);
  1219. }
  1220. /*
  1221. * Callback function from exofs_delete_inode() - don't have much cleaning up to
  1222. * do.
  1223. */
  1224. static void delete_done(struct ore_io_state *ios, void *p)
  1225. {
  1226. struct exofs_sb_info *sbi = p;
  1227. ore_put_io_state(ios);
  1228. atomic_dec(&sbi->s_curr_pending);
  1229. }
  1230. /*
  1231. * Called when the refcount of an inode reaches zero. We remove the object
  1232. * from the OSD here. We make sure the object was created before we try and
  1233. * delete it.
  1234. */
  1235. void exofs_evict_inode(struct inode *inode)
  1236. {
  1237. struct exofs_i_info *oi = exofs_i(inode);
  1238. struct super_block *sb = inode->i_sb;
  1239. struct exofs_sb_info *sbi = sb->s_fs_info;
  1240. struct ore_io_state *ios;
  1241. int ret;
  1242. truncate_inode_pages_final(&inode->i_data);
  1243. /* TODO: should do better here */
  1244. if (inode->i_nlink || is_bad_inode(inode))
  1245. goto no_delete;
  1246. inode->i_size = 0;
  1247. clear_inode(inode);
  1248. /* if we are deleting an obj that hasn't been created yet, wait.
  1249. * This also makes sure that create_done cannot be called with an
  1250. * already evicted inode.
  1251. */
  1252. wait_obj_created(oi);
  1253. /* ignore the error, attempt a remove anyway */
  1254. /* Now Remove the OSD objects */
  1255. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1256. if (unlikely(ret)) {
  1257. EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
  1258. return;
  1259. }
  1260. ios->done = delete_done;
  1261. ios->private = sbi;
  1262. ret = ore_remove(ios);
  1263. if (ret) {
  1264. EXOFS_ERR("%s: ore_remove failed\n", __func__);
  1265. ore_put_io_state(ios);
  1266. return;
  1267. }
  1268. atomic_inc(&sbi->s_curr_pending);
  1269. return;
  1270. no_delete:
  1271. clear_inode(inode);
  1272. }