addr.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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_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. dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
  156. page, page->index, PageDirty(page) ? "" : "not ");
  157. /* Can we release the page from the cache? */
  158. if (!ceph_release_fscache_page(page, g))
  159. return 0;
  160. return !PagePrivate(page);
  161. }
  162. /*
  163. * read a single page, without unlocking it.
  164. */
  165. static int ceph_do_readpage(struct file *filp, struct page *page)
  166. {
  167. struct inode *inode = file_inode(filp);
  168. struct ceph_inode_info *ci = ceph_inode(inode);
  169. struct ceph_osd_client *osdc =
  170. &ceph_inode_to_client(inode)->client->osdc;
  171. int err = 0;
  172. u64 off = page_offset(page);
  173. u64 len = PAGE_SIZE;
  174. if (off >= i_size_read(inode)) {
  175. zero_user_segment(page, 0, PAGE_SIZE);
  176. SetPageUptodate(page);
  177. return 0;
  178. }
  179. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  180. /*
  181. * Uptodate inline data should have been added
  182. * into page cache while getting Fcr caps.
  183. */
  184. if (off == 0)
  185. return -EINVAL;
  186. zero_user_segment(page, 0, PAGE_SIZE);
  187. SetPageUptodate(page);
  188. return 0;
  189. }
  190. err = ceph_readpage_from_fscache(inode, page);
  191. if (err == 0)
  192. return -EINPROGRESS;
  193. dout("readpage inode %p file %p page %p index %lu\n",
  194. inode, filp, page, page->index);
  195. err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
  196. off, &len,
  197. ci->i_truncate_seq, ci->i_truncate_size,
  198. &page, 1, 0);
  199. if (err == -ENOENT)
  200. err = 0;
  201. if (err < 0) {
  202. SetPageError(page);
  203. ceph_fscache_readpage_cancel(inode, page);
  204. goto out;
  205. }
  206. if (err < PAGE_SIZE)
  207. /* zero fill remainder of page */
  208. zero_user_segment(page, err, PAGE_SIZE);
  209. else
  210. flush_dcache_page(page);
  211. SetPageUptodate(page);
  212. ceph_readpage_to_fscache(inode, page);
  213. out:
  214. return err < 0 ? err : 0;
  215. }
  216. static int ceph_readpage(struct file *filp, struct page *page)
  217. {
  218. int r = ceph_do_readpage(filp, page);
  219. if (r != -EINPROGRESS)
  220. unlock_page(page);
  221. else
  222. r = 0;
  223. return r;
  224. }
  225. /*
  226. * Finish an async read(ahead) op.
  227. */
  228. static void finish_read(struct ceph_osd_request *req)
  229. {
  230. struct inode *inode = req->r_inode;
  231. struct ceph_osd_data *osd_data;
  232. int rc = req->r_result <= 0 ? req->r_result : 0;
  233. int bytes = req->r_result >= 0 ? req->r_result : 0;
  234. int num_pages;
  235. int i;
  236. dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
  237. /* unlock all pages, zeroing any data we didn't read */
  238. osd_data = osd_req_op_extent_osd_data(req, 0);
  239. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  240. num_pages = calc_pages_for((u64)osd_data->alignment,
  241. (u64)osd_data->length);
  242. for (i = 0; i < num_pages; i++) {
  243. struct page *page = osd_data->pages[i];
  244. if (rc < 0 && rc != -ENOENT) {
  245. ceph_fscache_readpage_cancel(inode, page);
  246. goto unlock;
  247. }
  248. if (bytes < (int)PAGE_SIZE) {
  249. /* zero (remainder of) page */
  250. int s = bytes < 0 ? 0 : bytes;
  251. zero_user_segment(page, s, PAGE_SIZE);
  252. }
  253. dout("finish_read %p uptodate %p idx %lu\n", inode, page,
  254. page->index);
  255. flush_dcache_page(page);
  256. SetPageUptodate(page);
  257. ceph_readpage_to_fscache(inode, page);
  258. unlock:
  259. unlock_page(page);
  260. put_page(page);
  261. bytes -= PAGE_SIZE;
  262. }
  263. kfree(osd_data->pages);
  264. }
  265. /*
  266. * start an async read(ahead) operation. return nr_pages we submitted
  267. * a read for on success, or negative error code.
  268. */
  269. static int start_read(struct inode *inode, struct list_head *page_list, int max)
  270. {
  271. struct ceph_osd_client *osdc =
  272. &ceph_inode_to_client(inode)->client->osdc;
  273. struct ceph_inode_info *ci = ceph_inode(inode);
  274. struct page *page = list_entry(page_list->prev, struct page, lru);
  275. struct ceph_vino vino;
  276. struct ceph_osd_request *req;
  277. u64 off;
  278. u64 len;
  279. int i;
  280. struct page **pages;
  281. pgoff_t next_index;
  282. int nr_pages = 0;
  283. int got = 0;
  284. int ret = 0;
  285. if (!current->journal_info) {
  286. /* caller of readpages does not hold buffer and read caps
  287. * (fadvise, madvise and readahead cases) */
  288. int want = CEPH_CAP_FILE_CACHE;
  289. ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
  290. if (ret < 0) {
  291. dout("start_read %p, error getting cap\n", inode);
  292. } else if (!(got & want)) {
  293. dout("start_read %p, no cache cap\n", inode);
  294. ret = 0;
  295. }
  296. if (ret <= 0) {
  297. if (got)
  298. ceph_put_cap_refs(ci, got);
  299. while (!list_empty(page_list)) {
  300. page = list_entry(page_list->prev,
  301. struct page, lru);
  302. list_del(&page->lru);
  303. put_page(page);
  304. }
  305. return ret;
  306. }
  307. }
  308. off = (u64) page_offset(page);
  309. /* count pages */
  310. next_index = page->index;
  311. list_for_each_entry_reverse(page, page_list, lru) {
  312. if (page->index != next_index)
  313. break;
  314. nr_pages++;
  315. next_index++;
  316. if (max && nr_pages == max)
  317. break;
  318. }
  319. len = nr_pages << PAGE_SHIFT;
  320. dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
  321. off, len);
  322. vino = ceph_vino(inode);
  323. req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
  324. 0, 1, CEPH_OSD_OP_READ,
  325. CEPH_OSD_FLAG_READ, NULL,
  326. ci->i_truncate_seq, ci->i_truncate_size,
  327. false);
  328. if (IS_ERR(req)) {
  329. ret = PTR_ERR(req);
  330. goto out;
  331. }
  332. /* build page vector */
  333. nr_pages = calc_pages_for(0, len);
  334. pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
  335. if (!pages) {
  336. ret = -ENOMEM;
  337. goto out_put;
  338. }
  339. for (i = 0; i < nr_pages; ++i) {
  340. page = list_entry(page_list->prev, struct page, lru);
  341. BUG_ON(PageLocked(page));
  342. list_del(&page->lru);
  343. dout("start_read %p adding %p idx %lu\n", inode, page,
  344. page->index);
  345. if (add_to_page_cache_lru(page, &inode->i_data, page->index,
  346. GFP_KERNEL)) {
  347. ceph_fscache_uncache_page(inode, page);
  348. put_page(page);
  349. dout("start_read %p add_to_page_cache failed %p\n",
  350. inode, page);
  351. nr_pages = i;
  352. if (nr_pages > 0) {
  353. len = nr_pages << PAGE_SHIFT;
  354. osd_req_op_extent_update(req, 0, len);
  355. break;
  356. }
  357. goto out_pages;
  358. }
  359. pages[i] = page;
  360. }
  361. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
  362. req->r_callback = finish_read;
  363. req->r_inode = inode;
  364. dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
  365. ret = ceph_osdc_start_request(osdc, req, false);
  366. if (ret < 0)
  367. goto out_pages;
  368. ceph_osdc_put_request(req);
  369. /* After adding locked pages to page cache, the inode holds cache cap.
  370. * So we can drop our cap refs. */
  371. if (got)
  372. ceph_put_cap_refs(ci, got);
  373. return nr_pages;
  374. out_pages:
  375. for (i = 0; i < nr_pages; ++i) {
  376. ceph_fscache_readpage_cancel(inode, pages[i]);
  377. unlock_page(pages[i]);
  378. }
  379. ceph_put_page_vector(pages, nr_pages, false);
  380. out_put:
  381. ceph_osdc_put_request(req);
  382. out:
  383. if (got)
  384. ceph_put_cap_refs(ci, got);
  385. return ret;
  386. }
  387. /*
  388. * Read multiple pages. Leave pages we don't read + unlock in page_list;
  389. * the caller (VM) cleans them up.
  390. */
  391. static int ceph_readpages(struct file *file, struct address_space *mapping,
  392. struct list_head *page_list, unsigned nr_pages)
  393. {
  394. struct inode *inode = file_inode(file);
  395. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  396. int rc = 0;
  397. int max = 0;
  398. if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
  399. return -EINVAL;
  400. rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
  401. &nr_pages);
  402. if (rc == 0)
  403. goto out;
  404. if (fsc->mount_options->rsize >= PAGE_SIZE)
  405. max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
  406. >> PAGE_SHIFT;
  407. dout("readpages %p file %p nr_pages %d max %d\n", inode,
  408. file, nr_pages,
  409. max);
  410. while (!list_empty(page_list)) {
  411. rc = start_read(inode, page_list, max);
  412. if (rc < 0)
  413. goto out;
  414. }
  415. out:
  416. ceph_fscache_readpages_cancel(inode, page_list);
  417. dout("readpages %p file %p ret %d\n", inode, file, rc);
  418. return rc;
  419. }
  420. /*
  421. * Get ref for the oldest snapc for an inode with dirty data... that is, the
  422. * only snap context we are allowed to write back.
  423. */
  424. static struct ceph_snap_context *get_oldest_context(struct inode *inode,
  425. loff_t *snap_size)
  426. {
  427. struct ceph_inode_info *ci = ceph_inode(inode);
  428. struct ceph_snap_context *snapc = NULL;
  429. struct ceph_cap_snap *capsnap = NULL;
  430. spin_lock(&ci->i_ceph_lock);
  431. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  432. dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
  433. capsnap->context, capsnap->dirty_pages);
  434. if (capsnap->dirty_pages) {
  435. snapc = ceph_get_snap_context(capsnap->context);
  436. if (snap_size)
  437. *snap_size = capsnap->size;
  438. break;
  439. }
  440. }
  441. if (!snapc && ci->i_wrbuffer_ref_head) {
  442. snapc = ceph_get_snap_context(ci->i_head_snapc);
  443. dout(" head snapc %p has %d dirty pages\n",
  444. snapc, ci->i_wrbuffer_ref_head);
  445. }
  446. spin_unlock(&ci->i_ceph_lock);
  447. return snapc;
  448. }
  449. /*
  450. * Write a single page, but leave the page locked.
  451. *
  452. * If we get a write error, set the page error bit, but still adjust the
  453. * dirty page accounting (i.e., page is no longer dirty).
  454. */
  455. static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
  456. {
  457. struct inode *inode;
  458. struct ceph_inode_info *ci;
  459. struct ceph_fs_client *fsc;
  460. struct ceph_osd_client *osdc;
  461. struct ceph_snap_context *snapc, *oldest;
  462. loff_t page_off = page_offset(page);
  463. loff_t snap_size = -1;
  464. long writeback_stat;
  465. u64 truncate_size;
  466. u32 truncate_seq;
  467. int err = 0, len = PAGE_SIZE;
  468. dout("writepage %p idx %lu\n", page, page->index);
  469. if (!page->mapping || !page->mapping->host) {
  470. dout("writepage %p - no mapping\n", page);
  471. return -EFAULT;
  472. }
  473. inode = page->mapping->host;
  474. ci = ceph_inode(inode);
  475. fsc = ceph_inode_to_client(inode);
  476. osdc = &fsc->client->osdc;
  477. /* verify this is a writeable snap context */
  478. snapc = page_snap_context(page);
  479. if (snapc == NULL) {
  480. dout("writepage %p page %p not dirty?\n", inode, page);
  481. goto out;
  482. }
  483. oldest = get_oldest_context(inode, &snap_size);
  484. if (snapc->seq > oldest->seq) {
  485. dout("writepage %p page %p snapc %p not writeable - noop\n",
  486. inode, page, snapc);
  487. /* we should only noop if called by kswapd */
  488. WARN_ON((current->flags & PF_MEMALLOC) == 0);
  489. ceph_put_snap_context(oldest);
  490. goto out;
  491. }
  492. ceph_put_snap_context(oldest);
  493. spin_lock(&ci->i_ceph_lock);
  494. truncate_seq = ci->i_truncate_seq;
  495. truncate_size = ci->i_truncate_size;
  496. if (snap_size == -1)
  497. snap_size = i_size_read(inode);
  498. spin_unlock(&ci->i_ceph_lock);
  499. /* is this a partial page at end of file? */
  500. if (page_off >= snap_size) {
  501. dout("%p page eof %llu\n", page, snap_size);
  502. goto out;
  503. }
  504. if (snap_size < page_off + len)
  505. len = snap_size - page_off;
  506. dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
  507. inode, page, page->index, page_off, len, snapc);
  508. writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
  509. if (writeback_stat >
  510. CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
  511. set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
  512. set_page_writeback(page);
  513. err = ceph_osdc_writepages(osdc, ceph_vino(inode),
  514. &ci->i_layout, snapc,
  515. page_off, len,
  516. truncate_seq, truncate_size,
  517. &inode->i_mtime, &page, 1);
  518. if (err < 0) {
  519. struct writeback_control tmp_wbc;
  520. if (!wbc)
  521. wbc = &tmp_wbc;
  522. if (err == -ERESTARTSYS) {
  523. /* killed by SIGKILL */
  524. dout("writepage interrupted page %p\n", page);
  525. redirty_page_for_writepage(wbc, page);
  526. end_page_writeback(page);
  527. goto out;
  528. }
  529. dout("writepage setting page/mapping error %d %p\n",
  530. err, page);
  531. SetPageError(page);
  532. mapping_set_error(&inode->i_data, err);
  533. wbc->pages_skipped++;
  534. } else {
  535. dout("writepage cleaned page %p\n", page);
  536. err = 0; /* vfs expects us to return 0 */
  537. }
  538. page->private = 0;
  539. ClearPagePrivate(page);
  540. end_page_writeback(page);
  541. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  542. ceph_put_snap_context(snapc); /* page's reference */
  543. out:
  544. return err;
  545. }
  546. static int ceph_writepage(struct page *page, struct writeback_control *wbc)
  547. {
  548. int err;
  549. struct inode *inode = page->mapping->host;
  550. BUG_ON(!inode);
  551. ihold(inode);
  552. err = writepage_nounlock(page, wbc);
  553. if (err == -ERESTARTSYS) {
  554. /* direct memory reclaimer was killed by SIGKILL. return 0
  555. * to prevent caller from setting mapping/page error */
  556. err = 0;
  557. }
  558. unlock_page(page);
  559. iput(inode);
  560. return err;
  561. }
  562. /*
  563. * lame release_pages helper. release_pages() isn't exported to
  564. * modules.
  565. */
  566. static void ceph_release_pages(struct page **pages, int num)
  567. {
  568. struct pagevec pvec;
  569. int i;
  570. pagevec_init(&pvec, 0);
  571. for (i = 0; i < num; i++) {
  572. if (pagevec_add(&pvec, pages[i]) == 0)
  573. pagevec_release(&pvec);
  574. }
  575. pagevec_release(&pvec);
  576. }
  577. /*
  578. * async writeback completion handler.
  579. *
  580. * If we get an error, set the mapping error bit, but not the individual
  581. * page error bits.
  582. */
  583. static void writepages_finish(struct ceph_osd_request *req)
  584. {
  585. struct inode *inode = req->r_inode;
  586. struct ceph_inode_info *ci = ceph_inode(inode);
  587. struct ceph_osd_data *osd_data;
  588. struct page *page;
  589. int num_pages, total_pages = 0;
  590. int i, j;
  591. int rc = req->r_result;
  592. struct ceph_snap_context *snapc = req->r_snapc;
  593. struct address_space *mapping = inode->i_mapping;
  594. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  595. bool remove_page;
  596. dout("writepages_finish %p rc %d\n", inode, rc);
  597. if (rc < 0)
  598. mapping_set_error(mapping, rc);
  599. /*
  600. * We lost the cache cap, need to truncate the page before
  601. * it is unlocked, otherwise we'd truncate it later in the
  602. * page truncation thread, possibly losing some data that
  603. * raced its way in
  604. */
  605. remove_page = !(ceph_caps_issued(ci) &
  606. (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
  607. /* clean all pages */
  608. for (i = 0; i < req->r_num_ops; i++) {
  609. if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
  610. break;
  611. osd_data = osd_req_op_extent_osd_data(req, i);
  612. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  613. num_pages = calc_pages_for((u64)osd_data->alignment,
  614. (u64)osd_data->length);
  615. total_pages += num_pages;
  616. for (j = 0; j < num_pages; j++) {
  617. page = osd_data->pages[j];
  618. BUG_ON(!page);
  619. WARN_ON(!PageUptodate(page));
  620. if (atomic_long_dec_return(&fsc->writeback_count) <
  621. CONGESTION_OFF_THRESH(
  622. fsc->mount_options->congestion_kb))
  623. clear_bdi_congested(&fsc->backing_dev_info,
  624. BLK_RW_ASYNC);
  625. if (rc < 0)
  626. SetPageError(page);
  627. ceph_put_snap_context(page_snap_context(page));
  628. page->private = 0;
  629. ClearPagePrivate(page);
  630. dout("unlocking %p\n", page);
  631. end_page_writeback(page);
  632. if (remove_page)
  633. generic_error_remove_page(inode->i_mapping,
  634. page);
  635. unlock_page(page);
  636. }
  637. dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
  638. inode, osd_data->length, rc >= 0 ? num_pages : 0);
  639. ceph_release_pages(osd_data->pages, num_pages);
  640. }
  641. ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
  642. osd_data = osd_req_op_extent_osd_data(req, 0);
  643. if (osd_data->pages_from_pool)
  644. mempool_free(osd_data->pages,
  645. ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
  646. else
  647. kfree(osd_data->pages);
  648. ceph_osdc_put_request(req);
  649. }
  650. /*
  651. * initiate async writeback
  652. */
  653. static int ceph_writepages_start(struct address_space *mapping,
  654. struct writeback_control *wbc)
  655. {
  656. struct inode *inode = mapping->host;
  657. struct ceph_inode_info *ci = ceph_inode(inode);
  658. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  659. struct ceph_vino vino = ceph_vino(inode);
  660. pgoff_t index, start, end;
  661. int range_whole = 0;
  662. int should_loop = 1;
  663. pgoff_t max_pages = 0, max_pages_ever = 0;
  664. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  665. struct pagevec pvec;
  666. int done = 0;
  667. int rc = 0;
  668. unsigned int wsize = i_blocksize(inode);
  669. struct ceph_osd_request *req = NULL;
  670. int do_sync = 0;
  671. loff_t snap_size, i_size;
  672. u64 truncate_size;
  673. u32 truncate_seq;
  674. /*
  675. * Include a 'sync' in the OSD request if this is a data
  676. * integrity write (e.g., O_SYNC write or fsync()), or if our
  677. * cap is being revoked.
  678. */
  679. if ((wbc->sync_mode == WB_SYNC_ALL) ||
  680. ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
  681. do_sync = 1;
  682. dout("writepages_start %p dosync=%d (mode=%s)\n",
  683. inode, do_sync,
  684. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  685. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  686. if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  687. if (ci->i_wrbuffer_ref > 0) {
  688. pr_warn_ratelimited(
  689. "writepage_start %p %lld forced umount\n",
  690. inode, ceph_ino(inode));
  691. }
  692. mapping_set_error(mapping, -EIO);
  693. return -EIO; /* we're in a forced umount, don't write! */
  694. }
  695. if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
  696. wsize = fsc->mount_options->wsize;
  697. if (wsize < PAGE_SIZE)
  698. wsize = PAGE_SIZE;
  699. max_pages_ever = wsize >> PAGE_SHIFT;
  700. pagevec_init(&pvec, 0);
  701. /* where to start/end? */
  702. if (wbc->range_cyclic) {
  703. start = mapping->writeback_index; /* Start from prev offset */
  704. end = -1;
  705. dout(" cyclic, start at %lu\n", start);
  706. } else {
  707. start = wbc->range_start >> PAGE_SHIFT;
  708. end = wbc->range_end >> PAGE_SHIFT;
  709. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  710. range_whole = 1;
  711. should_loop = 0;
  712. dout(" not cyclic, %lu to %lu\n", start, end);
  713. }
  714. index = start;
  715. retry:
  716. /* find oldest snap context with dirty data */
  717. ceph_put_snap_context(snapc);
  718. snap_size = -1;
  719. snapc = get_oldest_context(inode, &snap_size);
  720. if (!snapc) {
  721. /* hmm, why does writepages get called when there
  722. is no dirty data? */
  723. dout(" no snap context with dirty data?\n");
  724. goto out;
  725. }
  726. dout(" oldest snapc is %p seq %lld (%d snaps)\n",
  727. snapc, snapc->seq, snapc->num_snaps);
  728. spin_lock(&ci->i_ceph_lock);
  729. truncate_seq = ci->i_truncate_seq;
  730. truncate_size = ci->i_truncate_size;
  731. i_size = i_size_read(inode);
  732. spin_unlock(&ci->i_ceph_lock);
  733. if (last_snapc && snapc != last_snapc) {
  734. /* if we switched to a newer snapc, restart our scan at the
  735. * start of the original file range. */
  736. dout(" snapc differs from last pass, restarting at %lu\n",
  737. index);
  738. index = start;
  739. }
  740. last_snapc = snapc;
  741. while (!done && index <= end) {
  742. unsigned i;
  743. int first;
  744. pgoff_t strip_unit_end = 0;
  745. int num_ops = 0, op_idx;
  746. int pvec_pages, locked_pages = 0;
  747. struct page **pages = NULL, **data_pages;
  748. mempool_t *pool = NULL; /* Becomes non-null if mempool used */
  749. struct page *page;
  750. int want;
  751. u64 offset = 0, len = 0;
  752. max_pages = max_pages_ever;
  753. get_more_pages:
  754. first = -1;
  755. want = min(end - index,
  756. min((pgoff_t)PAGEVEC_SIZE,
  757. max_pages - (pgoff_t)locked_pages) - 1)
  758. + 1;
  759. pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  760. PAGECACHE_TAG_DIRTY,
  761. want);
  762. dout("pagevec_lookup_tag got %d\n", pvec_pages);
  763. if (!pvec_pages && !locked_pages)
  764. break;
  765. for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
  766. page = pvec.pages[i];
  767. dout("? %p idx %lu\n", page, page->index);
  768. if (locked_pages == 0)
  769. lock_page(page); /* first page */
  770. else if (!trylock_page(page))
  771. break;
  772. /* only dirty pages, or our accounting breaks */
  773. if (unlikely(!PageDirty(page)) ||
  774. unlikely(page->mapping != mapping)) {
  775. dout("!dirty or !mapping %p\n", page);
  776. unlock_page(page);
  777. break;
  778. }
  779. if (!wbc->range_cyclic && page->index > end) {
  780. dout("end of range %p\n", page);
  781. done = 1;
  782. unlock_page(page);
  783. break;
  784. }
  785. if (strip_unit_end && (page->index > strip_unit_end)) {
  786. dout("end of strip unit %p\n", page);
  787. unlock_page(page);
  788. break;
  789. }
  790. if (wbc->sync_mode != WB_SYNC_NONE) {
  791. dout("waiting on writeback %p\n", page);
  792. wait_on_page_writeback(page);
  793. }
  794. if (page_offset(page) >=
  795. (snap_size == -1 ? i_size : snap_size)) {
  796. dout("%p page eof %llu\n", page,
  797. (snap_size == -1 ? i_size : snap_size));
  798. done = 1;
  799. unlock_page(page);
  800. break;
  801. }
  802. if (PageWriteback(page)) {
  803. dout("%p under writeback\n", page);
  804. unlock_page(page);
  805. break;
  806. }
  807. /* only if matching snap context */
  808. pgsnapc = page_snap_context(page);
  809. if (pgsnapc->seq > snapc->seq) {
  810. dout("page snapc %p %lld > oldest %p %lld\n",
  811. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  812. unlock_page(page);
  813. if (!locked_pages)
  814. continue; /* keep looking for snap */
  815. break;
  816. }
  817. if (!clear_page_dirty_for_io(page)) {
  818. dout("%p !clear_page_dirty_for_io\n", page);
  819. unlock_page(page);
  820. break;
  821. }
  822. /*
  823. * We have something to write. If this is
  824. * the first locked page this time through,
  825. * calculate max possinle write size and
  826. * allocate a page array
  827. */
  828. if (locked_pages == 0) {
  829. u64 objnum;
  830. u64 objoff;
  831. /* prepare async write request */
  832. offset = (u64)page_offset(page);
  833. len = wsize;
  834. rc = ceph_calc_file_object_mapping(&ci->i_layout,
  835. offset, len,
  836. &objnum, &objoff,
  837. &len);
  838. if (rc < 0) {
  839. unlock_page(page);
  840. break;
  841. }
  842. num_ops = 1 + do_sync;
  843. strip_unit_end = page->index +
  844. ((len - 1) >> PAGE_SHIFT);
  845. BUG_ON(pages);
  846. max_pages = calc_pages_for(0, (u64)len);
  847. pages = kmalloc(max_pages * sizeof (*pages),
  848. GFP_NOFS);
  849. if (!pages) {
  850. pool = fsc->wb_pagevec_pool;
  851. pages = mempool_alloc(pool, GFP_NOFS);
  852. BUG_ON(!pages);
  853. }
  854. len = 0;
  855. } else if (page->index !=
  856. (offset + len) >> PAGE_SHIFT) {
  857. if (num_ops >= (pool ? CEPH_OSD_SLAB_OPS :
  858. CEPH_OSD_MAX_OPS)) {
  859. redirty_page_for_writepage(wbc, page);
  860. unlock_page(page);
  861. break;
  862. }
  863. num_ops++;
  864. offset = (u64)page_offset(page);
  865. len = 0;
  866. }
  867. /* note position of first page in pvec */
  868. if (first < 0)
  869. first = i;
  870. dout("%p will write page %p idx %lu\n",
  871. inode, page, page->index);
  872. if (atomic_long_inc_return(&fsc->writeback_count) >
  873. CONGESTION_ON_THRESH(
  874. fsc->mount_options->congestion_kb)) {
  875. set_bdi_congested(&fsc->backing_dev_info,
  876. BLK_RW_ASYNC);
  877. }
  878. pages[locked_pages] = page;
  879. locked_pages++;
  880. len += PAGE_SIZE;
  881. }
  882. /* did we get anything? */
  883. if (!locked_pages)
  884. goto release_pvec_pages;
  885. if (i) {
  886. int j;
  887. BUG_ON(!locked_pages || first < 0);
  888. if (pvec_pages && i == pvec_pages &&
  889. locked_pages < max_pages) {
  890. dout("reached end pvec, trying for more\n");
  891. pagevec_reinit(&pvec);
  892. goto get_more_pages;
  893. }
  894. /* shift unused pages over in the pvec... we
  895. * will need to release them below. */
  896. for (j = i; j < pvec_pages; j++) {
  897. dout(" pvec leftover page %p\n", pvec.pages[j]);
  898. pvec.pages[j-i+first] = pvec.pages[j];
  899. }
  900. pvec.nr -= i-first;
  901. }
  902. new_request:
  903. offset = page_offset(pages[0]);
  904. len = wsize;
  905. req = ceph_osdc_new_request(&fsc->client->osdc,
  906. &ci->i_layout, vino,
  907. offset, &len, 0, num_ops,
  908. CEPH_OSD_OP_WRITE,
  909. CEPH_OSD_FLAG_WRITE |
  910. CEPH_OSD_FLAG_ONDISK,
  911. snapc, truncate_seq,
  912. truncate_size, false);
  913. if (IS_ERR(req)) {
  914. req = ceph_osdc_new_request(&fsc->client->osdc,
  915. &ci->i_layout, vino,
  916. offset, &len, 0,
  917. min(num_ops,
  918. CEPH_OSD_SLAB_OPS),
  919. CEPH_OSD_OP_WRITE,
  920. CEPH_OSD_FLAG_WRITE |
  921. CEPH_OSD_FLAG_ONDISK,
  922. snapc, truncate_seq,
  923. truncate_size, true);
  924. BUG_ON(IS_ERR(req));
  925. }
  926. BUG_ON(len < page_offset(pages[locked_pages - 1]) +
  927. PAGE_SIZE - offset);
  928. req->r_callback = writepages_finish;
  929. req->r_inode = inode;
  930. /* Format the osd request message and submit the write */
  931. len = 0;
  932. data_pages = pages;
  933. op_idx = 0;
  934. for (i = 0; i < locked_pages; i++) {
  935. u64 cur_offset = page_offset(pages[i]);
  936. if (offset + len != cur_offset) {
  937. if (op_idx + do_sync + 1 == req->r_num_ops)
  938. break;
  939. osd_req_op_extent_dup_last(req, op_idx,
  940. cur_offset - offset);
  941. dout("writepages got pages at %llu~%llu\n",
  942. offset, len);
  943. osd_req_op_extent_osd_data_pages(req, op_idx,
  944. data_pages, len, 0,
  945. !!pool, false);
  946. osd_req_op_extent_update(req, op_idx, len);
  947. len = 0;
  948. offset = cur_offset;
  949. data_pages = pages + i;
  950. op_idx++;
  951. }
  952. set_page_writeback(pages[i]);
  953. len += PAGE_SIZE;
  954. }
  955. if (snap_size != -1) {
  956. len = min(len, snap_size - offset);
  957. } else if (i == locked_pages) {
  958. /* writepages_finish() clears writeback pages
  959. * according to the data length, so make sure
  960. * data length covers all locked pages */
  961. u64 min_len = len + 1 - PAGE_SIZE;
  962. len = min(len, (u64)i_size_read(inode) - offset);
  963. len = max(len, min_len);
  964. }
  965. dout("writepages got pages at %llu~%llu\n", offset, len);
  966. osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
  967. 0, !!pool, false);
  968. osd_req_op_extent_update(req, op_idx, len);
  969. if (do_sync) {
  970. op_idx++;
  971. osd_req_op_init(req, op_idx, CEPH_OSD_OP_STARTSYNC, 0);
  972. }
  973. BUG_ON(op_idx + 1 != req->r_num_ops);
  974. pool = NULL;
  975. if (i < locked_pages) {
  976. BUG_ON(num_ops <= req->r_num_ops);
  977. num_ops -= req->r_num_ops;
  978. num_ops += do_sync;
  979. locked_pages -= i;
  980. /* allocate new pages array for next request */
  981. data_pages = pages;
  982. pages = kmalloc(locked_pages * sizeof (*pages),
  983. GFP_NOFS);
  984. if (!pages) {
  985. pool = fsc->wb_pagevec_pool;
  986. pages = mempool_alloc(pool, GFP_NOFS);
  987. BUG_ON(!pages);
  988. }
  989. memcpy(pages, data_pages + i,
  990. locked_pages * sizeof(*pages));
  991. memset(data_pages + i, 0,
  992. locked_pages * sizeof(*pages));
  993. } else {
  994. BUG_ON(num_ops != req->r_num_ops);
  995. index = pages[i - 1]->index + 1;
  996. /* request message now owns the pages array */
  997. pages = NULL;
  998. }
  999. req->r_mtime = inode->i_mtime;
  1000. rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
  1001. BUG_ON(rc);
  1002. req = NULL;
  1003. wbc->nr_to_write -= i;
  1004. if (pages)
  1005. goto new_request;
  1006. if (wbc->nr_to_write <= 0)
  1007. done = 1;
  1008. release_pvec_pages:
  1009. dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
  1010. pvec.nr ? pvec.pages[0] : NULL);
  1011. pagevec_release(&pvec);
  1012. if (locked_pages && !done)
  1013. goto retry;
  1014. }
  1015. if (should_loop && !done) {
  1016. /* more to do; loop back to beginning of file */
  1017. dout("writepages looping back to beginning of file\n");
  1018. should_loop = 0;
  1019. index = 0;
  1020. goto retry;
  1021. }
  1022. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1023. mapping->writeback_index = index;
  1024. out:
  1025. ceph_osdc_put_request(req);
  1026. ceph_put_snap_context(snapc);
  1027. dout("writepages done, rc = %d\n", rc);
  1028. return rc;
  1029. }
  1030. /*
  1031. * See if a given @snapc is either writeable, or already written.
  1032. */
  1033. static int context_is_writeable_or_written(struct inode *inode,
  1034. struct ceph_snap_context *snapc)
  1035. {
  1036. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL);
  1037. int ret = !oldest || snapc->seq <= oldest->seq;
  1038. ceph_put_snap_context(oldest);
  1039. return ret;
  1040. }
  1041. /*
  1042. * We are only allowed to write into/dirty the page if the page is
  1043. * clean, or already dirty within the same snap context.
  1044. *
  1045. * called with page locked.
  1046. * return success with page locked,
  1047. * or any failure (incl -EAGAIN) with page unlocked.
  1048. */
  1049. static int ceph_update_writeable_page(struct file *file,
  1050. loff_t pos, unsigned len,
  1051. struct page *page)
  1052. {
  1053. struct inode *inode = file_inode(file);
  1054. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1055. struct ceph_inode_info *ci = ceph_inode(inode);
  1056. loff_t page_off = pos & PAGE_MASK;
  1057. int pos_in_page = pos & ~PAGE_MASK;
  1058. int end_in_page = pos_in_page + len;
  1059. loff_t i_size;
  1060. int r;
  1061. struct ceph_snap_context *snapc, *oldest;
  1062. if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  1063. dout(" page %p forced umount\n", page);
  1064. unlock_page(page);
  1065. return -EIO;
  1066. }
  1067. retry_locked:
  1068. /* writepages currently holds page lock, but if we change that later, */
  1069. wait_on_page_writeback(page);
  1070. snapc = page_snap_context(page);
  1071. if (snapc && snapc != ci->i_head_snapc) {
  1072. /*
  1073. * this page is already dirty in another (older) snap
  1074. * context! is it writeable now?
  1075. */
  1076. oldest = get_oldest_context(inode, NULL);
  1077. if (snapc->seq > oldest->seq) {
  1078. ceph_put_snap_context(oldest);
  1079. dout(" page %p snapc %p not current or oldest\n",
  1080. page, snapc);
  1081. /*
  1082. * queue for writeback, and wait for snapc to
  1083. * be writeable or written
  1084. */
  1085. snapc = ceph_get_snap_context(snapc);
  1086. unlock_page(page);
  1087. ceph_queue_writeback(inode);
  1088. r = wait_event_killable(ci->i_cap_wq,
  1089. context_is_writeable_or_written(inode, snapc));
  1090. ceph_put_snap_context(snapc);
  1091. if (r == -ERESTARTSYS)
  1092. return r;
  1093. return -EAGAIN;
  1094. }
  1095. ceph_put_snap_context(oldest);
  1096. /* yay, writeable, do it now (without dropping page lock) */
  1097. dout(" page %p snapc %p not current, but oldest\n",
  1098. page, snapc);
  1099. if (!clear_page_dirty_for_io(page))
  1100. goto retry_locked;
  1101. r = writepage_nounlock(page, NULL);
  1102. if (r < 0)
  1103. goto fail_unlock;
  1104. goto retry_locked;
  1105. }
  1106. if (PageUptodate(page)) {
  1107. dout(" page %p already uptodate\n", page);
  1108. return 0;
  1109. }
  1110. /* full page? */
  1111. if (pos_in_page == 0 && len == PAGE_SIZE)
  1112. return 0;
  1113. /* past end of file? */
  1114. i_size = i_size_read(inode);
  1115. if (page_off >= i_size ||
  1116. (pos_in_page == 0 && (pos+len) >= i_size &&
  1117. end_in_page - pos_in_page != PAGE_SIZE)) {
  1118. dout(" zeroing %p 0 - %d and %d - %d\n",
  1119. page, pos_in_page, end_in_page, (int)PAGE_SIZE);
  1120. zero_user_segments(page,
  1121. 0, pos_in_page,
  1122. end_in_page, PAGE_SIZE);
  1123. return 0;
  1124. }
  1125. /* we need to read it. */
  1126. r = ceph_do_readpage(file, page);
  1127. if (r < 0) {
  1128. if (r == -EINPROGRESS)
  1129. return -EAGAIN;
  1130. goto fail_unlock;
  1131. }
  1132. goto retry_locked;
  1133. fail_unlock:
  1134. unlock_page(page);
  1135. return r;
  1136. }
  1137. /*
  1138. * We are only allowed to write into/dirty the page if the page is
  1139. * clean, or already dirty within the same snap context.
  1140. */
  1141. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  1142. loff_t pos, unsigned len, unsigned flags,
  1143. struct page **pagep, void **fsdata)
  1144. {
  1145. struct inode *inode = file_inode(file);
  1146. struct page *page;
  1147. pgoff_t index = pos >> PAGE_SHIFT;
  1148. int r;
  1149. do {
  1150. /* get a page */
  1151. page = grab_cache_page_write_begin(mapping, index, 0);
  1152. if (!page)
  1153. return -ENOMEM;
  1154. dout("write_begin file %p inode %p page %p %d~%d\n", file,
  1155. inode, page, (int)pos, (int)len);
  1156. r = ceph_update_writeable_page(file, pos, len, page);
  1157. if (r < 0)
  1158. put_page(page);
  1159. else
  1160. *pagep = page;
  1161. } while (r == -EAGAIN);
  1162. return r;
  1163. }
  1164. /*
  1165. * we don't do anything in here that simple_write_end doesn't do
  1166. * except adjust dirty page accounting
  1167. */
  1168. static int ceph_write_end(struct file *file, struct address_space *mapping,
  1169. loff_t pos, unsigned len, unsigned copied,
  1170. struct page *page, void *fsdata)
  1171. {
  1172. struct inode *inode = file_inode(file);
  1173. unsigned from = pos & (PAGE_SIZE - 1);
  1174. int check_cap = 0;
  1175. dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
  1176. inode, page, (int)pos, (int)copied, (int)len);
  1177. /* zero the stale part of the page if we did a short copy */
  1178. if (copied < len)
  1179. zero_user_segment(page, from+copied, len);
  1180. /* did file size increase? */
  1181. if (pos+copied > i_size_read(inode))
  1182. check_cap = ceph_inode_set_size(inode, pos+copied);
  1183. if (!PageUptodate(page))
  1184. SetPageUptodate(page);
  1185. set_page_dirty(page);
  1186. unlock_page(page);
  1187. put_page(page);
  1188. if (check_cap)
  1189. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
  1190. return copied;
  1191. }
  1192. /*
  1193. * we set .direct_IO to indicate direct io is supported, but since we
  1194. * intercept O_DIRECT reads and writes early, this function should
  1195. * never get called.
  1196. */
  1197. static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
  1198. {
  1199. WARN_ON(1);
  1200. return -EINVAL;
  1201. }
  1202. const struct address_space_operations ceph_aops = {
  1203. .readpage = ceph_readpage,
  1204. .readpages = ceph_readpages,
  1205. .writepage = ceph_writepage,
  1206. .writepages = ceph_writepages_start,
  1207. .write_begin = ceph_write_begin,
  1208. .write_end = ceph_write_end,
  1209. .set_page_dirty = ceph_set_page_dirty,
  1210. .invalidatepage = ceph_invalidatepage,
  1211. .releasepage = ceph_releasepage,
  1212. .direct_IO = ceph_direct_io,
  1213. };
  1214. static void ceph_block_sigs(sigset_t *oldset)
  1215. {
  1216. sigset_t mask;
  1217. siginitsetinv(&mask, sigmask(SIGKILL));
  1218. sigprocmask(SIG_BLOCK, &mask, oldset);
  1219. }
  1220. static void ceph_restore_sigs(sigset_t *oldset)
  1221. {
  1222. sigprocmask(SIG_SETMASK, oldset, NULL);
  1223. }
  1224. /*
  1225. * vm ops
  1226. */
  1227. static int ceph_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1228. {
  1229. struct inode *inode = file_inode(vma->vm_file);
  1230. struct ceph_inode_info *ci = ceph_inode(inode);
  1231. struct ceph_file_info *fi = vma->vm_file->private_data;
  1232. struct page *pinned_page = NULL;
  1233. loff_t off = vmf->pgoff << PAGE_SHIFT;
  1234. int want, got, ret;
  1235. sigset_t oldset;
  1236. ceph_block_sigs(&oldset);
  1237. dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
  1238. inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
  1239. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1240. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1241. else
  1242. want = CEPH_CAP_FILE_CACHE;
  1243. got = 0;
  1244. ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
  1245. if (ret < 0)
  1246. goto out_restore;
  1247. dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
  1248. inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
  1249. if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
  1250. ci->i_inline_version == CEPH_INLINE_NONE) {
  1251. current->journal_info = vma->vm_file;
  1252. ret = filemap_fault(vma, vmf);
  1253. current->journal_info = NULL;
  1254. } else
  1255. ret = -EAGAIN;
  1256. dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
  1257. inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
  1258. if (pinned_page)
  1259. put_page(pinned_page);
  1260. ceph_put_cap_refs(ci, got);
  1261. if (ret != -EAGAIN)
  1262. goto out_restore;
  1263. /* read inline data */
  1264. if (off >= PAGE_SIZE) {
  1265. /* does not support inline data > PAGE_SIZE */
  1266. ret = VM_FAULT_SIGBUS;
  1267. } else {
  1268. int ret1;
  1269. struct address_space *mapping = inode->i_mapping;
  1270. struct page *page = find_or_create_page(mapping, 0,
  1271. mapping_gfp_constraint(mapping,
  1272. ~__GFP_FS));
  1273. if (!page) {
  1274. ret = VM_FAULT_OOM;
  1275. goto out_inline;
  1276. }
  1277. ret1 = __ceph_do_getattr(inode, page,
  1278. CEPH_STAT_CAP_INLINE_DATA, true);
  1279. if (ret1 < 0 || off >= i_size_read(inode)) {
  1280. unlock_page(page);
  1281. put_page(page);
  1282. if (ret1 < 0)
  1283. ret = ret1;
  1284. else
  1285. ret = VM_FAULT_SIGBUS;
  1286. goto out_inline;
  1287. }
  1288. if (ret1 < PAGE_SIZE)
  1289. zero_user_segment(page, ret1, PAGE_SIZE);
  1290. else
  1291. flush_dcache_page(page);
  1292. SetPageUptodate(page);
  1293. vmf->page = page;
  1294. ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
  1295. out_inline:
  1296. dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
  1297. inode, off, (size_t)PAGE_SIZE, ret);
  1298. }
  1299. out_restore:
  1300. ceph_restore_sigs(&oldset);
  1301. if (ret < 0)
  1302. ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  1303. return ret;
  1304. }
  1305. /*
  1306. * Reuse write_begin here for simplicity.
  1307. */
  1308. static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1309. {
  1310. struct inode *inode = file_inode(vma->vm_file);
  1311. struct ceph_inode_info *ci = ceph_inode(inode);
  1312. struct ceph_file_info *fi = vma->vm_file->private_data;
  1313. struct ceph_cap_flush *prealloc_cf;
  1314. struct page *page = vmf->page;
  1315. loff_t off = page_offset(page);
  1316. loff_t size = i_size_read(inode);
  1317. size_t len;
  1318. int want, got, ret;
  1319. sigset_t oldset;
  1320. prealloc_cf = ceph_alloc_cap_flush();
  1321. if (!prealloc_cf)
  1322. return VM_FAULT_OOM;
  1323. ceph_block_sigs(&oldset);
  1324. if (ci->i_inline_version != CEPH_INLINE_NONE) {
  1325. struct page *locked_page = NULL;
  1326. if (off == 0) {
  1327. lock_page(page);
  1328. locked_page = page;
  1329. }
  1330. ret = ceph_uninline_data(vma->vm_file, locked_page);
  1331. if (locked_page)
  1332. unlock_page(locked_page);
  1333. if (ret < 0)
  1334. goto out_free;
  1335. }
  1336. if (off + PAGE_SIZE <= size)
  1337. len = PAGE_SIZE;
  1338. else
  1339. len = size & ~PAGE_MASK;
  1340. dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
  1341. inode, ceph_vinop(inode), off, len, size);
  1342. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1343. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1344. else
  1345. want = CEPH_CAP_FILE_BUFFER;
  1346. got = 0;
  1347. ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
  1348. &got, NULL);
  1349. if (ret < 0)
  1350. goto out_free;
  1351. dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
  1352. inode, off, len, ceph_cap_string(got));
  1353. /* Update time before taking page lock */
  1354. file_update_time(vma->vm_file);
  1355. do {
  1356. lock_page(page);
  1357. if ((off > size) || (page->mapping != inode->i_mapping)) {
  1358. unlock_page(page);
  1359. ret = VM_FAULT_NOPAGE;
  1360. break;
  1361. }
  1362. ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
  1363. if (ret >= 0) {
  1364. /* success. we'll keep the page locked. */
  1365. set_page_dirty(page);
  1366. ret = VM_FAULT_LOCKED;
  1367. }
  1368. } while (ret == -EAGAIN);
  1369. if (ret == VM_FAULT_LOCKED ||
  1370. ci->i_inline_version != CEPH_INLINE_NONE) {
  1371. int dirty;
  1372. spin_lock(&ci->i_ceph_lock);
  1373. ci->i_inline_version = CEPH_INLINE_NONE;
  1374. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1375. &prealloc_cf);
  1376. spin_unlock(&ci->i_ceph_lock);
  1377. if (dirty)
  1378. __mark_inode_dirty(inode, dirty);
  1379. }
  1380. dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
  1381. inode, off, len, ceph_cap_string(got), ret);
  1382. ceph_put_cap_refs(ci, got);
  1383. out_free:
  1384. ceph_restore_sigs(&oldset);
  1385. ceph_free_cap_flush(prealloc_cf);
  1386. if (ret < 0)
  1387. ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  1388. return ret;
  1389. }
  1390. void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
  1391. char *data, size_t len)
  1392. {
  1393. struct address_space *mapping = inode->i_mapping;
  1394. struct page *page;
  1395. if (locked_page) {
  1396. page = locked_page;
  1397. } else {
  1398. if (i_size_read(inode) == 0)
  1399. return;
  1400. page = find_or_create_page(mapping, 0,
  1401. mapping_gfp_constraint(mapping,
  1402. ~__GFP_FS));
  1403. if (!page)
  1404. return;
  1405. if (PageUptodate(page)) {
  1406. unlock_page(page);
  1407. put_page(page);
  1408. return;
  1409. }
  1410. }
  1411. dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
  1412. inode, ceph_vinop(inode), len, locked_page);
  1413. if (len > 0) {
  1414. void *kaddr = kmap_atomic(page);
  1415. memcpy(kaddr, data, len);
  1416. kunmap_atomic(kaddr);
  1417. }
  1418. if (page != locked_page) {
  1419. if (len < PAGE_SIZE)
  1420. zero_user_segment(page, len, PAGE_SIZE);
  1421. else
  1422. flush_dcache_page(page);
  1423. SetPageUptodate(page);
  1424. unlock_page(page);
  1425. put_page(page);
  1426. }
  1427. }
  1428. int ceph_uninline_data(struct file *filp, struct page *locked_page)
  1429. {
  1430. struct inode *inode = file_inode(filp);
  1431. struct ceph_inode_info *ci = ceph_inode(inode);
  1432. struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
  1433. struct ceph_osd_request *req;
  1434. struct page *page = NULL;
  1435. u64 len, inline_version;
  1436. int err = 0;
  1437. bool from_pagecache = false;
  1438. spin_lock(&ci->i_ceph_lock);
  1439. inline_version = ci->i_inline_version;
  1440. spin_unlock(&ci->i_ceph_lock);
  1441. dout("uninline_data %p %llx.%llx inline_version %llu\n",
  1442. inode, ceph_vinop(inode), inline_version);
  1443. if (inline_version == 1 || /* initial version, no data */
  1444. inline_version == CEPH_INLINE_NONE)
  1445. goto out;
  1446. if (locked_page) {
  1447. page = locked_page;
  1448. WARN_ON(!PageUptodate(page));
  1449. } else if (ceph_caps_issued(ci) &
  1450. (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
  1451. page = find_get_page(inode->i_mapping, 0);
  1452. if (page) {
  1453. if (PageUptodate(page)) {
  1454. from_pagecache = true;
  1455. lock_page(page);
  1456. } else {
  1457. put_page(page);
  1458. page = NULL;
  1459. }
  1460. }
  1461. }
  1462. if (page) {
  1463. len = i_size_read(inode);
  1464. if (len > PAGE_SIZE)
  1465. len = PAGE_SIZE;
  1466. } else {
  1467. page = __page_cache_alloc(GFP_NOFS);
  1468. if (!page) {
  1469. err = -ENOMEM;
  1470. goto out;
  1471. }
  1472. err = __ceph_do_getattr(inode, page,
  1473. CEPH_STAT_CAP_INLINE_DATA, true);
  1474. if (err < 0) {
  1475. /* no inline data */
  1476. if (err == -ENODATA)
  1477. err = 0;
  1478. goto out;
  1479. }
  1480. len = err;
  1481. }
  1482. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1483. ceph_vino(inode), 0, &len, 0, 1,
  1484. CEPH_OSD_OP_CREATE,
  1485. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1486. NULL, 0, 0, false);
  1487. if (IS_ERR(req)) {
  1488. err = PTR_ERR(req);
  1489. goto out;
  1490. }
  1491. req->r_mtime = inode->i_mtime;
  1492. err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1493. if (!err)
  1494. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1495. ceph_osdc_put_request(req);
  1496. if (err < 0)
  1497. goto out;
  1498. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1499. ceph_vino(inode), 0, &len, 1, 3,
  1500. CEPH_OSD_OP_WRITE,
  1501. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1502. NULL, ci->i_truncate_seq,
  1503. ci->i_truncate_size, false);
  1504. if (IS_ERR(req)) {
  1505. err = PTR_ERR(req);
  1506. goto out;
  1507. }
  1508. osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
  1509. {
  1510. __le64 xattr_buf = cpu_to_le64(inline_version);
  1511. err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
  1512. "inline_version", &xattr_buf,
  1513. sizeof(xattr_buf),
  1514. CEPH_OSD_CMPXATTR_OP_GT,
  1515. CEPH_OSD_CMPXATTR_MODE_U64);
  1516. if (err)
  1517. goto out_put;
  1518. }
  1519. {
  1520. char xattr_buf[32];
  1521. int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
  1522. "%llu", inline_version);
  1523. err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
  1524. "inline_version",
  1525. xattr_buf, xattr_len, 0, 0);
  1526. if (err)
  1527. goto out_put;
  1528. }
  1529. req->r_mtime = inode->i_mtime;
  1530. err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
  1531. if (!err)
  1532. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1533. out_put:
  1534. ceph_osdc_put_request(req);
  1535. if (err == -ECANCELED)
  1536. err = 0;
  1537. out:
  1538. if (page && page != locked_page) {
  1539. if (from_pagecache) {
  1540. unlock_page(page);
  1541. put_page(page);
  1542. } else
  1543. __free_pages(page, 0);
  1544. }
  1545. dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
  1546. inode, ceph_vinop(inode), inline_version, err);
  1547. return err;
  1548. }
  1549. static const struct vm_operations_struct ceph_vmops = {
  1550. .fault = ceph_filemap_fault,
  1551. .page_mkwrite = ceph_page_mkwrite,
  1552. };
  1553. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1554. {
  1555. struct address_space *mapping = file->f_mapping;
  1556. if (!mapping->a_ops->readpage)
  1557. return -ENOEXEC;
  1558. file_accessed(file);
  1559. vma->vm_ops = &ceph_vmops;
  1560. return 0;
  1561. }
  1562. enum {
  1563. POOL_READ = 1,
  1564. POOL_WRITE = 2,
  1565. };
  1566. static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
  1567. s64 pool, struct ceph_string *pool_ns)
  1568. {
  1569. struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
  1570. struct ceph_mds_client *mdsc = fsc->mdsc;
  1571. struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
  1572. struct rb_node **p, *parent;
  1573. struct ceph_pool_perm *perm;
  1574. struct page **pages;
  1575. size_t pool_ns_len;
  1576. int err = 0, err2 = 0, have = 0;
  1577. down_read(&mdsc->pool_perm_rwsem);
  1578. p = &mdsc->pool_perm_tree.rb_node;
  1579. while (*p) {
  1580. perm = rb_entry(*p, struct ceph_pool_perm, node);
  1581. if (pool < perm->pool)
  1582. p = &(*p)->rb_left;
  1583. else if (pool > perm->pool)
  1584. p = &(*p)->rb_right;
  1585. else {
  1586. int ret = ceph_compare_string(pool_ns,
  1587. perm->pool_ns,
  1588. perm->pool_ns_len);
  1589. if (ret < 0)
  1590. p = &(*p)->rb_left;
  1591. else if (ret > 0)
  1592. p = &(*p)->rb_right;
  1593. else {
  1594. have = perm->perm;
  1595. break;
  1596. }
  1597. }
  1598. }
  1599. up_read(&mdsc->pool_perm_rwsem);
  1600. if (*p)
  1601. goto out;
  1602. if (pool_ns)
  1603. dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
  1604. pool, (int)pool_ns->len, pool_ns->str);
  1605. else
  1606. dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
  1607. down_write(&mdsc->pool_perm_rwsem);
  1608. p = &mdsc->pool_perm_tree.rb_node;
  1609. parent = NULL;
  1610. while (*p) {
  1611. parent = *p;
  1612. perm = rb_entry(parent, struct ceph_pool_perm, node);
  1613. if (pool < perm->pool)
  1614. p = &(*p)->rb_left;
  1615. else if (pool > perm->pool)
  1616. p = &(*p)->rb_right;
  1617. else {
  1618. int ret = ceph_compare_string(pool_ns,
  1619. perm->pool_ns,
  1620. perm->pool_ns_len);
  1621. if (ret < 0)
  1622. p = &(*p)->rb_left;
  1623. else if (ret > 0)
  1624. p = &(*p)->rb_right;
  1625. else {
  1626. have = perm->perm;
  1627. break;
  1628. }
  1629. }
  1630. }
  1631. if (*p) {
  1632. up_write(&mdsc->pool_perm_rwsem);
  1633. goto out;
  1634. }
  1635. rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
  1636. 1, false, GFP_NOFS);
  1637. if (!rd_req) {
  1638. err = -ENOMEM;
  1639. goto out_unlock;
  1640. }
  1641. rd_req->r_flags = CEPH_OSD_FLAG_READ;
  1642. osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
  1643. rd_req->r_base_oloc.pool = pool;
  1644. if (pool_ns)
  1645. rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
  1646. ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
  1647. err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
  1648. if (err)
  1649. goto out_unlock;
  1650. wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
  1651. 1, false, GFP_NOFS);
  1652. if (!wr_req) {
  1653. err = -ENOMEM;
  1654. goto out_unlock;
  1655. }
  1656. wr_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ACK;
  1657. osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
  1658. ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
  1659. ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
  1660. err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
  1661. if (err)
  1662. goto out_unlock;
  1663. /* one page should be large enough for STAT data */
  1664. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  1665. if (IS_ERR(pages)) {
  1666. err = PTR_ERR(pages);
  1667. goto out_unlock;
  1668. }
  1669. osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
  1670. 0, false, true);
  1671. err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
  1672. wr_req->r_mtime = ci->vfs_inode.i_mtime;
  1673. err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
  1674. if (!err)
  1675. err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
  1676. if (!err2)
  1677. err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
  1678. if (err >= 0 || err == -ENOENT)
  1679. have |= POOL_READ;
  1680. else if (err != -EPERM)
  1681. goto out_unlock;
  1682. if (err2 == 0 || err2 == -EEXIST)
  1683. have |= POOL_WRITE;
  1684. else if (err2 != -EPERM) {
  1685. err = err2;
  1686. goto out_unlock;
  1687. }
  1688. pool_ns_len = pool_ns ? pool_ns->len : 0;
  1689. perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
  1690. if (!perm) {
  1691. err = -ENOMEM;
  1692. goto out_unlock;
  1693. }
  1694. perm->pool = pool;
  1695. perm->perm = have;
  1696. perm->pool_ns_len = pool_ns_len;
  1697. if (pool_ns_len > 0)
  1698. memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
  1699. perm->pool_ns[pool_ns_len] = 0;
  1700. rb_link_node(&perm->node, parent, p);
  1701. rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
  1702. err = 0;
  1703. out_unlock:
  1704. up_write(&mdsc->pool_perm_rwsem);
  1705. ceph_osdc_put_request(rd_req);
  1706. ceph_osdc_put_request(wr_req);
  1707. out:
  1708. if (!err)
  1709. err = have;
  1710. if (pool_ns)
  1711. dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
  1712. pool, (int)pool_ns->len, pool_ns->str, err);
  1713. else
  1714. dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
  1715. return err;
  1716. }
  1717. int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
  1718. {
  1719. s64 pool;
  1720. struct ceph_string *pool_ns;
  1721. int ret, flags;
  1722. if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
  1723. NOPOOLPERM))
  1724. return 0;
  1725. spin_lock(&ci->i_ceph_lock);
  1726. flags = ci->i_ceph_flags;
  1727. pool = ci->i_layout.pool_id;
  1728. spin_unlock(&ci->i_ceph_lock);
  1729. check:
  1730. if (flags & CEPH_I_POOL_PERM) {
  1731. if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
  1732. dout("ceph_pool_perm_check pool %lld no read perm\n",
  1733. pool);
  1734. return -EPERM;
  1735. }
  1736. if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
  1737. dout("ceph_pool_perm_check pool %lld no write perm\n",
  1738. pool);
  1739. return -EPERM;
  1740. }
  1741. return 0;
  1742. }
  1743. pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
  1744. ret = __ceph_pool_perm_get(ci, pool, pool_ns);
  1745. ceph_put_string(pool_ns);
  1746. if (ret < 0)
  1747. return ret;
  1748. flags = CEPH_I_POOL_PERM;
  1749. if (ret & POOL_READ)
  1750. flags |= CEPH_I_POOL_RD;
  1751. if (ret & POOL_WRITE)
  1752. flags |= CEPH_I_POOL_WR;
  1753. spin_lock(&ci->i_ceph_lock);
  1754. if (pool == ci->i_layout.pool_id &&
  1755. pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
  1756. ci->i_ceph_flags |= flags;
  1757. } else {
  1758. pool = ci->i_layout.pool_id;
  1759. flags = ci->i_ceph_flags;
  1760. }
  1761. spin_unlock(&ci->i_ceph_lock);
  1762. goto check;
  1763. }
  1764. void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
  1765. {
  1766. struct ceph_pool_perm *perm;
  1767. struct rb_node *n;
  1768. while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
  1769. n = rb_first(&mdsc->pool_perm_tree);
  1770. perm = rb_entry(n, struct ceph_pool_perm, node);
  1771. rb_erase(n, &mdsc->pool_perm_tree);
  1772. kfree(perm);
  1773. }
  1774. }