inode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * Copyright (C) 2005, 2006
  3. * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
  4. * Copyright (C) 2005, 2006
  5. * International Business Machines
  6. * Copyright (C) 2008, 2009
  7. * Boaz Harrosh <bharrosh@panasas.com>
  8. *
  9. * Copyrights for code taken from ext2:
  10. * Copyright (C) 1992, 1993, 1994, 1995
  11. * Remy Card (card@masi.ibp.fr)
  12. * Laboratoire MASI - Institut Blaise Pascal
  13. * Universite Pierre et Marie Curie (Paris VI)
  14. * from
  15. * linux/fs/minix/inode.c
  16. * Copyright (C) 1991, 1992 Linus Torvalds
  17. *
  18. * This file is part of exofs.
  19. *
  20. * exofs is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation. Since it is based on ext2, and the only
  23. * valid version of GPL for the Linux kernel is version 2, the only valid
  24. * version of GPL for exofs is version 2.
  25. *
  26. * exofs is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with exofs; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. #include <linux/writeback.h>
  36. #include <linux/buffer_head.h>
  37. #include <scsi/scsi_device.h>
  38. #include "exofs.h"
  39. #ifdef CONFIG_EXOFS_DEBUG
  40. # define EXOFS_DEBUG_OBJ_ISIZE 1
  41. #endif
  42. struct page_collect {
  43. struct exofs_sb_info *sbi;
  44. struct request_queue *req_q;
  45. struct inode *inode;
  46. unsigned expected_pages;
  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. struct request_queue *req_q = sbi->s_dev->scsi_device->request_queue;
  57. pcol->sbi = sbi;
  58. pcol->req_q = req_q;
  59. pcol->inode = inode;
  60. pcol->expected_pages = expected_pages;
  61. pcol->bio = NULL;
  62. pcol->nr_pages = 0;
  63. pcol->length = 0;
  64. pcol->pg_first = -1;
  65. EXOFS_DBGMSG("_pcol_init ino=0x%lx expected_pages=%u\n", inode->i_ino,
  66. expected_pages);
  67. }
  68. static void _pcol_reset(struct page_collect *pcol)
  69. {
  70. pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
  71. pcol->bio = NULL;
  72. pcol->nr_pages = 0;
  73. pcol->length = 0;
  74. pcol->pg_first = -1;
  75. EXOFS_DBGMSG("_pcol_reset ino=0x%lx expected_pages=%u\n",
  76. pcol->inode->i_ino, pcol->expected_pages);
  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 = 128;
  82. }
  83. static int pcol_try_alloc(struct page_collect *pcol)
  84. {
  85. int pages = min_t(unsigned, pcol->expected_pages, BIO_MAX_PAGES);
  86. for (; pages; pages >>= 1) {
  87. pcol->bio = bio_alloc(GFP_KERNEL, pages);
  88. if (likely(pcol->bio))
  89. return 0;
  90. }
  91. EXOFS_ERR("Failed to kcalloc expected_pages=%u\n",
  92. pcol->expected_pages);
  93. return -ENOMEM;
  94. }
  95. static void pcol_free(struct page_collect *pcol)
  96. {
  97. bio_put(pcol->bio);
  98. pcol->bio = NULL;
  99. }
  100. static int pcol_add_page(struct page_collect *pcol, struct page *page,
  101. unsigned len)
  102. {
  103. int added_len = bio_add_pc_page(pcol->req_q, pcol->bio, page, len, 0);
  104. if (unlikely(len != added_len))
  105. return -ENOMEM;
  106. ++pcol->nr_pages;
  107. pcol->length += len;
  108. return 0;
  109. }
  110. static int update_read_page(struct page *page, int ret)
  111. {
  112. if (ret == 0) {
  113. /* Everything is OK */
  114. SetPageUptodate(page);
  115. if (PageError(page))
  116. ClearPageError(page);
  117. } else if (ret == -EFAULT) {
  118. /* In this case we were trying to read something that wasn't on
  119. * disk yet - return a page full of zeroes. This should be OK,
  120. * because the object should be empty (if there was a write
  121. * before this read, the read would be waiting with the page
  122. * locked */
  123. clear_highpage(page);
  124. SetPageUptodate(page);
  125. if (PageError(page))
  126. ClearPageError(page);
  127. ret = 0; /* recovered error */
  128. EXOFS_DBGMSG("recovered read error\n");
  129. } else /* Error */
  130. SetPageError(page);
  131. return ret;
  132. }
  133. static void update_write_page(struct page *page, int ret)
  134. {
  135. if (ret) {
  136. mapping_set_error(page->mapping, ret);
  137. SetPageError(page);
  138. }
  139. end_page_writeback(page);
  140. }
  141. /* Called at the end of reads, to optionally unlock pages and update their
  142. * status.
  143. */
  144. static int __readpages_done(struct osd_request *or, struct page_collect *pcol,
  145. bool do_unlock)
  146. {
  147. struct bio_vec *bvec;
  148. int i;
  149. u64 resid;
  150. u64 good_bytes;
  151. u64 length = 0;
  152. int ret = exofs_check_ok_resid(or, &resid, NULL);
  153. osd_end_request(or);
  154. if (likely(!ret))
  155. good_bytes = pcol->length;
  156. else if (!resid)
  157. good_bytes = 0;
  158. else
  159. good_bytes = pcol->length - resid;
  160. EXOFS_DBGMSG("readpages_done(0x%lx) good_bytes=0x%llx"
  161. " length=0x%lx nr_pages=%u\n",
  162. pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
  163. pcol->nr_pages);
  164. __bio_for_each_segment(bvec, pcol->bio, i, 0) {
  165. struct page *page = bvec->bv_page;
  166. struct inode *inode = page->mapping->host;
  167. int page_stat;
  168. if (inode != pcol->inode)
  169. continue; /* osd might add more pages at end */
  170. if (likely(length < good_bytes))
  171. page_stat = 0;
  172. else
  173. page_stat = ret;
  174. EXOFS_DBGMSG(" readpages_done(0x%lx, 0x%lx) %s\n",
  175. inode->i_ino, page->index,
  176. page_stat ? "bad_bytes" : "good_bytes");
  177. ret = update_read_page(page, page_stat);
  178. if (do_unlock)
  179. unlock_page(page);
  180. length += bvec->bv_len;
  181. }
  182. pcol_free(pcol);
  183. EXOFS_DBGMSG("readpages_done END\n");
  184. return ret;
  185. }
  186. /* callback of async reads */
  187. static void readpages_done(struct osd_request *or, void *p)
  188. {
  189. struct page_collect *pcol = p;
  190. __readpages_done(or, pcol, true);
  191. atomic_dec(&pcol->sbi->s_curr_pending);
  192. kfree(p);
  193. }
  194. static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
  195. {
  196. struct bio_vec *bvec;
  197. int i;
  198. __bio_for_each_segment(bvec, pcol->bio, i, 0) {
  199. struct page *page = bvec->bv_page;
  200. if (rw == READ)
  201. update_read_page(page, ret);
  202. else
  203. update_write_page(page, ret);
  204. unlock_page(page);
  205. }
  206. pcol_free(pcol);
  207. }
  208. static int read_exec(struct page_collect *pcol, bool is_sync)
  209. {
  210. struct exofs_i_info *oi = exofs_i(pcol->inode);
  211. struct osd_obj_id obj = {pcol->sbi->s_pid,
  212. pcol->inode->i_ino + EXOFS_OBJ_OFF};
  213. struct osd_request *or = NULL;
  214. struct page_collect *pcol_copy = NULL;
  215. loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
  216. int ret;
  217. if (!pcol->bio)
  218. return 0;
  219. /* see comment in _readpage() about sync reads */
  220. WARN_ON(is_sync && (pcol->nr_pages != 1));
  221. or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
  222. if (unlikely(!or)) {
  223. ret = -ENOMEM;
  224. goto err;
  225. }
  226. osd_req_read(or, &obj, pcol->bio, i_start);
  227. if (is_sync) {
  228. exofs_sync_op(or, pcol->sbi->s_timeout, oi->i_cred);
  229. return __readpages_done(or, pcol, false);
  230. }
  231. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  232. if (!pcol_copy) {
  233. ret = -ENOMEM;
  234. goto err;
  235. }
  236. *pcol_copy = *pcol;
  237. ret = exofs_async_op(or, readpages_done, pcol_copy, oi->i_cred);
  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. obj.id, _LLU(i_start), 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. kfree(pcol_copy);
  250. if (or)
  251. osd_end_request(or);
  252. return ret;
  253. }
  254. /* readpage_strip is called either directly from readpage() or by the VFS from
  255. * within read_cache_pages(), to add one more page to be read. It will try to
  256. * collect as many contiguous pages as posible. If a discontinuity is
  257. * encountered, or it runs out of resources, it will submit the previous segment
  258. * and will start a new collection. Eventually caller must submit the last
  259. * segment if present.
  260. */
  261. static int readpage_strip(void *data, struct page *page)
  262. {
  263. struct page_collect *pcol = data;
  264. struct inode *inode = pcol->inode;
  265. struct exofs_i_info *oi = exofs_i(inode);
  266. loff_t i_size = i_size_read(inode);
  267. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  268. size_t len;
  269. int ret;
  270. /* FIXME: Just for debugging, will be removed */
  271. if (PageUptodate(page))
  272. EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
  273. page->index);
  274. if (page->index < end_index)
  275. len = PAGE_CACHE_SIZE;
  276. else if (page->index == end_index)
  277. len = i_size & ~PAGE_CACHE_MASK;
  278. else
  279. len = 0;
  280. if (!len || !obj_created(oi)) {
  281. /* this will be out of bounds, or doesn't exist yet.
  282. * Current page is cleared and the request is split
  283. */
  284. clear_highpage(page);
  285. SetPageUptodate(page);
  286. if (PageError(page))
  287. ClearPageError(page);
  288. unlock_page(page);
  289. EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page,"
  290. " splitting\n", inode->i_ino, page->index);
  291. return read_exec(pcol, false);
  292. }
  293. try_again:
  294. if (unlikely(pcol->pg_first == -1)) {
  295. pcol->pg_first = page->index;
  296. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  297. page->index)) {
  298. /* Discontinuity detected, split the request */
  299. ret = read_exec(pcol, false);
  300. if (unlikely(ret))
  301. goto fail;
  302. goto try_again;
  303. }
  304. if (!pcol->bio) {
  305. ret = pcol_try_alloc(pcol);
  306. if (unlikely(ret))
  307. goto fail;
  308. }
  309. if (len != PAGE_CACHE_SIZE)
  310. zero_user(page, len, PAGE_CACHE_SIZE - len);
  311. EXOFS_DBGMSG(" readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
  312. inode->i_ino, page->index, len);
  313. ret = pcol_add_page(pcol, page, len);
  314. if (ret) {
  315. EXOFS_DBGMSG("Failed pcol_add_page pages[i]=%p "
  316. "this_len=0x%zx nr_pages=%u length=0x%lx\n",
  317. page, len, pcol->nr_pages, pcol->length);
  318. /* split the request, and start again with current page */
  319. ret = read_exec(pcol, false);
  320. if (unlikely(ret))
  321. goto fail;
  322. goto try_again;
  323. }
  324. return 0;
  325. fail:
  326. /* SetPageError(page); ??? */
  327. unlock_page(page);
  328. return ret;
  329. }
  330. static int exofs_readpages(struct file *file, struct address_space *mapping,
  331. struct list_head *pages, unsigned nr_pages)
  332. {
  333. struct page_collect pcol;
  334. int ret;
  335. _pcol_init(&pcol, nr_pages, mapping->host);
  336. ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
  337. if (ret) {
  338. EXOFS_ERR("read_cache_pages => %d\n", ret);
  339. return ret;
  340. }
  341. return read_exec(&pcol, false);
  342. }
  343. static int _readpage(struct page *page, bool is_sync)
  344. {
  345. struct page_collect pcol;
  346. int ret;
  347. _pcol_init(&pcol, 1, page->mapping->host);
  348. /* readpage_strip might call read_exec(,async) inside at several places
  349. * but this is safe for is_async=0 since read_exec will not do anything
  350. * when we have a single page.
  351. */
  352. ret = readpage_strip(&pcol, page);
  353. if (ret) {
  354. EXOFS_ERR("_readpage => %d\n", ret);
  355. return ret;
  356. }
  357. return read_exec(&pcol, is_sync);
  358. }
  359. /*
  360. * We don't need the file
  361. */
  362. static int exofs_readpage(struct file *file, struct page *page)
  363. {
  364. return _readpage(page, false);
  365. }
  366. /* Callback for osd_write. All writes are asynchronouse */
  367. static void writepages_done(struct osd_request *or, void *p)
  368. {
  369. struct page_collect *pcol = p;
  370. struct bio_vec *bvec;
  371. int i;
  372. u64 resid;
  373. u64 good_bytes;
  374. u64 length = 0;
  375. int ret = exofs_check_ok_resid(or, NULL, &resid);
  376. osd_end_request(or);
  377. atomic_dec(&pcol->sbi->s_curr_pending);
  378. if (likely(!ret))
  379. good_bytes = pcol->length;
  380. else if (!resid)
  381. good_bytes = 0;
  382. else
  383. good_bytes = pcol->length - resid;
  384. EXOFS_DBGMSG("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. __bio_for_each_segment(bvec, pcol->bio, i, 0) {
  389. struct page *page = bvec->bv_page;
  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_DBGMSG(" writepages_done(0x%lx, 0x%lx) status=%d\n",
  401. inode->i_ino, page->index, page_stat);
  402. length += bvec->bv_len;
  403. }
  404. pcol_free(pcol);
  405. kfree(pcol);
  406. EXOFS_DBGMSG("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 osd_obj_id obj = {pcol->sbi->s_pid,
  412. pcol->inode->i_ino + EXOFS_OBJ_OFF};
  413. struct osd_request *or = NULL;
  414. struct page_collect *pcol_copy = NULL;
  415. loff_t i_start = pcol->pg_first << PAGE_CACHE_SHIFT;
  416. int ret;
  417. if (!pcol->bio)
  418. return 0;
  419. or = osd_start_request(pcol->sbi->s_dev, GFP_KERNEL);
  420. if (unlikely(!or)) {
  421. EXOFS_ERR("write_exec: Faild to osd_start_request()\n");
  422. ret = -ENOMEM;
  423. goto err;
  424. }
  425. pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
  426. if (!pcol_copy) {
  427. EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
  428. ret = -ENOMEM;
  429. goto err;
  430. }
  431. *pcol_copy = *pcol;
  432. osd_req_write(or, &obj, pcol_copy->bio, i_start);
  433. ret = exofs_async_op(or, writepages_done, pcol_copy, oi->i_cred);
  434. if (unlikely(ret)) {
  435. EXOFS_ERR("write_exec: exofs_async_op() Faild\n");
  436. goto err;
  437. }
  438. atomic_inc(&pcol->sbi->s_curr_pending);
  439. EXOFS_DBGMSG("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
  440. pcol->inode->i_ino, pcol->pg_first, _LLU(i_start),
  441. pcol->length);
  442. /* pages ownership was passed to pcol_copy */
  443. _pcol_reset(pcol);
  444. return 0;
  445. err:
  446. _unlock_pcol_pages(pcol, ret, WRITE);
  447. kfree(pcol_copy);
  448. if (or)
  449. osd_end_request(or);
  450. return ret;
  451. }
  452. /* writepage_strip is called either directly from writepage() or by the VFS from
  453. * within write_cache_pages(), to add one more page to be written to storage.
  454. * It will try to collect as many contiguous pages as possible. If a
  455. * discontinuity is encountered or it runs out of resources it will submit the
  456. * previous segment and will start a new collection.
  457. * Eventually caller must submit the last segment if present.
  458. */
  459. static int writepage_strip(struct page *page,
  460. struct writeback_control *wbc_unused, void *data)
  461. {
  462. struct page_collect *pcol = data;
  463. struct inode *inode = pcol->inode;
  464. struct exofs_i_info *oi = exofs_i(inode);
  465. loff_t i_size = i_size_read(inode);
  466. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  467. size_t len;
  468. int ret;
  469. BUG_ON(!PageLocked(page));
  470. ret = wait_obj_created(oi);
  471. if (unlikely(ret))
  472. goto fail;
  473. if (page->index < end_index)
  474. /* in this case, the page is within the limits of the file */
  475. len = PAGE_CACHE_SIZE;
  476. else {
  477. len = i_size & ~PAGE_CACHE_MASK;
  478. if (page->index > end_index || !len) {
  479. /* in this case, the page is outside the limits
  480. * (truncate in progress)
  481. */
  482. ret = write_exec(pcol);
  483. if (unlikely(ret))
  484. goto fail;
  485. if (PageError(page))
  486. ClearPageError(page);
  487. unlock_page(page);
  488. return 0;
  489. }
  490. }
  491. try_again:
  492. if (unlikely(pcol->pg_first == -1)) {
  493. pcol->pg_first = page->index;
  494. } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
  495. page->index)) {
  496. /* Discontinuity detected, split the request */
  497. ret = write_exec(pcol);
  498. if (unlikely(ret))
  499. goto fail;
  500. goto try_again;
  501. }
  502. if (!pcol->bio) {
  503. ret = pcol_try_alloc(pcol);
  504. if (unlikely(ret))
  505. goto fail;
  506. }
  507. EXOFS_DBGMSG(" 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_DBGMSG("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. set_bit(AS_EIO, &page->mapping->flags);
  527. unlock_page(page);
  528. return ret;
  529. }
  530. static int exofs_writepages(struct address_space *mapping,
  531. struct writeback_control *wbc)
  532. {
  533. struct page_collect pcol;
  534. long start, end, expected_pages;
  535. int ret;
  536. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  537. end = (wbc->range_end == LLONG_MAX) ?
  538. start + mapping->nrpages :
  539. wbc->range_end >> PAGE_CACHE_SHIFT;
  540. if (start || end)
  541. expected_pages = min(end - start + 1, 32L);
  542. else
  543. expected_pages = mapping->nrpages;
  544. EXOFS_DBGMSG("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx"
  545. " m->nrpages=%lu start=0x%lx end=0x%lx\n",
  546. mapping->host->i_ino, wbc->range_start, wbc->range_end,
  547. mapping->nrpages, start, end);
  548. _pcol_init(&pcol, expected_pages, mapping->host);
  549. ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
  550. if (ret) {
  551. EXOFS_ERR("write_cache_pages => %d\n", ret);
  552. return ret;
  553. }
  554. return write_exec(&pcol);
  555. }
  556. static int exofs_writepage(struct page *page, struct writeback_control *wbc)
  557. {
  558. struct page_collect pcol;
  559. int ret;
  560. _pcol_init(&pcol, 1, page->mapping->host);
  561. ret = writepage_strip(page, NULL, &pcol);
  562. if (ret) {
  563. EXOFS_ERR("exofs_writepage => %d\n", ret);
  564. return ret;
  565. }
  566. return write_exec(&pcol);
  567. }
  568. int exofs_write_begin(struct file *file, struct address_space *mapping,
  569. loff_t pos, unsigned len, unsigned flags,
  570. struct page **pagep, void **fsdata)
  571. {
  572. int ret = 0;
  573. struct page *page;
  574. page = *pagep;
  575. if (page == NULL) {
  576. ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
  577. fsdata);
  578. if (ret) {
  579. EXOFS_DBGMSG("simple_write_begin faild\n");
  580. return ret;
  581. }
  582. page = *pagep;
  583. }
  584. /* read modify write */
  585. if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
  586. ret = _readpage(page, true);
  587. if (ret) {
  588. /*SetPageError was done by _readpage. Is it ok?*/
  589. unlock_page(page);
  590. EXOFS_DBGMSG("__readpage_filler faild\n");
  591. }
  592. }
  593. return ret;
  594. }
  595. static int exofs_write_begin_export(struct file *file,
  596. struct address_space *mapping,
  597. loff_t pos, unsigned len, unsigned flags,
  598. struct page **pagep, void **fsdata)
  599. {
  600. *pagep = NULL;
  601. return exofs_write_begin(file, mapping, pos, len, flags, pagep,
  602. fsdata);
  603. }
  604. const struct address_space_operations exofs_aops = {
  605. .readpage = exofs_readpage,
  606. .readpages = exofs_readpages,
  607. .writepage = exofs_writepage,
  608. .writepages = exofs_writepages,
  609. .write_begin = exofs_write_begin_export,
  610. .write_end = simple_write_end,
  611. };
  612. /******************************************************************************
  613. * INODE OPERATIONS
  614. *****************************************************************************/
  615. /*
  616. * Test whether an inode is a fast symlink.
  617. */
  618. static inline int exofs_inode_is_fast_symlink(struct inode *inode)
  619. {
  620. struct exofs_i_info *oi = exofs_i(inode);
  621. return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
  622. }
  623. /*
  624. * get_block_t - Fill in a buffer_head
  625. * An OSD takes care of block allocation so we just fake an allocation by
  626. * putting in the inode's sector_t in the buffer_head.
  627. * TODO: What about the case of create==0 and @iblock does not exist in the
  628. * object?
  629. */
  630. static int exofs_get_block(struct inode *inode, sector_t iblock,
  631. struct buffer_head *bh_result, int create)
  632. {
  633. map_bh(bh_result, inode->i_sb, iblock);
  634. return 0;
  635. }
  636. const struct osd_attr g_attr_logical_length = ATTR_DEF(
  637. OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
  638. /*
  639. * Truncate a file to the specified size - all we have to do is set the size
  640. * attribute. We make sure the object exists first.
  641. */
  642. void exofs_truncate(struct inode *inode)
  643. {
  644. struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
  645. struct exofs_i_info *oi = exofs_i(inode);
  646. struct osd_obj_id obj = {sbi->s_pid, inode->i_ino + EXOFS_OBJ_OFF};
  647. struct osd_request *or;
  648. struct osd_attr attr;
  649. loff_t isize = i_size_read(inode);
  650. __be64 newsize;
  651. int ret;
  652. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  653. || S_ISLNK(inode->i_mode)))
  654. return;
  655. if (exofs_inode_is_fast_symlink(inode))
  656. return;
  657. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  658. return;
  659. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  660. nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
  661. or = osd_start_request(sbi->s_dev, GFP_KERNEL);
  662. if (unlikely(!or)) {
  663. EXOFS_ERR("ERROR: exofs_truncate: osd_start_request failed\n");
  664. goto fail;
  665. }
  666. osd_req_set_attributes(or, &obj);
  667. newsize = cpu_to_be64((u64)isize);
  668. attr = g_attr_logical_length;
  669. attr.val_ptr = &newsize;
  670. osd_req_add_set_attr_list(or, &attr, 1);
  671. /* if we are about to truncate an object, and it hasn't been
  672. * created yet, wait
  673. */
  674. if (unlikely(wait_obj_created(oi)))
  675. goto fail;
  676. ret = exofs_sync_op(or, sbi->s_timeout, oi->i_cred);
  677. osd_end_request(or);
  678. if (ret)
  679. goto fail;
  680. out:
  681. mark_inode_dirty(inode);
  682. return;
  683. fail:
  684. make_bad_inode(inode);
  685. goto out;
  686. }
  687. /*
  688. * Set inode attributes - just call generic functions.
  689. */
  690. int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
  691. {
  692. struct inode *inode = dentry->d_inode;
  693. int error;
  694. error = inode_change_ok(inode, iattr);
  695. if (error)
  696. return error;
  697. error = inode_setattr(inode, iattr);
  698. return error;
  699. }