file.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/ceph/striper.h>
  4. #include <linux/module.h>
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/file.h>
  8. #include <linux/mount.h>
  9. #include <linux/namei.h>
  10. #include <linux/writeback.h>
  11. #include <linux/falloc.h>
  12. #include <linux/iversion.h>
  13. #include <linux/ktime.h>
  14. #include <linux/splice.h>
  15. #include "super.h"
  16. #include "mds_client.h"
  17. #include "cache.h"
  18. #include "io.h"
  19. #include "metric.h"
  20. static __le32 ceph_flags_sys2wire(struct ceph_mds_client *mdsc, u32 flags)
  21. {
  22. struct ceph_client *cl = mdsc->fsc->client;
  23. u32 wire_flags = 0;
  24. switch (flags & O_ACCMODE) {
  25. case O_RDONLY:
  26. wire_flags |= CEPH_O_RDONLY;
  27. break;
  28. case O_WRONLY:
  29. wire_flags |= CEPH_O_WRONLY;
  30. break;
  31. case O_RDWR:
  32. wire_flags |= CEPH_O_RDWR;
  33. break;
  34. }
  35. flags &= ~O_ACCMODE;
  36. #define ceph_sys2wire(a) if (flags & a) { wire_flags |= CEPH_##a; flags &= ~a; }
  37. ceph_sys2wire(O_CREAT);
  38. ceph_sys2wire(O_EXCL);
  39. ceph_sys2wire(O_TRUNC);
  40. ceph_sys2wire(O_DIRECTORY);
  41. ceph_sys2wire(O_NOFOLLOW);
  42. #undef ceph_sys2wire
  43. if (flags)
  44. doutc(cl, "unused open flags: %x\n", flags);
  45. return cpu_to_le32(wire_flags);
  46. }
  47. /*
  48. * Ceph file operations
  49. *
  50. * Implement basic open/close functionality, and implement
  51. * read/write.
  52. *
  53. * We implement three modes of file I/O:
  54. * - buffered uses the generic_file_aio_{read,write} helpers
  55. *
  56. * - synchronous is used when there is multi-client read/write
  57. * sharing, avoids the page cache, and synchronously waits for an
  58. * ack from the OSD.
  59. *
  60. * - direct io takes the variant of the sync path that references
  61. * user pages directly.
  62. *
  63. * fsync() flushes and waits on dirty pages, but just queues metadata
  64. * for writeback: since the MDS can recover size and mtime there is no
  65. * need to wait for MDS acknowledgement.
  66. */
  67. /*
  68. * How many pages to get in one call to iov_iter_get_pages(). This
  69. * determines the size of the on-stack array used as a buffer.
  70. */
  71. #define ITER_GET_BVECS_PAGES 64
  72. static ssize_t __iter_get_bvecs(struct iov_iter *iter, size_t maxsize,
  73. struct bio_vec *bvecs)
  74. {
  75. size_t size = 0;
  76. int bvec_idx = 0;
  77. if (maxsize > iov_iter_count(iter))
  78. maxsize = iov_iter_count(iter);
  79. while (size < maxsize) {
  80. struct page *pages[ITER_GET_BVECS_PAGES];
  81. ssize_t bytes;
  82. size_t start;
  83. int idx = 0;
  84. bytes = iov_iter_get_pages2(iter, pages, maxsize - size,
  85. ITER_GET_BVECS_PAGES, &start);
  86. if (bytes < 0)
  87. return size ?: bytes;
  88. size += bytes;
  89. for ( ; bytes; idx++, bvec_idx++) {
  90. int len = min_t(int, bytes, PAGE_SIZE - start);
  91. bvec_set_page(&bvecs[bvec_idx], pages[idx], len, start);
  92. bytes -= len;
  93. start = 0;
  94. }
  95. }
  96. return size;
  97. }
  98. /*
  99. * iov_iter_get_pages() only considers one iov_iter segment, no matter
  100. * what maxsize or maxpages are given. For ITER_BVEC that is a single
  101. * page.
  102. *
  103. * Attempt to get up to @maxsize bytes worth of pages from @iter.
  104. * Return the number of bytes in the created bio_vec array, or an error.
  105. */
  106. static ssize_t iter_get_bvecs_alloc(struct iov_iter *iter, size_t maxsize,
  107. struct bio_vec **bvecs, int *num_bvecs)
  108. {
  109. struct bio_vec *bv;
  110. size_t orig_count = iov_iter_count(iter);
  111. ssize_t bytes;
  112. int npages;
  113. iov_iter_truncate(iter, maxsize);
  114. npages = iov_iter_npages(iter, INT_MAX);
  115. iov_iter_reexpand(iter, orig_count);
  116. /*
  117. * __iter_get_bvecs() may populate only part of the array -- zero it
  118. * out.
  119. */
  120. bv = kvmalloc_objs(*bv, npages, GFP_KERNEL | __GFP_ZERO);
  121. if (!bv)
  122. return -ENOMEM;
  123. bytes = __iter_get_bvecs(iter, maxsize, bv);
  124. if (bytes < 0) {
  125. /*
  126. * No pages were pinned -- just free the array.
  127. */
  128. kvfree(bv);
  129. return bytes;
  130. }
  131. *bvecs = bv;
  132. *num_bvecs = npages;
  133. return bytes;
  134. }
  135. static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty)
  136. {
  137. int i;
  138. for (i = 0; i < num_bvecs; i++) {
  139. if (bvecs[i].bv_page) {
  140. if (should_dirty)
  141. set_page_dirty_lock(bvecs[i].bv_page);
  142. put_page(bvecs[i].bv_page);
  143. }
  144. }
  145. kvfree(bvecs);
  146. }
  147. /*
  148. * Prepare an open request. Preallocate ceph_cap to avoid an
  149. * inopportune ENOMEM later.
  150. */
  151. static struct ceph_mds_request *
  152. prepare_open_request(struct super_block *sb, int flags, int create_mode)
  153. {
  154. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
  155. struct ceph_mds_request *req;
  156. int want_auth = USE_ANY_MDS;
  157. int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN;
  158. if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC))
  159. want_auth = USE_AUTH_MDS;
  160. req = ceph_mdsc_create_request(mdsc, op, want_auth);
  161. if (IS_ERR(req))
  162. goto out;
  163. req->r_fmode = ceph_flags_to_mode(flags);
  164. req->r_args.open.flags = ceph_flags_sys2wire(mdsc, flags);
  165. req->r_args.open.mode = cpu_to_le32(create_mode);
  166. out:
  167. return req;
  168. }
  169. static int ceph_init_file_info(struct inode *inode, struct file *file,
  170. int fmode, bool isdir)
  171. {
  172. struct ceph_inode_info *ci = ceph_inode(inode);
  173. struct ceph_mount_options *opt =
  174. ceph_inode_to_fs_client(&ci->netfs.inode)->mount_options;
  175. struct ceph_client *cl = ceph_inode_to_client(inode);
  176. struct ceph_file_info *fi;
  177. int ret;
  178. doutc(cl, "%p %llx.%llx %p 0%o (%s)\n", inode, ceph_vinop(inode),
  179. file, inode->i_mode, isdir ? "dir" : "regular");
  180. BUG_ON(inode->i_fop->release != ceph_release);
  181. if (isdir) {
  182. struct ceph_dir_file_info *dfi =
  183. kmem_cache_zalloc(ceph_dir_file_cachep, GFP_KERNEL);
  184. if (!dfi)
  185. return -ENOMEM;
  186. file->private_data = dfi;
  187. fi = &dfi->file_info;
  188. dfi->next_offset = 2;
  189. dfi->readdir_cache_idx = -1;
  190. } else {
  191. fi = kmem_cache_zalloc(ceph_file_cachep, GFP_KERNEL);
  192. if (!fi)
  193. return -ENOMEM;
  194. if (opt->flags & CEPH_MOUNT_OPT_NOPAGECACHE)
  195. fi->flags |= CEPH_F_SYNC;
  196. file->private_data = fi;
  197. }
  198. ceph_get_fmode(ci, fmode, 1);
  199. fi->fmode = fmode;
  200. spin_lock_init(&fi->rw_contexts_lock);
  201. INIT_LIST_HEAD(&fi->rw_contexts);
  202. fi->filp_gen = READ_ONCE(ceph_inode_to_fs_client(inode)->filp_gen);
  203. if ((file->f_mode & FMODE_WRITE) && ceph_has_inline_data(ci)) {
  204. ret = ceph_uninline_data(file);
  205. if (ret < 0)
  206. goto error;
  207. }
  208. return 0;
  209. error:
  210. ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
  211. ceph_put_fmode(ci, fi->fmode, 1);
  212. kmem_cache_free(ceph_file_cachep, fi);
  213. /* wake up anyone waiting for caps on this inode */
  214. wake_up_all(&ci->i_cap_wq);
  215. return ret;
  216. }
  217. /*
  218. * initialize private struct file data.
  219. * if we fail, clean up by dropping fmode reference on the ceph_inode
  220. */
  221. static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
  222. {
  223. struct ceph_client *cl = ceph_inode_to_client(inode);
  224. int ret = 0;
  225. switch (inode->i_mode & S_IFMT) {
  226. case S_IFREG:
  227. ceph_fscache_use_cookie(inode, file->f_mode & FMODE_WRITE);
  228. fallthrough;
  229. case S_IFDIR:
  230. ret = ceph_init_file_info(inode, file, fmode,
  231. S_ISDIR(inode->i_mode));
  232. break;
  233. case S_IFLNK:
  234. doutc(cl, "%p %llx.%llx %p 0%o (symlink)\n", inode,
  235. ceph_vinop(inode), file, inode->i_mode);
  236. break;
  237. default:
  238. doutc(cl, "%p %llx.%llx %p 0%o (special)\n", inode,
  239. ceph_vinop(inode), file, inode->i_mode);
  240. /*
  241. * we need to drop the open ref now, since we don't
  242. * have .release set to ceph_release.
  243. */
  244. BUG_ON(inode->i_fop->release == ceph_release);
  245. /* call the proper open fop */
  246. ret = inode->i_fop->open(inode, file);
  247. }
  248. return ret;
  249. }
  250. /*
  251. * try renew caps after session gets killed.
  252. */
  253. int ceph_renew_caps(struct inode *inode, int fmode)
  254. {
  255. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  256. struct ceph_client *cl = mdsc->fsc->client;
  257. struct ceph_inode_info *ci = ceph_inode(inode);
  258. struct ceph_mds_request *req;
  259. int err, flags, wanted;
  260. spin_lock(&ci->i_ceph_lock);
  261. __ceph_touch_fmode(ci, mdsc, fmode);
  262. wanted = __ceph_caps_file_wanted(ci);
  263. if (__ceph_is_any_real_caps(ci) &&
  264. (!(wanted & CEPH_CAP_ANY_WR) || ci->i_auth_cap)) {
  265. int issued = __ceph_caps_issued(ci, NULL);
  266. spin_unlock(&ci->i_ceph_lock);
  267. doutc(cl, "%p %llx.%llx want %s issued %s updating mds_wanted\n",
  268. inode, ceph_vinop(inode), ceph_cap_string(wanted),
  269. ceph_cap_string(issued));
  270. ceph_check_caps(ci, 0);
  271. return 0;
  272. }
  273. spin_unlock(&ci->i_ceph_lock);
  274. flags = 0;
  275. if ((wanted & CEPH_CAP_FILE_RD) && (wanted & CEPH_CAP_FILE_WR))
  276. flags = O_RDWR;
  277. else if (wanted & CEPH_CAP_FILE_RD)
  278. flags = O_RDONLY;
  279. else if (wanted & CEPH_CAP_FILE_WR)
  280. flags = O_WRONLY;
  281. #ifdef O_LAZY
  282. if (wanted & CEPH_CAP_FILE_LAZYIO)
  283. flags |= O_LAZY;
  284. #endif
  285. req = prepare_open_request(inode->i_sb, flags, 0);
  286. if (IS_ERR(req)) {
  287. err = PTR_ERR(req);
  288. goto out;
  289. }
  290. req->r_inode = inode;
  291. ihold(inode);
  292. req->r_num_caps = 1;
  293. err = ceph_mdsc_do_request(mdsc, NULL, req);
  294. ceph_mdsc_put_request(req);
  295. out:
  296. doutc(cl, "%p %llx.%llx open result=%d\n", inode, ceph_vinop(inode),
  297. err);
  298. return err < 0 ? err : 0;
  299. }
  300. /*
  301. * If we already have the requisite capabilities, we can satisfy
  302. * the open request locally (no need to request new caps from the
  303. * MDS). We do, however, need to inform the MDS (asynchronously)
  304. * if our wanted caps set expands.
  305. */
  306. int ceph_open(struct inode *inode, struct file *file)
  307. {
  308. struct ceph_inode_info *ci = ceph_inode(inode);
  309. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
  310. struct ceph_client *cl = fsc->client;
  311. struct ceph_mds_client *mdsc = fsc->mdsc;
  312. struct ceph_mds_request *req;
  313. struct ceph_file_info *fi = file->private_data;
  314. int err;
  315. int flags, fmode, wanted;
  316. struct dentry *dentry;
  317. char *path;
  318. bool do_sync = false;
  319. int mask = MAY_READ;
  320. if (fi) {
  321. doutc(cl, "file %p is already opened\n", file);
  322. return 0;
  323. }
  324. /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */
  325. flags = file->f_flags & ~(O_CREAT|O_EXCL);
  326. if (S_ISDIR(inode->i_mode)) {
  327. flags = O_DIRECTORY; /* mds likes to know */
  328. } else if (S_ISREG(inode->i_mode)) {
  329. err = fscrypt_file_open(inode, file);
  330. if (err)
  331. return err;
  332. }
  333. doutc(cl, "%p %llx.%llx file %p flags %d (%d)\n", inode,
  334. ceph_vinop(inode), file, flags, file->f_flags);
  335. fmode = ceph_flags_to_mode(flags);
  336. wanted = ceph_caps_for_mode(fmode);
  337. if (fmode & CEPH_FILE_MODE_WR)
  338. mask |= MAY_WRITE;
  339. dentry = d_find_alias(inode);
  340. if (!dentry) {
  341. do_sync = true;
  342. } else {
  343. struct ceph_path_info path_info = {0};
  344. path = ceph_mdsc_build_path(mdsc, dentry, &path_info, 0);
  345. if (IS_ERR(path)) {
  346. do_sync = true;
  347. err = 0;
  348. } else {
  349. err = ceph_mds_check_access(mdsc, path, mask);
  350. }
  351. ceph_mdsc_free_path_info(&path_info);
  352. dput(dentry);
  353. /* For none EACCES cases will let the MDS do the mds auth check */
  354. if (err == -EACCES) {
  355. return err;
  356. } else if (err < 0) {
  357. do_sync = true;
  358. err = 0;
  359. }
  360. }
  361. /* snapped files are read-only */
  362. if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE))
  363. return -EROFS;
  364. /* trivially open snapdir */
  365. if (ceph_snap(inode) == CEPH_SNAPDIR) {
  366. return ceph_init_file(inode, file, fmode);
  367. }
  368. /*
  369. * No need to block if we have caps on the auth MDS (for
  370. * write) or any MDS (for read). Update wanted set
  371. * asynchronously.
  372. */
  373. spin_lock(&ci->i_ceph_lock);
  374. if (!do_sync && __ceph_is_any_real_caps(ci) &&
  375. (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
  376. int mds_wanted = __ceph_caps_mds_wanted(ci, true);
  377. int issued = __ceph_caps_issued(ci, NULL);
  378. doutc(cl, "open %p fmode %d want %s issued %s using existing\n",
  379. inode, fmode, ceph_cap_string(wanted),
  380. ceph_cap_string(issued));
  381. __ceph_touch_fmode(ci, mdsc, fmode);
  382. spin_unlock(&ci->i_ceph_lock);
  383. /* adjust wanted? */
  384. if ((issued & wanted) != wanted &&
  385. (mds_wanted & wanted) != wanted &&
  386. ceph_snap(inode) != CEPH_SNAPDIR)
  387. ceph_check_caps(ci, 0);
  388. return ceph_init_file(inode, file, fmode);
  389. } else if (!do_sync && ceph_snap(inode) != CEPH_NOSNAP &&
  390. (ci->i_snap_caps & wanted) == wanted) {
  391. __ceph_touch_fmode(ci, mdsc, fmode);
  392. spin_unlock(&ci->i_ceph_lock);
  393. return ceph_init_file(inode, file, fmode);
  394. }
  395. spin_unlock(&ci->i_ceph_lock);
  396. doutc(cl, "open fmode %d wants %s\n", fmode, ceph_cap_string(wanted));
  397. req = prepare_open_request(inode->i_sb, flags, 0);
  398. if (IS_ERR(req)) {
  399. err = PTR_ERR(req);
  400. goto out;
  401. }
  402. req->r_inode = inode;
  403. ihold(inode);
  404. req->r_num_caps = 1;
  405. err = ceph_mdsc_do_request(mdsc, NULL, req);
  406. if (!err)
  407. err = ceph_init_file(inode, file, req->r_fmode);
  408. ceph_mdsc_put_request(req);
  409. doutc(cl, "open result=%d on %llx.%llx\n", err, ceph_vinop(inode));
  410. out:
  411. return err;
  412. }
  413. /* Clone the layout from a synchronous create, if the dir now has Dc caps */
  414. static void
  415. cache_file_layout(struct inode *dst, struct inode *src)
  416. {
  417. struct ceph_inode_info *cdst = ceph_inode(dst);
  418. struct ceph_inode_info *csrc = ceph_inode(src);
  419. spin_lock(&cdst->i_ceph_lock);
  420. if ((__ceph_caps_issued(cdst, NULL) & CEPH_CAP_DIR_CREATE) &&
  421. !ceph_file_layout_is_valid(&cdst->i_cached_layout)) {
  422. memcpy(&cdst->i_cached_layout, &csrc->i_layout,
  423. sizeof(cdst->i_cached_layout));
  424. rcu_assign_pointer(cdst->i_cached_layout.pool_ns,
  425. ceph_try_get_string(csrc->i_layout.pool_ns));
  426. }
  427. spin_unlock(&cdst->i_ceph_lock);
  428. }
  429. /*
  430. * Try to set up an async create. We need caps, a file layout, and inode number,
  431. * and either a lease on the dentry or complete dir info. If any of those
  432. * criteria are not satisfied, then return false and the caller can go
  433. * synchronous.
  434. */
  435. static int try_prep_async_create(struct inode *dir, struct dentry *dentry,
  436. struct ceph_file_layout *lo, u64 *pino)
  437. {
  438. struct ceph_inode_info *ci = ceph_inode(dir);
  439. struct ceph_dentry_info *di = ceph_dentry(dentry);
  440. int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_CREATE;
  441. u64 ino;
  442. spin_lock(&ci->i_ceph_lock);
  443. /* No auth cap means no chance for Dc caps */
  444. if (!ci->i_auth_cap)
  445. goto no_async;
  446. /* Any delegated inos? */
  447. if (xa_empty(&ci->i_auth_cap->session->s_delegated_inos))
  448. goto no_async;
  449. if (!ceph_file_layout_is_valid(&ci->i_cached_layout))
  450. goto no_async;
  451. if ((__ceph_caps_issued(ci, NULL) & want) != want)
  452. goto no_async;
  453. if (d_in_lookup(dentry)) {
  454. if (!__ceph_dir_is_complete(ci))
  455. goto no_async;
  456. spin_lock(&dentry->d_lock);
  457. di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
  458. spin_unlock(&dentry->d_lock);
  459. } else if (atomic_read(&ci->i_shared_gen) !=
  460. READ_ONCE(di->lease_shared_gen)) {
  461. goto no_async;
  462. }
  463. ino = ceph_get_deleg_ino(ci->i_auth_cap->session);
  464. if (!ino)
  465. goto no_async;
  466. *pino = ino;
  467. ceph_take_cap_refs(ci, want, false);
  468. memcpy(lo, &ci->i_cached_layout, sizeof(*lo));
  469. rcu_assign_pointer(lo->pool_ns,
  470. ceph_try_get_string(ci->i_cached_layout.pool_ns));
  471. got = want;
  472. no_async:
  473. spin_unlock(&ci->i_ceph_lock);
  474. return got;
  475. }
  476. static void restore_deleg_ino(struct inode *dir, u64 ino)
  477. {
  478. struct ceph_client *cl = ceph_inode_to_client(dir);
  479. struct ceph_inode_info *ci = ceph_inode(dir);
  480. struct ceph_mds_session *s = NULL;
  481. spin_lock(&ci->i_ceph_lock);
  482. if (ci->i_auth_cap)
  483. s = ceph_get_mds_session(ci->i_auth_cap->session);
  484. spin_unlock(&ci->i_ceph_lock);
  485. if (s) {
  486. int err = ceph_restore_deleg_ino(s, ino);
  487. if (err)
  488. pr_warn_client(cl,
  489. "unable to restore delegated ino 0x%llx to session: %d\n",
  490. ino, err);
  491. ceph_put_mds_session(s);
  492. }
  493. }
  494. static void wake_async_create_waiters(struct inode *inode,
  495. struct ceph_mds_session *session)
  496. {
  497. struct ceph_inode_info *ci = ceph_inode(inode);
  498. bool check_cap = false;
  499. spin_lock(&ci->i_ceph_lock);
  500. if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE) {
  501. clear_and_wake_up_bit(CEPH_ASYNC_CREATE_BIT, &ci->i_ceph_flags);
  502. if (ci->i_ceph_flags & CEPH_I_ASYNC_CHECK_CAPS) {
  503. ci->i_ceph_flags &= ~CEPH_I_ASYNC_CHECK_CAPS;
  504. check_cap = true;
  505. }
  506. }
  507. ceph_kick_flushing_inode_caps(session, ci);
  508. spin_unlock(&ci->i_ceph_lock);
  509. if (check_cap)
  510. ceph_check_caps(ci, CHECK_CAPS_FLUSH);
  511. }
  512. static void ceph_async_create_cb(struct ceph_mds_client *mdsc,
  513. struct ceph_mds_request *req)
  514. {
  515. struct ceph_client *cl = mdsc->fsc->client;
  516. struct dentry *dentry = req->r_dentry;
  517. struct inode *dinode = d_inode(dentry);
  518. struct inode *tinode = req->r_target_inode;
  519. int result = req->r_err ? req->r_err :
  520. le32_to_cpu(req->r_reply_info.head->result);
  521. WARN_ON_ONCE(dinode && tinode && dinode != tinode);
  522. /* MDS changed -- caller must resubmit */
  523. if (result == -EJUKEBOX)
  524. goto out;
  525. mapping_set_error(req->r_parent->i_mapping, result);
  526. if (result) {
  527. struct ceph_path_info path_info = {0};
  528. char *path = ceph_mdsc_build_path(mdsc, req->r_dentry, &path_info, 0);
  529. pr_warn_client(cl,
  530. "async create failure path=(%llx)%s result=%d!\n",
  531. path_info.vino.ino, IS_ERR(path) ? "<<bad>>" : path, result);
  532. ceph_mdsc_free_path_info(&path_info);
  533. ceph_dir_clear_complete(req->r_parent);
  534. if (!d_unhashed(dentry))
  535. d_drop(dentry);
  536. if (dinode) {
  537. mapping_set_error(dinode->i_mapping, result);
  538. ceph_inode_shutdown(dinode);
  539. wake_async_create_waiters(dinode, req->r_session);
  540. }
  541. }
  542. if (tinode) {
  543. u64 ino = ceph_vino(tinode).ino;
  544. if (req->r_deleg_ino != ino)
  545. pr_warn_client(cl,
  546. "inode number mismatch! err=%d deleg_ino=0x%llx target=0x%llx\n",
  547. req->r_err, req->r_deleg_ino, ino);
  548. mapping_set_error(tinode->i_mapping, result);
  549. wake_async_create_waiters(tinode, req->r_session);
  550. } else if (!result) {
  551. pr_warn_client(cl, "no req->r_target_inode for 0x%llx\n",
  552. req->r_deleg_ino);
  553. }
  554. out:
  555. ceph_mdsc_release_dir_caps(req);
  556. }
  557. static int ceph_finish_async_create(struct inode *dir, struct inode *inode,
  558. struct dentry *dentry,
  559. struct file *file, umode_t mode,
  560. struct ceph_mds_request *req,
  561. struct ceph_acl_sec_ctx *as_ctx,
  562. struct ceph_file_layout *lo)
  563. {
  564. int ret;
  565. char xattr_buf[4];
  566. struct ceph_mds_reply_inode in = { };
  567. struct ceph_mds_reply_info_in iinfo = { .in = &in };
  568. struct ceph_inode_info *ci = ceph_inode(dir);
  569. struct ceph_dentry_info *di = ceph_dentry(dentry);
  570. struct timespec64 now;
  571. struct ceph_string *pool_ns;
  572. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
  573. struct ceph_client *cl = mdsc->fsc->client;
  574. struct ceph_vino vino = { .ino = req->r_deleg_ino,
  575. .snap = CEPH_NOSNAP };
  576. ktime_get_real_ts64(&now);
  577. iinfo.inline_version = CEPH_INLINE_NONE;
  578. iinfo.change_attr = 1;
  579. ceph_encode_timespec64(&iinfo.btime, &now);
  580. if (req->r_pagelist) {
  581. iinfo.xattr_len = req->r_pagelist->length;
  582. iinfo.xattr_data = req->r_pagelist->mapped_tail;
  583. } else {
  584. /* fake it */
  585. iinfo.xattr_len = ARRAY_SIZE(xattr_buf);
  586. iinfo.xattr_data = xattr_buf;
  587. memset(iinfo.xattr_data, 0, iinfo.xattr_len);
  588. }
  589. in.ino = cpu_to_le64(vino.ino);
  590. in.snapid = cpu_to_le64(CEPH_NOSNAP);
  591. in.version = cpu_to_le64(1); // ???
  592. in.cap.caps = in.cap.wanted = cpu_to_le32(CEPH_CAP_ALL_FILE);
  593. in.cap.cap_id = cpu_to_le64(1);
  594. in.cap.realm = cpu_to_le64(ci->i_snap_realm->ino);
  595. in.cap.flags = CEPH_CAP_FLAG_AUTH;
  596. in.ctime = in.mtime = in.atime = iinfo.btime;
  597. in.truncate_seq = cpu_to_le32(1);
  598. in.truncate_size = cpu_to_le64(-1ULL);
  599. in.xattr_version = cpu_to_le64(1);
  600. in.uid = cpu_to_le32(from_kuid(&init_user_ns,
  601. mapped_fsuid(req->r_mnt_idmap,
  602. &init_user_ns)));
  603. if (dir->i_mode & S_ISGID) {
  604. in.gid = cpu_to_le32(from_kgid(&init_user_ns, dir->i_gid));
  605. /* Directories always inherit the setgid bit. */
  606. if (S_ISDIR(mode))
  607. mode |= S_ISGID;
  608. } else {
  609. in.gid = cpu_to_le32(from_kgid(&init_user_ns,
  610. mapped_fsgid(req->r_mnt_idmap,
  611. &init_user_ns)));
  612. }
  613. in.mode = cpu_to_le32((u32)mode);
  614. in.nlink = cpu_to_le32(1);
  615. in.max_size = cpu_to_le64(lo->stripe_unit);
  616. ceph_file_layout_to_legacy(lo, &in.layout);
  617. /* lo is private, so pool_ns can't change */
  618. pool_ns = rcu_dereference_raw(lo->pool_ns);
  619. if (pool_ns) {
  620. iinfo.pool_ns_len = pool_ns->len;
  621. iinfo.pool_ns_data = pool_ns->str;
  622. }
  623. down_read(&mdsc->snap_rwsem);
  624. ret = ceph_fill_inode(inode, NULL, &iinfo, NULL, req->r_session,
  625. req->r_fmode, NULL);
  626. up_read(&mdsc->snap_rwsem);
  627. if (ret) {
  628. doutc(cl, "failed to fill inode: %d\n", ret);
  629. ceph_dir_clear_complete(dir);
  630. if (!d_unhashed(dentry))
  631. d_drop(dentry);
  632. discard_new_inode(inode);
  633. } else {
  634. struct dentry *dn;
  635. doutc(cl, "d_adding new inode 0x%llx to 0x%llx/%s\n",
  636. vino.ino, ceph_ino(dir), dentry->d_name.name);
  637. ceph_dir_clear_ordered(dir);
  638. ceph_init_inode_acls(inode, as_ctx);
  639. if (inode_state_read_once(inode) & I_NEW) {
  640. /*
  641. * If it's not I_NEW, then someone created this before
  642. * we got here. Assume the server is aware of it at
  643. * that point and don't worry about setting
  644. * CEPH_I_ASYNC_CREATE.
  645. */
  646. ceph_inode(inode)->i_ceph_flags = CEPH_I_ASYNC_CREATE;
  647. unlock_new_inode(inode);
  648. }
  649. if (d_in_lookup(dentry) || d_really_is_negative(dentry)) {
  650. if (!d_unhashed(dentry))
  651. d_drop(dentry);
  652. dn = d_splice_alias(inode, dentry);
  653. WARN_ON_ONCE(dn && dn != dentry);
  654. }
  655. file->f_mode |= FMODE_CREATED;
  656. ret = finish_open(file, dentry, ceph_open);
  657. }
  658. spin_lock(&dentry->d_lock);
  659. clear_and_wake_up_bit(CEPH_DENTRY_ASYNC_CREATE_BIT, &di->flags);
  660. spin_unlock(&dentry->d_lock);
  661. return ret;
  662. }
  663. /*
  664. * Do a lookup + open with a single request. If we get a non-existent
  665. * file or symlink, return 1 so the VFS can retry.
  666. */
  667. int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  668. struct file *file, unsigned flags, umode_t mode)
  669. {
  670. struct mnt_idmap *idmap = file_mnt_idmap(file);
  671. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dir->i_sb);
  672. struct ceph_client *cl = fsc->client;
  673. struct ceph_mds_client *mdsc = fsc->mdsc;
  674. struct ceph_mds_request *req;
  675. struct inode *new_inode = NULL;
  676. struct dentry *dn;
  677. struct ceph_acl_sec_ctx as_ctx = {};
  678. bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
  679. int mask;
  680. int err;
  681. char *path;
  682. doutc(cl, "%p %llx.%llx dentry %p '%pd' %s flags %d mode 0%o\n",
  683. dir, ceph_vinop(dir), dentry, dentry,
  684. d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
  685. if (dentry->d_name.len > NAME_MAX)
  686. return -ENAMETOOLONG;
  687. err = ceph_wait_on_conflict_unlink(dentry);
  688. if (err)
  689. return err;
  690. /*
  691. * Do not truncate the file, since atomic_open is called before the
  692. * permission check. The caller will do the truncation afterward.
  693. */
  694. flags &= ~O_TRUNC;
  695. dn = d_find_alias(dir);
  696. if (!dn) {
  697. try_async = false;
  698. } else {
  699. struct ceph_path_info path_info = {0};
  700. path = ceph_mdsc_build_path(mdsc, dn, &path_info, 0);
  701. if (IS_ERR(path)) {
  702. try_async = false;
  703. err = 0;
  704. } else {
  705. int fmode = ceph_flags_to_mode(flags);
  706. mask = MAY_READ;
  707. if (fmode & CEPH_FILE_MODE_WR)
  708. mask |= MAY_WRITE;
  709. err = ceph_mds_check_access(mdsc, path, mask);
  710. }
  711. ceph_mdsc_free_path_info(&path_info);
  712. dput(dn);
  713. /* For none EACCES cases will let the MDS do the mds auth check */
  714. if (err == -EACCES) {
  715. return err;
  716. } else if (err < 0) {
  717. try_async = false;
  718. err = 0;
  719. }
  720. }
  721. retry:
  722. if (flags & O_CREAT) {
  723. if (ceph_quota_is_max_files_exceeded(dir))
  724. return -EDQUOT;
  725. new_inode = ceph_new_inode(dir, dentry, &mode, &as_ctx);
  726. if (IS_ERR(new_inode)) {
  727. err = PTR_ERR(new_inode);
  728. goto out_ctx;
  729. }
  730. /* Async create can't handle more than a page of xattrs */
  731. if (as_ctx.pagelist &&
  732. !list_is_singular(&as_ctx.pagelist->head))
  733. try_async = false;
  734. } else if (!d_in_lookup(dentry)) {
  735. /* If it's not being looked up, it's negative */
  736. return -ENOENT;
  737. }
  738. /* do the open */
  739. req = prepare_open_request(dir->i_sb, flags, mode);
  740. if (IS_ERR(req)) {
  741. err = PTR_ERR(req);
  742. goto out_ctx;
  743. }
  744. req->r_dentry = dget(dentry);
  745. req->r_num_caps = 2;
  746. mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
  747. if (ceph_security_xattr_wanted(dir))
  748. mask |= CEPH_CAP_XATTR_SHARED;
  749. req->r_args.open.mask = cpu_to_le32(mask);
  750. req->r_parent = dir;
  751. if (req->r_op == CEPH_MDS_OP_CREATE)
  752. req->r_mnt_idmap = mnt_idmap_get(idmap);
  753. ihold(dir);
  754. if (IS_ENCRYPTED(dir)) {
  755. set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
  756. err = fscrypt_prepare_lookup_partial(dir, dentry);
  757. if (err < 0)
  758. goto out_req;
  759. }
  760. if (flags & O_CREAT) {
  761. struct ceph_file_layout lo;
  762. req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL |
  763. CEPH_CAP_XATTR_EXCL;
  764. req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
  765. ceph_as_ctx_to_req(req, &as_ctx);
  766. if (try_async && (req->r_dir_caps =
  767. try_prep_async_create(dir, dentry, &lo,
  768. &req->r_deleg_ino))) {
  769. struct ceph_vino vino = { .ino = req->r_deleg_ino,
  770. .snap = CEPH_NOSNAP };
  771. struct ceph_dentry_info *di = ceph_dentry(dentry);
  772. set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
  773. req->r_args.open.flags |= cpu_to_le32(CEPH_O_EXCL);
  774. req->r_callback = ceph_async_create_cb;
  775. /* Hash inode before RPC */
  776. new_inode = ceph_get_inode(dir->i_sb, vino, new_inode);
  777. if (IS_ERR(new_inode)) {
  778. err = PTR_ERR(new_inode);
  779. new_inode = NULL;
  780. goto out_req;
  781. }
  782. WARN_ON_ONCE(!(inode_state_read_once(new_inode) & I_NEW));
  783. spin_lock(&dentry->d_lock);
  784. di->flags |= CEPH_DENTRY_ASYNC_CREATE;
  785. spin_unlock(&dentry->d_lock);
  786. err = ceph_mdsc_submit_request(mdsc, dir, req);
  787. if (!err) {
  788. err = ceph_finish_async_create(dir, new_inode,
  789. dentry, file,
  790. mode, req,
  791. &as_ctx, &lo);
  792. new_inode = NULL;
  793. } else if (err == -EJUKEBOX) {
  794. restore_deleg_ino(dir, req->r_deleg_ino);
  795. ceph_mdsc_put_request(req);
  796. discard_new_inode(new_inode);
  797. ceph_release_acl_sec_ctx(&as_ctx);
  798. memset(&as_ctx, 0, sizeof(as_ctx));
  799. new_inode = NULL;
  800. try_async = false;
  801. ceph_put_string(rcu_dereference_raw(lo.pool_ns));
  802. goto retry;
  803. }
  804. ceph_put_string(rcu_dereference_raw(lo.pool_ns));
  805. goto out_req;
  806. }
  807. }
  808. set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
  809. req->r_new_inode = new_inode;
  810. new_inode = NULL;
  811. err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir : NULL, req);
  812. if (err == -ENOENT) {
  813. dentry = ceph_handle_snapdir(req, dentry);
  814. if (IS_ERR(dentry)) {
  815. err = PTR_ERR(dentry);
  816. goto out_req;
  817. }
  818. err = 0;
  819. }
  820. if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
  821. err = ceph_handle_notrace_create(dir, dentry);
  822. if (d_in_lookup(dentry)) {
  823. dn = ceph_finish_lookup(req, dentry, err);
  824. if (IS_ERR(dn))
  825. err = PTR_ERR(dn);
  826. } else {
  827. /* we were given a hashed negative dentry */
  828. dn = NULL;
  829. }
  830. if (err)
  831. goto out_req;
  832. if (dn || d_really_is_negative(dentry) || d_is_symlink(dentry)) {
  833. /* make vfs retry on splice, ENOENT, or symlink */
  834. doutc(cl, "finish_no_open on dn %p\n", dn);
  835. err = finish_no_open(file, dn);
  836. } else {
  837. if (IS_ENCRYPTED(dir) &&
  838. !fscrypt_has_permitted_context(dir, d_inode(dentry))) {
  839. pr_warn_client(cl,
  840. "Inconsistent encryption context (parent %llx:%llx child %llx:%llx)\n",
  841. ceph_vinop(dir), ceph_vinop(d_inode(dentry)));
  842. goto out_req;
  843. }
  844. doutc(cl, "finish_open on dn %p\n", dn);
  845. if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
  846. struct inode *newino = d_inode(dentry);
  847. cache_file_layout(dir, newino);
  848. ceph_init_inode_acls(newino, &as_ctx);
  849. file->f_mode |= FMODE_CREATED;
  850. }
  851. err = finish_open(file, dentry, ceph_open);
  852. }
  853. out_req:
  854. ceph_mdsc_put_request(req);
  855. iput(new_inode);
  856. out_ctx:
  857. ceph_release_acl_sec_ctx(&as_ctx);
  858. doutc(cl, "result=%d\n", err);
  859. return err;
  860. }
  861. int ceph_release(struct inode *inode, struct file *file)
  862. {
  863. struct ceph_client *cl = ceph_inode_to_client(inode);
  864. struct ceph_inode_info *ci = ceph_inode(inode);
  865. if (S_ISDIR(inode->i_mode)) {
  866. struct ceph_dir_file_info *dfi = file->private_data;
  867. doutc(cl, "%p %llx.%llx dir file %p\n", inode,
  868. ceph_vinop(inode), file);
  869. WARN_ON(!list_empty(&dfi->file_info.rw_contexts));
  870. ceph_put_fmode(ci, dfi->file_info.fmode, 1);
  871. if (dfi->last_readdir)
  872. ceph_mdsc_put_request(dfi->last_readdir);
  873. kfree(dfi->last_name);
  874. kfree(dfi->dir_info);
  875. kmem_cache_free(ceph_dir_file_cachep, dfi);
  876. } else {
  877. struct ceph_file_info *fi = file->private_data;
  878. doutc(cl, "%p %llx.%llx regular file %p\n", inode,
  879. ceph_vinop(inode), file);
  880. WARN_ON(!list_empty(&fi->rw_contexts));
  881. ceph_fscache_unuse_cookie(inode, file->f_mode & FMODE_WRITE);
  882. ceph_put_fmode(ci, fi->fmode, 1);
  883. kmem_cache_free(ceph_file_cachep, fi);
  884. }
  885. /* wake up anyone waiting for caps on this inode */
  886. wake_up_all(&ci->i_cap_wq);
  887. return 0;
  888. }
  889. enum {
  890. HAVE_RETRIED = 1,
  891. CHECK_EOF = 2,
  892. READ_INLINE = 3,
  893. };
  894. /*
  895. * Completely synchronous read and write methods. Direct from __user
  896. * buffer to osd, or directly to user pages (if O_DIRECT).
  897. *
  898. * If the read spans object boundary, just do multiple reads. (That's not
  899. * atomic, but good enough for now.)
  900. *
  901. * If we get a short result from the OSD, check against i_size; we need to
  902. * only return a short read to the caller if we hit EOF.
  903. */
  904. ssize_t __ceph_sync_read(struct inode *inode, loff_t *ki_pos,
  905. struct iov_iter *to, int *retry_op,
  906. u64 *last_objver)
  907. {
  908. struct ceph_inode_info *ci = ceph_inode(inode);
  909. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  910. struct ceph_client *cl = fsc->client;
  911. struct ceph_osd_client *osdc = &fsc->client->osdc;
  912. ssize_t ret;
  913. u64 off = *ki_pos;
  914. u64 len = iov_iter_count(to);
  915. u64 i_size = i_size_read(inode);
  916. bool sparse = IS_ENCRYPTED(inode) || ceph_test_mount_opt(fsc, SPARSEREAD);
  917. u64 objver = 0;
  918. doutc(cl, "on inode %p %llx.%llx %llx~%llx\n", inode,
  919. ceph_vinop(inode), *ki_pos, len);
  920. if (ceph_inode_is_shutdown(inode))
  921. return -EIO;
  922. if (!len || !i_size)
  923. return 0;
  924. /*
  925. * flush any page cache pages in this range. this
  926. * will make concurrent normal and sync io slow,
  927. * but it will at least behave sensibly when they are
  928. * in sequence.
  929. */
  930. ret = filemap_write_and_wait_range(inode->i_mapping,
  931. off, off + len - 1);
  932. if (ret < 0)
  933. return ret;
  934. ret = 0;
  935. while ((len = iov_iter_count(to)) > 0) {
  936. struct ceph_osd_request *req;
  937. struct page **pages;
  938. int num_pages;
  939. size_t page_off;
  940. bool more;
  941. int idx = 0;
  942. size_t left;
  943. struct ceph_osd_req_op *op;
  944. u64 read_off = off;
  945. u64 read_len = len;
  946. int extent_cnt;
  947. /* determine new offset/length if encrypted */
  948. ceph_fscrypt_adjust_off_and_len(inode, &read_off, &read_len);
  949. doutc(cl, "orig %llu~%llu reading %llu~%llu", off, len,
  950. read_off, read_len);
  951. req = ceph_osdc_new_request(osdc, &ci->i_layout,
  952. ci->i_vino, read_off, &read_len, 0, 1,
  953. sparse ? CEPH_OSD_OP_SPARSE_READ :
  954. CEPH_OSD_OP_READ,
  955. CEPH_OSD_FLAG_READ,
  956. NULL, ci->i_truncate_seq,
  957. ci->i_truncate_size, false);
  958. if (IS_ERR(req)) {
  959. ret = PTR_ERR(req);
  960. break;
  961. }
  962. /* adjust len downward if the request truncated the len */
  963. if (off + len > read_off + read_len)
  964. len = read_off + read_len - off;
  965. more = len < iov_iter_count(to);
  966. op = &req->r_ops[0];
  967. if (sparse) {
  968. extent_cnt = __ceph_sparse_read_ext_count(inode, read_len);
  969. ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
  970. if (ret) {
  971. ceph_osdc_put_request(req);
  972. break;
  973. }
  974. }
  975. num_pages = calc_pages_for(read_off, read_len);
  976. page_off = offset_in_page(off);
  977. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  978. if (IS_ERR(pages)) {
  979. ceph_osdc_put_request(req);
  980. ret = PTR_ERR(pages);
  981. break;
  982. }
  983. osd_req_op_extent_osd_data_pages(req, 0, pages, read_len,
  984. offset_in_page(read_off),
  985. false, true);
  986. ceph_osdc_start_request(osdc, req);
  987. ret = ceph_osdc_wait_request(osdc, req);
  988. ceph_update_read_metrics(&fsc->mdsc->metric,
  989. req->r_start_latency,
  990. req->r_end_latency,
  991. read_len, ret);
  992. if (ret > 0)
  993. objver = req->r_version;
  994. i_size = i_size_read(inode);
  995. doutc(cl, "%llu~%llu got %zd i_size %llu%s\n", off, len,
  996. ret, i_size, (more ? " MORE" : ""));
  997. /* Fix it to go to end of extent map */
  998. if (sparse && ret >= 0)
  999. ret = ceph_sparse_ext_map_end(op);
  1000. else if (ret == -ENOENT)
  1001. ret = 0;
  1002. if (ret < 0) {
  1003. ceph_osdc_put_request(req);
  1004. if (ret == -EBLOCKLISTED)
  1005. fsc->blocklisted = true;
  1006. break;
  1007. }
  1008. if (IS_ENCRYPTED(inode)) {
  1009. int fret;
  1010. fret = ceph_fscrypt_decrypt_extents(inode, pages,
  1011. read_off, op->extent.sparse_ext,
  1012. op->extent.sparse_ext_cnt);
  1013. if (fret < 0) {
  1014. ret = fret;
  1015. ceph_osdc_put_request(req);
  1016. break;
  1017. }
  1018. /* account for any partial block at the beginning */
  1019. fret -= (off - read_off);
  1020. /*
  1021. * Short read after big offset adjustment?
  1022. * Nothing is usable, just call it a zero
  1023. * len read.
  1024. */
  1025. fret = max(fret, 0);
  1026. /* account for partial block at the end */
  1027. ret = min_t(ssize_t, fret, len);
  1028. }
  1029. /* Short read but not EOF? Zero out the remainder. */
  1030. if (ret < len && (off + ret < i_size)) {
  1031. int zlen = min(len - ret, i_size - off - ret);
  1032. int zoff = page_off + ret;
  1033. doutc(cl, "zero gap %llu~%llu\n", off + ret,
  1034. off + ret + zlen);
  1035. ceph_zero_page_vector_range(zoff, zlen, pages);
  1036. ret += zlen;
  1037. }
  1038. if (off + ret > i_size)
  1039. left = (i_size > off) ? i_size - off : 0;
  1040. else
  1041. left = ret;
  1042. while (left > 0) {
  1043. size_t plen, copied;
  1044. plen = min_t(size_t, left, PAGE_SIZE - page_off);
  1045. SetPageUptodate(pages[idx]);
  1046. copied = copy_page_to_iter(pages[idx++],
  1047. page_off, plen, to);
  1048. off += copied;
  1049. left -= copied;
  1050. page_off = 0;
  1051. if (copied < plen) {
  1052. ret = -EFAULT;
  1053. break;
  1054. }
  1055. }
  1056. ceph_osdc_put_request(req);
  1057. if (off >= i_size || !more)
  1058. break;
  1059. }
  1060. if (ret > 0) {
  1061. if (off >= i_size) {
  1062. *retry_op = CHECK_EOF;
  1063. ret = i_size - *ki_pos;
  1064. *ki_pos = i_size;
  1065. } else {
  1066. ret = off - *ki_pos;
  1067. *ki_pos = off;
  1068. }
  1069. if (last_objver)
  1070. *last_objver = objver;
  1071. }
  1072. doutc(cl, "result %zd retry_op %d\n", ret, *retry_op);
  1073. return ret;
  1074. }
  1075. static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *to,
  1076. int *retry_op)
  1077. {
  1078. struct file *file = iocb->ki_filp;
  1079. struct inode *inode = file_inode(file);
  1080. struct ceph_client *cl = ceph_inode_to_client(inode);
  1081. doutc(cl, "on file %p %llx~%zx %s\n", file, iocb->ki_pos,
  1082. iov_iter_count(to),
  1083. (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
  1084. return __ceph_sync_read(inode, &iocb->ki_pos, to, retry_op, NULL);
  1085. }
  1086. struct ceph_aio_request {
  1087. struct kiocb *iocb;
  1088. size_t total_len;
  1089. bool write;
  1090. bool should_dirty;
  1091. int error;
  1092. struct list_head osd_reqs;
  1093. unsigned num_reqs;
  1094. atomic_t pending_reqs;
  1095. struct timespec64 mtime;
  1096. struct ceph_cap_flush *prealloc_cf;
  1097. };
  1098. struct ceph_aio_work {
  1099. struct work_struct work;
  1100. struct ceph_osd_request *req;
  1101. };
  1102. static void ceph_aio_retry_work(struct work_struct *work);
  1103. static void ceph_aio_complete(struct inode *inode,
  1104. struct ceph_aio_request *aio_req)
  1105. {
  1106. struct ceph_client *cl = ceph_inode_to_client(inode);
  1107. struct ceph_inode_info *ci = ceph_inode(inode);
  1108. int ret;
  1109. if (!atomic_dec_and_test(&aio_req->pending_reqs))
  1110. return;
  1111. if (aio_req->iocb->ki_flags & IOCB_DIRECT)
  1112. inode_dio_end(inode);
  1113. ret = aio_req->error;
  1114. if (!ret)
  1115. ret = aio_req->total_len;
  1116. doutc(cl, "%p %llx.%llx rc %d\n", inode, ceph_vinop(inode), ret);
  1117. if (ret >= 0 && aio_req->write) {
  1118. int dirty;
  1119. loff_t endoff = aio_req->iocb->ki_pos + aio_req->total_len;
  1120. if (endoff > i_size_read(inode)) {
  1121. if (ceph_inode_set_size(inode, endoff))
  1122. ceph_check_caps(ci, CHECK_CAPS_AUTHONLY);
  1123. }
  1124. spin_lock(&ci->i_ceph_lock);
  1125. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  1126. &aio_req->prealloc_cf);
  1127. spin_unlock(&ci->i_ceph_lock);
  1128. if (dirty)
  1129. __mark_inode_dirty(inode, dirty);
  1130. }
  1131. ceph_put_cap_refs(ci, (aio_req->write ? CEPH_CAP_FILE_WR :
  1132. CEPH_CAP_FILE_RD));
  1133. aio_req->iocb->ki_complete(aio_req->iocb, ret);
  1134. ceph_free_cap_flush(aio_req->prealloc_cf);
  1135. kfree(aio_req);
  1136. }
  1137. static void ceph_aio_complete_req(struct ceph_osd_request *req)
  1138. {
  1139. int rc = req->r_result;
  1140. struct inode *inode = req->r_inode;
  1141. struct ceph_aio_request *aio_req = req->r_priv;
  1142. struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0);
  1143. struct ceph_osd_req_op *op = &req->r_ops[0];
  1144. struct ceph_client_metric *metric = &ceph_sb_to_mdsc(inode->i_sb)->metric;
  1145. unsigned int len = osd_data->bvec_pos.iter.bi_size;
  1146. bool sparse = (op->op == CEPH_OSD_OP_SPARSE_READ);
  1147. struct ceph_client *cl = ceph_inode_to_client(inode);
  1148. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS);
  1149. BUG_ON(!osd_data->num_bvecs);
  1150. doutc(cl, "req %p inode %p %llx.%llx, rc %d bytes %u\n", req,
  1151. inode, ceph_vinop(inode), rc, len);
  1152. if (rc == -EOLDSNAPC) {
  1153. struct ceph_aio_work *aio_work;
  1154. BUG_ON(!aio_req->write);
  1155. aio_work = kmalloc_obj(*aio_work, GFP_NOFS);
  1156. if (aio_work) {
  1157. INIT_WORK(&aio_work->work, ceph_aio_retry_work);
  1158. aio_work->req = req;
  1159. queue_work(ceph_inode_to_fs_client(inode)->inode_wq,
  1160. &aio_work->work);
  1161. return;
  1162. }
  1163. rc = -ENOMEM;
  1164. } else if (!aio_req->write) {
  1165. if (sparse && rc >= 0)
  1166. rc = ceph_sparse_ext_map_end(op);
  1167. if (rc == -ENOENT)
  1168. rc = 0;
  1169. if (rc >= 0 && len > rc) {
  1170. struct iov_iter i;
  1171. int zlen = len - rc;
  1172. /*
  1173. * If read is satisfied by single OSD request,
  1174. * it can pass EOF. Otherwise read is within
  1175. * i_size.
  1176. */
  1177. if (aio_req->num_reqs == 1) {
  1178. loff_t i_size = i_size_read(inode);
  1179. loff_t endoff = aio_req->iocb->ki_pos + rc;
  1180. if (endoff < i_size)
  1181. zlen = min_t(size_t, zlen,
  1182. i_size - endoff);
  1183. aio_req->total_len = rc + zlen;
  1184. }
  1185. iov_iter_bvec(&i, ITER_DEST, osd_data->bvec_pos.bvecs,
  1186. osd_data->num_bvecs, len);
  1187. iov_iter_advance(&i, rc);
  1188. iov_iter_zero(zlen, &i);
  1189. }
  1190. }
  1191. /* r_start_latency == 0 means the request was not submitted */
  1192. if (req->r_start_latency) {
  1193. if (aio_req->write)
  1194. ceph_update_write_metrics(metric, req->r_start_latency,
  1195. req->r_end_latency, len, rc);
  1196. else
  1197. ceph_update_read_metrics(metric, req->r_start_latency,
  1198. req->r_end_latency, len, rc);
  1199. }
  1200. put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs,
  1201. aio_req->should_dirty);
  1202. ceph_osdc_put_request(req);
  1203. if (rc < 0)
  1204. cmpxchg(&aio_req->error, 0, rc);
  1205. ceph_aio_complete(inode, aio_req);
  1206. return;
  1207. }
  1208. static void ceph_aio_retry_work(struct work_struct *work)
  1209. {
  1210. struct ceph_aio_work *aio_work =
  1211. container_of(work, struct ceph_aio_work, work);
  1212. struct ceph_osd_request *orig_req = aio_work->req;
  1213. struct ceph_aio_request *aio_req = orig_req->r_priv;
  1214. struct inode *inode = orig_req->r_inode;
  1215. struct ceph_inode_info *ci = ceph_inode(inode);
  1216. struct ceph_snap_context *snapc;
  1217. struct ceph_osd_request *req;
  1218. int ret;
  1219. spin_lock(&ci->i_ceph_lock);
  1220. if (__ceph_have_pending_cap_snap(ci)) {
  1221. struct ceph_cap_snap *capsnap =
  1222. list_last_entry(&ci->i_cap_snaps,
  1223. struct ceph_cap_snap,
  1224. ci_item);
  1225. snapc = ceph_get_snap_context(capsnap->context);
  1226. } else {
  1227. BUG_ON(!ci->i_head_snapc);
  1228. snapc = ceph_get_snap_context(ci->i_head_snapc);
  1229. }
  1230. spin_unlock(&ci->i_ceph_lock);
  1231. req = ceph_osdc_alloc_request(orig_req->r_osdc, snapc, 1,
  1232. false, GFP_NOFS);
  1233. if (!req) {
  1234. ret = -ENOMEM;
  1235. req = orig_req;
  1236. goto out;
  1237. }
  1238. req->r_flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
  1239. ceph_oloc_copy(&req->r_base_oloc, &orig_req->r_base_oloc);
  1240. ceph_oid_copy(&req->r_base_oid, &orig_req->r_base_oid);
  1241. req->r_ops[0] = orig_req->r_ops[0];
  1242. req->r_mtime = aio_req->mtime;
  1243. req->r_data_offset = req->r_ops[0].extent.offset;
  1244. ret = ceph_osdc_alloc_messages(req, GFP_NOFS);
  1245. if (ret) {
  1246. ceph_osdc_put_request(req);
  1247. req = orig_req;
  1248. goto out;
  1249. }
  1250. ceph_osdc_put_request(orig_req);
  1251. req->r_callback = ceph_aio_complete_req;
  1252. req->r_inode = inode;
  1253. req->r_priv = aio_req;
  1254. ceph_osdc_start_request(req->r_osdc, req);
  1255. out:
  1256. if (ret < 0) {
  1257. req->r_result = ret;
  1258. ceph_aio_complete_req(req);
  1259. }
  1260. ceph_put_snap_context(snapc);
  1261. kfree(aio_work);
  1262. }
  1263. static ssize_t
  1264. ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter,
  1265. struct ceph_snap_context *snapc,
  1266. struct ceph_cap_flush **pcf)
  1267. {
  1268. struct file *file = iocb->ki_filp;
  1269. struct inode *inode = file_inode(file);
  1270. struct ceph_inode_info *ci = ceph_inode(inode);
  1271. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  1272. struct ceph_client *cl = fsc->client;
  1273. struct ceph_client_metric *metric = &fsc->mdsc->metric;
  1274. struct ceph_vino vino;
  1275. struct ceph_osd_request *req;
  1276. struct bio_vec *bvecs;
  1277. struct ceph_aio_request *aio_req = NULL;
  1278. int num_pages = 0;
  1279. int flags;
  1280. int ret = 0;
  1281. struct timespec64 mtime = current_time(inode);
  1282. size_t count = iov_iter_count(iter);
  1283. loff_t pos = iocb->ki_pos;
  1284. bool write = iov_iter_rw(iter) == WRITE;
  1285. bool should_dirty = !write && user_backed_iter(iter);
  1286. bool sparse = ceph_test_mount_opt(fsc, SPARSEREAD);
  1287. if (write && ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  1288. return -EROFS;
  1289. doutc(cl, "sync_direct_%s on file %p %lld~%u snapc %p seq %lld\n",
  1290. (write ? "write" : "read"), file, pos, (unsigned)count,
  1291. snapc, snapc ? snapc->seq : 0);
  1292. if (write) {
  1293. int ret2;
  1294. ceph_fscache_invalidate(inode, true);
  1295. ret2 = invalidate_inode_pages2_range(inode->i_mapping,
  1296. pos >> PAGE_SHIFT,
  1297. (pos + count - 1) >> PAGE_SHIFT);
  1298. if (ret2 < 0)
  1299. doutc(cl, "invalidate_inode_pages2_range returned %d\n",
  1300. ret2);
  1301. flags = /* CEPH_OSD_FLAG_ORDERSNAP | */ CEPH_OSD_FLAG_WRITE;
  1302. } else {
  1303. flags = CEPH_OSD_FLAG_READ;
  1304. }
  1305. while (iov_iter_count(iter) > 0) {
  1306. u64 size = iov_iter_count(iter);
  1307. ssize_t len;
  1308. struct ceph_osd_req_op *op;
  1309. int readop = sparse ? CEPH_OSD_OP_SPARSE_READ : CEPH_OSD_OP_READ;
  1310. int extent_cnt;
  1311. if (write)
  1312. size = min_t(u64, size, fsc->mount_options->wsize);
  1313. else
  1314. size = min_t(u64, size, fsc->mount_options->rsize);
  1315. vino = ceph_vino(inode);
  1316. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  1317. vino, pos, &size, 0,
  1318. 1,
  1319. write ? CEPH_OSD_OP_WRITE : readop,
  1320. flags, snapc,
  1321. ci->i_truncate_seq,
  1322. ci->i_truncate_size,
  1323. false);
  1324. if (IS_ERR(req)) {
  1325. ret = PTR_ERR(req);
  1326. break;
  1327. }
  1328. op = &req->r_ops[0];
  1329. if (!write && sparse) {
  1330. extent_cnt = __ceph_sparse_read_ext_count(inode, size);
  1331. ret = ceph_alloc_sparse_ext_map(op, extent_cnt);
  1332. if (ret) {
  1333. ceph_osdc_put_request(req);
  1334. break;
  1335. }
  1336. }
  1337. len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages);
  1338. if (len < 0) {
  1339. ceph_osdc_put_request(req);
  1340. ret = len;
  1341. break;
  1342. }
  1343. if (len != size)
  1344. osd_req_op_extent_update(req, 0, len);
  1345. osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len);
  1346. /*
  1347. * To simplify error handling, allow AIO when IO within i_size
  1348. * or IO can be satisfied by single OSD request.
  1349. */
  1350. if (pos == iocb->ki_pos && !is_sync_kiocb(iocb) &&
  1351. (len == count || pos + count <= i_size_read(inode))) {
  1352. aio_req = kzalloc_obj(*aio_req);
  1353. if (aio_req) {
  1354. aio_req->iocb = iocb;
  1355. aio_req->write = write;
  1356. aio_req->should_dirty = should_dirty;
  1357. INIT_LIST_HEAD(&aio_req->osd_reqs);
  1358. if (write) {
  1359. aio_req->mtime = mtime;
  1360. swap(aio_req->prealloc_cf, *pcf);
  1361. }
  1362. }
  1363. /* ignore error */
  1364. }
  1365. if (write) {
  1366. /*
  1367. * throw out any page cache pages in this range. this
  1368. * may block.
  1369. */
  1370. truncate_inode_pages_range(inode->i_mapping, pos,
  1371. PAGE_ALIGN(pos + len) - 1);
  1372. req->r_mtime = mtime;
  1373. }
  1374. if (aio_req) {
  1375. aio_req->total_len += len;
  1376. aio_req->num_reqs++;
  1377. atomic_inc(&aio_req->pending_reqs);
  1378. req->r_callback = ceph_aio_complete_req;
  1379. req->r_inode = inode;
  1380. req->r_priv = aio_req;
  1381. list_add_tail(&req->r_private_item, &aio_req->osd_reqs);
  1382. pos += len;
  1383. continue;
  1384. }
  1385. ceph_osdc_start_request(req->r_osdc, req);
  1386. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  1387. if (write)
  1388. ceph_update_write_metrics(metric, req->r_start_latency,
  1389. req->r_end_latency, len, ret);
  1390. else
  1391. ceph_update_read_metrics(metric, req->r_start_latency,
  1392. req->r_end_latency, len, ret);
  1393. size = i_size_read(inode);
  1394. if (!write) {
  1395. if (sparse && ret >= 0)
  1396. ret = ceph_sparse_ext_map_end(op);
  1397. else if (ret == -ENOENT)
  1398. ret = 0;
  1399. if (ret >= 0 && ret < len && pos + ret < size) {
  1400. struct iov_iter i;
  1401. int zlen = min_t(size_t, len - ret,
  1402. size - pos - ret);
  1403. iov_iter_bvec(&i, ITER_DEST, bvecs, num_pages, len);
  1404. iov_iter_advance(&i, ret);
  1405. iov_iter_zero(zlen, &i);
  1406. ret += zlen;
  1407. }
  1408. if (ret >= 0)
  1409. len = ret;
  1410. }
  1411. put_bvecs(bvecs, num_pages, should_dirty);
  1412. ceph_osdc_put_request(req);
  1413. if (ret < 0)
  1414. break;
  1415. pos += len;
  1416. if (!write && pos >= size)
  1417. break;
  1418. if (write && pos > size) {
  1419. if (ceph_inode_set_size(inode, pos))
  1420. ceph_check_caps(ceph_inode(inode),
  1421. CHECK_CAPS_AUTHONLY);
  1422. }
  1423. }
  1424. if (aio_req) {
  1425. LIST_HEAD(osd_reqs);
  1426. if (aio_req->num_reqs == 0) {
  1427. kfree(aio_req);
  1428. return ret;
  1429. }
  1430. ceph_get_cap_refs(ci, write ? CEPH_CAP_FILE_WR :
  1431. CEPH_CAP_FILE_RD);
  1432. list_splice(&aio_req->osd_reqs, &osd_reqs);
  1433. inode_dio_begin(inode);
  1434. while (!list_empty(&osd_reqs)) {
  1435. req = list_first_entry(&osd_reqs,
  1436. struct ceph_osd_request,
  1437. r_private_item);
  1438. list_del_init(&req->r_private_item);
  1439. if (ret >= 0)
  1440. ceph_osdc_start_request(req->r_osdc, req);
  1441. if (ret < 0) {
  1442. req->r_result = ret;
  1443. ceph_aio_complete_req(req);
  1444. }
  1445. }
  1446. return -EIOCBQUEUED;
  1447. }
  1448. if (ret != -EOLDSNAPC && pos > iocb->ki_pos) {
  1449. ret = pos - iocb->ki_pos;
  1450. iocb->ki_pos = pos;
  1451. }
  1452. return ret;
  1453. }
  1454. /*
  1455. * Synchronous write, straight from __user pointer or user pages.
  1456. *
  1457. * If write spans object boundary, just do multiple writes. (For a
  1458. * correct atomic write, we should e.g. take write locks on all
  1459. * objects, rollback on failure, etc.)
  1460. */
  1461. static ssize_t
  1462. ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos,
  1463. struct ceph_snap_context *snapc)
  1464. {
  1465. struct file *file = iocb->ki_filp;
  1466. struct inode *inode = file_inode(file);
  1467. struct ceph_inode_info *ci = ceph_inode(inode);
  1468. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  1469. struct ceph_client *cl = fsc->client;
  1470. struct ceph_osd_client *osdc = &fsc->client->osdc;
  1471. struct ceph_osd_request *req;
  1472. struct page **pages;
  1473. u64 len;
  1474. int num_pages;
  1475. int written = 0;
  1476. int ret;
  1477. bool check_caps = false;
  1478. struct timespec64 mtime = current_time(inode);
  1479. size_t count = iov_iter_count(from);
  1480. if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
  1481. return -EROFS;
  1482. doutc(cl, "on file %p %lld~%u snapc %p seq %lld\n", file, pos,
  1483. (unsigned)count, snapc, snapc->seq);
  1484. ret = filemap_write_and_wait_range(inode->i_mapping,
  1485. pos, pos + count - 1);
  1486. if (ret < 0)
  1487. return ret;
  1488. ceph_fscache_invalidate(inode, false);
  1489. while ((len = iov_iter_count(from)) > 0) {
  1490. size_t left;
  1491. int n;
  1492. u64 write_pos = pos;
  1493. u64 write_len = len;
  1494. u64 objnum, objoff;
  1495. u32 xlen;
  1496. u64 assert_ver = 0;
  1497. bool rmw;
  1498. bool first, last;
  1499. struct iov_iter saved_iter = *from;
  1500. size_t off;
  1501. ceph_fscrypt_adjust_off_and_len(inode, &write_pos, &write_len);
  1502. /* clamp the length to the end of first object */
  1503. ceph_calc_file_object_mapping(&ci->i_layout, write_pos,
  1504. write_len, &objnum, &objoff,
  1505. &xlen);
  1506. write_len = xlen;
  1507. /* adjust len downward if it goes beyond current object */
  1508. if (pos + len > write_pos + write_len)
  1509. len = write_pos + write_len - pos;
  1510. /*
  1511. * If we had to adjust the length or position to align with a
  1512. * crypto block, then we must do a read/modify/write cycle. We
  1513. * use a version assertion to redrive the thing if something
  1514. * changes in between.
  1515. */
  1516. first = pos != write_pos;
  1517. last = (pos + len) != (write_pos + write_len);
  1518. rmw = first || last;
  1519. doutc(cl, "ino %llx %lld~%llu adjusted %lld~%llu -- %srmw\n",
  1520. ci->i_vino.ino, pos, len, write_pos, write_len,
  1521. rmw ? "" : "no ");
  1522. /*
  1523. * The data is emplaced into the page as it would be if it were
  1524. * in an array of pagecache pages.
  1525. */
  1526. num_pages = calc_pages_for(write_pos, write_len);
  1527. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  1528. if (IS_ERR(pages)) {
  1529. ret = PTR_ERR(pages);
  1530. break;
  1531. }
  1532. /* Do we need to preload the pages? */
  1533. if (rmw) {
  1534. u64 first_pos = write_pos;
  1535. u64 last_pos = (write_pos + write_len) - CEPH_FSCRYPT_BLOCK_SIZE;
  1536. u64 read_len = CEPH_FSCRYPT_BLOCK_SIZE;
  1537. struct ceph_osd_req_op *op;
  1538. /* We should only need to do this for encrypted inodes */
  1539. WARN_ON_ONCE(!IS_ENCRYPTED(inode));
  1540. /* No need to do two reads if first and last blocks are same */
  1541. if (first && last_pos == first_pos)
  1542. last = false;
  1543. /*
  1544. * Allocate a read request for one or two extents,
  1545. * depending on how the request was aligned.
  1546. */
  1547. req = ceph_osdc_new_request(osdc, &ci->i_layout,
  1548. ci->i_vino, first ? first_pos : last_pos,
  1549. &read_len, 0, (first && last) ? 2 : 1,
  1550. CEPH_OSD_OP_SPARSE_READ, CEPH_OSD_FLAG_READ,
  1551. NULL, ci->i_truncate_seq,
  1552. ci->i_truncate_size, false);
  1553. if (IS_ERR(req)) {
  1554. ceph_release_page_vector(pages, num_pages);
  1555. ret = PTR_ERR(req);
  1556. break;
  1557. }
  1558. /* Something is misaligned! */
  1559. if (read_len != CEPH_FSCRYPT_BLOCK_SIZE) {
  1560. ceph_osdc_put_request(req);
  1561. ceph_release_page_vector(pages, num_pages);
  1562. ret = -EIO;
  1563. break;
  1564. }
  1565. /* Add extent for first block? */
  1566. op = &req->r_ops[0];
  1567. if (first) {
  1568. osd_req_op_extent_osd_data_pages(req, 0, pages,
  1569. CEPH_FSCRYPT_BLOCK_SIZE,
  1570. offset_in_page(first_pos),
  1571. false, false);
  1572. /* We only expect a single extent here */
  1573. ret = __ceph_alloc_sparse_ext_map(op, 1);
  1574. if (ret) {
  1575. ceph_osdc_put_request(req);
  1576. ceph_release_page_vector(pages, num_pages);
  1577. break;
  1578. }
  1579. }
  1580. /* Add extent for last block */
  1581. if (last) {
  1582. /* Init the other extent if first extent has been used */
  1583. if (first) {
  1584. op = &req->r_ops[1];
  1585. osd_req_op_extent_init(req, 1,
  1586. CEPH_OSD_OP_SPARSE_READ,
  1587. last_pos, CEPH_FSCRYPT_BLOCK_SIZE,
  1588. ci->i_truncate_size,
  1589. ci->i_truncate_seq);
  1590. }
  1591. ret = __ceph_alloc_sparse_ext_map(op, 1);
  1592. if (ret) {
  1593. ceph_osdc_put_request(req);
  1594. ceph_release_page_vector(pages, num_pages);
  1595. break;
  1596. }
  1597. osd_req_op_extent_osd_data_pages(req, first ? 1 : 0,
  1598. &pages[num_pages - 1],
  1599. CEPH_FSCRYPT_BLOCK_SIZE,
  1600. offset_in_page(last_pos),
  1601. false, false);
  1602. }
  1603. ceph_osdc_start_request(osdc, req);
  1604. ret = ceph_osdc_wait_request(osdc, req);
  1605. /* FIXME: length field is wrong if there are 2 extents */
  1606. ceph_update_read_metrics(&fsc->mdsc->metric,
  1607. req->r_start_latency,
  1608. req->r_end_latency,
  1609. read_len, ret);
  1610. /* Ok if object is not already present */
  1611. if (ret == -ENOENT) {
  1612. /*
  1613. * If there is no object, then we can't assert
  1614. * on its version. Set it to 0, and we'll use an
  1615. * exclusive create instead.
  1616. */
  1617. ceph_osdc_put_request(req);
  1618. ret = 0;
  1619. /*
  1620. * zero out the soon-to-be uncopied parts of the
  1621. * first and last pages.
  1622. */
  1623. if (first)
  1624. zero_user_segment(pages[0], 0,
  1625. offset_in_page(first_pos));
  1626. if (last)
  1627. zero_user_segment(pages[num_pages - 1],
  1628. offset_in_page(last_pos),
  1629. PAGE_SIZE);
  1630. } else {
  1631. if (ret < 0) {
  1632. ceph_osdc_put_request(req);
  1633. ceph_release_page_vector(pages, num_pages);
  1634. break;
  1635. }
  1636. op = &req->r_ops[0];
  1637. if (op->extent.sparse_ext_cnt == 0) {
  1638. if (first)
  1639. zero_user_segment(pages[0], 0,
  1640. offset_in_page(first_pos));
  1641. else
  1642. zero_user_segment(pages[num_pages - 1],
  1643. offset_in_page(last_pos),
  1644. PAGE_SIZE);
  1645. } else if (op->extent.sparse_ext_cnt != 1 ||
  1646. ceph_sparse_ext_map_end(op) !=
  1647. CEPH_FSCRYPT_BLOCK_SIZE) {
  1648. ret = -EIO;
  1649. ceph_osdc_put_request(req);
  1650. ceph_release_page_vector(pages, num_pages);
  1651. break;
  1652. }
  1653. if (first && last) {
  1654. op = &req->r_ops[1];
  1655. if (op->extent.sparse_ext_cnt == 0) {
  1656. zero_user_segment(pages[num_pages - 1],
  1657. offset_in_page(last_pos),
  1658. PAGE_SIZE);
  1659. } else if (op->extent.sparse_ext_cnt != 1 ||
  1660. ceph_sparse_ext_map_end(op) !=
  1661. CEPH_FSCRYPT_BLOCK_SIZE) {
  1662. ret = -EIO;
  1663. ceph_osdc_put_request(req);
  1664. ceph_release_page_vector(pages, num_pages);
  1665. break;
  1666. }
  1667. }
  1668. /* Grab assert version. It must be non-zero. */
  1669. assert_ver = req->r_version;
  1670. WARN_ON_ONCE(ret > 0 && assert_ver == 0);
  1671. ceph_osdc_put_request(req);
  1672. if (first) {
  1673. ret = ceph_fscrypt_decrypt_block_inplace(inode,
  1674. pages[0], CEPH_FSCRYPT_BLOCK_SIZE,
  1675. offset_in_page(first_pos),
  1676. first_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
  1677. if (ret < 0) {
  1678. ceph_release_page_vector(pages, num_pages);
  1679. break;
  1680. }
  1681. }
  1682. if (last) {
  1683. ret = ceph_fscrypt_decrypt_block_inplace(inode,
  1684. pages[num_pages - 1],
  1685. CEPH_FSCRYPT_BLOCK_SIZE,
  1686. offset_in_page(last_pos),
  1687. last_pos >> CEPH_FSCRYPT_BLOCK_SHIFT);
  1688. if (ret < 0) {
  1689. ceph_release_page_vector(pages, num_pages);
  1690. break;
  1691. }
  1692. }
  1693. }
  1694. }
  1695. left = len;
  1696. off = offset_in_page(pos);
  1697. for (n = 0; n < num_pages; n++) {
  1698. size_t plen = min_t(size_t, left, PAGE_SIZE - off);
  1699. /* copy the data */
  1700. ret = copy_page_from_iter(pages[n], off, plen, from);
  1701. if (ret != plen) {
  1702. ret = -EFAULT;
  1703. break;
  1704. }
  1705. off = 0;
  1706. left -= ret;
  1707. }
  1708. if (ret < 0) {
  1709. doutc(cl, "write failed with %d\n", ret);
  1710. ceph_release_page_vector(pages, num_pages);
  1711. break;
  1712. }
  1713. if (IS_ENCRYPTED(inode)) {
  1714. ret = ceph_fscrypt_encrypt_pages(inode, pages,
  1715. write_pos, write_len);
  1716. if (ret < 0) {
  1717. doutc(cl, "encryption failed with %d\n", ret);
  1718. ceph_release_page_vector(pages, num_pages);
  1719. break;
  1720. }
  1721. }
  1722. req = ceph_osdc_new_request(osdc, &ci->i_layout,
  1723. ci->i_vino, write_pos, &write_len,
  1724. rmw ? 1 : 0, rmw ? 2 : 1,
  1725. CEPH_OSD_OP_WRITE,
  1726. CEPH_OSD_FLAG_WRITE,
  1727. snapc, ci->i_truncate_seq,
  1728. ci->i_truncate_size, false);
  1729. if (IS_ERR(req)) {
  1730. ret = PTR_ERR(req);
  1731. ceph_release_page_vector(pages, num_pages);
  1732. break;
  1733. }
  1734. doutc(cl, "write op %lld~%llu\n", write_pos, write_len);
  1735. osd_req_op_extent_osd_data_pages(req, rmw ? 1 : 0, pages, write_len,
  1736. offset_in_page(write_pos), false,
  1737. true);
  1738. req->r_inode = inode;
  1739. req->r_mtime = mtime;
  1740. /* Set up the assertion */
  1741. if (rmw) {
  1742. /*
  1743. * Set up the assertion. If we don't have a version
  1744. * number, then the object doesn't exist yet. Use an
  1745. * exclusive create instead of a version assertion in
  1746. * that case.
  1747. */
  1748. if (assert_ver) {
  1749. osd_req_op_init(req, 0, CEPH_OSD_OP_ASSERT_VER, 0);
  1750. req->r_ops[0].assert_ver.ver = assert_ver;
  1751. } else {
  1752. osd_req_op_init(req, 0, CEPH_OSD_OP_CREATE,
  1753. CEPH_OSD_OP_FLAG_EXCL);
  1754. }
  1755. }
  1756. ceph_osdc_start_request(osdc, req);
  1757. ret = ceph_osdc_wait_request(osdc, req);
  1758. ceph_update_write_metrics(&fsc->mdsc->metric, req->r_start_latency,
  1759. req->r_end_latency, len, ret);
  1760. ceph_osdc_put_request(req);
  1761. if (ret != 0) {
  1762. doutc(cl, "osd write returned %d\n", ret);
  1763. /* Version changed! Must re-do the rmw cycle */
  1764. if ((assert_ver && (ret == -ERANGE || ret == -EOVERFLOW)) ||
  1765. (!assert_ver && ret == -EEXIST)) {
  1766. /* We should only ever see this on a rmw */
  1767. WARN_ON_ONCE(!rmw);
  1768. /* The version should never go backward */
  1769. WARN_ON_ONCE(ret == -EOVERFLOW);
  1770. *from = saved_iter;
  1771. /* FIXME: limit number of times we loop? */
  1772. continue;
  1773. }
  1774. ceph_set_error_write(ci);
  1775. break;
  1776. }
  1777. ceph_clear_error_write(ci);
  1778. /*
  1779. * We successfully wrote to a range of the file. Declare
  1780. * that region of the pagecache invalid.
  1781. */
  1782. ret = invalidate_inode_pages2_range(
  1783. inode->i_mapping,
  1784. pos >> PAGE_SHIFT,
  1785. (pos + len - 1) >> PAGE_SHIFT);
  1786. if (ret < 0) {
  1787. doutc(cl, "invalidate_inode_pages2_range returned %d\n",
  1788. ret);
  1789. ret = 0;
  1790. }
  1791. pos += len;
  1792. written += len;
  1793. doutc(cl, "written %d\n", written);
  1794. if (pos > i_size_read(inode)) {
  1795. check_caps = ceph_inode_set_size(inode, pos);
  1796. if (check_caps)
  1797. ceph_check_caps(ceph_inode(inode),
  1798. CHECK_CAPS_AUTHONLY);
  1799. }
  1800. }
  1801. if (ret != -EOLDSNAPC && written > 0) {
  1802. ret = written;
  1803. iocb->ki_pos = pos;
  1804. }
  1805. doutc(cl, "returning %d\n", ret);
  1806. return ret;
  1807. }
  1808. /*
  1809. * Wrap generic_file_aio_read with checks for cap bits on the inode.
  1810. * Atomically grab references, so that those bits are not released
  1811. * back to the MDS mid-read.
  1812. *
  1813. * Hmm, the sync read case isn't actually async... should it be?
  1814. */
  1815. static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1816. {
  1817. struct file *filp = iocb->ki_filp;
  1818. struct ceph_file_info *fi = filp->private_data;
  1819. size_t len = iov_iter_count(to);
  1820. struct inode *inode = file_inode(filp);
  1821. struct ceph_inode_info *ci = ceph_inode(inode);
  1822. bool direct_lock = iocb->ki_flags & IOCB_DIRECT;
  1823. struct ceph_client *cl = ceph_inode_to_client(inode);
  1824. ssize_t ret;
  1825. int want = 0, got = 0;
  1826. int retry_op = 0, read = 0;
  1827. again:
  1828. doutc(cl, "%llu~%u trying to get caps on %p %llx.%llx\n",
  1829. iocb->ki_pos, (unsigned)len, inode, ceph_vinop(inode));
  1830. if (ceph_inode_is_shutdown(inode))
  1831. return -ESTALE;
  1832. ret = direct_lock ? ceph_start_io_direct(inode) :
  1833. ceph_start_io_read(inode);
  1834. if (ret)
  1835. return ret;
  1836. if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
  1837. want |= CEPH_CAP_FILE_CACHE;
  1838. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1839. want |= CEPH_CAP_FILE_LAZYIO;
  1840. ret = ceph_get_caps(filp, CEPH_CAP_FILE_RD, want, -1, &got);
  1841. if (ret < 0) {
  1842. if (direct_lock)
  1843. ceph_end_io_direct(inode);
  1844. else
  1845. ceph_end_io_read(inode);
  1846. return ret;
  1847. }
  1848. if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  1849. (iocb->ki_flags & IOCB_DIRECT) ||
  1850. (fi->flags & CEPH_F_SYNC)) {
  1851. doutc(cl, "sync %p %llx.%llx %llu~%u got cap refs on %s\n",
  1852. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1853. ceph_cap_string(got));
  1854. if (!ceph_has_inline_data(ci)) {
  1855. if (!retry_op &&
  1856. (iocb->ki_flags & IOCB_DIRECT) &&
  1857. !IS_ENCRYPTED(inode)) {
  1858. ret = ceph_direct_read_write(iocb, to,
  1859. NULL, NULL);
  1860. if (ret >= 0 && ret < len)
  1861. retry_op = CHECK_EOF;
  1862. } else {
  1863. ret = ceph_sync_read(iocb, to, &retry_op);
  1864. }
  1865. } else {
  1866. retry_op = READ_INLINE;
  1867. }
  1868. } else {
  1869. CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
  1870. doutc(cl, "async %p %llx.%llx %llu~%u got cap refs on %s\n",
  1871. inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len,
  1872. ceph_cap_string(got));
  1873. ceph_add_rw_context(fi, &rw_ctx);
  1874. ret = generic_file_read_iter(iocb, to);
  1875. ceph_del_rw_context(fi, &rw_ctx);
  1876. }
  1877. doutc(cl, "%p %llx.%llx dropping cap refs on %s = %d\n",
  1878. inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
  1879. ceph_put_cap_refs(ci, got);
  1880. if (direct_lock)
  1881. ceph_end_io_direct(inode);
  1882. else
  1883. ceph_end_io_read(inode);
  1884. if (retry_op > HAVE_RETRIED && ret >= 0) {
  1885. int statret;
  1886. struct page *page = NULL;
  1887. loff_t i_size;
  1888. int mask = CEPH_STAT_CAP_SIZE;
  1889. if (retry_op == READ_INLINE) {
  1890. page = __page_cache_alloc(GFP_KERNEL);
  1891. if (!page)
  1892. return -ENOMEM;
  1893. mask = CEPH_STAT_CAP_INLINE_DATA;
  1894. }
  1895. statret = __ceph_do_getattr(inode, page, mask, !!page);
  1896. if (statret < 0) {
  1897. if (page)
  1898. __free_page(page);
  1899. if (statret == -ENODATA) {
  1900. BUG_ON(retry_op != READ_INLINE);
  1901. goto again;
  1902. }
  1903. return statret;
  1904. }
  1905. i_size = i_size_read(inode);
  1906. if (retry_op == READ_INLINE) {
  1907. BUG_ON(ret > 0 || read > 0);
  1908. if (iocb->ki_pos < i_size &&
  1909. iocb->ki_pos < PAGE_SIZE) {
  1910. loff_t end = min_t(loff_t, i_size,
  1911. iocb->ki_pos + len);
  1912. end = min_t(loff_t, end, PAGE_SIZE);
  1913. if (statret < end)
  1914. zero_user_segment(page, statret, end);
  1915. ret = copy_page_to_iter(page,
  1916. iocb->ki_pos & ~PAGE_MASK,
  1917. end - iocb->ki_pos, to);
  1918. iocb->ki_pos += ret;
  1919. read += ret;
  1920. }
  1921. if (iocb->ki_pos < i_size && read < len) {
  1922. size_t zlen = min_t(size_t, len - read,
  1923. i_size - iocb->ki_pos);
  1924. ret = iov_iter_zero(zlen, to);
  1925. iocb->ki_pos += ret;
  1926. read += ret;
  1927. }
  1928. __free_pages(page, 0);
  1929. return read;
  1930. }
  1931. /* hit EOF or hole? */
  1932. if (retry_op == CHECK_EOF && iocb->ki_pos < i_size &&
  1933. ret < len) {
  1934. doutc(cl, "may hit hole, ppos %lld < size %lld, reading more\n",
  1935. iocb->ki_pos, i_size);
  1936. read += ret;
  1937. len -= ret;
  1938. retry_op = HAVE_RETRIED;
  1939. goto again;
  1940. }
  1941. }
  1942. if (ret >= 0)
  1943. ret += read;
  1944. return ret;
  1945. }
  1946. /*
  1947. * Wrap filemap_splice_read with checks for cap bits on the inode.
  1948. * Atomically grab references, so that those bits are not released
  1949. * back to the MDS mid-read.
  1950. */
  1951. static ssize_t ceph_splice_read(struct file *in, loff_t *ppos,
  1952. struct pipe_inode_info *pipe,
  1953. size_t len, unsigned int flags)
  1954. {
  1955. struct ceph_file_info *fi = in->private_data;
  1956. struct inode *inode = file_inode(in);
  1957. struct ceph_inode_info *ci = ceph_inode(inode);
  1958. ssize_t ret;
  1959. int want = 0, got = 0;
  1960. CEPH_DEFINE_RW_CONTEXT(rw_ctx, 0);
  1961. dout("splice_read %p %llx.%llx %llu~%zu trying to get caps on %p\n",
  1962. inode, ceph_vinop(inode), *ppos, len, inode);
  1963. if (ceph_inode_is_shutdown(inode))
  1964. return -ESTALE;
  1965. if (ceph_has_inline_data(ci) ||
  1966. (fi->flags & CEPH_F_SYNC))
  1967. return copy_splice_read(in, ppos, pipe, len, flags);
  1968. ret = ceph_start_io_read(inode);
  1969. if (ret)
  1970. return ret;
  1971. want = CEPH_CAP_FILE_CACHE;
  1972. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  1973. want |= CEPH_CAP_FILE_LAZYIO;
  1974. ret = ceph_get_caps(in, CEPH_CAP_FILE_RD, want, -1, &got);
  1975. if (ret < 0)
  1976. goto out_end;
  1977. if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) == 0) {
  1978. dout("splice_read/sync %p %llx.%llx %llu~%zu got cap refs on %s\n",
  1979. inode, ceph_vinop(inode), *ppos, len,
  1980. ceph_cap_string(got));
  1981. ceph_put_cap_refs(ci, got);
  1982. ceph_end_io_read(inode);
  1983. return copy_splice_read(in, ppos, pipe, len, flags);
  1984. }
  1985. dout("splice_read %p %llx.%llx %llu~%zu got cap refs on %s\n",
  1986. inode, ceph_vinop(inode), *ppos, len, ceph_cap_string(got));
  1987. rw_ctx.caps = got;
  1988. ceph_add_rw_context(fi, &rw_ctx);
  1989. ret = filemap_splice_read(in, ppos, pipe, len, flags);
  1990. ceph_del_rw_context(fi, &rw_ctx);
  1991. dout("splice_read %p %llx.%llx dropping cap refs on %s = %zd\n",
  1992. inode, ceph_vinop(inode), ceph_cap_string(got), ret);
  1993. ceph_put_cap_refs(ci, got);
  1994. out_end:
  1995. ceph_end_io_read(inode);
  1996. return ret;
  1997. }
  1998. /*
  1999. * Take cap references to avoid releasing caps to MDS mid-write.
  2000. *
  2001. * If we are synchronous, and write with an old snap context, the OSD
  2002. * may return EOLDSNAPC. In that case, retry the write.. _after_
  2003. * dropping our cap refs and allowing the pending snap to logically
  2004. * complete _before_ this write occurs.
  2005. *
  2006. * If we are near ENOSPC, write synchronously.
  2007. */
  2008. static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)
  2009. {
  2010. struct file *file = iocb->ki_filp;
  2011. struct ceph_file_info *fi = file->private_data;
  2012. struct inode *inode = file_inode(file);
  2013. struct ceph_inode_info *ci = ceph_inode(inode);
  2014. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  2015. struct ceph_client *cl = fsc->client;
  2016. struct ceph_osd_client *osdc = &fsc->client->osdc;
  2017. struct ceph_cap_flush *prealloc_cf;
  2018. ssize_t count, written = 0;
  2019. int err, want = 0, got;
  2020. bool direct_lock = false;
  2021. u32 map_flags;
  2022. u64 pool_flags;
  2023. loff_t pos;
  2024. loff_t limit = max(i_size_read(inode), fsc->max_file_size);
  2025. if (ceph_inode_is_shutdown(inode))
  2026. return -ESTALE;
  2027. if (ceph_snap(inode) != CEPH_NOSNAP)
  2028. return -EROFS;
  2029. prealloc_cf = ceph_alloc_cap_flush();
  2030. if (!prealloc_cf)
  2031. return -ENOMEM;
  2032. if ((iocb->ki_flags & (IOCB_DIRECT | IOCB_APPEND)) == IOCB_DIRECT)
  2033. direct_lock = true;
  2034. retry_snap:
  2035. err = direct_lock ? ceph_start_io_direct(inode) :
  2036. ceph_start_io_write(inode);
  2037. if (err)
  2038. goto out_unlocked;
  2039. if (iocb->ki_flags & IOCB_APPEND) {
  2040. err = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  2041. if (err < 0)
  2042. goto out;
  2043. }
  2044. err = generic_write_checks(iocb, from);
  2045. if (err <= 0)
  2046. goto out;
  2047. pos = iocb->ki_pos;
  2048. if (unlikely(pos >= limit)) {
  2049. err = -EFBIG;
  2050. goto out;
  2051. } else {
  2052. iov_iter_truncate(from, limit - pos);
  2053. }
  2054. count = iov_iter_count(from);
  2055. if (ceph_quota_is_max_bytes_exceeded(inode, pos + count)) {
  2056. err = -EDQUOT;
  2057. goto out;
  2058. }
  2059. down_read(&osdc->lock);
  2060. map_flags = osdc->osdmap->flags;
  2061. pool_flags = ceph_pg_pool_flags(osdc->osdmap, ci->i_layout.pool_id);
  2062. up_read(&osdc->lock);
  2063. if ((map_flags & CEPH_OSDMAP_FULL) ||
  2064. (pool_flags & CEPH_POOL_FLAG_FULL)) {
  2065. err = -ENOSPC;
  2066. goto out;
  2067. }
  2068. err = file_remove_privs(file);
  2069. if (err)
  2070. goto out;
  2071. doutc(cl, "%p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
  2072. inode, ceph_vinop(inode), pos, count,
  2073. i_size_read(inode));
  2074. if (!(fi->flags & CEPH_F_SYNC) && !direct_lock)
  2075. want |= CEPH_CAP_FILE_BUFFER;
  2076. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  2077. want |= CEPH_CAP_FILE_LAZYIO;
  2078. got = 0;
  2079. err = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, pos + count, &got);
  2080. if (err < 0)
  2081. goto out;
  2082. err = file_update_time(file);
  2083. if (err)
  2084. goto out_caps;
  2085. inode_inc_iversion_raw(inode);
  2086. doutc(cl, "%p %llx.%llx %llu~%zd got cap refs on %s\n",
  2087. inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
  2088. if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
  2089. (iocb->ki_flags & IOCB_DIRECT) || (fi->flags & CEPH_F_SYNC) ||
  2090. (ci->i_ceph_flags & CEPH_I_ERROR_WRITE)) {
  2091. struct ceph_snap_context *snapc;
  2092. struct iov_iter data;
  2093. spin_lock(&ci->i_ceph_lock);
  2094. if (__ceph_have_pending_cap_snap(ci)) {
  2095. struct ceph_cap_snap *capsnap =
  2096. list_last_entry(&ci->i_cap_snaps,
  2097. struct ceph_cap_snap,
  2098. ci_item);
  2099. snapc = ceph_get_snap_context(capsnap->context);
  2100. } else {
  2101. BUG_ON(!ci->i_head_snapc);
  2102. snapc = ceph_get_snap_context(ci->i_head_snapc);
  2103. }
  2104. spin_unlock(&ci->i_ceph_lock);
  2105. /* we might need to revert back to that point */
  2106. data = *from;
  2107. if ((iocb->ki_flags & IOCB_DIRECT) && !IS_ENCRYPTED(inode))
  2108. written = ceph_direct_read_write(iocb, &data, snapc,
  2109. &prealloc_cf);
  2110. else
  2111. written = ceph_sync_write(iocb, &data, pos, snapc);
  2112. if (direct_lock)
  2113. ceph_end_io_direct(inode);
  2114. else
  2115. ceph_end_io_write(inode);
  2116. if (written > 0)
  2117. iov_iter_advance(from, written);
  2118. ceph_put_snap_context(snapc);
  2119. } else {
  2120. /*
  2121. * No need to acquire the i_truncate_mutex. Because
  2122. * the MDS revokes Fwb caps before sending truncate
  2123. * message to us. We can't get Fwb cap while there
  2124. * are pending vmtruncate. So write and vmtruncate
  2125. * can not run at the same time
  2126. */
  2127. written = generic_perform_write(iocb, from);
  2128. ceph_end_io_write(inode);
  2129. }
  2130. if (written >= 0) {
  2131. int dirty;
  2132. spin_lock(&ci->i_ceph_lock);
  2133. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  2134. &prealloc_cf);
  2135. spin_unlock(&ci->i_ceph_lock);
  2136. if (dirty)
  2137. __mark_inode_dirty(inode, dirty);
  2138. if (ceph_quota_is_max_bytes_approaching(inode, iocb->ki_pos))
  2139. ceph_check_caps(ci, CHECK_CAPS_FLUSH);
  2140. }
  2141. doutc(cl, "%p %llx.%llx %llu~%u dropping cap refs on %s\n",
  2142. inode, ceph_vinop(inode), pos, (unsigned)count,
  2143. ceph_cap_string(got));
  2144. ceph_put_cap_refs(ci, got);
  2145. if (written == -EOLDSNAPC) {
  2146. doutc(cl, "%p %llx.%llx %llu~%u" "got EOLDSNAPC, retrying\n",
  2147. inode, ceph_vinop(inode), pos, (unsigned)count);
  2148. goto retry_snap;
  2149. }
  2150. if (written >= 0) {
  2151. if ((map_flags & CEPH_OSDMAP_NEARFULL) ||
  2152. (pool_flags & CEPH_POOL_FLAG_NEARFULL))
  2153. iocb->ki_flags |= IOCB_DSYNC;
  2154. written = generic_write_sync(iocb, written);
  2155. }
  2156. goto out_unlocked;
  2157. out_caps:
  2158. ceph_put_cap_refs(ci, got);
  2159. out:
  2160. if (direct_lock)
  2161. ceph_end_io_direct(inode);
  2162. else
  2163. ceph_end_io_write(inode);
  2164. out_unlocked:
  2165. ceph_free_cap_flush(prealloc_cf);
  2166. return written ? written : err;
  2167. }
  2168. /*
  2169. * llseek. be sure to verify file size on SEEK_END.
  2170. */
  2171. static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
  2172. {
  2173. if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
  2174. struct inode *inode = file_inode(file);
  2175. int ret;
  2176. ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false);
  2177. if (ret < 0)
  2178. return ret;
  2179. }
  2180. return generic_file_llseek(file, offset, whence);
  2181. }
  2182. static inline void ceph_zero_partial_page(struct inode *inode,
  2183. loff_t offset, size_t size)
  2184. {
  2185. struct folio *folio;
  2186. folio = filemap_lock_folio(inode->i_mapping, offset >> PAGE_SHIFT);
  2187. if (IS_ERR(folio))
  2188. return;
  2189. folio_wait_writeback(folio);
  2190. folio_zero_range(folio, offset_in_folio(folio, offset), size);
  2191. folio_unlock(folio);
  2192. folio_put(folio);
  2193. }
  2194. static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
  2195. loff_t length)
  2196. {
  2197. loff_t nearly = round_up(offset, PAGE_SIZE);
  2198. if (offset < nearly) {
  2199. loff_t size = nearly - offset;
  2200. if (length < size)
  2201. size = length;
  2202. ceph_zero_partial_page(inode, offset, size);
  2203. offset += size;
  2204. length -= size;
  2205. }
  2206. if (length >= PAGE_SIZE) {
  2207. loff_t size = round_down(length, PAGE_SIZE);
  2208. truncate_pagecache_range(inode, offset, offset + size - 1);
  2209. offset += size;
  2210. length -= size;
  2211. }
  2212. if (length)
  2213. ceph_zero_partial_page(inode, offset, length);
  2214. }
  2215. static int ceph_zero_partial_object(struct inode *inode,
  2216. loff_t offset, loff_t *length)
  2217. {
  2218. struct ceph_inode_info *ci = ceph_inode(inode);
  2219. struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
  2220. struct ceph_osd_request *req;
  2221. struct ceph_snap_context *snapc;
  2222. int ret = 0;
  2223. loff_t zero = 0;
  2224. int op;
  2225. if (ceph_inode_is_shutdown(inode))
  2226. return -EIO;
  2227. if (!length) {
  2228. op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
  2229. length = &zero;
  2230. } else {
  2231. op = CEPH_OSD_OP_ZERO;
  2232. }
  2233. spin_lock(&ci->i_ceph_lock);
  2234. if (__ceph_have_pending_cap_snap(ci)) {
  2235. struct ceph_cap_snap *capsnap =
  2236. list_last_entry(&ci->i_cap_snaps,
  2237. struct ceph_cap_snap,
  2238. ci_item);
  2239. snapc = ceph_get_snap_context(capsnap->context);
  2240. } else {
  2241. BUG_ON(!ci->i_head_snapc);
  2242. snapc = ceph_get_snap_context(ci->i_head_snapc);
  2243. }
  2244. spin_unlock(&ci->i_ceph_lock);
  2245. req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
  2246. ceph_vino(inode),
  2247. offset, length,
  2248. 0, 1, op,
  2249. CEPH_OSD_FLAG_WRITE,
  2250. snapc, 0, 0, false);
  2251. if (IS_ERR(req)) {
  2252. ret = PTR_ERR(req);
  2253. goto out;
  2254. }
  2255. req->r_mtime = inode_get_mtime(inode);
  2256. ceph_osdc_start_request(&fsc->client->osdc, req);
  2257. ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
  2258. if (ret == -ENOENT)
  2259. ret = 0;
  2260. ceph_osdc_put_request(req);
  2261. out:
  2262. ceph_put_snap_context(snapc);
  2263. return ret;
  2264. }
  2265. static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
  2266. {
  2267. int ret = 0;
  2268. struct ceph_inode_info *ci = ceph_inode(inode);
  2269. s32 stripe_unit = ci->i_layout.stripe_unit;
  2270. s32 stripe_count = ci->i_layout.stripe_count;
  2271. s32 object_size = ci->i_layout.object_size;
  2272. u64 object_set_size = (u64) object_size * stripe_count;
  2273. u64 nearly, t;
  2274. /* round offset up to next period boundary */
  2275. nearly = offset + object_set_size - 1;
  2276. t = nearly;
  2277. nearly -= do_div(t, object_set_size);
  2278. while (length && offset < nearly) {
  2279. loff_t size = length;
  2280. ret = ceph_zero_partial_object(inode, offset, &size);
  2281. if (ret < 0)
  2282. return ret;
  2283. offset += size;
  2284. length -= size;
  2285. }
  2286. while (length >= object_set_size) {
  2287. int i;
  2288. loff_t pos = offset;
  2289. for (i = 0; i < stripe_count; ++i) {
  2290. ret = ceph_zero_partial_object(inode, pos, NULL);
  2291. if (ret < 0)
  2292. return ret;
  2293. pos += stripe_unit;
  2294. }
  2295. offset += object_set_size;
  2296. length -= object_set_size;
  2297. }
  2298. while (length) {
  2299. loff_t size = length;
  2300. ret = ceph_zero_partial_object(inode, offset, &size);
  2301. if (ret < 0)
  2302. return ret;
  2303. offset += size;
  2304. length -= size;
  2305. }
  2306. return ret;
  2307. }
  2308. static long ceph_fallocate(struct file *file, int mode,
  2309. loff_t offset, loff_t length)
  2310. {
  2311. struct ceph_file_info *fi = file->private_data;
  2312. struct inode *inode = file_inode(file);
  2313. struct ceph_inode_info *ci = ceph_inode(inode);
  2314. struct ceph_cap_flush *prealloc_cf;
  2315. struct ceph_client *cl = ceph_inode_to_client(inode);
  2316. int want, got = 0;
  2317. int dirty;
  2318. int ret = 0;
  2319. loff_t endoff = 0;
  2320. loff_t size;
  2321. doutc(cl, "%p %llx.%llx mode %x, offset %llu length %llu\n",
  2322. inode, ceph_vinop(inode), mode, offset, length);
  2323. if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  2324. return -EOPNOTSUPP;
  2325. if (!S_ISREG(inode->i_mode))
  2326. return -EOPNOTSUPP;
  2327. if (IS_ENCRYPTED(inode))
  2328. return -EOPNOTSUPP;
  2329. prealloc_cf = ceph_alloc_cap_flush();
  2330. if (!prealloc_cf)
  2331. return -ENOMEM;
  2332. inode_lock(inode);
  2333. if (ceph_snap(inode) != CEPH_NOSNAP) {
  2334. ret = -EROFS;
  2335. goto unlock;
  2336. }
  2337. size = i_size_read(inode);
  2338. /* Are we punching a hole beyond EOF? */
  2339. if (offset >= size)
  2340. goto unlock;
  2341. if ((offset + length) > size)
  2342. length = size - offset;
  2343. if (fi->fmode & CEPH_FILE_MODE_LAZY)
  2344. want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
  2345. else
  2346. want = CEPH_CAP_FILE_BUFFER;
  2347. ret = ceph_get_caps(file, CEPH_CAP_FILE_WR, want, endoff, &got);
  2348. if (ret < 0)
  2349. goto unlock;
  2350. ret = file_modified(file);
  2351. if (ret)
  2352. goto put_caps;
  2353. filemap_invalidate_lock(inode->i_mapping);
  2354. ceph_fscache_invalidate(inode, false);
  2355. ceph_zero_pagecache_range(inode, offset, length);
  2356. ret = ceph_zero_objects(inode, offset, length);
  2357. if (!ret) {
  2358. spin_lock(&ci->i_ceph_lock);
  2359. dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
  2360. &prealloc_cf);
  2361. spin_unlock(&ci->i_ceph_lock);
  2362. if (dirty)
  2363. __mark_inode_dirty(inode, dirty);
  2364. }
  2365. filemap_invalidate_unlock(inode->i_mapping);
  2366. put_caps:
  2367. ceph_put_cap_refs(ci, got);
  2368. unlock:
  2369. inode_unlock(inode);
  2370. ceph_free_cap_flush(prealloc_cf);
  2371. return ret;
  2372. }
  2373. /*
  2374. * This function tries to get FILE_WR capabilities for dst_ci and FILE_RD for
  2375. * src_ci. Two attempts are made to obtain both caps, and an error is return if
  2376. * this fails; zero is returned on success.
  2377. */
  2378. static int get_rd_wr_caps(struct file *src_filp, int *src_got,
  2379. struct file *dst_filp,
  2380. loff_t dst_endoff, int *dst_got)
  2381. {
  2382. int ret = 0;
  2383. bool retrying = false;
  2384. retry_caps:
  2385. ret = ceph_get_caps(dst_filp, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER,
  2386. dst_endoff, dst_got);
  2387. if (ret < 0)
  2388. return ret;
  2389. /*
  2390. * Since we're already holding the FILE_WR capability for the dst file,
  2391. * we would risk a deadlock by using ceph_get_caps. Thus, we'll do some
  2392. * retry dance instead to try to get both capabilities.
  2393. */
  2394. ret = ceph_try_get_caps(file_inode(src_filp),
  2395. CEPH_CAP_FILE_RD, CEPH_CAP_FILE_SHARED,
  2396. false, src_got);
  2397. if (ret <= 0) {
  2398. /* Start by dropping dst_ci caps and getting src_ci caps */
  2399. ceph_put_cap_refs(ceph_inode(file_inode(dst_filp)), *dst_got);
  2400. if (retrying) {
  2401. if (!ret)
  2402. /* ceph_try_get_caps masks EAGAIN */
  2403. ret = -EAGAIN;
  2404. return ret;
  2405. }
  2406. ret = ceph_get_caps(src_filp, CEPH_CAP_FILE_RD,
  2407. CEPH_CAP_FILE_SHARED, -1, src_got);
  2408. if (ret < 0)
  2409. return ret;
  2410. /*... drop src_ci caps too, and retry */
  2411. ceph_put_cap_refs(ceph_inode(file_inode(src_filp)), *src_got);
  2412. retrying = true;
  2413. goto retry_caps;
  2414. }
  2415. return ret;
  2416. }
  2417. static void put_rd_wr_caps(struct ceph_inode_info *src_ci, int src_got,
  2418. struct ceph_inode_info *dst_ci, int dst_got)
  2419. {
  2420. ceph_put_cap_refs(src_ci, src_got);
  2421. ceph_put_cap_refs(dst_ci, dst_got);
  2422. }
  2423. /*
  2424. * This function does several size-related checks, returning an error if:
  2425. * - source file is smaller than off+len
  2426. * - destination file size is not OK (inode_newsize_ok())
  2427. * - max bytes quotas is exceeded
  2428. */
  2429. static int is_file_size_ok(struct inode *src_inode, struct inode *dst_inode,
  2430. loff_t src_off, loff_t dst_off, size_t len)
  2431. {
  2432. struct ceph_client *cl = ceph_inode_to_client(src_inode);
  2433. loff_t size, endoff;
  2434. size = i_size_read(src_inode);
  2435. /*
  2436. * Don't copy beyond source file EOF. Instead of simply setting length
  2437. * to (size - src_off), just drop to VFS default implementation, as the
  2438. * local i_size may be stale due to other clients writing to the source
  2439. * inode.
  2440. */
  2441. if (src_off + len > size) {
  2442. doutc(cl, "Copy beyond EOF (%llu + %zu > %llu)\n", src_off,
  2443. len, size);
  2444. return -EOPNOTSUPP;
  2445. }
  2446. size = i_size_read(dst_inode);
  2447. endoff = dst_off + len;
  2448. if (inode_newsize_ok(dst_inode, endoff))
  2449. return -EOPNOTSUPP;
  2450. if (ceph_quota_is_max_bytes_exceeded(dst_inode, endoff))
  2451. return -EDQUOT;
  2452. return 0;
  2453. }
  2454. static struct ceph_osd_request *
  2455. ceph_alloc_copyfrom_request(struct ceph_osd_client *osdc,
  2456. u64 src_snapid,
  2457. struct ceph_object_id *src_oid,
  2458. struct ceph_object_locator *src_oloc,
  2459. struct ceph_object_id *dst_oid,
  2460. struct ceph_object_locator *dst_oloc,
  2461. u32 truncate_seq, u64 truncate_size)
  2462. {
  2463. struct ceph_osd_request *req;
  2464. int ret;
  2465. u32 src_fadvise_flags =
  2466. CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
  2467. CEPH_OSD_OP_FLAG_FADVISE_NOCACHE;
  2468. u32 dst_fadvise_flags =
  2469. CEPH_OSD_OP_FLAG_FADVISE_SEQUENTIAL |
  2470. CEPH_OSD_OP_FLAG_FADVISE_DONTNEED;
  2471. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
  2472. if (!req)
  2473. return ERR_PTR(-ENOMEM);
  2474. req->r_flags = CEPH_OSD_FLAG_WRITE;
  2475. ceph_oloc_copy(&req->r_t.base_oloc, dst_oloc);
  2476. ceph_oid_copy(&req->r_t.base_oid, dst_oid);
  2477. ret = osd_req_op_copy_from_init(req, src_snapid, 0,
  2478. src_oid, src_oloc,
  2479. src_fadvise_flags,
  2480. dst_fadvise_flags,
  2481. truncate_seq,
  2482. truncate_size,
  2483. CEPH_OSD_COPY_FROM_FLAG_TRUNCATE_SEQ);
  2484. if (ret)
  2485. goto out;
  2486. ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
  2487. if (ret)
  2488. goto out;
  2489. return req;
  2490. out:
  2491. ceph_osdc_put_request(req);
  2492. return ERR_PTR(ret);
  2493. }
  2494. static ssize_t ceph_do_objects_copy(struct ceph_inode_info *src_ci, u64 *src_off,
  2495. struct ceph_inode_info *dst_ci, u64 *dst_off,
  2496. struct ceph_fs_client *fsc,
  2497. size_t len, unsigned int flags)
  2498. {
  2499. struct ceph_object_locator src_oloc, dst_oloc;
  2500. struct ceph_object_id src_oid, dst_oid;
  2501. struct ceph_osd_client *osdc;
  2502. struct ceph_osd_request *req;
  2503. ssize_t bytes = 0;
  2504. u64 src_objnum, src_objoff, dst_objnum, dst_objoff;
  2505. u32 src_objlen, dst_objlen;
  2506. u32 object_size = src_ci->i_layout.object_size;
  2507. struct ceph_client *cl = fsc->client;
  2508. int ret;
  2509. src_oloc.pool = src_ci->i_layout.pool_id;
  2510. src_oloc.pool_ns = ceph_try_get_string(src_ci->i_layout.pool_ns);
  2511. dst_oloc.pool = dst_ci->i_layout.pool_id;
  2512. dst_oloc.pool_ns = ceph_try_get_string(dst_ci->i_layout.pool_ns);
  2513. osdc = &fsc->client->osdc;
  2514. while (len >= object_size) {
  2515. ceph_calc_file_object_mapping(&src_ci->i_layout, *src_off,
  2516. object_size, &src_objnum,
  2517. &src_objoff, &src_objlen);
  2518. ceph_calc_file_object_mapping(&dst_ci->i_layout, *dst_off,
  2519. object_size, &dst_objnum,
  2520. &dst_objoff, &dst_objlen);
  2521. ceph_oid_init(&src_oid);
  2522. ceph_oid_printf(&src_oid, "%llx.%08llx",
  2523. src_ci->i_vino.ino, src_objnum);
  2524. ceph_oid_init(&dst_oid);
  2525. ceph_oid_printf(&dst_oid, "%llx.%08llx",
  2526. dst_ci->i_vino.ino, dst_objnum);
  2527. /* Do an object remote copy */
  2528. req = ceph_alloc_copyfrom_request(osdc, src_ci->i_vino.snap,
  2529. &src_oid, &src_oloc,
  2530. &dst_oid, &dst_oloc,
  2531. dst_ci->i_truncate_seq,
  2532. dst_ci->i_truncate_size);
  2533. if (IS_ERR(req))
  2534. ret = PTR_ERR(req);
  2535. else {
  2536. ceph_osdc_start_request(osdc, req);
  2537. ret = ceph_osdc_wait_request(osdc, req);
  2538. ceph_update_copyfrom_metrics(&fsc->mdsc->metric,
  2539. req->r_start_latency,
  2540. req->r_end_latency,
  2541. object_size, ret);
  2542. ceph_osdc_put_request(req);
  2543. }
  2544. if (ret) {
  2545. if (ret == -EOPNOTSUPP) {
  2546. fsc->have_copy_from2 = false;
  2547. pr_notice_client(cl,
  2548. "OSDs don't support copy-from2; disabling copy offload\n");
  2549. }
  2550. doutc(cl, "returned %d\n", ret);
  2551. if (bytes <= 0)
  2552. bytes = ret;
  2553. goto out;
  2554. }
  2555. len -= object_size;
  2556. bytes += object_size;
  2557. *src_off += object_size;
  2558. *dst_off += object_size;
  2559. }
  2560. out:
  2561. ceph_oloc_destroy(&src_oloc);
  2562. ceph_oloc_destroy(&dst_oloc);
  2563. return bytes;
  2564. }
  2565. static ssize_t __ceph_copy_file_range(struct file *src_file, loff_t src_off,
  2566. struct file *dst_file, loff_t dst_off,
  2567. size_t len, unsigned int flags)
  2568. {
  2569. struct inode *src_inode = file_inode(src_file);
  2570. struct inode *dst_inode = file_inode(dst_file);
  2571. struct ceph_inode_info *src_ci = ceph_inode(src_inode);
  2572. struct ceph_inode_info *dst_ci = ceph_inode(dst_inode);
  2573. struct ceph_cap_flush *prealloc_cf;
  2574. struct ceph_fs_client *src_fsc = ceph_inode_to_fs_client(src_inode);
  2575. struct ceph_client *cl = src_fsc->client;
  2576. loff_t size;
  2577. ssize_t ret = -EIO, bytes;
  2578. u64 src_objnum, dst_objnum, src_objoff, dst_objoff;
  2579. u32 src_objlen, dst_objlen;
  2580. int src_got = 0, dst_got = 0, err, dirty;
  2581. if (src_inode->i_sb != dst_inode->i_sb) {
  2582. struct ceph_fs_client *dst_fsc = ceph_inode_to_fs_client(dst_inode);
  2583. if (ceph_fsid_compare(&src_fsc->client->fsid,
  2584. &dst_fsc->client->fsid)) {
  2585. dout("Copying files across clusters: src: %pU dst: %pU\n",
  2586. &src_fsc->client->fsid, &dst_fsc->client->fsid);
  2587. return -EXDEV;
  2588. }
  2589. }
  2590. if (ceph_snap(dst_inode) != CEPH_NOSNAP)
  2591. return -EROFS;
  2592. /*
  2593. * Some of the checks below will return -EOPNOTSUPP, which will force a
  2594. * fallback to the default VFS copy_file_range implementation. This is
  2595. * desirable in several cases (for ex, the 'len' is smaller than the
  2596. * size of the objects, or in cases where that would be more
  2597. * efficient).
  2598. */
  2599. if (ceph_test_mount_opt(src_fsc, NOCOPYFROM))
  2600. return -EOPNOTSUPP;
  2601. if (!src_fsc->have_copy_from2)
  2602. return -EOPNOTSUPP;
  2603. /*
  2604. * Striped file layouts require that we copy partial objects, but the
  2605. * OSD copy-from operation only supports full-object copies. Limit
  2606. * this to non-striped file layouts for now.
  2607. */
  2608. if ((src_ci->i_layout.stripe_unit != dst_ci->i_layout.stripe_unit) ||
  2609. (src_ci->i_layout.stripe_count != 1) ||
  2610. (dst_ci->i_layout.stripe_count != 1) ||
  2611. (src_ci->i_layout.object_size != dst_ci->i_layout.object_size)) {
  2612. doutc(cl, "Invalid src/dst files layout\n");
  2613. return -EOPNOTSUPP;
  2614. }
  2615. /* Every encrypted inode gets its own key, so we can't offload them */
  2616. if (IS_ENCRYPTED(src_inode) || IS_ENCRYPTED(dst_inode))
  2617. return -EOPNOTSUPP;
  2618. if (len < src_ci->i_layout.object_size)
  2619. return -EOPNOTSUPP; /* no remote copy will be done */
  2620. prealloc_cf = ceph_alloc_cap_flush();
  2621. if (!prealloc_cf)
  2622. return -ENOMEM;
  2623. /* Start by sync'ing the source and destination files */
  2624. ret = file_write_and_wait_range(src_file, src_off, (src_off + len));
  2625. if (ret < 0) {
  2626. doutc(cl, "failed to write src file (%zd)\n", ret);
  2627. goto out;
  2628. }
  2629. ret = file_write_and_wait_range(dst_file, dst_off, (dst_off + len));
  2630. if (ret < 0) {
  2631. doutc(cl, "failed to write dst file (%zd)\n", ret);
  2632. goto out;
  2633. }
  2634. /*
  2635. * We need FILE_WR caps for dst_ci and FILE_RD for src_ci as other
  2636. * clients may have dirty data in their caches. And OSDs know nothing
  2637. * about caps, so they can't safely do the remote object copies.
  2638. */
  2639. err = get_rd_wr_caps(src_file, &src_got,
  2640. dst_file, (dst_off + len), &dst_got);
  2641. if (err < 0) {
  2642. doutc(cl, "get_rd_wr_caps returned %d\n", err);
  2643. ret = -EOPNOTSUPP;
  2644. goto out;
  2645. }
  2646. ret = is_file_size_ok(src_inode, dst_inode, src_off, dst_off, len);
  2647. if (ret < 0)
  2648. goto out_caps;
  2649. /* Drop dst file cached pages */
  2650. ceph_fscache_invalidate(dst_inode, false);
  2651. ret = invalidate_inode_pages2_range(dst_inode->i_mapping,
  2652. dst_off >> PAGE_SHIFT,
  2653. (dst_off + len) >> PAGE_SHIFT);
  2654. if (ret < 0) {
  2655. doutc(cl, "Failed to invalidate inode pages (%zd)\n",
  2656. ret);
  2657. ret = 0; /* XXX */
  2658. }
  2659. ceph_calc_file_object_mapping(&src_ci->i_layout, src_off,
  2660. src_ci->i_layout.object_size,
  2661. &src_objnum, &src_objoff, &src_objlen);
  2662. ceph_calc_file_object_mapping(&dst_ci->i_layout, dst_off,
  2663. dst_ci->i_layout.object_size,
  2664. &dst_objnum, &dst_objoff, &dst_objlen);
  2665. /* object-level offsets need to the same */
  2666. if (src_objoff != dst_objoff) {
  2667. ret = -EOPNOTSUPP;
  2668. goto out_caps;
  2669. }
  2670. /*
  2671. * Do a manual copy if the object offset isn't object aligned.
  2672. * 'src_objlen' contains the bytes left until the end of the object,
  2673. * starting at the src_off
  2674. */
  2675. if (src_objoff) {
  2676. doutc(cl, "Initial partial copy of %u bytes\n", src_objlen);
  2677. /*
  2678. * we need to temporarily drop all caps as we'll be calling
  2679. * {read,write}_iter, which will get caps again.
  2680. */
  2681. put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
  2682. ret = splice_file_range(src_file, &src_off, dst_file, &dst_off,
  2683. src_objlen);
  2684. /* Abort on short copies or on error */
  2685. if (ret < (long)src_objlen) {
  2686. doutc(cl, "Failed partial copy (%zd)\n", ret);
  2687. goto out;
  2688. }
  2689. len -= ret;
  2690. err = get_rd_wr_caps(src_file, &src_got,
  2691. dst_file, (dst_off + len), &dst_got);
  2692. if (err < 0)
  2693. goto out;
  2694. err = is_file_size_ok(src_inode, dst_inode,
  2695. src_off, dst_off, len);
  2696. if (err < 0)
  2697. goto out_caps;
  2698. }
  2699. size = i_size_read(dst_inode);
  2700. bytes = ceph_do_objects_copy(src_ci, &src_off, dst_ci, &dst_off,
  2701. src_fsc, len, flags);
  2702. if (bytes <= 0) {
  2703. if (!ret)
  2704. ret = bytes;
  2705. goto out_caps;
  2706. }
  2707. doutc(cl, "Copied %zu bytes out of %zu\n", bytes, len);
  2708. len -= bytes;
  2709. ret += bytes;
  2710. file_update_time(dst_file);
  2711. inode_inc_iversion_raw(dst_inode);
  2712. if (dst_off > size) {
  2713. /* Let the MDS know about dst file size change */
  2714. if (ceph_inode_set_size(dst_inode, dst_off) ||
  2715. ceph_quota_is_max_bytes_approaching(dst_inode, dst_off))
  2716. ceph_check_caps(dst_ci, CHECK_CAPS_AUTHONLY | CHECK_CAPS_FLUSH);
  2717. }
  2718. /* Mark Fw dirty */
  2719. spin_lock(&dst_ci->i_ceph_lock);
  2720. dirty = __ceph_mark_dirty_caps(dst_ci, CEPH_CAP_FILE_WR, &prealloc_cf);
  2721. spin_unlock(&dst_ci->i_ceph_lock);
  2722. if (dirty)
  2723. __mark_inode_dirty(dst_inode, dirty);
  2724. out_caps:
  2725. put_rd_wr_caps(src_ci, src_got, dst_ci, dst_got);
  2726. /*
  2727. * Do the final manual copy if we still have some bytes left, unless
  2728. * there were errors in remote object copies (len >= object_size).
  2729. */
  2730. if (len && (len < src_ci->i_layout.object_size)) {
  2731. doutc(cl, "Final partial copy of %zu bytes\n", len);
  2732. bytes = splice_file_range(src_file, &src_off, dst_file,
  2733. &dst_off, len);
  2734. if (bytes > 0)
  2735. ret += bytes;
  2736. else
  2737. doutc(cl, "Failed partial copy (%zd)\n", bytes);
  2738. }
  2739. out:
  2740. ceph_free_cap_flush(prealloc_cf);
  2741. return ret;
  2742. }
  2743. static ssize_t ceph_copy_file_range(struct file *src_file, loff_t src_off,
  2744. struct file *dst_file, loff_t dst_off,
  2745. size_t len, unsigned int flags)
  2746. {
  2747. ssize_t ret;
  2748. ret = __ceph_copy_file_range(src_file, src_off, dst_file, dst_off,
  2749. len, flags);
  2750. if (ret == -EOPNOTSUPP || ret == -EXDEV)
  2751. ret = splice_copy_file_range(src_file, src_off, dst_file,
  2752. dst_off, len);
  2753. return ret;
  2754. }
  2755. const struct file_operations ceph_file_fops = {
  2756. .open = ceph_open,
  2757. .release = ceph_release,
  2758. .llseek = ceph_llseek,
  2759. .read_iter = ceph_read_iter,
  2760. .write_iter = ceph_write_iter,
  2761. .mmap_prepare = ceph_mmap_prepare,
  2762. .fsync = ceph_fsync,
  2763. .lock = ceph_lock,
  2764. .flock = ceph_flock,
  2765. .splice_read = ceph_splice_read,
  2766. .splice_write = iter_file_splice_write,
  2767. .unlocked_ioctl = ceph_ioctl,
  2768. .compat_ioctl = compat_ptr_ioctl,
  2769. .fallocate = ceph_fallocate,
  2770. .copy_file_range = ceph_copy_file_range,
  2771. };