addr.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/backing-dev.h>
  3. #include <linux/fs.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/writeback.h> /* generic_writepages */
  7. #include <linux/slab.h>
  8. #include <linux/pagevec.h>
  9. #include <linux/task_io_accounting_ops.h>
  10. #include "super.h"
  11. #include "mds_client.h"
  12. #include "cache.h"
  13. #include <linux/ceph/osd_client.h>
  14. /*
  15. * Ceph address space ops.
  16. *
  17. * There are a few funny things going on here.
  18. *
  19. * The page->private field is used to reference a struct
  20. * ceph_snap_context for _every_ dirty page. This indicates which
  21. * snapshot the page was logically dirtied in, and thus which snap
  22. * context needs to be associated with the osd write during writeback.
  23. *
  24. * Similarly, struct ceph_inode_info maintains a set of counters to
  25. * count dirty pages on the inode. In the absence of snapshots,
  26. * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
  27. *
  28. * When a snapshot is taken (that is, when the client receives
  29. * notification that a snapshot was taken), each inode with caps and
  30. * with dirty pages (dirty pages implies there is a cap) gets a new
  31. * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
  32. * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
  33. * moved to capsnap->dirty. (Unless a sync write is currently in
  34. * progress. In that case, the capsnap is said to be "pending", new
  35. * writes cannot start, and the capsnap isn't "finalized" until the
  36. * write completes (or fails) and a final size/mtime for the inode for
  37. * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
  38. *
  39. * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
  40. * we look for the first capsnap in i_cap_snaps and write out pages in
  41. * that snap context _only_. Then we move on to the next capsnap,
  42. * eventually reaching the "live" or "head" context (i.e., pages that
  43. * are not yet snapped) and are writing the most recently dirtied
  44. * pages.
  45. *
  46. * Invalidate and so forth must take care to ensure the dirty page
  47. * accounting is preserved.
  48. */
  49. #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
  50. #define CONGESTION_OFF_THRESH(congestion_kb) \
  51. (CONGESTION_ON_THRESH(congestion_kb) - \
  52. (CONGESTION_ON_THRESH(congestion_kb) >> 2))
  53. static inline struct ceph_snap_context *page_snap_context(struct page *page)
  54. {
  55. if (PagePrivate(page))
  56. return (void *)page->private;
  57. return NULL;
  58. }
  59. /*
  60. * Dirty a page. Optimistically adjust accounting, on the assumption
  61. * that we won't race with invalidate. If we do, readjust.
  62. */
  63. static int ceph_set_page_dirty(struct page *page)
  64. {
  65. struct address_space *mapping = page->mapping;
  66. struct inode *inode;
  67. struct ceph_inode_info *ci;
  68. struct ceph_snap_context *snapc;
  69. int ret;
  70. if (unlikely(!mapping))
  71. return !TestSetPageDirty(page);
  72. if (PageDirty(page)) {
  73. dout("%p set_page_dirty %p idx %lu -- already dirty\n",
  74. mapping->host, page, page->index);
  75. BUG_ON(!PagePrivate(page));
  76. return 0;
  77. }
  78. inode = mapping->host;
  79. ci = ceph_inode(inode);
  80. /* dirty the head */
  81. spin_lock(&ci->i_ceph_lock);
  82. BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
  83. if (__ceph_have_pending_cap_snap(ci)) {
  84. struct ceph_cap_snap *capsnap =
  85. list_last_entry(&ci->i_cap_snaps,
  86. struct ceph_cap_snap,
  87. ci_item);
  88. snapc = ceph_get_snap_context(capsnap->context);
  89. capsnap->dirty_pages++;
  90. } else {
  91. BUG_ON(!ci->i_head_snapc);
  92. snapc = ceph_get_snap_context(ci->i_head_snapc);
  93. ++ci->i_wrbuffer_ref_head;
  94. }
  95. if (ci->i_wrbuffer_ref == 0)
  96. ihold(inode);
  97. ++ci->i_wrbuffer_ref;
  98. dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
  99. "snapc %p seq %lld (%d snaps)\n",
  100. mapping->host, page, page->index,
  101. ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
  102. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  103. snapc, snapc->seq, snapc->num_snaps);
  104. spin_unlock(&ci->i_ceph_lock);
  105. /*
  106. * Reference snap context in page->private. Also set
  107. * PagePrivate so that we get invalidatepage callback.
  108. */
  109. BUG_ON(PagePrivate(page));
  110. page->private = (unsigned long)snapc;
  111. SetPagePrivate(page);
  112. ret = __set_page_dirty_nobuffers(page);
  113. WARN_ON(!PageLocked(page));
  114. WARN_ON(!page->mapping);
  115. return ret;
  116. }
  117. /*
  118. * If we are truncating the full page (i.e. offset == 0), adjust the
  119. * dirty page counters appropriately. Only called if there is private
  120. * data on the page.
  121. */
  122. static void ceph_invalidatepage(struct page *page, unsigned int offset,
  123. unsigned int length)
  124. {
  125. struct inode *inode;
  126. struct ceph_inode_info *ci;
  127. struct ceph_snap_context *snapc = page_snap_context(page);
  128. inode = page->mapping->host;
  129. ci = ceph_inode(inode);
  130. if (offset != 0 || length != PAGE_CACHE_SIZE) {
  131. dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
  132. inode, page, page->index, offset, length);
  133. return;
  134. }
  135. ceph_invalidate_fscache_page(inode, page);
  136. if (!PagePrivate(page))
  137. return;
  138. /*
  139. * We can get non-dirty pages here due to races between
  140. * set_page_dirty and truncate_complete_page; just spit out a
  141. * warning, in case we end up with accounting problems later.
  142. */
  143. if (!PageDirty(page))
  144. pr_err("%p invalidatepage %p page not dirty\n", inode, page);
  145. ClearPageChecked(page);
  146. dout("%p invalidatepage %p idx %lu full dirty page\n",
  147. inode, page, page->index);
  148. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  149. ceph_put_snap_context(snapc);
  150. page->private = 0;
  151. ClearPagePrivate(page);
  152. }
  153. static int ceph_releasepage(struct page *page, gfp_t g)
  154. {
  155. struct inode *inode = page->mapping ? page->mapping->host : NULL;
  156. dout("%p releasepage %p idx %lu\n", inode, page, page->index);
  157. WARN_ON(PageDirty(page));
  158. /* Can we release the page from the cache? */
  159. if (!ceph_release_fscache_page(page, g))
  160. return 0;
  161. return !PagePrivate(page);
  162. }
  163. /*
  164. * read a single page, without unlocking it.
  165. */
  166. static int readpage_nounlock(struct file *filp, struct page *page)
  167. {
  168. struct inode *inode = file_inode(filp);
  169. struct ceph_inode_info *ci = ceph_inode(inode);
  170. struct ceph_osd_client *osdc =
  171. &ceph_inode_to_client(inode)->client->osdc;
  172. int err = 0;
  173. u64 off = page_offset(page);
  174. u64 len = PAGE_CACHE_SIZE;
  175. if (off >= i_size_read(inode)) {
  176. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  177. SetPageUptodate(page);
  178. return 0;
  179. }
  180. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  181. /*
  182. * Uptodate inline data should have been added
  183. * into page cache while getting Fcr caps.
  184. */
  185. if (off == 0)
  186. return -EINVAL;
  187. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  188. SetPageUptodate(page);
  189. return 0;
  190. }
  191. err = ceph_readpage_from_fscache(inode, page);
  192. if (err == 0)
  193. goto out;
  194. dout("readpage inode %p file %p page %p index %lu\n",
  195. inode, filp, page, page->index);
  196. err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
  197. off, &len,
  198. ci->i_truncate_seq, ci->i_truncate_size,
  199. &page, 1, 0);
  200. if (err == -ENOENT)
  201. err = 0;
  202. if (err < 0) {
  203. SetPageError(page);
  204. ceph_fscache_readpage_cancel(inode, page);
  205. goto out;
  206. }
  207. if (err < PAGE_CACHE_SIZE)
  208. /* zero fill remainder of page */
  209. zero_user_segment(page, err, PAGE_CACHE_SIZE);
  210. else
  211. flush_dcache_page(page);
  212. SetPageUptodate(page);
  213. ceph_readpage_to_fscache(inode, page);
  214. out:
  215. return err < 0 ? err : 0;
  216. }
  217. static int ceph_readpage(struct file *filp, struct page *page)
  218. {
  219. int r = readpage_nounlock(filp, page);
  220. unlock_page(page);
  221. return r;
  222. }
  223. /*
  224. * Finish an async read(ahead) op.
  225. */
  226. static void finish_read(struct ceph_osd_request *req, struct ceph_msg *msg)
  227. {
  228. struct inode *inode = req->r_inode;
  229. struct ceph_osd_data *osd_data;
  230. int rc = req->r_result;
  231. int bytes = le32_to_cpu(msg->hdr.data_len);
  232. int num_pages;
  233. int i;
  234. dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
  235. /* unlock all pages, zeroing any data we didn't read */
  236. osd_data = osd_req_op_extent_osd_data(req, 0);
  237. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  238. num_pages = calc_pages_for((u64)osd_data->alignment,
  239. (u64)osd_data->length);
  240. for (i = 0; i < num_pages; i++) {
  241. struct page *page = osd_data->pages[i];
  242. if (rc < 0 && rc != ENOENT)
  243. goto unlock;
  244. if (bytes < (int)PAGE_CACHE_SIZE) {
  245. /* zero (remainder of) page */
  246. int s = bytes < 0 ? 0 : bytes;
  247. zero_user_segment(page, s, PAGE_CACHE_SIZE);
  248. }
  249. dout("finish_read %p uptodate %p idx %lu\n", inode, page,
  250. page->index);
  251. flush_dcache_page(page);
  252. SetPageUptodate(page);
  253. ceph_readpage_to_fscache(inode, page);
  254. unlock:
  255. unlock_page(page);
  256. page_cache_release(page);
  257. bytes -= PAGE_CACHE_SIZE;
  258. }
  259. kfree(osd_data->pages);
  260. }
  261. static void ceph_unlock_page_vector(struct page **pages, int num_pages)
  262. {
  263. int i;
  264. for (i = 0; i < num_pages; i++)
  265. unlock_page(pages[i]);
  266. }
  267. /*
  268. * start an async read(ahead) operation. return nr_pages we submitted
  269. * a read for on success, or negative error code.
  270. */
  271. static int start_read(struct inode *inode, struct list_head *page_list, int max)
  272. {
  273. struct ceph_osd_client *osdc =
  274. &ceph_inode_to_client(inode)->client->osdc;
  275. struct ceph_inode_info *ci = ceph_inode(inode);
  276. struct page *page = list_entry(page_list->prev, struct page, lru);
  277. struct ceph_vino vino;
  278. struct ceph_osd_request *req;
  279. u64 off;
  280. u64 len;
  281. int i;
  282. struct page **pages;
  283. pgoff_t next_index;
  284. int nr_pages = 0;
  285. int ret;
  286. off = (u64) page_offset(page);
  287. /* count pages */
  288. next_index = page->index;
  289. list_for_each_entry_reverse(page, page_list, lru) {
  290. if (page->index != next_index)
  291. break;
  292. nr_pages++;
  293. next_index++;
  294. if (max && nr_pages == max)
  295. break;
  296. }
  297. len = nr_pages << PAGE_CACHE_SHIFT;
  298. dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
  299. off, len);
  300. vino = ceph_vino(inode);
  301. req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
  302. 0, 1, CEPH_OSD_OP_READ,
  303. CEPH_OSD_FLAG_READ, NULL,
  304. ci->i_truncate_seq, ci->i_truncate_size,
  305. false);
  306. if (IS_ERR(req))
  307. return PTR_ERR(req);
  308. /* build page vector */
  309. nr_pages = calc_pages_for(0, len);
  310. pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
  311. ret = -ENOMEM;
  312. if (!pages)
  313. goto out;
  314. for (i = 0; i < nr_pages; ++i) {
  315. page = list_entry(page_list->prev, struct page, lru);
  316. BUG_ON(PageLocked(page));
  317. list_del(&page->lru);
  318. dout("start_read %p adding %p idx %lu\n", inode, page,
  319. page->index);
  320. if (add_to_page_cache_lru(page, &inode->i_data, page->index,
  321. GFP_KERNEL)) {
  322. ceph_fscache_uncache_page(inode, page);
  323. page_cache_release(page);
  324. dout("start_read %p add_to_page_cache failed %p\n",
  325. inode, page);
  326. nr_pages = i;
  327. goto out_pages;
  328. }
  329. pages[i] = page;
  330. }
  331. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
  332. req->r_callback = finish_read;
  333. req->r_inode = inode;
  334. ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
  335. dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
  336. ret = ceph_osdc_start_request(osdc, req, false);
  337. if (ret < 0)
  338. goto out_pages;
  339. ceph_osdc_put_request(req);
  340. return nr_pages;
  341. out_pages:
  342. ceph_unlock_page_vector(pages, nr_pages);
  343. ceph_release_page_vector(pages, nr_pages);
  344. out:
  345. ceph_osdc_put_request(req);
  346. return ret;
  347. }
  348. /*
  349. * Read multiple pages. Leave pages we don't read + unlock in page_list;
  350. * the caller (VM) cleans them up.
  351. */
  352. static int ceph_readpages(struct file *file, struct address_space *mapping,
  353. struct list_head *page_list, unsigned nr_pages)
  354. {
  355. struct inode *inode = file_inode(file);
  356. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  357. int rc = 0;
  358. int max = 0;
  359. if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
  360. return -EINVAL;
  361. rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
  362. &nr_pages);
  363. if (rc == 0)
  364. goto out;
  365. if (fsc->mount_options->rsize >= PAGE_CACHE_SIZE)
  366. max = (fsc->mount_options->rsize + PAGE_CACHE_SIZE - 1)
  367. >> PAGE_SHIFT;
  368. dout("readpages %p file %p nr_pages %d max %d\n", inode,
  369. file, nr_pages,
  370. max);
  371. while (!list_empty(page_list)) {
  372. rc = start_read(inode, page_list, max);
  373. if (rc < 0)
  374. goto out;
  375. BUG_ON(rc == 0);
  376. }
  377. out:
  378. ceph_fscache_readpages_cancel(inode, page_list);
  379. dout("readpages %p file %p ret %d\n", inode, file, rc);
  380. return rc;
  381. }
  382. /*
  383. * Get ref for the oldest snapc for an inode with dirty data... that is, the
  384. * only snap context we are allowed to write back.
  385. */
  386. static struct ceph_snap_context *get_oldest_context(struct inode *inode,
  387. loff_t *snap_size)
  388. {
  389. struct ceph_inode_info *ci = ceph_inode(inode);
  390. struct ceph_snap_context *snapc = NULL;
  391. struct ceph_cap_snap *capsnap = NULL;
  392. spin_lock(&ci->i_ceph_lock);
  393. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  394. dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
  395. capsnap->context, capsnap->dirty_pages);
  396. if (capsnap->dirty_pages) {
  397. snapc = ceph_get_snap_context(capsnap->context);
  398. if (snap_size)
  399. *snap_size = capsnap->size;
  400. break;
  401. }
  402. }
  403. if (!snapc && ci->i_wrbuffer_ref_head) {
  404. snapc = ceph_get_snap_context(ci->i_head_snapc);
  405. dout(" head snapc %p has %d dirty pages\n",
  406. snapc, ci->i_wrbuffer_ref_head);
  407. }
  408. spin_unlock(&ci->i_ceph_lock);
  409. return snapc;
  410. }
  411. /*
  412. * Write a single page, but leave the page locked.
  413. *
  414. * If we get a write error, set the page error bit, but still adjust the
  415. * dirty page accounting (i.e., page is no longer dirty).
  416. */
  417. static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
  418. {
  419. struct inode *inode;
  420. struct ceph_inode_info *ci;
  421. struct ceph_fs_client *fsc;
  422. struct ceph_osd_client *osdc;
  423. struct ceph_snap_context *snapc, *oldest;
  424. loff_t page_off = page_offset(page);
  425. loff_t snap_size = -1;
  426. long writeback_stat;
  427. u64 truncate_size;
  428. u32 truncate_seq;
  429. int err = 0, len = PAGE_CACHE_SIZE;
  430. dout("writepage %p idx %lu\n", page, page->index);
  431. if (!page->mapping || !page->mapping->host) {
  432. dout("writepage %p - no mapping\n", page);
  433. return -EFAULT;
  434. }
  435. inode = page->mapping->host;
  436. ci = ceph_inode(inode);
  437. fsc = ceph_inode_to_client(inode);
  438. osdc = &fsc->client->osdc;
  439. /* verify this is a writeable snap context */
  440. snapc = page_snap_context(page);
  441. if (snapc == NULL) {
  442. dout("writepage %p page %p not dirty?\n", inode, page);
  443. goto out;
  444. }
  445. oldest = get_oldest_context(inode, &snap_size);
  446. if (snapc->seq > oldest->seq) {
  447. dout("writepage %p page %p snapc %p not writeable - noop\n",
  448. inode, page, snapc);
  449. /* we should only noop if called by kswapd */
  450. WARN_ON((current->flags & PF_MEMALLOC) == 0);
  451. ceph_put_snap_context(oldest);
  452. goto out;
  453. }
  454. ceph_put_snap_context(oldest);
  455. spin_lock(&ci->i_ceph_lock);
  456. truncate_seq = ci->i_truncate_seq;
  457. truncate_size = ci->i_truncate_size;
  458. if (snap_size == -1)
  459. snap_size = i_size_read(inode);
  460. spin_unlock(&ci->i_ceph_lock);
  461. /* is this a partial page at end of file? */
  462. if (page_off >= snap_size) {
  463. dout("%p page eof %llu\n", page, snap_size);
  464. goto out;
  465. }
  466. if (snap_size < page_off + len)
  467. len = snap_size - page_off;
  468. dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
  469. inode, page, page->index, page_off, len, snapc);
  470. writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
  471. if (writeback_stat >
  472. CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
  473. set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
  474. ceph_readpage_to_fscache(inode, page);
  475. set_page_writeback(page);
  476. err = ceph_osdc_writepages(osdc, ceph_vino(inode),
  477. &ci->i_layout, snapc,
  478. page_off, len,
  479. truncate_seq, truncate_size,
  480. &inode->i_mtime, &page, 1);
  481. if (err < 0) {
  482. dout("writepage setting page/mapping error %d %p\n", err, page);
  483. SetPageError(page);
  484. mapping_set_error(&inode->i_data, err);
  485. if (wbc)
  486. wbc->pages_skipped++;
  487. } else {
  488. dout("writepage cleaned page %p\n", page);
  489. err = 0; /* vfs expects us to return 0 */
  490. }
  491. page->private = 0;
  492. ClearPagePrivate(page);
  493. end_page_writeback(page);
  494. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  495. ceph_put_snap_context(snapc); /* page's reference */
  496. out:
  497. return err;
  498. }
  499. static int ceph_writepage(struct page *page, struct writeback_control *wbc)
  500. {
  501. int err;
  502. struct inode *inode = page->mapping->host;
  503. BUG_ON(!inode);
  504. ihold(inode);
  505. err = writepage_nounlock(page, wbc);
  506. unlock_page(page);
  507. iput(inode);
  508. return err;
  509. }
  510. /*
  511. * lame release_pages helper. release_pages() isn't exported to
  512. * modules.
  513. */
  514. static void ceph_release_pages(struct page **pages, int num)
  515. {
  516. struct pagevec pvec;
  517. int i;
  518. pagevec_init(&pvec, 0);
  519. for (i = 0; i < num; i++) {
  520. if (pagevec_add(&pvec, pages[i]) == 0)
  521. pagevec_release(&pvec);
  522. }
  523. pagevec_release(&pvec);
  524. }
  525. /*
  526. * async writeback completion handler.
  527. *
  528. * If we get an error, set the mapping error bit, but not the individual
  529. * page error bits.
  530. */
  531. static void writepages_finish(struct ceph_osd_request *req,
  532. struct ceph_msg *msg)
  533. {
  534. struct inode *inode = req->r_inode;
  535. struct ceph_inode_info *ci = ceph_inode(inode);
  536. struct ceph_osd_data *osd_data;
  537. unsigned wrote;
  538. struct page *page;
  539. int num_pages;
  540. int i;
  541. struct ceph_snap_context *snapc = req->r_snapc;
  542. struct address_space *mapping = inode->i_mapping;
  543. int rc = req->r_result;
  544. u64 bytes = req->r_ops[0].extent.length;
  545. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  546. long writeback_stat;
  547. unsigned issued = ceph_caps_issued(ci);
  548. osd_data = osd_req_op_extent_osd_data(req, 0);
  549. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  550. num_pages = calc_pages_for((u64)osd_data->alignment,
  551. (u64)osd_data->length);
  552. if (rc >= 0) {
  553. /*
  554. * Assume we wrote the pages we originally sent. The
  555. * osd might reply with fewer pages if our writeback
  556. * raced with a truncation and was adjusted at the osd,
  557. * so don't believe the reply.
  558. */
  559. wrote = num_pages;
  560. } else {
  561. wrote = 0;
  562. mapping_set_error(mapping, rc);
  563. }
  564. dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n",
  565. inode, rc, bytes, wrote);
  566. /* clean all pages */
  567. for (i = 0; i < num_pages; i++) {
  568. page = osd_data->pages[i];
  569. BUG_ON(!page);
  570. WARN_ON(!PageUptodate(page));
  571. writeback_stat =
  572. atomic_long_dec_return(&fsc->writeback_count);
  573. if (writeback_stat <
  574. CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
  575. clear_bdi_congested(&fsc->backing_dev_info,
  576. BLK_RW_ASYNC);
  577. ceph_put_snap_context(page_snap_context(page));
  578. page->private = 0;
  579. ClearPagePrivate(page);
  580. dout("unlocking %d %p\n", i, page);
  581. end_page_writeback(page);
  582. /*
  583. * We lost the cache cap, need to truncate the page before
  584. * it is unlocked, otherwise we'd truncate it later in the
  585. * page truncation thread, possibly losing some data that
  586. * raced its way in
  587. */
  588. if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0)
  589. generic_error_remove_page(inode->i_mapping, page);
  590. unlock_page(page);
  591. }
  592. dout("%p wrote+cleaned %d pages\n", inode, wrote);
  593. ceph_put_wrbuffer_cap_refs(ci, num_pages, snapc);
  594. ceph_release_pages(osd_data->pages, num_pages);
  595. if (osd_data->pages_from_pool)
  596. mempool_free(osd_data->pages,
  597. ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
  598. else
  599. kfree(osd_data->pages);
  600. ceph_osdc_put_request(req);
  601. }
  602. /*
  603. * initiate async writeback
  604. */
  605. static int ceph_writepages_start(struct address_space *mapping,
  606. struct writeback_control *wbc)
  607. {
  608. struct inode *inode = mapping->host;
  609. struct ceph_inode_info *ci = ceph_inode(inode);
  610. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  611. struct ceph_vino vino = ceph_vino(inode);
  612. pgoff_t index, start, end;
  613. int range_whole = 0;
  614. int should_loop = 1;
  615. pgoff_t max_pages = 0, max_pages_ever = 0;
  616. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  617. struct pagevec pvec;
  618. int done = 0;
  619. int rc = 0;
  620. unsigned wsize = 1 << inode->i_blkbits;
  621. struct ceph_osd_request *req = NULL;
  622. int do_sync = 0;
  623. loff_t snap_size, i_size;
  624. u64 truncate_size;
  625. u32 truncate_seq;
  626. /*
  627. * Include a 'sync' in the OSD request if this is a data
  628. * integrity write (e.g., O_SYNC write or fsync()), or if our
  629. * cap is being revoked.
  630. */
  631. if ((wbc->sync_mode == WB_SYNC_ALL) ||
  632. ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
  633. do_sync = 1;
  634. dout("writepages_start %p dosync=%d (mode=%s)\n",
  635. inode, do_sync,
  636. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  637. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  638. if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  639. pr_warn("writepage_start %p on forced umount\n", inode);
  640. truncate_pagecache(inode, 0);
  641. mapping_set_error(mapping, -EIO);
  642. return -EIO; /* we're in a forced umount, don't write! */
  643. }
  644. if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
  645. wsize = fsc->mount_options->wsize;
  646. if (wsize < PAGE_CACHE_SIZE)
  647. wsize = PAGE_CACHE_SIZE;
  648. max_pages_ever = wsize >> PAGE_CACHE_SHIFT;
  649. pagevec_init(&pvec, 0);
  650. /* where to start/end? */
  651. if (wbc->range_cyclic) {
  652. start = mapping->writeback_index; /* Start from prev offset */
  653. end = -1;
  654. dout(" cyclic, start at %lu\n", start);
  655. } else {
  656. start = wbc->range_start >> PAGE_CACHE_SHIFT;
  657. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  658. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  659. range_whole = 1;
  660. should_loop = 0;
  661. dout(" not cyclic, %lu to %lu\n", start, end);
  662. }
  663. index = start;
  664. retry:
  665. /* find oldest snap context with dirty data */
  666. ceph_put_snap_context(snapc);
  667. snap_size = -1;
  668. snapc = get_oldest_context(inode, &snap_size);
  669. if (!snapc) {
  670. /* hmm, why does writepages get called when there
  671. is no dirty data? */
  672. dout(" no snap context with dirty data?\n");
  673. goto out;
  674. }
  675. dout(" oldest snapc is %p seq %lld (%d snaps)\n",
  676. snapc, snapc->seq, snapc->num_snaps);
  677. spin_lock(&ci->i_ceph_lock);
  678. truncate_seq = ci->i_truncate_seq;
  679. truncate_size = ci->i_truncate_size;
  680. i_size = i_size_read(inode);
  681. spin_unlock(&ci->i_ceph_lock);
  682. if (last_snapc && snapc != last_snapc) {
  683. /* if we switched to a newer snapc, restart our scan at the
  684. * start of the original file range. */
  685. dout(" snapc differs from last pass, restarting at %lu\n",
  686. index);
  687. index = start;
  688. }
  689. last_snapc = snapc;
  690. while (!done && index <= end) {
  691. unsigned i;
  692. int first;
  693. pgoff_t next;
  694. int pvec_pages, locked_pages;
  695. struct page **pages = NULL;
  696. mempool_t *pool = NULL; /* Becomes non-null if mempool used */
  697. struct page *page;
  698. int want;
  699. u64 offset, len;
  700. long writeback_stat;
  701. next = 0;
  702. locked_pages = 0;
  703. max_pages = max_pages_ever;
  704. get_more_pages:
  705. first = -1;
  706. want = min(end - index,
  707. min((pgoff_t)PAGEVEC_SIZE,
  708. max_pages - (pgoff_t)locked_pages) - 1)
  709. + 1;
  710. pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  711. PAGECACHE_TAG_DIRTY,
  712. want);
  713. dout("pagevec_lookup_tag got %d\n", pvec_pages);
  714. if (!pvec_pages && !locked_pages)
  715. break;
  716. for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
  717. page = pvec.pages[i];
  718. dout("? %p idx %lu\n", page, page->index);
  719. if (locked_pages == 0)
  720. lock_page(page); /* first page */
  721. else if (!trylock_page(page))
  722. break;
  723. /* only dirty pages, or our accounting breaks */
  724. if (unlikely(!PageDirty(page)) ||
  725. unlikely(page->mapping != mapping)) {
  726. dout("!dirty or !mapping %p\n", page);
  727. unlock_page(page);
  728. break;
  729. }
  730. if (!wbc->range_cyclic && page->index > end) {
  731. dout("end of range %p\n", page);
  732. done = 1;
  733. unlock_page(page);
  734. break;
  735. }
  736. if (next && (page->index != next)) {
  737. dout("not consecutive %p\n", page);
  738. unlock_page(page);
  739. break;
  740. }
  741. if (wbc->sync_mode != WB_SYNC_NONE) {
  742. dout("waiting on writeback %p\n", page);
  743. wait_on_page_writeback(page);
  744. }
  745. if (page_offset(page) >=
  746. (snap_size == -1 ? i_size : snap_size)) {
  747. dout("%p page eof %llu\n", page,
  748. (snap_size == -1 ? i_size : snap_size));
  749. done = 1;
  750. unlock_page(page);
  751. break;
  752. }
  753. if (PageWriteback(page)) {
  754. dout("%p under writeback\n", page);
  755. unlock_page(page);
  756. break;
  757. }
  758. /* only if matching snap context */
  759. pgsnapc = page_snap_context(page);
  760. if (pgsnapc->seq > snapc->seq) {
  761. dout("page snapc %p %lld > oldest %p %lld\n",
  762. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  763. unlock_page(page);
  764. if (!locked_pages)
  765. continue; /* keep looking for snap */
  766. break;
  767. }
  768. if (!clear_page_dirty_for_io(page)) {
  769. dout("%p !clear_page_dirty_for_io\n", page);
  770. unlock_page(page);
  771. break;
  772. }
  773. /*
  774. * We have something to write. If this is
  775. * the first locked page this time through,
  776. * allocate an osd request and a page array
  777. * that it will use.
  778. */
  779. if (locked_pages == 0) {
  780. BUG_ON(pages);
  781. /* prepare async write request */
  782. offset = (u64)page_offset(page);
  783. len = wsize;
  784. req = ceph_osdc_new_request(&fsc->client->osdc,
  785. &ci->i_layout, vino,
  786. offset, &len, 0,
  787. do_sync ? 2 : 1,
  788. CEPH_OSD_OP_WRITE,
  789. CEPH_OSD_FLAG_WRITE |
  790. CEPH_OSD_FLAG_ONDISK,
  791. snapc, truncate_seq,
  792. truncate_size, true);
  793. if (IS_ERR(req)) {
  794. rc = PTR_ERR(req);
  795. unlock_page(page);
  796. break;
  797. }
  798. if (do_sync)
  799. osd_req_op_init(req, 1,
  800. CEPH_OSD_OP_STARTSYNC, 0);
  801. req->r_callback = writepages_finish;
  802. req->r_inode = inode;
  803. max_pages = calc_pages_for(0, (u64)len);
  804. pages = kmalloc(max_pages * sizeof (*pages),
  805. GFP_NOFS);
  806. if (!pages) {
  807. pool = fsc->wb_pagevec_pool;
  808. pages = mempool_alloc(pool, GFP_NOFS);
  809. BUG_ON(!pages);
  810. }
  811. }
  812. /* note position of first page in pvec */
  813. if (first < 0)
  814. first = i;
  815. dout("%p will write page %p idx %lu\n",
  816. inode, page, page->index);
  817. writeback_stat =
  818. atomic_long_inc_return(&fsc->writeback_count);
  819. if (writeback_stat > CONGESTION_ON_THRESH(
  820. fsc->mount_options->congestion_kb)) {
  821. set_bdi_congested(&fsc->backing_dev_info,
  822. BLK_RW_ASYNC);
  823. }
  824. set_page_writeback(page);
  825. pages[locked_pages] = page;
  826. locked_pages++;
  827. next = page->index + 1;
  828. }
  829. /* did we get anything? */
  830. if (!locked_pages)
  831. goto release_pvec_pages;
  832. if (i) {
  833. int j;
  834. BUG_ON(!locked_pages || first < 0);
  835. if (pvec_pages && i == pvec_pages &&
  836. locked_pages < max_pages) {
  837. dout("reached end pvec, trying for more\n");
  838. pagevec_reinit(&pvec);
  839. goto get_more_pages;
  840. }
  841. /* shift unused pages over in the pvec... we
  842. * will need to release them below. */
  843. for (j = i; j < pvec_pages; j++) {
  844. dout(" pvec leftover page %p\n",
  845. pvec.pages[j]);
  846. pvec.pages[j-i+first] = pvec.pages[j];
  847. }
  848. pvec.nr -= i-first;
  849. }
  850. /* Format the osd request message and submit the write */
  851. offset = page_offset(pages[0]);
  852. len = (u64)locked_pages << PAGE_CACHE_SHIFT;
  853. if (snap_size == -1) {
  854. len = min(len, (u64)i_size_read(inode) - offset);
  855. /* writepages_finish() clears writeback pages
  856. * according to the data length, so make sure
  857. * data length covers all locked pages */
  858. len = max(len, 1 +
  859. ((u64)(locked_pages - 1) << PAGE_CACHE_SHIFT));
  860. } else {
  861. len = min(len, snap_size - offset);
  862. }
  863. dout("writepages got %d pages at %llu~%llu\n",
  864. locked_pages, offset, len);
  865. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
  866. !!pool, false);
  867. pages = NULL; /* request message now owns the pages array */
  868. pool = NULL;
  869. /* Update the write op length in case we changed it */
  870. osd_req_op_extent_update(req, 0, len);
  871. vino = ceph_vino(inode);
  872. ceph_osdc_build_request(req, offset, snapc, vino.snap,
  873. &inode->i_mtime);
  874. rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
  875. BUG_ON(rc);
  876. req = NULL;
  877. /* continue? */
  878. index = next;
  879. wbc->nr_to_write -= locked_pages;
  880. if (wbc->nr_to_write <= 0)
  881. done = 1;
  882. release_pvec_pages:
  883. dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
  884. pvec.nr ? pvec.pages[0] : NULL);
  885. pagevec_release(&pvec);
  886. if (locked_pages && !done)
  887. goto retry;
  888. }
  889. if (should_loop && !done) {
  890. /* more to do; loop back to beginning of file */
  891. dout("writepages looping back to beginning of file\n");
  892. should_loop = 0;
  893. index = 0;
  894. goto retry;
  895. }
  896. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  897. mapping->writeback_index = index;
  898. out:
  899. if (req)
  900. ceph_osdc_put_request(req);
  901. ceph_put_snap_context(snapc);
  902. dout("writepages done, rc = %d\n", rc);
  903. return rc;
  904. }
  905. /*
  906. * See if a given @snapc is either writeable, or already written.
  907. */
  908. static int context_is_writeable_or_written(struct inode *inode,
  909. struct ceph_snap_context *snapc)
  910. {
  911. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
  912. int ret = !oldest || snapc->seq <= oldest->seq;
  913. ceph_put_snap_context(oldest);
  914. return ret;
  915. }
  916. /*
  917. * We are only allowed to write into/dirty the page if the page is
  918. * clean, or already dirty within the same snap context.
  919. *
  920. * called with page locked.
  921. * return success with page locked,
  922. * or any failure (incl -EAGAIN) with page unlocked.
  923. */
  924. static int ceph_update_writeable_page(struct file *file,
  925. loff_t pos, unsigned len,
  926. struct page *page)
  927. {
  928. struct inode *inode = file_inode(file);
  929. struct ceph_inode_info *ci = ceph_inode(inode);
  930. loff_t page_off = pos & PAGE_CACHE_MASK;
  931. int pos_in_page = pos & ~PAGE_CACHE_MASK;
  932. int end_in_page = pos_in_page + len;
  933. loff_t i_size;
  934. int r;
  935. struct ceph_snap_context *snapc, *oldest;
  936. retry_locked:
  937. /* writepages currently holds page lock, but if we change that later, */
  938. wait_on_page_writeback(page);
  939. snapc = page_snap_context(page);
  940. if (snapc && snapc != ci->i_head_snapc) {
  941. /*
  942. * this page is already dirty in another (older) snap
  943. * context! is it writeable now?
  944. */
  945. oldest = get_oldest_context(inode, NULL);
  946. if (snapc->seq > oldest->seq) {
  947. ceph_put_snap_context(oldest);
  948. dout(" page %p snapc %p not current or oldest\n",
  949. page, snapc);
  950. /*
  951. * queue for writeback, and wait for snapc to
  952. * be writeable or written
  953. */
  954. snapc = ceph_get_snap_context(snapc);
  955. unlock_page(page);
  956. ceph_queue_writeback(inode);
  957. r = wait_event_interruptible(ci->i_cap_wq,
  958. context_is_writeable_or_written(inode, snapc));
  959. ceph_put_snap_context(snapc);
  960. if (r == -ERESTARTSYS)
  961. return r;
  962. return -EAGAIN;
  963. }
  964. ceph_put_snap_context(oldest);
  965. /* yay, writeable, do it now (without dropping page lock) */
  966. dout(" page %p snapc %p not current, but oldest\n",
  967. page, snapc);
  968. if (!clear_page_dirty_for_io(page))
  969. goto retry_locked;
  970. r = writepage_nounlock(page, NULL);
  971. if (r < 0)
  972. goto fail_nosnap;
  973. goto retry_locked;
  974. }
  975. if (PageUptodate(page)) {
  976. dout(" page %p already uptodate\n", page);
  977. return 0;
  978. }
  979. /* full page? */
  980. if (pos_in_page == 0 && len == PAGE_CACHE_SIZE)
  981. return 0;
  982. /* past end of file? */
  983. i_size = i_size_read(inode);
  984. if (page_off >= i_size ||
  985. (pos_in_page == 0 && (pos+len) >= i_size &&
  986. end_in_page - pos_in_page != PAGE_CACHE_SIZE)) {
  987. dout(" zeroing %p 0 - %d and %d - %d\n",
  988. page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE);
  989. zero_user_segments(page,
  990. 0, pos_in_page,
  991. end_in_page, PAGE_CACHE_SIZE);
  992. return 0;
  993. }
  994. /* we need to read it. */
  995. r = readpage_nounlock(file, page);
  996. if (r < 0)
  997. goto fail_nosnap;
  998. goto retry_locked;
  999. fail_nosnap:
  1000. unlock_page(page);
  1001. return r;
  1002. }
  1003. /*
  1004. * We are only allowed to write into/dirty the page if the page is
  1005. * clean, or already dirty within the same snap context.
  1006. */
  1007. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  1008. loff_t pos, unsigned len, unsigned flags,
  1009. struct page **pagep, void **fsdata)
  1010. {
  1011. struct inode *inode = file_inode(file);
  1012. struct page *page;
  1013. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1014. int r;
  1015. do {
  1016. /* get a page */
  1017. page = grab_cache_page_write_begin(mapping, index, 0);
  1018. if (!page)
  1019. return -ENOMEM;
  1020. dout("write_begin file %p inode %p page %p %d~%d\n", file,
  1021. inode, page, (int)pos, (int)len);
  1022. r = ceph_update_writeable_page(file, pos, len, page);
  1023. if (r < 0)
  1024. page_cache_release(page);
  1025. else
  1026. *pagep = page;
  1027. } while (r == -EAGAIN);
  1028. return r;
  1029. }
  1030. /*
  1031. * we don't do anything in here that simple_write_end doesn't do
  1032. * except adjust dirty page accounting
  1033. */
  1034. static int ceph_write_end(struct file *file, struct address_space *mapping,
  1035. loff_t pos, unsigned len, unsigned copied,
  1036. struct page *page, void *fsdata)
  1037. {
  1038. struct inode *inode = file_inode(file);
  1039. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  1040. int check_cap = 0;
  1041. dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
  1042. inode, page, (int)pos, (int)copied, (int)len);
  1043. /* zero the stale part of the page if we did a short copy */
  1044. if (copied < len)
  1045. zero_user_segment(page, from+copied, len);
  1046. /* did file size increase? */
  1047. if (pos+copied > i_size_read(inode))
  1048. check_cap = ceph_inode_set_size(inode, pos+copied);
  1049. if (!PageUptodate(page))
  1050. SetPageUptodate(page);
  1051. set_page_dirty(page);
  1052. unlock_page(page);
  1053. page_cache_release(page);
  1054. if (check_cap)
  1055. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
  1056. return copied;
  1057. }
  1058. /*
  1059. * we set .direct_IO to indicate direct io is supported, but since we
  1060. * intercept O_DIRECT reads and writes early, this function should
  1061. * never get called.
  1062. */
  1063. static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter,
  1064. loff_t pos)
  1065. {
  1066. WARN_ON(1);
  1067. return -EINVAL;
  1068. }
  1069. const struct address_space_operations ceph_aops = {
  1070. .readpage = ceph_readpage,
  1071. .readpages = ceph_readpages,
  1072. .writepage = ceph_writepage,
  1073. .writepages = ceph_writepages_start,
  1074. .write_begin = ceph_write_begin,
  1075. .write_end = ceph_write_end,
  1076. .set_page_dirty = ceph_set_page_dirty,
  1077. .invalidatepage = ceph_invalidatepage,
  1078. .releasepage = ceph_releasepage,
  1079. .direct_IO = ceph_direct_io,
  1080. };
  1081. /*
  1082. * vm ops
  1083. */
  1084. static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1085. {
  1086. struct inode *inode = file_inode(vma->vm_file);
  1087. struct ceph_inode_info *ci = ceph_inode(inode);
  1088. struct ceph_file_info *fi = vma->vm_file->private_data;
  1089. struct page *pinned_page = NULL;
  1090. loff_t off = vmf->pgoff << PAGE_CACHE_SHIFT;
  1091. int want, got, ret;
  1092. dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
  1093. inode, ceph_vinop(inode), off, (size_t)PAGE_CACHE_SIZE);
  1094. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1095. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1096. else
  1097. want = CEPH_CAP_FILE_CACHE;
  1098. while (1) {
  1099. got = 0;
  1100. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want,
  1101. -1, &got, &pinned_page);
  1102. if (ret == 0)
  1103. break;
  1104. if (ret != -ERESTARTSYS) {
  1105. WARN_ON(1);
  1106. return VM_FAULT_SIGBUS;
  1107. }
  1108. }
  1109. dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
  1110. inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got));
  1111. if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
  1112. ci->i_inline_version == CEPH_INLINE_NONE)
  1113. ret = filemap_fault(vma, vmf);
  1114. else
  1115. ret = -EAGAIN;
  1116. dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
  1117. inode, off, (size_t)PAGE_CACHE_SIZE, ceph_cap_string(got), ret);
  1118. if (pinned_page)
  1119. page_cache_release(pinned_page);
  1120. ceph_put_cap_refs(ci, got);
  1121. if (ret != -EAGAIN)
  1122. return ret;
  1123. /* read inline data */
  1124. if (off >= PAGE_CACHE_SIZE) {
  1125. /* does not support inline data > PAGE_SIZE */
  1126. ret = VM_FAULT_SIGBUS;
  1127. } else {
  1128. int ret1;
  1129. struct address_space *mapping = inode->i_mapping;
  1130. struct page *page = find_or_create_page(mapping, 0,
  1131. mapping_gfp_constraint(mapping,
  1132. ~__GFP_FS));
  1133. if (!page) {
  1134. ret = VM_FAULT_OOM;
  1135. goto out;
  1136. }
  1137. ret1 = __ceph_do_getattr(inode, page,
  1138. CEPH_STAT_CAP_INLINE_DATA, true);
  1139. if (ret1 < 0 || off >= i_size_read(inode)) {
  1140. unlock_page(page);
  1141. page_cache_release(page);
  1142. ret = VM_FAULT_SIGBUS;
  1143. goto out;
  1144. }
  1145. if (ret1 < PAGE_CACHE_SIZE)
  1146. zero_user_segment(page, ret1, PAGE_CACHE_SIZE);
  1147. else
  1148. flush_dcache_page(page);
  1149. SetPageUptodate(page);
  1150. vmf->page = page;
  1151. ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
  1152. }
  1153. out:
  1154. dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
  1155. inode, off, (size_t)PAGE_CACHE_SIZE, ret);
  1156. return ret;
  1157. }
  1158. /*
  1159. * Reuse write_begin here for simplicity.
  1160. */
  1161. static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1162. {
  1163. struct inode *inode = file_inode(vma->vm_file);
  1164. struct ceph_inode_info *ci = ceph_inode(inode);
  1165. struct ceph_file_info *fi = vma->vm_file->private_data;
  1166. struct ceph_cap_flush *prealloc_cf;
  1167. struct page *page = vmf->page;
  1168. loff_t off = page_offset(page);
  1169. loff_t size = i_size_read(inode);
  1170. size_t len;
  1171. int want, got, ret;
  1172. prealloc_cf = ceph_alloc_cap_flush();
  1173. if (!prealloc_cf)
  1174. return VM_FAULT_SIGBUS;
  1175. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1176. struct page *locked_page = NULL;
  1177. if (off == 0) {
  1178. lock_page(page);
  1179. locked_page = page;
  1180. }
  1181. ret = ceph_uninline_data(vma->vm_file, locked_page);
  1182. if (locked_page)
  1183. unlock_page(locked_page);
  1184. if (ret < 0) {
  1185. ret = VM_FAULT_SIGBUS;
  1186. goto out_free;
  1187. }
  1188. }
  1189. if (off + PAGE_CACHE_SIZE <= size)
  1190. len = PAGE_CACHE_SIZE;
  1191. else
  1192. len = size & ~PAGE_CACHE_MASK;
  1193. dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
  1194. inode, ceph_vinop(inode), off, len, size);
  1195. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1196. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1197. else
  1198. want = CEPH_CAP_FILE_BUFFER;
  1199. while (1) {
  1200. got = 0;
  1201. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
  1202. &got, NULL);
  1203. if (ret == 0)
  1204. break;
  1205. if (ret != -ERESTARTSYS) {
  1206. WARN_ON(1);
  1207. ret = VM_FAULT_SIGBUS;
  1208. goto out_free;
  1209. }
  1210. }
  1211. dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
  1212. inode, off, len, ceph_cap_string(got));
  1213. /* Update time before taking page lock */
  1214. file_update_time(vma->vm_file);
  1215. lock_page(page);
  1216. ret = VM_FAULT_NOPAGE;
  1217. if ((off > size) ||
  1218. (page->mapping != inode->i_mapping)) {
  1219. unlock_page(page);
  1220. goto out;
  1221. }
  1222. ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
  1223. if (ret >= 0) {
  1224. /* success. we'll keep the page locked. */
  1225. set_page_dirty(page);
  1226. ret = VM_FAULT_LOCKED;
  1227. } else {
  1228. if (ret == -ENOMEM)
  1229. ret = VM_FAULT_OOM;
  1230. else
  1231. ret = VM_FAULT_SIGBUS;
  1232. }
  1233. out:
  1234. if (ret == VM_FAULT_LOCKED ||
  1235. ci->i_inline_version != CEPH_INLINE_NONE) {
  1236. int dirty;
  1237. spin_lock(&ci->i_ceph_lock);
  1238. ci->i_inline_version = CEPH_INLINE_NONE;
  1239. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1240. &prealloc_cf);
  1241. spin_unlock(&ci->i_ceph_lock);
  1242. if (dirty)
  1243. __mark_inode_dirty(inode, dirty);
  1244. }
  1245. dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
  1246. inode, off, len, ceph_cap_string(got), ret);
  1247. ceph_put_cap_refs(ci, got);
  1248. out_free:
  1249. ceph_free_cap_flush(prealloc_cf);
  1250. return ret;
  1251. }
  1252. void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
  1253. char *data, size_t len)
  1254. {
  1255. struct address_space *mapping = inode->i_mapping;
  1256. struct page *page;
  1257. if (locked_page) {
  1258. page = locked_page;
  1259. } else {
  1260. if (i_size_read(inode) == 0)
  1261. return;
  1262. page = find_or_create_page(mapping, 0,
  1263. mapping_gfp_constraint(mapping,
  1264. ~__GFP_FS));
  1265. if (!page)
  1266. return;
  1267. if (PageUptodate(page)) {
  1268. unlock_page(page);
  1269. page_cache_release(page);
  1270. return;
  1271. }
  1272. }
  1273. dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
  1274. inode, ceph_vinop(inode), len, locked_page);
  1275. if (len > 0) {
  1276. void *kaddr = kmap_atomic(page);
  1277. memcpy(kaddr, data, len);
  1278. kunmap_atomic(kaddr);
  1279. }
  1280. if (page != locked_page) {
  1281. if (len < PAGE_CACHE_SIZE)
  1282. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  1283. else
  1284. flush_dcache_page(page);
  1285. SetPageUptodate(page);
  1286. unlock_page(page);
  1287. page_cache_release(page);
  1288. }
  1289. }
  1290. int ceph_uninline_data(struct file *filp, struct page *locked_page)
  1291. {
  1292. struct inode *inode = file_inode(filp);
  1293. struct ceph_inode_info *ci = ceph_inode(inode);
  1294. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1295. struct ceph_osd_request *req;
  1296. struct page *page = NULL;
  1297. u64 len, inline_version;
  1298. int err = 0;
  1299. bool from_pagecache = false;
  1300. spin_lock(&ci->i_ceph_lock);
  1301. inline_version = ci->i_inline_version;
  1302. spin_unlock(&ci->i_ceph_lock);
  1303. dout("uninline_data %p %llx.%llx inline_version %llu\n",
  1304. inode, ceph_vinop(inode), inline_version);
  1305. if (inline_version == 1 || /* initial version, no data */
  1306. inline_version == CEPH_INLINE_NONE)
  1307. goto out;
  1308. if (locked_page) {
  1309. page = locked_page;
  1310. WARN_ON(!PageUptodate(page));
  1311. } else if (ceph_caps_issued(ci) &
  1312. (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
  1313. page = find_get_page(inode->i_mapping, 0);
  1314. if (page) {
  1315. if (PageUptodate(page)) {
  1316. from_pagecache = true;
  1317. lock_page(page);
  1318. } else {
  1319. page_cache_release(page);
  1320. page = NULL;
  1321. }
  1322. }
  1323. }
  1324. if (page) {
  1325. len = i_size_read(inode);
  1326. if (len > PAGE_CACHE_SIZE)
  1327. len = PAGE_CACHE_SIZE;
  1328. } else {
  1329. page = __page_cache_alloc(GFP_NOFS);
  1330. if (!page) {
  1331. err = -ENOMEM;
  1332. goto out;
  1333. }
  1334. err = __ceph_do_getattr(inode, page,
  1335. CEPH_STAT_CAP_INLINE_DATA, true);
  1336. if (err < 0) {
  1337. /* no inline data */
  1338. if (err == -ENODATA)
  1339. err = 0;
  1340. goto out;
  1341. }
  1342. len = err;
  1343. }
  1344. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1345. ceph_vino(inode), 0, &len, 0, 1,
  1346. CEPH_OSD_OP_CREATE,
  1347. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1348. ceph_empty_snapc, 0, 0, false);
  1349. if (IS_ERR(req)) {
  1350. err = PTR_ERR(req);
  1351. goto out;
  1352. }
  1353. ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
  1354. err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1355. if (!err)
  1356. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1357. ceph_osdc_put_request(req);
  1358. if (err < 0)
  1359. goto out;
  1360. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1361. ceph_vino(inode), 0, &len, 1, 3,
  1362. CEPH_OSD_OP_WRITE,
  1363. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1364. ceph_empty_snapc,
  1365. ci->i_truncate_seq, ci->i_truncate_size,
  1366. false);
  1367. if (IS_ERR(req)) {
  1368. err = PTR_ERR(req);
  1369. goto out;
  1370. }
  1371. osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
  1372. {
  1373. __le64 xattr_buf = cpu_to_le64(inline_version);
  1374. err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
  1375. "inline_version", &xattr_buf,
  1376. sizeof(xattr_buf),
  1377. CEPH_OSD_CMPXATTR_OP_GT,
  1378. CEPH_OSD_CMPXATTR_MODE_U64);
  1379. if (err)
  1380. goto out_put;
  1381. }
  1382. {
  1383. char xattr_buf[32];
  1384. int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
  1385. "%llu", inline_version);
  1386. err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
  1387. "inline_version",
  1388. xattr_buf, xattr_len, 0, 0);
  1389. if (err)
  1390. goto out_put;
  1391. }
  1392. ceph_osdc_build_request(req, 0, NULL, CEPH_NOSNAP, &inode->i_mtime);
  1393. err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1394. if (!err)
  1395. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1396. out_put:
  1397. ceph_osdc_put_request(req);
  1398. if (err == -ECANCELED)
  1399. err = 0;
  1400. out:
  1401. if (page && page != locked_page) {
  1402. if (from_pagecache) {
  1403. unlock_page(page);
  1404. page_cache_release(page);
  1405. } else
  1406. __free_pages(page, 0);
  1407. }
  1408. dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
  1409. inode, ceph_vinop(inode), inline_version, err);
  1410. return err;
  1411. }
  1412. static const struct vm_operations_struct ceph_vmops = {
  1413. .fault = ceph_filemap_fault,
  1414. .page_mkwrite = ceph_page_mkwrite,
  1415. };
  1416. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1417. {
  1418. struct address_space *mapping = file->f_mapping;
  1419. if (!mapping->a_ops->readpage)
  1420. return -ENOEXEC;
  1421. file_accessed(file);
  1422. vma->vm_ops = &ceph_vmops;
  1423. return 0;
  1424. }
  1425. enum {
  1426. POOL_READ = 1,
  1427. POOL_WRITE = 2,
  1428. };
  1429. static int __ceph_pool_perm_get(struct ceph_inode_info *ci, u32 pool)
  1430. {
  1431. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1432. struct ceph_mds_client *mdsc = fsc->mdsc;
  1433. struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
  1434. struct rb_node **p, *parent;
  1435. struct ceph_pool_perm *perm;
  1436. struct page **pages;
  1437. int err = 0, err2 = 0, have = 0;
  1438. down_read(&mdsc->pool_perm_rwsem);
  1439. p = &mdsc->pool_perm_tree.rb_node;
  1440. while (*p) {
  1441. perm = rb_entry(*p, struct ceph_pool_perm, node);
  1442. if (pool < perm->pool)
  1443. p = &(*p)->rb_left;
  1444. else if (pool > perm->pool)
  1445. p = &(*p)->rb_right;
  1446. else {
  1447. have = perm->perm;
  1448. break;
  1449. }
  1450. }
  1451. up_read(&mdsc->pool_perm_rwsem);
  1452. if (*p)
  1453. goto out;
  1454. dout("__ceph_pool_perm_get pool %u no perm cached\n", pool);
  1455. down_write(&mdsc->pool_perm_rwsem);
  1456. parent = NULL;
  1457. while (*p) {
  1458. parent = *p;
  1459. perm = rb_entry(parent, struct ceph_pool_perm, node);
  1460. if (pool < perm->pool)
  1461. p = &(*p)->rb_left;
  1462. else if (pool > perm->pool)
  1463. p = &(*p)->rb_right;
  1464. else {
  1465. have = perm->perm;
  1466. break;
  1467. }
  1468. }
  1469. if (*p) {
  1470. up_write(&mdsc->pool_perm_rwsem);
  1471. goto out;
  1472. }
  1473. rd_req = ceph_osdc_alloc_request(&fsc->client->osdc,
  1474. ceph_empty_snapc,
  1475. 1, false, GFP_NOFS);
  1476. if (!rd_req) {
  1477. err = -ENOMEM;
  1478. goto out_unlock;
  1479. }
  1480. rd_req->r_flags = CEPH_OSD_FLAG_READ;
  1481. osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
  1482. rd_req->r_base_oloc.pool = pool;
  1483. snprintf(rd_req->r_base_oid.name, sizeof(rd_req->r_base_oid.name),
  1484. "%llx.00000000", ci->i_vino.ino);
  1485. rd_req->r_base_oid.name_len = strlen(rd_req->r_base_oid.name);
  1486. wr_req = ceph_osdc_alloc_request(&fsc->client->osdc,
  1487. ceph_empty_snapc,
  1488. 1, false, GFP_NOFS);
  1489. if (!wr_req) {
  1490. err = -ENOMEM;
  1491. goto out_unlock;
  1492. }
  1493. wr_req->r_flags = CEPH_OSD_FLAG_WRITE |
  1494. CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK;
  1495. osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
  1496. wr_req->r_base_oloc.pool = pool;
  1497. wr_req->r_base_oid = rd_req->r_base_oid;
  1498. /* one page should be large enough for STAT data */
  1499. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  1500. if (IS_ERR(pages)) {
  1501. err = PTR_ERR(pages);
  1502. goto out_unlock;
  1503. }
  1504. osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
  1505. 0, false, true);
  1506. ceph_osdc_build_request(rd_req, 0, NULL, CEPH_NOSNAP,
  1507. &ci->vfs_inode.i_mtime);
  1508. err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
  1509. ceph_osdc_build_request(wr_req, 0, NULL, CEPH_NOSNAP,
  1510. &ci->vfs_inode.i_mtime);
  1511. err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
  1512. if (!err)
  1513. err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
  1514. if (!err2)
  1515. err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
  1516. if (err >= 0 || err == -ENOENT)
  1517. have |= POOL_READ;
  1518. else if (err != -EPERM)
  1519. goto out_unlock;
  1520. if (err2 == 0 || err2 == -EEXIST)
  1521. have |= POOL_WRITE;
  1522. else if (err2 != -EPERM) {
  1523. err = err2;
  1524. goto out_unlock;
  1525. }
  1526. perm = kmalloc(sizeof(*perm), GFP_NOFS);
  1527. if (!perm) {
  1528. err = -ENOMEM;
  1529. goto out_unlock;
  1530. }
  1531. perm->pool = pool;
  1532. perm->perm = have;
  1533. rb_link_node(&perm->node, parent, p);
  1534. rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
  1535. err = 0;
  1536. out_unlock:
  1537. up_write(&mdsc->pool_perm_rwsem);
  1538. if (rd_req)
  1539. ceph_osdc_put_request(rd_req);
  1540. if (wr_req)
  1541. ceph_osdc_put_request(wr_req);
  1542. out:
  1543. if (!err)
  1544. err = have;
  1545. dout("__ceph_pool_perm_get pool %u result = %d\n", pool, err);
  1546. return err;
  1547. }
  1548. int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
  1549. {
  1550. u32 pool;
  1551. int ret, flags;
  1552. if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
  1553. NOPOOLPERM))
  1554. return 0;
  1555. spin_lock(&ci->i_ceph_lock);
  1556. flags = ci->i_ceph_flags;
  1557. pool = ceph_file_layout_pg_pool(ci->i_layout);
  1558. spin_unlock(&ci->i_ceph_lock);
  1559. check:
  1560. if (flags & CEPH_I_POOL_PERM) {
  1561. if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
  1562. dout("ceph_pool_perm_check pool %u no read perm\n",
  1563. pool);
  1564. return -EPERM;
  1565. }
  1566. if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
  1567. dout("ceph_pool_perm_check pool %u no write perm\n",
  1568. pool);
  1569. return -EPERM;
  1570. }
  1571. return 0;
  1572. }
  1573. ret = __ceph_pool_perm_get(ci, pool);
  1574. if (ret < 0)
  1575. return ret;
  1576. flags = CEPH_I_POOL_PERM;
  1577. if (ret & POOL_READ)
  1578. flags |= CEPH_I_POOL_RD;
  1579. if (ret & POOL_WRITE)
  1580. flags |= CEPH_I_POOL_WR;
  1581. spin_lock(&ci->i_ceph_lock);
  1582. if (pool == ceph_file_layout_pg_pool(ci->i_layout)) {
  1583. ci->i_ceph_flags = flags;
  1584. } else {
  1585. pool = ceph_file_layout_pg_pool(ci->i_layout);
  1586. flags = ci->i_ceph_flags;
  1587. }
  1588. spin_unlock(&ci->i_ceph_lock);
  1589. goto check;
  1590. }
  1591. void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
  1592. {
  1593. struct ceph_pool_perm *perm;
  1594. struct rb_node *n;
  1595. while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
  1596. n = rb_first(&mdsc->pool_perm_tree);
  1597. perm = rb_entry(n, struct ceph_pool_perm, node);
  1598. rb_erase(n, &mdsc->pool_perm_tree);
  1599. kfree(perm);
  1600. }
  1601. }