9p.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415
  1. /*
  2. * Virtio 9p backend
  3. *
  4. * Copyright IBM, Corp. 2010
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. /*
  14. * Not so fast! You might want to read the 9p developer docs first:
  15. * https://wiki.qemu.org/Documentation/9p
  16. */
  17. #include "qemu/osdep.h"
  18. #ifdef CONFIG_LINUX
  19. #include <linux/limits.h>
  20. #endif
  21. #include <glib/gprintf.h>
  22. #include "hw/virtio/virtio.h"
  23. #include "qapi/error.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/iov.h"
  26. #include "qemu/main-loop.h"
  27. #include "qemu/sockets.h"
  28. #include "virtio-9p.h"
  29. #include "fsdev/qemu-fsdev.h"
  30. #include "9p-xattr.h"
  31. #include "9p-util.h"
  32. #include "coth.h"
  33. #include "trace.h"
  34. #include "migration/blocker.h"
  35. #include "qemu/xxhash.h"
  36. #include <math.h>
  37. int open_fd_hw;
  38. int total_open_fd;
  39. static int open_fd_rc;
  40. enum {
  41. Oread = 0x00,
  42. Owrite = 0x01,
  43. Ordwr = 0x02,
  44. Oexec = 0x03,
  45. Oexcl = 0x04,
  46. Otrunc = 0x10,
  47. Orexec = 0x20,
  48. Orclose = 0x40,
  49. Oappend = 0x80,
  50. };
  51. P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free);
  52. static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
  53. {
  54. ssize_t ret;
  55. va_list ap;
  56. va_start(ap, fmt);
  57. ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
  58. va_end(ap);
  59. return ret;
  60. }
  61. static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
  62. {
  63. ssize_t ret;
  64. va_list ap;
  65. va_start(ap, fmt);
  66. ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
  67. va_end(ap);
  68. return ret;
  69. }
  70. static int omode_to_uflags(int8_t mode)
  71. {
  72. int ret = 0;
  73. switch (mode & 3) {
  74. case Oread:
  75. ret = O_RDONLY;
  76. break;
  77. case Ordwr:
  78. ret = O_RDWR;
  79. break;
  80. case Owrite:
  81. ret = O_WRONLY;
  82. break;
  83. case Oexec:
  84. ret = O_RDONLY;
  85. break;
  86. }
  87. if (mode & Otrunc) {
  88. ret |= O_TRUNC;
  89. }
  90. if (mode & Oappend) {
  91. ret |= O_APPEND;
  92. }
  93. if (mode & Oexcl) {
  94. ret |= O_EXCL;
  95. }
  96. return ret;
  97. }
  98. typedef struct DotlOpenflagMap {
  99. int dotl_flag;
  100. int open_flag;
  101. } DotlOpenflagMap;
  102. static int dotl_to_open_flags(int flags)
  103. {
  104. int i;
  105. /*
  106. * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
  107. * and P9_DOTL_NOACCESS
  108. */
  109. int oflags = flags & O_ACCMODE;
  110. DotlOpenflagMap dotl_oflag_map[] = {
  111. { P9_DOTL_CREATE, O_CREAT },
  112. { P9_DOTL_EXCL, O_EXCL },
  113. { P9_DOTL_NOCTTY , O_NOCTTY },
  114. { P9_DOTL_TRUNC, O_TRUNC },
  115. { P9_DOTL_APPEND, O_APPEND },
  116. { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
  117. { P9_DOTL_DSYNC, O_DSYNC },
  118. { P9_DOTL_FASYNC, FASYNC },
  119. #ifndef CONFIG_DARWIN
  120. { P9_DOTL_NOATIME, O_NOATIME },
  121. /*
  122. * On Darwin, we could map to F_NOCACHE, which is
  123. * similar, but doesn't quite have the same
  124. * semantics. However, we don't support O_DIRECT
  125. * even on linux at the moment, so we just ignore
  126. * it here.
  127. */
  128. { P9_DOTL_DIRECT, O_DIRECT },
  129. #endif
  130. { P9_DOTL_LARGEFILE, O_LARGEFILE },
  131. { P9_DOTL_DIRECTORY, O_DIRECTORY },
  132. { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
  133. { P9_DOTL_SYNC, O_SYNC },
  134. };
  135. for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
  136. if (flags & dotl_oflag_map[i].dotl_flag) {
  137. oflags |= dotl_oflag_map[i].open_flag;
  138. }
  139. }
  140. return oflags;
  141. }
  142. void cred_init(FsCred *credp)
  143. {
  144. credp->fc_uid = -1;
  145. credp->fc_gid = -1;
  146. credp->fc_mode = -1;
  147. credp->fc_rdev = -1;
  148. }
  149. static int get_dotl_openflags(V9fsState *s, int oflags)
  150. {
  151. int flags;
  152. /*
  153. * Filter the client open flags
  154. */
  155. flags = dotl_to_open_flags(oflags);
  156. flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
  157. #ifndef CONFIG_DARWIN
  158. /*
  159. * Ignore direct disk access hint until the server supports it.
  160. */
  161. flags &= ~O_DIRECT;
  162. #endif
  163. return flags;
  164. }
  165. void v9fs_path_init(V9fsPath *path)
  166. {
  167. path->data = NULL;
  168. path->size = 0;
  169. }
  170. void v9fs_path_free(V9fsPath *path)
  171. {
  172. g_free(path->data);
  173. path->data = NULL;
  174. path->size = 0;
  175. }
  176. void G_GNUC_PRINTF(2, 3)
  177. v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
  178. {
  179. va_list ap;
  180. v9fs_path_free(path);
  181. va_start(ap, fmt);
  182. /* Bump the size for including terminating NULL */
  183. path->size = g_vasprintf(&path->data, fmt, ap) + 1;
  184. va_end(ap);
  185. }
  186. void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
  187. {
  188. v9fs_path_free(dst);
  189. dst->size = src->size;
  190. dst->data = g_memdup(src->data, src->size);
  191. }
  192. int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
  193. const char *name, V9fsPath *path)
  194. {
  195. int err;
  196. err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
  197. if (err < 0) {
  198. err = -errno;
  199. }
  200. return err;
  201. }
  202. /*
  203. * Return TRUE if s1 is an ancestor of s2.
  204. *
  205. * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
  206. * As a special case, We treat s1 as ancestor of s2 if they are same!
  207. */
  208. static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
  209. {
  210. if (!strncmp(s1->data, s2->data, s1->size - 1)) {
  211. if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
  212. return 1;
  213. }
  214. }
  215. return 0;
  216. }
  217. static size_t v9fs_string_size(V9fsString *str)
  218. {
  219. return str->size;
  220. }
  221. /*
  222. * returns 0 if fid got re-opened, 1 if not, < 0 on error
  223. */
  224. static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
  225. {
  226. int err = 1;
  227. if (f->fid_type == P9_FID_FILE) {
  228. if (f->fs.fd == -1) {
  229. do {
  230. err = v9fs_co_open(pdu, f, f->open_flags);
  231. } while (err == -EINTR && !pdu->cancelled);
  232. }
  233. } else if (f->fid_type == P9_FID_DIR) {
  234. if (f->fs.dir.stream == NULL) {
  235. do {
  236. err = v9fs_co_opendir(pdu, f);
  237. } while (err == -EINTR && !pdu->cancelled);
  238. }
  239. }
  240. return err;
  241. }
  242. static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
  243. {
  244. int err;
  245. V9fsFidState *f;
  246. V9fsState *s = pdu->s;
  247. f = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
  248. if (f) {
  249. BUG_ON(f->clunked);
  250. /*
  251. * Update the fid ref upfront so that
  252. * we don't get reclaimed when we yield
  253. * in open later.
  254. */
  255. f->ref++;
  256. /*
  257. * check whether we need to reopen the
  258. * file. We might have closed the fd
  259. * while trying to free up some file
  260. * descriptors.
  261. */
  262. err = v9fs_reopen_fid(pdu, f);
  263. if (err < 0) {
  264. f->ref--;
  265. return NULL;
  266. }
  267. /*
  268. * Mark the fid as referenced so that the LRU
  269. * reclaim won't close the file descriptor
  270. */
  271. f->flags |= FID_REFERENCED;
  272. return f;
  273. }
  274. return NULL;
  275. }
  276. static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
  277. {
  278. V9fsFidState *f;
  279. f = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
  280. if (f) {
  281. /* If fid is already there return NULL */
  282. BUG_ON(f->clunked);
  283. return NULL;
  284. }
  285. f = g_new0(V9fsFidState, 1);
  286. f->fid = fid;
  287. f->fid_type = P9_FID_NONE;
  288. f->ref = 1;
  289. /*
  290. * Mark the fid as referenced so that the LRU
  291. * reclaim won't close the file descriptor
  292. */
  293. f->flags |= FID_REFERENCED;
  294. g_hash_table_insert(s->fids, GINT_TO_POINTER(fid), f);
  295. v9fs_readdir_init(s->proto_version, &f->fs.dir);
  296. v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir);
  297. return f;
  298. }
  299. static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
  300. {
  301. int retval = 0;
  302. if (fidp->fs.xattr.xattrwalk_fid) {
  303. /* getxattr/listxattr fid */
  304. goto free_value;
  305. }
  306. /*
  307. * if this is fid for setxattr. clunk should
  308. * result in setxattr localcall
  309. */
  310. if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
  311. /* clunk after partial write */
  312. retval = -EINVAL;
  313. goto free_out;
  314. }
  315. if (fidp->fs.xattr.len) {
  316. retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
  317. fidp->fs.xattr.value,
  318. fidp->fs.xattr.len,
  319. fidp->fs.xattr.flags);
  320. } else {
  321. retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
  322. }
  323. free_out:
  324. v9fs_string_free(&fidp->fs.xattr.name);
  325. free_value:
  326. g_free(fidp->fs.xattr.value);
  327. return retval;
  328. }
  329. static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
  330. {
  331. int retval = 0;
  332. if (fidp->fid_type == P9_FID_FILE) {
  333. /* If we reclaimed the fd no need to close */
  334. if (fidp->fs.fd != -1) {
  335. retval = v9fs_co_close(pdu, &fidp->fs);
  336. }
  337. } else if (fidp->fid_type == P9_FID_DIR) {
  338. if (fidp->fs.dir.stream != NULL) {
  339. retval = v9fs_co_closedir(pdu, &fidp->fs);
  340. }
  341. } else if (fidp->fid_type == P9_FID_XATTR) {
  342. retval = v9fs_xattr_fid_clunk(pdu, fidp);
  343. }
  344. v9fs_path_free(&fidp->path);
  345. g_free(fidp);
  346. return retval;
  347. }
  348. static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
  349. {
  350. BUG_ON(!fidp->ref);
  351. fidp->ref--;
  352. /*
  353. * Don't free the fid if it is in reclaim list
  354. */
  355. if (!fidp->ref && fidp->clunked) {
  356. if (fidp->fid == pdu->s->root_fid) {
  357. /*
  358. * if the clunked fid is root fid then we
  359. * have unmounted the fs on the client side.
  360. * delete the migration blocker. Ideally, this
  361. * should be hooked to transport close notification
  362. */
  363. migrate_del_blocker(&pdu->s->migration_blocker);
  364. }
  365. return free_fid(pdu, fidp);
  366. }
  367. return 0;
  368. }
  369. static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
  370. {
  371. V9fsFidState *fidp;
  372. /* TODO: Use g_hash_table_steal_extended() instead? */
  373. fidp = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
  374. if (fidp) {
  375. g_hash_table_remove(s->fids, GINT_TO_POINTER(fid));
  376. fidp->clunked = true;
  377. return fidp;
  378. }
  379. return NULL;
  380. }
  381. void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
  382. {
  383. int reclaim_count = 0;
  384. V9fsState *s = pdu->s;
  385. V9fsFidState *f;
  386. GHashTableIter iter;
  387. gpointer fid;
  388. int err;
  389. int nclosed = 0;
  390. /* prevent multiple coroutines running this function simultaniously */
  391. if (s->reclaiming) {
  392. return;
  393. }
  394. s->reclaiming = true;
  395. g_hash_table_iter_init(&iter, s->fids);
  396. QSLIST_HEAD(, V9fsFidState) reclaim_list =
  397. QSLIST_HEAD_INITIALIZER(reclaim_list);
  398. /* Pick FIDs to be closed, collect them on reclaim_list. */
  399. while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &f)) {
  400. /*
  401. * Unlinked fids cannot be reclaimed, skip those, and also skip fids
  402. * currently being operated on.
  403. */
  404. if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
  405. continue;
  406. }
  407. /*
  408. * if it is a recently referenced fid
  409. * we leave the fid untouched and clear the
  410. * reference bit. We come back to it later
  411. * in the next iteration. (a simple LRU without
  412. * moving list elements around)
  413. */
  414. if (f->flags & FID_REFERENCED) {
  415. f->flags &= ~FID_REFERENCED;
  416. continue;
  417. }
  418. /*
  419. * Add fids to reclaim list.
  420. */
  421. if (f->fid_type == P9_FID_FILE) {
  422. if (f->fs.fd != -1) {
  423. /*
  424. * Up the reference count so that
  425. * a clunk request won't free this fid
  426. */
  427. f->ref++;
  428. QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
  429. f->fs_reclaim.fd = f->fs.fd;
  430. f->fs.fd = -1;
  431. reclaim_count++;
  432. }
  433. } else if (f->fid_type == P9_FID_DIR) {
  434. if (f->fs.dir.stream != NULL) {
  435. /*
  436. * Up the reference count so that
  437. * a clunk request won't free this fid
  438. */
  439. f->ref++;
  440. QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
  441. f->fs_reclaim.dir.stream = f->fs.dir.stream;
  442. f->fs.dir.stream = NULL;
  443. reclaim_count++;
  444. }
  445. }
  446. if (reclaim_count >= open_fd_rc) {
  447. break;
  448. }
  449. }
  450. /*
  451. * Close the picked FIDs altogether on a background I/O driver thread. Do
  452. * this all at once to keep latency (i.e. amount of thread hops between main
  453. * thread <-> fs driver background thread) as low as possible.
  454. */
  455. v9fs_co_run_in_worker({
  456. QSLIST_FOREACH(f, &reclaim_list, reclaim_next) {
  457. err = (f->fid_type == P9_FID_DIR) ?
  458. s->ops->closedir(&s->ctx, &f->fs_reclaim) :
  459. s->ops->close(&s->ctx, &f->fs_reclaim);
  460. if (!err) {
  461. /* total_open_fd must only be mutated on main thread */
  462. nclosed++;
  463. }
  464. }
  465. });
  466. total_open_fd -= nclosed;
  467. /* Free the closed FIDs. */
  468. while (!QSLIST_EMPTY(&reclaim_list)) {
  469. f = QSLIST_FIRST(&reclaim_list);
  470. QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next);
  471. /*
  472. * Now drop the fid reference, free it
  473. * if clunked.
  474. */
  475. put_fid(pdu, f);
  476. }
  477. s->reclaiming = false;
  478. }
  479. /*
  480. * This is used when a path is removed from the directory tree. Any
  481. * fids that still reference it must not be closed from then on, since
  482. * they cannot be reopened.
  483. */
  484. static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
  485. {
  486. int err = 0;
  487. V9fsState *s = pdu->s;
  488. V9fsFidState *fidp;
  489. gpointer fid;
  490. GHashTableIter iter;
  491. /*
  492. * The most common case is probably that we have exactly one
  493. * fid for the given path, so preallocate exactly one.
  494. */
  495. g_autoptr(GArray) to_reopen = g_array_sized_new(FALSE, FALSE,
  496. sizeof(V9fsFidState *), 1);
  497. gint i;
  498. g_hash_table_iter_init(&iter, s->fids);
  499. /*
  500. * We iterate over the fid table looking for the entries we need
  501. * to reopen, and store them in to_reopen. This is because
  502. * v9fs_reopen_fid() and put_fid() yield. This allows the fid table
  503. * to be modified in the meantime, invalidating our iterator.
  504. */
  505. while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &fidp)) {
  506. if (fidp->path.size == path->size &&
  507. !memcmp(fidp->path.data, path->data, path->size)) {
  508. /*
  509. * Ensure the fid survives a potential clunk request during
  510. * v9fs_reopen_fid or put_fid.
  511. */
  512. fidp->ref++;
  513. fidp->flags |= FID_NON_RECLAIMABLE;
  514. g_array_append_val(to_reopen, fidp);
  515. }
  516. }
  517. for (i = 0; i < to_reopen->len; i++) {
  518. fidp = g_array_index(to_reopen, V9fsFidState*, i);
  519. /* reopen the file/dir if already closed */
  520. err = v9fs_reopen_fid(pdu, fidp);
  521. if (err < 0) {
  522. break;
  523. }
  524. }
  525. for (i = 0; i < to_reopen->len; i++) {
  526. put_fid(pdu, g_array_index(to_reopen, V9fsFidState*, i));
  527. }
  528. return err;
  529. }
  530. static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
  531. {
  532. V9fsState *s = pdu->s;
  533. V9fsFidState *fidp;
  534. GList *freeing;
  535. /*
  536. * Get a list of all the values (fid states) in the table, which
  537. * we then...
  538. */
  539. g_autoptr(GList) fids = g_hash_table_get_values(s->fids);
  540. /* ... remove from the table, taking over ownership. */
  541. g_hash_table_steal_all(s->fids);
  542. /*
  543. * This allows us to release our references to them asynchronously without
  544. * iterating over the hash table and risking iterator invalidation
  545. * through concurrent modifications.
  546. */
  547. for (freeing = fids; freeing; freeing = freeing->next) {
  548. fidp = freeing->data;
  549. fidp->ref++;
  550. fidp->clunked = true;
  551. put_fid(pdu, fidp);
  552. }
  553. }
  554. #define P9_QID_TYPE_DIR 0x80
  555. #define P9_QID_TYPE_SYMLINK 0x02
  556. #define P9_STAT_MODE_DIR 0x80000000
  557. #define P9_STAT_MODE_APPEND 0x40000000
  558. #define P9_STAT_MODE_EXCL 0x20000000
  559. #define P9_STAT_MODE_MOUNT 0x10000000
  560. #define P9_STAT_MODE_AUTH 0x08000000
  561. #define P9_STAT_MODE_TMP 0x04000000
  562. #define P9_STAT_MODE_SYMLINK 0x02000000
  563. #define P9_STAT_MODE_LINK 0x01000000
  564. #define P9_STAT_MODE_DEVICE 0x00800000
  565. #define P9_STAT_MODE_NAMED_PIPE 0x00200000
  566. #define P9_STAT_MODE_SOCKET 0x00100000
  567. #define P9_STAT_MODE_SETUID 0x00080000
  568. #define P9_STAT_MODE_SETGID 0x00040000
  569. #define P9_STAT_MODE_SETVTX 0x00010000
  570. #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
  571. P9_STAT_MODE_SYMLINK | \
  572. P9_STAT_MODE_LINK | \
  573. P9_STAT_MODE_DEVICE | \
  574. P9_STAT_MODE_NAMED_PIPE | \
  575. P9_STAT_MODE_SOCKET)
  576. /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
  577. static inline uint8_t mirror8bit(uint8_t byte)
  578. {
  579. return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
  580. }
  581. /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
  582. static inline uint64_t mirror64bit(uint64_t value)
  583. {
  584. return ((uint64_t)mirror8bit(value & 0xff) << 56) |
  585. ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) |
  586. ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
  587. ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
  588. ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
  589. ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
  590. ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) |
  591. ((uint64_t)mirror8bit((value >> 56) & 0xff));
  592. }
  593. /*
  594. * Parameter k for the Exponential Golomb algorithm to be used.
  595. *
  596. * The smaller this value, the smaller the minimum bit count for the Exp.
  597. * Golomb generated affixes will be (at lowest index) however for the
  598. * price of having higher maximum bit count of generated affixes (at highest
  599. * index). Likewise increasing this parameter yields in smaller maximum bit
  600. * count for the price of having higher minimum bit count.
  601. *
  602. * In practice that means: a good value for k depends on the expected amount
  603. * of devices to be exposed by one export. For a small amount of devices k
  604. * should be small, for a large amount of devices k might be increased
  605. * instead. The default of k=0 should be fine for most users though.
  606. *
  607. * IMPORTANT: In case this ever becomes a runtime parameter; the value of
  608. * k should not change as long as guest is still running! Because that would
  609. * cause completely different inode numbers to be generated on guest.
  610. */
  611. #define EXP_GOLOMB_K 0
  612. /**
  613. * expGolombEncode() - Exponential Golomb algorithm for arbitrary k
  614. * (including k=0).
  615. *
  616. * @n: natural number (or index) of the prefix to be generated
  617. * (1, 2, 3, ...)
  618. * @k: parameter k of Exp. Golomb algorithm to be used
  619. * (see comment on EXP_GOLOMB_K macro for details about k)
  620. * Return: prefix for given @n and @k
  621. *
  622. * The Exponential Golomb algorithm generates prefixes (NOT suffixes!)
  623. * with growing length and with the mathematical property of being
  624. * "prefix-free". The latter means the generated prefixes can be prepended
  625. * in front of arbitrary numbers and the resulting concatenated numbers are
  626. * guaranteed to be always unique.
  627. *
  628. * This is a minor adjustment to the original Exp. Golomb algorithm in the
  629. * sense that lowest allowed index (@n) starts with 1, not with zero.
  630. */
  631. static VariLenAffix expGolombEncode(uint64_t n, int k)
  632. {
  633. const uint64_t value = n + (1 << k) - 1;
  634. const int bits = (int) log2(value) + 1;
  635. return (VariLenAffix) {
  636. .type = AffixType_Prefix,
  637. .value = value,
  638. .bits = bits + MAX((bits - 1 - k), 0)
  639. };
  640. }
  641. /**
  642. * invertAffix() - Converts a suffix into a prefix, or a prefix into a suffix.
  643. * @affix: either suffix or prefix to be inverted
  644. * Return: inversion of passed @affix
  645. *
  646. * Simply mirror all bits of the affix value, for the purpose to preserve
  647. * respectively the mathematical "prefix-free" or "suffix-free" property
  648. * after the conversion.
  649. *
  650. * If a passed prefix is suitable to create unique numbers, then the
  651. * returned suffix is suitable to create unique numbers as well (and vice
  652. * versa).
  653. */
  654. static VariLenAffix invertAffix(const VariLenAffix *affix)
  655. {
  656. return (VariLenAffix) {
  657. .type =
  658. (affix->type == AffixType_Suffix) ?
  659. AffixType_Prefix : AffixType_Suffix,
  660. .value =
  661. mirror64bit(affix->value) >>
  662. ((sizeof(affix->value) * 8) - affix->bits),
  663. .bits = affix->bits
  664. };
  665. }
  666. /**
  667. * affixForIndex() - Generates suffix numbers with "suffix-free" property.
  668. * @index: natural number (or index) of the suffix to be generated
  669. * (1, 2, 3, ...)
  670. * Return: Suffix suitable to assemble unique number.
  671. *
  672. * This is just a wrapper function on top of the Exp. Golomb algorithm.
  673. *
  674. * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
  675. * this function converts the Exp. Golomb prefixes into appropriate suffixes
  676. * which are still suitable for generating unique numbers.
  677. */
  678. static VariLenAffix affixForIndex(uint64_t index)
  679. {
  680. VariLenAffix prefix;
  681. prefix = expGolombEncode(index, EXP_GOLOMB_K);
  682. return invertAffix(&prefix); /* convert prefix to suffix */
  683. }
  684. static uint32_t qpp_hash(QppEntry e)
  685. {
  686. return qemu_xxhash4(e.ino_prefix, e.dev);
  687. }
  688. static uint32_t qpf_hash(QpfEntry e)
  689. {
  690. return qemu_xxhash4(e.ino, e.dev);
  691. }
  692. static bool qpd_cmp_func(const void *obj, const void *userp)
  693. {
  694. const QpdEntry *e1 = obj, *e2 = userp;
  695. return e1->dev == e2->dev;
  696. }
  697. static bool qpp_cmp_func(const void *obj, const void *userp)
  698. {
  699. const QppEntry *e1 = obj, *e2 = userp;
  700. return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
  701. }
  702. static bool qpf_cmp_func(const void *obj, const void *userp)
  703. {
  704. const QpfEntry *e1 = obj, *e2 = userp;
  705. return e1->dev == e2->dev && e1->ino == e2->ino;
  706. }
  707. static void qp_table_remove(void *p, uint32_t h, void *up)
  708. {
  709. g_free(p);
  710. }
  711. static void qp_table_destroy(struct qht *ht)
  712. {
  713. if (!ht || !ht->map) {
  714. return;
  715. }
  716. qht_iter(ht, qp_table_remove, NULL);
  717. qht_destroy(ht);
  718. }
  719. static void qpd_table_init(struct qht *ht)
  720. {
  721. qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
  722. }
  723. static void qpp_table_init(struct qht *ht)
  724. {
  725. qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
  726. }
  727. static void qpf_table_init(struct qht *ht)
  728. {
  729. qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
  730. }
  731. /*
  732. * Returns how many (high end) bits of inode numbers of the passed fs
  733. * device shall be used (in combination with the device number) to
  734. * generate hash values for qpp_table entries.
  735. *
  736. * This function is required if variable length suffixes are used for inode
  737. * number mapping on guest level. Since a device may end up having multiple
  738. * entries in qpp_table, each entry most probably with a different suffix
  739. * length, we thus need this function in conjunction with qpd_table to
  740. * "agree" about a fix amount of bits (per device) to be always used for
  741. * generating hash values for the purpose of accessing qpp_table in order
  742. * get consistent behaviour when accessing qpp_table.
  743. */
  744. static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
  745. {
  746. QpdEntry lookup = {
  747. .dev = dev
  748. }, *val;
  749. uint32_t hash = dev;
  750. VariLenAffix affix;
  751. val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
  752. if (!val) {
  753. val = g_new0(QpdEntry, 1);
  754. *val = lookup;
  755. affix = affixForIndex(pdu->s->qp_affix_next);
  756. val->prefix_bits = affix.bits;
  757. qht_insert(&pdu->s->qpd_table, val, hash, NULL);
  758. pdu->s->qp_ndevices++;
  759. }
  760. return val->prefix_bits;
  761. }
  762. /*
  763. * Slow / full mapping host inode nr -> guest inode nr.
  764. *
  765. * This function performs a slower and much more costly remapping of an
  766. * original file inode number on host to an appropriate different inode
  767. * number on guest. For every (dev, inode) combination on host a new
  768. * sequential number is generated, cached and exposed as inode number on
  769. * guest.
  770. *
  771. * This is just a "last resort" fallback solution if the much faster/cheaper
  772. * qid_path_suffixmap() failed. In practice this slow / full mapping is not
  773. * expected ever to be used at all though.
  774. *
  775. * See qid_path_suffixmap() for details
  776. *
  777. */
  778. static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
  779. uint64_t *path)
  780. {
  781. QpfEntry lookup = {
  782. .dev = stbuf->st_dev,
  783. .ino = stbuf->st_ino
  784. }, *val;
  785. uint32_t hash = qpf_hash(lookup);
  786. VariLenAffix affix;
  787. val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
  788. if (!val) {
  789. if (pdu->s->qp_fullpath_next == 0) {
  790. /* no more files can be mapped :'( */
  791. error_report_once(
  792. "9p: No more prefixes available for remapping inodes from "
  793. "host to guest."
  794. );
  795. return -ENFILE;
  796. }
  797. val = g_new0(QpfEntry, 1);
  798. *val = lookup;
  799. /* new unique inode and device combo */
  800. affix = affixForIndex(
  801. 1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
  802. );
  803. val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
  804. pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
  805. qht_insert(&pdu->s->qpf_table, val, hash, NULL);
  806. }
  807. *path = val->path;
  808. return 0;
  809. }
  810. /*
  811. * Quick mapping host inode nr -> guest inode nr.
  812. *
  813. * This function performs quick remapping of an original file inode number
  814. * on host to an appropriate different inode number on guest. This remapping
  815. * of inodes is required to avoid inode nr collisions on guest which would
  816. * happen if the 9p export contains more than 1 exported file system (or
  817. * more than 1 file system data set), because unlike on host level where the
  818. * files would have different device nrs, all files exported by 9p would
  819. * share the same device nr on guest (the device nr of the virtual 9p device
  820. * that is).
  821. *
  822. * Inode remapping is performed by chopping off high end bits of the original
  823. * inode number from host, shifting the result upwards and then assigning a
  824. * generated suffix number for the low end bits, where the same suffix number
  825. * will be shared by all inodes with the same device id AND the same high end
  826. * bits that have been chopped off. That approach utilizes the fact that inode
  827. * numbers very likely share the same high end bits (i.e. due to their common
  828. * sequential generation by file systems) and hence we only have to generate
  829. * and track a very limited amount of suffixes in practice due to that.
  830. *
  831. * We generate variable size suffixes for that purpose. The 1st generated
  832. * suffix will only have 1 bit and hence we only need to chop off 1 bit from
  833. * the original inode number. The subsequent suffixes being generated will
  834. * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
  835. * generated will have 3 bits and hence we have to chop off 3 bits from their
  836. * original inodes, and so on. That approach of using variable length suffixes
  837. * (i.e. over fixed size ones) utilizes the fact that in practice only a very
  838. * limited amount of devices are shared by the same export (e.g. typically
  839. * less than 2 dozen devices per 9p export), so in practice we need to chop
  840. * off less bits than with fixed size prefixes and yet are flexible to add
  841. * new devices at runtime below host's export directory at any time without
  842. * having to reboot guest nor requiring to reconfigure guest for that. And due
  843. * to the very limited amount of original high end bits that we chop off that
  844. * way, the total amount of suffixes we need to generate is less than by using
  845. * fixed size prefixes and hence it also improves performance of the inode
  846. * remapping algorithm, and finally has the nice side effect that the inode
  847. * numbers on guest will be much smaller & human friendly. ;-)
  848. */
  849. static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
  850. uint64_t *path)
  851. {
  852. const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
  853. QppEntry lookup = {
  854. .dev = stbuf->st_dev,
  855. .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
  856. }, *val;
  857. uint32_t hash = qpp_hash(lookup);
  858. val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
  859. if (!val) {
  860. if (pdu->s->qp_affix_next == 0) {
  861. /* we ran out of affixes */
  862. warn_report_once(
  863. "9p: Potential degraded performance of inode remapping"
  864. );
  865. return -ENFILE;
  866. }
  867. val = g_new0(QppEntry, 1);
  868. *val = lookup;
  869. /* new unique inode affix and device combo */
  870. val->qp_affix_index = pdu->s->qp_affix_next++;
  871. val->qp_affix = affixForIndex(val->qp_affix_index);
  872. qht_insert(&pdu->s->qpp_table, val, hash, NULL);
  873. }
  874. /* assuming generated affix to be suffix type, not prefix */
  875. *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
  876. return 0;
  877. }
  878. static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
  879. {
  880. int err;
  881. size_t size;
  882. if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
  883. /* map inode+device to qid path (fast path) */
  884. err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
  885. if (err == -ENFILE) {
  886. /* fast path didn't work, fall back to full map */
  887. err = qid_path_fullmap(pdu, stbuf, &qidp->path);
  888. }
  889. if (err) {
  890. return err;
  891. }
  892. } else {
  893. if (pdu->s->dev_id != stbuf->st_dev) {
  894. if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
  895. error_report_once(
  896. "9p: Multiple devices detected in same VirtFS export. "
  897. "Access of guest to additional devices is (partly) "
  898. "denied due to virtfs option 'multidevs=forbid' being "
  899. "effective."
  900. );
  901. return -ENODEV;
  902. } else {
  903. warn_report_once(
  904. "9p: Multiple devices detected in same VirtFS export, "
  905. "which might lead to file ID collisions and severe "
  906. "misbehaviours on guest! You should either use a "
  907. "separate export for each device shared from host or "
  908. "use virtfs option 'multidevs=remap'!"
  909. );
  910. }
  911. }
  912. memset(&qidp->path, 0, sizeof(qidp->path));
  913. size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
  914. memcpy(&qidp->path, &stbuf->st_ino, size);
  915. }
  916. qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
  917. qidp->type = 0;
  918. if (S_ISDIR(stbuf->st_mode)) {
  919. qidp->type |= P9_QID_TYPE_DIR;
  920. }
  921. if (S_ISLNK(stbuf->st_mode)) {
  922. qidp->type |= P9_QID_TYPE_SYMLINK;
  923. }
  924. return 0;
  925. }
  926. V9fsPDU *pdu_alloc(V9fsState *s)
  927. {
  928. V9fsPDU *pdu = NULL;
  929. if (!QLIST_EMPTY(&s->free_list)) {
  930. pdu = QLIST_FIRST(&s->free_list);
  931. QLIST_REMOVE(pdu, next);
  932. QLIST_INSERT_HEAD(&s->active_list, pdu, next);
  933. }
  934. return pdu;
  935. }
  936. void pdu_free(V9fsPDU *pdu)
  937. {
  938. V9fsState *s = pdu->s;
  939. g_assert(!pdu->cancelled);
  940. QLIST_REMOVE(pdu, next);
  941. QLIST_INSERT_HEAD(&s->free_list, pdu, next);
  942. }
  943. static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
  944. {
  945. int8_t id = pdu->id + 1; /* Response */
  946. V9fsState *s = pdu->s;
  947. int ret;
  948. /*
  949. * The 9p spec requires that successfully cancelled pdus receive no reply.
  950. * Sending a reply would confuse clients because they would
  951. * assume that any EINTR is the actual result of the operation,
  952. * rather than a consequence of the cancellation. However, if
  953. * the operation completed (successfully or with an error other
  954. * than caused be cancellation), we do send out that reply, both
  955. * for efficiency and to avoid confusing the rest of the state machine
  956. * that assumes passing a non-error here will mean a successful
  957. * transmission of the reply.
  958. */
  959. bool discard = pdu->cancelled && len == -EINTR;
  960. if (discard) {
  961. trace_v9fs_rcancel(pdu->tag, pdu->id);
  962. pdu->size = 0;
  963. goto out_notify;
  964. }
  965. if (len < 0) {
  966. int err = -len;
  967. len = 7;
  968. if (s->proto_version != V9FS_PROTO_2000L) {
  969. V9fsString str;
  970. str.data = strerror(err);
  971. str.size = strlen(str.data);
  972. ret = pdu_marshal(pdu, len, "s", &str);
  973. if (ret < 0) {
  974. goto out_notify;
  975. }
  976. len += ret;
  977. id = P9_RERROR;
  978. } else {
  979. err = errno_to_dotl(err);
  980. }
  981. ret = pdu_marshal(pdu, len, "d", err);
  982. if (ret < 0) {
  983. goto out_notify;
  984. }
  985. len += ret;
  986. if (s->proto_version == V9FS_PROTO_2000L) {
  987. id = P9_RLERROR;
  988. }
  989. trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
  990. }
  991. /* fill out the header */
  992. if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
  993. goto out_notify;
  994. }
  995. /* keep these in sync */
  996. pdu->size = len;
  997. pdu->id = id;
  998. out_notify:
  999. pdu->s->transport->push_and_notify(pdu);
  1000. /* Now wakeup anybody waiting in flush for this request */
  1001. if (!qemu_co_queue_next(&pdu->complete)) {
  1002. pdu_free(pdu);
  1003. }
  1004. }
  1005. static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
  1006. {
  1007. mode_t ret;
  1008. ret = mode & 0777;
  1009. if (mode & P9_STAT_MODE_DIR) {
  1010. ret |= S_IFDIR;
  1011. }
  1012. if (mode & P9_STAT_MODE_SYMLINK) {
  1013. ret |= S_IFLNK;
  1014. }
  1015. if (mode & P9_STAT_MODE_SOCKET) {
  1016. ret |= S_IFSOCK;
  1017. }
  1018. if (mode & P9_STAT_MODE_NAMED_PIPE) {
  1019. ret |= S_IFIFO;
  1020. }
  1021. if (mode & P9_STAT_MODE_DEVICE) {
  1022. if (extension->size && extension->data[0] == 'c') {
  1023. ret |= S_IFCHR;
  1024. } else {
  1025. ret |= S_IFBLK;
  1026. }
  1027. }
  1028. if (!(ret & ~0777)) {
  1029. ret |= S_IFREG;
  1030. }
  1031. if (mode & P9_STAT_MODE_SETUID) {
  1032. ret |= S_ISUID;
  1033. }
  1034. if (mode & P9_STAT_MODE_SETGID) {
  1035. ret |= S_ISGID;
  1036. }
  1037. if (mode & P9_STAT_MODE_SETVTX) {
  1038. ret |= S_ISVTX;
  1039. }
  1040. return ret;
  1041. }
  1042. static int donttouch_stat(V9fsStat *stat)
  1043. {
  1044. if (stat->type == -1 &&
  1045. stat->dev == -1 &&
  1046. stat->qid.type == 0xff &&
  1047. stat->qid.version == (uint32_t) -1 &&
  1048. stat->qid.path == (uint64_t) -1 &&
  1049. stat->mode == -1 &&
  1050. stat->atime == -1 &&
  1051. stat->mtime == -1 &&
  1052. stat->length == -1 &&
  1053. !stat->name.size &&
  1054. !stat->uid.size &&
  1055. !stat->gid.size &&
  1056. !stat->muid.size &&
  1057. stat->n_uid == -1 &&
  1058. stat->n_gid == -1 &&
  1059. stat->n_muid == -1) {
  1060. return 1;
  1061. }
  1062. return 0;
  1063. }
  1064. static void v9fs_stat_init(V9fsStat *stat)
  1065. {
  1066. v9fs_string_init(&stat->name);
  1067. v9fs_string_init(&stat->uid);
  1068. v9fs_string_init(&stat->gid);
  1069. v9fs_string_init(&stat->muid);
  1070. v9fs_string_init(&stat->extension);
  1071. }
  1072. static void v9fs_stat_free(V9fsStat *stat)
  1073. {
  1074. v9fs_string_free(&stat->name);
  1075. v9fs_string_free(&stat->uid);
  1076. v9fs_string_free(&stat->gid);
  1077. v9fs_string_free(&stat->muid);
  1078. v9fs_string_free(&stat->extension);
  1079. }
  1080. static uint32_t stat_to_v9mode(const struct stat *stbuf)
  1081. {
  1082. uint32_t mode;
  1083. mode = stbuf->st_mode & 0777;
  1084. if (S_ISDIR(stbuf->st_mode)) {
  1085. mode |= P9_STAT_MODE_DIR;
  1086. }
  1087. if (S_ISLNK(stbuf->st_mode)) {
  1088. mode |= P9_STAT_MODE_SYMLINK;
  1089. }
  1090. if (S_ISSOCK(stbuf->st_mode)) {
  1091. mode |= P9_STAT_MODE_SOCKET;
  1092. }
  1093. if (S_ISFIFO(stbuf->st_mode)) {
  1094. mode |= P9_STAT_MODE_NAMED_PIPE;
  1095. }
  1096. if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
  1097. mode |= P9_STAT_MODE_DEVICE;
  1098. }
  1099. if (stbuf->st_mode & S_ISUID) {
  1100. mode |= P9_STAT_MODE_SETUID;
  1101. }
  1102. if (stbuf->st_mode & S_ISGID) {
  1103. mode |= P9_STAT_MODE_SETGID;
  1104. }
  1105. if (stbuf->st_mode & S_ISVTX) {
  1106. mode |= P9_STAT_MODE_SETVTX;
  1107. }
  1108. return mode;
  1109. }
  1110. static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
  1111. const char *basename,
  1112. const struct stat *stbuf,
  1113. V9fsStat *v9stat)
  1114. {
  1115. int err;
  1116. memset(v9stat, 0, sizeof(*v9stat));
  1117. err = stat_to_qid(pdu, stbuf, &v9stat->qid);
  1118. if (err < 0) {
  1119. return err;
  1120. }
  1121. v9stat->mode = stat_to_v9mode(stbuf);
  1122. v9stat->atime = stbuf->st_atime;
  1123. v9stat->mtime = stbuf->st_mtime;
  1124. v9stat->length = stbuf->st_size;
  1125. v9fs_string_free(&v9stat->uid);
  1126. v9fs_string_free(&v9stat->gid);
  1127. v9fs_string_free(&v9stat->muid);
  1128. v9stat->n_uid = stbuf->st_uid;
  1129. v9stat->n_gid = stbuf->st_gid;
  1130. v9stat->n_muid = 0;
  1131. v9fs_string_free(&v9stat->extension);
  1132. if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
  1133. err = v9fs_co_readlink(pdu, path, &v9stat->extension);
  1134. if (err < 0) {
  1135. return err;
  1136. }
  1137. } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
  1138. v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
  1139. S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
  1140. major(stbuf->st_rdev), minor(stbuf->st_rdev));
  1141. } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
  1142. v9fs_string_sprintf(&v9stat->extension, "%s %lu",
  1143. "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
  1144. }
  1145. v9fs_string_sprintf(&v9stat->name, "%s", basename);
  1146. v9stat->size = 61 +
  1147. v9fs_string_size(&v9stat->name) +
  1148. v9fs_string_size(&v9stat->uid) +
  1149. v9fs_string_size(&v9stat->gid) +
  1150. v9fs_string_size(&v9stat->muid) +
  1151. v9fs_string_size(&v9stat->extension);
  1152. return 0;
  1153. }
  1154. #define P9_STATS_MODE 0x00000001ULL
  1155. #define P9_STATS_NLINK 0x00000002ULL
  1156. #define P9_STATS_UID 0x00000004ULL
  1157. #define P9_STATS_GID 0x00000008ULL
  1158. #define P9_STATS_RDEV 0x00000010ULL
  1159. #define P9_STATS_ATIME 0x00000020ULL
  1160. #define P9_STATS_MTIME 0x00000040ULL
  1161. #define P9_STATS_CTIME 0x00000080ULL
  1162. #define P9_STATS_INO 0x00000100ULL
  1163. #define P9_STATS_SIZE 0x00000200ULL
  1164. #define P9_STATS_BLOCKS 0x00000400ULL
  1165. #define P9_STATS_BTIME 0x00000800ULL
  1166. #define P9_STATS_GEN 0x00001000ULL
  1167. #define P9_STATS_DATA_VERSION 0x00002000ULL
  1168. #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
  1169. #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
  1170. /**
  1171. * blksize_to_iounit() - Block size exposed to 9p client.
  1172. * Return: block size
  1173. *
  1174. * @pdu: 9p client request
  1175. * @blksize: host filesystem's block size
  1176. *
  1177. * Convert host filesystem's block size into an appropriate block size for
  1178. * 9p client (guest OS side). The value returned suggests an "optimum" block
  1179. * size for 9p I/O, i.e. to maximize performance.
  1180. */
  1181. static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize)
  1182. {
  1183. int32_t iounit = 0;
  1184. V9fsState *s = pdu->s;
  1185. /*
  1186. * iounit should be multiples of blksize (host filesystem block size)
  1187. * as well as less than (client msize - P9_IOHDRSZ)
  1188. */
  1189. if (blksize) {
  1190. iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize);
  1191. }
  1192. if (!iounit) {
  1193. iounit = s->msize - P9_IOHDRSZ;
  1194. }
  1195. return iounit;
  1196. }
  1197. static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
  1198. {
  1199. return blksize_to_iounit(pdu, stbuf->st_blksize);
  1200. }
  1201. static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
  1202. V9fsStatDotl *v9lstat)
  1203. {
  1204. memset(v9lstat, 0, sizeof(*v9lstat));
  1205. v9lstat->st_mode = stbuf->st_mode;
  1206. v9lstat->st_nlink = stbuf->st_nlink;
  1207. v9lstat->st_uid = stbuf->st_uid;
  1208. v9lstat->st_gid = stbuf->st_gid;
  1209. v9lstat->st_rdev = host_dev_to_dotl_dev(stbuf->st_rdev);
  1210. v9lstat->st_size = stbuf->st_size;
  1211. v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
  1212. v9lstat->st_blocks = stbuf->st_blocks;
  1213. v9lstat->st_atime_sec = stbuf->st_atime;
  1214. v9lstat->st_mtime_sec = stbuf->st_mtime;
  1215. v9lstat->st_ctime_sec = stbuf->st_ctime;
  1216. #ifdef CONFIG_DARWIN
  1217. v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec;
  1218. v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec;
  1219. v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec;
  1220. #else
  1221. v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
  1222. v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
  1223. v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
  1224. #endif
  1225. /* Currently we only support BASIC fields in stat */
  1226. v9lstat->st_result_mask = P9_STATS_BASIC;
  1227. return stat_to_qid(pdu, stbuf, &v9lstat->qid);
  1228. }
  1229. static void print_sg(struct iovec *sg, int cnt)
  1230. {
  1231. int i;
  1232. printf("sg[%d]: {", cnt);
  1233. for (i = 0; i < cnt; i++) {
  1234. if (i) {
  1235. printf(", ");
  1236. }
  1237. printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
  1238. }
  1239. printf("}\n");
  1240. }
  1241. /* Will call this only for path name based fid */
  1242. static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
  1243. {
  1244. V9fsPath str;
  1245. v9fs_path_init(&str);
  1246. v9fs_path_copy(&str, dst);
  1247. v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
  1248. v9fs_path_free(&str);
  1249. }
  1250. static inline bool is_ro_export(FsContext *ctx)
  1251. {
  1252. return ctx->export_flags & V9FS_RDONLY;
  1253. }
  1254. static void coroutine_fn v9fs_version(void *opaque)
  1255. {
  1256. ssize_t err;
  1257. V9fsPDU *pdu = opaque;
  1258. V9fsState *s = pdu->s;
  1259. V9fsString version;
  1260. size_t offset = 7;
  1261. v9fs_string_init(&version);
  1262. err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
  1263. if (err < 0) {
  1264. goto out;
  1265. }
  1266. trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
  1267. virtfs_reset(pdu);
  1268. if (!strcmp(version.data, "9P2000.u")) {
  1269. s->proto_version = V9FS_PROTO_2000U;
  1270. } else if (!strcmp(version.data, "9P2000.L")) {
  1271. s->proto_version = V9FS_PROTO_2000L;
  1272. } else {
  1273. v9fs_string_sprintf(&version, "unknown");
  1274. /* skip min. msize check, reporting invalid version has priority */
  1275. goto marshal;
  1276. }
  1277. if (s->msize < P9_MIN_MSIZE) {
  1278. err = -EMSGSIZE;
  1279. error_report(
  1280. "9pfs: Client requested msize < minimum msize ("
  1281. stringify(P9_MIN_MSIZE) ") supported by this server."
  1282. );
  1283. goto out;
  1284. }
  1285. /* 8192 is the default msize of Linux clients */
  1286. if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) {
  1287. warn_report_once(
  1288. "9p: degraded performance: a reasonable high msize should be "
  1289. "chosen on client/guest side (chosen msize is <= 8192). See "
  1290. "https://wiki.qemu.org/Documentation/9psetup#msize for details."
  1291. );
  1292. }
  1293. marshal:
  1294. err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
  1295. if (err < 0) {
  1296. goto out;
  1297. }
  1298. err += offset;
  1299. trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
  1300. out:
  1301. pdu_complete(pdu, err);
  1302. v9fs_string_free(&version);
  1303. }
  1304. static void coroutine_fn v9fs_attach(void *opaque)
  1305. {
  1306. V9fsPDU *pdu = opaque;
  1307. V9fsState *s = pdu->s;
  1308. int32_t fid, afid, n_uname;
  1309. V9fsString uname, aname;
  1310. V9fsFidState *fidp;
  1311. size_t offset = 7;
  1312. V9fsQID qid;
  1313. ssize_t err;
  1314. struct stat stbuf;
  1315. v9fs_string_init(&uname);
  1316. v9fs_string_init(&aname);
  1317. err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
  1318. &afid, &uname, &aname, &n_uname);
  1319. if (err < 0) {
  1320. goto out_nofid;
  1321. }
  1322. trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
  1323. fidp = alloc_fid(s, fid);
  1324. if (fidp == NULL) {
  1325. err = -EINVAL;
  1326. goto out_nofid;
  1327. }
  1328. fidp->uid = n_uname;
  1329. err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
  1330. if (err < 0) {
  1331. err = -EINVAL;
  1332. clunk_fid(s, fid);
  1333. goto out;
  1334. }
  1335. err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  1336. if (err < 0) {
  1337. err = -EINVAL;
  1338. clunk_fid(s, fid);
  1339. goto out;
  1340. }
  1341. err = stat_to_qid(pdu, &stbuf, &qid);
  1342. if (err < 0) {
  1343. err = -EINVAL;
  1344. clunk_fid(s, fid);
  1345. goto out;
  1346. }
  1347. /*
  1348. * disable migration if we haven't done already.
  1349. * attach could get called multiple times for the same export.
  1350. */
  1351. if (!s->migration_blocker) {
  1352. error_setg(&s->migration_blocker,
  1353. "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
  1354. s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
  1355. err = migrate_add_blocker(&s->migration_blocker, NULL);
  1356. if (err < 0) {
  1357. clunk_fid(s, fid);
  1358. goto out;
  1359. }
  1360. s->root_fid = fid;
  1361. }
  1362. err = pdu_marshal(pdu, offset, "Q", &qid);
  1363. if (err < 0) {
  1364. clunk_fid(s, fid);
  1365. goto out;
  1366. }
  1367. err += offset;
  1368. memcpy(&s->root_st, &stbuf, sizeof(stbuf));
  1369. trace_v9fs_attach_return(pdu->tag, pdu->id,
  1370. qid.type, qid.version, qid.path);
  1371. out:
  1372. put_fid(pdu, fidp);
  1373. out_nofid:
  1374. pdu_complete(pdu, err);
  1375. v9fs_string_free(&uname);
  1376. v9fs_string_free(&aname);
  1377. }
  1378. static void coroutine_fn v9fs_stat(void *opaque)
  1379. {
  1380. int32_t fid;
  1381. V9fsStat v9stat;
  1382. ssize_t err = 0;
  1383. size_t offset = 7;
  1384. struct stat stbuf;
  1385. V9fsFidState *fidp;
  1386. V9fsPDU *pdu = opaque;
  1387. char *basename;
  1388. err = pdu_unmarshal(pdu, offset, "d", &fid);
  1389. if (err < 0) {
  1390. goto out_nofid;
  1391. }
  1392. trace_v9fs_stat(pdu->tag, pdu->id, fid);
  1393. fidp = get_fid(pdu, fid);
  1394. if (fidp == NULL) {
  1395. err = -ENOENT;
  1396. goto out_nofid;
  1397. }
  1398. err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  1399. if (err < 0) {
  1400. goto out;
  1401. }
  1402. basename = g_path_get_basename(fidp->path.data);
  1403. err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
  1404. g_free(basename);
  1405. if (err < 0) {
  1406. goto out;
  1407. }
  1408. err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
  1409. if (err < 0) {
  1410. v9fs_stat_free(&v9stat);
  1411. goto out;
  1412. }
  1413. trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
  1414. v9stat.atime, v9stat.mtime, v9stat.length);
  1415. err += offset;
  1416. v9fs_stat_free(&v9stat);
  1417. out:
  1418. put_fid(pdu, fidp);
  1419. out_nofid:
  1420. pdu_complete(pdu, err);
  1421. }
  1422. static void coroutine_fn v9fs_getattr(void *opaque)
  1423. {
  1424. int32_t fid;
  1425. size_t offset = 7;
  1426. ssize_t retval = 0;
  1427. struct stat stbuf;
  1428. V9fsFidState *fidp;
  1429. uint64_t request_mask;
  1430. V9fsStatDotl v9stat_dotl;
  1431. V9fsPDU *pdu = opaque;
  1432. retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
  1433. if (retval < 0) {
  1434. goto out_nofid;
  1435. }
  1436. trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
  1437. fidp = get_fid(pdu, fid);
  1438. if (fidp == NULL) {
  1439. retval = -ENOENT;
  1440. goto out_nofid;
  1441. }
  1442. if ((fidp->fid_type == P9_FID_FILE && fidp->fs.fd != -1) ||
  1443. (fidp->fid_type == P9_FID_DIR && fidp->fs.dir.stream))
  1444. {
  1445. retval = v9fs_co_fstat(pdu, fidp, &stbuf);
  1446. } else {
  1447. retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  1448. }
  1449. if (retval < 0) {
  1450. goto out;
  1451. }
  1452. retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
  1453. if (retval < 0) {
  1454. goto out;
  1455. }
  1456. /* fill st_gen if requested and supported by underlying fs */
  1457. if (request_mask & P9_STATS_GEN) {
  1458. retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
  1459. switch (retval) {
  1460. case 0:
  1461. /* we have valid st_gen: update result mask */
  1462. v9stat_dotl.st_result_mask |= P9_STATS_GEN;
  1463. break;
  1464. case -EINTR:
  1465. /* request cancelled, e.g. by Tflush */
  1466. goto out;
  1467. default:
  1468. /* failed to get st_gen: not fatal, ignore */
  1469. break;
  1470. }
  1471. }
  1472. retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
  1473. if (retval < 0) {
  1474. goto out;
  1475. }
  1476. retval += offset;
  1477. trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
  1478. v9stat_dotl.st_mode, v9stat_dotl.st_uid,
  1479. v9stat_dotl.st_gid);
  1480. out:
  1481. put_fid(pdu, fidp);
  1482. out_nofid:
  1483. pdu_complete(pdu, retval);
  1484. }
  1485. /* Attribute flags */
  1486. #define P9_ATTR_MODE (1 << 0)
  1487. #define P9_ATTR_UID (1 << 1)
  1488. #define P9_ATTR_GID (1 << 2)
  1489. #define P9_ATTR_SIZE (1 << 3)
  1490. #define P9_ATTR_ATIME (1 << 4)
  1491. #define P9_ATTR_MTIME (1 << 5)
  1492. #define P9_ATTR_CTIME (1 << 6)
  1493. #define P9_ATTR_ATIME_SET (1 << 7)
  1494. #define P9_ATTR_MTIME_SET (1 << 8)
  1495. #define P9_ATTR_MASK 127
  1496. static void coroutine_fn v9fs_setattr(void *opaque)
  1497. {
  1498. int err = 0;
  1499. int32_t fid;
  1500. V9fsFidState *fidp;
  1501. size_t offset = 7;
  1502. V9fsIattr v9iattr;
  1503. V9fsPDU *pdu = opaque;
  1504. err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
  1505. if (err < 0) {
  1506. goto out_nofid;
  1507. }
  1508. trace_v9fs_setattr(pdu->tag, pdu->id, fid,
  1509. v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
  1510. v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
  1511. fidp = get_fid(pdu, fid);
  1512. if (fidp == NULL) {
  1513. err = -EINVAL;
  1514. goto out_nofid;
  1515. }
  1516. if (v9iattr.valid & P9_ATTR_MODE) {
  1517. err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
  1518. if (err < 0) {
  1519. goto out;
  1520. }
  1521. }
  1522. if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
  1523. struct timespec times[2];
  1524. if (v9iattr.valid & P9_ATTR_ATIME) {
  1525. if (v9iattr.valid & P9_ATTR_ATIME_SET) {
  1526. times[0].tv_sec = v9iattr.atime_sec;
  1527. times[0].tv_nsec = v9iattr.atime_nsec;
  1528. } else {
  1529. times[0].tv_nsec = UTIME_NOW;
  1530. }
  1531. } else {
  1532. times[0].tv_nsec = UTIME_OMIT;
  1533. }
  1534. if (v9iattr.valid & P9_ATTR_MTIME) {
  1535. if (v9iattr.valid & P9_ATTR_MTIME_SET) {
  1536. times[1].tv_sec = v9iattr.mtime_sec;
  1537. times[1].tv_nsec = v9iattr.mtime_nsec;
  1538. } else {
  1539. times[1].tv_nsec = UTIME_NOW;
  1540. }
  1541. } else {
  1542. times[1].tv_nsec = UTIME_OMIT;
  1543. }
  1544. err = v9fs_co_utimensat(pdu, &fidp->path, times);
  1545. if (err < 0) {
  1546. goto out;
  1547. }
  1548. }
  1549. /*
  1550. * If the only valid entry in iattr is ctime we can call
  1551. * chown(-1,-1) to update the ctime of the file
  1552. */
  1553. if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
  1554. ((v9iattr.valid & P9_ATTR_CTIME)
  1555. && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
  1556. if (!(v9iattr.valid & P9_ATTR_UID)) {
  1557. v9iattr.uid = -1;
  1558. }
  1559. if (!(v9iattr.valid & P9_ATTR_GID)) {
  1560. v9iattr.gid = -1;
  1561. }
  1562. err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
  1563. v9iattr.gid);
  1564. if (err < 0) {
  1565. goto out;
  1566. }
  1567. }
  1568. if (v9iattr.valid & (P9_ATTR_SIZE)) {
  1569. err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
  1570. if (err < 0) {
  1571. goto out;
  1572. }
  1573. }
  1574. err = offset;
  1575. trace_v9fs_setattr_return(pdu->tag, pdu->id);
  1576. out:
  1577. put_fid(pdu, fidp);
  1578. out_nofid:
  1579. pdu_complete(pdu, err);
  1580. }
  1581. static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
  1582. {
  1583. int i;
  1584. ssize_t err;
  1585. size_t offset = 7;
  1586. err = pdu_marshal(pdu, offset, "w", nwnames);
  1587. if (err < 0) {
  1588. return err;
  1589. }
  1590. offset += err;
  1591. for (i = 0; i < nwnames; i++) {
  1592. err = pdu_marshal(pdu, offset, "Q", &qids[i]);
  1593. if (err < 0) {
  1594. return err;
  1595. }
  1596. offset += err;
  1597. }
  1598. return offset;
  1599. }
  1600. static bool name_is_illegal(const char *name)
  1601. {
  1602. return !*name || strchr(name, '/') != NULL;
  1603. }
  1604. static bool same_stat_id(const struct stat *a, const struct stat *b)
  1605. {
  1606. return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
  1607. }
  1608. /*
  1609. * Returns a (newly allocated) comma-separated string presentation of the
  1610. * passed array for logging (tracing) purpose for trace event "v9fs_walk".
  1611. *
  1612. * It is caller's responsibility to free the returned string.
  1613. */
  1614. static char *trace_v9fs_walk_wnames(V9fsString *wnames, size_t nwnames)
  1615. {
  1616. g_autofree char **arr = g_malloc0_n(nwnames + 1, sizeof(char *));
  1617. for (size_t i = 0; i < nwnames; ++i) {
  1618. arr[i] = wnames[i].data;
  1619. }
  1620. return g_strjoinv(", ", arr);
  1621. }
  1622. static void coroutine_fn v9fs_walk(void *opaque)
  1623. {
  1624. int name_idx, nwalked;
  1625. g_autofree V9fsQID *qids = NULL;
  1626. int i, err = 0, any_err = 0;
  1627. V9fsPath dpath, path;
  1628. P9ARRAY_REF(V9fsPath) pathes = NULL;
  1629. uint16_t nwnames;
  1630. struct stat stbuf, fidst;
  1631. g_autofree struct stat *stbufs = NULL;
  1632. size_t offset = 7;
  1633. int32_t fid, newfid;
  1634. P9ARRAY_REF(V9fsString) wnames = NULL;
  1635. g_autofree char *trace_wnames = NULL;
  1636. V9fsFidState *fidp;
  1637. V9fsFidState *newfidp = NULL;
  1638. V9fsPDU *pdu = opaque;
  1639. V9fsState *s = pdu->s;
  1640. V9fsQID qid;
  1641. err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
  1642. if (err < 0) {
  1643. pdu_complete(pdu, err);
  1644. return;
  1645. }
  1646. offset += err;
  1647. if (nwnames > P9_MAXWELEM) {
  1648. err = -EINVAL;
  1649. goto out_nofid_nownames;
  1650. }
  1651. if (nwnames) {
  1652. P9ARRAY_NEW(V9fsString, wnames, nwnames);
  1653. qids = g_new0(V9fsQID, nwnames);
  1654. stbufs = g_new0(struct stat, nwnames);
  1655. P9ARRAY_NEW(V9fsPath, pathes, nwnames);
  1656. for (i = 0; i < nwnames; i++) {
  1657. err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
  1658. if (err < 0) {
  1659. goto out_nofid_nownames;
  1660. }
  1661. if (name_is_illegal(wnames[i].data)) {
  1662. err = -ENOENT;
  1663. goto out_nofid_nownames;
  1664. }
  1665. offset += err;
  1666. }
  1667. if (trace_event_get_state_backends(TRACE_V9FS_WALK)) {
  1668. trace_wnames = trace_v9fs_walk_wnames(wnames, nwnames);
  1669. trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames,
  1670. trace_wnames);
  1671. }
  1672. } else {
  1673. trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames, "");
  1674. }
  1675. fidp = get_fid(pdu, fid);
  1676. if (fidp == NULL) {
  1677. err = -ENOENT;
  1678. goto out_nofid;
  1679. }
  1680. v9fs_path_init(&dpath);
  1681. v9fs_path_init(&path);
  1682. /*
  1683. * Both dpath and path initially point to fidp.
  1684. * Needed to handle request with nwnames == 0
  1685. */
  1686. v9fs_path_copy(&dpath, &fidp->path);
  1687. v9fs_path_copy(&path, &fidp->path);
  1688. /*
  1689. * To keep latency (i.e. overall execution time for processing this
  1690. * Twalk client request) as small as possible, run all the required fs
  1691. * driver code altogether inside the following block.
  1692. */
  1693. v9fs_co_run_in_worker({
  1694. nwalked = 0;
  1695. if (v9fs_request_cancelled(pdu)) {
  1696. any_err |= err = -EINTR;
  1697. break;
  1698. }
  1699. err = s->ops->lstat(&s->ctx, &dpath, &fidst);
  1700. if (err < 0) {
  1701. any_err |= err = -errno;
  1702. break;
  1703. }
  1704. stbuf = fidst;
  1705. for (; nwalked < nwnames; nwalked++) {
  1706. if (v9fs_request_cancelled(pdu)) {
  1707. any_err |= err = -EINTR;
  1708. break;
  1709. }
  1710. if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
  1711. strcmp("..", wnames[nwalked].data))
  1712. {
  1713. err = s->ops->name_to_path(&s->ctx, &dpath,
  1714. wnames[nwalked].data,
  1715. &pathes[nwalked]);
  1716. if (err < 0) {
  1717. any_err |= err = -errno;
  1718. break;
  1719. }
  1720. if (v9fs_request_cancelled(pdu)) {
  1721. any_err |= err = -EINTR;
  1722. break;
  1723. }
  1724. err = s->ops->lstat(&s->ctx, &pathes[nwalked], &stbuf);
  1725. if (err < 0) {
  1726. any_err |= err = -errno;
  1727. break;
  1728. }
  1729. stbufs[nwalked] = stbuf;
  1730. v9fs_path_copy(&dpath, &pathes[nwalked]);
  1731. }
  1732. }
  1733. });
  1734. /*
  1735. * Handle all the rest of this Twalk request on main thread ...
  1736. *
  1737. * NOTE: -EINTR is an exception where we deviate from the protocol spec
  1738. * and simply send a (R)Lerror response instead of bothering to assemble
  1739. * a (deducted) Rwalk response; because -EINTR is always the result of a
  1740. * Tflush request, so client would no longer wait for a response in this
  1741. * case anyway.
  1742. */
  1743. if ((err < 0 && !nwalked) || err == -EINTR) {
  1744. goto out;
  1745. }
  1746. any_err |= err = stat_to_qid(pdu, &fidst, &qid);
  1747. if (err < 0 && !nwalked) {
  1748. goto out;
  1749. }
  1750. stbuf = fidst;
  1751. /* reset dpath and path */
  1752. v9fs_path_copy(&dpath, &fidp->path);
  1753. v9fs_path_copy(&path, &fidp->path);
  1754. for (name_idx = 0; name_idx < nwalked; name_idx++) {
  1755. if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
  1756. strcmp("..", wnames[name_idx].data))
  1757. {
  1758. stbuf = stbufs[name_idx];
  1759. any_err |= err = stat_to_qid(pdu, &stbuf, &qid);
  1760. if (err < 0) {
  1761. break;
  1762. }
  1763. v9fs_path_copy(&path, &pathes[name_idx]);
  1764. v9fs_path_copy(&dpath, &path);
  1765. }
  1766. memcpy(&qids[name_idx], &qid, sizeof(qid));
  1767. }
  1768. if (any_err < 0) {
  1769. if (!name_idx) {
  1770. /* don't send any QIDs, send Rlerror instead */
  1771. goto out;
  1772. } else {
  1773. /* send QIDs (not Rlerror), but fid MUST remain unaffected */
  1774. goto send_qids;
  1775. }
  1776. }
  1777. if (fid == newfid) {
  1778. if (fidp->fid_type != P9_FID_NONE) {
  1779. err = -EINVAL;
  1780. goto out;
  1781. }
  1782. v9fs_path_write_lock(s);
  1783. v9fs_path_copy(&fidp->path, &path);
  1784. v9fs_path_unlock(s);
  1785. } else {
  1786. newfidp = alloc_fid(s, newfid);
  1787. if (newfidp == NULL) {
  1788. err = -EINVAL;
  1789. goto out;
  1790. }
  1791. newfidp->uid = fidp->uid;
  1792. v9fs_path_copy(&newfidp->path, &path);
  1793. }
  1794. send_qids:
  1795. err = v9fs_walk_marshal(pdu, name_idx, qids);
  1796. trace_v9fs_walk_return(pdu->tag, pdu->id, name_idx, qids);
  1797. out:
  1798. put_fid(pdu, fidp);
  1799. if (newfidp) {
  1800. put_fid(pdu, newfidp);
  1801. }
  1802. v9fs_path_free(&dpath);
  1803. v9fs_path_free(&path);
  1804. goto out_pdu_complete;
  1805. out_nofid_nownames:
  1806. trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames, "<?>");
  1807. out_nofid:
  1808. out_pdu_complete:
  1809. pdu_complete(pdu, err);
  1810. }
  1811. static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
  1812. {
  1813. struct statfs stbuf;
  1814. int err = v9fs_co_statfs(pdu, path, &stbuf);
  1815. return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0);
  1816. }
  1817. static void coroutine_fn v9fs_open(void *opaque)
  1818. {
  1819. int flags;
  1820. int32_t fid;
  1821. int32_t mode;
  1822. V9fsQID qid;
  1823. int iounit = 0;
  1824. ssize_t err = 0;
  1825. size_t offset = 7;
  1826. struct stat stbuf;
  1827. V9fsFidState *fidp;
  1828. V9fsPDU *pdu = opaque;
  1829. V9fsState *s = pdu->s;
  1830. g_autofree char *trace_oflags = NULL;
  1831. if (s->proto_version == V9FS_PROTO_2000L) {
  1832. err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
  1833. } else {
  1834. uint8_t modebyte;
  1835. err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
  1836. mode = modebyte;
  1837. }
  1838. if (err < 0) {
  1839. goto out_nofid;
  1840. }
  1841. if (trace_event_get_state_backends(TRACE_V9FS_OPEN)) {
  1842. trace_oflags = qemu_open_flags_tostr(
  1843. (s->proto_version == V9FS_PROTO_2000L) ?
  1844. dotl_to_open_flags(mode) : omode_to_uflags(mode)
  1845. );
  1846. trace_v9fs_open(pdu->tag, pdu->id, fid, mode, trace_oflags);
  1847. }
  1848. fidp = get_fid(pdu, fid);
  1849. if (fidp == NULL) {
  1850. err = -ENOENT;
  1851. goto out_nofid;
  1852. }
  1853. if (fidp->fid_type != P9_FID_NONE) {
  1854. err = -EINVAL;
  1855. goto out;
  1856. }
  1857. err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  1858. if (err < 0) {
  1859. goto out;
  1860. }
  1861. err = stat_to_qid(pdu, &stbuf, &qid);
  1862. if (err < 0) {
  1863. goto out;
  1864. }
  1865. if (S_ISDIR(stbuf.st_mode)) {
  1866. err = v9fs_co_opendir(pdu, fidp);
  1867. if (err < 0) {
  1868. goto out;
  1869. }
  1870. fidp->fid_type = P9_FID_DIR;
  1871. err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
  1872. if (err < 0) {
  1873. goto out;
  1874. }
  1875. err += offset;
  1876. } else {
  1877. if (s->proto_version == V9FS_PROTO_2000L) {
  1878. flags = get_dotl_openflags(s, mode);
  1879. } else {
  1880. flags = omode_to_uflags(mode);
  1881. }
  1882. if (is_ro_export(&s->ctx)) {
  1883. if (mode & O_WRONLY || mode & O_RDWR ||
  1884. mode & O_APPEND || mode & O_TRUNC) {
  1885. err = -EROFS;
  1886. goto out;
  1887. }
  1888. }
  1889. err = v9fs_co_open(pdu, fidp, flags);
  1890. if (err < 0) {
  1891. goto out;
  1892. }
  1893. fidp->fid_type = P9_FID_FILE;
  1894. fidp->open_flags = flags;
  1895. if (flags & O_EXCL) {
  1896. /*
  1897. * We let the host file system do O_EXCL check
  1898. * We should not reclaim such fd
  1899. */
  1900. fidp->flags |= FID_NON_RECLAIMABLE;
  1901. }
  1902. iounit = get_iounit(pdu, &fidp->path);
  1903. err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
  1904. if (err < 0) {
  1905. goto out;
  1906. }
  1907. err += offset;
  1908. }
  1909. trace_v9fs_open_return(pdu->tag, pdu->id,
  1910. qid.type, qid.version, qid.path, iounit);
  1911. out:
  1912. put_fid(pdu, fidp);
  1913. out_nofid:
  1914. pdu_complete(pdu, err);
  1915. }
  1916. static void coroutine_fn v9fs_lcreate(void *opaque)
  1917. {
  1918. int32_t dfid, flags, mode;
  1919. gid_t gid;
  1920. ssize_t err = 0;
  1921. ssize_t offset = 7;
  1922. V9fsString name;
  1923. V9fsFidState *fidp;
  1924. struct stat stbuf;
  1925. V9fsQID qid;
  1926. int32_t iounit;
  1927. V9fsPDU *pdu = opaque;
  1928. v9fs_string_init(&name);
  1929. err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
  1930. &name, &flags, &mode, &gid);
  1931. if (err < 0) {
  1932. goto out_nofid;
  1933. }
  1934. trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
  1935. if (name_is_illegal(name.data)) {
  1936. err = -ENOENT;
  1937. goto out_nofid;
  1938. }
  1939. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  1940. err = -EEXIST;
  1941. goto out_nofid;
  1942. }
  1943. fidp = get_fid(pdu, dfid);
  1944. if (fidp == NULL) {
  1945. err = -ENOENT;
  1946. goto out_nofid;
  1947. }
  1948. if (fidp->fid_type != P9_FID_NONE) {
  1949. err = -EINVAL;
  1950. goto out;
  1951. }
  1952. flags = get_dotl_openflags(pdu->s, flags);
  1953. err = v9fs_co_open2(pdu, fidp, &name, gid,
  1954. flags | O_CREAT, mode, &stbuf);
  1955. if (err < 0) {
  1956. goto out;
  1957. }
  1958. fidp->fid_type = P9_FID_FILE;
  1959. fidp->open_flags = flags;
  1960. if (flags & O_EXCL) {
  1961. /*
  1962. * We let the host file system do O_EXCL check
  1963. * We should not reclaim such fd
  1964. */
  1965. fidp->flags |= FID_NON_RECLAIMABLE;
  1966. }
  1967. iounit = get_iounit(pdu, &fidp->path);
  1968. err = stat_to_qid(pdu, &stbuf, &qid);
  1969. if (err < 0) {
  1970. goto out;
  1971. }
  1972. err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
  1973. if (err < 0) {
  1974. goto out;
  1975. }
  1976. err += offset;
  1977. trace_v9fs_lcreate_return(pdu->tag, pdu->id,
  1978. qid.type, qid.version, qid.path, iounit);
  1979. out:
  1980. put_fid(pdu, fidp);
  1981. out_nofid:
  1982. pdu_complete(pdu, err);
  1983. v9fs_string_free(&name);
  1984. }
  1985. static void coroutine_fn v9fs_fsync(void *opaque)
  1986. {
  1987. int err;
  1988. int32_t fid;
  1989. int datasync;
  1990. size_t offset = 7;
  1991. V9fsFidState *fidp;
  1992. V9fsPDU *pdu = opaque;
  1993. err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
  1994. if (err < 0) {
  1995. goto out_nofid;
  1996. }
  1997. trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
  1998. fidp = get_fid(pdu, fid);
  1999. if (fidp == NULL) {
  2000. err = -ENOENT;
  2001. goto out_nofid;
  2002. }
  2003. err = v9fs_co_fsync(pdu, fidp, datasync);
  2004. if (!err) {
  2005. err = offset;
  2006. }
  2007. put_fid(pdu, fidp);
  2008. out_nofid:
  2009. pdu_complete(pdu, err);
  2010. }
  2011. static void coroutine_fn v9fs_clunk(void *opaque)
  2012. {
  2013. int err;
  2014. int32_t fid;
  2015. size_t offset = 7;
  2016. V9fsFidState *fidp;
  2017. V9fsPDU *pdu = opaque;
  2018. V9fsState *s = pdu->s;
  2019. err = pdu_unmarshal(pdu, offset, "d", &fid);
  2020. if (err < 0) {
  2021. goto out_nofid;
  2022. }
  2023. trace_v9fs_clunk(pdu->tag, pdu->id, fid);
  2024. fidp = clunk_fid(s, fid);
  2025. if (fidp == NULL) {
  2026. err = -ENOENT;
  2027. goto out_nofid;
  2028. }
  2029. /*
  2030. * Bump the ref so that put_fid will
  2031. * free the fid.
  2032. */
  2033. fidp->ref++;
  2034. err = put_fid(pdu, fidp);
  2035. if (!err) {
  2036. err = offset;
  2037. }
  2038. out_nofid:
  2039. pdu_complete(pdu, err);
  2040. }
  2041. /*
  2042. * Create a QEMUIOVector for a sub-region of PDU iovecs
  2043. *
  2044. * @qiov: uninitialized QEMUIOVector
  2045. * @skip: number of bytes to skip from beginning of PDU
  2046. * @size: number of bytes to include
  2047. * @is_write: true - write, false - read
  2048. *
  2049. * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
  2050. * with qemu_iovec_destroy().
  2051. */
  2052. static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
  2053. size_t skip, size_t size,
  2054. bool is_write)
  2055. {
  2056. QEMUIOVector elem;
  2057. struct iovec *iov;
  2058. unsigned int niov;
  2059. if (is_write) {
  2060. pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
  2061. } else {
  2062. pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
  2063. }
  2064. qemu_iovec_init_external(&elem, iov, niov);
  2065. qemu_iovec_init(qiov, niov);
  2066. qemu_iovec_concat(qiov, &elem, skip, size);
  2067. }
  2068. static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
  2069. uint64_t off, uint32_t max_count)
  2070. {
  2071. ssize_t err;
  2072. size_t offset = 7;
  2073. uint64_t read_count;
  2074. QEMUIOVector qiov_full;
  2075. if (fidp->fs.xattr.len < off) {
  2076. read_count = 0;
  2077. } else {
  2078. read_count = fidp->fs.xattr.len - off;
  2079. }
  2080. if (read_count > max_count) {
  2081. read_count = max_count;
  2082. }
  2083. err = pdu_marshal(pdu, offset, "d", read_count);
  2084. if (err < 0) {
  2085. return err;
  2086. }
  2087. offset += err;
  2088. v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
  2089. err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
  2090. ((char *)fidp->fs.xattr.value) + off,
  2091. read_count);
  2092. qemu_iovec_destroy(&qiov_full);
  2093. if (err < 0) {
  2094. return err;
  2095. }
  2096. offset += err;
  2097. return offset;
  2098. }
  2099. static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
  2100. V9fsFidState *fidp,
  2101. uint32_t max_count)
  2102. {
  2103. V9fsPath path;
  2104. V9fsStat v9stat;
  2105. int len, err = 0;
  2106. int32_t count = 0;
  2107. struct stat stbuf;
  2108. off_t saved_dir_pos;
  2109. struct dirent *dent;
  2110. /* save the directory position */
  2111. saved_dir_pos = v9fs_co_telldir(pdu, fidp);
  2112. if (saved_dir_pos < 0) {
  2113. return saved_dir_pos;
  2114. }
  2115. while (1) {
  2116. v9fs_path_init(&path);
  2117. v9fs_readdir_lock(&fidp->fs.dir);
  2118. err = v9fs_co_readdir(pdu, fidp, &dent);
  2119. if (err || !dent) {
  2120. break;
  2121. }
  2122. err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
  2123. if (err < 0) {
  2124. break;
  2125. }
  2126. err = v9fs_co_lstat(pdu, &path, &stbuf);
  2127. if (err < 0) {
  2128. break;
  2129. }
  2130. err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
  2131. if (err < 0) {
  2132. break;
  2133. }
  2134. if ((count + v9stat.size + 2) > max_count) {
  2135. v9fs_readdir_unlock(&fidp->fs.dir);
  2136. /* Ran out of buffer. Set dir back to old position and return */
  2137. v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
  2138. v9fs_stat_free(&v9stat);
  2139. v9fs_path_free(&path);
  2140. return count;
  2141. }
  2142. /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
  2143. len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
  2144. v9fs_readdir_unlock(&fidp->fs.dir);
  2145. if (len < 0) {
  2146. v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
  2147. v9fs_stat_free(&v9stat);
  2148. v9fs_path_free(&path);
  2149. return len;
  2150. }
  2151. count += len;
  2152. v9fs_stat_free(&v9stat);
  2153. v9fs_path_free(&path);
  2154. saved_dir_pos = qemu_dirent_off(dent);
  2155. }
  2156. v9fs_readdir_unlock(&fidp->fs.dir);
  2157. v9fs_path_free(&path);
  2158. if (err < 0) {
  2159. return err;
  2160. }
  2161. return count;
  2162. }
  2163. static void coroutine_fn v9fs_read(void *opaque)
  2164. {
  2165. int32_t fid;
  2166. uint64_t off;
  2167. ssize_t err = 0;
  2168. int32_t count = 0;
  2169. size_t offset = 7;
  2170. uint32_t max_count;
  2171. V9fsFidState *fidp;
  2172. V9fsPDU *pdu = opaque;
  2173. V9fsState *s = pdu->s;
  2174. err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
  2175. if (err < 0) {
  2176. goto out_nofid;
  2177. }
  2178. trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
  2179. fidp = get_fid(pdu, fid);
  2180. if (fidp == NULL) {
  2181. err = -EINVAL;
  2182. goto out_nofid;
  2183. }
  2184. if (fidp->fid_type == P9_FID_DIR) {
  2185. if (s->proto_version != V9FS_PROTO_2000U) {
  2186. warn_report_once(
  2187. "9p: bad client: T_read request on directory only expected "
  2188. "with 9P2000.u protocol version"
  2189. );
  2190. err = -EOPNOTSUPP;
  2191. goto out;
  2192. }
  2193. if (off == 0) {
  2194. v9fs_co_rewinddir(pdu, fidp);
  2195. }
  2196. count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
  2197. if (count < 0) {
  2198. err = count;
  2199. goto out;
  2200. }
  2201. err = pdu_marshal(pdu, offset, "d", count);
  2202. if (err < 0) {
  2203. goto out;
  2204. }
  2205. err += offset + count;
  2206. } else if (fidp->fid_type == P9_FID_FILE) {
  2207. QEMUIOVector qiov_full;
  2208. QEMUIOVector qiov;
  2209. int32_t len;
  2210. v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
  2211. qemu_iovec_init(&qiov, qiov_full.niov);
  2212. do {
  2213. qemu_iovec_reset(&qiov);
  2214. qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
  2215. if (0) {
  2216. print_sg(qiov.iov, qiov.niov);
  2217. }
  2218. /* Loop in case of EINTR */
  2219. do {
  2220. len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
  2221. if (len >= 0) {
  2222. off += len;
  2223. count += len;
  2224. }
  2225. } while (len == -EINTR && !pdu->cancelled);
  2226. if (len < 0) {
  2227. /* IO error return the error */
  2228. err = len;
  2229. goto out_free_iovec;
  2230. }
  2231. } while (count < max_count && len > 0);
  2232. err = pdu_marshal(pdu, offset, "d", count);
  2233. if (err < 0) {
  2234. goto out_free_iovec;
  2235. }
  2236. err += offset + count;
  2237. out_free_iovec:
  2238. qemu_iovec_destroy(&qiov);
  2239. qemu_iovec_destroy(&qiov_full);
  2240. } else if (fidp->fid_type == P9_FID_XATTR) {
  2241. err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
  2242. } else {
  2243. err = -EINVAL;
  2244. }
  2245. trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
  2246. out:
  2247. put_fid(pdu, fidp);
  2248. out_nofid:
  2249. pdu_complete(pdu, err);
  2250. }
  2251. /**
  2252. * v9fs_readdir_response_size() - Returns size required in Rreaddir response
  2253. * for the passed dirent @name.
  2254. *
  2255. * @name: directory entry's name (i.e. file name, directory name)
  2256. * Return: required size in bytes
  2257. */
  2258. size_t v9fs_readdir_response_size(V9fsString *name)
  2259. {
  2260. /*
  2261. * Size of each dirent on the wire: size of qid (13) + size of offset (8)
  2262. * size of type (1) + size of name.size (2) + strlen(name.data)
  2263. */
  2264. return 24 + v9fs_string_size(name);
  2265. }
  2266. static void v9fs_free_dirents(struct V9fsDirEnt *e)
  2267. {
  2268. struct V9fsDirEnt *next = NULL;
  2269. for (; e; e = next) {
  2270. next = e->next;
  2271. g_free(e->dent);
  2272. g_free(e->st);
  2273. g_free(e);
  2274. }
  2275. }
  2276. static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
  2277. off_t offset, int32_t max_count)
  2278. {
  2279. size_t size;
  2280. V9fsQID qid;
  2281. V9fsString name;
  2282. int len, err = 0;
  2283. int32_t count = 0;
  2284. off_t off;
  2285. struct dirent *dent;
  2286. struct stat *st;
  2287. struct V9fsDirEnt *entries = NULL;
  2288. /*
  2289. * inode remapping requires the device id, which in turn might be
  2290. * different for different directory entries, so if inode remapping is
  2291. * enabled we have to make a full stat for each directory entry
  2292. */
  2293. const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES;
  2294. /*
  2295. * Fetch all required directory entries altogether on a background IO
  2296. * thread from fs driver. We don't want to do that for each entry
  2297. * individually, because hopping between threads (this main IO thread
  2298. * and background IO driver thread) would sum up to huge latencies.
  2299. */
  2300. count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count,
  2301. dostat);
  2302. if (count < 0) {
  2303. err = count;
  2304. count = 0;
  2305. goto out;
  2306. }
  2307. count = 0;
  2308. for (struct V9fsDirEnt *e = entries; e; e = e->next) {
  2309. dent = e->dent;
  2310. if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
  2311. st = e->st;
  2312. /* e->st should never be NULL, but just to be sure */
  2313. if (!st) {
  2314. err = -1;
  2315. break;
  2316. }
  2317. /* remap inode */
  2318. err = stat_to_qid(pdu, st, &qid);
  2319. if (err < 0) {
  2320. break;
  2321. }
  2322. } else {
  2323. /*
  2324. * Fill up just the path field of qid because the client uses
  2325. * only that. To fill the entire qid structure we will have
  2326. * to stat each dirent found, which is expensive. For the
  2327. * latter reason we don't call stat_to_qid() here. Only drawback
  2328. * is that no multi-device export detection of stat_to_qid()
  2329. * would be done and provided as error to the user here. But
  2330. * user would get that error anyway when accessing those
  2331. * files/dirs through other ways.
  2332. */
  2333. size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
  2334. memcpy(&qid.path, &dent->d_ino, size);
  2335. /* Fill the other fields with dummy values */
  2336. qid.type = 0;
  2337. qid.version = 0;
  2338. }
  2339. off = qemu_dirent_off(dent);
  2340. v9fs_string_init(&name);
  2341. v9fs_string_sprintf(&name, "%s", dent->d_name);
  2342. /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
  2343. len = pdu_marshal(pdu, 11 + count, "Qqbs",
  2344. &qid, off,
  2345. dent->d_type, &name);
  2346. v9fs_string_free(&name);
  2347. if (len < 0) {
  2348. err = len;
  2349. break;
  2350. }
  2351. count += len;
  2352. }
  2353. out:
  2354. v9fs_free_dirents(entries);
  2355. if (err < 0) {
  2356. return err;
  2357. }
  2358. return count;
  2359. }
  2360. static void coroutine_fn v9fs_readdir(void *opaque)
  2361. {
  2362. int32_t fid;
  2363. V9fsFidState *fidp;
  2364. ssize_t retval = 0;
  2365. size_t offset = 7;
  2366. uint64_t initial_offset;
  2367. int32_t count;
  2368. uint32_t max_count;
  2369. V9fsPDU *pdu = opaque;
  2370. V9fsState *s = pdu->s;
  2371. retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
  2372. &initial_offset, &max_count);
  2373. if (retval < 0) {
  2374. goto out_nofid;
  2375. }
  2376. trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
  2377. /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
  2378. if (max_count > s->msize - 11) {
  2379. max_count = s->msize - 11;
  2380. warn_report_once(
  2381. "9p: bad client: T_readdir with count > msize - 11"
  2382. );
  2383. }
  2384. fidp = get_fid(pdu, fid);
  2385. if (fidp == NULL) {
  2386. retval = -EINVAL;
  2387. goto out_nofid;
  2388. }
  2389. if (fidp->fid_type != P9_FID_DIR) {
  2390. warn_report_once("9p: bad client: T_readdir on non-directory stream");
  2391. retval = -ENOTDIR;
  2392. goto out;
  2393. }
  2394. if (!fidp->fs.dir.stream) {
  2395. retval = -EINVAL;
  2396. goto out;
  2397. }
  2398. if (s->proto_version != V9FS_PROTO_2000L) {
  2399. warn_report_once(
  2400. "9p: bad client: T_readdir request only expected with 9P2000.L "
  2401. "protocol version"
  2402. );
  2403. retval = -EOPNOTSUPP;
  2404. goto out;
  2405. }
  2406. count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count);
  2407. if (count < 0) {
  2408. retval = count;
  2409. goto out;
  2410. }
  2411. retval = pdu_marshal(pdu, offset, "d", count);
  2412. if (retval < 0) {
  2413. goto out;
  2414. }
  2415. retval += count + offset;
  2416. trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
  2417. out:
  2418. put_fid(pdu, fidp);
  2419. out_nofid:
  2420. pdu_complete(pdu, retval);
  2421. }
  2422. static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
  2423. uint64_t off, uint32_t count,
  2424. struct iovec *sg, int cnt)
  2425. {
  2426. int i, to_copy;
  2427. ssize_t err = 0;
  2428. uint64_t write_count;
  2429. size_t offset = 7;
  2430. if (fidp->fs.xattr.len < off) {
  2431. return -ENOSPC;
  2432. }
  2433. write_count = fidp->fs.xattr.len - off;
  2434. if (write_count > count) {
  2435. write_count = count;
  2436. }
  2437. err = pdu_marshal(pdu, offset, "d", write_count);
  2438. if (err < 0) {
  2439. return err;
  2440. }
  2441. err += offset;
  2442. fidp->fs.xattr.copied_len += write_count;
  2443. /*
  2444. * Now copy the content from sg list
  2445. */
  2446. for (i = 0; i < cnt; i++) {
  2447. if (write_count > sg[i].iov_len) {
  2448. to_copy = sg[i].iov_len;
  2449. } else {
  2450. to_copy = write_count;
  2451. }
  2452. memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
  2453. /* updating vs->off since we are not using below */
  2454. off += to_copy;
  2455. write_count -= to_copy;
  2456. }
  2457. return err;
  2458. }
  2459. static void coroutine_fn v9fs_write(void *opaque)
  2460. {
  2461. ssize_t err;
  2462. int32_t fid;
  2463. uint64_t off;
  2464. uint32_t count;
  2465. int32_t len = 0;
  2466. int32_t total = 0;
  2467. size_t offset = 7;
  2468. V9fsFidState *fidp;
  2469. V9fsPDU *pdu = opaque;
  2470. V9fsState *s = pdu->s;
  2471. QEMUIOVector qiov_full;
  2472. QEMUIOVector qiov;
  2473. err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
  2474. if (err < 0) {
  2475. pdu_complete(pdu, err);
  2476. return;
  2477. }
  2478. offset += err;
  2479. v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
  2480. trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
  2481. fidp = get_fid(pdu, fid);
  2482. if (fidp == NULL) {
  2483. err = -EINVAL;
  2484. goto out_nofid;
  2485. }
  2486. if (fidp->fid_type == P9_FID_FILE) {
  2487. if (fidp->fs.fd == -1) {
  2488. err = -EINVAL;
  2489. goto out;
  2490. }
  2491. } else if (fidp->fid_type == P9_FID_XATTR) {
  2492. /*
  2493. * setxattr operation
  2494. */
  2495. err = v9fs_xattr_write(s, pdu, fidp, off, count,
  2496. qiov_full.iov, qiov_full.niov);
  2497. goto out;
  2498. } else {
  2499. err = -EINVAL;
  2500. goto out;
  2501. }
  2502. qemu_iovec_init(&qiov, qiov_full.niov);
  2503. do {
  2504. qemu_iovec_reset(&qiov);
  2505. qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
  2506. if (0) {
  2507. print_sg(qiov.iov, qiov.niov);
  2508. }
  2509. /* Loop in case of EINTR */
  2510. do {
  2511. len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
  2512. if (len >= 0) {
  2513. off += len;
  2514. total += len;
  2515. }
  2516. } while (len == -EINTR && !pdu->cancelled);
  2517. if (len < 0) {
  2518. /* IO error return the error */
  2519. err = len;
  2520. goto out_qiov;
  2521. }
  2522. } while (total < count && len > 0);
  2523. offset = 7;
  2524. err = pdu_marshal(pdu, offset, "d", total);
  2525. if (err < 0) {
  2526. goto out_qiov;
  2527. }
  2528. err += offset;
  2529. trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
  2530. out_qiov:
  2531. qemu_iovec_destroy(&qiov);
  2532. out:
  2533. put_fid(pdu, fidp);
  2534. out_nofid:
  2535. qemu_iovec_destroy(&qiov_full);
  2536. pdu_complete(pdu, err);
  2537. }
  2538. static void coroutine_fn v9fs_create(void *opaque)
  2539. {
  2540. int32_t fid;
  2541. int err = 0;
  2542. size_t offset = 7;
  2543. V9fsFidState *fidp;
  2544. V9fsQID qid;
  2545. int32_t perm;
  2546. int8_t mode;
  2547. V9fsPath path;
  2548. struct stat stbuf;
  2549. V9fsString name;
  2550. V9fsString extension;
  2551. int iounit;
  2552. V9fsPDU *pdu = opaque;
  2553. V9fsState *s = pdu->s;
  2554. v9fs_path_init(&path);
  2555. v9fs_string_init(&name);
  2556. v9fs_string_init(&extension);
  2557. err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
  2558. &perm, &mode, &extension);
  2559. if (err < 0) {
  2560. goto out_nofid;
  2561. }
  2562. trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
  2563. if (name_is_illegal(name.data)) {
  2564. err = -ENOENT;
  2565. goto out_nofid;
  2566. }
  2567. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  2568. err = -EEXIST;
  2569. goto out_nofid;
  2570. }
  2571. fidp = get_fid(pdu, fid);
  2572. if (fidp == NULL) {
  2573. err = -EINVAL;
  2574. goto out_nofid;
  2575. }
  2576. if (fidp->fid_type != P9_FID_NONE) {
  2577. err = -EINVAL;
  2578. goto out;
  2579. }
  2580. if (perm & P9_STAT_MODE_DIR) {
  2581. err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
  2582. fidp->uid, -1, &stbuf);
  2583. if (err < 0) {
  2584. goto out;
  2585. }
  2586. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2587. if (err < 0) {
  2588. goto out;
  2589. }
  2590. v9fs_path_write_lock(s);
  2591. v9fs_path_copy(&fidp->path, &path);
  2592. v9fs_path_unlock(s);
  2593. err = v9fs_co_opendir(pdu, fidp);
  2594. if (err < 0) {
  2595. goto out;
  2596. }
  2597. fidp->fid_type = P9_FID_DIR;
  2598. } else if (perm & P9_STAT_MODE_SYMLINK) {
  2599. err = v9fs_co_symlink(pdu, fidp, &name,
  2600. extension.data, -1 , &stbuf);
  2601. if (err < 0) {
  2602. goto out;
  2603. }
  2604. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2605. if (err < 0) {
  2606. goto out;
  2607. }
  2608. v9fs_path_write_lock(s);
  2609. v9fs_path_copy(&fidp->path, &path);
  2610. v9fs_path_unlock(s);
  2611. } else if (perm & P9_STAT_MODE_LINK) {
  2612. int32_t ofid = atoi(extension.data);
  2613. V9fsFidState *ofidp = get_fid(pdu, ofid);
  2614. if (ofidp == NULL) {
  2615. err = -EINVAL;
  2616. goto out;
  2617. }
  2618. err = v9fs_co_link(pdu, ofidp, fidp, &name);
  2619. put_fid(pdu, ofidp);
  2620. if (err < 0) {
  2621. goto out;
  2622. }
  2623. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2624. if (err < 0) {
  2625. fidp->fid_type = P9_FID_NONE;
  2626. goto out;
  2627. }
  2628. v9fs_path_write_lock(s);
  2629. v9fs_path_copy(&fidp->path, &path);
  2630. v9fs_path_unlock(s);
  2631. err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  2632. if (err < 0) {
  2633. fidp->fid_type = P9_FID_NONE;
  2634. goto out;
  2635. }
  2636. } else if (perm & P9_STAT_MODE_DEVICE) {
  2637. char ctype;
  2638. uint32_t major, minor;
  2639. mode_t nmode = 0;
  2640. if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
  2641. err = -errno;
  2642. goto out;
  2643. }
  2644. switch (ctype) {
  2645. case 'c':
  2646. nmode = S_IFCHR;
  2647. break;
  2648. case 'b':
  2649. nmode = S_IFBLK;
  2650. break;
  2651. default:
  2652. err = -EIO;
  2653. goto out;
  2654. }
  2655. nmode |= perm & 0777;
  2656. err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
  2657. makedev(major, minor), nmode, &stbuf);
  2658. if (err < 0) {
  2659. goto out;
  2660. }
  2661. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2662. if (err < 0) {
  2663. goto out;
  2664. }
  2665. v9fs_path_write_lock(s);
  2666. v9fs_path_copy(&fidp->path, &path);
  2667. v9fs_path_unlock(s);
  2668. } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
  2669. err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
  2670. 0, S_IFIFO | (perm & 0777), &stbuf);
  2671. if (err < 0) {
  2672. goto out;
  2673. }
  2674. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2675. if (err < 0) {
  2676. goto out;
  2677. }
  2678. v9fs_path_write_lock(s);
  2679. v9fs_path_copy(&fidp->path, &path);
  2680. v9fs_path_unlock(s);
  2681. } else if (perm & P9_STAT_MODE_SOCKET) {
  2682. err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
  2683. 0, S_IFSOCK | (perm & 0777), &stbuf);
  2684. if (err < 0) {
  2685. goto out;
  2686. }
  2687. err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
  2688. if (err < 0) {
  2689. goto out;
  2690. }
  2691. v9fs_path_write_lock(s);
  2692. v9fs_path_copy(&fidp->path, &path);
  2693. v9fs_path_unlock(s);
  2694. } else {
  2695. err = v9fs_co_open2(pdu, fidp, &name, -1,
  2696. omode_to_uflags(mode) | O_CREAT, perm, &stbuf);
  2697. if (err < 0) {
  2698. goto out;
  2699. }
  2700. fidp->fid_type = P9_FID_FILE;
  2701. fidp->open_flags = omode_to_uflags(mode);
  2702. if (fidp->open_flags & O_EXCL) {
  2703. /*
  2704. * We let the host file system do O_EXCL check
  2705. * We should not reclaim such fd
  2706. */
  2707. fidp->flags |= FID_NON_RECLAIMABLE;
  2708. }
  2709. }
  2710. iounit = get_iounit(pdu, &fidp->path);
  2711. err = stat_to_qid(pdu, &stbuf, &qid);
  2712. if (err < 0) {
  2713. goto out;
  2714. }
  2715. err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
  2716. if (err < 0) {
  2717. goto out;
  2718. }
  2719. err += offset;
  2720. trace_v9fs_create_return(pdu->tag, pdu->id,
  2721. qid.type, qid.version, qid.path, iounit);
  2722. out:
  2723. put_fid(pdu, fidp);
  2724. out_nofid:
  2725. pdu_complete(pdu, err);
  2726. v9fs_string_free(&name);
  2727. v9fs_string_free(&extension);
  2728. v9fs_path_free(&path);
  2729. }
  2730. static void coroutine_fn v9fs_symlink(void *opaque)
  2731. {
  2732. V9fsPDU *pdu = opaque;
  2733. V9fsString name;
  2734. V9fsString symname;
  2735. V9fsFidState *dfidp;
  2736. V9fsQID qid;
  2737. struct stat stbuf;
  2738. int32_t dfid;
  2739. int err = 0;
  2740. gid_t gid;
  2741. size_t offset = 7;
  2742. v9fs_string_init(&name);
  2743. v9fs_string_init(&symname);
  2744. err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
  2745. if (err < 0) {
  2746. goto out_nofid;
  2747. }
  2748. trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
  2749. if (name_is_illegal(name.data)) {
  2750. err = -ENOENT;
  2751. goto out_nofid;
  2752. }
  2753. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  2754. err = -EEXIST;
  2755. goto out_nofid;
  2756. }
  2757. dfidp = get_fid(pdu, dfid);
  2758. if (dfidp == NULL) {
  2759. err = -EINVAL;
  2760. goto out_nofid;
  2761. }
  2762. err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
  2763. if (err < 0) {
  2764. goto out;
  2765. }
  2766. err = stat_to_qid(pdu, &stbuf, &qid);
  2767. if (err < 0) {
  2768. goto out;
  2769. }
  2770. err = pdu_marshal(pdu, offset, "Q", &qid);
  2771. if (err < 0) {
  2772. goto out;
  2773. }
  2774. err += offset;
  2775. trace_v9fs_symlink_return(pdu->tag, pdu->id,
  2776. qid.type, qid.version, qid.path);
  2777. out:
  2778. put_fid(pdu, dfidp);
  2779. out_nofid:
  2780. pdu_complete(pdu, err);
  2781. v9fs_string_free(&name);
  2782. v9fs_string_free(&symname);
  2783. }
  2784. static void coroutine_fn v9fs_flush(void *opaque)
  2785. {
  2786. ssize_t err;
  2787. int16_t tag;
  2788. size_t offset = 7;
  2789. V9fsPDU *cancel_pdu = NULL;
  2790. V9fsPDU *pdu = opaque;
  2791. V9fsState *s = pdu->s;
  2792. err = pdu_unmarshal(pdu, offset, "w", &tag);
  2793. if (err < 0) {
  2794. pdu_complete(pdu, err);
  2795. return;
  2796. }
  2797. trace_v9fs_flush(pdu->tag, pdu->id, tag);
  2798. if (pdu->tag == tag) {
  2799. warn_report("the guest sent a self-referencing 9P flush request");
  2800. } else {
  2801. QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
  2802. if (cancel_pdu->tag == tag) {
  2803. break;
  2804. }
  2805. }
  2806. }
  2807. if (cancel_pdu) {
  2808. cancel_pdu->cancelled = 1;
  2809. /*
  2810. * Wait for pdu to complete.
  2811. */
  2812. qemu_co_queue_wait(&cancel_pdu->complete, NULL);
  2813. if (!qemu_co_queue_next(&cancel_pdu->complete)) {
  2814. cancel_pdu->cancelled = 0;
  2815. pdu_free(cancel_pdu);
  2816. }
  2817. }
  2818. pdu_complete(pdu, 7);
  2819. }
  2820. static void coroutine_fn v9fs_link(void *opaque)
  2821. {
  2822. V9fsPDU *pdu = opaque;
  2823. int32_t dfid, oldfid;
  2824. V9fsFidState *dfidp, *oldfidp;
  2825. V9fsString name;
  2826. size_t offset = 7;
  2827. int err = 0;
  2828. v9fs_string_init(&name);
  2829. err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
  2830. if (err < 0) {
  2831. goto out_nofid;
  2832. }
  2833. trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
  2834. if (name_is_illegal(name.data)) {
  2835. err = -ENOENT;
  2836. goto out_nofid;
  2837. }
  2838. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  2839. err = -EEXIST;
  2840. goto out_nofid;
  2841. }
  2842. dfidp = get_fid(pdu, dfid);
  2843. if (dfidp == NULL) {
  2844. err = -ENOENT;
  2845. goto out_nofid;
  2846. }
  2847. oldfidp = get_fid(pdu, oldfid);
  2848. if (oldfidp == NULL) {
  2849. err = -ENOENT;
  2850. goto out;
  2851. }
  2852. err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
  2853. if (!err) {
  2854. err = offset;
  2855. }
  2856. put_fid(pdu, oldfidp);
  2857. out:
  2858. put_fid(pdu, dfidp);
  2859. out_nofid:
  2860. v9fs_string_free(&name);
  2861. pdu_complete(pdu, err);
  2862. }
  2863. /* Only works with path name based fid */
  2864. static void coroutine_fn v9fs_remove(void *opaque)
  2865. {
  2866. int32_t fid;
  2867. int err = 0;
  2868. size_t offset = 7;
  2869. V9fsFidState *fidp;
  2870. V9fsPDU *pdu = opaque;
  2871. err = pdu_unmarshal(pdu, offset, "d", &fid);
  2872. if (err < 0) {
  2873. goto out_nofid;
  2874. }
  2875. trace_v9fs_remove(pdu->tag, pdu->id, fid);
  2876. fidp = get_fid(pdu, fid);
  2877. if (fidp == NULL) {
  2878. err = -EINVAL;
  2879. goto out_nofid;
  2880. }
  2881. /* if fs driver is not path based, return EOPNOTSUPP */
  2882. if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
  2883. err = -EOPNOTSUPP;
  2884. goto out_err;
  2885. }
  2886. /*
  2887. * IF the file is unlinked, we cannot reopen
  2888. * the file later. So don't reclaim fd
  2889. */
  2890. err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
  2891. if (err < 0) {
  2892. goto out_err;
  2893. }
  2894. err = v9fs_co_remove(pdu, &fidp->path);
  2895. if (!err) {
  2896. err = offset;
  2897. }
  2898. out_err:
  2899. /* For TREMOVE we need to clunk the fid even on failed remove */
  2900. clunk_fid(pdu->s, fidp->fid);
  2901. put_fid(pdu, fidp);
  2902. out_nofid:
  2903. pdu_complete(pdu, err);
  2904. }
  2905. static void coroutine_fn v9fs_unlinkat(void *opaque)
  2906. {
  2907. int err = 0;
  2908. V9fsString name;
  2909. int32_t dfid, flags, rflags = 0;
  2910. size_t offset = 7;
  2911. V9fsPath path;
  2912. V9fsFidState *dfidp;
  2913. V9fsPDU *pdu = opaque;
  2914. v9fs_string_init(&name);
  2915. err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
  2916. if (err < 0) {
  2917. goto out_nofid;
  2918. }
  2919. if (name_is_illegal(name.data)) {
  2920. err = -ENOENT;
  2921. goto out_nofid;
  2922. }
  2923. if (!strcmp(".", name.data)) {
  2924. err = -EINVAL;
  2925. goto out_nofid;
  2926. }
  2927. if (!strcmp("..", name.data)) {
  2928. err = -ENOTEMPTY;
  2929. goto out_nofid;
  2930. }
  2931. if (flags & ~P9_DOTL_AT_REMOVEDIR) {
  2932. err = -EINVAL;
  2933. goto out_nofid;
  2934. }
  2935. if (flags & P9_DOTL_AT_REMOVEDIR) {
  2936. rflags |= AT_REMOVEDIR;
  2937. }
  2938. dfidp = get_fid(pdu, dfid);
  2939. if (dfidp == NULL) {
  2940. err = -EINVAL;
  2941. goto out_nofid;
  2942. }
  2943. /*
  2944. * IF the file is unlinked, we cannot reopen
  2945. * the file later. So don't reclaim fd
  2946. */
  2947. v9fs_path_init(&path);
  2948. err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
  2949. if (err < 0) {
  2950. goto out_err;
  2951. }
  2952. err = v9fs_mark_fids_unreclaim(pdu, &path);
  2953. if (err < 0) {
  2954. goto out_err;
  2955. }
  2956. err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
  2957. if (!err) {
  2958. err = offset;
  2959. }
  2960. out_err:
  2961. put_fid(pdu, dfidp);
  2962. v9fs_path_free(&path);
  2963. out_nofid:
  2964. pdu_complete(pdu, err);
  2965. v9fs_string_free(&name);
  2966. }
  2967. /* Only works with path name based fid */
  2968. static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
  2969. int32_t newdirfid,
  2970. V9fsString *name)
  2971. {
  2972. int err = 0;
  2973. V9fsPath new_path;
  2974. V9fsFidState *tfidp;
  2975. V9fsState *s = pdu->s;
  2976. V9fsFidState *dirfidp = NULL;
  2977. GHashTableIter iter;
  2978. gpointer fid;
  2979. v9fs_path_init(&new_path);
  2980. if (newdirfid != -1) {
  2981. dirfidp = get_fid(pdu, newdirfid);
  2982. if (dirfidp == NULL) {
  2983. return -ENOENT;
  2984. }
  2985. if (fidp->fid_type != P9_FID_NONE) {
  2986. err = -EINVAL;
  2987. goto out;
  2988. }
  2989. err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
  2990. if (err < 0) {
  2991. goto out;
  2992. }
  2993. } else {
  2994. char *dir_name = g_path_get_dirname(fidp->path.data);
  2995. V9fsPath dir_path;
  2996. v9fs_path_init(&dir_path);
  2997. v9fs_path_sprintf(&dir_path, "%s", dir_name);
  2998. g_free(dir_name);
  2999. err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
  3000. v9fs_path_free(&dir_path);
  3001. if (err < 0) {
  3002. goto out;
  3003. }
  3004. }
  3005. err = v9fs_co_rename(pdu, &fidp->path, &new_path);
  3006. if (err < 0) {
  3007. goto out;
  3008. }
  3009. /*
  3010. * Fixup fid's pointing to the old name to
  3011. * start pointing to the new name
  3012. */
  3013. g_hash_table_iter_init(&iter, s->fids);
  3014. while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) {
  3015. if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
  3016. /* replace the name */
  3017. v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
  3018. }
  3019. }
  3020. out:
  3021. if (dirfidp) {
  3022. put_fid(pdu, dirfidp);
  3023. }
  3024. v9fs_path_free(&new_path);
  3025. return err;
  3026. }
  3027. /* Only works with path name based fid */
  3028. static void coroutine_fn v9fs_rename(void *opaque)
  3029. {
  3030. int32_t fid;
  3031. ssize_t err = 0;
  3032. size_t offset = 7;
  3033. V9fsString name;
  3034. int32_t newdirfid;
  3035. V9fsFidState *fidp;
  3036. V9fsPDU *pdu = opaque;
  3037. V9fsState *s = pdu->s;
  3038. v9fs_string_init(&name);
  3039. err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
  3040. if (err < 0) {
  3041. goto out_nofid;
  3042. }
  3043. if (name_is_illegal(name.data)) {
  3044. err = -ENOENT;
  3045. goto out_nofid;
  3046. }
  3047. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  3048. err = -EISDIR;
  3049. goto out_nofid;
  3050. }
  3051. fidp = get_fid(pdu, fid);
  3052. if (fidp == NULL) {
  3053. err = -ENOENT;
  3054. goto out_nofid;
  3055. }
  3056. if (fidp->fid_type != P9_FID_NONE) {
  3057. err = -EINVAL;
  3058. goto out;
  3059. }
  3060. /* if fs driver is not path based, return EOPNOTSUPP */
  3061. if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
  3062. err = -EOPNOTSUPP;
  3063. goto out;
  3064. }
  3065. v9fs_path_write_lock(s);
  3066. err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
  3067. v9fs_path_unlock(s);
  3068. if (!err) {
  3069. err = offset;
  3070. }
  3071. out:
  3072. put_fid(pdu, fidp);
  3073. out_nofid:
  3074. pdu_complete(pdu, err);
  3075. v9fs_string_free(&name);
  3076. }
  3077. static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
  3078. V9fsString *old_name,
  3079. V9fsPath *newdir,
  3080. V9fsString *new_name)
  3081. {
  3082. V9fsFidState *tfidp;
  3083. V9fsPath oldpath, newpath;
  3084. V9fsState *s = pdu->s;
  3085. int err;
  3086. GHashTableIter iter;
  3087. gpointer fid;
  3088. v9fs_path_init(&oldpath);
  3089. v9fs_path_init(&newpath);
  3090. err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
  3091. if (err < 0) {
  3092. goto out;
  3093. }
  3094. err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
  3095. if (err < 0) {
  3096. goto out;
  3097. }
  3098. /*
  3099. * Fixup fid's pointing to the old name to
  3100. * start pointing to the new name
  3101. */
  3102. g_hash_table_iter_init(&iter, s->fids);
  3103. while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) {
  3104. if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
  3105. /* replace the name */
  3106. v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
  3107. }
  3108. }
  3109. out:
  3110. v9fs_path_free(&oldpath);
  3111. v9fs_path_free(&newpath);
  3112. return err;
  3113. }
  3114. static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
  3115. V9fsString *old_name,
  3116. int32_t newdirfid,
  3117. V9fsString *new_name)
  3118. {
  3119. int err = 0;
  3120. V9fsState *s = pdu->s;
  3121. V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
  3122. olddirfidp = get_fid(pdu, olddirfid);
  3123. if (olddirfidp == NULL) {
  3124. err = -ENOENT;
  3125. goto out;
  3126. }
  3127. if (newdirfid != -1) {
  3128. newdirfidp = get_fid(pdu, newdirfid);
  3129. if (newdirfidp == NULL) {
  3130. err = -ENOENT;
  3131. goto out;
  3132. }
  3133. } else {
  3134. newdirfidp = get_fid(pdu, olddirfid);
  3135. }
  3136. err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
  3137. &newdirfidp->path, new_name);
  3138. if (err < 0) {
  3139. goto out;
  3140. }
  3141. if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
  3142. /* Only for path based fid we need to do the below fixup */
  3143. err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
  3144. &newdirfidp->path, new_name);
  3145. }
  3146. out:
  3147. if (olddirfidp) {
  3148. put_fid(pdu, olddirfidp);
  3149. }
  3150. if (newdirfidp) {
  3151. put_fid(pdu, newdirfidp);
  3152. }
  3153. return err;
  3154. }
  3155. static void coroutine_fn v9fs_renameat(void *opaque)
  3156. {
  3157. ssize_t err = 0;
  3158. size_t offset = 7;
  3159. V9fsPDU *pdu = opaque;
  3160. V9fsState *s = pdu->s;
  3161. int32_t olddirfid, newdirfid;
  3162. V9fsString old_name, new_name;
  3163. v9fs_string_init(&old_name);
  3164. v9fs_string_init(&new_name);
  3165. err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
  3166. &old_name, &newdirfid, &new_name);
  3167. if (err < 0) {
  3168. goto out_err;
  3169. }
  3170. if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
  3171. err = -ENOENT;
  3172. goto out_err;
  3173. }
  3174. if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
  3175. !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
  3176. err = -EISDIR;
  3177. goto out_err;
  3178. }
  3179. v9fs_path_write_lock(s);
  3180. err = v9fs_complete_renameat(pdu, olddirfid,
  3181. &old_name, newdirfid, &new_name);
  3182. v9fs_path_unlock(s);
  3183. if (!err) {
  3184. err = offset;
  3185. }
  3186. out_err:
  3187. pdu_complete(pdu, err);
  3188. v9fs_string_free(&old_name);
  3189. v9fs_string_free(&new_name);
  3190. }
  3191. static void coroutine_fn v9fs_wstat(void *opaque)
  3192. {
  3193. int32_t fid;
  3194. int err = 0;
  3195. int16_t unused;
  3196. V9fsStat v9stat;
  3197. size_t offset = 7;
  3198. struct stat stbuf;
  3199. V9fsFidState *fidp;
  3200. V9fsPDU *pdu = opaque;
  3201. V9fsState *s = pdu->s;
  3202. v9fs_stat_init(&v9stat);
  3203. err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
  3204. if (err < 0) {
  3205. goto out_nofid;
  3206. }
  3207. trace_v9fs_wstat(pdu->tag, pdu->id, fid,
  3208. v9stat.mode, v9stat.atime, v9stat.mtime);
  3209. fidp = get_fid(pdu, fid);
  3210. if (fidp == NULL) {
  3211. err = -EINVAL;
  3212. goto out_nofid;
  3213. }
  3214. /* do we need to sync the file? */
  3215. if (donttouch_stat(&v9stat)) {
  3216. err = v9fs_co_fsync(pdu, fidp, 0);
  3217. goto out;
  3218. }
  3219. if (v9stat.mode != -1) {
  3220. uint32_t v9_mode;
  3221. err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
  3222. if (err < 0) {
  3223. goto out;
  3224. }
  3225. v9_mode = stat_to_v9mode(&stbuf);
  3226. if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
  3227. (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
  3228. /* Attempting to change the type */
  3229. err = -EIO;
  3230. goto out;
  3231. }
  3232. err = v9fs_co_chmod(pdu, &fidp->path,
  3233. v9mode_to_mode(v9stat.mode,
  3234. &v9stat.extension));
  3235. if (err < 0) {
  3236. goto out;
  3237. }
  3238. }
  3239. if (v9stat.mtime != -1 || v9stat.atime != -1) {
  3240. struct timespec times[2];
  3241. if (v9stat.atime != -1) {
  3242. times[0].tv_sec = v9stat.atime;
  3243. times[0].tv_nsec = 0;
  3244. } else {
  3245. times[0].tv_nsec = UTIME_OMIT;
  3246. }
  3247. if (v9stat.mtime != -1) {
  3248. times[1].tv_sec = v9stat.mtime;
  3249. times[1].tv_nsec = 0;
  3250. } else {
  3251. times[1].tv_nsec = UTIME_OMIT;
  3252. }
  3253. err = v9fs_co_utimensat(pdu, &fidp->path, times);
  3254. if (err < 0) {
  3255. goto out;
  3256. }
  3257. }
  3258. if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
  3259. err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
  3260. if (err < 0) {
  3261. goto out;
  3262. }
  3263. }
  3264. if (v9stat.name.size != 0) {
  3265. v9fs_path_write_lock(s);
  3266. err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
  3267. v9fs_path_unlock(s);
  3268. if (err < 0) {
  3269. goto out;
  3270. }
  3271. }
  3272. if (v9stat.length != -1) {
  3273. err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
  3274. if (err < 0) {
  3275. goto out;
  3276. }
  3277. }
  3278. err = offset;
  3279. out:
  3280. put_fid(pdu, fidp);
  3281. out_nofid:
  3282. v9fs_stat_free(&v9stat);
  3283. pdu_complete(pdu, err);
  3284. }
  3285. static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
  3286. {
  3287. uint32_t f_type;
  3288. uint32_t f_bsize;
  3289. uint64_t f_blocks;
  3290. uint64_t f_bfree;
  3291. uint64_t f_bavail;
  3292. uint64_t f_files;
  3293. uint64_t f_ffree;
  3294. uint64_t fsid_val;
  3295. uint32_t f_namelen;
  3296. size_t offset = 7;
  3297. int32_t bsize_factor;
  3298. /*
  3299. * compute bsize factor based on host file system block size
  3300. * and client msize
  3301. */
  3302. bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize;
  3303. if (!bsize_factor) {
  3304. bsize_factor = 1;
  3305. }
  3306. f_type = stbuf->f_type;
  3307. f_bsize = stbuf->f_bsize;
  3308. f_bsize *= bsize_factor;
  3309. /*
  3310. * f_bsize is adjusted(multiplied) by bsize factor, so we need to
  3311. * adjust(divide) the number of blocks, free blocks and available
  3312. * blocks by bsize factor
  3313. */
  3314. f_blocks = stbuf->f_blocks / bsize_factor;
  3315. f_bfree = stbuf->f_bfree / bsize_factor;
  3316. f_bavail = stbuf->f_bavail / bsize_factor;
  3317. f_files = stbuf->f_files;
  3318. f_ffree = stbuf->f_ffree;
  3319. #ifdef CONFIG_DARWIN
  3320. fsid_val = (unsigned int)stbuf->f_fsid.val[0] |
  3321. (unsigned long long)stbuf->f_fsid.val[1] << 32;
  3322. f_namelen = NAME_MAX;
  3323. #else
  3324. fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
  3325. (unsigned long long)stbuf->f_fsid.__val[1] << 32;
  3326. f_namelen = stbuf->f_namelen;
  3327. #endif
  3328. return pdu_marshal(pdu, offset, "ddqqqqqqd",
  3329. f_type, f_bsize, f_blocks, f_bfree,
  3330. f_bavail, f_files, f_ffree,
  3331. fsid_val, f_namelen);
  3332. }
  3333. static void coroutine_fn v9fs_statfs(void *opaque)
  3334. {
  3335. int32_t fid;
  3336. ssize_t retval = 0;
  3337. size_t offset = 7;
  3338. V9fsFidState *fidp;
  3339. struct statfs stbuf;
  3340. V9fsPDU *pdu = opaque;
  3341. V9fsState *s = pdu->s;
  3342. retval = pdu_unmarshal(pdu, offset, "d", &fid);
  3343. if (retval < 0) {
  3344. goto out_nofid;
  3345. }
  3346. fidp = get_fid(pdu, fid);
  3347. if (fidp == NULL) {
  3348. retval = -ENOENT;
  3349. goto out_nofid;
  3350. }
  3351. retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
  3352. if (retval < 0) {
  3353. goto out;
  3354. }
  3355. retval = v9fs_fill_statfs(s, pdu, &stbuf);
  3356. if (retval < 0) {
  3357. goto out;
  3358. }
  3359. retval += offset;
  3360. out:
  3361. put_fid(pdu, fidp);
  3362. out_nofid:
  3363. pdu_complete(pdu, retval);
  3364. }
  3365. static void coroutine_fn v9fs_mknod(void *opaque)
  3366. {
  3367. int mode;
  3368. gid_t gid;
  3369. int32_t fid;
  3370. V9fsQID qid;
  3371. int err = 0;
  3372. int major, minor;
  3373. size_t offset = 7;
  3374. V9fsString name;
  3375. struct stat stbuf;
  3376. V9fsFidState *fidp;
  3377. V9fsPDU *pdu = opaque;
  3378. v9fs_string_init(&name);
  3379. err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
  3380. &major, &minor, &gid);
  3381. if (err < 0) {
  3382. goto out_nofid;
  3383. }
  3384. trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
  3385. if (name_is_illegal(name.data)) {
  3386. err = -ENOENT;
  3387. goto out_nofid;
  3388. }
  3389. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  3390. err = -EEXIST;
  3391. goto out_nofid;
  3392. }
  3393. fidp = get_fid(pdu, fid);
  3394. if (fidp == NULL) {
  3395. err = -ENOENT;
  3396. goto out_nofid;
  3397. }
  3398. err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
  3399. makedev(major, minor), mode, &stbuf);
  3400. if (err < 0) {
  3401. goto out;
  3402. }
  3403. err = stat_to_qid(pdu, &stbuf, &qid);
  3404. if (err < 0) {
  3405. goto out;
  3406. }
  3407. err = pdu_marshal(pdu, offset, "Q", &qid);
  3408. if (err < 0) {
  3409. goto out;
  3410. }
  3411. err += offset;
  3412. trace_v9fs_mknod_return(pdu->tag, pdu->id,
  3413. qid.type, qid.version, qid.path);
  3414. out:
  3415. put_fid(pdu, fidp);
  3416. out_nofid:
  3417. pdu_complete(pdu, err);
  3418. v9fs_string_free(&name);
  3419. }
  3420. /*
  3421. * Implement posix byte range locking code
  3422. * Server side handling of locking code is very simple, because 9p server in
  3423. * QEMU can handle only one client. And most of the lock handling
  3424. * (like conflict, merging) etc is done by the VFS layer itself, so no need to
  3425. * do any thing in * qemu 9p server side lock code path.
  3426. * So when a TLOCK request comes, always return success
  3427. */
  3428. static void coroutine_fn v9fs_lock(void *opaque)
  3429. {
  3430. V9fsFlock flock;
  3431. size_t offset = 7;
  3432. struct stat stbuf;
  3433. V9fsFidState *fidp;
  3434. int32_t fid, err = 0;
  3435. V9fsPDU *pdu = opaque;
  3436. v9fs_string_init(&flock.client_id);
  3437. err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
  3438. &flock.flags, &flock.start, &flock.length,
  3439. &flock.proc_id, &flock.client_id);
  3440. if (err < 0) {
  3441. goto out_nofid;
  3442. }
  3443. trace_v9fs_lock(pdu->tag, pdu->id, fid,
  3444. flock.type, flock.start, flock.length);
  3445. /* We support only block flag now (that too ignored currently) */
  3446. if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
  3447. err = -EINVAL;
  3448. goto out_nofid;
  3449. }
  3450. fidp = get_fid(pdu, fid);
  3451. if (fidp == NULL) {
  3452. err = -ENOENT;
  3453. goto out_nofid;
  3454. }
  3455. err = v9fs_co_fstat(pdu, fidp, &stbuf);
  3456. if (err < 0) {
  3457. goto out;
  3458. }
  3459. err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
  3460. if (err < 0) {
  3461. goto out;
  3462. }
  3463. err += offset;
  3464. trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
  3465. out:
  3466. put_fid(pdu, fidp);
  3467. out_nofid:
  3468. pdu_complete(pdu, err);
  3469. v9fs_string_free(&flock.client_id);
  3470. }
  3471. /*
  3472. * When a TGETLOCK request comes, always return success because all lock
  3473. * handling is done by client's VFS layer.
  3474. */
  3475. static void coroutine_fn v9fs_getlock(void *opaque)
  3476. {
  3477. size_t offset = 7;
  3478. struct stat stbuf;
  3479. V9fsFidState *fidp;
  3480. V9fsGetlock glock;
  3481. int32_t fid, err = 0;
  3482. V9fsPDU *pdu = opaque;
  3483. v9fs_string_init(&glock.client_id);
  3484. err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
  3485. &glock.start, &glock.length, &glock.proc_id,
  3486. &glock.client_id);
  3487. if (err < 0) {
  3488. goto out_nofid;
  3489. }
  3490. trace_v9fs_getlock(pdu->tag, pdu->id, fid,
  3491. glock.type, glock.start, glock.length);
  3492. fidp = get_fid(pdu, fid);
  3493. if (fidp == NULL) {
  3494. err = -ENOENT;
  3495. goto out_nofid;
  3496. }
  3497. err = v9fs_co_fstat(pdu, fidp, &stbuf);
  3498. if (err < 0) {
  3499. goto out;
  3500. }
  3501. glock.type = P9_LOCK_TYPE_UNLCK;
  3502. err = pdu_marshal(pdu, offset, "bqqds", glock.type,
  3503. glock.start, glock.length, glock.proc_id,
  3504. &glock.client_id);
  3505. if (err < 0) {
  3506. goto out;
  3507. }
  3508. err += offset;
  3509. trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
  3510. glock.length, glock.proc_id);
  3511. out:
  3512. put_fid(pdu, fidp);
  3513. out_nofid:
  3514. pdu_complete(pdu, err);
  3515. v9fs_string_free(&glock.client_id);
  3516. }
  3517. static void coroutine_fn v9fs_mkdir(void *opaque)
  3518. {
  3519. V9fsPDU *pdu = opaque;
  3520. size_t offset = 7;
  3521. int32_t fid;
  3522. struct stat stbuf;
  3523. V9fsQID qid;
  3524. V9fsString name;
  3525. V9fsFidState *fidp;
  3526. gid_t gid;
  3527. int mode;
  3528. int err = 0;
  3529. v9fs_string_init(&name);
  3530. err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
  3531. if (err < 0) {
  3532. goto out_nofid;
  3533. }
  3534. trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
  3535. if (name_is_illegal(name.data)) {
  3536. err = -ENOENT;
  3537. goto out_nofid;
  3538. }
  3539. if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
  3540. err = -EEXIST;
  3541. goto out_nofid;
  3542. }
  3543. fidp = get_fid(pdu, fid);
  3544. if (fidp == NULL) {
  3545. err = -ENOENT;
  3546. goto out_nofid;
  3547. }
  3548. err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
  3549. if (err < 0) {
  3550. goto out;
  3551. }
  3552. err = stat_to_qid(pdu, &stbuf, &qid);
  3553. if (err < 0) {
  3554. goto out;
  3555. }
  3556. err = pdu_marshal(pdu, offset, "Q", &qid);
  3557. if (err < 0) {
  3558. goto out;
  3559. }
  3560. err += offset;
  3561. trace_v9fs_mkdir_return(pdu->tag, pdu->id,
  3562. qid.type, qid.version, qid.path, err);
  3563. out:
  3564. put_fid(pdu, fidp);
  3565. out_nofid:
  3566. pdu_complete(pdu, err);
  3567. v9fs_string_free(&name);
  3568. }
  3569. static void coroutine_fn v9fs_xattrwalk(void *opaque)
  3570. {
  3571. int64_t size;
  3572. V9fsString name;
  3573. ssize_t err = 0;
  3574. size_t offset = 7;
  3575. int32_t fid, newfid;
  3576. V9fsFidState *file_fidp;
  3577. V9fsFidState *xattr_fidp = NULL;
  3578. V9fsPDU *pdu = opaque;
  3579. V9fsState *s = pdu->s;
  3580. v9fs_string_init(&name);
  3581. err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
  3582. if (err < 0) {
  3583. goto out_nofid;
  3584. }
  3585. trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
  3586. file_fidp = get_fid(pdu, fid);
  3587. if (file_fidp == NULL) {
  3588. err = -ENOENT;
  3589. goto out_nofid;
  3590. }
  3591. xattr_fidp = alloc_fid(s, newfid);
  3592. if (xattr_fidp == NULL) {
  3593. err = -EINVAL;
  3594. goto out;
  3595. }
  3596. v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
  3597. if (!v9fs_string_size(&name)) {
  3598. /*
  3599. * listxattr request. Get the size first
  3600. */
  3601. size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
  3602. if (size < 0) {
  3603. err = size;
  3604. clunk_fid(s, xattr_fidp->fid);
  3605. goto out;
  3606. }
  3607. /*
  3608. * Read the xattr value
  3609. */
  3610. xattr_fidp->fs.xattr.len = size;
  3611. xattr_fidp->fid_type = P9_FID_XATTR;
  3612. xattr_fidp->fs.xattr.xattrwalk_fid = true;
  3613. xattr_fidp->fs.xattr.value = g_malloc0(size);
  3614. if (size) {
  3615. err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
  3616. xattr_fidp->fs.xattr.value,
  3617. xattr_fidp->fs.xattr.len);
  3618. if (err < 0) {
  3619. clunk_fid(s, xattr_fidp->fid);
  3620. goto out;
  3621. }
  3622. }
  3623. err = pdu_marshal(pdu, offset, "q", size);
  3624. if (err < 0) {
  3625. goto out;
  3626. }
  3627. err += offset;
  3628. } else {
  3629. /*
  3630. * specific xattr fid. We check for xattr
  3631. * presence also collect the xattr size
  3632. */
  3633. size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
  3634. &name, NULL, 0);
  3635. if (size < 0) {
  3636. err = size;
  3637. clunk_fid(s, xattr_fidp->fid);
  3638. goto out;
  3639. }
  3640. /*
  3641. * Read the xattr value
  3642. */
  3643. xattr_fidp->fs.xattr.len = size;
  3644. xattr_fidp->fid_type = P9_FID_XATTR;
  3645. xattr_fidp->fs.xattr.xattrwalk_fid = true;
  3646. xattr_fidp->fs.xattr.value = g_malloc0(size);
  3647. if (size) {
  3648. err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
  3649. &name, xattr_fidp->fs.xattr.value,
  3650. xattr_fidp->fs.xattr.len);
  3651. if (err < 0) {
  3652. clunk_fid(s, xattr_fidp->fid);
  3653. goto out;
  3654. }
  3655. }
  3656. err = pdu_marshal(pdu, offset, "q", size);
  3657. if (err < 0) {
  3658. goto out;
  3659. }
  3660. err += offset;
  3661. }
  3662. trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
  3663. out:
  3664. put_fid(pdu, file_fidp);
  3665. if (xattr_fidp) {
  3666. put_fid(pdu, xattr_fidp);
  3667. }
  3668. out_nofid:
  3669. pdu_complete(pdu, err);
  3670. v9fs_string_free(&name);
  3671. }
  3672. #if defined(CONFIG_LINUX)
  3673. /* Currently, only Linux has XATTR_SIZE_MAX */
  3674. #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX
  3675. #elif defined(CONFIG_DARWIN)
  3676. /*
  3677. * Darwin doesn't seem to define a maximum xattr size in its user
  3678. * space header, so manually configure it across platforms as 64k.
  3679. *
  3680. * Having no limit at all can lead to QEMU crashing during large g_malloc()
  3681. * calls. Because QEMU does not currently support macOS guests, the below
  3682. * preliminary solution only works due to its being a reflection of the limit of
  3683. * Linux guests.
  3684. */
  3685. #define P9_XATTR_SIZE_MAX 65536
  3686. #else
  3687. #error Missing definition for P9_XATTR_SIZE_MAX for this host system
  3688. #endif
  3689. static void coroutine_fn v9fs_xattrcreate(void *opaque)
  3690. {
  3691. int flags, rflags = 0;
  3692. int32_t fid;
  3693. uint64_t size;
  3694. ssize_t err = 0;
  3695. V9fsString name;
  3696. size_t offset = 7;
  3697. V9fsFidState *file_fidp;
  3698. V9fsFidState *xattr_fidp;
  3699. V9fsPDU *pdu = opaque;
  3700. v9fs_string_init(&name);
  3701. err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
  3702. if (err < 0) {
  3703. goto out_nofid;
  3704. }
  3705. trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
  3706. if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
  3707. err = -EINVAL;
  3708. goto out_nofid;
  3709. }
  3710. if (flags & P9_XATTR_CREATE) {
  3711. rflags |= XATTR_CREATE;
  3712. }
  3713. if (flags & P9_XATTR_REPLACE) {
  3714. rflags |= XATTR_REPLACE;
  3715. }
  3716. if (size > P9_XATTR_SIZE_MAX) {
  3717. err = -E2BIG;
  3718. goto out_nofid;
  3719. }
  3720. file_fidp = get_fid(pdu, fid);
  3721. if (file_fidp == NULL) {
  3722. err = -EINVAL;
  3723. goto out_nofid;
  3724. }
  3725. if (file_fidp->fid_type != P9_FID_NONE) {
  3726. err = -EINVAL;
  3727. goto out_put_fid;
  3728. }
  3729. /* Make the file fid point to xattr */
  3730. xattr_fidp = file_fidp;
  3731. xattr_fidp->fid_type = P9_FID_XATTR;
  3732. xattr_fidp->fs.xattr.copied_len = 0;
  3733. xattr_fidp->fs.xattr.xattrwalk_fid = false;
  3734. xattr_fidp->fs.xattr.len = size;
  3735. xattr_fidp->fs.xattr.flags = rflags;
  3736. v9fs_string_init(&xattr_fidp->fs.xattr.name);
  3737. v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
  3738. xattr_fidp->fs.xattr.value = g_malloc0(size);
  3739. err = offset;
  3740. out_put_fid:
  3741. put_fid(pdu, file_fidp);
  3742. out_nofid:
  3743. pdu_complete(pdu, err);
  3744. v9fs_string_free(&name);
  3745. }
  3746. static void coroutine_fn v9fs_readlink(void *opaque)
  3747. {
  3748. V9fsPDU *pdu = opaque;
  3749. size_t offset = 7;
  3750. V9fsString target;
  3751. int32_t fid;
  3752. int err = 0;
  3753. V9fsFidState *fidp;
  3754. err = pdu_unmarshal(pdu, offset, "d", &fid);
  3755. if (err < 0) {
  3756. goto out_nofid;
  3757. }
  3758. trace_v9fs_readlink(pdu->tag, pdu->id, fid);
  3759. fidp = get_fid(pdu, fid);
  3760. if (fidp == NULL) {
  3761. err = -ENOENT;
  3762. goto out_nofid;
  3763. }
  3764. v9fs_string_init(&target);
  3765. err = v9fs_co_readlink(pdu, &fidp->path, &target);
  3766. if (err < 0) {
  3767. goto out;
  3768. }
  3769. err = pdu_marshal(pdu, offset, "s", &target);
  3770. if (err < 0) {
  3771. v9fs_string_free(&target);
  3772. goto out;
  3773. }
  3774. err += offset;
  3775. trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
  3776. v9fs_string_free(&target);
  3777. out:
  3778. put_fid(pdu, fidp);
  3779. out_nofid:
  3780. pdu_complete(pdu, err);
  3781. }
  3782. static CoroutineEntry *pdu_co_handlers[] = {
  3783. [P9_TREADDIR] = v9fs_readdir,
  3784. [P9_TSTATFS] = v9fs_statfs,
  3785. [P9_TGETATTR] = v9fs_getattr,
  3786. [P9_TSETATTR] = v9fs_setattr,
  3787. [P9_TXATTRWALK] = v9fs_xattrwalk,
  3788. [P9_TXATTRCREATE] = v9fs_xattrcreate,
  3789. [P9_TMKNOD] = v9fs_mknod,
  3790. [P9_TRENAME] = v9fs_rename,
  3791. [P9_TLOCK] = v9fs_lock,
  3792. [P9_TGETLOCK] = v9fs_getlock,
  3793. [P9_TRENAMEAT] = v9fs_renameat,
  3794. [P9_TREADLINK] = v9fs_readlink,
  3795. [P9_TUNLINKAT] = v9fs_unlinkat,
  3796. [P9_TMKDIR] = v9fs_mkdir,
  3797. [P9_TVERSION] = v9fs_version,
  3798. [P9_TLOPEN] = v9fs_open,
  3799. [P9_TATTACH] = v9fs_attach,
  3800. [P9_TSTAT] = v9fs_stat,
  3801. [P9_TWALK] = v9fs_walk,
  3802. [P9_TCLUNK] = v9fs_clunk,
  3803. [P9_TFSYNC] = v9fs_fsync,
  3804. [P9_TOPEN] = v9fs_open,
  3805. [P9_TREAD] = v9fs_read,
  3806. #if 0
  3807. [P9_TAUTH] = v9fs_auth,
  3808. #endif
  3809. [P9_TFLUSH] = v9fs_flush,
  3810. [P9_TLINK] = v9fs_link,
  3811. [P9_TSYMLINK] = v9fs_symlink,
  3812. [P9_TCREATE] = v9fs_create,
  3813. [P9_TLCREATE] = v9fs_lcreate,
  3814. [P9_TWRITE] = v9fs_write,
  3815. [P9_TWSTAT] = v9fs_wstat,
  3816. [P9_TREMOVE] = v9fs_remove,
  3817. };
  3818. static void coroutine_fn v9fs_op_not_supp(void *opaque)
  3819. {
  3820. V9fsPDU *pdu = opaque;
  3821. pdu_complete(pdu, -EOPNOTSUPP);
  3822. }
  3823. static void coroutine_fn v9fs_fs_ro(void *opaque)
  3824. {
  3825. V9fsPDU *pdu = opaque;
  3826. pdu_complete(pdu, -EROFS);
  3827. }
  3828. static inline bool is_read_only_op(V9fsPDU *pdu)
  3829. {
  3830. switch (pdu->id) {
  3831. case P9_TREADDIR:
  3832. case P9_TSTATFS:
  3833. case P9_TGETATTR:
  3834. case P9_TXATTRWALK:
  3835. case P9_TLOCK:
  3836. case P9_TGETLOCK:
  3837. case P9_TREADLINK:
  3838. case P9_TVERSION:
  3839. case P9_TLOPEN:
  3840. case P9_TATTACH:
  3841. case P9_TSTAT:
  3842. case P9_TWALK:
  3843. case P9_TCLUNK:
  3844. case P9_TFSYNC:
  3845. case P9_TOPEN:
  3846. case P9_TREAD:
  3847. case P9_TAUTH:
  3848. case P9_TFLUSH:
  3849. return 1;
  3850. default:
  3851. return 0;
  3852. }
  3853. }
  3854. void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
  3855. {
  3856. Coroutine *co;
  3857. CoroutineEntry *handler;
  3858. V9fsState *s = pdu->s;
  3859. pdu->size = le32_to_cpu(hdr->size_le);
  3860. pdu->id = hdr->id;
  3861. pdu->tag = le16_to_cpu(hdr->tag_le);
  3862. if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
  3863. (pdu_co_handlers[pdu->id] == NULL)) {
  3864. handler = v9fs_op_not_supp;
  3865. } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
  3866. handler = v9fs_fs_ro;
  3867. } else {
  3868. handler = pdu_co_handlers[pdu->id];
  3869. }
  3870. qemu_co_queue_init(&pdu->complete);
  3871. co = qemu_coroutine_create(handler, pdu);
  3872. qemu_coroutine_enter(co);
  3873. }
  3874. /* Returns 0 on success, 1 on failure. */
  3875. int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
  3876. Error **errp)
  3877. {
  3878. ERRP_GUARD();
  3879. int i, len;
  3880. struct stat stat;
  3881. FsDriverEntry *fse;
  3882. V9fsPath path;
  3883. int rc = 1;
  3884. assert(!s->transport);
  3885. s->transport = t;
  3886. /* initialize pdu allocator */
  3887. QLIST_INIT(&s->free_list);
  3888. QLIST_INIT(&s->active_list);
  3889. for (i = 0; i < MAX_REQ; i++) {
  3890. QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
  3891. s->pdus[i].s = s;
  3892. s->pdus[i].idx = i;
  3893. }
  3894. v9fs_path_init(&path);
  3895. fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
  3896. if (!fse) {
  3897. /* We don't have a fsdev identified by fsdev_id */
  3898. error_setg(errp, "9pfs device couldn't find fsdev with the "
  3899. "id = %s",
  3900. s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
  3901. goto out;
  3902. }
  3903. if (!s->fsconf.tag) {
  3904. /* we haven't specified a mount_tag */
  3905. error_setg(errp, "fsdev with id %s needs mount_tag arguments",
  3906. s->fsconf.fsdev_id);
  3907. goto out;
  3908. }
  3909. s->ctx.export_flags = fse->export_flags;
  3910. s->ctx.fs_root = g_strdup(fse->path);
  3911. s->ctx.exops.get_st_gen = NULL;
  3912. len = strlen(s->fsconf.tag);
  3913. if (len > MAX_TAG_LEN - 1) {
  3914. error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
  3915. "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
  3916. goto out;
  3917. }
  3918. s->tag = g_strdup(s->fsconf.tag);
  3919. s->ctx.uid = -1;
  3920. s->ops = fse->ops;
  3921. s->ctx.fmode = fse->fmode;
  3922. s->ctx.dmode = fse->dmode;
  3923. s->fids = g_hash_table_new(NULL, NULL);
  3924. qemu_co_rwlock_init(&s->rename_lock);
  3925. if (s->ops->init(&s->ctx, errp) < 0) {
  3926. error_prepend(errp, "cannot initialize fsdev '%s': ",
  3927. s->fsconf.fsdev_id);
  3928. goto out;
  3929. }
  3930. /*
  3931. * Check details of export path, We need to use fs driver
  3932. * call back to do that. Since we are in the init path, we don't
  3933. * use co-routines here.
  3934. */
  3935. if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
  3936. error_setg(errp,
  3937. "error in converting name to path %s", strerror(errno));
  3938. goto out;
  3939. }
  3940. if (s->ops->lstat(&s->ctx, &path, &stat)) {
  3941. error_setg(errp, "share path %s does not exist", fse->path);
  3942. goto out;
  3943. } else if (!S_ISDIR(stat.st_mode)) {
  3944. error_setg(errp, "share path %s is not a directory", fse->path);
  3945. goto out;
  3946. }
  3947. s->dev_id = stat.st_dev;
  3948. /* init inode remapping : */
  3949. /* hash table for variable length inode suffixes */
  3950. qpd_table_init(&s->qpd_table);
  3951. /* hash table for slow/full inode remapping (most users won't need it) */
  3952. qpf_table_init(&s->qpf_table);
  3953. /* hash table for quick inode remapping */
  3954. qpp_table_init(&s->qpp_table);
  3955. s->qp_ndevices = 0;
  3956. s->qp_affix_next = 1; /* reserve 0 to detect overflow */
  3957. s->qp_fullpath_next = 1;
  3958. s->ctx.fst = &fse->fst;
  3959. fsdev_throttle_init(s->ctx.fst);
  3960. s->reclaiming = false;
  3961. rc = 0;
  3962. out:
  3963. if (rc) {
  3964. v9fs_device_unrealize_common(s);
  3965. }
  3966. v9fs_path_free(&path);
  3967. return rc;
  3968. }
  3969. void v9fs_device_unrealize_common(V9fsState *s)
  3970. {
  3971. if (s->ops && s->ops->cleanup) {
  3972. s->ops->cleanup(&s->ctx);
  3973. }
  3974. if (s->ctx.fst) {
  3975. fsdev_throttle_cleanup(s->ctx.fst);
  3976. }
  3977. if (s->fids) {
  3978. g_hash_table_destroy(s->fids);
  3979. s->fids = NULL;
  3980. }
  3981. g_free(s->tag);
  3982. qp_table_destroy(&s->qpd_table);
  3983. qp_table_destroy(&s->qpp_table);
  3984. qp_table_destroy(&s->qpf_table);
  3985. g_free(s->ctx.fs_root);
  3986. }
  3987. typedef struct VirtfsCoResetData {
  3988. V9fsPDU pdu;
  3989. bool done;
  3990. } VirtfsCoResetData;
  3991. static void coroutine_fn virtfs_co_reset(void *opaque)
  3992. {
  3993. VirtfsCoResetData *data = opaque;
  3994. virtfs_reset(&data->pdu);
  3995. data->done = true;
  3996. }
  3997. void v9fs_reset(V9fsState *s)
  3998. {
  3999. VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
  4000. Coroutine *co;
  4001. while (!QLIST_EMPTY(&s->active_list)) {
  4002. aio_poll(qemu_get_aio_context(), true);
  4003. }
  4004. co = qemu_coroutine_create(virtfs_co_reset, &data);
  4005. qemu_coroutine_enter(co);
  4006. while (!data.done) {
  4007. aio_poll(qemu_get_aio_context(), true);
  4008. }
  4009. }
  4010. static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
  4011. {
  4012. struct rlimit rlim;
  4013. if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
  4014. error_report("Failed to get the resource limit");
  4015. exit(1);
  4016. }
  4017. open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3);
  4018. open_fd_rc = rlim.rlim_cur / 2;
  4019. }