inode.c 33 KB

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