2
0

9p.c 111 KB

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