inode.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <ooo@electrozaur.com>
  6. *
  7. * Copyrights for code taken from ext2:
  8. * Copyright (C) 1992, 1993, 1994, 1995
  9. * Remy Card (card@masi.ibp.fr)
  10. * Laboratoire MASI - Institut Blaise Pascal
  11. * Universite Pierre et Marie Curie (Paris VI)
  12. * from
  13. * linux/fs/minix/inode.c
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * This file is part of exofs.
  17. *
  18. * exofs is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation. Since it is based on ext2, and the only
  21. * valid version of GPL for the Linux kernel is version 2, the only valid
  22. * version of GPL for exofs is version 2.
  23. *
  24. * exofs is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with exofs; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  32. */
  33. #include <linux/slab.h>
  34. #include "exofs.h"
  35. #define EXOFS_DBGMSG2(M...) do {} while (0)
  36. unsigned exofs_max_io_pages(struct ore_layout *layout,
  37. unsigned expected_pages)
  38. {
  39. unsigned pages = min_t(unsigned, expected_pages,
  40. layout->max_io_length / PAGE_SIZE);
  41. return pages;
  42. }
  43. struct page_collect {
  44. struct exofs_sb_info *sbi;
  45. struct inode *inode;
  46. unsigned expected_pages;
  47. struct ore_io_state *ios;
  48. struct page **pages;
  49. unsigned alloc_pages;
  50. unsigned nr_pages;
  51. unsigned long length;
  52. loff_t pg_first; /* keep 64bit also in 32-arches */
  53. bool read_4_write; /* This means two things: that the read is sync
  54. * And the pages should not be unlocked.
  55. */
  56. struct page *that_locked_page;
  57. };
  58. static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
  59. struct inode *inode)
  60. {
  61. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  62. pcol->sbi = sbi;
  63. pcol->inode = inode;
  64. pcol->expected_pages = expected_pages;
  65. pcol->ios = NULL;
  66. pcol->pages = NULL;
  67. pcol->alloc_pages = 0;
  68. pcol->nr_pages = 0;
  69. pcol->length = 0;
  70. pcol->pg_first = -1;
  71. pcol->read_4_write = false;
  72. pcol->that_locked_page = NULL;
  73. }
  74. static void _pcol_reset(struct page_collect *pcol)
  75. {
  76. pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
  77. pcol->pages = NULL;
  78. pcol->alloc_pages = 0;
  79. pcol->nr_pages = 0;
  80. pcol->length = 0;
  81. pcol->pg_first = -1;
  82. pcol->ios = NULL;
  83. pcol->that_locked_page = NULL;
  84. /* this is probably the end of the loop but in writes
  85. * it might not end here. don't be left with nothing
  86. */
  87. if (!pcol->expected_pages)
  88. pcol->expected_pages =
  89. exofs_max_io_pages(&pcol->sbi->layout, ~0);
  90. }
  91. static int pcol_try_alloc(struct page_collect *pcol)
  92. {
  93. unsigned pages;
  94. /* TODO: easily support bio chaining */
  95. pages = exofs_max_io_pages(&pcol->sbi->layout, pcol->expected_pages);
  96. for (; pages; pages >>= 1) {
  97. pcol->pages = kmalloc(pages * sizeof(struct page *),
  98. GFP_KERNEL);
  99. if (likely(pcol->pages)) {
  100. pcol->alloc_pages = pages;
  101. return 0;
  102. }
  103. }
  104. EXOFS_ERR("Failed to kmalloc expected_pages=%u\n",
  105. pcol->expected_pages);
  106. return -ENOMEM;
  107. }
  108. static void pcol_free(struct page_collect *pcol)
  109. {
  110. kfree(pcol->pages);
  111. pcol->pages = NULL;
  112. if (pcol->ios) {
  113. ore_put_io_state(pcol->ios);
  114. pcol->ios = NULL;
  115. }
  116. }
  117. static int pcol_add_page(struct page_collect *pcol, struct page *page,
  118. unsigned len)
  119. {
  120. if (unlikely(pcol->nr_pages >= pcol->alloc_pages))
  121. return -ENOMEM;
  122. pcol->pages[pcol->nr_pages++] = page;
  123. pcol->length += len;
  124. return 0;
  125. }
  126. enum {PAGE_WAS_NOT_IN_IO = 17};
  127. static int update_read_page(struct page *page, int ret)
  128. {
  129. switch (ret) {
  130. case 0:
  131. /* Everything is OK */
  132. SetPageUptodate(page);
  133. if (PageError(page))
  134. ClearPageError(page);
  135. break;
  136. case -EFAULT:
  137. /* In this case we were trying to read something that wasn't on
  138. * disk yet - return a page full of zeroes. This should be OK,
  139. * because the object should be empty (if there was a write
  140. * before this read, the read would be waiting with the page
  141. * locked */
  142. clear_highpage(page);
  143. SetPageUptodate(page);
  144. if (PageError(page))
  145. ClearPageError(page);
  146. EXOFS_DBGMSG("recovered read error\n");
  147. /* fall through */
  148. case PAGE_WAS_NOT_IN_IO:
  149. ret = 0; /* recovered error */
  150. break;
  151. default:
  152. SetPageError(page);
  153. }
  154. return ret;
  155. }
  156. static void update_write_page(struct page *page, int ret)
  157. {
  158. if (unlikely(ret == PAGE_WAS_NOT_IN_IO))
  159. return; /* don't pass start don't collect $200 */
  160. if (ret) {
  161. mapping_set_error(page->mapping, ret);
  162. SetPageError(page);
  163. }
  164. end_page_writeback(page);
  165. }
  166. /* Called at the end of reads, to optionally unlock pages and update their
  167. * status.
  168. */
  169. static int __readpages_done(struct page_collect *pcol)
  170. {
  171. int i;
  172. u64 good_bytes;
  173. u64 length = 0;
  174. int ret = ore_check_io(pcol->ios, NULL);
  175. if (likely(!ret)) {
  176. good_bytes = pcol->length;
  177. ret = PAGE_WAS_NOT_IN_IO;
  178. } else {
  179. good_bytes = 0;
  180. }
  181. EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
  182. " length=0x%lx nr_pages=%u\n",
  183. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  184. pcol->nr_pages);
  185. for (i = 0; i < pcol->nr_pages; i++) {
  186. struct page *page = pcol->pages[i];
  187. struct inode *inode = page->mapping->host;
  188. int page_stat;
  189. if (inode != pcol->inode)
  190. continue; /* osd might add more pages at end */
  191. if (likely(length < good_bytes))
  192. page_stat = 0;
  193. else
  194. page_stat = ret;
  195. EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n",
  196. inode->i_ino, page->index,
  197. page_stat ? "bad_bytes" : "good_bytes");
  198. ret = update_read_page(page, page_stat);
  199. if (!pcol->read_4_write)
  200. unlock_page(page);
  201. length += PAGE_SIZE;
  202. }
  203. pcol_free(pcol);
  204. EXOFS_DBGMSG2("readpages_done END\n");
  205. return ret;
  206. }
  207. /* callback of async reads */
  208. static void readpages_done(struct ore_io_state *ios, void *p)
  209. {
  210. struct page_collect *pcol = p;
  211. __readpages_done(pcol);
  212. atomic_dec(&pcol->sbi->s_curr_pending);
  213. kfree(pcol);
  214. }
  215. static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
  216. {
  217. int i;
  218. for (i = 0; i < pcol->nr_pages; i++) {
  219. struct page *page = pcol->pages[i];
  220. if (rw == READ)
  221. update_read_page(page, ret);
  222. else
  223. update_write_page(page, ret);
  224. unlock_page(page);
  225. }
  226. }
  227. static int _maybe_not_all_in_one_io(struct ore_io_state *ios,
  228. struct page_collect *pcol_src, struct page_collect *pcol)
  229. {
  230. /* length was wrong or offset was not page aligned */
  231. BUG_ON(pcol_src->nr_pages < ios->nr_pages);
  232. if (pcol_src->nr_pages > ios->nr_pages) {
  233. struct page **src_page;
  234. unsigned pages_less = pcol_src->nr_pages - ios->nr_pages;
  235. unsigned long len_less = pcol_src->length - ios->length;
  236. unsigned i;
  237. int ret;
  238. /* This IO was trimmed */
  239. pcol_src->nr_pages = ios->nr_pages;
  240. pcol_src->length = ios->length;
  241. /* Left over pages are passed to the next io */
  242. pcol->expected_pages += pages_less;
  243. pcol->nr_pages = pages_less;
  244. pcol->length = len_less;
  245. src_page = pcol_src->pages + pcol_src->nr_pages;
  246. pcol->pg_first = (*src_page)->index;
  247. ret = pcol_try_alloc(pcol);
  248. if (unlikely(ret))
  249. return ret;
  250. for (i = 0; i < pages_less; ++i)
  251. pcol->pages[i] = *src_page++;
  252. EXOFS_DBGMSG("Length was adjusted nr_pages=0x%x "
  253. "pages_less=0x%x expected_pages=0x%x "
  254. "next_offset=0x%llx next_len=0x%lx\n",
  255. pcol_src->nr_pages, pages_less, pcol->expected_pages,
  256. pcol->pg_first * PAGE_SIZE, pcol->length);
  257. }
  258. return 0;
  259. }
  260. static int read_exec(struct page_collect *pcol)
  261. {
  262. struct exofs_i_info *oi = exofs_i(pcol->inode);
  263. struct ore_io_state *ios;
  264. struct page_collect *pcol_copy = NULL;
  265. int ret;
  266. if (!pcol->pages)
  267. return 0;
  268. if (!pcol->ios) {
  269. int ret = ore_get_rw_state(&pcol->sbi->layout, &oi->oc, true,
  270. pcol->pg_first << PAGE_CACHE_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_CACHE_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_CACHE_SIZE;
  333. else if (page->index == end_index)
  334. len = i_size & ~PAGE_CACHE_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_CACHE_SIZE)
  370. zero_user(page, len, PAGE_CACHE_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. page_cache_release(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_CACHE_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_CACHE_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_CACHE_SIZE;
  586. else {
  587. len = i_size & ~PAGE_CACHE_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. set_bit(AS_EIO, &page->mapping->flags);
  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_CACHE_SHIFT;
  654. end = (wbc->range_end == LLONG_MAX) ?
  655. start + mapping->nrpages :
  656. wbc->range_end >> PAGE_CACHE_SHIFT;
  657. if (start || end)
  658. expected_pages = end - start + 1;
  659. else
  660. expected_pages = mapping->nrpages;
  661. if (expected_pages < 32L)
  662. expected_pages = 32L;
  663. EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
  664. "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
  665. mapping->host->i_ino, wbc->range_start, wbc->range_end,
  666. mapping->nrpages, start, end, expected_pages);
  667. _pcol_init(&pcol, expected_pages, mapping->host);
  668. ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
  669. if (unlikely(ret)) {
  670. EXOFS_ERR("write_cache_pages => %d\n", ret);
  671. return ret;
  672. }
  673. ret = write_exec(&pcol);
  674. if (unlikely(ret))
  675. return ret;
  676. if (wbc->sync_mode == WB_SYNC_ALL) {
  677. return write_exec(&pcol); /* pump the last reminder */
  678. } else if (pcol.nr_pages) {
  679. /* not SYNC let the reminder join the next writeout */
  680. unsigned i;
  681. for (i = 0; i < pcol.nr_pages; i++) {
  682. struct page *page = pcol.pages[i];
  683. end_page_writeback(page);
  684. set_page_dirty(page);
  685. unlock_page(page);
  686. }
  687. }
  688. return 0;
  689. }
  690. /*
  691. static int exofs_writepage(struct page *page, struct writeback_control *wbc)
  692. {
  693. struct page_collect pcol;
  694. int ret;
  695. _pcol_init(&pcol, 1, page->mapping->host);
  696. ret = writepage_strip(page, NULL, &pcol);
  697. if (ret) {
  698. EXOFS_ERR("exofs_writepage => %d\n", ret);
  699. return ret;
  700. }
  701. return write_exec(&pcol);
  702. }
  703. */
  704. /* i_mutex held using inode->i_size directly */
  705. static void _write_failed(struct inode *inode, loff_t to)
  706. {
  707. if (to > inode->i_size)
  708. truncate_pagecache(inode, inode->i_size);
  709. }
  710. int exofs_write_begin(struct file *file, struct address_space *mapping,
  711. loff_t pos, unsigned len, unsigned flags,
  712. struct page **pagep, void **fsdata)
  713. {
  714. int ret = 0;
  715. struct page *page;
  716. page = *pagep;
  717. if (page == NULL) {
  718. ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
  719. fsdata);
  720. if (ret) {
  721. EXOFS_DBGMSG("simple_write_begin failed\n");
  722. goto out;
  723. }
  724. page = *pagep;
  725. }
  726. /* read modify write */
  727. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  728. loff_t i_size = i_size_read(mapping->host);
  729. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  730. size_t rlen;
  731. if (page->index < end_index)
  732. rlen = PAGE_CACHE_SIZE;
  733. else if (page->index == end_index)
  734. rlen = i_size & ~PAGE_CACHE_MASK;
  735. else
  736. rlen = 0;
  737. if (!rlen) {
  738. clear_highpage(page);
  739. SetPageUptodate(page);
  740. goto out;
  741. }
  742. ret = _readpage(page, true);
  743. if (ret) {
  744. /*SetPageError was done by _readpage. Is it ok?*/
  745. unlock_page(page);
  746. EXOFS_DBGMSG("__readpage failed\n");
  747. }
  748. }
  749. out:
  750. if (unlikely(ret))
  751. _write_failed(mapping->host, pos + len);
  752. return ret;
  753. }
  754. static int exofs_write_begin_export(struct file *file,
  755. struct address_space *mapping,
  756. loff_t pos, unsigned len, unsigned flags,
  757. struct page **pagep, void **fsdata)
  758. {
  759. *pagep = NULL;
  760. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  761. fsdata);
  762. }
  763. static int exofs_write_end(struct file *file, struct address_space *mapping,
  764. loff_t pos, unsigned len, unsigned copied,
  765. struct page *page, void *fsdata)
  766. {
  767. struct inode *inode = mapping->host;
  768. /* According to comment in simple_write_end i_mutex is held */
  769. loff_t i_size = inode->i_size;
  770. int ret;
  771. ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
  772. if (unlikely(ret))
  773. _write_failed(inode, pos + len);
  774. /* TODO: once simple_write_end marks inode dirty remove */
  775. if (i_size != inode->i_size)
  776. mark_inode_dirty(inode);
  777. return ret;
  778. }
  779. static int exofs_releasepage(struct page *page, gfp_t gfp)
  780. {
  781. EXOFS_DBGMSG("page 0x%lx\n", page->index);
  782. WARN_ON(1);
  783. return 0;
  784. }
  785. static void exofs_invalidatepage(struct page *page, unsigned int offset,
  786. unsigned int length)
  787. {
  788. EXOFS_DBGMSG("page 0x%lx offset 0x%x length 0x%x\n",
  789. page->index, offset, length);
  790. WARN_ON(1);
  791. }
  792. /* TODO: Should be easy enough to do proprly */
  793. static ssize_t exofs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  794. loff_t offset)
  795. {
  796. return 0;
  797. }
  798. const struct address_space_operations exofs_aops = {
  799. .readpage = exofs_readpage,
  800. .readpages = exofs_readpages,
  801. .writepage = NULL,
  802. .writepages = exofs_writepages,
  803. .write_begin = exofs_write_begin_export,
  804. .write_end = exofs_write_end,
  805. .releasepage = exofs_releasepage,
  806. .set_page_dirty = __set_page_dirty_nobuffers,
  807. .invalidatepage = exofs_invalidatepage,
  808. /* Not implemented Yet */
  809. .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
  810. .direct_IO = exofs_direct_IO,
  811. /* With these NULL has special meaning or default is not exported */
  812. .migratepage = NULL,
  813. .launder_page = NULL,
  814. .is_partially_uptodate = NULL,
  815. .error_remove_page = NULL,
  816. };
  817. /******************************************************************************
  818. * INODE OPERATIONS
  819. *****************************************************************************/
  820. /*
  821. * Test whether an inode is a fast symlink.
  822. */
  823. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  824. {
  825. struct exofs_i_info *oi = exofs_i(inode);
  826. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  827. }
  828. static int _do_truncate(struct inode *inode, loff_t newsize)
  829. {
  830. struct exofs_i_info *oi = exofs_i(inode);
  831. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  832. int ret;
  833. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  834. ret = ore_truncate(&sbi->layout, &oi->oc, (u64)newsize);
  835. if (likely(!ret))
  836. truncate_setsize(inode, newsize);
  837. EXOFS_DBGMSG2("(0x%lx) size=0x%llx ret=>%d\n",
  838. inode->i_ino, newsize, ret);
  839. return ret;
  840. }
  841. /*
  842. * Set inode attributes - update size attribute on OSD if needed,
  843. * otherwise just call generic functions.
  844. */
  845. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  846. {
  847. struct inode *inode = d_inode(dentry);
  848. int error;
  849. /* if we are about to modify an object, and it hasn't been
  850. * created yet, wait
  851. */
  852. error = wait_obj_created(exofs_i(inode));
  853. if (unlikely(error))
  854. return error;
  855. error = inode_change_ok(inode, iattr);
  856. if (unlikely(error))
  857. return error;
  858. if ((iattr->ia_valid & ATTR_SIZE) &&
  859. iattr->ia_size != i_size_read(inode)) {
  860. error = _do_truncate(inode, iattr->ia_size);
  861. if (unlikely(error))
  862. return error;
  863. }
  864. setattr_copy(inode, iattr);
  865. mark_inode_dirty(inode);
  866. return 0;
  867. }
  868. static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
  869. EXOFS_APAGE_FS_DATA,
  870. EXOFS_ATTR_INODE_FILE_LAYOUT,
  871. 0);
  872. static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
  873. EXOFS_APAGE_FS_DATA,
  874. EXOFS_ATTR_INODE_DIR_LAYOUT,
  875. 0);
  876. /*
  877. * Read the Linux inode info from the OSD, and return it as is. In exofs the
  878. * inode info is in an application specific page/attribute of the osd-object.
  879. */
  880. static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
  881. struct exofs_fcb *inode)
  882. {
  883. struct exofs_sb_info *sbi = sb->s_fs_info;
  884. struct osd_attr attrs[] = {
  885. [0] = g_attr_inode_data,
  886. [1] = g_attr_inode_file_layout,
  887. [2] = g_attr_inode_dir_layout,
  888. };
  889. struct ore_io_state *ios;
  890. struct exofs_on_disk_inode_layout *layout;
  891. int ret;
  892. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  893. if (unlikely(ret)) {
  894. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  895. return ret;
  896. }
  897. attrs[1].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  898. attrs[2].len = exofs_on_disk_inode_layout_size(sbi->oc.numdevs);
  899. ios->in_attr = attrs;
  900. ios->in_attr_len = ARRAY_SIZE(attrs);
  901. ret = ore_read(ios);
  902. if (unlikely(ret)) {
  903. EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
  904. _LLU(oi->one_comp.obj.id), ret);
  905. memset(inode, 0, sizeof(*inode));
  906. inode->i_mode = 0040000 | (0777 & ~022);
  907. /* If object is lost on target we might as well enable it's
  908. * delete.
  909. */
  910. ret = 0;
  911. goto out;
  912. }
  913. ret = extract_attr_from_ios(ios, &attrs[0]);
  914. if (ret) {
  915. EXOFS_ERR("%s: extract_attr 0 of inode failed\n", __func__);
  916. goto out;
  917. }
  918. WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
  919. memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
  920. ret = extract_attr_from_ios(ios, &attrs[1]);
  921. if (ret) {
  922. EXOFS_ERR("%s: extract_attr 1 of inode failed\n", __func__);
  923. goto out;
  924. }
  925. if (attrs[1].len) {
  926. layout = attrs[1].val_ptr;
  927. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  928. EXOFS_ERR("%s: unsupported files layout %d\n",
  929. __func__, layout->gen_func);
  930. ret = -ENOTSUPP;
  931. goto out;
  932. }
  933. }
  934. ret = extract_attr_from_ios(ios, &attrs[2]);
  935. if (ret) {
  936. EXOFS_ERR("%s: extract_attr 2 of inode failed\n", __func__);
  937. goto out;
  938. }
  939. if (attrs[2].len) {
  940. layout = attrs[2].val_ptr;
  941. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  942. EXOFS_ERR("%s: unsupported meta-data layout %d\n",
  943. __func__, layout->gen_func);
  944. ret = -ENOTSUPP;
  945. goto out;
  946. }
  947. }
  948. out:
  949. ore_put_io_state(ios);
  950. return ret;
  951. }
  952. static void __oi_init(struct exofs_i_info *oi)
  953. {
  954. init_waitqueue_head(&oi->i_wq);
  955. oi->i_flags = 0;
  956. }
  957. /*
  958. * Fill in an inode read from the OSD and set it up for use
  959. */
  960. struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
  961. {
  962. struct exofs_i_info *oi;
  963. struct exofs_fcb fcb;
  964. struct inode *inode;
  965. int ret;
  966. inode = iget_locked(sb, ino);
  967. if (!inode)
  968. return ERR_PTR(-ENOMEM);
  969. if (!(inode->i_state & I_NEW))
  970. return inode;
  971. oi = exofs_i(inode);
  972. __oi_init(oi);
  973. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  974. exofs_oi_objno(oi));
  975. /* read the inode from the osd */
  976. ret = exofs_get_inode(sb, oi, &fcb);
  977. if (ret)
  978. goto bad_inode;
  979. set_obj_created(oi);
  980. /* copy stuff from on-disk struct to in-memory struct */
  981. inode->i_mode = le16_to_cpu(fcb.i_mode);
  982. i_uid_write(inode, le32_to_cpu(fcb.i_uid));
  983. i_gid_write(inode, le32_to_cpu(fcb.i_gid));
  984. set_nlink(inode, le16_to_cpu(fcb.i_links_count));
  985. inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
  986. inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
  987. inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
  988. inode->i_ctime.tv_nsec =
  989. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  990. oi->i_commit_size = le64_to_cpu(fcb.i_size);
  991. i_size_write(inode, oi->i_commit_size);
  992. inode->i_blkbits = EXOFS_BLKSHIFT;
  993. inode->i_generation = le32_to_cpu(fcb.i_generation);
  994. oi->i_dir_start_lookup = 0;
  995. if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
  996. ret = -ESTALE;
  997. goto bad_inode;
  998. }
  999. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1000. if (fcb.i_data[0])
  1001. inode->i_rdev =
  1002. old_decode_dev(le32_to_cpu(fcb.i_data[0]));
  1003. else
  1004. inode->i_rdev =
  1005. new_decode_dev(le32_to_cpu(fcb.i_data[1]));
  1006. } else {
  1007. memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
  1008. }
  1009. if (S_ISREG(inode->i_mode)) {
  1010. inode->i_op = &exofs_file_inode_operations;
  1011. inode->i_fop = &exofs_file_operations;
  1012. inode->i_mapping->a_ops = &exofs_aops;
  1013. } else if (S_ISDIR(inode->i_mode)) {
  1014. inode->i_op = &exofs_dir_inode_operations;
  1015. inode->i_fop = &exofs_dir_operations;
  1016. inode->i_mapping->a_ops = &exofs_aops;
  1017. } else if (S_ISLNK(inode->i_mode)) {
  1018. if (exofs_inode_is_fast_symlink(inode)) {
  1019. inode->i_op = &simple_symlink_inode_operations;
  1020. inode->i_link = (char *)oi->i_data;
  1021. } else {
  1022. inode->i_op = &page_symlink_inode_operations;
  1023. inode->i_mapping->a_ops = &exofs_aops;
  1024. }
  1025. } else {
  1026. inode->i_op = &exofs_special_inode_operations;
  1027. if (fcb.i_data[0])
  1028. init_special_inode(inode, inode->i_mode,
  1029. old_decode_dev(le32_to_cpu(fcb.i_data[0])));
  1030. else
  1031. init_special_inode(inode, inode->i_mode,
  1032. new_decode_dev(le32_to_cpu(fcb.i_data[1])));
  1033. }
  1034. unlock_new_inode(inode);
  1035. return inode;
  1036. bad_inode:
  1037. iget_failed(inode);
  1038. return ERR_PTR(ret);
  1039. }
  1040. int __exofs_wait_obj_created(struct exofs_i_info *oi)
  1041. {
  1042. if (!obj_created(oi)) {
  1043. EXOFS_DBGMSG("!obj_created\n");
  1044. BUG_ON(!obj_2bcreated(oi));
  1045. wait_event(oi->i_wq, obj_created(oi));
  1046. EXOFS_DBGMSG("wait_event done\n");
  1047. }
  1048. return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
  1049. }
  1050. /*
  1051. * Callback function from exofs_new_inode(). The important thing is that we
  1052. * set the obj_created flag so that other methods know that the object exists on
  1053. * the OSD.
  1054. */
  1055. static void create_done(struct ore_io_state *ios, void *p)
  1056. {
  1057. struct inode *inode = p;
  1058. struct exofs_i_info *oi = exofs_i(inode);
  1059. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  1060. int ret;
  1061. ret = ore_check_io(ios, NULL);
  1062. ore_put_io_state(ios);
  1063. atomic_dec(&sbi->s_curr_pending);
  1064. if (unlikely(ret)) {
  1065. EXOFS_ERR("object=0x%llx creation failed in pid=0x%llx",
  1066. _LLU(exofs_oi_objno(oi)),
  1067. _LLU(oi->one_comp.obj.partition));
  1068. /*TODO: When FS is corrupted creation can fail, object already
  1069. * exist. Get rid of this asynchronous creation, if exist
  1070. * increment the obj counter and try the next object. Until we
  1071. * succeed. All these dangling objects will be made into lost
  1072. * files by chkfs.exofs
  1073. */
  1074. }
  1075. set_obj_created(oi);
  1076. wake_up(&oi->i_wq);
  1077. }
  1078. /*
  1079. * Set up a new inode and create an object for it on the OSD
  1080. */
  1081. struct inode *exofs_new_inode(struct inode *dir, umode_t mode)
  1082. {
  1083. struct super_block *sb = dir->i_sb;
  1084. struct exofs_sb_info *sbi = sb->s_fs_info;
  1085. struct inode *inode;
  1086. struct exofs_i_info *oi;
  1087. struct ore_io_state *ios;
  1088. int ret;
  1089. inode = new_inode(sb);
  1090. if (!inode)
  1091. return ERR_PTR(-ENOMEM);
  1092. oi = exofs_i(inode);
  1093. __oi_init(oi);
  1094. set_obj_2bcreated(oi);
  1095. inode_init_owner(inode, dir, mode);
  1096. inode->i_ino = sbi->s_nextid++;
  1097. inode->i_blkbits = EXOFS_BLKSHIFT;
  1098. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1099. oi->i_commit_size = inode->i_size = 0;
  1100. spin_lock(&sbi->s_next_gen_lock);
  1101. inode->i_generation = sbi->s_next_generation++;
  1102. spin_unlock(&sbi->s_next_gen_lock);
  1103. insert_inode_hash(inode);
  1104. exofs_init_comps(&oi->oc, &oi->one_comp, sb->s_fs_info,
  1105. exofs_oi_objno(oi));
  1106. exofs_sbi_write_stats(sbi); /* Make sure new sbi->s_nextid is on disk */
  1107. mark_inode_dirty(inode);
  1108. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1109. if (unlikely(ret)) {
  1110. EXOFS_ERR("exofs_new_inode: ore_get_io_state failed\n");
  1111. return ERR_PTR(ret);
  1112. }
  1113. ios->done = create_done;
  1114. ios->private = inode;
  1115. ret = ore_create(ios);
  1116. if (ret) {
  1117. ore_put_io_state(ios);
  1118. return ERR_PTR(ret);
  1119. }
  1120. atomic_inc(&sbi->s_curr_pending);
  1121. return inode;
  1122. }
  1123. /*
  1124. * struct to pass two arguments to update_inode's callback
  1125. */
  1126. struct updatei_args {
  1127. struct exofs_sb_info *sbi;
  1128. struct exofs_fcb fcb;
  1129. };
  1130. /*
  1131. * Callback function from exofs_update_inode().
  1132. */
  1133. static void updatei_done(struct ore_io_state *ios, void *p)
  1134. {
  1135. struct updatei_args *args = p;
  1136. ore_put_io_state(ios);
  1137. atomic_dec(&args->sbi->s_curr_pending);
  1138. kfree(args);
  1139. }
  1140. /*
  1141. * Write the inode to the OSD. Just fill up the struct, and set the attribute
  1142. * synchronously or asynchronously depending on the do_sync flag.
  1143. */
  1144. static int exofs_update_inode(struct inode *inode, int do_sync)
  1145. {
  1146. struct exofs_i_info *oi = exofs_i(inode);
  1147. struct super_block *sb = inode->i_sb;
  1148. struct exofs_sb_info *sbi = sb->s_fs_info;
  1149. struct ore_io_state *ios;
  1150. struct osd_attr attr;
  1151. struct exofs_fcb *fcb;
  1152. struct updatei_args *args;
  1153. int ret;
  1154. args = kzalloc(sizeof(*args), GFP_KERNEL);
  1155. if (!args) {
  1156. EXOFS_DBGMSG("Failed kzalloc of args\n");
  1157. return -ENOMEM;
  1158. }
  1159. fcb = &args->fcb;
  1160. fcb->i_mode = cpu_to_le16(inode->i_mode);
  1161. fcb->i_uid = cpu_to_le32(i_uid_read(inode));
  1162. fcb->i_gid = cpu_to_le32(i_gid_read(inode));
  1163. fcb->i_links_count = cpu_to_le16(inode->i_nlink);
  1164. fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1165. fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1166. fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1167. oi->i_commit_size = i_size_read(inode);
  1168. fcb->i_size = cpu_to_le64(oi->i_commit_size);
  1169. fcb->i_generation = cpu_to_le32(inode->i_generation);
  1170. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1171. if (old_valid_dev(inode->i_rdev)) {
  1172. fcb->i_data[0] =
  1173. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1174. fcb->i_data[1] = 0;
  1175. } else {
  1176. fcb->i_data[0] = 0;
  1177. fcb->i_data[1] =
  1178. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1179. fcb->i_data[2] = 0;
  1180. }
  1181. } else
  1182. memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
  1183. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1184. if (unlikely(ret)) {
  1185. EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
  1186. goto free_args;
  1187. }
  1188. attr = g_attr_inode_data;
  1189. attr.val_ptr = fcb;
  1190. ios->out_attr_len = 1;
  1191. ios->out_attr = &attr;
  1192. wait_obj_created(oi);
  1193. if (!do_sync) {
  1194. args->sbi = sbi;
  1195. ios->done = updatei_done;
  1196. ios->private = args;
  1197. }
  1198. ret = ore_write(ios);
  1199. if (!do_sync && !ret) {
  1200. atomic_inc(&sbi->s_curr_pending);
  1201. goto out; /* deallocation in updatei_done */
  1202. }
  1203. ore_put_io_state(ios);
  1204. free_args:
  1205. kfree(args);
  1206. out:
  1207. EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
  1208. inode->i_ino, do_sync, ret);
  1209. return ret;
  1210. }
  1211. int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1212. {
  1213. /* FIXME: fix fsync and use wbc->sync_mode == WB_SYNC_ALL */
  1214. return exofs_update_inode(inode, 1);
  1215. }
  1216. /*
  1217. * Callback function from exofs_delete_inode() - don't have much cleaning up to
  1218. * do.
  1219. */
  1220. static void delete_done(struct ore_io_state *ios, void *p)
  1221. {
  1222. struct exofs_sb_info *sbi = p;
  1223. ore_put_io_state(ios);
  1224. atomic_dec(&sbi->s_curr_pending);
  1225. }
  1226. /*
  1227. * Called when the refcount of an inode reaches zero. We remove the object
  1228. * from the OSD here. We make sure the object was created before we try and
  1229. * delete it.
  1230. */
  1231. void exofs_evict_inode(struct inode *inode)
  1232. {
  1233. struct exofs_i_info *oi = exofs_i(inode);
  1234. struct super_block *sb = inode->i_sb;
  1235. struct exofs_sb_info *sbi = sb->s_fs_info;
  1236. struct ore_io_state *ios;
  1237. int ret;
  1238. truncate_inode_pages_final(&inode->i_data);
  1239. /* TODO: should do better here */
  1240. if (inode->i_nlink || is_bad_inode(inode))
  1241. goto no_delete;
  1242. inode->i_size = 0;
  1243. clear_inode(inode);
  1244. /* if we are deleting an obj that hasn't been created yet, wait.
  1245. * This also makes sure that create_done cannot be called with an
  1246. * already evicted inode.
  1247. */
  1248. wait_obj_created(oi);
  1249. /* ignore the error, attempt a remove anyway */
  1250. /* Now Remove the OSD objects */
  1251. ret = ore_get_io_state(&sbi->layout, &oi->oc, &ios);
  1252. if (unlikely(ret)) {
  1253. EXOFS_ERR("%s: ore_get_io_state failed\n", __func__);
  1254. return;
  1255. }
  1256. ios->done = delete_done;
  1257. ios->private = sbi;
  1258. ret = ore_remove(ios);
  1259. if (ret) {
  1260. EXOFS_ERR("%s: ore_remove failed\n", __func__);
  1261. ore_put_io_state(ios);
  1262. return;
  1263. }
  1264. atomic_inc(&sbi->s_curr_pending);
  1265. return;
  1266. no_delete:
  1267. clear_inode(inode);
  1268. }