addr.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  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/swap.h>
  7. #include <linux/pagemap.h>
  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 <linux/iversion.h>
  13. #include <linux/ktime.h>
  14. #include <linux/netfs.h>
  15. #include <trace/events/netfs.h>
  16. #include "super.h"
  17. #include "mds_client.h"
  18. #include "cache.h"
  19. #include "metric.h"
  20. #include "crypto.h"
  21. #include <linux/ceph/osd_client.h>
  22. #include <linux/ceph/striper.h>
  23. /*
  24. * Ceph address space ops.
  25. *
  26. * There are a few funny things going on here.
  27. *
  28. * The page->private field is used to reference a struct
  29. * ceph_snap_context for _every_ dirty page. This indicates which
  30. * snapshot the page was logically dirtied in, and thus which snap
  31. * context needs to be associated with the osd write during writeback.
  32. *
  33. * Similarly, struct ceph_inode_info maintains a set of counters to
  34. * count dirty pages on the inode. In the absence of snapshots,
  35. * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
  36. *
  37. * When a snapshot is taken (that is, when the client receives
  38. * notification that a snapshot was taken), each inode with caps and
  39. * with dirty pages (dirty pages implies there is a cap) gets a new
  40. * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
  41. * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
  42. * moved to capsnap->dirty. (Unless a sync write is currently in
  43. * progress. In that case, the capsnap is said to be "pending", new
  44. * writes cannot start, and the capsnap isn't "finalized" until the
  45. * write completes (or fails) and a final size/mtime for the inode for
  46. * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
  47. *
  48. * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
  49. * we look for the first capsnap in i_cap_snaps and write out pages in
  50. * that snap context _only_. Then we move on to the next capsnap,
  51. * eventually reaching the "live" or "head" context (i.e., pages that
  52. * are not yet snapped) and are writing the most recently dirtied
  53. * pages.
  54. *
  55. * Invalidate and so forth must take care to ensure the dirty page
  56. * accounting is preserved.
  57. */
  58. #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
  59. #define CONGESTION_OFF_THRESH(congestion_kb) \
  60. (CONGESTION_ON_THRESH(congestion_kb) - \
  61. (CONGESTION_ON_THRESH(congestion_kb) >> 2))
  62. static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
  63. struct folio **foliop, void **_fsdata);
  64. static inline struct ceph_snap_context *page_snap_context(struct page *page)
  65. {
  66. if (PagePrivate(page))
  67. return (void *)page->private;
  68. return NULL;
  69. }
  70. /*
  71. * Dirty a page. Optimistically adjust accounting, on the assumption
  72. * that we won't race with invalidate. If we do, readjust.
  73. */
  74. static bool ceph_dirty_folio(struct address_space *mapping, struct folio *folio)
  75. {
  76. struct inode *inode = mapping->host;
  77. struct ceph_client *cl = ceph_inode_to_client(inode);
  78. struct ceph_inode_info *ci;
  79. struct ceph_snap_context *snapc;
  80. if (folio_test_dirty(folio)) {
  81. doutc(cl, "%llx.%llx %p idx %lu -- already dirty\n",
  82. ceph_vinop(inode), folio, folio->index);
  83. VM_BUG_ON_FOLIO(!folio_test_private(folio), folio);
  84. return false;
  85. }
  86. ci = ceph_inode(inode);
  87. /* dirty the head */
  88. spin_lock(&ci->i_ceph_lock);
  89. if (__ceph_have_pending_cap_snap(ci)) {
  90. struct ceph_cap_snap *capsnap =
  91. list_last_entry(&ci->i_cap_snaps,
  92. struct ceph_cap_snap,
  93. ci_item);
  94. snapc = ceph_get_snap_context(capsnap->context);
  95. capsnap->dirty_pages++;
  96. } else {
  97. BUG_ON(!ci->i_head_snapc);
  98. snapc = ceph_get_snap_context(ci->i_head_snapc);
  99. ++ci->i_wrbuffer_ref_head;
  100. }
  101. if (ci->i_wrbuffer_ref == 0)
  102. ihold(inode);
  103. ++ci->i_wrbuffer_ref;
  104. doutc(cl, "%llx.%llx %p idx %lu head %d/%d -> %d/%d "
  105. "snapc %p seq %lld (%d snaps)\n",
  106. ceph_vinop(inode), folio, folio->index,
  107. ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
  108. ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
  109. snapc, snapc->seq, snapc->num_snaps);
  110. spin_unlock(&ci->i_ceph_lock);
  111. /*
  112. * Reference snap context in folio->private. Also set
  113. * PagePrivate so that we get invalidate_folio callback.
  114. */
  115. VM_WARN_ON_FOLIO(folio->private, folio);
  116. folio_attach_private(folio, snapc);
  117. return ceph_fscache_dirty_folio(mapping, folio);
  118. }
  119. /*
  120. * If we are truncating the full folio (i.e. offset == 0), adjust the
  121. * dirty folio counters appropriately. Only called if there is private
  122. * data on the folio.
  123. */
  124. static void ceph_invalidate_folio(struct folio *folio, size_t offset,
  125. size_t length)
  126. {
  127. struct inode *inode = folio->mapping->host;
  128. struct ceph_client *cl = ceph_inode_to_client(inode);
  129. struct ceph_inode_info *ci = ceph_inode(inode);
  130. struct ceph_snap_context *snapc;
  131. if (offset != 0 || length != folio_size(folio)) {
  132. doutc(cl, "%llx.%llx idx %lu partial dirty page %zu~%zu\n",
  133. ceph_vinop(inode), folio->index, offset, length);
  134. return;
  135. }
  136. WARN_ON(!folio_test_locked(folio));
  137. if (folio_test_private(folio)) {
  138. doutc(cl, "%llx.%llx idx %lu full dirty page\n",
  139. ceph_vinop(inode), folio->index);
  140. snapc = folio_detach_private(folio);
  141. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  142. ceph_put_snap_context(snapc);
  143. }
  144. netfs_invalidate_folio(folio, offset, length);
  145. }
  146. static void ceph_netfs_expand_readahead(struct netfs_io_request *rreq)
  147. {
  148. struct inode *inode = rreq->inode;
  149. struct ceph_inode_info *ci = ceph_inode(inode);
  150. struct ceph_file_layout *lo = &ci->i_layout;
  151. unsigned long max_pages = inode->i_sb->s_bdi->ra_pages;
  152. loff_t end = rreq->start + rreq->len, new_end;
  153. struct ceph_netfs_request_data *priv = rreq->netfs_priv;
  154. unsigned long max_len;
  155. u32 blockoff;
  156. if (priv) {
  157. /* Readahead is disabled by posix_fadvise POSIX_FADV_RANDOM */
  158. if (priv->file_ra_disabled)
  159. max_pages = 0;
  160. else
  161. max_pages = priv->file_ra_pages;
  162. }
  163. /* Readahead is disabled */
  164. if (!max_pages)
  165. return;
  166. max_len = max_pages << PAGE_SHIFT;
  167. /*
  168. * Try to expand the length forward by rounding up it to the next
  169. * block, but do not exceed the file size, unless the original
  170. * request already exceeds it.
  171. */
  172. new_end = umin(round_up(end, lo->stripe_unit), rreq->i_size);
  173. if (new_end > end && new_end <= rreq->start + max_len)
  174. rreq->len = new_end - rreq->start;
  175. /* Try to expand the start downward */
  176. div_u64_rem(rreq->start, lo->stripe_unit, &blockoff);
  177. if (rreq->len + blockoff <= max_len) {
  178. rreq->start -= blockoff;
  179. rreq->len += blockoff;
  180. }
  181. }
  182. static void finish_netfs_read(struct ceph_osd_request *req)
  183. {
  184. struct inode *inode = req->r_inode;
  185. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  186. struct ceph_client *cl = fsc->client;
  187. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  188. struct netfs_io_subrequest *subreq = req->r_priv;
  189. struct ceph_osd_req_op *op = &req->r_ops[0];
  190. int err = req->r_result;
  191. bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
  192. ceph_update_read_metrics(&fsc->mdsc->metric, req->r_start_latency,
  193. req->r_end_latency, osd_data->length, err);
  194. doutc(cl, "result %d subreq->len=%zu i_size=%lld\n", req->r_result,
  195. subreq->len, i_size_read(req->r_inode));
  196. /* no object means success but no data */
  197. if (err == -ENOENT)
  198. err = 0;
  199. else if (err == -EBLOCKLISTED)
  200. fsc->blocklisted = true;
  201. if (err >= 0) {
  202. if (sparse && err > 0)
  203. err = ceph_sparse_ext_map_end(op);
  204. if (err < subreq->len &&
  205. subreq->rreq->origin != NETFS_DIO_READ)
  206. __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
  207. if (IS_ENCRYPTED(inode) && err > 0) {
  208. err = ceph_fscrypt_decrypt_extents(inode,
  209. osd_data->pages, subreq->start,
  210. op->extent.sparse_ext,
  211. op->extent.sparse_ext_cnt);
  212. if (err > subreq->len)
  213. err = subreq->len;
  214. }
  215. }
  216. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  217. ceph_put_page_vector(osd_data->pages,
  218. calc_pages_for(osd_data->alignment,
  219. osd_data->length), false);
  220. }
  221. if (err > 0) {
  222. subreq->transferred = err;
  223. err = 0;
  224. }
  225. trace_netfs_sreq(subreq, netfs_sreq_trace_io_progress);
  226. netfs_read_subreq_terminated(subreq, err, false);
  227. iput(req->r_inode);
  228. ceph_dec_osd_stopping_blocker(fsc->mdsc);
  229. }
  230. static bool ceph_netfs_issue_op_inline(struct netfs_io_subrequest *subreq)
  231. {
  232. struct netfs_io_request *rreq = subreq->rreq;
  233. struct inode *inode = rreq->inode;
  234. struct ceph_mds_reply_info_parsed *rinfo;
  235. struct ceph_mds_reply_info_in *iinfo;
  236. struct ceph_mds_request *req;
  237. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  238. struct ceph_inode_info *ci = ceph_inode(inode);
  239. ssize_t err = 0;
  240. size_t len;
  241. int mode;
  242. if (rreq->origin != NETFS_DIO_READ)
  243. __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
  244. __clear_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
  245. if (subreq->start >= inode->i_size)
  246. goto out;
  247. /* We need to fetch the inline data. */
  248. mode = ceph_try_to_choose_auth_mds(inode, CEPH_STAT_CAP_INLINE_DATA);
  249. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
  250. if (IS_ERR(req)) {
  251. err = PTR_ERR(req);
  252. goto out;
  253. }
  254. req->r_ino1 = ci->i_vino;
  255. req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INLINE_DATA);
  256. req->r_num_caps = 2;
  257. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  258. err = ceph_mdsc_do_request(mdsc, NULL, req);
  259. if (err < 0)
  260. goto out;
  261. rinfo = &req->r_reply_info;
  262. iinfo = &rinfo->targeti;
  263. if (iinfo->inline_version == CEPH_INLINE_NONE) {
  264. /* The data got uninlined */
  265. ceph_mdsc_put_request(req);
  266. return false;
  267. }
  268. len = min_t(size_t, iinfo->inline_len - subreq->start, subreq->len);
  269. err = copy_to_iter(iinfo->inline_data + subreq->start, len, &subreq->io_iter);
  270. if (err == 0) {
  271. err = -EFAULT;
  272. } else {
  273. subreq->transferred += err;
  274. err = 0;
  275. }
  276. ceph_mdsc_put_request(req);
  277. out:
  278. netfs_read_subreq_terminated(subreq, err, false);
  279. return true;
  280. }
  281. static int ceph_netfs_prepare_read(struct netfs_io_subrequest *subreq)
  282. {
  283. struct netfs_io_request *rreq = subreq->rreq;
  284. struct inode *inode = rreq->inode;
  285. struct ceph_inode_info *ci = ceph_inode(inode);
  286. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  287. u64 objno, objoff;
  288. u32 xlen;
  289. /* Truncate the extent at the end of the current block */
  290. ceph_calc_file_object_mapping(&ci->i_layout, subreq->start, subreq->len,
  291. &objno, &objoff, &xlen);
  292. rreq->io_streams[0].sreq_max_len = umin(xlen, fsc->mount_options->rsize);
  293. return 0;
  294. }
  295. static void ceph_netfs_issue_read(struct netfs_io_subrequest *subreq)
  296. {
  297. struct netfs_io_request *rreq = subreq->rreq;
  298. struct inode *inode = rreq->inode;
  299. struct ceph_inode_info *ci = ceph_inode(inode);
  300. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  301. struct ceph_client *cl = fsc->client;
  302. struct ceph_osd_request *req = NULL;
  303. struct ceph_vino vino = ceph_vino(inode);
  304. int err;
  305. u64 len;
  306. bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
  307. u64 off = subreq->start;
  308. int extent_cnt;
  309. if (ceph_inode_is_shutdown(inode)) {
  310. err = -EIO;
  311. goto out;
  312. }
  313. if (ceph_has_inline_data(ci) && ceph_netfs_issue_op_inline(subreq))
  314. return;
  315. // TODO: This rounding here is slightly dodgy. It *should* work, for
  316. // now, as the cache only deals in blocks that are a multiple of
  317. // PAGE_SIZE and fscrypt blocks are at most PAGE_SIZE. What needs to
  318. // happen is for the fscrypt driving to be moved into netfslib and the
  319. // data in the cache also to be stored encrypted.
  320. len = subreq->len;
  321. ceph_fscrypt_adjust_off_and_len(inode, &off, &len);
  322. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, vino,
  323. off, &len, 0, 1, sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ,
  324. CEPH_OSD_FLAG_READ, NULL, ci->i_truncate_seq,
  325. ci->i_truncate_size, false);
  326. if (IS_ERR(req)) {
  327. err = PTR_ERR(req);
  328. req = NULL;
  329. goto out;
  330. }
  331. if (sparse) {
  332. extent_cnt = __ceph_sparse_read_ext_count(inode, len);
  333. err = ceph_alloc_sparse_ext_map(&req->r_ops[0], extent_cnt);
  334. if (err)
  335. goto out;
  336. }
  337. doutc(cl, "%llx.%llx pos=%llu orig_len=%zu len=%llu\n",
  338. ceph_vinop(inode), subreq->start, subreq->len, len);
  339. /*
  340. * FIXME: For now, use CEPH_OSD_DATA_TYPE_PAGES instead of _ITER for
  341. * encrypted inodes. We'd need infrastructure that handles an iov_iter
  342. * instead of page arrays, and we don't have that as of yet. Once the
  343. * dust settles on the write helpers and encrypt/decrypt routines for
  344. * netfs, we should be able to rework this.
  345. */
  346. if (IS_ENCRYPTED(inode)) {
  347. struct page **pages;
  348. size_t page_off;
  349. /*
  350. * FIXME: io_iter.count needs to be corrected to aligned
  351. * length. Otherwise, iov_iter_get_pages_alloc2() operates
  352. * with the initial unaligned length value. As a result,
  353. * ceph_msg_data_cursor_init() triggers BUG_ON() in the case
  354. * if msg->sparse_read_total > msg->data_length.
  355. */
  356. subreq->io_iter.count = len;
  357. err = iov_iter_get_pages_alloc2(&subreq->io_iter, &pages, len, &page_off);
  358. if (err < 0) {
  359. doutc(cl, "%llx.%llx failed to allocate pages, %d\n",
  360. ceph_vinop(inode), err);
  361. goto out;
  362. }
  363. /* should always give us a page-aligned read */
  364. WARN_ON_ONCE(page_off);
  365. len = err;
  366. err = 0;
  367. osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false,
  368. false);
  369. } else {
  370. osd_req_op_extent_osd_iter(req, 0, &subreq->io_iter);
  371. }
  372. if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
  373. err = -EIO;
  374. goto out;
  375. }
  376. req->r_callback = finish_netfs_read;
  377. req->r_priv = subreq;
  378. req->r_inode = inode;
  379. ihold(inode);
  380. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  381. ceph_osdc_start_request(req->r_osdc, req);
  382. out:
  383. ceph_osdc_put_request(req);
  384. if (err)
  385. netfs_read_subreq_terminated(subreq, err, false);
  386. doutc(cl, "%llx.%llx result %d\n", ceph_vinop(inode), err);
  387. }
  388. static int ceph_init_request(struct netfs_io_request *rreq, struct file *file)
  389. {
  390. struct inode *inode = rreq->inode;
  391. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  392. struct ceph_client *cl = ceph_inode_to_client(inode);
  393. int got = 0, want = CEPH_CAP_FILE_CACHE;
  394. struct ceph_netfs_request_data *priv;
  395. int ret = 0;
  396. /* [DEPRECATED] Use PG_private_2 to mark folio being written to the cache. */
  397. __set_bit(NETFS_RREQ_USE_PGPRIV2, &rreq->flags);
  398. if (rreq->origin != NETFS_READAHEAD)
  399. return 0;
  400. priv = kzalloc(sizeof(*priv), GFP_NOFS);
  401. if (!priv)
  402. return -ENOMEM;
  403. if (file) {
  404. struct ceph_rw_context *rw_ctx;
  405. struct ceph_file_info *fi = file->private_data;
  406. priv->file_ra_pages = file->f_ra.ra_pages;
  407. priv->file_ra_disabled = file->f_mode & FMODE_RANDOM;
  408. rw_ctx = ceph_find_rw_context(fi);
  409. if (rw_ctx) {
  410. rreq->netfs_priv = priv;
  411. return 0;
  412. }
  413. }
  414. /*
  415. * readahead callers do not necessarily hold Fcb caps
  416. * (e.g. fadvise, madvise).
  417. */
  418. ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want, true, &got);
  419. if (ret < 0) {
  420. doutc(cl, "%llx.%llx, error getting cap\n", ceph_vinop(inode));
  421. goto out;
  422. }
  423. if (!(got & want)) {
  424. doutc(cl, "%llx.%llx, no cache cap\n", ceph_vinop(inode));
  425. ret = -EACCES;
  426. goto out;
  427. }
  428. if (ret == 0) {
  429. ret = -EACCES;
  430. goto out;
  431. }
  432. priv->caps = got;
  433. rreq->netfs_priv = priv;
  434. rreq->io_streams[0].sreq_max_len = fsc->mount_options->rsize;
  435. out:
  436. if (ret < 0) {
  437. if (got)
  438. ceph_put_cap_refs(ceph_inode(inode), got);
  439. kfree(priv);
  440. }
  441. return ret;
  442. }
  443. static void ceph_netfs_free_request(struct netfs_io_request *rreq)
  444. {
  445. struct ceph_netfs_request_data *priv = rreq->netfs_priv;
  446. if (!priv)
  447. return;
  448. if (priv->caps)
  449. ceph_put_cap_refs(ceph_inode(rreq->inode), priv->caps);
  450. kfree(priv);
  451. rreq->netfs_priv = NULL;
  452. }
  453. const struct netfs_request_ops ceph_netfs_ops = {
  454. .init_request = ceph_init_request,
  455. .free_request = ceph_netfs_free_request,
  456. .prepare_read = ceph_netfs_prepare_read,
  457. .issue_read = ceph_netfs_issue_read,
  458. .expand_readahead = ceph_netfs_expand_readahead,
  459. .check_write_begin = ceph_netfs_check_write_begin,
  460. };
  461. #ifdef CONFIG_CEPH_FSCACHE
  462. static void ceph_set_page_fscache(struct page *page)
  463. {
  464. folio_start_private_2(page_folio(page)); /* [DEPRECATED] */
  465. }
  466. static void ceph_fscache_write_terminated(void *priv, ssize_t error, bool was_async)
  467. {
  468. struct inode *inode = priv;
  469. if (IS_ERR_VALUE(error) && error != -ENOBUFS)
  470. ceph_fscache_invalidate(inode, false);
  471. }
  472. static void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
  473. {
  474. struct ceph_inode_info *ci = ceph_inode(inode);
  475. struct fscache_cookie *cookie = ceph_fscache_cookie(ci);
  476. fscache_write_to_cache(cookie, inode->i_mapping, off, len, i_size_read(inode),
  477. ceph_fscache_write_terminated, inode, true, caching);
  478. }
  479. #else
  480. static inline void ceph_set_page_fscache(struct page *page)
  481. {
  482. }
  483. static inline void ceph_fscache_write_to_cache(struct inode *inode, u64 off, u64 len, bool caching)
  484. {
  485. }
  486. #endif /* CONFIG_CEPH_FSCACHE */
  487. struct ceph_writeback_ctl
  488. {
  489. loff_t i_size;
  490. u64 truncate_size;
  491. u32 truncate_seq;
  492. bool size_stable;
  493. bool head_snapc;
  494. };
  495. /*
  496. * Get ref for the oldest snapc for an inode with dirty data... that is, the
  497. * only snap context we are allowed to write back.
  498. */
  499. static struct ceph_snap_context *
  500. get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
  501. struct ceph_snap_context *page_snapc)
  502. {
  503. struct ceph_inode_info *ci = ceph_inode(inode);
  504. struct ceph_client *cl = ceph_inode_to_client(inode);
  505. struct ceph_snap_context *snapc = NULL;
  506. struct ceph_cap_snap *capsnap = NULL;
  507. spin_lock(&ci->i_ceph_lock);
  508. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  509. doutc(cl, " capsnap %p snapc %p has %d dirty pages\n",
  510. capsnap, capsnap->context, capsnap->dirty_pages);
  511. if (!capsnap->dirty_pages)
  512. continue;
  513. /* get i_size, truncate_{seq,size} for page_snapc? */
  514. if (snapc && capsnap->context != page_snapc)
  515. continue;
  516. if (ctl) {
  517. if (capsnap->writing) {
  518. ctl->i_size = i_size_read(inode);
  519. ctl->size_stable = false;
  520. } else {
  521. ctl->i_size = capsnap->size;
  522. ctl->size_stable = true;
  523. }
  524. ctl->truncate_size = capsnap->truncate_size;
  525. ctl->truncate_seq = capsnap->truncate_seq;
  526. ctl->head_snapc = false;
  527. }
  528. if (snapc)
  529. break;
  530. snapc = ceph_get_snap_context(capsnap->context);
  531. if (!page_snapc ||
  532. page_snapc == snapc ||
  533. page_snapc->seq > snapc->seq)
  534. break;
  535. }
  536. if (!snapc && ci->i_wrbuffer_ref_head) {
  537. snapc = ceph_get_snap_context(ci->i_head_snapc);
  538. doutc(cl, " head snapc %p has %d dirty pages\n", snapc,
  539. ci->i_wrbuffer_ref_head);
  540. if (ctl) {
  541. ctl->i_size = i_size_read(inode);
  542. ctl->truncate_size = ci->i_truncate_size;
  543. ctl->truncate_seq = ci->i_truncate_seq;
  544. ctl->size_stable = false;
  545. ctl->head_snapc = true;
  546. }
  547. }
  548. spin_unlock(&ci->i_ceph_lock);
  549. return snapc;
  550. }
  551. static u64 get_writepages_data_length(struct inode *inode,
  552. struct page *page, u64 start)
  553. {
  554. struct ceph_inode_info *ci = ceph_inode(inode);
  555. struct ceph_snap_context *snapc;
  556. struct ceph_cap_snap *capsnap = NULL;
  557. u64 end = i_size_read(inode);
  558. u64 ret;
  559. snapc = page_snap_context(ceph_fscrypt_pagecache_page(page));
  560. if (snapc != ci->i_head_snapc) {
  561. bool found = false;
  562. spin_lock(&ci->i_ceph_lock);
  563. list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
  564. if (capsnap->context == snapc) {
  565. if (!capsnap->writing)
  566. end = capsnap->size;
  567. found = true;
  568. break;
  569. }
  570. }
  571. spin_unlock(&ci->i_ceph_lock);
  572. WARN_ON(!found);
  573. }
  574. if (end > ceph_fscrypt_page_offset(page) + thp_size(page))
  575. end = ceph_fscrypt_page_offset(page) + thp_size(page);
  576. ret = end > start ? end - start : 0;
  577. if (ret && fscrypt_is_bounce_page(page))
  578. ret = round_up(ret, CEPH_FSCRYPT_BLOCK_SIZE);
  579. return ret;
  580. }
  581. /*
  582. * Write a single page, but leave the page locked.
  583. *
  584. * If we get a write error, mark the mapping for error, but still adjust the
  585. * dirty page accounting (i.e., page is no longer dirty).
  586. */
  587. static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
  588. {
  589. struct folio *folio = page_folio(page);
  590. struct inode *inode = page->mapping->host;
  591. struct ceph_inode_info *ci = ceph_inode(inode);
  592. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  593. struct ceph_client *cl = fsc->client;
  594. struct ceph_snap_context *snapc, *oldest;
  595. loff_t page_off = page_offset(page);
  596. int err;
  597. loff_t len = thp_size(page);
  598. loff_t wlen;
  599. struct ceph_writeback_ctl ceph_wbc;
  600. struct ceph_osd_client *osdc = &fsc->client->osdc;
  601. struct ceph_osd_request *req;
  602. bool caching = ceph_is_cache_enabled(inode);
  603. struct page *bounce_page = NULL;
  604. doutc(cl, "%llx.%llx page %p idx %lu\n", ceph_vinop(inode), page,
  605. page->index);
  606. if (ceph_inode_is_shutdown(inode))
  607. return -EIO;
  608. /* verify this is a writeable snap context */
  609. snapc = page_snap_context(page);
  610. if (!snapc) {
  611. doutc(cl, "%llx.%llx page %p not dirty?\n", ceph_vinop(inode),
  612. page);
  613. return 0;
  614. }
  615. oldest = get_oldest_context(inode, &ceph_wbc, snapc);
  616. if (snapc->seq > oldest->seq) {
  617. doutc(cl, "%llx.%llx page %p snapc %p not writeable - noop\n",
  618. ceph_vinop(inode), page, snapc);
  619. /* we should only noop if called by kswapd */
  620. WARN_ON(!(current->flags & PF_MEMALLOC));
  621. ceph_put_snap_context(oldest);
  622. redirty_page_for_writepage(wbc, page);
  623. return 0;
  624. }
  625. ceph_put_snap_context(oldest);
  626. /* is this a partial page at end of file? */
  627. if (page_off >= ceph_wbc.i_size) {
  628. doutc(cl, "%llx.%llx folio at %lu beyond eof %llu\n",
  629. ceph_vinop(inode), folio->index, ceph_wbc.i_size);
  630. folio_invalidate(folio, 0, folio_size(folio));
  631. return 0;
  632. }
  633. if (ceph_wbc.i_size < page_off + len)
  634. len = ceph_wbc.i_size - page_off;
  635. wlen = IS_ENCRYPTED(inode) ? round_up(len, CEPH_FSCRYPT_BLOCK_SIZE) : len;
  636. doutc(cl, "%llx.%llx page %p index %lu on %llu~%llu snapc %p seq %lld\n",
  637. ceph_vinop(inode), page, page->index, page_off, wlen, snapc,
  638. snapc->seq);
  639. if (atomic_long_inc_return(&fsc->writeback_count) >
  640. CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
  641. fsc->write_congested = true;
  642. req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode),
  643. page_off, &wlen, 0, 1, CEPH_OSD_OP_WRITE,
  644. CEPH_OSD_FLAG_WRITE, snapc,
  645. ceph_wbc.truncate_seq,
  646. ceph_wbc.truncate_size, true);
  647. if (IS_ERR(req)) {
  648. redirty_page_for_writepage(wbc, page);
  649. return PTR_ERR(req);
  650. }
  651. if (wlen < len)
  652. len = wlen;
  653. set_page_writeback(page);
  654. if (caching)
  655. ceph_set_page_fscache(page);
  656. ceph_fscache_write_to_cache(inode, page_off, len, caching);
  657. if (IS_ENCRYPTED(inode)) {
  658. bounce_page = fscrypt_encrypt_pagecache_blocks(page,
  659. CEPH_FSCRYPT_BLOCK_SIZE, 0,
  660. GFP_NOFS);
  661. if (IS_ERR(bounce_page)) {
  662. redirty_page_for_writepage(wbc, page);
  663. end_page_writeback(page);
  664. ceph_osdc_put_request(req);
  665. return PTR_ERR(bounce_page);
  666. }
  667. }
  668. /* it may be a short write due to an object boundary */
  669. WARN_ON_ONCE(len > thp_size(page));
  670. osd_req_op_extent_osd_data_pages(req, 0,
  671. bounce_page ? &bounce_page : &page, wlen, 0,
  672. false, false);
  673. doutc(cl, "%llx.%llx %llu~%llu (%llu bytes, %sencrypted)\n",
  674. ceph_vinop(inode), page_off, len, wlen,
  675. IS_ENCRYPTED(inode) ? "" : "not ");
  676. req->r_mtime = inode_get_mtime(inode);
  677. ceph_osdc_start_request(osdc, req);
  678. err = ceph_osdc_wait_request(osdc, req);
  679. ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
  680. req->r_end_latency, len, err);
  681. fscrypt_free_bounce_page(bounce_page);
  682. ceph_osdc_put_request(req);
  683. if (err == 0)
  684. err = len;
  685. if (err < 0) {
  686. struct writeback_control tmp_wbc;
  687. if (!wbc)
  688. wbc = &tmp_wbc;
  689. if (err == -ERESTARTSYS) {
  690. /* killed by SIGKILL */
  691. doutc(cl, "%llx.%llx interrupted page %p\n",
  692. ceph_vinop(inode), page);
  693. redirty_page_for_writepage(wbc, page);
  694. end_page_writeback(page);
  695. return err;
  696. }
  697. if (err == -EBLOCKLISTED)
  698. fsc->blocklisted = true;
  699. doutc(cl, "%llx.%llx setting page/mapping error %d %p\n",
  700. ceph_vinop(inode), err, page);
  701. mapping_set_error(&inode->i_data, err);
  702. wbc->pages_skipped++;
  703. } else {
  704. doutc(cl, "%llx.%llx cleaned page %p\n",
  705. ceph_vinop(inode), page);
  706. err = 0; /* vfs expects us to return 0 */
  707. }
  708. oldest = detach_page_private(page);
  709. WARN_ON_ONCE(oldest != snapc);
  710. end_page_writeback(page);
  711. ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
  712. ceph_put_snap_context(snapc); /* page's reference */
  713. if (atomic_long_dec_return(&fsc->writeback_count) <
  714. CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
  715. fsc->write_congested = false;
  716. return err;
  717. }
  718. static int ceph_writepage(struct page *page, struct writeback_control *wbc)
  719. {
  720. int err;
  721. struct inode *inode = page->mapping->host;
  722. BUG_ON(!inode);
  723. ihold(inode);
  724. if (wbc->sync_mode == WB_SYNC_NONE &&
  725. ceph_inode_to_fs_client(inode)->write_congested) {
  726. redirty_page_for_writepage(wbc, page);
  727. return AOP_WRITEPAGE_ACTIVATE;
  728. }
  729. folio_wait_private_2(page_folio(page)); /* [DEPRECATED] */
  730. err = writepage_nounlock(page, wbc);
  731. if (err == -ERESTARTSYS) {
  732. /* direct memory reclaimer was killed by SIGKILL. return 0
  733. * to prevent caller from setting mapping/page error */
  734. err = 0;
  735. }
  736. unlock_page(page);
  737. iput(inode);
  738. return err;
  739. }
  740. /*
  741. * async writeback completion handler.
  742. *
  743. * If we get an error, set the mapping error bit, but not the individual
  744. * page error bits.
  745. */
  746. static void writepages_finish(struct ceph_osd_request *req)
  747. {
  748. struct inode *inode = req->r_inode;
  749. struct ceph_inode_info *ci = ceph_inode(inode);
  750. struct ceph_client *cl = ceph_inode_to_client(inode);
  751. struct ceph_osd_data *osd_data;
  752. struct page *page;
  753. int num_pages, total_pages = 0;
  754. int i, j;
  755. int rc = req->r_result;
  756. struct ceph_snap_context *snapc = req->r_snapc;
  757. struct address_space *mapping = inode->i_mapping;
  758. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  759. unsigned int len = 0;
  760. bool remove_page;
  761. doutc(cl, "%llx.%llx rc %d\n", ceph_vinop(inode), rc);
  762. if (rc < 0) {
  763. mapping_set_error(mapping, rc);
  764. ceph_set_error_write(ci);
  765. if (rc == -EBLOCKLISTED)
  766. fsc->blocklisted = true;
  767. } else {
  768. ceph_clear_error_write(ci);
  769. }
  770. /*
  771. * We lost the cache cap, need to truncate the page before
  772. * it is unlocked, otherwise we'd truncate it later in the
  773. * page truncation thread, possibly losing some data that
  774. * raced its way in
  775. */
  776. remove_page = !(ceph_caps_issued(ci) &
  777. (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
  778. /* clean all pages */
  779. for (i = 0; i < req->r_num_ops; i++) {
  780. if (req->r_ops[i].op != CEPH_OSD_OP_WRITE) {
  781. pr_warn_client(cl,
  782. "%llx.%llx incorrect op %d req %p index %d tid %llu\n",
  783. ceph_vinop(inode), req->r_ops[i].op, req, i,
  784. req->r_tid);
  785. break;
  786. }
  787. osd_data = osd_req_op_extent_osd_data(req, i);
  788. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  789. len += osd_data->length;
  790. num_pages = calc_pages_for((u64)osd_data->alignment,
  791. (u64)osd_data->length);
  792. total_pages += num_pages;
  793. for (j = 0; j < num_pages; j++) {
  794. page = osd_data->pages[j];
  795. if (fscrypt_is_bounce_page(page)) {
  796. page = fscrypt_pagecache_page(page);
  797. fscrypt_free_bounce_page(osd_data->pages[j]);
  798. osd_data->pages[j] = page;
  799. }
  800. BUG_ON(!page);
  801. WARN_ON(!PageUptodate(page));
  802. if (atomic_long_dec_return(&fsc->writeback_count) <
  803. CONGESTION_OFF_THRESH(
  804. fsc->mount_options->congestion_kb))
  805. fsc->write_congested = false;
  806. ceph_put_snap_context(detach_page_private(page));
  807. end_page_writeback(page);
  808. doutc(cl, "unlocking %p\n", page);
  809. if (remove_page)
  810. generic_error_remove_folio(inode->i_mapping,
  811. page_folio(page));
  812. unlock_page(page);
  813. }
  814. doutc(cl, "%llx.%llx wrote %llu bytes cleaned %d pages\n",
  815. ceph_vinop(inode), osd_data->length,
  816. rc >= 0 ? num_pages : 0);
  817. release_pages(osd_data->pages, num_pages);
  818. }
  819. ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
  820. req->r_end_latency, len, rc);
  821. ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
  822. osd_data = osd_req_op_extent_osd_data(req, 0);
  823. if (osd_data->pages_from_pool)
  824. mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
  825. else
  826. kfree(osd_data->pages);
  827. ceph_osdc_put_request(req);
  828. ceph_dec_osd_stopping_blocker(fsc->mdsc);
  829. }
  830. /*
  831. * initiate async writeback
  832. */
  833. static int ceph_writepages_start(struct address_space *mapping,
  834. struct writeback_control *wbc)
  835. {
  836. struct inode *inode = mapping->host;
  837. struct ceph_inode_info *ci = ceph_inode(inode);
  838. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  839. struct ceph_client *cl = fsc->client;
  840. struct ceph_vino vino = ceph_vino(inode);
  841. pgoff_t index, start_index, end = -1;
  842. struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
  843. struct folio_batch fbatch;
  844. int rc = 0;
  845. unsigned int wsize = i_blocksize(inode);
  846. struct ceph_osd_request *req = NULL;
  847. struct ceph_writeback_ctl ceph_wbc;
  848. bool should_loop, range_whole = false;
  849. bool done = false;
  850. bool caching = ceph_is_cache_enabled(inode);
  851. xa_mark_t tag;
  852. if (wbc->sync_mode == WB_SYNC_NONE &&
  853. fsc->write_congested)
  854. return 0;
  855. doutc(cl, "%llx.%llx (mode=%s)\n", ceph_vinop(inode),
  856. wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
  857. (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
  858. if (ceph_inode_is_shutdown(inode)) {
  859. if (ci->i_wrbuffer_ref > 0) {
  860. pr_warn_ratelimited_client(cl,
  861. "%llx.%llx %lld forced umount\n",
  862. ceph_vinop(inode), ceph_ino(inode));
  863. }
  864. mapping_set_error(mapping, -EIO);
  865. return -EIO; /* we're in a forced umount, don't write! */
  866. }
  867. if (fsc->mount_options->wsize < wsize)
  868. wsize = fsc->mount_options->wsize;
  869. folio_batch_init(&fbatch);
  870. start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
  871. index = start_index;
  872. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) {
  873. tag = PAGECACHE_TAG_TOWRITE;
  874. } else {
  875. tag = PAGECACHE_TAG_DIRTY;
  876. }
  877. retry:
  878. /* find oldest snap context with dirty data */
  879. snapc = get_oldest_context(inode, &ceph_wbc, NULL);
  880. if (!snapc) {
  881. /* hmm, why does writepages get called when there
  882. is no dirty data? */
  883. doutc(cl, " no snap context with dirty data?\n");
  884. goto out;
  885. }
  886. doutc(cl, " oldest snapc is %p seq %lld (%d snaps)\n", snapc,
  887. snapc->seq, snapc->num_snaps);
  888. should_loop = false;
  889. if (ceph_wbc.head_snapc && snapc != last_snapc) {
  890. /* where to start/end? */
  891. if (wbc->range_cyclic) {
  892. index = start_index;
  893. end = -1;
  894. if (index > 0)
  895. should_loop = true;
  896. doutc(cl, " cyclic, start at %lu\n", index);
  897. } else {
  898. index = wbc->range_start >> PAGE_SHIFT;
  899. end = wbc->range_end >> PAGE_SHIFT;
  900. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  901. range_whole = true;
  902. doutc(cl, " not cyclic, %lu to %lu\n", index, end);
  903. }
  904. } else if (!ceph_wbc.head_snapc) {
  905. /* Do not respect wbc->range_{start,end}. Dirty pages
  906. * in that range can be associated with newer snapc.
  907. * They are not writeable until we write all dirty pages
  908. * associated with 'snapc' get written */
  909. if (index > 0)
  910. should_loop = true;
  911. doutc(cl, " non-head snapc, range whole\n");
  912. }
  913. if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
  914. tag_pages_for_writeback(mapping, index, end);
  915. ceph_put_snap_context(last_snapc);
  916. last_snapc = snapc;
  917. while (!done && index <= end) {
  918. int num_ops = 0, op_idx;
  919. unsigned i, nr_folios, max_pages, locked_pages = 0;
  920. struct page **pages = NULL, **data_pages;
  921. struct page *page;
  922. pgoff_t strip_unit_end = 0;
  923. u64 offset = 0, len = 0;
  924. bool from_pool = false;
  925. max_pages = wsize >> PAGE_SHIFT;
  926. get_more_pages:
  927. nr_folios = filemap_get_folios_tag(mapping, &index,
  928. end, tag, &fbatch);
  929. doutc(cl, "pagevec_lookup_range_tag got %d\n", nr_folios);
  930. if (!nr_folios && !locked_pages)
  931. break;
  932. for (i = 0; i < nr_folios && locked_pages < max_pages; i++) {
  933. page = &fbatch.folios[i]->page;
  934. doutc(cl, "? %p idx %lu\n", page, page->index);
  935. if (locked_pages == 0)
  936. lock_page(page); /* first page */
  937. else if (!trylock_page(page))
  938. break;
  939. /* only dirty pages, or our accounting breaks */
  940. if (unlikely(!PageDirty(page)) ||
  941. unlikely(page->mapping != mapping)) {
  942. doutc(cl, "!dirty or !mapping %p\n", page);
  943. unlock_page(page);
  944. continue;
  945. }
  946. /* only if matching snap context */
  947. pgsnapc = page_snap_context(page);
  948. if (pgsnapc != snapc) {
  949. doutc(cl, "page snapc %p %lld != oldest %p %lld\n",
  950. pgsnapc, pgsnapc->seq, snapc, snapc->seq);
  951. if (!should_loop &&
  952. !ceph_wbc.head_snapc &&
  953. wbc->sync_mode != WB_SYNC_NONE)
  954. should_loop = true;
  955. unlock_page(page);
  956. continue;
  957. }
  958. if (page_offset(page) >= ceph_wbc.i_size) {
  959. struct folio *folio = page_folio(page);
  960. doutc(cl, "folio at %lu beyond eof %llu\n",
  961. folio->index, ceph_wbc.i_size);
  962. if ((ceph_wbc.size_stable ||
  963. folio_pos(folio) >= i_size_read(inode)) &&
  964. folio_clear_dirty_for_io(folio))
  965. folio_invalidate(folio, 0,
  966. folio_size(folio));
  967. folio_unlock(folio);
  968. continue;
  969. }
  970. if (strip_unit_end && (page->index > strip_unit_end)) {
  971. doutc(cl, "end of strip unit %p\n", page);
  972. unlock_page(page);
  973. break;
  974. }
  975. if (PageWriteback(page) ||
  976. PagePrivate2(page) /* [DEPRECATED] */) {
  977. if (wbc->sync_mode == WB_SYNC_NONE) {
  978. doutc(cl, "%p under writeback\n", page);
  979. unlock_page(page);
  980. continue;
  981. }
  982. doutc(cl, "waiting on writeback %p\n", page);
  983. wait_on_page_writeback(page);
  984. folio_wait_private_2(page_folio(page)); /* [DEPRECATED] */
  985. }
  986. if (!clear_page_dirty_for_io(page)) {
  987. doutc(cl, "%p !clear_page_dirty_for_io\n", page);
  988. unlock_page(page);
  989. continue;
  990. }
  991. /*
  992. * We have something to write. If this is
  993. * the first locked page this time through,
  994. * calculate max possinle write size and
  995. * allocate a page array
  996. */
  997. if (locked_pages == 0) {
  998. u64 objnum;
  999. u64 objoff;
  1000. u32 xlen;
  1001. /* prepare async write request */
  1002. offset = (u64)page_offset(page);
  1003. ceph_calc_file_object_mapping(&ci->i_layout,
  1004. offset, wsize,
  1005. &objnum, &objoff,
  1006. &xlen);
  1007. len = xlen;
  1008. num_ops = 1;
  1009. strip_unit_end = page->index +
  1010. ((len - 1) >> PAGE_SHIFT);
  1011. BUG_ON(pages);
  1012. max_pages = calc_pages_for(0, (u64)len);
  1013. pages = kmalloc_array(max_pages,
  1014. sizeof(*pages),
  1015. GFP_NOFS);
  1016. if (!pages) {
  1017. from_pool = true;
  1018. pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
  1019. BUG_ON(!pages);
  1020. }
  1021. len = 0;
  1022. } else if (page->index !=
  1023. (offset + len) >> PAGE_SHIFT) {
  1024. if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
  1025. CEPH_OSD_MAX_OPS)) {
  1026. redirty_page_for_writepage(wbc, page);
  1027. unlock_page(page);
  1028. break;
  1029. }
  1030. num_ops++;
  1031. offset = (u64)page_offset(page);
  1032. len = 0;
  1033. }
  1034. /* note position of first page in fbatch */
  1035. doutc(cl, "%llx.%llx will write page %p idx %lu\n",
  1036. ceph_vinop(inode), page, page->index);
  1037. if (atomic_long_inc_return(&fsc->writeback_count) >
  1038. CONGESTION_ON_THRESH(
  1039. fsc->mount_options->congestion_kb))
  1040. fsc->write_congested = true;
  1041. if (IS_ENCRYPTED(inode)) {
  1042. pages[locked_pages] =
  1043. fscrypt_encrypt_pagecache_blocks(page,
  1044. PAGE_SIZE, 0,
  1045. locked_pages ? GFP_NOWAIT : GFP_NOFS);
  1046. if (IS_ERR(pages[locked_pages])) {
  1047. if (PTR_ERR(pages[locked_pages]) == -EINVAL)
  1048. pr_err_client(cl,
  1049. "inode->i_blkbits=%hhu\n",
  1050. inode->i_blkbits);
  1051. /* better not fail on first page! */
  1052. BUG_ON(locked_pages == 0);
  1053. pages[locked_pages] = NULL;
  1054. redirty_page_for_writepage(wbc, page);
  1055. unlock_page(page);
  1056. break;
  1057. }
  1058. ++locked_pages;
  1059. } else {
  1060. pages[locked_pages++] = page;
  1061. }
  1062. fbatch.folios[i] = NULL;
  1063. len += thp_size(page);
  1064. }
  1065. /* did we get anything? */
  1066. if (!locked_pages)
  1067. goto release_folios;
  1068. if (i) {
  1069. unsigned j, n = 0;
  1070. /* shift unused page to beginning of fbatch */
  1071. for (j = 0; j < nr_folios; j++) {
  1072. if (!fbatch.folios[j])
  1073. continue;
  1074. if (n < j)
  1075. fbatch.folios[n] = fbatch.folios[j];
  1076. n++;
  1077. }
  1078. fbatch.nr = n;
  1079. if (nr_folios && i == nr_folios &&
  1080. locked_pages < max_pages) {
  1081. doutc(cl, "reached end fbatch, trying for more\n");
  1082. folio_batch_release(&fbatch);
  1083. goto get_more_pages;
  1084. }
  1085. }
  1086. new_request:
  1087. offset = ceph_fscrypt_page_offset(pages[0]);
  1088. len = wsize;
  1089. req = ceph_osdc_new_request(&fsc->client->osdc,
  1090. &ci->i_layout, vino,
  1091. offset, &len, 0, num_ops,
  1092. CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
  1093. snapc, ceph_wbc.truncate_seq,
  1094. ceph_wbc.truncate_size, false);
  1095. if (IS_ERR(req)) {
  1096. req = ceph_osdc_new_request(&fsc->client->osdc,
  1097. &ci->i_layout, vino,
  1098. offset, &len, 0,
  1099. min(num_ops,
  1100. CEPH_OSD_SLAB_OPS),
  1101. CEPH_OSD_OP_WRITE,
  1102. CEPH_OSD_FLAG_WRITE,
  1103. snapc, ceph_wbc.truncate_seq,
  1104. ceph_wbc.truncate_size, true);
  1105. BUG_ON(IS_ERR(req));
  1106. }
  1107. BUG_ON(len < ceph_fscrypt_page_offset(pages[locked_pages - 1]) +
  1108. thp_size(pages[locked_pages - 1]) - offset);
  1109. if (!ceph_inc_osd_stopping_blocker(fsc->mdsc)) {
  1110. rc = -EIO;
  1111. goto release_folios;
  1112. }
  1113. req->r_callback = writepages_finish;
  1114. req->r_inode = inode;
  1115. /* Format the osd request message and submit the write */
  1116. len = 0;
  1117. data_pages = pages;
  1118. op_idx = 0;
  1119. for (i = 0; i < locked_pages; i++) {
  1120. struct page *page = ceph_fscrypt_pagecache_page(pages[i]);
  1121. u64 cur_offset = page_offset(page);
  1122. /*
  1123. * Discontinuity in page range? Ceph can handle that by just passing
  1124. * multiple extents in the write op.
  1125. */
  1126. if (offset + len != cur_offset) {
  1127. /* If it's full, stop here */
  1128. if (op_idx + 1 == req->r_num_ops)
  1129. break;
  1130. /* Kick off an fscache write with what we have so far. */
  1131. ceph_fscache_write_to_cache(inode, offset, len, caching);
  1132. /* Start a new extent */
  1133. osd_req_op_extent_dup_last(req, op_idx,
  1134. cur_offset - offset);
  1135. doutc(cl, "got pages at %llu~%llu\n", offset,
  1136. len);
  1137. osd_req_op_extent_osd_data_pages(req, op_idx,
  1138. data_pages, len, 0,
  1139. from_pool, false);
  1140. osd_req_op_extent_update(req, op_idx, len);
  1141. len = 0;
  1142. offset = cur_offset;
  1143. data_pages = pages + i;
  1144. op_idx++;
  1145. }
  1146. set_page_writeback(page);
  1147. if (caching)
  1148. ceph_set_page_fscache(page);
  1149. len += thp_size(page);
  1150. }
  1151. ceph_fscache_write_to_cache(inode, offset, len, caching);
  1152. if (ceph_wbc.size_stable) {
  1153. len = min(len, ceph_wbc.i_size - offset);
  1154. } else if (i == locked_pages) {
  1155. /* writepages_finish() clears writeback pages
  1156. * according to the data length, so make sure
  1157. * data length covers all locked pages */
  1158. u64 min_len = len + 1 - thp_size(page);
  1159. len = get_writepages_data_length(inode, pages[i - 1],
  1160. offset);
  1161. len = max(len, min_len);
  1162. }
  1163. if (IS_ENCRYPTED(inode))
  1164. len = round_up(len, CEPH_FSCRYPT_BLOCK_SIZE);
  1165. doutc(cl, "got pages at %llu~%llu\n", offset, len);
  1166. if (IS_ENCRYPTED(inode) &&
  1167. ((offset | len) & ~CEPH_FSCRYPT_BLOCK_MASK))
  1168. pr_warn_client(cl,
  1169. "bad encrypted write offset=%lld len=%llu\n",
  1170. offset, len);
  1171. osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
  1172. 0, from_pool, false);
  1173. osd_req_op_extent_update(req, op_idx, len);
  1174. BUG_ON(op_idx + 1 != req->r_num_ops);
  1175. from_pool = false;
  1176. if (i < locked_pages) {
  1177. BUG_ON(num_ops <= req->r_num_ops);
  1178. num_ops -= req->r_num_ops;
  1179. locked_pages -= i;
  1180. /* allocate new pages array for next request */
  1181. data_pages = pages;
  1182. pages = kmalloc_array(locked_pages, sizeof(*pages),
  1183. GFP_NOFS);
  1184. if (!pages) {
  1185. from_pool = true;
  1186. pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
  1187. BUG_ON(!pages);
  1188. }
  1189. memcpy(pages, data_pages + i,
  1190. locked_pages * sizeof(*pages));
  1191. memset(data_pages + i, 0,
  1192. locked_pages * sizeof(*pages));
  1193. } else {
  1194. BUG_ON(num_ops != req->r_num_ops);
  1195. index = pages[i - 1]->index + 1;
  1196. /* request message now owns the pages array */
  1197. pages = NULL;
  1198. }
  1199. req->r_mtime = inode_get_mtime(inode);
  1200. ceph_osdc_start_request(&fsc->client->osdc, req);
  1201. req = NULL;
  1202. wbc->nr_to_write -= i;
  1203. if (pages)
  1204. goto new_request;
  1205. /*
  1206. * We stop writing back only if we are not doing
  1207. * integrity sync. In case of integrity sync we have to
  1208. * keep going until we have written all the pages
  1209. * we tagged for writeback prior to entering this loop.
  1210. */
  1211. if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
  1212. done = true;
  1213. release_folios:
  1214. doutc(cl, "folio_batch release on %d folios (%p)\n",
  1215. (int)fbatch.nr, fbatch.nr ? fbatch.folios[0] : NULL);
  1216. folio_batch_release(&fbatch);
  1217. }
  1218. if (should_loop && !done) {
  1219. /* more to do; loop back to beginning of file */
  1220. doutc(cl, "looping back to beginning of file\n");
  1221. end = start_index - 1; /* OK even when start_index == 0 */
  1222. /* to write dirty pages associated with next snapc,
  1223. * we need to wait until current writes complete */
  1224. if (wbc->sync_mode != WB_SYNC_NONE &&
  1225. start_index == 0 && /* all dirty pages were checked */
  1226. !ceph_wbc.head_snapc) {
  1227. struct page *page;
  1228. unsigned i, nr;
  1229. index = 0;
  1230. while ((index <= end) &&
  1231. (nr = filemap_get_folios_tag(mapping, &index,
  1232. (pgoff_t)-1,
  1233. PAGECACHE_TAG_WRITEBACK,
  1234. &fbatch))) {
  1235. for (i = 0; i < nr; i++) {
  1236. page = &fbatch.folios[i]->page;
  1237. if (page_snap_context(page) != snapc)
  1238. continue;
  1239. wait_on_page_writeback(page);
  1240. }
  1241. folio_batch_release(&fbatch);
  1242. cond_resched();
  1243. }
  1244. }
  1245. start_index = 0;
  1246. index = 0;
  1247. goto retry;
  1248. }
  1249. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1250. mapping->writeback_index = index;
  1251. out:
  1252. ceph_osdc_put_request(req);
  1253. ceph_put_snap_context(last_snapc);
  1254. doutc(cl, "%llx.%llx dend - startone, rc = %d\n", ceph_vinop(inode),
  1255. rc);
  1256. return rc;
  1257. }
  1258. /*
  1259. * See if a given @snapc is either writeable, or already written.
  1260. */
  1261. static int context_is_writeable_or_written(struct inode *inode,
  1262. struct ceph_snap_context *snapc)
  1263. {
  1264. struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
  1265. int ret = !oldest || snapc->seq <= oldest->seq;
  1266. ceph_put_snap_context(oldest);
  1267. return ret;
  1268. }
  1269. /**
  1270. * ceph_find_incompatible - find an incompatible context and return it
  1271. * @page: page being dirtied
  1272. *
  1273. * We are only allowed to write into/dirty a page if the page is
  1274. * clean, or already dirty within the same snap context. Returns a
  1275. * conflicting context if there is one, NULL if there isn't, or a
  1276. * negative error code on other errors.
  1277. *
  1278. * Must be called with page lock held.
  1279. */
  1280. static struct ceph_snap_context *
  1281. ceph_find_incompatible(struct page *page)
  1282. {
  1283. struct inode *inode = page->mapping->host;
  1284. struct ceph_client *cl = ceph_inode_to_client(inode);
  1285. struct ceph_inode_info *ci = ceph_inode(inode);
  1286. if (ceph_inode_is_shutdown(inode)) {
  1287. doutc(cl, " %llx.%llx page %p is shutdown\n",
  1288. ceph_vinop(inode), page);
  1289. return ERR_PTR(-ESTALE);
  1290. }
  1291. for (;;) {
  1292. struct ceph_snap_context *snapc, *oldest;
  1293. wait_on_page_writeback(page);
  1294. snapc = page_snap_context(page);
  1295. if (!snapc || snapc == ci->i_head_snapc)
  1296. break;
  1297. /*
  1298. * this page is already dirty in another (older) snap
  1299. * context! is it writeable now?
  1300. */
  1301. oldest = get_oldest_context(inode, NULL, NULL);
  1302. if (snapc->seq > oldest->seq) {
  1303. /* not writeable -- return it for the caller to deal with */
  1304. ceph_put_snap_context(oldest);
  1305. doutc(cl, " %llx.%llx page %p snapc %p not current or oldest\n",
  1306. ceph_vinop(inode), page, snapc);
  1307. return ceph_get_snap_context(snapc);
  1308. }
  1309. ceph_put_snap_context(oldest);
  1310. /* yay, writeable, do it now (without dropping page lock) */
  1311. doutc(cl, " %llx.%llx page %p snapc %p not current, but oldest\n",
  1312. ceph_vinop(inode), page, snapc);
  1313. if (clear_page_dirty_for_io(page)) {
  1314. int r = writepage_nounlock(page, NULL);
  1315. if (r < 0)
  1316. return ERR_PTR(r);
  1317. }
  1318. }
  1319. return NULL;
  1320. }
  1321. static int ceph_netfs_check_write_begin(struct file *file, loff_t pos, unsigned int len,
  1322. struct folio **foliop, void **_fsdata)
  1323. {
  1324. struct inode *inode = file_inode(file);
  1325. struct ceph_inode_info *ci = ceph_inode(inode);
  1326. struct ceph_snap_context *snapc;
  1327. snapc = ceph_find_incompatible(folio_page(*foliop, 0));
  1328. if (snapc) {
  1329. int r;
  1330. folio_unlock(*foliop);
  1331. folio_put(*foliop);
  1332. *foliop = NULL;
  1333. if (IS_ERR(snapc))
  1334. return PTR_ERR(snapc);
  1335. ceph_queue_writeback(inode);
  1336. r = wait_event_killable(ci->i_cap_wq,
  1337. context_is_writeable_or_written(inode, snapc));
  1338. ceph_put_snap_context(snapc);
  1339. return r == 0 ? -EAGAIN : r;
  1340. }
  1341. return 0;
  1342. }
  1343. /*
  1344. * We are only allowed to write into/dirty the page if the page is
  1345. * clean, or already dirty within the same snap context.
  1346. */
  1347. static int ceph_write_begin(struct file *file, struct address_space *mapping,
  1348. loff_t pos, unsigned len,
  1349. struct folio **foliop, void **fsdata)
  1350. {
  1351. struct inode *inode = file_inode(file);
  1352. struct ceph_inode_info *ci = ceph_inode(inode);
  1353. int r;
  1354. r = netfs_write_begin(&ci->netfs, file, inode->i_mapping, pos, len, foliop, NULL);
  1355. if (r < 0)
  1356. return r;
  1357. folio_wait_private_2(*foliop); /* [DEPRECATED] */
  1358. WARN_ON_ONCE(!folio_test_locked(*foliop));
  1359. return 0;
  1360. }
  1361. /*
  1362. * we don't do anything in here that simple_write_end doesn't do
  1363. * except adjust dirty page accounting
  1364. */
  1365. static int ceph_write_end(struct file *file, struct address_space *mapping,
  1366. loff_t pos, unsigned len, unsigned copied,
  1367. struct folio *folio, void *fsdata)
  1368. {
  1369. struct inode *inode = file_inode(file);
  1370. struct ceph_client *cl = ceph_inode_to_client(inode);
  1371. bool check_cap = false;
  1372. doutc(cl, "%llx.%llx file %p folio %p %d~%d (%d)\n", ceph_vinop(inode),
  1373. file, folio, (int)pos, (int)copied, (int)len);
  1374. if (!folio_test_uptodate(folio)) {
  1375. /* just return that nothing was copied on a short copy */
  1376. if (copied < len) {
  1377. copied = 0;
  1378. goto out;
  1379. }
  1380. folio_mark_uptodate(folio);
  1381. }
  1382. /* did file size increase? */
  1383. if (pos+copied > i_size_read(inode))
  1384. check_cap = ceph_inode_set_size(inode, pos+copied);
  1385. folio_mark_dirty(folio);
  1386. out:
  1387. folio_unlock(folio);
  1388. folio_put(folio);
  1389. if (check_cap)
  1390. ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY);
  1391. return copied;
  1392. }
  1393. const struct address_space_operations ceph_aops = {
  1394. .read_folio = netfs_read_folio,
  1395. .readahead = netfs_readahead,
  1396. .writepage = ceph_writepage,
  1397. .writepages = ceph_writepages_start,
  1398. .write_begin = ceph_write_begin,
  1399. .write_end = ceph_write_end,
  1400. .dirty_folio = ceph_dirty_folio,
  1401. .invalidate_folio = ceph_invalidate_folio,
  1402. .release_folio = netfs_release_folio,
  1403. .direct_IO = noop_direct_IO,
  1404. };
  1405. static void ceph_block_sigs(sigset_t *oldset)
  1406. {
  1407. sigset_t mask;
  1408. siginitsetinv(&mask, sigmask(SIGKILL));
  1409. sigprocmask(SIG_BLOCK, &mask, oldset);
  1410. }
  1411. static void ceph_restore_sigs(sigset_t *oldset)
  1412. {
  1413. sigprocmask(SIG_SETMASK, oldset, NULL);
  1414. }
  1415. /*
  1416. * vm ops
  1417. */
  1418. static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
  1419. {
  1420. struct vm_area_struct *vma = vmf->vma;
  1421. struct inode *inode = file_inode(vma->vm_file);
  1422. struct ceph_inode_info *ci = ceph_inode(inode);
  1423. struct ceph_client *cl = ceph_inode_to_client(inode);
  1424. struct ceph_file_info *fi = vma->vm_file->private_data;
  1425. loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1426. int want, got, err;
  1427. sigset_t oldset;
  1428. vm_fault_t ret = VM_FAULT_SIGBUS;
  1429. if (ceph_inode_is_shutdown(inode))
  1430. return ret;
  1431. ceph_block_sigs(&oldset);
  1432. doutc(cl, "%llx.%llx %llu trying to get caps\n",
  1433. ceph_vinop(inode), off);
  1434. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1435. want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
  1436. else
  1437. want = CEPH_CAP_FILE_CACHE;
  1438. got = 0;
  1439. err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1, &got);
  1440. if (err < 0)
  1441. goto out_restore;
  1442. doutc(cl, "%llx.%llx %llu got cap refs on %s\n", ceph_vinop(inode),
  1443. off, ceph_cap_string(got));
  1444. if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
  1445. !ceph_has_inline_data(ci)) {
  1446. CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
  1447. ceph_add_rw_context(fi, &rw_ctx);
  1448. ret = filemap_fault(vmf);
  1449. ceph_del_rw_context(fi, &rw_ctx);
  1450. doutc(cl, "%llx.%llx %llu drop cap refs %s ret %x\n",
  1451. ceph_vinop(inode), off, ceph_cap_string(got), ret);
  1452. } else
  1453. err = -EAGAIN;
  1454. ceph_put_cap_refs(ci, got);
  1455. if (err != -EAGAIN)
  1456. goto out_restore;
  1457. /* read inline data */
  1458. if (off >= PAGE_SIZE) {
  1459. /* does not support inline data > PAGE_SIZE */
  1460. ret = VM_FAULT_SIGBUS;
  1461. } else {
  1462. struct address_space *mapping = inode->i_mapping;
  1463. struct page *page;
  1464. filemap_invalidate_lock_shared(mapping);
  1465. page = find_or_create_page(mapping, 0,
  1466. mapping_gfp_constraint(mapping, ~__GFP_FS));
  1467. if (!page) {
  1468. ret = VM_FAULT_OOM;
  1469. goto out_inline;
  1470. }
  1471. err = __ceph_do_getattr(inode, page,
  1472. CEPH_STAT_CAP_INLINE_DATA, true);
  1473. if (err < 0 || off >= i_size_read(inode)) {
  1474. unlock_page(page);
  1475. put_page(page);
  1476. ret = vmf_error(err);
  1477. goto out_inline;
  1478. }
  1479. if (err < PAGE_SIZE)
  1480. zero_user_segment(page, err, PAGE_SIZE);
  1481. else
  1482. flush_dcache_page(page);
  1483. SetPageUptodate(page);
  1484. vmf->page = page;
  1485. ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
  1486. out_inline:
  1487. filemap_invalidate_unlock_shared(mapping);
  1488. doutc(cl, "%llx.%llx %llu read inline data ret %x\n",
  1489. ceph_vinop(inode), off, ret);
  1490. }
  1491. out_restore:
  1492. ceph_restore_sigs(&oldset);
  1493. if (err < 0)
  1494. ret = vmf_error(err);
  1495. return ret;
  1496. }
  1497. static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
  1498. {
  1499. struct vm_area_struct *vma = vmf->vma;
  1500. struct inode *inode = file_inode(vma->vm_file);
  1501. struct ceph_client *cl = ceph_inode_to_client(inode);
  1502. struct ceph_inode_info *ci = ceph_inode(inode);
  1503. struct ceph_file_info *fi = vma->vm_file->private_data;
  1504. struct ceph_cap_flush *prealloc_cf;
  1505. struct page *page = vmf->page;
  1506. loff_t off = page_offset(page);
  1507. loff_t size = i_size_read(inode);
  1508. size_t len;
  1509. int want, got, err;
  1510. sigset_t oldset;
  1511. vm_fault_t ret = VM_FAULT_SIGBUS;
  1512. if (ceph_inode_is_shutdown(inode))
  1513. return ret;
  1514. prealloc_cf = ceph_alloc_cap_flush();
  1515. if (!prealloc_cf)
  1516. return VM_FAULT_OOM;
  1517. sb_start_pagefault(inode->i_sb);
  1518. ceph_block_sigs(&oldset);
  1519. if (off + thp_size(page) <= size)
  1520. len = thp_size(page);
  1521. else
  1522. len = offset_in_thp(page, size);
  1523. doutc(cl, "%llx.%llx %llu~%zd getting caps i_size %llu\n",
  1524. ceph_vinop(inode), off, len, size);
  1525. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1526. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  1527. else
  1528. want = CEPH_CAP_FILE_BUFFER;
  1529. got = 0;
  1530. err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len, &got);
  1531. if (err < 0)
  1532. goto out_free;
  1533. doutc(cl, "%llx.%llx %llu~%zd got cap refs on %s\n", ceph_vinop(inode),
  1534. off, len, ceph_cap_string(got));
  1535. /* Update time before taking page lock */
  1536. file_update_time(vma->vm_file);
  1537. inode_inc_iversion_raw(inode);
  1538. do {
  1539. struct ceph_snap_context *snapc;
  1540. lock_page(page);
  1541. if (page_mkwrite_check_truncate(page, inode) < 0) {
  1542. unlock_page(page);
  1543. ret = VM_FAULT_NOPAGE;
  1544. break;
  1545. }
  1546. snapc = ceph_find_incompatible(page);
  1547. if (!snapc) {
  1548. /* success. we'll keep the page locked. */
  1549. set_page_dirty(page);
  1550. ret = VM_FAULT_LOCKED;
  1551. break;
  1552. }
  1553. unlock_page(page);
  1554. if (IS_ERR(snapc)) {
  1555. ret = VM_FAULT_SIGBUS;
  1556. break;
  1557. }
  1558. ceph_queue_writeback(inode);
  1559. err = wait_event_killable(ci->i_cap_wq,
  1560. context_is_writeable_or_written(inode, snapc));
  1561. ceph_put_snap_context(snapc);
  1562. } while (err == 0);
  1563. if (ret == VM_FAULT_LOCKED) {
  1564. int dirty;
  1565. spin_lock(&ci->i_ceph_lock);
  1566. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1567. &prealloc_cf);
  1568. spin_unlock(&ci->i_ceph_lock);
  1569. if (dirty)
  1570. __mark_inode_dirty(inode, dirty);
  1571. }
  1572. doutc(cl, "%llx.%llx %llu~%zd dropping cap refs on %s ret %x\n",
  1573. ceph_vinop(inode), off, len, ceph_cap_string(got), ret);
  1574. ceph_put_cap_refs_async(ci, got);
  1575. out_free:
  1576. ceph_restore_sigs(&oldset);
  1577. sb_end_pagefault(inode->i_sb);
  1578. ceph_free_cap_flush(prealloc_cf);
  1579. if (err < 0)
  1580. ret = vmf_error(err);
  1581. return ret;
  1582. }
  1583. void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
  1584. char *data, size_t len)
  1585. {
  1586. struct ceph_client *cl = ceph_inode_to_client(inode);
  1587. struct address_space *mapping = inode->i_mapping;
  1588. struct page *page;
  1589. if (locked_page) {
  1590. page = locked_page;
  1591. } else {
  1592. if (i_size_read(inode) == 0)
  1593. return;
  1594. page = find_or_create_page(mapping, 0,
  1595. mapping_gfp_constraint(mapping,
  1596. ~__GFP_FS));
  1597. if (!page)
  1598. return;
  1599. if (PageUptodate(page)) {
  1600. unlock_page(page);
  1601. put_page(page);
  1602. return;
  1603. }
  1604. }
  1605. doutc(cl, "%p %llx.%llx len %zu locked_page %p\n", inode,
  1606. ceph_vinop(inode), len, locked_page);
  1607. if (len > 0) {
  1608. void *kaddr = kmap_atomic(page);
  1609. memcpy(kaddr, data, len);
  1610. kunmap_atomic(kaddr);
  1611. }
  1612. if (page != locked_page) {
  1613. if (len < PAGE_SIZE)
  1614. zero_user_segment(page, len, PAGE_SIZE);
  1615. else
  1616. flush_dcache_page(page);
  1617. SetPageUptodate(page);
  1618. unlock_page(page);
  1619. put_page(page);
  1620. }
  1621. }
  1622. int ceph_uninline_data(struct file *file)
  1623. {
  1624. struct inode *inode = file_inode(file);
  1625. struct ceph_inode_info *ci = ceph_inode(inode);
  1626. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  1627. struct ceph_client *cl = fsc->client;
  1628. struct ceph_osd_request *req = NULL;
  1629. struct ceph_cap_flush *prealloc_cf = NULL;
  1630. struct folio *folio = NULL;
  1631. u64 inline_version = CEPH_INLINE_NONE;
  1632. struct page *pages[1];
  1633. int err = 0;
  1634. u64 len;
  1635. spin_lock(&ci->i_ceph_lock);
  1636. inline_version = ci->i_inline_version;
  1637. spin_unlock(&ci->i_ceph_lock);
  1638. doutc(cl, "%llx.%llx inline_version %llu\n", ceph_vinop(inode),
  1639. inline_version);
  1640. if (ceph_inode_is_shutdown(inode)) {
  1641. err = -EIO;
  1642. goto out;
  1643. }
  1644. if (inline_version == CEPH_INLINE_NONE)
  1645. return 0;
  1646. prealloc_cf = ceph_alloc_cap_flush();
  1647. if (!prealloc_cf)
  1648. return -ENOMEM;
  1649. if (inline_version == 1) /* initial version, no data */
  1650. goto out_uninline;
  1651. folio = read_mapping_folio(inode->i_mapping, 0, file);
  1652. if (IS_ERR(folio)) {
  1653. err = PTR_ERR(folio);
  1654. goto out;
  1655. }
  1656. folio_lock(folio);
  1657. len = i_size_read(inode);
  1658. if (len > folio_size(folio))
  1659. len = folio_size(folio);
  1660. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1661. ceph_vino(inode), 0, &len, 0, 1,
  1662. CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
  1663. NULL, 0, 0, false);
  1664. if (IS_ERR(req)) {
  1665. err = PTR_ERR(req);
  1666. goto out_unlock;
  1667. }
  1668. req->r_mtime = inode_get_mtime(inode);
  1669. ceph_osdc_start_request(&fsc->client->osdc, req);
  1670. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1671. ceph_osdc_put_request(req);
  1672. if (err < 0)
  1673. goto out_unlock;
  1674. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1675. ceph_vino(inode), 0, &len, 1, 3,
  1676. CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
  1677. NULL, ci->i_truncate_seq,
  1678. ci->i_truncate_size, false);
  1679. if (IS_ERR(req)) {
  1680. err = PTR_ERR(req);
  1681. goto out_unlock;
  1682. }
  1683. pages[0] = folio_page(folio, 0);
  1684. osd_req_op_extent_osd_data_pages(req, 1, pages, len, 0, false, false);
  1685. {
  1686. __le64 xattr_buf = cpu_to_le64(inline_version);
  1687. err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
  1688. "inline_version", &xattr_buf,
  1689. sizeof(xattr_buf),
  1690. CEPH_OSD_CMPXATTR_OP_GT,
  1691. CEPH_OSD_CMPXATTR_MODE_U64);
  1692. if (err)
  1693. goto out_put_req;
  1694. }
  1695. {
  1696. char xattr_buf[32];
  1697. int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
  1698. "%llu", inline_version);
  1699. err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
  1700. "inline_version",
  1701. xattr_buf, xattr_len, 0, 0);
  1702. if (err)
  1703. goto out_put_req;
  1704. }
  1705. req->r_mtime = inode_get_mtime(inode);
  1706. ceph_osdc_start_request(&fsc->client->osdc, req);
  1707. err = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1708. ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
  1709. req->r_end_latency, len, err);
  1710. out_uninline:
  1711. if (!err) {
  1712. int dirty;
  1713. /* Set to CAP_INLINE_NONE and dirty the caps */
  1714. down_read(&fsc->mdsc->snap_rwsem);
  1715. spin_lock(&ci->i_ceph_lock);
  1716. ci->i_inline_version = CEPH_INLINE_NONE;
  1717. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR, &prealloc_cf);
  1718. spin_unlock(&ci->i_ceph_lock);
  1719. up_read(&fsc->mdsc->snap_rwsem);
  1720. if (dirty)
  1721. __mark_inode_dirty(inode, dirty);
  1722. }
  1723. out_put_req:
  1724. ceph_osdc_put_request(req);
  1725. if (err == -ECANCELED)
  1726. err = 0;
  1727. out_unlock:
  1728. if (folio) {
  1729. folio_unlock(folio);
  1730. folio_put(folio);
  1731. }
  1732. out:
  1733. ceph_free_cap_flush(prealloc_cf);
  1734. doutc(cl, "%llx.%llx inline_version %llu = %d\n",
  1735. ceph_vinop(inode), inline_version, err);
  1736. return err;
  1737. }
  1738. static const struct vm_operations_struct ceph_vmops = {
  1739. .fault = ceph_filemap_fault,
  1740. .page_mkwrite = ceph_page_mkwrite,
  1741. };
  1742. int ceph_mmap(struct file *file, struct vm_area_struct *vma)
  1743. {
  1744. struct address_space *mapping = file->f_mapping;
  1745. if (!mapping->a_ops->read_folio)
  1746. return -ENOEXEC;
  1747. vma->vm_ops = &ceph_vmops;
  1748. return 0;
  1749. }
  1750. enum {
  1751. POOL_READ = 1,
  1752. POOL_WRITE = 2,
  1753. };
  1754. static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
  1755. s64 pool, struct ceph_string *pool_ns)
  1756. {
  1757. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(&ci->netfs.inode);
  1758. struct ceph_mds_client *mdsc = fsc->mdsc;
  1759. struct ceph_client *cl = fsc->client;
  1760. struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
  1761. struct rb_node **p, *parent;
  1762. struct ceph_pool_perm *perm;
  1763. struct page **pages;
  1764. size_t pool_ns_len;
  1765. int err = 0, err2 = 0, have = 0;
  1766. down_read(&mdsc->pool_perm_rwsem);
  1767. p = &mdsc->pool_perm_tree.rb_node;
  1768. while (*p) {
  1769. perm = rb_entry(*p, struct ceph_pool_perm, node);
  1770. if (pool < perm->pool)
  1771. p = &(*p)->rb_left;
  1772. else if (pool > perm->pool)
  1773. p = &(*p)->rb_right;
  1774. else {
  1775. int ret = ceph_compare_string(pool_ns,
  1776. perm->pool_ns,
  1777. perm->pool_ns_len);
  1778. if (ret < 0)
  1779. p = &(*p)->rb_left;
  1780. else if (ret > 0)
  1781. p = &(*p)->rb_right;
  1782. else {
  1783. have = perm->perm;
  1784. break;
  1785. }
  1786. }
  1787. }
  1788. up_read(&mdsc->pool_perm_rwsem);
  1789. if (*p)
  1790. goto out;
  1791. if (pool_ns)
  1792. doutc(cl, "pool %lld ns %.*s no perm cached\n", pool,
  1793. (int)pool_ns->len, pool_ns->str);
  1794. else
  1795. doutc(cl, "pool %lld no perm cached\n", pool);
  1796. down_write(&mdsc->pool_perm_rwsem);
  1797. p = &mdsc->pool_perm_tree.rb_node;
  1798. parent = NULL;
  1799. while (*p) {
  1800. parent = *p;
  1801. perm = rb_entry(parent, struct ceph_pool_perm, node);
  1802. if (pool < perm->pool)
  1803. p = &(*p)->rb_left;
  1804. else if (pool > perm->pool)
  1805. p = &(*p)->rb_right;
  1806. else {
  1807. int ret = ceph_compare_string(pool_ns,
  1808. perm->pool_ns,
  1809. perm->pool_ns_len);
  1810. if (ret < 0)
  1811. p = &(*p)->rb_left;
  1812. else if (ret > 0)
  1813. p = &(*p)->rb_right;
  1814. else {
  1815. have = perm->perm;
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. if (*p) {
  1821. up_write(&mdsc->pool_perm_rwsem);
  1822. goto out;
  1823. }
  1824. rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
  1825. 1, false, GFP_NOFS);
  1826. if (!rd_req) {
  1827. err = -ENOMEM;
  1828. goto out_unlock;
  1829. }
  1830. rd_req->r_flags = CEPH_OSD_FLAG_READ;
  1831. osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
  1832. rd_req->r_base_oloc.pool = pool;
  1833. if (pool_ns)
  1834. rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
  1835. ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
  1836. err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
  1837. if (err)
  1838. goto out_unlock;
  1839. wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
  1840. 1, false, GFP_NOFS);
  1841. if (!wr_req) {
  1842. err = -ENOMEM;
  1843. goto out_unlock;
  1844. }
  1845. wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
  1846. osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
  1847. ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
  1848. ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
  1849. err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
  1850. if (err)
  1851. goto out_unlock;
  1852. /* one page should be large enough for STAT data */
  1853. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  1854. if (IS_ERR(pages)) {
  1855. err = PTR_ERR(pages);
  1856. goto out_unlock;
  1857. }
  1858. osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
  1859. 0, false, true);
  1860. ceph_osdc_start_request(&fsc->client->osdc, rd_req);
  1861. wr_req->r_mtime = inode_get_mtime(&ci->netfs.inode);
  1862. ceph_osdc_start_request(&fsc->client->osdc, wr_req);
  1863. err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
  1864. err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
  1865. if (err >= 0 || err == -ENOENT)
  1866. have |= POOL_READ;
  1867. else if (err != -EPERM) {
  1868. if (err == -EBLOCKLISTED)
  1869. fsc->blocklisted = true;
  1870. goto out_unlock;
  1871. }
  1872. if (err2 == 0 || err2 == -EEXIST)
  1873. have |= POOL_WRITE;
  1874. else if (err2 != -EPERM) {
  1875. if (err2 == -EBLOCKLISTED)
  1876. fsc->blocklisted = true;
  1877. err = err2;
  1878. goto out_unlock;
  1879. }
  1880. pool_ns_len = pool_ns ? pool_ns->len : 0;
  1881. perm = kmalloc(struct_size(perm, pool_ns, pool_ns_len + 1), GFP_NOFS);
  1882. if (!perm) {
  1883. err = -ENOMEM;
  1884. goto out_unlock;
  1885. }
  1886. perm->pool = pool;
  1887. perm->perm = have;
  1888. perm->pool_ns_len = pool_ns_len;
  1889. if (pool_ns_len > 0)
  1890. memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
  1891. perm->pool_ns[pool_ns_len] = 0;
  1892. rb_link_node(&perm->node, parent, p);
  1893. rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
  1894. err = 0;
  1895. out_unlock:
  1896. up_write(&mdsc->pool_perm_rwsem);
  1897. ceph_osdc_put_request(rd_req);
  1898. ceph_osdc_put_request(wr_req);
  1899. out:
  1900. if (!err)
  1901. err = have;
  1902. if (pool_ns)
  1903. doutc(cl, "pool %lld ns %.*s result = %d\n", pool,
  1904. (int)pool_ns->len, pool_ns->str, err);
  1905. else
  1906. doutc(cl, "pool %lld result = %d\n", pool, err);
  1907. return err;
  1908. }
  1909. int ceph_pool_perm_check(struct inode *inode, int need)
  1910. {
  1911. struct ceph_client *cl = ceph_inode_to_client(inode);
  1912. struct ceph_inode_info *ci = ceph_inode(inode);
  1913. struct ceph_string *pool_ns;
  1914. s64 pool;
  1915. int ret, flags;
  1916. /* Only need to do this for regular files */
  1917. if (!S_ISREG(inode->i_mode))
  1918. return 0;
  1919. if (ci->i_vino.snap != CEPH_NOSNAP) {
  1920. /*
  1921. * Pool permission check needs to write to the first object.
  1922. * But for snapshot, head of the first object may have alread
  1923. * been deleted. Skip check to avoid creating orphan object.
  1924. */
  1925. return 0;
  1926. }
  1927. if (ceph_test_mount_opt(ceph_inode_to_fs_client(inode),
  1928. NOPOOLPERM))
  1929. return 0;
  1930. spin_lock(&ci->i_ceph_lock);
  1931. flags = ci->i_ceph_flags;
  1932. pool = ci->i_layout.pool_id;
  1933. spin_unlock(&ci->i_ceph_lock);
  1934. check:
  1935. if (flags & CEPH_I_POOL_PERM) {
  1936. if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
  1937. doutc(cl, "pool %lld no read perm\n", pool);
  1938. return -EPERM;
  1939. }
  1940. if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
  1941. doutc(cl, "pool %lld no write perm\n", pool);
  1942. return -EPERM;
  1943. }
  1944. return 0;
  1945. }
  1946. pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
  1947. ret = __ceph_pool_perm_get(ci, pool, pool_ns);
  1948. ceph_put_string(pool_ns);
  1949. if (ret < 0)
  1950. return ret;
  1951. flags = CEPH_I_POOL_PERM;
  1952. if (ret & POOL_READ)
  1953. flags |= CEPH_I_POOL_RD;
  1954. if (ret & POOL_WRITE)
  1955. flags |= CEPH_I_POOL_WR;
  1956. spin_lock(&ci->i_ceph_lock);
  1957. if (pool == ci->i_layout.pool_id &&
  1958. pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
  1959. ci->i_ceph_flags |= flags;
  1960. } else {
  1961. pool = ci->i_layout.pool_id;
  1962. flags = ci->i_ceph_flags;
  1963. }
  1964. spin_unlock(&ci->i_ceph_lock);
  1965. goto check;
  1966. }
  1967. void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
  1968. {
  1969. struct ceph_pool_perm *perm;
  1970. struct rb_node *n;
  1971. while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
  1972. n = rb_first(&mdsc->pool_perm_tree);
  1973. perm = rb_entry(n, struct ceph_pool_perm, node);
  1974. rb_erase(n, &mdsc->pool_perm_tree);
  1975. kfree(perm);
  1976. }
  1977. }