inode.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com)
  4. * Copyright (C) 2008, 2009
  5. * Boaz Harrosh <bharrosh@panasas.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 <linux/writeback.h>
  35. #include <linux/buffer_head.h>
  36. #include <scsi/scsi_device.h>
  37. #include "exofs.h"
  38. #define EXOFS_DBGMSG2(M...) do {} while (0)
  39. enum { BIO_MAX_PAGES_KMALLOC =
  40. (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
  41. MAX_PAGES_KMALLOC =
  42. PAGE_SIZE / sizeof(struct page *),
  43. };
  44. struct page_collect {
  45. struct exofs_sb_info *sbi;
  46. struct inode *inode;
  47. unsigned expected_pages;
  48. struct exofs_io_state *ios;
  49. struct page **pages;
  50. unsigned alloc_pages;
  51. unsigned nr_pages;
  52. unsigned long length;
  53. loff_t pg_first; /* keep 64bit also in 32-arches */
  54. };
  55. static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
  56. struct inode *inode)
  57. {
  58. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  59. pcol->sbi = sbi;
  60. pcol->inode = inode;
  61. pcol->expected_pages = expected_pages;
  62. pcol->ios = NULL;
  63. pcol->pages = NULL;
  64. pcol->alloc_pages = 0;
  65. pcol->nr_pages = 0;
  66. pcol->length = 0;
  67. pcol->pg_first = -1;
  68. }
  69. static void _pcol_reset(struct page_collect *pcol)
  70. {
  71. pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
  72. pcol->pages = NULL;
  73. pcol->alloc_pages = 0;
  74. pcol->nr_pages = 0;
  75. pcol->length = 0;
  76. pcol->pg_first = -1;
  77. pcol->ios = NULL;
  78. /* this is probably the end of the loop but in writes
  79. * it might not end here. don't be left with nothing
  80. */
  81. if (!pcol->expected_pages)
  82. pcol->expected_pages = MAX_PAGES_KMALLOC;
  83. }
  84. static int pcol_try_alloc(struct page_collect *pcol)
  85. {
  86. unsigned pages = min_t(unsigned, pcol->expected_pages,
  87. MAX_PAGES_KMALLOC);
  88. if (!pcol->ios) { /* First time allocate io_state */
  89. int ret = exofs_get_io_state(&pcol->sbi->layout, &pcol->ios);
  90. if (ret)
  91. return ret;
  92. }
  93. /* TODO: easily support bio chaining */
  94. pages = min_t(unsigned, pages,
  95. pcol->sbi->layout.group_width * BIO_MAX_PAGES_KMALLOC);
  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. exofs_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. static int update_read_page(struct page *page, int ret)
  127. {
  128. if (ret == 0) {
  129. /* Everything is OK */
  130. SetPageUptodate(page);
  131. if (PageError(page))
  132. ClearPageError(page);
  133. } else if (ret == -EFAULT) {
  134. /* In this case we were trying to read something that wasn't on
  135. * disk yet - return a page full of zeroes. This should be OK,
  136. * because the object should be empty (if there was a write
  137. * before this read, the read would be waiting with the page
  138. * locked */
  139. clear_highpage(page);
  140. SetPageUptodate(page);
  141. if (PageError(page))
  142. ClearPageError(page);
  143. ret = 0; /* recovered error */
  144. EXOFS_DBGMSG("recovered read error\n");
  145. } else /* Error */
  146. SetPageError(page);
  147. return ret;
  148. }
  149. static void update_write_page(struct page *page, int ret)
  150. {
  151. if (ret) {
  152. mapping_set_error(page->mapping, ret);
  153. SetPageError(page);
  154. }
  155. end_page_writeback(page);
  156. }
  157. /* Called at the end of reads, to optionally unlock pages and update their
  158. * status.
  159. */
  160. static int __readpages_done(struct page_collect *pcol, bool do_unlock)
  161. {
  162. int i;
  163. u64 resid;
  164. u64 good_bytes;
  165. u64 length = 0;
  166. int ret = exofs_check_io(pcol->ios, &resid);
  167. if (likely(!ret))
  168. good_bytes = pcol->length;
  169. else
  170. good_bytes = pcol->length - resid;
  171. EXOFS_DBGMSG2("readpages_done(0x%lx) good_bytes=0x%llx"
  172. " length=0x%lx nr_pages=%u\n",
  173. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  174. pcol->nr_pages);
  175. for (i = 0; i < pcol->nr_pages; i++) {
  176. struct page *page = pcol->pages[i];
  177. struct inode *inode = page->mapping->host;
  178. int page_stat;
  179. if (inode != pcol->inode)
  180. continue; /* osd might add more pages at end */
  181. if (likely(length < good_bytes))
  182. page_stat = 0;
  183. else
  184. page_stat = ret;
  185. EXOFS_DBGMSG2(" readpages_done(0x%lx, 0x%lx) %s\n",
  186. inode->i_ino, page->index,
  187. page_stat ? "bad_bytes" : "good_bytes");
  188. ret = update_read_page(page, page_stat);
  189. if (do_unlock)
  190. unlock_page(page);
  191. length += PAGE_SIZE;
  192. }
  193. pcol_free(pcol);
  194. EXOFS_DBGMSG2("readpages_done END\n");
  195. return ret;
  196. }
  197. /* callback of async reads */
  198. static void readpages_done(struct exofs_io_state *ios, void *p)
  199. {
  200. struct page_collect *pcol = p;
  201. __readpages_done(pcol, true);
  202. atomic_dec(&pcol->sbi->s_curr_pending);
  203. kfree(pcol);
  204. }
  205. static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
  206. {
  207. int i;
  208. for (i = 0; i < pcol->nr_pages; i++) {
  209. struct page *page = pcol->pages[i];
  210. if (rw == READ)
  211. update_read_page(page, ret);
  212. else
  213. update_write_page(page, ret);
  214. unlock_page(page);
  215. }
  216. }
  217. static int read_exec(struct page_collect *pcol, bool is_sync)
  218. {
  219. struct exofs_i_info *oi = exofs_i(pcol->inode);
  220. struct exofs_io_state *ios = pcol->ios;
  221. struct page_collect *pcol_copy = NULL;
  222. int ret;
  223. if (!pcol->pages)
  224. return 0;
  225. /* see comment in _readpage() about sync reads */
  226. WARN_ON(is_sync && (pcol->nr_pages != 1));
  227. ios->pages = pcol->pages;
  228. ios->nr_pages = pcol->nr_pages;
  229. ios->length = pcol->length;
  230. ios->offset = pcol->pg_first << PAGE_CACHE_SHIFT;
  231. if (is_sync) {
  232. exofs_oi_read(oi, pcol->ios);
  233. return __readpages_done(pcol, false);
  234. }
  235. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  236. if (!pcol_copy) {
  237. ret = -ENOMEM;
  238. goto err;
  239. }
  240. *pcol_copy = *pcol;
  241. ios->done = readpages_done;
  242. ios->private = pcol_copy;
  243. ret = exofs_oi_read(oi, ios);
  244. if (unlikely(ret))
  245. goto err;
  246. atomic_inc(&pcol->sbi->s_curr_pending);
  247. EXOFS_DBGMSG2("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
  248. ios->obj.id, _LLU(ios->offset), pcol->length);
  249. /* pages ownership was passed to pcol_copy */
  250. _pcol_reset(pcol);
  251. return 0;
  252. err:
  253. if (!is_sync)
  254. _unlock_pcol_pages(pcol, ret, READ);
  255. pcol_free(pcol);
  256. kfree(pcol_copy);
  257. return ret;
  258. }
  259. /* readpage_strip is called either directly from readpage() or by the VFS from
  260. * within read_cache_pages(), to add one more page to be read. It will try to
  261. * collect as many contiguous pages as posible. If a discontinuity is
  262. * encountered, or it runs out of resources, it will submit the previous segment
  263. * and will start a new collection. Eventually caller must submit the last
  264. * segment if present.
  265. */
  266. static int readpage_strip(void *data, struct page *page)
  267. {
  268. struct page_collect *pcol = data;
  269. struct inode *inode = pcol->inode;
  270. struct exofs_i_info *oi = exofs_i(inode);
  271. loff_t i_size = i_size_read(inode);
  272. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  273. size_t len;
  274. int ret;
  275. /* FIXME: Just for debugging, will be removed */
  276. if (PageUptodate(page))
  277. EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
  278. page->index);
  279. if (page->index < end_index)
  280. len = PAGE_CACHE_SIZE;
  281. else if (page->index == end_index)
  282. len = i_size & ~PAGE_CACHE_MASK;
  283. else
  284. len = 0;
  285. if (!len || !obj_created(oi)) {
  286. /* this will be out of bounds, or doesn't exist yet.
  287. * Current page is cleared and the request is split
  288. */
  289. clear_highpage(page);
  290. SetPageUptodate(page);
  291. if (PageError(page))
  292. ClearPageError(page);
  293. unlock_page(page);
  294. EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page,"
  295. " splitting\n", inode->i_ino, page->index);
  296. return read_exec(pcol, false);
  297. }
  298. try_again:
  299. if (unlikely(pcol->pg_first == -1)) {
  300. pcol->pg_first = page->index;
  301. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  302. page->index)) {
  303. /* Discontinuity detected, split the request */
  304. ret = read_exec(pcol, false);
  305. if (unlikely(ret))
  306. goto fail;
  307. goto try_again;
  308. }
  309. if (!pcol->pages) {
  310. ret = pcol_try_alloc(pcol);
  311. if (unlikely(ret))
  312. goto fail;
  313. }
  314. if (len != PAGE_CACHE_SIZE)
  315. zero_user(page, len, PAGE_CACHE_SIZE - len);
  316. EXOFS_DBGMSG2(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  317. inode->i_ino, page->index, len);
  318. ret = pcol_add_page(pcol, page, len);
  319. if (ret) {
  320. EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
  321. "this_len=0x%zx nr_pages=%u length=0x%lx\n",
  322. page, len, pcol->nr_pages, pcol->length);
  323. /* split the request, and start again with current page */
  324. ret = read_exec(pcol, false);
  325. if (unlikely(ret))
  326. goto fail;
  327. goto try_again;
  328. }
  329. return 0;
  330. fail:
  331. /* SetPageError(page); ??? */
  332. unlock_page(page);
  333. return ret;
  334. }
  335. static int exofs_readpages(struct file *file, struct address_space *mapping,
  336. struct list_head *pages, unsigned nr_pages)
  337. {
  338. struct page_collect pcol;
  339. int ret;
  340. _pcol_init(&pcol, nr_pages, mapping->host);
  341. ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
  342. if (ret) {
  343. EXOFS_ERR("read_cache_pages => %d\n", ret);
  344. return ret;
  345. }
  346. return read_exec(&pcol, false);
  347. }
  348. static int _readpage(struct page *page, bool is_sync)
  349. {
  350. struct page_collect pcol;
  351. int ret;
  352. _pcol_init(&pcol, 1, page->mapping->host);
  353. /* readpage_strip might call read_exec(,is_sync==false) at several
  354. * places but not if we have a single page.
  355. */
  356. ret = readpage_strip(&pcol, page);
  357. if (ret) {
  358. EXOFS_ERR("_readpage => %d\n", ret);
  359. return ret;
  360. }
  361. return read_exec(&pcol, is_sync);
  362. }
  363. /*
  364. * We don't need the file
  365. */
  366. static int exofs_readpage(struct file *file, struct page *page)
  367. {
  368. return _readpage(page, false);
  369. }
  370. /* Callback for osd_write. All writes are asynchronous */
  371. static void writepages_done(struct exofs_io_state *ios, void *p)
  372. {
  373. struct page_collect *pcol = p;
  374. int i;
  375. u64 resid;
  376. u64 good_bytes;
  377. u64 length = 0;
  378. int ret = exofs_check_io(ios, &resid);
  379. atomic_dec(&pcol->sbi->s_curr_pending);
  380. if (likely(!ret))
  381. good_bytes = pcol->length;
  382. else
  383. good_bytes = pcol->length - resid;
  384. EXOFS_DBGMSG2("writepages_done(0x%lx) good_bytes=0x%llx"
  385. " length=0x%lx nr_pages=%u\n",
  386. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  387. pcol->nr_pages);
  388. for (i = 0; i < pcol->nr_pages; i++) {
  389. struct page *page = pcol->pages[i];
  390. struct inode *inode = page->mapping->host;
  391. int page_stat;
  392. if (inode != pcol->inode)
  393. continue; /* osd might add more pages to a bio */
  394. if (likely(length < good_bytes))
  395. page_stat = 0;
  396. else
  397. page_stat = ret;
  398. update_write_page(page, page_stat);
  399. unlock_page(page);
  400. EXOFS_DBGMSG2(" writepages_done(0x%lx, 0x%lx) status=%d\n",
  401. inode->i_ino, page->index, page_stat);
  402. length += PAGE_SIZE;
  403. }
  404. pcol_free(pcol);
  405. kfree(pcol);
  406. EXOFS_DBGMSG2("writepages_done END\n");
  407. }
  408. static int write_exec(struct page_collect *pcol)
  409. {
  410. struct exofs_i_info *oi = exofs_i(pcol->inode);
  411. struct exofs_io_state *ios = pcol->ios;
  412. struct page_collect *pcol_copy = NULL;
  413. int ret;
  414. if (!pcol->pages)
  415. return 0;
  416. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  417. if (!pcol_copy) {
  418. EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
  419. ret = -ENOMEM;
  420. goto err;
  421. }
  422. *pcol_copy = *pcol;
  423. ios->pages = pcol_copy->pages;
  424. ios->nr_pages = pcol_copy->nr_pages;
  425. ios->offset = pcol_copy->pg_first << PAGE_CACHE_SHIFT;
  426. ios->length = pcol_copy->length;
  427. ios->done = writepages_done;
  428. ios->private = pcol_copy;
  429. ret = exofs_oi_write(oi, ios);
  430. if (unlikely(ret)) {
  431. EXOFS_ERR("write_exec: exofs_oi_write() Faild\n");
  432. goto err;
  433. }
  434. atomic_inc(&pcol->sbi->s_curr_pending);
  435. EXOFS_DBGMSG2("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
  436. pcol->inode->i_ino, pcol->pg_first, _LLU(ios->offset),
  437. pcol->length);
  438. /* pages ownership was passed to pcol_copy */
  439. _pcol_reset(pcol);
  440. return 0;
  441. err:
  442. _unlock_pcol_pages(pcol, ret, WRITE);
  443. pcol_free(pcol);
  444. kfree(pcol_copy);
  445. return ret;
  446. }
  447. /* writepage_strip is called either directly from writepage() or by the VFS from
  448. * within write_cache_pages(), to add one more page to be written to storage.
  449. * It will try to collect as many contiguous pages as possible. If a
  450. * discontinuity is encountered or it runs out of resources it will submit the
  451. * previous segment and will start a new collection.
  452. * Eventually caller must submit the last segment if present.
  453. */
  454. static int writepage_strip(struct page *page,
  455. struct writeback_control *wbc_unused, void *data)
  456. {
  457. struct page_collect *pcol = data;
  458. struct inode *inode = pcol->inode;
  459. struct exofs_i_info *oi = exofs_i(inode);
  460. loff_t i_size = i_size_read(inode);
  461. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  462. size_t len;
  463. int ret;
  464. BUG_ON(!PageLocked(page));
  465. ret = wait_obj_created(oi);
  466. if (unlikely(ret))
  467. goto fail;
  468. if (page->index < end_index)
  469. /* in this case, the page is within the limits of the file */
  470. len = PAGE_CACHE_SIZE;
  471. else {
  472. len = i_size & ~PAGE_CACHE_MASK;
  473. if (page->index > end_index || !len) {
  474. /* in this case, the page is outside the limits
  475. * (truncate in progress)
  476. */
  477. ret = write_exec(pcol);
  478. if (unlikely(ret))
  479. goto fail;
  480. if (PageError(page))
  481. ClearPageError(page);
  482. unlock_page(page);
  483. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
  484. "outside the limits\n",
  485. inode->i_ino, page->index);
  486. return 0;
  487. }
  488. }
  489. try_again:
  490. if (unlikely(pcol->pg_first == -1)) {
  491. pcol->pg_first = page->index;
  492. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  493. page->index)) {
  494. /* Discontinuity detected, split the request */
  495. ret = write_exec(pcol);
  496. if (unlikely(ret))
  497. goto fail;
  498. EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
  499. inode->i_ino, page->index);
  500. goto try_again;
  501. }
  502. if (!pcol->pages) {
  503. ret = pcol_try_alloc(pcol);
  504. if (unlikely(ret))
  505. goto fail;
  506. }
  507. EXOFS_DBGMSG2(" writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  508. inode->i_ino, page->index, len);
  509. ret = pcol_add_page(pcol, page, len);
  510. if (unlikely(ret)) {
  511. EXOFS_DBGMSG2("Failed pcol_add_page "
  512. "nr_pages=%u total_length=0x%lx\n",
  513. pcol->nr_pages, pcol->length);
  514. /* split the request, next loop will start again */
  515. ret = write_exec(pcol);
  516. if (unlikely(ret)) {
  517. EXOFS_DBGMSG("write_exec faild => %d", ret);
  518. goto fail;
  519. }
  520. goto try_again;
  521. }
  522. BUG_ON(PageWriteback(page));
  523. set_page_writeback(page);
  524. return 0;
  525. fail:
  526. EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
  527. inode->i_ino, page->index, ret);
  528. set_bit(AS_EIO, &page->mapping->flags);
  529. unlock_page(page);
  530. return ret;
  531. }
  532. static int exofs_writepages(struct address_space *mapping,
  533. struct writeback_control *wbc)
  534. {
  535. struct page_collect pcol;
  536. long start, end, expected_pages;
  537. int ret;
  538. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  539. end = (wbc->range_end == LLONG_MAX) ?
  540. start + mapping->nrpages :
  541. wbc->range_end >> PAGE_CACHE_SHIFT;
  542. if (start || end)
  543. expected_pages = end - start + 1;
  544. else
  545. expected_pages = mapping->nrpages;
  546. if (expected_pages < 32L)
  547. expected_pages = 32L;
  548. EXOFS_DBGMSG2("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
  549. "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
  550. mapping->host->i_ino, wbc->range_start, wbc->range_end,
  551. mapping->nrpages, start, end, expected_pages);
  552. _pcol_init(&pcol, expected_pages, mapping->host);
  553. ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
  554. if (ret) {
  555. EXOFS_ERR("write_cache_pages => %d\n", ret);
  556. return ret;
  557. }
  558. return write_exec(&pcol);
  559. }
  560. static int exofs_writepage(struct page *page, struct writeback_control *wbc)
  561. {
  562. struct page_collect pcol;
  563. int ret;
  564. _pcol_init(&pcol, 1, page->mapping->host);
  565. ret = writepage_strip(page, NULL, &pcol);
  566. if (ret) {
  567. EXOFS_ERR("exofs_writepage => %d\n", ret);
  568. return ret;
  569. }
  570. return write_exec(&pcol);
  571. }
  572. int exofs_write_begin(struct file *file, struct address_space *mapping,
  573. loff_t pos, unsigned len, unsigned flags,
  574. struct page **pagep, void **fsdata)
  575. {
  576. int ret = 0;
  577. struct page *page;
  578. page = *pagep;
  579. if (page == NULL) {
  580. ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
  581. fsdata);
  582. if (ret) {
  583. EXOFS_DBGMSG("simple_write_begin faild\n");
  584. return ret;
  585. }
  586. page = *pagep;
  587. }
  588. /* read modify write */
  589. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  590. ret = _readpage(page, true);
  591. if (ret) {
  592. /*SetPageError was done by _readpage. Is it ok?*/
  593. unlock_page(page);
  594. EXOFS_DBGMSG("__readpage_filler faild\n");
  595. }
  596. }
  597. return ret;
  598. }
  599. static int exofs_write_begin_export(struct file *file,
  600. struct address_space *mapping,
  601. loff_t pos, unsigned len, unsigned flags,
  602. struct page **pagep, void **fsdata)
  603. {
  604. *pagep = NULL;
  605. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  606. fsdata);
  607. }
  608. static int exofs_write_end(struct file *file, struct address_space *mapping,
  609. loff_t pos, unsigned len, unsigned copied,
  610. struct page *page, void *fsdata)
  611. {
  612. struct inode *inode = mapping->host;
  613. /* According to comment in simple_write_end i_mutex is held */
  614. loff_t i_size = inode->i_size;
  615. int ret;
  616. ret = simple_write_end(file, mapping,pos, len, copied, page, fsdata);
  617. if (i_size != inode->i_size)
  618. mark_inode_dirty(inode);
  619. return ret;
  620. }
  621. static int exofs_releasepage(struct page *page, gfp_t gfp)
  622. {
  623. EXOFS_DBGMSG("page 0x%lx\n", page->index);
  624. WARN_ON(1);
  625. return try_to_free_buffers(page);
  626. }
  627. static void exofs_invalidatepage(struct page *page, unsigned long offset)
  628. {
  629. EXOFS_DBGMSG("page_has_buffers=>%d\n", page_has_buffers(page));
  630. WARN_ON(1);
  631. block_invalidatepage(page, offset);
  632. }
  633. const struct address_space_operations exofs_aops = {
  634. .readpage = exofs_readpage,
  635. .readpages = exofs_readpages,
  636. .writepage = exofs_writepage,
  637. .writepages = exofs_writepages,
  638. .write_begin = exofs_write_begin_export,
  639. .write_end = exofs_write_end,
  640. .releasepage = exofs_releasepage,
  641. .set_page_dirty = __set_page_dirty_nobuffers,
  642. .invalidatepage = exofs_invalidatepage,
  643. /* Not implemented Yet */
  644. .bmap = NULL, /* TODO: use osd's OSD_ACT_READ_MAP */
  645. .direct_IO = NULL, /* TODO: Should be trivial to do */
  646. /* With these NULL has special meaning or default is not exported */
  647. .sync_page = NULL,
  648. .get_xip_mem = NULL,
  649. .migratepage = NULL,
  650. .launder_page = NULL,
  651. .is_partially_uptodate = NULL,
  652. .error_remove_page = NULL,
  653. };
  654. /******************************************************************************
  655. * INODE OPERATIONS
  656. *****************************************************************************/
  657. /*
  658. * Test whether an inode is a fast symlink.
  659. */
  660. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  661. {
  662. struct exofs_i_info *oi = exofs_i(inode);
  663. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  664. }
  665. /*
  666. * get_block_t - Fill in a buffer_head
  667. * An OSD takes care of block allocation so we just fake an allocation by
  668. * putting in the inode's sector_t in the buffer_head.
  669. * TODO: What about the case of create==0 and @iblock does not exist in the
  670. * object?
  671. */
  672. static int exofs_get_block(struct inode *inode, sector_t iblock,
  673. struct buffer_head *bh_result, int create)
  674. {
  675. map_bh(bh_result, inode->i_sb, iblock);
  676. return 0;
  677. }
  678. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  679. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  680. static int _do_truncate(struct inode *inode)
  681. {
  682. struct exofs_i_info *oi = exofs_i(inode);
  683. loff_t isize = i_size_read(inode);
  684. int ret;
  685. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  686. nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
  687. ret = exofs_oi_truncate(oi, (u64)isize);
  688. EXOFS_DBGMSG("(0x%lx) size=0x%llx\n", inode->i_ino, isize);
  689. return ret;
  690. }
  691. /*
  692. * Truncate a file to the specified size - all we have to do is set the size
  693. * attribute. We make sure the object exists first.
  694. */
  695. void exofs_truncate(struct inode *inode)
  696. {
  697. struct exofs_i_info *oi = exofs_i(inode);
  698. int ret;
  699. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  700. || S_ISLNK(inode->i_mode)))
  701. return;
  702. if (exofs_inode_is_fast_symlink(inode))
  703. return;
  704. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  705. return;
  706. /* if we are about to truncate an object, and it hasn't been
  707. * created yet, wait
  708. */
  709. if (unlikely(wait_obj_created(oi)))
  710. goto fail;
  711. ret = _do_truncate(inode);
  712. if (ret)
  713. goto fail;
  714. out:
  715. mark_inode_dirty(inode);
  716. return;
  717. fail:
  718. make_bad_inode(inode);
  719. goto out;
  720. }
  721. /*
  722. * Set inode attributes - just call generic functions.
  723. */
  724. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  725. {
  726. struct inode *inode = dentry->d_inode;
  727. int error;
  728. error = inode_change_ok(inode, iattr);
  729. if (error)
  730. return error;
  731. error = inode_setattr(inode, iattr);
  732. return error;
  733. }
  734. static const struct osd_attr g_attr_inode_file_layout = ATTR_DEF(
  735. EXOFS_APAGE_FS_DATA,
  736. EXOFS_ATTR_INODE_FILE_LAYOUT,
  737. 0);
  738. static const struct osd_attr g_attr_inode_dir_layout = ATTR_DEF(
  739. EXOFS_APAGE_FS_DATA,
  740. EXOFS_ATTR_INODE_DIR_LAYOUT,
  741. 0);
  742. /*
  743. * Read the Linux inode info from the OSD, and return it as is. In exofs the
  744. * inode info is in an application specific page/attribute of the osd-object.
  745. */
  746. static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
  747. struct exofs_fcb *inode)
  748. {
  749. struct exofs_sb_info *sbi = sb->s_fs_info;
  750. struct osd_attr attrs[] = {
  751. [0] = g_attr_inode_data,
  752. [1] = g_attr_inode_file_layout,
  753. [2] = g_attr_inode_dir_layout,
  754. };
  755. struct exofs_io_state *ios;
  756. struct exofs_on_disk_inode_layout *layout;
  757. int ret;
  758. ret = exofs_get_io_state(&sbi->layout, &ios);
  759. if (unlikely(ret)) {
  760. EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
  761. return ret;
  762. }
  763. ios->obj.id = exofs_oi_objno(oi);
  764. exofs_make_credential(oi->i_cred, &ios->obj);
  765. ios->cred = oi->i_cred;
  766. attrs[1].len = exofs_on_disk_inode_layout_size(sbi->layout.s_numdevs);
  767. attrs[2].len = exofs_on_disk_inode_layout_size(sbi->layout.s_numdevs);
  768. ios->in_attr = attrs;
  769. ios->in_attr_len = ARRAY_SIZE(attrs);
  770. ret = exofs_sbi_read(ios);
  771. if (unlikely(ret)) {
  772. EXOFS_ERR("object(0x%llx) corrupted, return empty file=>%d\n",
  773. _LLU(ios->obj.id), ret);
  774. memset(inode, 0, sizeof(*inode));
  775. inode->i_mode = 0040000 | (0777 & ~022);
  776. /* If object is lost on target we might as well enable it's
  777. * delete.
  778. */
  779. if ((ret == -ENOENT) || (ret == -EINVAL))
  780. ret = 0;
  781. goto out;
  782. }
  783. ret = extract_attr_from_ios(ios, &attrs[0]);
  784. if (ret) {
  785. EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
  786. goto out;
  787. }
  788. WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
  789. memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
  790. ret = extract_attr_from_ios(ios, &attrs[1]);
  791. if (ret) {
  792. EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
  793. goto out;
  794. }
  795. if (attrs[1].len) {
  796. layout = attrs[1].val_ptr;
  797. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  798. EXOFS_ERR("%s: unsupported files layout %d\n",
  799. __func__, layout->gen_func);
  800. ret = -ENOTSUPP;
  801. goto out;
  802. }
  803. }
  804. ret = extract_attr_from_ios(ios, &attrs[2]);
  805. if (ret) {
  806. EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
  807. goto out;
  808. }
  809. if (attrs[2].len) {
  810. layout = attrs[2].val_ptr;
  811. if (layout->gen_func != cpu_to_le16(LAYOUT_MOVING_WINDOW)) {
  812. EXOFS_ERR("%s: unsupported meta-data layout %d\n",
  813. __func__, layout->gen_func);
  814. ret = -ENOTSUPP;
  815. goto out;
  816. }
  817. }
  818. out:
  819. exofs_put_io_state(ios);
  820. return ret;
  821. }
  822. static void __oi_init(struct exofs_i_info *oi)
  823. {
  824. init_waitqueue_head(&oi->i_wq);
  825. oi->i_flags = 0;
  826. }
  827. /*
  828. * Fill in an inode read from the OSD and set it up for use
  829. */
  830. struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
  831. {
  832. struct exofs_i_info *oi;
  833. struct exofs_fcb fcb;
  834. struct inode *inode;
  835. int ret;
  836. inode = iget_locked(sb, ino);
  837. if (!inode)
  838. return ERR_PTR(-ENOMEM);
  839. if (!(inode->i_state & I_NEW))
  840. return inode;
  841. oi = exofs_i(inode);
  842. __oi_init(oi);
  843. /* read the inode from the osd */
  844. ret = exofs_get_inode(sb, oi, &fcb);
  845. if (ret)
  846. goto bad_inode;
  847. set_obj_created(oi);
  848. /* copy stuff from on-disk struct to in-memory struct */
  849. inode->i_mode = le16_to_cpu(fcb.i_mode);
  850. inode->i_uid = le32_to_cpu(fcb.i_uid);
  851. inode->i_gid = le32_to_cpu(fcb.i_gid);
  852. inode->i_nlink = le16_to_cpu(fcb.i_links_count);
  853. inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
  854. inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
  855. inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
  856. inode->i_ctime.tv_nsec =
  857. inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
  858. oi->i_commit_size = le64_to_cpu(fcb.i_size);
  859. i_size_write(inode, oi->i_commit_size);
  860. inode->i_blkbits = EXOFS_BLKSHIFT;
  861. inode->i_generation = le32_to_cpu(fcb.i_generation);
  862. oi->i_dir_start_lookup = 0;
  863. if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
  864. ret = -ESTALE;
  865. goto bad_inode;
  866. }
  867. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  868. if (fcb.i_data[0])
  869. inode->i_rdev =
  870. old_decode_dev(le32_to_cpu(fcb.i_data[0]));
  871. else
  872. inode->i_rdev =
  873. new_decode_dev(le32_to_cpu(fcb.i_data[1]));
  874. } else {
  875. memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
  876. }
  877. if (S_ISREG(inode->i_mode)) {
  878. inode->i_op = &exofs_file_inode_operations;
  879. inode->i_fop = &exofs_file_operations;
  880. inode->i_mapping->a_ops = &exofs_aops;
  881. } else if (S_ISDIR(inode->i_mode)) {
  882. inode->i_op = &exofs_dir_inode_operations;
  883. inode->i_fop = &exofs_dir_operations;
  884. inode->i_mapping->a_ops = &exofs_aops;
  885. } else if (S_ISLNK(inode->i_mode)) {
  886. if (exofs_inode_is_fast_symlink(inode))
  887. inode->i_op = &exofs_fast_symlink_inode_operations;
  888. else {
  889. inode->i_op = &exofs_symlink_inode_operations;
  890. inode->i_mapping->a_ops = &exofs_aops;
  891. }
  892. } else {
  893. inode->i_op = &exofs_special_inode_operations;
  894. if (fcb.i_data[0])
  895. init_special_inode(inode, inode->i_mode,
  896. old_decode_dev(le32_to_cpu(fcb.i_data[0])));
  897. else
  898. init_special_inode(inode, inode->i_mode,
  899. new_decode_dev(le32_to_cpu(fcb.i_data[1])));
  900. }
  901. unlock_new_inode(inode);
  902. return inode;
  903. bad_inode:
  904. iget_failed(inode);
  905. return ERR_PTR(ret);
  906. }
  907. int __exofs_wait_obj_created(struct exofs_i_info *oi)
  908. {
  909. if (!obj_created(oi)) {
  910. BUG_ON(!obj_2bcreated(oi));
  911. wait_event(oi->i_wq, obj_created(oi));
  912. }
  913. return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
  914. }
  915. /*
  916. * Callback function from exofs_new_inode(). The important thing is that we
  917. * set the obj_created flag so that other methods know that the object exists on
  918. * the OSD.
  919. */
  920. static void create_done(struct exofs_io_state *ios, void *p)
  921. {
  922. struct inode *inode = p;
  923. struct exofs_i_info *oi = exofs_i(inode);
  924. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  925. int ret;
  926. ret = exofs_check_io(ios, NULL);
  927. exofs_put_io_state(ios);
  928. atomic_dec(&sbi->s_curr_pending);
  929. if (unlikely(ret)) {
  930. EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
  931. _LLU(exofs_oi_objno(oi)), _LLU(sbi->layout.s_pid));
  932. /*TODO: When FS is corrupted creation can fail, object already
  933. * exist. Get rid of this asynchronous creation, if exist
  934. * increment the obj counter and try the next object. Until we
  935. * succeed. All these dangling objects will be made into lost
  936. * files by chkfs.exofs
  937. */
  938. }
  939. set_obj_created(oi);
  940. atomic_dec(&inode->i_count);
  941. wake_up(&oi->i_wq);
  942. }
  943. /*
  944. * Set up a new inode and create an object for it on the OSD
  945. */
  946. struct inode *exofs_new_inode(struct inode *dir, int mode)
  947. {
  948. struct super_block *sb;
  949. struct inode *inode;
  950. struct exofs_i_info *oi;
  951. struct exofs_sb_info *sbi;
  952. struct exofs_io_state *ios;
  953. int ret;
  954. sb = dir->i_sb;
  955. inode = new_inode(sb);
  956. if (!inode)
  957. return ERR_PTR(-ENOMEM);
  958. oi = exofs_i(inode);
  959. __oi_init(oi);
  960. set_obj_2bcreated(oi);
  961. sbi = sb->s_fs_info;
  962. sb->s_dirt = 1;
  963. inode_init_owner(inode, dir, mode);
  964. inode->i_ino = sbi->s_nextid++;
  965. inode->i_blkbits = EXOFS_BLKSHIFT;
  966. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  967. oi->i_commit_size = inode->i_size = 0;
  968. spin_lock(&sbi->s_next_gen_lock);
  969. inode->i_generation = sbi->s_next_generation++;
  970. spin_unlock(&sbi->s_next_gen_lock);
  971. insert_inode_hash(inode);
  972. mark_inode_dirty(inode);
  973. ret = exofs_get_io_state(&sbi->layout, &ios);
  974. if (unlikely(ret)) {
  975. EXOFS_ERR("exofs_new_inode: exofs_get_io_state failed\n");
  976. return ERR_PTR(ret);
  977. }
  978. ios->obj.id = exofs_oi_objno(oi);
  979. exofs_make_credential(oi->i_cred, &ios->obj);
  980. /* increment the refcount so that the inode will still be around when we
  981. * reach the callback
  982. */
  983. atomic_inc(&inode->i_count);
  984. ios->done = create_done;
  985. ios->private = inode;
  986. ios->cred = oi->i_cred;
  987. ret = exofs_sbi_create(ios);
  988. if (ret) {
  989. atomic_dec(&inode->i_count);
  990. exofs_put_io_state(ios);
  991. return ERR_PTR(ret);
  992. }
  993. atomic_inc(&sbi->s_curr_pending);
  994. return inode;
  995. }
  996. /*
  997. * struct to pass two arguments to update_inode's callback
  998. */
  999. struct updatei_args {
  1000. struct exofs_sb_info *sbi;
  1001. struct exofs_fcb fcb;
  1002. };
  1003. /*
  1004. * Callback function from exofs_update_inode().
  1005. */
  1006. static void updatei_done(struct exofs_io_state *ios, void *p)
  1007. {
  1008. struct updatei_args *args = p;
  1009. exofs_put_io_state(ios);
  1010. atomic_dec(&args->sbi->s_curr_pending);
  1011. kfree(args);
  1012. }
  1013. /*
  1014. * Write the inode to the OSD. Just fill up the struct, and set the attribute
  1015. * synchronously or asynchronously depending on the do_sync flag.
  1016. */
  1017. static int exofs_update_inode(struct inode *inode, int do_sync)
  1018. {
  1019. struct exofs_i_info *oi = exofs_i(inode);
  1020. struct super_block *sb = inode->i_sb;
  1021. struct exofs_sb_info *sbi = sb->s_fs_info;
  1022. struct exofs_io_state *ios;
  1023. struct osd_attr attr;
  1024. struct exofs_fcb *fcb;
  1025. struct updatei_args *args;
  1026. int ret;
  1027. args = kzalloc(sizeof(*args), GFP_KERNEL);
  1028. if (!args) {
  1029. EXOFS_DBGMSG("Faild kzalloc of args\n");
  1030. return -ENOMEM;
  1031. }
  1032. fcb = &args->fcb;
  1033. fcb->i_mode = cpu_to_le16(inode->i_mode);
  1034. fcb->i_uid = cpu_to_le32(inode->i_uid);
  1035. fcb->i_gid = cpu_to_le32(inode->i_gid);
  1036. fcb->i_links_count = cpu_to_le16(inode->i_nlink);
  1037. fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
  1038. fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
  1039. fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
  1040. oi->i_commit_size = i_size_read(inode);
  1041. fcb->i_size = cpu_to_le64(oi->i_commit_size);
  1042. fcb->i_generation = cpu_to_le32(inode->i_generation);
  1043. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1044. if (old_valid_dev(inode->i_rdev)) {
  1045. fcb->i_data[0] =
  1046. cpu_to_le32(old_encode_dev(inode->i_rdev));
  1047. fcb->i_data[1] = 0;
  1048. } else {
  1049. fcb->i_data[0] = 0;
  1050. fcb->i_data[1] =
  1051. cpu_to_le32(new_encode_dev(inode->i_rdev));
  1052. fcb->i_data[2] = 0;
  1053. }
  1054. } else
  1055. memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
  1056. ret = exofs_get_io_state(&sbi->layout, &ios);
  1057. if (unlikely(ret)) {
  1058. EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
  1059. goto free_args;
  1060. }
  1061. attr = g_attr_inode_data;
  1062. attr.val_ptr = fcb;
  1063. ios->out_attr_len = 1;
  1064. ios->out_attr = &attr;
  1065. if (!obj_created(oi)) {
  1066. EXOFS_DBGMSG("!obj_created\n");
  1067. BUG_ON(!obj_2bcreated(oi));
  1068. wait_event(oi->i_wq, obj_created(oi));
  1069. EXOFS_DBGMSG("wait_event done\n");
  1070. }
  1071. if (!do_sync) {
  1072. args->sbi = sbi;
  1073. ios->done = updatei_done;
  1074. ios->private = args;
  1075. }
  1076. ret = exofs_oi_write(oi, ios);
  1077. if (!do_sync && !ret) {
  1078. atomic_inc(&sbi->s_curr_pending);
  1079. goto out; /* deallocation in updatei_done */
  1080. }
  1081. exofs_put_io_state(ios);
  1082. free_args:
  1083. kfree(args);
  1084. out:
  1085. EXOFS_DBGMSG("(0x%lx) do_sync=%d ret=>%d\n",
  1086. inode->i_ino, do_sync, ret);
  1087. return ret;
  1088. }
  1089. int exofs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1090. {
  1091. return exofs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1092. }
  1093. /*
  1094. * Callback function from exofs_delete_inode() - don't have much cleaning up to
  1095. * do.
  1096. */
  1097. static void delete_done(struct exofs_io_state *ios, void *p)
  1098. {
  1099. struct exofs_sb_info *sbi = p;
  1100. exofs_put_io_state(ios);
  1101. atomic_dec(&sbi->s_curr_pending);
  1102. }
  1103. /*
  1104. * Called when the refcount of an inode reaches zero. We remove the object
  1105. * from the OSD here. We make sure the object was created before we try and
  1106. * delete it.
  1107. */
  1108. void exofs_delete_inode(struct inode *inode)
  1109. {
  1110. struct exofs_i_info *oi = exofs_i(inode);
  1111. struct super_block *sb = inode->i_sb;
  1112. struct exofs_sb_info *sbi = sb->s_fs_info;
  1113. struct exofs_io_state *ios;
  1114. int ret;
  1115. truncate_inode_pages(&inode->i_data, 0);
  1116. if (is_bad_inode(inode))
  1117. goto no_delete;
  1118. mark_inode_dirty(inode);
  1119. exofs_update_inode(inode, inode_needs_sync(inode));
  1120. inode->i_size = 0;
  1121. if (inode->i_blocks)
  1122. exofs_truncate(inode);
  1123. clear_inode(inode);
  1124. ret = exofs_get_io_state(&sbi->layout, &ios);
  1125. if (unlikely(ret)) {
  1126. EXOFS_ERR("%s: exofs_get_io_state failed\n", __func__);
  1127. return;
  1128. }
  1129. /* if we are deleting an obj that hasn't been created yet, wait */
  1130. if (!obj_created(oi)) {
  1131. BUG_ON(!obj_2bcreated(oi));
  1132. wait_event(oi->i_wq, obj_created(oi));
  1133. }
  1134. ios->obj.id = exofs_oi_objno(oi);
  1135. ios->done = delete_done;
  1136. ios->private = sbi;
  1137. ios->cred = oi->i_cred;
  1138. ret = exofs_sbi_remove(ios);
  1139. if (ret) {
  1140. EXOFS_ERR("%s: exofs_sbi_remove failed\n", __func__);
  1141. exofs_put_io_state(ios);
  1142. return;
  1143. }
  1144. atomic_inc(&sbi->s_curr_pending);
  1145. return;
  1146. no_delete:
  1147. clear_inode(inode);
  1148. }