addr.c 53 KB

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