inode.c 31 KB

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