aufs6.6.63-20241202.patch 989 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745
  1. diff -Naur null/Documentation/ABI/testing/debugfs-aufs linux-6.6.63/Documentation/ABI/testing/debugfs-aufs
  2. --- /dev/null
  3. +++ linux-6.6.63/Documentation/ABI/testing/debugfs-aufs 2024-12-18 15:12:59.290519651 +0300
  4. @@ -0,0 +1,55 @@
  5. +What: /debug/aufs/si_<id>/
  6. +Date: March 2009
  7. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  8. +Description:
  9. + Under /debug/aufs, a directory named si_<id> is created
  10. + per aufs mount, where <id> is a unique id generated
  11. + internally.
  12. +
  13. +What: /debug/aufs/si_<id>/plink
  14. +Date: Apr 2013
  15. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  16. +Description:
  17. + It has three lines and shows the information about the
  18. + pseudo-link. The first line is a single number
  19. + representing a number of buckets. The second line is a
  20. + number of pseudo-links per buckets (separated by a
  21. + blank). The last line is a single number representing a
  22. + total number of psedo-links.
  23. + When the aufs mount option 'noplink' is specified, it
  24. + will show "1\n0\n0\n".
  25. +
  26. +What: /debug/aufs/si_<id>/xib
  27. +Date: March 2009
  28. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  29. +Description:
  30. + It shows the consumed blocks by xib (External Inode Number
  31. + Bitmap), its block size and file size.
  32. + When the aufs mount option 'noxino' is specified, it
  33. + will be empty. About XINO files, see the aufs manual.
  34. +
  35. +What: /debug/aufs/si_<id>/xi<branch-index>
  36. +Date: March 2009
  37. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  38. +Description:
  39. + It shows the consumed blocks by xino (External Inode Number
  40. + Translation Table), its link count, block size and file
  41. + size.
  42. + Due to the file size limit, there may exist multiple
  43. + xino files per branch. In this case, "-N" is added to
  44. + the filename and it corresponds to the index of the
  45. + internal xino array. "-0" is omitted.
  46. + When the aufs mount option 'noxino' is specified, Those
  47. + entries won't exist. About XINO files, see the aufs
  48. + manual.
  49. +
  50. +What: /debug/aufs/si_<id>/xigen
  51. +Date: March 2009
  52. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  53. +Description:
  54. + It shows the consumed blocks by xigen (External Inode
  55. + Generation Table), its block size and file size.
  56. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  57. + be created.
  58. + When the aufs mount option 'noxino' is specified, it
  59. + will be empty. About XINO files, see the aufs manual.
  60. diff -Naur null/Documentation/ABI/testing/sysfs-aufs linux-6.6.63/Documentation/ABI/testing/sysfs-aufs
  61. --- /dev/null
  62. +++ linux-6.6.63/Documentation/ABI/testing/sysfs-aufs 2024-12-18 15:12:59.290519651 +0300
  63. @@ -0,0 +1,31 @@
  64. +What: /sys/fs/aufs/si_<id>/
  65. +Date: March 2009
  66. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  67. +Description:
  68. + Under /sys/fs/aufs, a directory named si_<id> is created
  69. + per aufs mount, where <id> is a unique id generated
  70. + internally.
  71. +
  72. +What: /sys/fs/aufs/si_<id>/br<idx>
  73. +Date: March 2009
  74. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  75. +Description:
  76. + It shows the abolute path of a member directory (which
  77. + is called branch) in aufs, and its permission.
  78. +
  79. +What: /sys/fs/aufs/si_<id>/brid<idx>
  80. +Date: July 2013
  81. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  82. +Description:
  83. + It shows the id of a member directory (which is called
  84. + branch) in aufs.
  85. +
  86. +What: /sys/fs/aufs/si_<id>/xi_path
  87. +Date: March 2009
  88. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  89. +Description:
  90. + It shows the abolute path of XINO (External Inode Number
  91. + Bitmap, Translation Table and Generation Table) file
  92. + even if it is the default path.
  93. + When the aufs mount option 'noxino' is specified, it
  94. + will be empty. About XINO files, see the aufs manual.
  95. diff -Naur null/Documentation/filesystems/aufs/design/01intro.txt linux-6.6.63/Documentation/filesystems/aufs/design/01intro.txt
  96. --- /dev/null
  97. +++ linux-6.6.63/Documentation/filesystems/aufs/design/01intro.txt 2024-12-18 15:12:59.290519651 +0300
  98. @@ -0,0 +1,171 @@
  99. +
  100. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  101. +#
  102. +# This program is free software; you can redistribute it and/or modify
  103. +# it under the terms of the GNU General Public License as published by
  104. +# the Free Software Foundation; either version 2 of the License, or
  105. +# (at your option) any later version.
  106. +#
  107. +# This program is distributed in the hope that it will be useful,
  108. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  109. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  110. +# GNU General Public License for more details.
  111. +#
  112. +# You should have received a copy of the GNU General Public License
  113. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  114. +
  115. +Introduction
  116. +----------------------------------------
  117. +
  118. +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
  119. +1. abbrev. for "advanced multi-layered unification filesystem".
  120. +2. abbrev. for "another unionfs".
  121. +3. abbrev. for "auf das" in German which means "on the" in English.
  122. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  123. + But "Filesystem aufs Filesystem" is hard to understand.
  124. +4. abbrev. for "African Urban Fashion Show".
  125. +
  126. +AUFS is a filesystem with features:
  127. +- multi layered stackable unification filesystem, the member directory
  128. + is called as a branch.
  129. +- branch permission and attribute, 'readonly', 'real-readonly',
  130. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  131. + combination.
  132. +- internal "file copy-on-write".
  133. +- logical deletion, whiteout.
  134. +- dynamic branch manipulation, adding, deleting and changing permission.
  135. +- allow bypassing aufs, user's direct branch access.
  136. +- external inode number translation table and bitmap which maintains the
  137. + persistent aufs inode number.
  138. +- seekable directory, including NFS readdir.
  139. +- file mapping, mmap and sharing pages.
  140. +- pseudo-link, hardlink over branches.
  141. +- loopback mounted filesystem as a branch.
  142. +- several policies to select one among multiple writable branches.
  143. +- revert a single systemcall when an error occurs in aufs.
  144. +- and more...
  145. +
  146. +
  147. +Multi Layered Stackable Unification Filesystem
  148. +----------------------------------------------------------------------
  149. +Most people already knows what it is.
  150. +It is a filesystem which unifies several directories and provides a
  151. +merged single directory. When users access a file, the access will be
  152. +passed/re-directed/converted (sorry, I am not sure which English word is
  153. +correct) to the real file on the member filesystem. The member
  154. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  155. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  156. +readonly branch is handled by creating 'whiteout' on the upper writable
  157. +branch.
  158. +
  159. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  160. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  161. +different approaches to implement the merged-view.
  162. +The former tries putting it into VFS, and the latter implements as a
  163. +separate filesystem.
  164. +(If I misunderstand about these implementations, please let me know and
  165. +I shall correct it. Because it is a long time ago when I read their
  166. +source files last time).
  167. +
  168. +UnionMount's approach will be able to small, but may be hard to share
  169. +branches between several UnionMount since the whiteout in it is
  170. +implemented in the inode on branch filesystem and always
  171. +shared. According to Bharata's post, readdir does not seems to be
  172. +finished yet.
  173. +There are several missing features known in this implementations such as
  174. +- for users, the inode number may change silently. eg. copy-up.
  175. +- link(2) may break by copy-up.
  176. +- read(2) may get an obsoleted filedata (fstat(2) too).
  177. +- fcntl(F_SETLK) may be broken by copy-up.
  178. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  179. + open(O_RDWR).
  180. +
  181. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  182. +merged into mainline. This is another implementation of UnionMount as a
  183. +separated filesystem. All the limitations and known problems which
  184. +UnionMount are equally inherited to "overlay" filesystem.
  185. +
  186. +Unionfs has a longer history. When I started implementing a stackable
  187. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  188. +inode, dentry and file objects and they have an array pointing lower
  189. +same kind objects. After contributing many patches for Unionfs, I
  190. +re-started my project AUFS (Jun 2006).
  191. +
  192. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  193. +implemented my own ideas, approaches and enhancements and it became
  194. +totally different one.
  195. +
  196. +Comparing DM snapshot and fs based implementation
  197. +- the number of bytes to be copied between devices is much smaller.
  198. +- the type of filesystem must be one and only.
  199. +- the fs must be writable, no readonly fs, even for the lower original
  200. + device. so the compression fs will not be usable. but if we use
  201. + loopback mount, we may address this issue.
  202. + for instance,
  203. + mount /cdrom/squashfs.img /sq
  204. + losetup /sq/ext2.img
  205. + losetup /somewhere/cow
  206. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  207. +- it will be difficult (or needs more operations) to extract the
  208. + difference between the original device and COW.
  209. +- DM snapshot-merge may help a lot when users try merging. in the
  210. + fs-layer union, users will use rsync(1).
  211. +
  212. +You may want to read my old paper "Filesystems in LiveCD"
  213. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  214. +
  215. +
  216. +Several characters/aspects/persona of aufs
  217. +----------------------------------------------------------------------
  218. +
  219. +Aufs has several characters, aspects or persona.
  220. +1. a filesystem, callee of VFS helper
  221. +2. sub-VFS, caller of VFS helper for branches
  222. +3. a virtual filesystem which maintains persistent inode number
  223. +4. reader/writer of files on branches such like an application
  224. +
  225. +1. Callee of VFS Helper
  226. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  227. +unlink(2) from an application reaches sys_unlink() kernel function and
  228. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  229. +calls filesystem specific unlink operation. Actually aufs implements the
  230. +unlink operation but it behaves like a redirector.
  231. +
  232. +2. Caller of VFS Helper for Branches
  233. +aufs_unlink() passes the unlink request to the branch filesystem as if
  234. +it were called from VFS. So the called unlink operation of the branch
  235. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  236. +every necessary pre/post operation for the branch filesystem.
  237. +- acquire the lock for the parent dir on a branch
  238. +- lookup in a branch
  239. +- revalidate dentry on a branch
  240. +- mnt_want_write() for a branch
  241. +- vfs_unlink() for a branch
  242. +- mnt_drop_write() for a branch
  243. +- release the lock on a branch
  244. +
  245. +3. Persistent Inode Number
  246. +One of the most important issue for a filesystem is to maintain inode
  247. +numbers. This is particularly important to support exporting a
  248. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  249. +backend block device for its own. But some storage is necessary to
  250. +keep and maintain the inode numbers. It may be a large space and may not
  251. +suit to keep in memory. Aufs rents some space from its first writable
  252. +branch filesystem (by default) and creates file(s) on it. These files
  253. +are created by aufs internally and removed soon (currently) keeping
  254. +opened.
  255. +Note: Because these files are removed, they are totally gone after
  256. + unmounting aufs. It means the inode numbers are not persistent
  257. + across unmount or reboot. I have a plan to make them really
  258. + persistent which will be important for aufs on NFS server.
  259. +
  260. +4. Read/Write Files Internally (copy-on-write)
  261. +Because a branch can be readonly, when you write a file on it, aufs will
  262. +"copy-up" it to the upper writable branch internally. And then write the
  263. +originally requested thing to the file. Generally kernel doesn't
  264. +open/read/write file actively. In aufs, even a single write may cause a
  265. +internal "file copy". This behaviour is very similar to cp(1) command.
  266. +
  267. +Some people may think it is better to pass such work to user space
  268. +helper, instead of doing in kernel space. Actually I am still thinking
  269. +about it. But currently I have implemented it in kernel space.
  270. diff -Naur null/Documentation/filesystems/aufs/design/02struct.txt linux-6.6.63/Documentation/filesystems/aufs/design/02struct.txt
  271. --- /dev/null
  272. +++ linux-6.6.63/Documentation/filesystems/aufs/design/02struct.txt 2024-12-18 15:12:59.291519638 +0300
  273. @@ -0,0 +1,258 @@
  274. +
  275. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  276. +#
  277. +# This program is free software; you can redistribute it and/or modify
  278. +# it under the terms of the GNU General Public License as published by
  279. +# the Free Software Foundation; either version 2 of the License, or
  280. +# (at your option) any later version.
  281. +#
  282. +# This program is distributed in the hope that it will be useful,
  283. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  284. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  285. +# GNU General Public License for more details.
  286. +#
  287. +# You should have received a copy of the GNU General Public License
  288. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  289. +
  290. +Basic Aufs Internal Structure
  291. +
  292. +Superblock/Inode/Dentry/File Objects
  293. +----------------------------------------------------------------------
  294. +As like an ordinary filesystem, aufs has its own
  295. +superblock/inode/dentry/file objects. All these objects have a
  296. +dynamically allocated array and store the same kind of pointers to the
  297. +lower filesystem, branch.
  298. +For example, when you build a union with one readwrite branch and one
  299. +readonly, mounted /au, /rw and /ro respectively.
  300. +- /au = /rw + /ro
  301. +- /ro/fileA exists but /rw/fileA
  302. +
  303. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  304. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  305. +- [0] = NULL (because /rw/fileA doesn't exist)
  306. +- [1] = /ro/fileA
  307. +
  308. +This style of an array is essentially same to the aufs
  309. +superblock/inode/dentry/file objects.
  310. +
  311. +Because aufs supports manipulating branches, ie. add/delete/change
  312. +branches dynamically, these objects has its own generation. When
  313. +branches are changed, the generation in aufs superblock is
  314. +incremented. And a generation in other object are compared when it is
  315. +accessed. When a generation in other objects are obsoleted, aufs
  316. +refreshes the internal array.
  317. +
  318. +
  319. +Superblock
  320. +----------------------------------------------------------------------
  321. +Additionally aufs superblock has some data for policies to select one
  322. +among multiple writable branches, XIB files, pseudo-links and kobject.
  323. +See below in detail.
  324. +About the policies which supports copy-down a directory, see
  325. +wbr_policy.txt too.
  326. +
  327. +
  328. +Branch and XINO(External Inode Number Translation Table)
  329. +----------------------------------------------------------------------
  330. +Every branch has its own xino (external inode number translation table)
  331. +file. The xino file is created and unlinked by aufs internally. When two
  332. +members of a union exist on the same filesystem, they share the single
  333. +xino file.
  334. +The struct of a xino file is simple, just a sequence of aufs inode
  335. +numbers which is indexed by the lower inode number.
  336. +In the above sample, assume the inode number of /ro/fileA is i111 and
  337. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  338. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  339. +
  340. +When the inode numbers are not contiguous, the xino file will be sparse
  341. +which has a hole in it and doesn't consume as much disk space as it
  342. +might appear. If your branch filesystem consumes disk space for such
  343. +holes, then you should specify 'xino=' option at mounting aufs.
  344. +
  345. +Aufs has a mount option to free the disk blocks for such holes in XINO
  346. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  347. +meet a problem of disk shortage due to XINO files, then you should try
  348. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  349. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  350. +the holes in XINO files.
  351. +
  352. +Also a writable branch has three kinds of "whiteout bases". All these
  353. +are existed when the branch is joined to aufs, and their names are
  354. +whiteout-ed doubly, so that users will never see their names in aufs
  355. +hierarchy.
  356. +1. a regular file which will be hardlinked to all whiteouts.
  357. +2. a directory to store a pseudo-link.
  358. +3. a directory to store an "orphan"-ed file temporary.
  359. +
  360. +1. Whiteout Base
  361. + When you remove a file on a readonly branch, aufs handles it as a
  362. + logical deletion and creates a whiteout on the upper writable branch
  363. + as a hardlink of this file in order not to consume inode on the
  364. + writable branch.
  365. +2. Pseudo-link Dir
  366. + See below, Pseudo-link.
  367. +3. Step-Parent Dir
  368. + When "fileC" exists on the lower readonly branch only and it is
  369. + opened and removed with its parent dir, and then user writes
  370. + something into it, then aufs copies-up fileC to this
  371. + directory. Because there is no other dir to store fileC. After
  372. + creating a file under this dir, the file is unlinked.
  373. +
  374. +Because aufs supports manipulating branches, ie. add/delete/change
  375. +dynamically, a branch has its own id. When the branch order changes,
  376. +aufs finds the new index by searching the branch id.
  377. +
  378. +
  379. +Pseudo-link
  380. +----------------------------------------------------------------------
  381. +Assume "fileA" exists on the lower readonly branch only and it is
  382. +hardlinked to "fileB" on the branch. When you write something to fileA,
  383. +aufs copies-up it to the upper writable branch. Additionally aufs
  384. +creates a hardlink under the Pseudo-link Directory of the writable
  385. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  386. +simple list. If fileB is read after unlinking fileA, aufs returns
  387. +filedata from the pseudo-link instead of the lower readonly
  388. +branch. Because the pseudo-link is based upon the inode, to keep the
  389. +inode number by xino (see above) is essentially necessary.
  390. +
  391. +All the hardlinks under the Pseudo-link Directory of the writable branch
  392. +should be restored in a proper location later. Aufs provides a utility
  393. +to do this. The userspace helpers executed at remounting and unmounting
  394. +aufs by default.
  395. +During this utility is running, it puts aufs into the pseudo-link
  396. +maintenance mode. In this mode, only the process which began the
  397. +maintenance mode (and its child processes) is allowed to operate in
  398. +aufs. Some other processes which are not related to the pseudo-link will
  399. +be allowed to run too, but the rest have to return an error or wait
  400. +until the maintenance mode ends. If a process already acquires an inode
  401. +mutex (in VFS), it has to return an error.
  402. +
  403. +
  404. +XIB(external inode number bitmap)
  405. +----------------------------------------------------------------------
  406. +Addition to the xino file per a branch, aufs has an external inode number
  407. +bitmap in a superblock object. It is also an internal file such like a
  408. +xino file.
  409. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  410. +not.
  411. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  412. +
  413. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  414. +reduce the number of consumed disk blocks for these files.
  415. +
  416. +
  417. +Virtual or Vertical Dir, and Readdir in Userspace
  418. +----------------------------------------------------------------------
  419. +In order to support multiple layers (branches), aufs readdir operation
  420. +constructs a virtual dir block on memory. For readdir, aufs calls
  421. +vfs_readdir() internally for each dir on branches, merges their entries
  422. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  423. +object. So the file object has its entry list until it is closed. The
  424. +entry list will be updated when the file position is zero and becomes
  425. +obsoleted. This decision is made in aufs automatically.
  426. +
  427. +The dynamically allocated memory block for the name of entries has a
  428. +unit of 512 bytes (by default) and stores the names contiguously (no
  429. +padding). Another block for each entry is handled by kmem_cache too.
  430. +During building dir blocks, aufs creates hash list and judging whether
  431. +the entry is whiteouted by its upper branch or already listed.
  432. +The merged result is cached in the corresponding inode object and
  433. +maintained by a customizable life-time option.
  434. +
  435. +Some people may call it can be a security hole or invite DoS attack
  436. +since the opened and once readdir-ed dir (file object) holds its entry
  437. +list and becomes a pressure for system memory. But I'd say it is similar
  438. +to files under /proc or /sys. The virtual files in them also holds a
  439. +memory page (generally) while they are opened. When an idea to reduce
  440. +memory for them is introduced, it will be applied to aufs too.
  441. +For those who really hate this situation, I've developed readdir(3)
  442. +library which operates this merging in userspace. You just need to set
  443. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  444. +kernel space for readdir(3).
  445. +
  446. +
  447. +Workqueue
  448. +----------------------------------------------------------------------
  449. +Aufs sometimes requires privilege access to a branch. For instance,
  450. +in copy-up/down operation. When a user process is going to make changes
  451. +to a file which exists in the lower readonly branch only, and the mode
  452. +of one of ancestor directories may not be writable by a user
  453. +process. Here aufs copy-up the file with its ancestors and they may
  454. +require privilege to set its owner/group/mode/etc.
  455. +This is a typical case of a application character of aufs (see
  456. +Introduction).
  457. +
  458. +Aufs uses workqueue synchronously for this case. It creates its own
  459. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  460. +passes the request to call mkdir or write (for example), and wait for
  461. +its completion. This approach solves a problem of a signal handler
  462. +simply.
  463. +If aufs didn't adopt the workqueue and changed the privilege of the
  464. +process, then the process may receive the unexpected SIGXFSZ or other
  465. +signals.
  466. +
  467. +Also aufs uses the system global workqueue ("events" kernel thread) too
  468. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  469. +whiteout base and etc. This is unrelated to a privilege.
  470. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  471. +superblock at the beginning, at the same time waits for the completion
  472. +of all queued asynchronous tasks.
  473. +
  474. +
  475. +Whiteout
  476. +----------------------------------------------------------------------
  477. +The whiteout in aufs is very similar to Unionfs's. That is represented
  478. +by its filename. UnionMount takes an approach of a file mode, but I am
  479. +afraid several utilities (find(1) or something) will have to support it.
  480. +
  481. +Basically the whiteout represents "logical deletion" which stops aufs to
  482. +lookup further, but also it represents "dir is opaque" which also stop
  483. +further lookup.
  484. +
  485. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  486. +In order to make several functions in a single systemcall to be
  487. +revertible, aufs adopts an approach to rename a directory to a temporary
  488. +unique whiteouted name.
  489. +For example, in rename(2) dir where the target dir already existed, aufs
  490. +renames the target dir to a temporary unique whiteouted name before the
  491. +actual rename on a branch, and then handles other actions (make it opaque,
  492. +update the attributes, etc). If an error happens in these actions, aufs
  493. +simply renames the whiteouted name back and returns an error. If all are
  494. +succeeded, aufs registers a function to remove the whiteouted unique
  495. +temporary name completely and asynchronously to the system global
  496. +workqueue.
  497. +
  498. +
  499. +Copy-up
  500. +----------------------------------------------------------------------
  501. +It is a well-known feature or concept.
  502. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  503. +internally and makes change to the new file on the upper writable branch.
  504. +When the trigger systemcall does not update the timestamps of the parent
  505. +dir, aufs reverts it after copy-up.
  506. +
  507. +
  508. +Move-down (aufs3.9 and later)
  509. +----------------------------------------------------------------------
  510. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  511. +the lower readonly branch to the upper writable branch when a user
  512. +changes something about the file.
  513. +"Move-down" is an opposite action of copy-up. Basically this action is
  514. +ran manually instead of automatically and internally.
  515. +For desgin and implementation, aufs has to consider these issues.
  516. +- whiteout for the file may exist on the lower branch.
  517. +- ancestor directories may not exist on the lower branch.
  518. +- diropq for the ancestor directories may exist on the upper branch.
  519. +- free space on the lower branch will reduce.
  520. +- another access to the file may happen during moving-down, including
  521. + UDBA (see "Revalidate Dentry and UDBA").
  522. +- the file should not be hard-linked nor pseudo-linked. they should be
  523. + handled by auplink utility later.
  524. +
  525. +Sometimes users want to move-down a file from the upper writable branch
  526. +to the lower readonly or writable branch. For instance,
  527. +- the free space of the upper writable branch is going to run out.
  528. +- create a new intermediate branch between the upper and lower branch.
  529. +- etc.
  530. +
  531. +For this purpose, use "aumvdown" command in aufs-util.git.
  532. diff -Naur null/Documentation/filesystems/aufs/design/03atomic_open.txt linux-6.6.63/Documentation/filesystems/aufs/design/03atomic_open.txt
  533. --- /dev/null
  534. +++ linux-6.6.63/Documentation/filesystems/aufs/design/03atomic_open.txt 2024-12-18 15:12:59.291519638 +0300
  535. @@ -0,0 +1,85 @@
  536. +
  537. +# Copyright (C) 2015-2022 Junjiro R. Okajima
  538. +#
  539. +# This program is free software; you can redistribute it and/or modify
  540. +# it under the terms of the GNU General Public License as published by
  541. +# the Free Software Foundation; either version 2 of the License, or
  542. +# (at your option) any later version.
  543. +#
  544. +# This program is distributed in the hope that it will be useful,
  545. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  546. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  547. +# GNU General Public License for more details.
  548. +#
  549. +# You should have received a copy of the GNU General Public License
  550. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  551. +
  552. +Support for a branch who has its ->atomic_open()
  553. +----------------------------------------------------------------------
  554. +The filesystems who implement its ->atomic_open() are not majority. For
  555. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  556. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  557. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  558. +sure whether all filesystems who have ->atomic_open() behave like this,
  559. +but NFSv4 surely returns the error.
  560. +
  561. +In order to support ->atomic_open() for aufs, there are a few
  562. +approaches.
  563. +
  564. +A. Introduce aufs_atomic_open()
  565. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  566. + branch fs.
  567. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  568. + an aufs user Pip Cet's approach
  569. + - calls aufs_create(), VFS finish_open() and notify_change().
  570. + - pass fake-mode to finish_open(), and then correct the mode by
  571. + notify_change().
  572. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  573. + - no aufs_atomic_open().
  574. + - aufs_lookup() registers the TID to an aufs internal object.
  575. + - aufs_create() does nothing when the matching TID is registered, but
  576. + registers the mode.
  577. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  578. + TID is registered.
  579. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  580. + credential
  581. + - no aufs_atomic_open().
  582. + - aufs_create() registers the TID to an internal object. this info
  583. + represents "this process created this file just now."
  584. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  585. + registered TID and re-try open() with superuser's credential.
  586. +
  587. +Pros and cons for each approach.
  588. +
  589. +A.
  590. + - straightforward but highly depends upon VFS internal.
  591. + - the atomic behavaiour is kept.
  592. + - some of parameters such as nameidata are hard to reproduce for
  593. + branch fs.
  594. + - large overhead.
  595. +B.
  596. + - easy to implement.
  597. + - the atomic behavaiour is lost.
  598. +C.
  599. + - the atomic behavaiour is kept.
  600. + - dirty and tricky.
  601. + - VFS checks whether the file is created correctly after calling
  602. + ->create(), which means this approach doesn't work.
  603. +D.
  604. + - easy to implement.
  605. + - the atomic behavaiour is lost.
  606. + - to open a file with superuser's credential and give it to a user
  607. + process is a bad idea, since the file object keeps the credential
  608. + in it. It may affect LSM or something. This approach doesn't work
  609. + either.
  610. +
  611. +The approach A is ideal, but it hard to implement. So here is a
  612. +variation of A, which is to be implemented.
  613. +
  614. +A-1. Introduce aufs_atomic_open()
  615. + - calls branch fs ->atomic_open() if exists. otherwise calls
  616. + vfs_create() and finish_open().
  617. + - the demerit is that the several checks after branch fs
  618. + ->atomic_open() are lost. in the ordinary case, the checks are
  619. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  620. + be implemented in aufs, but not all I am afraid.
  621. diff -Naur null/Documentation/filesystems/aufs/design/03lookup.txt linux-6.6.63/Documentation/filesystems/aufs/design/03lookup.txt
  622. --- /dev/null
  623. +++ linux-6.6.63/Documentation/filesystems/aufs/design/03lookup.txt 2024-12-18 15:12:59.291519638 +0300
  624. @@ -0,0 +1,113 @@
  625. +
  626. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  627. +#
  628. +# This program is free software; you can redistribute it and/or modify
  629. +# it under the terms of the GNU General Public License as published by
  630. +# the Free Software Foundation; either version 2 of the License, or
  631. +# (at your option) any later version.
  632. +#
  633. +# This program is distributed in the hope that it will be useful,
  634. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  635. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  636. +# GNU General Public License for more details.
  637. +#
  638. +# You should have received a copy of the GNU General Public License
  639. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  640. +
  641. +Lookup in a Branch
  642. +----------------------------------------------------------------------
  643. +Since aufs has a character of sub-VFS (see Introduction), it operates
  644. +lookup for branches as VFS does. It may be a heavy work. But almost all
  645. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  646. +directly connected to its parent. Digging down the directory hierarchy
  647. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  648. +aufs calls it.
  649. +
  650. +When a branch is a remote filesystem, aufs basically relies upon its
  651. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  652. +them.
  653. +For d_revalidate, aufs implements three levels of revalidate tests. See
  654. +"Revalidate Dentry and UDBA" in detail.
  655. +
  656. +
  657. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  658. +----------------------------------------------------------------------
  659. +Let's try case study.
  660. +- aufs has two branches, upper readwrite and lower readonly.
  661. + /au = /rw + /ro
  662. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  663. +- user invoked "chmod a+rx /au/dirA"
  664. +- the internal copy-up is activated and "/rw/dirA" is created and its
  665. + permission bits are set to world readable.
  666. +- then "/au/dirA" becomes world readable?
  667. +
  668. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  669. +or it may be a natively readonly filesystem. If aufs respects the lower
  670. +branch, it should not respond readdir request from other users. But user
  671. +allowed it by chmod. Should really aufs rejects showing the entries
  672. +under /ro/dirA?
  673. +
  674. +To be honest, I don't have a good solution for this case. So aufs
  675. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  676. +users.
  677. +When dirperm1 is specified, aufs checks only the highest one for the
  678. +directory permission, and shows the entries. Otherwise, as usual, checks
  679. +every dir existing on all branches and rejects the request.
  680. +
  681. +As a side effect, dirperm1 option improves the performance of aufs
  682. +because the number of permission check is reduced when the number of
  683. +branch is many.
  684. +
  685. +
  686. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  687. +----------------------------------------------------------------------
  688. +Generally VFS helpers re-validate a dentry as a part of lookup.
  689. +0. digging down the directory hierarchy.
  690. +1. lock the parent dir by its i_mutex.
  691. +2. lookup the final (child) entry.
  692. +3. revalidate it.
  693. +4. call the actual operation (create, unlink, etc.)
  694. +5. unlock the parent dir
  695. +
  696. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  697. +called. Actually aufs implements it and checks the dentry on a branch is
  698. +still valid.
  699. +But it is not enough. Because aufs has to release the lock for the
  700. +parent dir on a branch at the end of ->lookup() (step 2) and
  701. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  702. +held by VFS.
  703. +If the file on a branch is changed directly, eg. bypassing aufs, after
  704. +aufs released the lock, then the subsequent operation may cause
  705. +something unpleasant result.
  706. +
  707. +This situation is a result of VFS architecture, ->lookup() and
  708. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  709. +design from VFS's point of view. It is just not suitable for sub-VFS
  710. +character in aufs.
  711. +
  712. +Aufs supports such case by three level of revalidation which is
  713. +selectable by user.
  714. +1. Simple Revalidate
  715. + Addition to the native flow in VFS's, confirm the child-parent
  716. + relationship on the branch just after locking the parent dir on the
  717. + branch in the "actual operation" (step 4). When this validation
  718. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  719. + checks the validation of the dentry on branches.
  720. +2. Monitor Changes Internally by Inotify/Fsnotify
  721. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  722. + the dentry on the branch, and returns EBUSY if it finds different
  723. + dentry.
  724. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  725. + during it is in cache. When the event is notified, aufs registers a
  726. + function to kernel 'events' thread by schedule_work(). And the
  727. + function sets some special status to the cached aufs dentry and inode
  728. + private data. If they are not cached, then aufs has nothing to
  729. + do. When the same file is accessed through aufs (step 0-3) later,
  730. + aufs will detect the status and refresh all necessary data.
  731. + In this mode, aufs has to ignore the event which is fired by aufs
  732. + itself.
  733. +3. No Extra Validation
  734. + This is the simplest test and doesn't add any additional revalidation
  735. + test, and skip the revalidation in step 4. It is useful and improves
  736. + aufs performance when system surely hide the aufs branches from user,
  737. + by over-mounting something (or another method).
  738. diff -Naur null/Documentation/filesystems/aufs/design/04branch.txt linux-6.6.63/Documentation/filesystems/aufs/design/04branch.txt
  739. --- /dev/null
  740. +++ linux-6.6.63/Documentation/filesystems/aufs/design/04branch.txt 2024-12-18 15:12:59.291519638 +0300
  741. @@ -0,0 +1,74 @@
  742. +
  743. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  744. +#
  745. +# This program is free software; you can redistribute it and/or modify
  746. +# it under the terms of the GNU General Public License as published by
  747. +# the Free Software Foundation; either version 2 of the License, or
  748. +# (at your option) any later version.
  749. +#
  750. +# This program is distributed in the hope that it will be useful,
  751. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  752. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  753. +# GNU General Public License for more details.
  754. +#
  755. +# You should have received a copy of the GNU General Public License
  756. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  757. +
  758. +Branch Manipulation
  759. +
  760. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  761. +and changing its permission/attribute, there are a lot of works to do.
  762. +
  763. +
  764. +Add a Branch
  765. +----------------------------------------------------------------------
  766. +o Confirm the adding dir exists outside of aufs, including loopback
  767. + mount, and its various attributes.
  768. +o Initialize the xino file and whiteout bases if necessary.
  769. + See struct.txt.
  770. +
  771. +o Check the owner/group/mode of the directory
  772. + When the owner/group/mode of the adding directory differs from the
  773. + existing branch, aufs issues a warning because it may impose a
  774. + security risk.
  775. + For example, when a upper writable branch has a world writable empty
  776. + top directory, a malicious user can create any files on the writable
  777. + branch directly, like copy-up and modify manually. If something like
  778. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  779. + writable branch, and the writable branch is world-writable, then a
  780. + malicious guy may create /etc/passwd on the writable branch directly
  781. + and the infected file will be valid in aufs.
  782. + I am afraid it can be a security issue, but aufs can do nothing except
  783. + producing a warning.
  784. +
  785. +
  786. +Delete a Branch
  787. +----------------------------------------------------------------------
  788. +o Confirm the deleting branch is not busy
  789. + To be general, there is one merit to adopt "remount" interface to
  790. + manipulate branches. It is to discard caches. At deleting a branch,
  791. + aufs checks the still cached (and connected) dentries and inodes. If
  792. + there are any, then they are all in-use. An inode without its
  793. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  794. +
  795. + For the cached one, aufs checks whether the same named entry exists on
  796. + other branches.
  797. + If the cached one is a directory, because aufs provides a merged view
  798. + to users, as long as one dir is left on any branch aufs can show the
  799. + dir to users. In this case, the branch can be removed from aufs.
  800. + Otherwise aufs rejects deleting the branch.
  801. +
  802. + If any file on the deleting branch is opened by aufs, then aufs
  803. + rejects deleting.
  804. +
  805. +
  806. +Modify the Permission of a Branch
  807. +----------------------------------------------------------------------
  808. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  809. + See struct.txt.
  810. +
  811. +o rw --> ro: Confirm the modifying branch is not busy
  812. + Aufs rejects the request if any of these conditions are true.
  813. + - a file on the branch is mmap-ed.
  814. + - a regular file on the branch is opened for write and there is no
  815. + same named entry on the upper branch.
  816. diff -Naur null/Documentation/filesystems/aufs/design/05wbr_policy.txt linux-6.6.63/Documentation/filesystems/aufs/design/05wbr_policy.txt
  817. --- /dev/null
  818. +++ linux-6.6.63/Documentation/filesystems/aufs/design/05wbr_policy.txt 2024-12-18 15:12:59.291519638 +0300
  819. @@ -0,0 +1,64 @@
  820. +
  821. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  822. +#
  823. +# This program is free software; you can redistribute it and/or modify
  824. +# it under the terms of the GNU General Public License as published by
  825. +# the Free Software Foundation; either version 2 of the License, or
  826. +# (at your option) any later version.
  827. +#
  828. +# This program is distributed in the hope that it will be useful,
  829. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  830. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  831. +# GNU General Public License for more details.
  832. +#
  833. +# You should have received a copy of the GNU General Public License
  834. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  835. +
  836. +Policies to Select One among Multiple Writable Branches
  837. +----------------------------------------------------------------------
  838. +When the number of writable branch is more than one, aufs has to decide
  839. +the target branch for file creation or copy-up. By default, the highest
  840. +writable branch which has the parent (or ancestor) dir of the target
  841. +file is chosen (top-down-parent policy).
  842. +By user's request, aufs implements some other policies to select the
  843. +writable branch, for file creation several policies, round-robin,
  844. +most-free-space, and other policies. For copy-up, top-down-parent,
  845. +bottom-up-parent, bottom-up and others.
  846. +
  847. +As expected, the round-robin policy selects the branch in circular. When
  848. +you have two writable branches and creates 10 new files, 5 files will be
  849. +created for each branch. mkdir(2) systemcall is an exception. When you
  850. +create 10 new directories, all will be created on the same branch.
  851. +And the most-free-space policy selects the one which has most free
  852. +space among the writable branches. The amount of free space will be
  853. +checked by aufs internally, and users can specify its time interval.
  854. +
  855. +The policies for copy-up is more simple,
  856. +top-down-parent is equivalent to the same named on in create policy,
  857. +bottom-up-parent selects the writable branch where the parent dir
  858. +exists and the nearest upper one from the copyup-source,
  859. +bottom-up selects the nearest upper writable branch from the
  860. +copyup-source, regardless the existence of the parent dir.
  861. +
  862. +There are some rules or exceptions to apply these policies.
  863. +- If there is a readonly branch above the policy-selected branch and
  864. + the parent dir is marked as opaque (a variation of whiteout), or the
  865. + target (creating) file is whiteout-ed on the upper readonly branch,
  866. + then the result of the policy is ignored and the target file will be
  867. + created on the nearest upper writable branch than the readonly branch.
  868. +- If there is a writable branch above the policy-selected branch and
  869. + the parent dir is marked as opaque or the target file is whiteouted
  870. + on the branch, then the result of the policy is ignored and the target
  871. + file will be created on the highest one among the upper writable
  872. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  873. + it as usual.
  874. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  875. + They try selecting the branch where the source exists as possible
  876. + since copyup a large file will take long time. If it can't be,
  877. + ie. the branch where the source exists is readonly, then they will
  878. + follow the copyup policy.
  879. +- There is an exception for rename(2) when the target exists.
  880. + If the rename target exists, aufs compares the index of the branches
  881. + where the source and the target exists and selects the higher
  882. + one. If the selected branch is readonly, then aufs follows the
  883. + copyup policy.
  884. diff -Naur null/Documentation/filesystems/aufs/design/06dirren.dot linux-6.6.63/Documentation/filesystems/aufs/design/06dirren.dot
  885. --- /dev/null
  886. +++ linux-6.6.63/Documentation/filesystems/aufs/design/06dirren.dot 2024-12-18 15:12:59.291519638 +0300
  887. @@ -0,0 +1,44 @@
  888. +
  889. +// to view this graph, run dot(1) command in GRAPHVIZ.
  890. +//
  891. +// This program is free software; you can redistribute it and/or modify
  892. +// it under the terms of the GNU General Public License as published by
  893. +// the Free Software Foundation; either version 2 of the License, or
  894. +// (at your option) any later version.
  895. +//
  896. +// This program is distributed in the hope that it will be useful,
  897. +// but WITHOUT ANY WARRANTY; without even the implied warranty of
  898. +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  899. +// GNU General Public License for more details.
  900. +//
  901. +// You should have received a copy of the GNU General Public License
  902. +// along with this program. If not, see <http://www.gnu.org/licenses/>.
  903. +
  904. +digraph G {
  905. +node [shape=box];
  906. +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
  907. +
  908. +node [shape=oval];
  909. +
  910. +aufs_rename -> whinfo [label="store/remove"];
  911. +
  912. +node [shape=oval];
  913. +inode_list [label="h_inum list in branch\ncache"];
  914. +
  915. +node [shape=box];
  916. +whinode [label="h_inum list file"];
  917. +
  918. +node [shape=oval];
  919. +brmgmt [label="br_add/del/mod/umount"];
  920. +
  921. +brmgmt -> inode_list [label="create/remove"];
  922. +brmgmt -> whinode [label="load/store"];
  923. +
  924. +inode_list -> whinode [style=dashed,dir=both];
  925. +
  926. +aufs_rename -> inode_list [label="add/del"];
  927. +
  928. +aufs_lookup -> inode_list [label="search"];
  929. +
  930. +aufs_lookup -> whinfo [label="load/remove"];
  931. +}
  932. diff -Naur null/Documentation/filesystems/aufs/design/06dirren.txt linux-6.6.63/Documentation/filesystems/aufs/design/06dirren.txt
  933. --- /dev/null
  934. +++ linux-6.6.63/Documentation/filesystems/aufs/design/06dirren.txt 2024-12-18 15:12:59.291519638 +0300
  935. @@ -0,0 +1,102 @@
  936. +
  937. +# Copyright (C) 2017-2022 Junjiro R. Okajima
  938. +#
  939. +# This program is free software; you can redistribute it and/or modify
  940. +# it under the terms of the GNU General Public License as published by
  941. +# the Free Software Foundation; either version 2 of the License, or
  942. +# (at your option) any later version.
  943. +#
  944. +# This program is distributed in the hope that it will be useful,
  945. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  946. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  947. +# GNU General Public License for more details.
  948. +#
  949. +# You should have received a copy of the GNU General Public License
  950. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  951. +
  952. +Special handling for renaming a directory (DIRREN)
  953. +----------------------------------------------------------------------
  954. +First, let's assume we have a simple usecase.
  955. +
  956. +- /u = /rw + /ro
  957. +- /rw/dirA exists
  958. +- /ro/dirA and /ro/dirA/file exist too
  959. +- there is no dirB on both branches
  960. +- a user issues rename("dirA", "dirB")
  961. +
  962. +Now, what should aufs behave against this rename(2)?
  963. +There are a few possible cases.
  964. +
  965. +A. returns EROFS.
  966. + since dirA exists on a readonly branch which cannot be renamed.
  967. +B. returns EXDEV.
  968. + it is possible to copy-up dirA (only the dir itself), but the child
  969. + entries ("file" in this case) should not be. it must be a bad
  970. + approach to copy-up recursively.
  971. +C. returns a success.
  972. + even the branch /ro is readonly, aufs tries renaming it. Obviously it
  973. + is a violation of aufs' policy.
  974. +D. construct an extra information which indicates that /ro/dirA should
  975. + be handled as the name of dirB.
  976. + overlayfs has a similar feature called REDIRECT.
  977. +
  978. +Until now, aufs implements the case B only which returns EXDEV, and
  979. +expects the userspace application behaves like mv(1) which tries
  980. +issueing rename(2) recursively.
  981. +
  982. +A new aufs feature called DIRREN is introduced which implements the case
  983. +D. There are several "extra information" added.
  984. +
  985. +1. detailed info per renamed directory
  986. + path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
  987. +2. the inode-number list of directories on a branch
  988. + path: /rw/dirB/$AUFS_WH_DR_BRHINO
  989. +
  990. +The filename of "detailed info per directory" represents the lower
  991. +branch, and its format is
  992. +- a type of the branch id
  993. + one of these.
  994. + + uuid (not implemented yet)
  995. + + fsid
  996. + + dev
  997. +- the inode-number of the branch root dir
  998. +
  999. +And it contains these info in a single regular file.
  1000. +- magic number
  1001. +- branch's inode-number of the logically renamed dir
  1002. +- the name of the before-renamed dir
  1003. +
  1004. +The "detailed info per directory" file is created in aufs rename(2), and
  1005. +loaded in any lookup.
  1006. +The info is considered in lookup for the matching case only. Here
  1007. +"matching" means that the root of branch (in the info filename) is same
  1008. +to the current looking-up branch. After looking-up the before-renamed
  1009. +name, the inode-number is compared. And the matched dentry is used.
  1010. +
  1011. +The "inode-number list of directories" is a regular file which contains
  1012. +simply the inode-numbers on the branch. The file is created or updated
  1013. +in removing the branch, and loaded in adding the branch. Its lifetime is
  1014. +equal to the branch.
  1015. +The list is referred in lookup, and when the current target inode is
  1016. +found in the list, the aufs tries loading the "detailed info per
  1017. +directory" and get the changed and valid name of the dir.
  1018. +
  1019. +Theoretically these "extra informaiton" may be able to be put into XATTR
  1020. +in the dir inode. But aufs doesn't choose this way because
  1021. +1. XATTR may not be supported by the branch (or its configuration)
  1022. +2. XATTR may have its size limit.
  1023. +3. XATTR may be less easy to convert than a regular file, when the
  1024. + format of the info is changed in the future.
  1025. +At the same time, I agree that the regular file approach is much slower
  1026. +than XATTR approach. So, in the future, aufs may take the XATTR or other
  1027. +better approach.
  1028. +
  1029. +This DIRREN feature is enabled by aufs configuration, and is activated
  1030. +by a new mount option.
  1031. +
  1032. +For the more complicated case, there is a work with UDBA option, which
  1033. +is to dected the direct access to the branches (by-passing aufs) and to
  1034. +maintain the cashes in aufs. Since a single cached aufs dentry may
  1035. +contains two names, before- and after-rename, the name comparision in
  1036. +UDBA handler may not work correctly. In this case, the behaviour will be
  1037. +equivalen to udba=reval case.
  1038. diff -Naur null/Documentation/filesystems/aufs/design/06fhsm.txt linux-6.6.63/Documentation/filesystems/aufs/design/06fhsm.txt
  1039. --- /dev/null
  1040. +++ linux-6.6.63/Documentation/filesystems/aufs/design/06fhsm.txt 2024-12-18 15:12:59.291519638 +0300
  1041. @@ -0,0 +1,118 @@
  1042. +
  1043. +# Copyright (C) 2011-2022 Junjiro R. Okajima
  1044. +#
  1045. +# This program is free software; you can redistribute it and/or modify
  1046. +# it under the terms of the GNU General Public License as published by
  1047. +# the Free Software Foundation; either version 2 of the License, or
  1048. +# (at your option) any later version.
  1049. +#
  1050. +# This program is distributed in the hope that it will be useful,
  1051. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1052. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1053. +# GNU General Public License for more details.
  1054. +#
  1055. +# You should have received a copy of the GNU General Public License
  1056. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1057. +
  1058. +File-based Hierarchical Storage Management (FHSM)
  1059. +----------------------------------------------------------------------
  1060. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  1061. +storage world. Aufs provides this feature as file-based with multiple
  1062. +writable branches, based upon the principle of "Colder, the Lower".
  1063. +Here the word "colder" means that the less used files, and "lower" means
  1064. +that the position in the order of the stacked branches vertically.
  1065. +These multiple writable branches are prioritized, ie. the topmost one
  1066. +should be the fastest drive and be used heavily.
  1067. +
  1068. +o Characters in aufs FHSM story
  1069. +- aufs itself and a new branch attribute.
  1070. +- a new ioctl interface to move-down and to establish a connection with
  1071. + the daemon ("move-down" is a converse of "copy-up").
  1072. +- userspace tool and daemon.
  1073. +
  1074. +The userspace daemon establishes a connection with aufs and waits for
  1075. +the notification. The notified information is very similar to struct
  1076. +statfs containing the number of consumed blocks and inodes.
  1077. +When the consumed blocks/inodes of a branch exceeds the user-specified
  1078. +upper watermark, the daemon activates its move-down process until the
  1079. +consumed blocks/inodes reaches the user-specified lower watermark.
  1080. +
  1081. +The actual move-down is done by aufs based upon the request from
  1082. +user-space since we need to maintain the inode number and the internal
  1083. +pointer arrays in aufs.
  1084. +
  1085. +Currently aufs FHSM handles the regular files only. Additionally they
  1086. +must not be hard-linked nor pseudo-linked.
  1087. +
  1088. +
  1089. +o Cowork of aufs and the user-space daemon
  1090. + During the userspace daemon established the connection, aufs sends a
  1091. + small notification to it whenever aufs writes something into the
  1092. + writable branch. But it may cost high since aufs issues statfs(2)
  1093. + internally. So user can specify a new option to cache the
  1094. + info. Actually the notification is controlled by these factors.
  1095. + + the specified cache time.
  1096. + + classified as "force" by aufs internally.
  1097. + Until the specified time expires, aufs doesn't send the info
  1098. + except the forced cases. When aufs decide forcing, the info is always
  1099. + notified to userspace.
  1100. + For example, the number of free inodes is generally large enough and
  1101. + the shortage of it happens rarely. So aufs doesn't force the
  1102. + notification when creating a new file, directory and others. This is
  1103. + the typical case which aufs doesn't force.
  1104. + When aufs writes the actual filedata and the files consumes any of new
  1105. + blocks, the aufs forces notifying.
  1106. +
  1107. +
  1108. +o Interfaces in aufs
  1109. +- New branch attribute.
  1110. + + fhsm
  1111. + Specifies that the branch is managed by FHSM feature. In other word,
  1112. + participant in the FHSM.
  1113. + When nofhsm is set to the branch, it will not be the source/target
  1114. + branch of the move-down operation. This attribute is set
  1115. + independently from coo and moo attributes, and if you want full
  1116. + FHSM, you should specify them as well.
  1117. +- New mount option.
  1118. + + fhsm_sec
  1119. + Specifies a second to suppress many less important info to be
  1120. + notified.
  1121. +- New ioctl.
  1122. + + AUFS_CTL_FHSM_FD
  1123. + create a new file descriptor which userspace can read the notification
  1124. + (a subset of struct statfs) from aufs.
  1125. +- Module parameter 'brs'
  1126. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  1127. + be set.
  1128. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  1129. + When there are two or more branches with fhsm attributes,
  1130. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  1131. + terminates it. As a result of remounting and branch-manipulation, the
  1132. + number of branches with fhsm attribute can be one. In this case,
  1133. + /sbin/mount.aufs will terminate the user-space daemon.
  1134. +
  1135. +
  1136. +Finally the operation is done as these steps in kernel-space.
  1137. +- make sure that,
  1138. + + no one else is using the file.
  1139. + + the file is not hard-linked.
  1140. + + the file is not pseudo-linked.
  1141. + + the file is a regular file.
  1142. + + the parent dir is not opaqued.
  1143. +- find the target writable branch.
  1144. +- make sure the file is not whiteout-ed by the upper (than the target)
  1145. + branch.
  1146. +- make the parent dir on the target branch.
  1147. +- mutex lock the inode on the branch.
  1148. +- unlink the whiteout on the target branch (if exists).
  1149. +- lookup and create the whiteout-ed temporary name on the target branch.
  1150. +- copy the file as the whiteout-ed temporary name on the target branch.
  1151. +- rename the whiteout-ed temporary name to the original name.
  1152. +- unlink the file on the source branch.
  1153. +- maintain the internal pointer array and the external inode number
  1154. + table (XINO).
  1155. +- maintain the timestamps and other attributes of the parent dir and the
  1156. + file.
  1157. +
  1158. +And of course, in every step, an error may happen. So the operation
  1159. +should restore the original file state after an error happens.
  1160. diff -Naur null/Documentation/filesystems/aufs/design/06mmap.txt linux-6.6.63/Documentation/filesystems/aufs/design/06mmap.txt
  1161. --- /dev/null
  1162. +++ linux-6.6.63/Documentation/filesystems/aufs/design/06mmap.txt 2024-12-18 15:12:59.291519638 +0300
  1163. @@ -0,0 +1,72 @@
  1164. +
  1165. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  1166. +#
  1167. +# This program is free software; you can redistribute it and/or modify
  1168. +# it under the terms of the GNU General Public License as published by
  1169. +# the Free Software Foundation; either version 2 of the License, or
  1170. +# (at your option) any later version.
  1171. +#
  1172. +# This program is distributed in the hope that it will be useful,
  1173. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1174. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1175. +# GNU General Public License for more details.
  1176. +#
  1177. +# You should have received a copy of the GNU General Public License
  1178. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1179. +
  1180. +mmap(2) -- File Memory Mapping
  1181. +----------------------------------------------------------------------
  1182. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1183. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1184. +->mmap().
  1185. +This approach is simple and good, but there is one problem.
  1186. +Under /proc, several entries show the mmapped files by its path (with
  1187. +device and inode number), and the printed path will be the path on the
  1188. +branch fs's instead of virtual aufs's.
  1189. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1190. +user) may expect the path on aufs.
  1191. +
  1192. +To address this issue, aufs adds a new member called vm_prfile in struct
  1193. +vm_area_struct (and struct vm_region). The original vm_file points to
  1194. +the file on the branch fs in order to handle everything correctly as
  1195. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1196. +show-functions in procfs refers to vm_prfile if it is set.
  1197. +Also we need to maintain several other places where touching vm_file
  1198. +such like
  1199. +- fork()/clone() copies vma and the reference count of vm_file is
  1200. + incremented.
  1201. +- merging vma maintains the ref count too.
  1202. +
  1203. +This is not a good approach. It just fakes the printed path. But it
  1204. +leaves all behaviour around f_mapping unchanged. This is surely an
  1205. +advantage.
  1206. +Actually aufs had adopted another complicated approach which calls
  1207. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1208. +approach, aufs met a hard problem and I could not solve it without
  1209. +switching the approach.
  1210. +
  1211. +There may be one more another approach which is
  1212. +- bind-mount the branch-root onto the aufs-root internally
  1213. +- grab the new vfsmount (ie. struct mount)
  1214. +- lazy-umount the branch-root internally
  1215. +- in open(2) the aufs-file, open the branch-file with the hidden
  1216. + vfsmount (instead of the original branch's vfsmount)
  1217. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1218. + but it may be possible from userspace by the mount helper.
  1219. +
  1220. +Adding the internal hidden vfsmount and using it in opening a file, the
  1221. +file path under /proc will be printed correctly. This approach looks
  1222. +smarter, but is not possible I am afraid.
  1223. +- aufs-root may be bind-mount later. when it happens, another hidden
  1224. + vfsmount will be required.
  1225. +- it is hard to get the chance to bind-mount and lazy-umount
  1226. + + in kernel-space, FS can have vfsmount in open(2) via
  1227. + file->f_path, and aufs can know its vfsmount. But several locks are
  1228. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1229. + here, then it may cause a deadlock.
  1230. + + in user-space, bind-mount doesn't invoke the mount helper.
  1231. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1232. + means a new member vm_prinode will be necessary. this is essentially
  1233. + equivalent to vm_prfile described above.
  1234. +
  1235. +I have to give up this "looks-smater" approach.
  1236. diff -Naur null/Documentation/filesystems/aufs/design/06xattr.txt linux-6.6.63/Documentation/filesystems/aufs/design/06xattr.txt
  1237. --- /dev/null
  1238. +++ linux-6.6.63/Documentation/filesystems/aufs/design/06xattr.txt 2024-12-18 15:12:59.291519638 +0300
  1239. @@ -0,0 +1,94 @@
  1240. +
  1241. +# Copyright (C) 2014-2022 Junjiro R. Okajima
  1242. +#
  1243. +# This program is free software; you can redistribute it and/or modify
  1244. +# it under the terms of the GNU General Public License as published by
  1245. +# the Free Software Foundation; either version 2 of the License, or
  1246. +# (at your option) any later version.
  1247. +#
  1248. +# This program is distributed in the hope that it will be useful,
  1249. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1250. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1251. +# GNU General Public License for more details.
  1252. +#
  1253. +# You should have received a copy of the GNU General Public License
  1254. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1255. +
  1256. +Listing XATTR/EA and getting the value
  1257. +----------------------------------------------------------------------
  1258. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1259. +shows the values from the topmost existing file. This behaviour is good
  1260. +for the non-dir entries since the bahaviour exactly matches the shown
  1261. +information. But for the directories, aufs considers all the same named
  1262. +entries on the lower branches. Which means, if one of the lower entry
  1263. +rejects readdir call, then aufs returns an error even if the topmost
  1264. +entry allows it. This behaviour is necessary to respect the branch fs's
  1265. +security, but can make users confused since the user-visible standard
  1266. +attributes don't match the behaviour.
  1267. +To address this issue, aufs has a mount option called dirperm1 which
  1268. +checks the permission for the topmost entry only, and ignores the lower
  1269. +entry's permission.
  1270. +
  1271. +A similar issue can happen around XATTR.
  1272. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1273. +always set. Otherwise these very unpleasant situation would happen.
  1274. +- listxattr(2) may return the duplicated entries.
  1275. +- users may not be able to remove or reset the XATTR forever,
  1276. +
  1277. +
  1278. +XATTR/EA support in the internal (copy,move)-(up,down)
  1279. +----------------------------------------------------------------------
  1280. +Generally the extended attributes of inode are categorized as these.
  1281. +- "security" for LSM and capability.
  1282. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1283. + fs generally.
  1284. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1285. +- "user" for userspace, 'user_xattr' mount option is required for the
  1286. + branch fs generally.
  1287. +
  1288. +Moreover there are some other categories. Aufs handles these rather
  1289. +unpopular categories as the ordinary ones, ie. there is no special
  1290. +condition nor exception.
  1291. +
  1292. +In copy-up, the support for XATTR on the dst branch may differ from the
  1293. +src branch. In this case, the copy-up operation will get an error and
  1294. +the original user operation which triggered the copy-up will fail. It
  1295. +can happen that even all copy-up will fail.
  1296. +When both of src and dst branches support XATTR and if an error occurs
  1297. +during copying XATTR, then the copy-up should fail obviously. That is a
  1298. +good reason and aufs should return an error to userspace. But when only
  1299. +the src branch support that XATTR, aufs should not return an error.
  1300. +For example, the src branch supports ACL but the dst branch doesn't
  1301. +because the dst branch may natively un-support it or temporary
  1302. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1303. +may NOT return an error even if the XATTR is not supported. It is
  1304. +totally up to the branch fs.
  1305. +
  1306. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1307. +fs, then aufs tries removing the just copied entry and returns the error
  1308. +to the userspace. The worst case of this situation will be all copy-up
  1309. +will fail.
  1310. +
  1311. +For the copy-up operation, there two basic approaches.
  1312. +- copy the specified XATTR only (by category above), and return the
  1313. + error unconditionally if it happens.
  1314. +- copy all XATTR, and ignore the error on the specified category only.
  1315. +
  1316. +In order to support XATTR and to implement the correct behaviour, aufs
  1317. +chooses the latter approach and introduces some new branch attributes,
  1318. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1319. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1320. +convenient, "icex" is also provided which means all "icex*" attributes
  1321. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1322. +
  1323. +The meaning of these attributes is to ignore the error from setting
  1324. +XATTR on that branch.
  1325. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1326. +error from the dst branch according to the specified attributes.
  1327. +
  1328. +Some XATTR may have its default value. The default value may come from
  1329. +the parent dir or the environment. If the default value is set at the
  1330. +file creating-time, it will be overwritten by copy-up.
  1331. +Some contradiction may happen I am afraid.
  1332. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1333. +now, aufs implements the branch attributes to ignore the error.
  1334. diff -Naur null/Documentation/filesystems/aufs/design/07export.txt linux-6.6.63/Documentation/filesystems/aufs/design/07export.txt
  1335. --- /dev/null
  1336. +++ linux-6.6.63/Documentation/filesystems/aufs/design/07export.txt 2024-12-18 15:12:59.291519638 +0300
  1337. @@ -0,0 +1,58 @@
  1338. +
  1339. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  1340. +#
  1341. +# This program is free software; you can redistribute it and/or modify
  1342. +# it under the terms of the GNU General Public License as published by
  1343. +# the Free Software Foundation; either version 2 of the License, or
  1344. +# (at your option) any later version.
  1345. +#
  1346. +# This program is distributed in the hope that it will be useful,
  1347. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1348. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1349. +# GNU General Public License for more details.
  1350. +#
  1351. +# You should have received a copy of the GNU General Public License
  1352. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1353. +
  1354. +Export Aufs via NFS
  1355. +----------------------------------------------------------------------
  1356. +Here is an approach.
  1357. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1358. + generation.
  1359. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1360. + store it in xigen file.
  1361. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1362. + file. it is necessary even if it is not unlinked, because any data of
  1363. + inode may be changed by UDBA.
  1364. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1365. + build file handle by
  1366. + + branch id (4 bytes)
  1367. + + superblock generation (4 bytes)
  1368. + + inode number (4 or 8 bytes)
  1369. + + parent dir inode number (4 or 8 bytes)
  1370. + + inode generation (4 bytes))
  1371. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1372. + bytes)
  1373. + + file handle for a branch (by exportfs_encode_fh())
  1374. +- fh_to_dentry():
  1375. + + find the index of a branch from its id in handle, and check it is
  1376. + still exist in aufs.
  1377. + + 1st level: get the inode number from handle and search it in cache.
  1378. + + 2nd level: if not found in cache, get the parent inode number from
  1379. + the handle and search it in cache. and then open the found parent
  1380. + dir, find the matching inode number by vfs_readdir() and get its
  1381. + name, and call lookup_one_len() for the target dentry.
  1382. + + 3rd level: if the parent dir is not cached, call
  1383. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1384. + build a pathname of it, convert it a pathname in aufs, call
  1385. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1386. + the 2nd level.
  1387. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1388. + for every branch, but not itself. to get this, (currently) aufs
  1389. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1390. + idea, but I didn't get other approach.
  1391. + + test the generation of the gotten inode.
  1392. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1393. + convert it into ESTALE for NFSD.
  1394. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1395. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1396. diff -Naur null/Documentation/filesystems/aufs/design/08shwh.txt linux-6.6.63/Documentation/filesystems/aufs/design/08shwh.txt
  1397. --- /dev/null
  1398. +++ linux-6.6.63/Documentation/filesystems/aufs/design/08shwh.txt 2024-12-18 15:12:59.291519638 +0300
  1399. @@ -0,0 +1,52 @@
  1400. +
  1401. +# Copyright (C) 2005-2022 Junjiro R. Okajima
  1402. +#
  1403. +# This program is free software; you can redistribute it and/or modify
  1404. +# it under the terms of the GNU General Public License as published by
  1405. +# the Free Software Foundation; either version 2 of the License, or
  1406. +# (at your option) any later version.
  1407. +#
  1408. +# This program is distributed in the hope that it will be useful,
  1409. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1410. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1411. +# GNU General Public License for more details.
  1412. +#
  1413. +# You should have received a copy of the GNU General Public License
  1414. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1415. +
  1416. +Show Whiteout Mode (shwh)
  1417. +----------------------------------------------------------------------
  1418. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1419. +them is very useful for users. For instance, creating a new middle layer
  1420. +(branch) by merging existing layers.
  1421. +
  1422. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1423. +When you have three branches,
  1424. +- Bottom: 'system', squashfs (underlying base system), read-only
  1425. +- Middle: 'mods', squashfs, read-only
  1426. +- Top: 'overlay', ram (tmpfs), read-write
  1427. +
  1428. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1429. +the changes made to the system during the session.
  1430. +When larger changes have been made, or smaller changes have accumulated,
  1431. +the size of the saved top layer data grows. At this point, it would be
  1432. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1433. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1434. +restoring save and load speed.
  1435. +
  1436. +This merging is simplified by the use of another aufs mount, of just the
  1437. +two overlay branches using the 'shwh' option.
  1438. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1439. + aufs /livesys/merge_union
  1440. +
  1441. +A merged view of these two branches is then available at
  1442. +/livesys/merge_union, and the new feature is that the whiteouts are
  1443. +visible!
  1444. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1445. +writing to all branches. Also the default mode for all branches is 'ro'.
  1446. +It is now possible to save the combined contents of the two overlay
  1447. +branches to a new squashfs, e.g.:
  1448. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1449. +
  1450. +This new squashfs archive can be stored on the boot device and the
  1451. +initramfs will use it to replace the old one at the next boot.
  1452. diff -Naur null/Documentation/filesystems/aufs/design/10dynop.txt linux-6.6.63/Documentation/filesystems/aufs/design/10dynop.txt
  1453. --- /dev/null
  1454. +++ linux-6.6.63/Documentation/filesystems/aufs/design/10dynop.txt 2024-12-18 15:12:59.291519638 +0300
  1455. @@ -0,0 +1,47 @@
  1456. +
  1457. +# Copyright (C) 2010-2022 Junjiro R. Okajima
  1458. +#
  1459. +# This program is free software; you can redistribute it and/or modify
  1460. +# it under the terms of the GNU General Public License as published by
  1461. +# the Free Software Foundation; either version 2 of the License, or
  1462. +# (at your option) any later version.
  1463. +#
  1464. +# This program is distributed in the hope that it will be useful,
  1465. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1466. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1467. +# GNU General Public License for more details.
  1468. +#
  1469. +# You should have received a copy of the GNU General Public License
  1470. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1471. +
  1472. +Dynamically customizable FS operations
  1473. +----------------------------------------------------------------------
  1474. +Generally FS operations (struct inode_operations, struct
  1475. +address_space_operations, struct file_operations, etc.) are defined as
  1476. +"static const", but it never means that FS have only one set of
  1477. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1478. +three sets, one for XIP, for NOBH, and for normal.
  1479. +Since aufs overrides and redirects these operations, sometimes aufs has
  1480. +to change its behaviour according to the branch FS type. More importantly
  1481. +VFS acts differently if a function (member in the struct) is set or
  1482. +not. It means aufs should have several sets of operations and select one
  1483. +among them according to the branch FS definition.
  1484. +
  1485. +In order to solve this problem and not to affect the behaviour of VFS,
  1486. +aufs defines these operations dynamically. For instance, aufs defines
  1487. +dummy direct_IO function for struct address_space_operations, but it may
  1488. +not be set to the address_space_operations actually. When the branch FS
  1489. +doesn't have it, aufs doesn't set it to its address_space_operations
  1490. +while the function definition itself is still alive. So the behaviour
  1491. +itself will not change, and it will return an error when direct_IO is
  1492. +not set.
  1493. +
  1494. +The lifetime of these dynamically generated operation object is
  1495. +maintained by aufs branch object. When the branch is removed from aufs,
  1496. +the reference counter of the object is decremented. When it reaches
  1497. +zero, the dynamically generated operation object will be freed.
  1498. +
  1499. +This approach is designed to support AIO (io_submit), Direct I/O and
  1500. +XIP (DAX) mainly.
  1501. +Currently this approach is applied to address_space_operations for
  1502. +regular files only.
  1503. diff -Naur null/Documentation/filesystems/aufs/README linux-6.6.63/Documentation/filesystems/aufs/README
  1504. --- /dev/null
  1505. +++ linux-6.6.63/Documentation/filesystems/aufs/README 2024-12-18 15:12:59.290519651 +0300
  1506. @@ -0,0 +1,409 @@
  1507. +
  1508. +Aufs6 -- advanced multi layered unification filesystem version 6.x
  1509. +http://aufs.sf.net
  1510. +Junjiro R. Okajima
  1511. +
  1512. +
  1513. +0. Introduction
  1514. +----------------------------------------
  1515. +In the early days, aufs was entirely re-designed and re-implemented
  1516. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1517. +improvements and implementations, it became totally different from
  1518. +Unionfs while keeping the basic features.
  1519. +Later, Unionfs Version 2.x series began taking some of the same
  1520. +approaches to aufs1's.
  1521. +Unionfs was being developed by Professor Erez Zadok at Stony Brook
  1522. +University and his team.
  1523. +
  1524. +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
  1525. +aufs-linux.git or aufs-standalone.git.
  1526. +If you want older kernel version support,
  1527. +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
  1528. +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
  1529. +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
  1530. +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
  1531. + or aufs1 from CVS on SourceForge.
  1532. +
  1533. +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
  1534. + were changed. Now they are aufs-linux.git and
  1535. + aufs-standalone.git and they contain aufs5 and later branches.
  1536. +
  1537. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1538. + According to Christoph Hellwig, linux rejects all union-type
  1539. + filesystems but UnionMount.
  1540. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1541. +
  1542. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1543. + UnionMount, and he pointed out an issue around a directory mutex
  1544. + lock and aufs addressed it. But it is still unsure whether aufs will
  1545. + be merged (or any other union solution).
  1546. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1547. +
  1548. +
  1549. +1. Features
  1550. +----------------------------------------
  1551. +- unite several directories into a single virtual filesystem. The member
  1552. + directory is called as a branch.
  1553. +- you can specify the permission flags to the branch, which are 'readonly',
  1554. + 'readwrite' and 'whiteout-able.'
  1555. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1556. + readonly branch are modifiable logically.
  1557. +- dynamic branch manipulation, add, del.
  1558. +- etc...
  1559. +
  1560. +Also there are many enhancements in aufs, such as:
  1561. +- test only the highest one for the directory permission (dirperm1)
  1562. +- copyup on open (coo=)
  1563. +- 'move' policy for copy-up between two writable branches, after
  1564. + checking free space.
  1565. +- xattr, acl
  1566. +- readdir(3) in userspace.
  1567. +- keep inode number by external inode number table
  1568. +- keep the timestamps of file/dir in internal copyup operation
  1569. +- seekable directory, supporting NFS readdir.
  1570. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1571. + on branch
  1572. +- do not copyup, nor create a whiteout when it is unnecessary
  1573. +- revert a single systemcall when an error occurs in aufs
  1574. +- remount interface instead of ioctl
  1575. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1576. +- loopback mounted filesystem as a branch
  1577. +- kernel thread for removing the dir who has a plenty of whiteouts
  1578. +- support copyup sparse file (a file which has a 'hole' in it)
  1579. +- default permission flags for branches
  1580. +- selectable permission flags for ro branch, whether whiteout can
  1581. + exist or not
  1582. +- export via NFS.
  1583. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1584. +- support multiple writable branches, some policies to select one
  1585. + among multiple writable branches.
  1586. +- a new semantics for link(2) and rename(2) to support multiple
  1587. + writable branches.
  1588. +- no glibc changes are required.
  1589. +- pseudo hardlink (hardlink over branches)
  1590. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1591. + including NFS or remote filesystem branch.
  1592. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1593. +- and more...
  1594. +
  1595. +Currently these features are dropped temporary from aufs6.
  1596. +See design/08plan.txt in detail.
  1597. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1598. + (robr)
  1599. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1600. +
  1601. +Features or just an idea in the future (see also design/*.txt),
  1602. +- reorder the branch index without del/re-add.
  1603. +- permanent xino files for NFSD
  1604. +- an option for refreshing the opened files after add/del branches
  1605. +- light version, without branch manipulation. (unnecessary?)
  1606. +- copyup in userspace
  1607. +- inotify in userspace
  1608. +- readv/writev
  1609. +
  1610. +
  1611. +2. Download
  1612. +----------------------------------------
  1613. +There are three GIT trees for aufs6, aufs-linux.git,
  1614. +aufs-standalone.git, and aufs-util.git.
  1615. +While the aufs-util is always necessary, you need either of aufs-linux
  1616. +or aufs-standalone.
  1617. +
  1618. +The aufs-linux tree includes the whole linux mainline GIT tree,
  1619. +git://git.kernel.org/.../torvalds/linux.git.
  1620. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1621. +build aufs6 as an external kernel module.
  1622. +Several extra patches are not included in this tree. Only
  1623. +aufs-standalone tree contains them. They are described in the later
  1624. +section "Configuration and Compilation."
  1625. +
  1626. +On the other hand, the aufs-standalone tree has only aufs source files
  1627. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1628. +But you need to apply all aufs patches manually.
  1629. +
  1630. +You will find GIT branches whose name is in form of "aufs6.x" where "x"
  1631. +represents the linux kernel version, "linux-6.x". For instance,
  1632. +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
  1633. +"aufs6.x-rcN" branch.
  1634. +
  1635. +o aufs-linux tree
  1636. +$ git clone --reference /your/linux/git/tree \
  1637. + git://github.com/sfjro/aufs-linux.git aufs-linux.git
  1638. +- if you don't have linux GIT tree, then remove "--reference ..."
  1639. +$ cd aufs-linux.git
  1640. +$ git checkout origin/aufs6.0
  1641. +
  1642. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1643. +leave the patch-work to GIT.
  1644. +$ cd /your/linux/git/tree
  1645. +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
  1646. +$ git fetch aufs
  1647. +$ git checkout -b my6.0 v6.0
  1648. +$ (add your local change...)
  1649. +$ git pull aufs aufs6.0
  1650. +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
  1651. +- you may need to solve some conflicts between your_changes and
  1652. + aufs6.0. in this case, git-rerere is recommended so that you can
  1653. + solve the similar conflicts automatically when you upgrade to 6.1 or
  1654. + later in the future.
  1655. +
  1656. +o aufs-standalone tree
  1657. +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
  1658. +$ cd aufs-standalone.git
  1659. +$ git checkout origin/aufs6.0
  1660. +
  1661. +o aufs-util tree
  1662. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1663. +- note that the public aufs-util.git is on SourceForge instead of
  1664. + GitHUB.
  1665. +$ cd aufs-util.git
  1666. +$ git checkout origin/aufs6.0
  1667. +
  1668. +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1669. +The minor version number, 'x' in '6.x', of aufs may not always
  1670. +follow the minor version number of the kernel.
  1671. +Because changes in the kernel that cause the use of a new
  1672. +minor version number do not always require changes to aufs-util.
  1673. +
  1674. +Since aufs-util has its own minor version number, you may not be
  1675. +able to find a GIT branch in aufs-util for your kernel's
  1676. +exact minor version number.
  1677. +In this case, you should git-checkout the branch for the
  1678. +nearest lower number.
  1679. +
  1680. +For (an unreleased) example:
  1681. +If you are using "linux-6.10" and the "aufs6.10" branch
  1682. +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
  1683. +or something numerically smaller is the branch for your kernel.
  1684. +
  1685. +Also you can view all branches by
  1686. + $ git branch -a
  1687. +
  1688. +
  1689. +3. Configuration and Compilation
  1690. +----------------------------------------
  1691. +Make sure you have git-checkout'ed the correct branch.
  1692. +
  1693. +For aufs-linux tree,
  1694. +- enable CONFIG_AUFS_FS.
  1695. +- set other aufs configurations if necessary.
  1696. +- for aufs5.13 and later
  1697. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1698. + also a caller of VFS functions for branch filesystems, subclassing of
  1699. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1700. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1701. + need to customize some LOCKDEP numbers. Here are what I use on my
  1702. + test environment.
  1703. + CONFIG_LOCKDEP_BITS=21
  1704. + CONFIG_LOCKDEP_CHAINS_BITS=21
  1705. + CONFIG_LOCKDEP_STACK_TRACE_BITS=24
  1706. + Also you will need to expand some constant values in LOCKDEP. Refer
  1707. + to lockdep-debug.patch in aufs-standalone.git.
  1708. +
  1709. +For aufs-standalone tree,
  1710. +There are several ways to build.
  1711. +
  1712. +1.
  1713. +- apply ./aufs6-kbuild.patch to your kernel source files.
  1714. +- apply ./aufs6-base.patch too.
  1715. +- apply ./aufs6-mmap.patch too.
  1716. +- apply ./aufs6-standalone.patch too, if you have a plan to set
  1717. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
  1718. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1719. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1720. +- enable CONFIG_AUFS_FS, you can select either
  1721. + =m or =y.
  1722. +- and build your kernel as usual.
  1723. +- install the built kernel.
  1724. +- install the header files too by "make headers_install" to the
  1725. + directory where you specify. By default, it is $PWD/usr.
  1726. + "make help" shows a brief note for headers_install.
  1727. +- and reboot your system.
  1728. +
  1729. +2.
  1730. +- module only (CONFIG_AUFS_FS=m).
  1731. +- apply ./aufs6-base.patch to your kernel source files.
  1732. +- apply ./aufs6-mmap.patch too.
  1733. +- apply ./aufs6-standalone.patch too.
  1734. +- build your kernel, don't forget "make headers_install", and reboot.
  1735. +- edit ./config.mk and set other aufs configurations if necessary.
  1736. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1737. + every aufs configurations.
  1738. +- build the module by simple "make".
  1739. +- you can specify ${KDIR} make variable which points to your kernel
  1740. + source tree.
  1741. +- install the files
  1742. + + run "make install" to install the aufs module, or copy the built
  1743. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1744. + + run "make install_headers" (instead of headers_install) to install
  1745. + the modified aufs header file (you can specify DESTDIR which is
  1746. + available in aufs standalone version's Makefile only), or copy
  1747. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1748. + you like manually. By default, the target directory is $PWD/usr.
  1749. +- no need to apply aufs6-kbuild.patch, nor copying source files to your
  1750. + kernel source tree.
  1751. +
  1752. +Note: The header file aufs_type.h is necessary to build aufs-util
  1753. + as well as "make headers_install" in the kernel source tree.
  1754. + headers_install is subject to be forgotten, but it is essentially
  1755. + necessary, not only for building aufs-util.
  1756. + You may not meet problems without headers_install in some older
  1757. + version though.
  1758. +
  1759. +And then,
  1760. +- read README in aufs-util, build and install it
  1761. +- note that your distribution may contain an obsoleted version of
  1762. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1763. + utilities, make sure that your compiler refers the correct aufs header
  1764. + file which is built by "make headers_install."
  1765. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1766. + then run "make install_ulib" too. And refer to the aufs manual in
  1767. + detail.
  1768. +
  1769. +There several other patches in aufs-standalone.git. They are all
  1770. +optional. When you meet some problems, they will help you.
  1771. +- aufs6-loopback.patch
  1772. + Supports a nested loopback mount in a branch-fs. This patch is
  1773. + unnecessary until aufs produces a message like "you may want to try
  1774. + another patch for loopback file".
  1775. +- vfs-ino.patch
  1776. + Modifies a system global kernel internal function get_next_ino() in
  1777. + order to stop assigning 0 for an inode-number. Not directly related to
  1778. + aufs, but recommended generally.
  1779. +- tmpfs-idr.patch
  1780. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1781. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1782. + duplication of inode number, which is important for backup tools and
  1783. + other utilities. When you find aufs XINO files for tmpfs branch
  1784. + growing too much, try this patch.
  1785. +- lockdep-debug.patch
  1786. + Similar to some kernel configurations for LOCKDEP (see the top of
  1787. + this section), you will need expand some constants in LOCKDEP for
  1788. + aufs if you enable CONFIG_LOCKDEP.
  1789. +
  1790. +
  1791. +4. Usage
  1792. +----------------------------------------
  1793. +At first, make sure aufs-util are installed, and please read the aufs
  1794. +manual, aufs.5 in aufs-util.git tree.
  1795. +$ man -l aufs.5
  1796. +
  1797. +And then,
  1798. +$ mkdir /tmp/rw /tmp/aufs
  1799. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1800. +
  1801. +Here is another example. The result is equivalent.
  1802. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1803. + Or
  1804. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1805. +# mount -o remount,append:${HOME} /tmp/aufs
  1806. +
  1807. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1808. +you modify a file under /tmp/aufs, the one on your home directory is
  1809. +not affected, instead the same named file will be newly created under
  1810. +/tmp/rw. And all of your modification to a file will be applied to
  1811. +the one under /tmp/rw. This is called the file based Copy on Write
  1812. +(COW) method.
  1813. +Aufs mount options are described in aufs.5.
  1814. +If you run chroot or something and make your aufs as a root directory,
  1815. +then you need to customize the shutdown script. See the aufs manual in
  1816. +detail.
  1817. +
  1818. +Additionally, there are some sample usages of aufs which are a
  1819. +diskless system with network booting, and LiveCD over NFS.
  1820. +See sample dir in CVS tree on SourceForge.
  1821. +
  1822. +
  1823. +5. Contact
  1824. +----------------------------------------
  1825. +When you have any problems or strange behaviour in aufs, please let me
  1826. +know with:
  1827. +- /proc/mounts (instead of the output of mount(8))
  1828. +- /sys/module/aufs/*
  1829. +- /sys/fs/aufs/* (if you have them)
  1830. +- /debug/aufs/* (if you have them)
  1831. +- linux kernel version
  1832. + if your kernel is not plain, for example modified by distributor,
  1833. + the url where i can download its source is necessary too.
  1834. +- aufs version which was printed at loading the module or booting the
  1835. + system, instead of the date you downloaded.
  1836. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1837. +- kernel configuration or /proc/config.gz (if you have it)
  1838. +- LSM (linux security module, if you are using)
  1839. +- behaviour which you think to be incorrect
  1840. +- actual operation, reproducible one is better
  1841. +- mailto: aufs-users at lists.sourceforge.net
  1842. +
  1843. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1844. +and Feature Requests) on SourceForge. Please join and write to
  1845. +aufs-users ML.
  1846. +
  1847. +
  1848. +6. Acknowledgements
  1849. +----------------------------------------
  1850. +Thanks to everyone who have tried and are using aufs, whoever
  1851. +have reported a bug or any feedback.
  1852. +
  1853. +Especially donators:
  1854. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1855. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1856. + scripts) is making "doubling" donations.
  1857. + Unfortunately I cannot list all of the donators, but I really
  1858. + appreciate.
  1859. + It ends Aug 2010, but the ordinary donation URL is still available.
  1860. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1861. +Dai Itasaka made a donation (2007/8).
  1862. +Chuck Smith made a donation (2008/4, 10 and 12).
  1863. +Henk Schoneveld made a donation (2008/9).
  1864. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1865. +Francois Dupoux made a donation (2008/11).
  1866. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1867. + aufs2 GIT tree (2009/2).
  1868. +William Grant made a donation (2009/3).
  1869. +Patrick Lane made a donation (2009/4).
  1870. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1871. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1872. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1873. +Pavel Pronskiy made a donation (2011/2).
  1874. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1875. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1876. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1877. +11).
  1878. +Sam Liddicott made a donation (2011/9).
  1879. +Era Scarecrow made a donation (2013/4).
  1880. +Bor Ratajc made a donation (2013/4).
  1881. +Alessandro Gorreta made a donation (2013/4).
  1882. +POIRETTE Marc made a donation (2013/4).
  1883. +Alessandro Gorreta made a donation (2013/4).
  1884. +lauri kasvandik made a donation (2013/5).
  1885. +"pemasu from Finland" made a donation (2013/7).
  1886. +The Parted Magic Project made a donation (2013/9 and 11).
  1887. +Pavel Barta made a donation (2013/10).
  1888. +Nikolay Pertsev made a donation (2014/5).
  1889. +James B made a donation (2014/7, 2015/7, and 2021/12).
  1890. +Stefano Di Biase made a donation (2014/8).
  1891. +Daniel Epellei made a donation (2015/1).
  1892. +OmegaPhil made a donation (2016/1, 2018/4).
  1893. +Tomasz Szewczyk made a donation (2016/4).
  1894. +James Burry made a donation (2016/12).
  1895. +Carsten Rose made a donation (2018/9).
  1896. +Porteus Kiosk made a donation (2018/10).
  1897. +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5, 8 and 2024/8).
  1898. +Vasily Mikhaylichenko made a donation (2023/5 and 2024/5).
  1899. +
  1900. +Thank you very much.
  1901. +Donations are always, including future donations, very important and
  1902. +helpful for me to keep on developing aufs.
  1903. +
  1904. +
  1905. +7.
  1906. +----------------------------------------
  1907. +If you are an experienced user, no explanation is needed. Aufs is
  1908. +just a linux filesystem.
  1909. +
  1910. +
  1911. +Enjoy!
  1912. +
  1913. +# Local variables: ;
  1914. +# mode: text;
  1915. +# End: ;
  1916. diff -Naur null/fs/aufs/aufs.h linux-6.6.63/fs/aufs/aufs.h
  1917. --- /dev/null
  1918. +++ linux-6.6.63/fs/aufs/aufs.h 2024-12-18 15:12:59.293519613 +0300
  1919. @@ -0,0 +1,62 @@
  1920. +/* SPDX-License-Identifier: GPL-2.0 */
  1921. +/*
  1922. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  1923. + *
  1924. + * This program is free software; you can redistribute it and/or modify
  1925. + * it under the terms of the GNU General Public License as published by
  1926. + * the Free Software Foundation; either version 2 of the License, or
  1927. + * (at your option) any later version.
  1928. + *
  1929. + * This program is distributed in the hope that it will be useful,
  1930. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1931. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1932. + * GNU General Public License for more details.
  1933. + *
  1934. + * You should have received a copy of the GNU General Public License
  1935. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1936. + */
  1937. +
  1938. +/*
  1939. + * all header files
  1940. + */
  1941. +
  1942. +#ifndef __AUFS_H__
  1943. +#define __AUFS_H__
  1944. +
  1945. +#ifdef __KERNEL__
  1946. +
  1947. +#define AuStub(type, name, body, ...) \
  1948. + static inline type name(__VA_ARGS__) { body; }
  1949. +
  1950. +#define AuStubVoid(name, ...) \
  1951. + AuStub(void, name, , __VA_ARGS__)
  1952. +#define AuStubInt0(name, ...) \
  1953. + AuStub(int, name, return 0, __VA_ARGS__)
  1954. +
  1955. +#include "debug.h"
  1956. +
  1957. +#include "branch.h"
  1958. +#include "cpup.h"
  1959. +#include "dcsub.h"
  1960. +#include "dbgaufs.h"
  1961. +#include "dentry.h"
  1962. +#include "dir.h"
  1963. +#include "dirren.h"
  1964. +#include "dynop.h"
  1965. +#include "file.h"
  1966. +#include "fstype.h"
  1967. +#include "hbl.h"
  1968. +#include "inode.h"
  1969. +#include "lcnt.h"
  1970. +#include "loop.h"
  1971. +#include "module.h"
  1972. +#include "opts.h"
  1973. +#include "rwsem.h"
  1974. +#include "super.h"
  1975. +#include "sysaufs.h"
  1976. +#include "vfsub.h"
  1977. +#include "whout.h"
  1978. +#include "wkq.h"
  1979. +
  1980. +#endif /* __KERNEL__ */
  1981. +#endif /* __AUFS_H__ */
  1982. diff -Naur null/fs/aufs/branch.c linux-6.6.63/fs/aufs/branch.c
  1983. --- /dev/null
  1984. +++ linux-6.6.63/fs/aufs/branch.c 2024-12-18 15:12:59.293519613 +0300
  1985. @@ -0,0 +1,1427 @@
  1986. +// SPDX-License-Identifier: GPL-2.0
  1987. +/*
  1988. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  1989. + *
  1990. + * This program is free software; you can redistribute it and/or modify
  1991. + * it under the terms of the GNU General Public License as published by
  1992. + * the Free Software Foundation; either version 2 of the License, or
  1993. + * (at your option) any later version.
  1994. + *
  1995. + * This program is distributed in the hope that it will be useful,
  1996. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1997. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1998. + * GNU General Public License for more details.
  1999. + *
  2000. + * You should have received a copy of the GNU General Public License
  2001. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  2002. + */
  2003. +
  2004. +/*
  2005. + * branch management
  2006. + */
  2007. +
  2008. +#include <linux/compat.h>
  2009. +#include <linux/statfs.h>
  2010. +#include "aufs.h"
  2011. +
  2012. +/*
  2013. + * free a single branch
  2014. + */
  2015. +static void au_br_do_free(struct au_branch *br)
  2016. +{
  2017. + int i;
  2018. + struct au_wbr *wbr;
  2019. + struct au_dykey **key;
  2020. +
  2021. + au_hnotify_fin_br(br);
  2022. + /* always, regardless the mount option */
  2023. + au_dr_hino_free(&br->br_dirren);
  2024. + au_xino_put(br);
  2025. +
  2026. + AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
  2027. + au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
  2028. + AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
  2029. + au_lcnt_fin(&br->br_count, /*do_sync*/0);
  2030. +
  2031. + wbr = br->br_wbr;
  2032. + if (wbr) {
  2033. + for (i = 0; i < AuBrWh_Last; i++)
  2034. + dput(wbr->wbr_wh[i]);
  2035. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  2036. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  2037. + }
  2038. +
  2039. + if (br->br_fhsm) {
  2040. + au_br_fhsm_fin(br->br_fhsm);
  2041. + au_kfree_try_rcu(br->br_fhsm);
  2042. + }
  2043. +
  2044. + key = br->br_dykey;
  2045. + for (i = 0; i < AuBrDynOp; i++, key++)
  2046. + if (*key)
  2047. + au_dy_put(*key);
  2048. + else
  2049. + break;
  2050. +
  2051. + /* recursive lock, s_umount of branch's */
  2052. + /* synchronize_rcu(); */ /* why? */
  2053. + lockdep_off();
  2054. + path_put(&br->br_path);
  2055. + lockdep_on();
  2056. + au_kfree_rcu(wbr);
  2057. + au_lcnt_wait_for_fin(&br->br_nfiles);
  2058. + au_lcnt_wait_for_fin(&br->br_count);
  2059. + /* I don't know why, but percpu_refcount requires this */
  2060. + /* synchronize_rcu(); */
  2061. + au_kfree_rcu(br);
  2062. +}
  2063. +
  2064. +/*
  2065. + * frees all branches
  2066. + */
  2067. +void au_br_free(struct au_sbinfo *sbinfo)
  2068. +{
  2069. + aufs_bindex_t bmax;
  2070. + struct au_branch **br;
  2071. +
  2072. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2073. +
  2074. + bmax = sbinfo->si_bbot + 1;
  2075. + br = sbinfo->si_branch;
  2076. + while (bmax--)
  2077. + au_br_do_free(*br++);
  2078. +}
  2079. +
  2080. +/*
  2081. + * find the index of a branch which is specified by @br_id.
  2082. + */
  2083. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  2084. +{
  2085. + aufs_bindex_t bindex, bbot;
  2086. +
  2087. + bbot = au_sbbot(sb);
  2088. + for (bindex = 0; bindex <= bbot; bindex++)
  2089. + if (au_sbr_id(sb, bindex) == br_id)
  2090. + return bindex;
  2091. + return -1;
  2092. +}
  2093. +
  2094. +/* ---------------------------------------------------------------------- */
  2095. +
  2096. +/*
  2097. + * add a branch
  2098. + */
  2099. +
  2100. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  2101. + struct dentry *h_root)
  2102. +{
  2103. + if (unlikely(h_adding == h_root
  2104. + || au_test_loopback_overlap(sb, h_adding)))
  2105. + return 1;
  2106. + if (h_adding->d_sb != h_root->d_sb)
  2107. + return 0;
  2108. + return au_test_subdir(h_adding, h_root)
  2109. + || au_test_subdir(h_root, h_adding);
  2110. +}
  2111. +
  2112. +/*
  2113. + * returns a newly allocated branch. @new_nbranch is a number of branches
  2114. + * after adding a branch.
  2115. + */
  2116. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  2117. + int perm)
  2118. +{
  2119. + struct au_branch *add_branch;
  2120. + struct dentry *root;
  2121. + struct inode *inode;
  2122. + int err;
  2123. +
  2124. + err = -ENOMEM;
  2125. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  2126. + if (unlikely(!add_branch))
  2127. + goto out;
  2128. + add_branch->br_xino = au_xino_alloc(/*nfile*/1);
  2129. + if (unlikely(!add_branch->br_xino))
  2130. + goto out_br;
  2131. + err = au_hnotify_init_br(add_branch, perm);
  2132. + if (unlikely(err))
  2133. + goto out_xino;
  2134. +
  2135. + if (au_br_writable(perm)) {
  2136. + /* may be freed separately at changing the branch permission */
  2137. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  2138. + GFP_NOFS);
  2139. + if (unlikely(!add_branch->br_wbr))
  2140. + goto out_hnotify;
  2141. + }
  2142. +
  2143. + if (au_br_fhsm(perm)) {
  2144. + err = au_fhsm_br_alloc(add_branch);
  2145. + if (unlikely(err))
  2146. + goto out_wbr;
  2147. + }
  2148. +
  2149. + root = sb->s_root;
  2150. + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
  2151. + if (!err)
  2152. + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
  2153. + if (!err) {
  2154. + inode = d_inode(root);
  2155. + err = au_hinode_realloc(au_ii(inode), new_nbranch,
  2156. + /*may_shrink*/0);
  2157. + }
  2158. + if (!err)
  2159. + return add_branch; /* success */
  2160. +
  2161. +out_wbr:
  2162. + au_kfree_rcu(add_branch->br_wbr);
  2163. +out_hnotify:
  2164. + au_hnotify_fin_br(add_branch);
  2165. +out_xino:
  2166. + au_xino_put(add_branch);
  2167. +out_br:
  2168. + au_kfree_rcu(add_branch);
  2169. +out:
  2170. + return ERR_PTR(err);
  2171. +}
  2172. +
  2173. +/*
  2174. + * test if the branch permission is legal or not.
  2175. + */
  2176. +static int test_br(struct inode *inode, int brperm, char *path)
  2177. +{
  2178. + int err;
  2179. +
  2180. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  2181. + if (!err)
  2182. + goto out;
  2183. +
  2184. + err = -EINVAL;
  2185. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2186. +
  2187. +out:
  2188. + return err;
  2189. +}
  2190. +
  2191. +/*
  2192. + * returns:
  2193. + * 0: success, the caller will add it
  2194. + * plus: success, it is already unified, the caller should ignore it
  2195. + * minus: error
  2196. + */
  2197. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2198. +{
  2199. + int err;
  2200. + aufs_bindex_t bbot, bindex;
  2201. + struct dentry *root, *h_dentry;
  2202. + struct inode *inode, *h_inode;
  2203. +
  2204. + root = sb->s_root;
  2205. + bbot = au_sbbot(sb);
  2206. + if (unlikely(bbot >= 0
  2207. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2208. + err = 1;
  2209. + if (!remount) {
  2210. + err = -EINVAL;
  2211. + pr_err("%s duplicated\n", add->pathname);
  2212. + }
  2213. + goto out;
  2214. + }
  2215. +
  2216. + err = -ENOSPC; /* -E2BIG; */
  2217. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2218. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2219. + pr_err("number of branches exceeded %s\n", add->pathname);
  2220. + goto out;
  2221. + }
  2222. +
  2223. + err = -EDOM;
  2224. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2225. + pr_err("bad index %d\n", add->bindex);
  2226. + goto out;
  2227. + }
  2228. +
  2229. + inode = d_inode(add->path.dentry);
  2230. + err = -ENOENT;
  2231. + if (unlikely(!vfsub_inode_nlink(inode, AU_I_UNKNOWN))) {
  2232. + pr_err("no existence %s\n", add->pathname);
  2233. + goto out;
  2234. + }
  2235. +
  2236. + err = -EINVAL;
  2237. + if (unlikely(inode->i_sb == sb)) {
  2238. + pr_err("%s must be outside\n", add->pathname);
  2239. + goto out;
  2240. + }
  2241. +
  2242. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2243. + pr_err("unsupported filesystem, %s (%s)\n",
  2244. + add->pathname, au_sbtype(inode->i_sb));
  2245. + goto out;
  2246. + }
  2247. +
  2248. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2249. + pr_err("already stacked, %s (%s)\n",
  2250. + add->pathname, au_sbtype(inode->i_sb));
  2251. + goto out;
  2252. + }
  2253. +
  2254. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2255. + if (unlikely(err))
  2256. + goto out;
  2257. +
  2258. + if (bbot < 0)
  2259. + return 0; /* success */
  2260. +
  2261. + err = -EINVAL;
  2262. + for (bindex = 0; bindex <= bbot; bindex++)
  2263. + if (unlikely(test_overlap(sb, add->path.dentry,
  2264. + au_h_dptr(root, bindex)))) {
  2265. + pr_err("%s is overlapped\n", add->pathname);
  2266. + goto out;
  2267. + }
  2268. +
  2269. + err = 0;
  2270. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2271. + h_dentry = au_h_dptr(root, 0);
  2272. + h_inode = d_inode(h_dentry);
  2273. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2274. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2275. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2276. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2277. + add->pathname,
  2278. + i_uid_read(inode), i_gid_read(inode),
  2279. + (inode->i_mode & S_IALLUGO),
  2280. + i_uid_read(h_inode), i_gid_read(h_inode),
  2281. + (h_inode->i_mode & S_IALLUGO));
  2282. + }
  2283. +
  2284. +out:
  2285. + return err;
  2286. +}
  2287. +
  2288. +/*
  2289. + * initialize or clean the whiteouts for an adding branch
  2290. + */
  2291. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2292. + int new_perm)
  2293. +{
  2294. + int err, old_perm;
  2295. + aufs_bindex_t bindex;
  2296. + struct inode *h_inode;
  2297. + struct au_wbr *wbr;
  2298. + struct au_hinode *hdir;
  2299. + struct dentry *h_dentry;
  2300. +
  2301. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2302. + if (unlikely(err))
  2303. + goto out;
  2304. +
  2305. + wbr = br->br_wbr;
  2306. + old_perm = br->br_perm;
  2307. + br->br_perm = new_perm;
  2308. + hdir = NULL;
  2309. + h_inode = NULL;
  2310. + bindex = au_br_index(sb, br->br_id);
  2311. + if (0 <= bindex) {
  2312. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2313. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  2314. + } else {
  2315. + h_dentry = au_br_dentry(br);
  2316. + h_inode = d_inode(h_dentry);
  2317. + inode_lock_nested(h_inode, AuLsc_I_PARENT);
  2318. + }
  2319. + if (!wbr)
  2320. + err = au_wh_init(br, sb);
  2321. + else {
  2322. + wbr_wh_write_lock(wbr);
  2323. + err = au_wh_init(br, sb);
  2324. + wbr_wh_write_unlock(wbr);
  2325. + }
  2326. + if (hdir)
  2327. + au_hn_inode_unlock(hdir);
  2328. + else
  2329. + inode_unlock(h_inode);
  2330. + vfsub_mnt_drop_write(au_br_mnt(br));
  2331. + br->br_perm = old_perm;
  2332. +
  2333. + if (!err && wbr && !au_br_writable(new_perm)) {
  2334. + au_kfree_rcu(wbr);
  2335. + br->br_wbr = NULL;
  2336. + }
  2337. +
  2338. +out:
  2339. + return err;
  2340. +}
  2341. +
  2342. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2343. + int perm)
  2344. +{
  2345. + int err;
  2346. + struct kstatfs kst;
  2347. + struct au_wbr *wbr;
  2348. +
  2349. + wbr = br->br_wbr;
  2350. + au_rw_init(&wbr->wbr_wh_rwsem);
  2351. + atomic_set(&wbr->wbr_wh_running, 0);
  2352. +
  2353. + /*
  2354. + * a limit for rmdir/rename a dir
  2355. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2356. + */
  2357. + err = vfs_statfs(&br->br_path, &kst);
  2358. + if (unlikely(err))
  2359. + goto out;
  2360. + err = -EINVAL;
  2361. + if (kst.f_namelen >= NAME_MAX)
  2362. + err = au_br_init_wh(sb, br, perm);
  2363. + else
  2364. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2365. + au_br_dentry(br),
  2366. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2367. +
  2368. +out:
  2369. + return err;
  2370. +}
  2371. +
  2372. +/* initialize a new branch */
  2373. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2374. + struct au_opt_add *add)
  2375. +{
  2376. + int err;
  2377. + struct au_branch *brbase;
  2378. + struct file *xf;
  2379. + struct inode *h_inode;
  2380. +
  2381. + err = 0;
  2382. + br->br_perm = add->perm;
  2383. + br->br_path = add->path; /* set first, path_get() later */
  2384. + spin_lock_init(&br->br_dykey_lock);
  2385. + au_lcnt_init(&br->br_nfiles, /*release*/NULL);
  2386. + au_lcnt_init(&br->br_count, /*release*/NULL);
  2387. + br->br_id = au_new_br_id(sb);
  2388. + AuDebugOn(br->br_id < 0);
  2389. +
  2390. + /* always, regardless the given option */
  2391. + err = au_dr_br_init(sb, br, &add->path);
  2392. + if (unlikely(err))
  2393. + goto out_err;
  2394. +
  2395. + if (au_br_writable(add->perm)) {
  2396. + err = au_wbr_init(br, sb, add->perm);
  2397. + if (unlikely(err))
  2398. + goto out_err;
  2399. + }
  2400. +
  2401. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2402. + brbase = au_sbr(sb, 0);
  2403. + xf = au_xino_file(brbase->br_xino, /*idx*/-1);
  2404. + AuDebugOn(!xf);
  2405. + h_inode = d_inode(add->path.dentry);
  2406. + err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
  2407. + if (unlikely(err)) {
  2408. + AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
  2409. + goto out_err;
  2410. + }
  2411. + }
  2412. +
  2413. + sysaufs_br_init(br);
  2414. + path_get(&br->br_path);
  2415. + goto out; /* success */
  2416. +
  2417. +out_err:
  2418. + memset(&br->br_path, 0, sizeof(br->br_path));
  2419. +out:
  2420. + return err;
  2421. +}
  2422. +
  2423. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2424. + struct au_branch *br, aufs_bindex_t bbot,
  2425. + aufs_bindex_t amount)
  2426. +{
  2427. + struct au_branch **brp;
  2428. +
  2429. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2430. +
  2431. + brp = sbinfo->si_branch + bindex;
  2432. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2433. + *brp = br;
  2434. + sbinfo->si_bbot++;
  2435. + if (unlikely(bbot < 0))
  2436. + sbinfo->si_bbot = 0;
  2437. +}
  2438. +
  2439. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2440. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2441. +{
  2442. + struct au_hdentry *hdp;
  2443. +
  2444. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2445. +
  2446. + hdp = au_hdentry(dinfo, bindex);
  2447. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2448. + au_h_dentry_init(hdp);
  2449. + dinfo->di_bbot++;
  2450. + if (unlikely(bbot < 0))
  2451. + dinfo->di_btop = 0;
  2452. +}
  2453. +
  2454. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2455. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2456. +{
  2457. + struct au_hinode *hip;
  2458. +
  2459. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2460. +
  2461. + hip = au_hinode(iinfo, bindex);
  2462. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2463. + au_hinode_init(hip);
  2464. + iinfo->ii_bbot++;
  2465. + if (unlikely(bbot < 0))
  2466. + iinfo->ii_btop = 0;
  2467. +}
  2468. +
  2469. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2470. + aufs_bindex_t bindex)
  2471. +{
  2472. + struct dentry *root, *h_dentry;
  2473. + struct inode *root_inode, *h_inode;
  2474. + aufs_bindex_t bbot, amount;
  2475. +
  2476. + root = sb->s_root;
  2477. + root_inode = d_inode(root);
  2478. + bbot = au_sbbot(sb);
  2479. + amount = bbot + 1 - bindex;
  2480. + h_dentry = au_br_dentry(br);
  2481. + au_sbilist_lock();
  2482. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2483. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2484. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2485. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2486. + h_inode = d_inode(h_dentry);
  2487. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2488. + au_sbilist_unlock();
  2489. +}
  2490. +
  2491. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2492. +{
  2493. + int err;
  2494. + aufs_bindex_t bbot, add_bindex;
  2495. + struct dentry *root, *h_dentry;
  2496. + struct inode *root_inode;
  2497. + struct au_branch *add_branch;
  2498. +
  2499. + root = sb->s_root;
  2500. + root_inode = d_inode(root);
  2501. + IMustLock(root_inode);
  2502. + IiMustWriteLock(root_inode);
  2503. + err = test_add(sb, add, remount);
  2504. + if (unlikely(err < 0))
  2505. + goto out;
  2506. + if (err) {
  2507. + err = 0;
  2508. + goto out; /* success */
  2509. + }
  2510. +
  2511. + bbot = au_sbbot(sb);
  2512. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2513. + err = PTR_ERR(add_branch);
  2514. + if (IS_ERR(add_branch))
  2515. + goto out;
  2516. +
  2517. + err = au_br_init(add_branch, sb, add);
  2518. + if (unlikely(err)) {
  2519. + au_br_do_free(add_branch);
  2520. + goto out;
  2521. + }
  2522. +
  2523. + add_bindex = add->bindex;
  2524. + sysaufs_brs_del(sb, add_bindex); /* remove successors */
  2525. + au_br_do_add(sb, add_branch, add_bindex);
  2526. + sysaufs_brs_add(sb, add_bindex); /* append successors */
  2527. + dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
  2528. +
  2529. + h_dentry = add->path.dentry;
  2530. + if (!add_bindex) {
  2531. + au_cpup_attr_all(root_inode, /*force*/1);
  2532. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2533. + } else
  2534. + au_add_nlink(root_inode, d_inode(h_dentry));
  2535. +
  2536. +out:
  2537. + return err;
  2538. +}
  2539. +
  2540. +/* ---------------------------------------------------------------------- */
  2541. +
  2542. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2543. + unsigned long long max __maybe_unused,
  2544. + void *arg)
  2545. +{
  2546. + unsigned long long n;
  2547. + struct file **p, *f;
  2548. + struct hlist_bl_head *files;
  2549. + struct hlist_bl_node *pos;
  2550. + struct au_finfo *finfo;
  2551. +
  2552. + n = 0;
  2553. + p = a;
  2554. + files = &au_sbi(sb)->si_files;
  2555. + hlist_bl_lock(files);
  2556. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  2557. + f = finfo->fi_file;
  2558. + if (file_count(f)
  2559. + && !special_file(file_inode(f)->i_mode)) {
  2560. + get_file(f);
  2561. + *p++ = f;
  2562. + n++;
  2563. + AuDebugOn(n > max);
  2564. + }
  2565. + }
  2566. + hlist_bl_unlock(files);
  2567. +
  2568. + return n;
  2569. +}
  2570. +
  2571. +static struct file **au_farray_alloc(struct super_block *sb,
  2572. + unsigned long long *max)
  2573. +{
  2574. + struct au_sbinfo *sbi;
  2575. +
  2576. + sbi = au_sbi(sb);
  2577. + *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
  2578. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2579. +}
  2580. +
  2581. +static void au_farray_free(struct file **a, unsigned long long max)
  2582. +{
  2583. + unsigned long long ull;
  2584. +
  2585. + for (ull = 0; ull < max; ull++)
  2586. + if (a[ull])
  2587. + fput(a[ull]);
  2588. + kvfree(a);
  2589. +}
  2590. +
  2591. +/* ---------------------------------------------------------------------- */
  2592. +
  2593. +/*
  2594. + * delete a branch
  2595. + */
  2596. +
  2597. +/* to show the line number, do not make it inlined function */
  2598. +#define AuVerbose(do_info, fmt, ...) do { \
  2599. + if (do_info) \
  2600. + pr_info(fmt, ##__VA_ARGS__); \
  2601. +} while (0)
  2602. +
  2603. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2604. + aufs_bindex_t bbot)
  2605. +{
  2606. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2607. +}
  2608. +
  2609. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2610. + aufs_bindex_t bbot)
  2611. +{
  2612. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2613. +}
  2614. +
  2615. +/*
  2616. + * test if the branch is deletable or not.
  2617. + */
  2618. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2619. + unsigned int sigen, const unsigned int verbose)
  2620. +{
  2621. + int err, i, j, ndentry;
  2622. + aufs_bindex_t btop, bbot;
  2623. + struct au_dcsub_pages dpages;
  2624. + struct au_dpage *dpage;
  2625. + struct dentry *d;
  2626. +
  2627. + err = au_dpages_init(&dpages, GFP_NOFS);
  2628. + if (unlikely(err))
  2629. + goto out;
  2630. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2631. + if (unlikely(err))
  2632. + goto out_dpages;
  2633. +
  2634. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2635. + dpage = dpages.dpages + i;
  2636. + ndentry = dpage->ndentry;
  2637. + for (j = 0; !err && j < ndentry; j++) {
  2638. + d = dpage->dentries[j];
  2639. + AuDebugOn(au_dcount(d) <= 0);
  2640. + if (!au_digen_test(d, sigen)) {
  2641. + di_read_lock_child(d, AuLock_IR);
  2642. + if (unlikely(au_dbrange_test(d))) {
  2643. + di_read_unlock(d, AuLock_IR);
  2644. + continue;
  2645. + }
  2646. + } else {
  2647. + di_write_lock_child(d);
  2648. + if (unlikely(au_dbrange_test(d))) {
  2649. + di_write_unlock(d);
  2650. + continue;
  2651. + }
  2652. + err = au_reval_dpath(d, sigen);
  2653. + if (!err)
  2654. + di_downgrade_lock(d, AuLock_IR);
  2655. + else {
  2656. + di_write_unlock(d);
  2657. + break;
  2658. + }
  2659. + }
  2660. +
  2661. + /* AuDbgDentry(d); */
  2662. + btop = au_dbtop(d);
  2663. + bbot = au_dbbot(d);
  2664. + if (btop <= bindex
  2665. + && bindex <= bbot
  2666. + && au_h_dptr(d, bindex)
  2667. + && au_test_dbusy(d, btop, bbot)) {
  2668. + err = -EBUSY;
  2669. + AuVerbose(verbose, "busy %pd\n", d);
  2670. + AuDbgDentry(d);
  2671. + }
  2672. + di_read_unlock(d, AuLock_IR);
  2673. + }
  2674. + }
  2675. +
  2676. +out_dpages:
  2677. + au_dpages_free(&dpages);
  2678. +out:
  2679. + return err;
  2680. +}
  2681. +
  2682. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2683. + unsigned int sigen, const unsigned int verbose)
  2684. +{
  2685. + int err;
  2686. + unsigned long long max, ull;
  2687. + struct inode *i, **array;
  2688. + aufs_bindex_t btop, bbot;
  2689. +
  2690. + array = au_iarray_alloc(sb, &max);
  2691. + err = PTR_ERR(array);
  2692. + if (IS_ERR(array))
  2693. + goto out;
  2694. +
  2695. + err = 0;
  2696. + AuDbg("b%d\n", bindex);
  2697. + for (ull = 0; !err && ull < max; ull++) {
  2698. + i = array[ull];
  2699. + if (unlikely(!i))
  2700. + break;
  2701. + if (i->i_ino == AUFS_ROOT_INO)
  2702. + continue;
  2703. +
  2704. + /* AuDbgInode(i); */
  2705. + if (au_iigen(i, NULL) == sigen)
  2706. + ii_read_lock_child(i);
  2707. + else {
  2708. + ii_write_lock_child(i);
  2709. + err = au_refresh_hinode_self(i);
  2710. + au_iigen_dec(i);
  2711. + if (!err)
  2712. + ii_downgrade_lock(i);
  2713. + else {
  2714. + ii_write_unlock(i);
  2715. + break;
  2716. + }
  2717. + }
  2718. +
  2719. + btop = au_ibtop(i);
  2720. + bbot = au_ibbot(i);
  2721. + if (btop <= bindex
  2722. + && bindex <= bbot
  2723. + && au_h_iptr(i, bindex)
  2724. + && au_test_ibusy(i, btop, bbot)) {
  2725. + err = -EBUSY;
  2726. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2727. + AuDbgInode(i);
  2728. + }
  2729. + ii_read_unlock(i);
  2730. + }
  2731. + au_iarray_free(array, max);
  2732. +
  2733. +out:
  2734. + return err;
  2735. +}
  2736. +
  2737. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2738. + const unsigned int verbose)
  2739. +{
  2740. + int err;
  2741. + unsigned int sigen;
  2742. +
  2743. + sigen = au_sigen(root->d_sb);
  2744. + DiMustNoWaiters(root);
  2745. + IiMustNoWaiters(d_inode(root));
  2746. + di_write_unlock(root);
  2747. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2748. + if (!err)
  2749. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2750. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2751. +
  2752. + return err;
  2753. +}
  2754. +
  2755. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2756. + struct file **to_free, int *idx)
  2757. +{
  2758. + int err;
  2759. + unsigned char matched, root;
  2760. + aufs_bindex_t bindex, bbot;
  2761. + struct au_fidir *fidir;
  2762. + struct au_hfile *hfile;
  2763. +
  2764. + err = 0;
  2765. + root = IS_ROOT(file->f_path.dentry);
  2766. + if (root) {
  2767. + get_file(file);
  2768. + to_free[*idx] = file;
  2769. + (*idx)++;
  2770. + goto out;
  2771. + }
  2772. +
  2773. + matched = 0;
  2774. + fidir = au_fi(file)->fi_hdir;
  2775. + AuDebugOn(!fidir);
  2776. + bbot = au_fbbot_dir(file);
  2777. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2778. + hfile = fidir->fd_hfile + bindex;
  2779. + if (!hfile->hf_file)
  2780. + continue;
  2781. +
  2782. + if (hfile->hf_br->br_id == br_id) {
  2783. + matched = 1;
  2784. + break;
  2785. + }
  2786. + }
  2787. + if (matched)
  2788. + err = -EBUSY;
  2789. +
  2790. +out:
  2791. + return err;
  2792. +}
  2793. +
  2794. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2795. + struct file **to_free, int opened)
  2796. +{
  2797. + int err, idx;
  2798. + unsigned long long ull, max;
  2799. + aufs_bindex_t btop;
  2800. + struct file *file, **array;
  2801. + struct dentry *root;
  2802. + struct au_hfile *hfile;
  2803. +
  2804. + array = au_farray_alloc(sb, &max);
  2805. + err = PTR_ERR(array);
  2806. + if (IS_ERR(array))
  2807. + goto out;
  2808. +
  2809. + err = 0;
  2810. + idx = 0;
  2811. + root = sb->s_root;
  2812. + di_write_unlock(root);
  2813. + for (ull = 0; ull < max; ull++) {
  2814. + file = array[ull];
  2815. + if (unlikely(!file))
  2816. + break;
  2817. +
  2818. + /* AuDbg("%pD\n", file); */
  2819. + fi_read_lock(file);
  2820. + btop = au_fbtop(file);
  2821. + if (!d_is_dir(file->f_path.dentry)) {
  2822. + hfile = &au_fi(file)->fi_htop;
  2823. + if (hfile->hf_br->br_id == br_id)
  2824. + err = -EBUSY;
  2825. + } else
  2826. + err = test_dir_busy(file, br_id, to_free, &idx);
  2827. + fi_read_unlock(file);
  2828. + if (unlikely(err))
  2829. + break;
  2830. + }
  2831. + di_write_lock_child(root);
  2832. + au_farray_free(array, max);
  2833. + AuDebugOn(idx > opened);
  2834. +
  2835. +out:
  2836. + return err;
  2837. +}
  2838. +
  2839. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2840. + aufs_bindex_t br_id)
  2841. +{
  2842. + unsigned long long ull;
  2843. + aufs_bindex_t bindex, btop, bbot, bfound;
  2844. + struct file *file;
  2845. + struct au_fidir *fidir;
  2846. + struct au_hfile *hfile;
  2847. +
  2848. + for (ull = 0; ull < opened; ull++) {
  2849. + file = to_free[ull];
  2850. + if (unlikely(!file))
  2851. + break;
  2852. +
  2853. + /* AuDbg("%pD\n", file); */
  2854. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2855. + bfound = -1;
  2856. + fidir = au_fi(file)->fi_hdir;
  2857. + AuDebugOn(!fidir);
  2858. + fi_write_lock(file);
  2859. + btop = au_fbtop(file);
  2860. + bbot = au_fbbot_dir(file);
  2861. + for (bindex = btop; bindex <= bbot; bindex++) {
  2862. + hfile = fidir->fd_hfile + bindex;
  2863. + if (!hfile->hf_file)
  2864. + continue;
  2865. +
  2866. + if (hfile->hf_br->br_id == br_id) {
  2867. + bfound = bindex;
  2868. + break;
  2869. + }
  2870. + }
  2871. + AuDebugOn(bfound < 0);
  2872. + au_set_h_fptr(file, bfound, NULL);
  2873. + if (bfound == btop) {
  2874. + for (btop++; btop <= bbot; btop++)
  2875. + if (au_hf_dir(file, btop)) {
  2876. + au_set_fbtop(file, btop);
  2877. + break;
  2878. + }
  2879. + }
  2880. + fi_write_unlock(file);
  2881. + }
  2882. +}
  2883. +
  2884. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2885. + const aufs_bindex_t bindex,
  2886. + const aufs_bindex_t bbot)
  2887. +{
  2888. + struct au_branch **brp, **p;
  2889. +
  2890. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2891. +
  2892. + brp = sbinfo->si_branch + bindex;
  2893. + if (bindex < bbot)
  2894. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2895. + sbinfo->si_branch[0 + bbot] = NULL;
  2896. + sbinfo->si_bbot--;
  2897. +
  2898. + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
  2899. + /*may_shrink*/1);
  2900. + if (p)
  2901. + sbinfo->si_branch = p;
  2902. + /* harmless error */
  2903. +}
  2904. +
  2905. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2906. + const aufs_bindex_t bbot)
  2907. +{
  2908. + struct au_hdentry *hdp, *p;
  2909. +
  2910. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2911. +
  2912. + hdp = au_hdentry(dinfo, bindex);
  2913. + if (bindex < bbot)
  2914. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2915. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2916. + dinfo->di_bbot--;
  2917. +
  2918. + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
  2919. + /*may_shrink*/1);
  2920. + if (p)
  2921. + dinfo->di_hdentry = p;
  2922. + /* harmless error */
  2923. +}
  2924. +
  2925. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2926. + const aufs_bindex_t bbot)
  2927. +{
  2928. + struct au_hinode *hip, *p;
  2929. +
  2930. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2931. +
  2932. + hip = au_hinode(iinfo, bindex);
  2933. + if (bindex < bbot)
  2934. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2935. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2936. + iinfo->ii_bbot--;
  2937. +
  2938. + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
  2939. + /*may_shrink*/1);
  2940. + if (p)
  2941. + iinfo->ii_hinode = p;
  2942. + /* harmless error */
  2943. +}
  2944. +
  2945. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2946. + struct au_branch *br)
  2947. +{
  2948. + aufs_bindex_t bbot;
  2949. + struct au_sbinfo *sbinfo;
  2950. + struct dentry *root, *h_root;
  2951. + struct inode *inode, *h_inode;
  2952. + struct au_hinode *hinode;
  2953. +
  2954. + SiMustWriteLock(sb);
  2955. +
  2956. + root = sb->s_root;
  2957. + inode = d_inode(root);
  2958. + sbinfo = au_sbi(sb);
  2959. + bbot = sbinfo->si_bbot;
  2960. +
  2961. + h_root = au_h_dptr(root, bindex);
  2962. + hinode = au_hi(inode, bindex);
  2963. + h_inode = au_igrab(hinode->hi_inode);
  2964. + au_hiput(hinode);
  2965. +
  2966. + au_sbilist_lock();
  2967. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2968. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2969. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2970. + au_sbilist_unlock();
  2971. +
  2972. + /* ignore an error */
  2973. + au_dr_br_fin(sb, br); /* always, regardless the mount option */
  2974. +
  2975. + dput(h_root);
  2976. + iput(h_inode);
  2977. + au_br_do_free(br);
  2978. +}
  2979. +
  2980. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2981. + unsigned long long max, void *arg)
  2982. +{
  2983. + return max;
  2984. +}
  2985. +
  2986. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2987. +{
  2988. + int err, rerr, i;
  2989. + unsigned long long opened;
  2990. + unsigned int mnt_flags;
  2991. + aufs_bindex_t bindex, bbot, br_id;
  2992. + unsigned char do_wh, verbose;
  2993. + struct au_branch *br;
  2994. + struct au_wbr *wbr;
  2995. + struct dentry *root;
  2996. + struct file **to_free;
  2997. +
  2998. + err = 0;
  2999. + opened = 0;
  3000. + to_free = NULL;
  3001. + root = sb->s_root;
  3002. + bindex = au_find_dbindex(root, del->h_path.dentry);
  3003. + if (bindex < 0) {
  3004. + if (remount)
  3005. + goto out; /* success */
  3006. + err = -ENOENT;
  3007. + pr_err("%s no such branch\n", del->pathname);
  3008. + goto out;
  3009. + }
  3010. + AuDbg("bindex b%d\n", bindex);
  3011. +
  3012. + err = -EBUSY;
  3013. + mnt_flags = au_mntflags(sb);
  3014. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3015. + bbot = au_sbbot(sb);
  3016. + if (unlikely(!bbot)) {
  3017. + AuVerbose(verbose, "no more branches left\n");
  3018. + goto out;
  3019. + }
  3020. +
  3021. + br = au_sbr(sb, bindex);
  3022. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  3023. + if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
  3024. + AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
  3025. + goto out;
  3026. + }
  3027. +
  3028. + br_id = br->br_id;
  3029. + opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
  3030. + if (unlikely(opened)) {
  3031. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  3032. + err = PTR_ERR(to_free);
  3033. + if (IS_ERR(to_free))
  3034. + goto out;
  3035. +
  3036. + err = test_file_busy(sb, br_id, to_free, opened);
  3037. + if (unlikely(err)) {
  3038. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  3039. + goto out;
  3040. + }
  3041. + }
  3042. +
  3043. + wbr = br->br_wbr;
  3044. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  3045. + if (do_wh) {
  3046. + /* instead of WbrWhMustWriteLock(wbr) */
  3047. + SiMustWriteLock(sb);
  3048. + for (i = 0; i < AuBrWh_Last; i++) {
  3049. + dput(wbr->wbr_wh[i]);
  3050. + wbr->wbr_wh[i] = NULL;
  3051. + }
  3052. + }
  3053. +
  3054. + err = test_children_busy(root, bindex, verbose);
  3055. + if (unlikely(err)) {
  3056. + if (do_wh)
  3057. + goto out_wh;
  3058. + goto out;
  3059. + }
  3060. +
  3061. + err = 0;
  3062. + if (to_free) {
  3063. + /*
  3064. + * now we confirmed the branch is deletable.
  3065. + * let's free the remaining opened dirs on the branch.
  3066. + */
  3067. + di_write_unlock(root);
  3068. + br_del_file(to_free, opened, br_id);
  3069. + di_write_lock_child(root);
  3070. + }
  3071. +
  3072. + sysaufs_brs_del(sb, bindex); /* remove successors */
  3073. + dbgaufs_xino_del(br); /* remove one */
  3074. + au_br_do_del(sb, bindex, br);
  3075. + sysaufs_brs_add(sb, bindex); /* append successors */
  3076. + dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
  3077. +
  3078. + if (!bindex) {
  3079. + au_cpup_attr_all(d_inode(root), /*force*/1);
  3080. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  3081. + } else
  3082. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  3083. + if (au_opt_test(mnt_flags, PLINK))
  3084. + au_plink_half_refresh(sb, br_id);
  3085. +
  3086. + goto out; /* success */
  3087. +
  3088. +out_wh:
  3089. + /* revert */
  3090. + rerr = au_br_init_wh(sb, br, br->br_perm);
  3091. + if (rerr)
  3092. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  3093. + del->pathname, rerr);
  3094. +out:
  3095. + if (to_free)
  3096. + au_farray_free(to_free, opened);
  3097. + return err;
  3098. +}
  3099. +
  3100. +/* ---------------------------------------------------------------------- */
  3101. +
  3102. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  3103. +{
  3104. + int err;
  3105. + aufs_bindex_t btop, bbot;
  3106. + struct aufs_ibusy ibusy;
  3107. + struct inode *inode, *h_inode;
  3108. +
  3109. + err = -EPERM;
  3110. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  3111. + goto out;
  3112. +
  3113. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  3114. + if (!err)
  3115. + /* VERIFY_WRITE */
  3116. + err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
  3117. + if (unlikely(err)) {
  3118. + err = -EFAULT;
  3119. + AuTraceErr(err);
  3120. + goto out;
  3121. + }
  3122. +
  3123. + err = -EINVAL;
  3124. + si_read_lock(sb, AuLock_FLUSH);
  3125. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  3126. + goto out_unlock;
  3127. +
  3128. + err = 0;
  3129. + ibusy.h_ino = 0; /* invalid */
  3130. + inode = ilookup(sb, ibusy.ino);
  3131. + if (!inode
  3132. + || inode->i_ino == AUFS_ROOT_INO
  3133. + || au_is_bad_inode(inode))
  3134. + goto out_unlock;
  3135. +
  3136. + ii_read_lock_child(inode);
  3137. + btop = au_ibtop(inode);
  3138. + bbot = au_ibbot(inode);
  3139. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  3140. + h_inode = au_h_iptr(inode, ibusy.bindex);
  3141. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  3142. + ibusy.h_ino = h_inode->i_ino;
  3143. + }
  3144. + ii_read_unlock(inode);
  3145. + iput(inode);
  3146. +
  3147. +out_unlock:
  3148. + si_read_unlock(sb);
  3149. + if (!err) {
  3150. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  3151. + if (unlikely(err)) {
  3152. + err = -EFAULT;
  3153. + AuTraceErr(err);
  3154. + }
  3155. + }
  3156. +out:
  3157. + return err;
  3158. +}
  3159. +
  3160. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  3161. +{
  3162. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  3163. +}
  3164. +
  3165. +#ifdef CONFIG_COMPAT
  3166. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  3167. +{
  3168. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  3169. +}
  3170. +#endif
  3171. +
  3172. +/* ---------------------------------------------------------------------- */
  3173. +
  3174. +/*
  3175. + * change a branch permission
  3176. + */
  3177. +
  3178. +static void au_warn_ima(void)
  3179. +{
  3180. +#ifdef CONFIG_IMA
  3181. + /* since it doesn't support mark_files_ro() */
  3182. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  3183. +#endif
  3184. +}
  3185. +
  3186. +static int do_need_sigen_inc(int a, int b)
  3187. +{
  3188. + return au_br_whable(a) && !au_br_whable(b);
  3189. +}
  3190. +
  3191. +static int need_sigen_inc(int old, int new)
  3192. +{
  3193. + return do_need_sigen_inc(old, new)
  3194. + || do_need_sigen_inc(new, old);
  3195. +}
  3196. +
  3197. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3198. +{
  3199. + int err, do_warn;
  3200. + unsigned int mnt_flags;
  3201. + unsigned long long ull, max;
  3202. + aufs_bindex_t br_id;
  3203. + unsigned char verbose, writer;
  3204. + struct file *file, *hf, **array;
  3205. + struct au_hfile *hfile;
  3206. + struct inode *h_inode;
  3207. +
  3208. + mnt_flags = au_mntflags(sb);
  3209. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3210. +
  3211. + array = au_farray_alloc(sb, &max);
  3212. + err = PTR_ERR(array);
  3213. + if (IS_ERR(array))
  3214. + goto out;
  3215. +
  3216. + do_warn = 0;
  3217. + br_id = au_sbr_id(sb, bindex);
  3218. + for (ull = 0; ull < max; ull++) {
  3219. + file = array[ull];
  3220. + if (unlikely(!file))
  3221. + break;
  3222. +
  3223. + /* AuDbg("%pD\n", file); */
  3224. + fi_read_lock(file);
  3225. + if (unlikely(au_test_mmapped(file))) {
  3226. + err = -EBUSY;
  3227. + AuVerbose(verbose, "mmapped %pD\n", file);
  3228. + AuDbgFile(file);
  3229. + FiMustNoWaiters(file);
  3230. + fi_read_unlock(file);
  3231. + goto out_array;
  3232. + }
  3233. +
  3234. + hfile = &au_fi(file)->fi_htop;
  3235. + hf = hfile->hf_file;
  3236. + if (!d_is_reg(file->f_path.dentry)
  3237. + || !(file->f_mode & FMODE_WRITE)
  3238. + || hfile->hf_br->br_id != br_id
  3239. + || !(hf->f_mode & FMODE_WRITE))
  3240. + array[ull] = NULL;
  3241. + else {
  3242. + do_warn = 1;
  3243. + get_file(file);
  3244. + }
  3245. +
  3246. + FiMustNoWaiters(file);
  3247. + fi_read_unlock(file);
  3248. + fput(file);
  3249. + }
  3250. +
  3251. + err = 0;
  3252. + if (do_warn)
  3253. + au_warn_ima();
  3254. +
  3255. + for (ull = 0; ull < max; ull++) {
  3256. + file = array[ull];
  3257. + if (!file)
  3258. + continue;
  3259. +
  3260. + /* todo: already flushed? */
  3261. + /*
  3262. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3263. + * approach which resets f_mode and calls mnt_drop_write() and
  3264. + * file_release_write() for each file, because the branch
  3265. + * attribute in aufs world is totally different from the native
  3266. + * fs rw/ro mode.
  3267. + */
  3268. + /* fi_read_lock(file); */
  3269. + hfile = &au_fi(file)->fi_htop;
  3270. + hf = hfile->hf_file;
  3271. + /* fi_read_unlock(file); */
  3272. + spin_lock(&hf->f_lock);
  3273. + writer = !!(hf->f_mode & FMODE_WRITER);
  3274. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3275. + spin_unlock(&hf->f_lock);
  3276. + if (writer) {
  3277. + h_inode = file_inode(hf);
  3278. + if (hf->f_mode & FMODE_READ)
  3279. + i_readcount_inc(h_inode);
  3280. + put_write_access(h_inode);
  3281. + __mnt_drop_write(hf->f_path.mnt);
  3282. + }
  3283. + }
  3284. +
  3285. +out_array:
  3286. + au_farray_free(array, max);
  3287. +out:
  3288. + AuTraceErr(err);
  3289. + return err;
  3290. +}
  3291. +
  3292. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3293. + int *do_refresh)
  3294. +{
  3295. + int err, rerr;
  3296. + aufs_bindex_t bindex;
  3297. + struct dentry *root;
  3298. + struct au_branch *br;
  3299. + struct au_br_fhsm *bf;
  3300. +
  3301. + root = sb->s_root;
  3302. + bindex = au_find_dbindex(root, mod->h_root);
  3303. + if (bindex < 0) {
  3304. + if (remount)
  3305. + return 0; /* success */
  3306. + err = -ENOENT;
  3307. + pr_err("%s no such branch\n", mod->path);
  3308. + goto out;
  3309. + }
  3310. + AuDbg("bindex b%d\n", bindex);
  3311. +
  3312. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3313. + if (unlikely(err))
  3314. + goto out;
  3315. +
  3316. + br = au_sbr(sb, bindex);
  3317. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3318. + if (br->br_perm == mod->perm)
  3319. + return 0; /* success */
  3320. +
  3321. + /* pre-allocate for non-fhsm --> fhsm */
  3322. + bf = NULL;
  3323. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3324. + err = au_fhsm_br_alloc(br);
  3325. + if (unlikely(err))
  3326. + goto out;
  3327. + bf = br->br_fhsm;
  3328. + br->br_fhsm = NULL;
  3329. + }
  3330. +
  3331. + if (au_br_writable(br->br_perm)) {
  3332. + /* remove whiteout base */
  3333. + err = au_br_init_wh(sb, br, mod->perm);
  3334. + if (unlikely(err))
  3335. + goto out_bf;
  3336. +
  3337. + if (!au_br_writable(mod->perm)) {
  3338. + /* rw --> ro, file might be mmapped */
  3339. + DiMustNoWaiters(root);
  3340. + IiMustNoWaiters(d_inode(root));
  3341. + di_write_unlock(root);
  3342. + err = au_br_mod_files_ro(sb, bindex);
  3343. + /* aufs_write_lock() calls ..._child() */
  3344. + di_write_lock_child(root);
  3345. +
  3346. + if (unlikely(err)) {
  3347. + rerr = -ENOMEM;
  3348. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3349. + GFP_NOFS);
  3350. + if (br->br_wbr)
  3351. + rerr = au_wbr_init(br, sb, br->br_perm);
  3352. + if (unlikely(rerr)) {
  3353. + AuIOErr("nested error %d (%d)\n",
  3354. + rerr, err);
  3355. + br->br_perm = mod->perm;
  3356. + }
  3357. + }
  3358. + }
  3359. + } else if (au_br_writable(mod->perm)) {
  3360. + /* ro --> rw */
  3361. + err = -ENOMEM;
  3362. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3363. + if (br->br_wbr) {
  3364. + err = au_wbr_init(br, sb, mod->perm);
  3365. + if (unlikely(err)) {
  3366. + au_kfree_rcu(br->br_wbr);
  3367. + br->br_wbr = NULL;
  3368. + }
  3369. + }
  3370. + }
  3371. + if (unlikely(err))
  3372. + goto out_bf;
  3373. +
  3374. + if (au_br_fhsm(br->br_perm)) {
  3375. + if (!au_br_fhsm(mod->perm)) {
  3376. + /* fhsm --> non-fhsm */
  3377. + au_br_fhsm_fin(br->br_fhsm);
  3378. + au_kfree_rcu(br->br_fhsm);
  3379. + br->br_fhsm = NULL;
  3380. + }
  3381. + } else if (au_br_fhsm(mod->perm))
  3382. + /* non-fhsm --> fhsm */
  3383. + br->br_fhsm = bf;
  3384. +
  3385. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3386. + br->br_perm = mod->perm;
  3387. + goto out; /* success */
  3388. +
  3389. +out_bf:
  3390. + au_kfree_try_rcu(bf);
  3391. +out:
  3392. + AuTraceErr(err);
  3393. + return err;
  3394. +}
  3395. +
  3396. +/* ---------------------------------------------------------------------- */
  3397. +
  3398. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3399. +{
  3400. + int err;
  3401. + struct kstatfs kstfs;
  3402. +
  3403. + err = vfs_statfs(&br->br_path, &kstfs);
  3404. + if (!err) {
  3405. + stfs->f_blocks = kstfs.f_blocks;
  3406. + stfs->f_bavail = kstfs.f_bavail;
  3407. + stfs->f_files = kstfs.f_files;
  3408. + stfs->f_ffree = kstfs.f_ffree;
  3409. + }
  3410. +
  3411. + return err;
  3412. +}
  3413. diff -Naur null/fs/aufs/branch.h linux-6.6.63/fs/aufs/branch.h
  3414. --- /dev/null
  3415. +++ linux-6.6.63/fs/aufs/branch.h 2024-12-18 15:12:59.293519613 +0300
  3416. @@ -0,0 +1,375 @@
  3417. +/* SPDX-License-Identifier: GPL-2.0 */
  3418. +/*
  3419. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  3420. + *
  3421. + * This program is free software; you can redistribute it and/or modify
  3422. + * it under the terms of the GNU General Public License as published by
  3423. + * the Free Software Foundation; either version 2 of the License, or
  3424. + * (at your option) any later version.
  3425. + *
  3426. + * This program is distributed in the hope that it will be useful,
  3427. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3428. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3429. + * GNU General Public License for more details.
  3430. + *
  3431. + * You should have received a copy of the GNU General Public License
  3432. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3433. + */
  3434. +
  3435. +/*
  3436. + * branch filesystems and xino for them
  3437. + */
  3438. +
  3439. +#ifndef __AUFS_BRANCH_H__
  3440. +#define __AUFS_BRANCH_H__
  3441. +
  3442. +#ifdef __KERNEL__
  3443. +
  3444. +#include <linux/mount.h>
  3445. +#include "dirren.h"
  3446. +#include "dynop.h"
  3447. +#include "lcnt.h"
  3448. +#include "rwsem.h"
  3449. +#include "super.h"
  3450. +
  3451. +/* ---------------------------------------------------------------------- */
  3452. +
  3453. +/* a xino file */
  3454. +struct au_xino {
  3455. + struct file **xi_file;
  3456. + unsigned int xi_nfile;
  3457. +
  3458. + struct {
  3459. + spinlock_t spin;
  3460. + ino_t *array;
  3461. + int total;
  3462. + /* reserved for future use */
  3463. + /* unsigned long *bitmap; */
  3464. + wait_queue_head_t wqh;
  3465. + } xi_nondir;
  3466. +
  3467. + struct mutex xi_mtx; /* protects xi_file array */
  3468. + struct hlist_bl_head xi_writing;
  3469. +
  3470. + atomic_t xi_truncating;
  3471. +
  3472. + struct kref xi_kref;
  3473. +};
  3474. +
  3475. +/* File-based Hierarchical Storage Management */
  3476. +struct au_br_fhsm {
  3477. +#ifdef CONFIG_AUFS_FHSM
  3478. + struct mutex bf_lock;
  3479. + unsigned long bf_jiffy;
  3480. + struct aufs_stfs bf_stfs;
  3481. + int bf_readable;
  3482. +#endif
  3483. +};
  3484. +
  3485. +/* members for writable branch only */
  3486. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3487. +struct au_wbr {
  3488. + struct au_rwsem wbr_wh_rwsem;
  3489. + struct dentry *wbr_wh[AuBrWh_Last];
  3490. + atomic_t wbr_wh_running;
  3491. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3492. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3493. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3494. +
  3495. + /* mfs mode */
  3496. + unsigned long long wbr_bytes;
  3497. +};
  3498. +
  3499. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3500. +#define AuBrDynOp (AuDyLast * 4)
  3501. +
  3502. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3503. +/* support for asynchronous destruction */
  3504. +struct au_br_hfsnotify {
  3505. + struct fsnotify_group *hfsn_group;
  3506. +};
  3507. +#endif
  3508. +
  3509. +/* sysfs entries */
  3510. +struct au_brsysfs {
  3511. + char name[16];
  3512. + struct attribute attr;
  3513. +};
  3514. +
  3515. +enum {
  3516. + AuBrSysfs_BR,
  3517. + AuBrSysfs_BRID,
  3518. + AuBrSysfs_Last
  3519. +};
  3520. +
  3521. +/* protected by superblock rwsem */
  3522. +struct au_branch {
  3523. + struct au_xino *br_xino;
  3524. +
  3525. + aufs_bindex_t br_id;
  3526. +
  3527. + int br_perm;
  3528. + struct path br_path;
  3529. + spinlock_t br_dykey_lock;
  3530. + struct au_dykey *br_dykey[AuBrDynOp];
  3531. + au_lcnt_t br_nfiles; /* opened files */
  3532. + au_lcnt_t br_count; /* in-use for other */
  3533. +
  3534. + struct au_wbr *br_wbr;
  3535. + struct au_br_fhsm *br_fhsm;
  3536. +
  3537. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3538. + struct au_br_hfsnotify *br_hfsn;
  3539. +#endif
  3540. +
  3541. +#ifdef CONFIG_SYSFS
  3542. + /* entries under sysfs per mount-point */
  3543. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3544. +#endif
  3545. +
  3546. +#ifdef CONFIG_DEBUG_FS
  3547. + struct dentry *br_dbgaufs; /* xino */
  3548. +#endif
  3549. +
  3550. + struct au_dr_br br_dirren;
  3551. +};
  3552. +
  3553. +/* ---------------------------------------------------------------------- */
  3554. +
  3555. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3556. +{
  3557. + return br->br_path.mnt;
  3558. +}
  3559. +
  3560. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3561. +{
  3562. + return br->br_path.dentry;
  3563. +}
  3564. +
  3565. +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
  3566. +{
  3567. + return mnt_idmap(br->br_path.mnt);
  3568. +}
  3569. +
  3570. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3571. +{
  3572. + return au_br_mnt(br)->mnt_sb;
  3573. +}
  3574. +
  3575. +static inline int au_br_rdonly(struct au_branch *br)
  3576. +{
  3577. + return (sb_rdonly(au_br_sb(br))
  3578. + || !au_br_writable(br->br_perm))
  3579. + ? -EROFS : 0;
  3580. +}
  3581. +
  3582. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3583. +{
  3584. +#ifdef CONFIG_AUFS_HNOTIFY
  3585. + return !(brperm & AuBrPerm_RR);
  3586. +#else
  3587. + return 0;
  3588. +#endif
  3589. +}
  3590. +
  3591. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3592. +{
  3593. + int err, exec_flag;
  3594. +
  3595. + err = 0;
  3596. + exec_flag = oflag & __FMODE_EXEC;
  3597. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3598. + err = -EACCES;
  3599. +
  3600. + return err;
  3601. +}
  3602. +
  3603. +static inline void au_xino_get(struct au_branch *br)
  3604. +{
  3605. + struct au_xino *xi;
  3606. +
  3607. + xi = br->br_xino;
  3608. + if (xi)
  3609. + kref_get(&xi->xi_kref);
  3610. +}
  3611. +
  3612. +static inline int au_xino_count(struct au_branch *br)
  3613. +{
  3614. + int v;
  3615. + struct au_xino *xi;
  3616. +
  3617. + v = 0;
  3618. + xi = br->br_xino;
  3619. + if (xi)
  3620. + v = kref_read(&xi->xi_kref);
  3621. +
  3622. + return v;
  3623. +}
  3624. +
  3625. +/* ---------------------------------------------------------------------- */
  3626. +
  3627. +/* branch.c */
  3628. +struct au_sbinfo;
  3629. +void au_br_free(struct au_sbinfo *sinfo);
  3630. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3631. +struct au_opt_add;
  3632. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3633. +struct au_opt_del;
  3634. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3635. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3636. +#ifdef CONFIG_COMPAT
  3637. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3638. +#endif
  3639. +struct au_opt_mod;
  3640. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3641. + int *do_refresh);
  3642. +struct aufs_stfs;
  3643. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3644. +
  3645. +/* xino.c */
  3646. +static const loff_t au_loff_max = LLONG_MAX;
  3647. +
  3648. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
  3649. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
  3650. + int wbrtop);
  3651. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  3652. + struct file *copy_src);
  3653. +struct au_xi_new {
  3654. + struct au_xino *xi; /* switch between xino and xigen */
  3655. + int idx;
  3656. + struct path *base;
  3657. + struct file *copy_src;
  3658. +};
  3659. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
  3660. +
  3661. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3662. + ino_t *ino);
  3663. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3664. + ino_t ino);
  3665. +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
  3666. +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
  3667. +
  3668. +int au_xib_trunc(struct super_block *sb);
  3669. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
  3670. +
  3671. +struct au_xino *au_xino_alloc(unsigned int nfile);
  3672. +int au_xino_put(struct au_branch *br);
  3673. +struct file *au_xino_file1(struct au_xino *xi);
  3674. +
  3675. +struct au_opt_xino;
  3676. +void au_xino_clr(struct super_block *sb);
  3677. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
  3678. +struct file *au_xino_def(struct super_block *sb);
  3679. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3680. + struct path *base);
  3681. +
  3682. +ino_t au_xino_new_ino(struct super_block *sb);
  3683. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3684. +
  3685. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  3686. + ino_t h_ino, int idx);
  3687. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3688. + int *idx);
  3689. +
  3690. +int au_xino_path(struct seq_file *seq, struct file *file);
  3691. +
  3692. +/* ---------------------------------------------------------------------- */
  3693. +
  3694. +/* @idx is signed to accept -1 meaning the first file */
  3695. +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
  3696. +{
  3697. + struct file *file;
  3698. +
  3699. + file = NULL;
  3700. + if (!xi)
  3701. + goto out;
  3702. +
  3703. + if (idx >= 0) {
  3704. + if (idx < xi->xi_nfile)
  3705. + file = xi->xi_file[idx];
  3706. + } else
  3707. + file = au_xino_file1(xi);
  3708. +
  3709. +out:
  3710. + return file;
  3711. +}
  3712. +
  3713. +/* ---------------------------------------------------------------------- */
  3714. +
  3715. +/* Superblock to branch */
  3716. +static inline
  3717. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3718. +{
  3719. + return au_sbr(sb, bindex)->br_id;
  3720. +}
  3721. +
  3722. +static inline
  3723. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3724. +{
  3725. + return au_br_mnt(au_sbr(sb, bindex));
  3726. +}
  3727. +
  3728. +static inline
  3729. +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
  3730. +{
  3731. + return au_br_idmap(au_sbr(sb, bindex));
  3732. +}
  3733. +
  3734. +static inline
  3735. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3736. +{
  3737. + return au_br_sb(au_sbr(sb, bindex));
  3738. +}
  3739. +
  3740. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3741. +{
  3742. + return au_sbr(sb, bindex)->br_perm;
  3743. +}
  3744. +
  3745. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3746. +{
  3747. + return au_br_whable(au_sbr_perm(sb, bindex));
  3748. +}
  3749. +
  3750. +/* ---------------------------------------------------------------------- */
  3751. +
  3752. +#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
  3753. +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
  3754. +#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
  3755. +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
  3756. +/*
  3757. +#define wbr_wh_read_trylock_nested(wbr) \
  3758. + au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3759. +#define wbr_wh_write_trylock_nested(wbr) \
  3760. + au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3761. +*/
  3762. +
  3763. +#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
  3764. +#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
  3765. +#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
  3766. +
  3767. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
  3768. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
  3769. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
  3770. +
  3771. +/* ---------------------------------------------------------------------- */
  3772. +
  3773. +#ifdef CONFIG_AUFS_FHSM
  3774. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3775. +{
  3776. + mutex_init(&brfhsm->bf_lock);
  3777. + brfhsm->bf_jiffy = 0;
  3778. + brfhsm->bf_readable = 0;
  3779. +}
  3780. +
  3781. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3782. +{
  3783. + mutex_destroy(&brfhsm->bf_lock);
  3784. +}
  3785. +#else
  3786. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3787. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3788. +#endif
  3789. +
  3790. +#endif /* __KERNEL__ */
  3791. +#endif /* __AUFS_BRANCH_H__ */
  3792. diff -Naur null/fs/aufs/conf.mk linux-6.6.63/fs/aufs/conf.mk
  3793. --- /dev/null
  3794. +++ linux-6.6.63/fs/aufs/conf.mk 2024-12-18 15:12:59.293519613 +0300
  3795. @@ -0,0 +1,40 @@
  3796. +# SPDX-License-Identifier: GPL-2.0
  3797. +
  3798. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3799. +
  3800. +define AuConf
  3801. +ifdef ${1}
  3802. +AuConfStr += ${1}=${${1}}
  3803. +endif
  3804. +endef
  3805. +
  3806. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3807. + SBILIST \
  3808. + HNOTIFY HFSNOTIFY \
  3809. + EXPORT INO_T_64 \
  3810. + XATTR \
  3811. + FHSM \
  3812. + RDU \
  3813. + DIRREN \
  3814. + SHWH \
  3815. + BR_RAMFS \
  3816. + BR_FUSE POLL \
  3817. + BR_HFSPLUS \
  3818. + BDEV_LOOP \
  3819. + DEBUG MAGIC_SYSRQ
  3820. +$(foreach i, ${AuConfAll}, \
  3821. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3822. +
  3823. +AuConfName = ${obj}/conf.str
  3824. +${AuConfName}.tmp: FORCE
  3825. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3826. +${AuConfName}: ${AuConfName}.tmp
  3827. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3828. + echo ' GEN ' $@; \
  3829. + cp -p $< $@; \
  3830. + }
  3831. +FORCE:
  3832. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3833. +${obj}/sysfs.o: ${AuConfName}
  3834. +
  3835. +-include ${srctree}/${src}/conf_priv.mk
  3836. diff -Naur null/fs/aufs/cpup.c linux-6.6.63/fs/aufs/cpup.c
  3837. --- /dev/null
  3838. +++ linux-6.6.63/fs/aufs/cpup.c 2024-12-18 15:12:59.293519613 +0300
  3839. @@ -0,0 +1,1458 @@
  3840. +// SPDX-License-Identifier: GPL-2.0
  3841. +/*
  3842. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  3843. + *
  3844. + * This program is free software; you can redistribute it and/or modify
  3845. + * it under the terms of the GNU General Public License as published by
  3846. + * the Free Software Foundation; either version 2 of the License, or
  3847. + * (at your option) any later version.
  3848. + *
  3849. + * This program is distributed in the hope that it will be useful,
  3850. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3851. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3852. + * GNU General Public License for more details.
  3853. + *
  3854. + * You should have received a copy of the GNU General Public License
  3855. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3856. + */
  3857. +
  3858. +/*
  3859. + * copy-up functions, see wbr_policy.c for copy-down
  3860. + */
  3861. +
  3862. +#include <linux/fs_stack.h>
  3863. +#include <linux/mm.h>
  3864. +#include <linux/task_work.h>
  3865. +#include "aufs.h"
  3866. +
  3867. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3868. +{
  3869. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3870. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3871. +
  3872. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3873. +
  3874. + dst->i_flags |= iflags & ~mask;
  3875. + if (au_test_fs_notime(dst->i_sb))
  3876. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3877. +}
  3878. +
  3879. +void au_cpup_attr_timesizes(struct inode *inode)
  3880. +{
  3881. + struct inode *h_inode;
  3882. +
  3883. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3884. + fsstack_copy_attr_times(inode, h_inode);
  3885. + fsstack_copy_inode_size(inode, h_inode);
  3886. +}
  3887. +
  3888. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3889. +{
  3890. + struct inode *h_inode;
  3891. + struct super_block *sb;
  3892. + aufs_bindex_t bindex, bbot;
  3893. +
  3894. + sb = inode->i_sb;
  3895. + bindex = au_ibtop(inode);
  3896. + h_inode = au_h_iptr(inode, bindex);
  3897. + if (!force
  3898. + && !S_ISDIR(h_inode->i_mode)
  3899. + && au_opt_test(au_mntflags(sb), PLINK)
  3900. + && au_plink_test(inode))
  3901. + return;
  3902. +
  3903. + /*
  3904. + * 0 can happen in revalidating.
  3905. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3906. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3907. + * case.
  3908. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3909. + * the incorrect link count.
  3910. + */
  3911. + vfsub_set_nlink(inode, vfsub_inode_nlink(h_inode, AU_I_BRANCH));
  3912. +
  3913. + /*
  3914. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3915. + * it may includes whplink directory.
  3916. + */
  3917. + if (S_ISDIR(h_inode->i_mode)) {
  3918. + bbot = au_ibbot(inode);
  3919. + for (bindex++; bindex <= bbot; bindex++) {
  3920. + h_inode = au_h_iptr(inode, bindex);
  3921. + if (h_inode)
  3922. + au_add_nlink(inode, h_inode);
  3923. + }
  3924. + }
  3925. +}
  3926. +
  3927. +void au_cpup_attr_changeable(struct inode *inode)
  3928. +{
  3929. + struct inode *h_inode;
  3930. +
  3931. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3932. + inode->i_mode = h_inode->i_mode;
  3933. + inode->i_uid = h_inode->i_uid;
  3934. + inode->i_gid = h_inode->i_gid;
  3935. + au_cpup_attr_timesizes(inode);
  3936. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3937. +}
  3938. +
  3939. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3940. +{
  3941. + struct au_iinfo *iinfo = au_ii(inode);
  3942. +
  3943. + IiMustWriteLock(inode);
  3944. +
  3945. + iinfo->ii_higen = h_inode->i_generation;
  3946. + iinfo->ii_hsb1 = h_inode->i_sb;
  3947. +}
  3948. +
  3949. +void au_cpup_attr_all(struct inode *inode, int force)
  3950. +{
  3951. + struct inode *h_inode;
  3952. +
  3953. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3954. + au_cpup_attr_changeable(inode);
  3955. + au_cpup_attr_nlink(inode, force);
  3956. + inode->i_rdev = h_inode->i_rdev;
  3957. + inode->i_blkbits = h_inode->i_blkbits;
  3958. + au_cpup_igen(inode, h_inode);
  3959. +}
  3960. +
  3961. +/* ---------------------------------------------------------------------- */
  3962. +
  3963. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3964. +
  3965. +/* keep the timestamps of the parent dir when cpup */
  3966. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3967. + struct path *h_path)
  3968. +{
  3969. + struct inode *h_inode;
  3970. +
  3971. + dt->dt_dentry = dentry;
  3972. + dt->dt_h_path = *h_path;
  3973. + h_inode = d_inode(h_path->dentry);
  3974. + dt->dt_atime = h_inode->i_atime;
  3975. + dt->dt_mtime = h_inode->i_mtime;
  3976. + /* smp_mb(); */
  3977. +}
  3978. +
  3979. +void au_dtime_revert(struct au_dtime *dt)
  3980. +{
  3981. + struct iattr attr;
  3982. + int err;
  3983. +
  3984. + attr.ia_atime = dt->dt_atime;
  3985. + attr.ia_mtime = dt->dt_mtime;
  3986. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3987. + | ATTR_ATIME | ATTR_ATIME_SET;
  3988. +
  3989. + /* no delegation since this is a directory */
  3990. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3991. + if (unlikely(err))
  3992. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3993. +}
  3994. +
  3995. +/* ---------------------------------------------------------------------- */
  3996. +
  3997. +/* internal use only */
  3998. +struct au_cpup_reg_attr {
  3999. + int valid;
  4000. + struct kstat st;
  4001. + unsigned int iflags; /* inode->i_flags */
  4002. +};
  4003. +
  4004. +static noinline_for_stack
  4005. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
  4006. + struct au_cpup_reg_attr *h_src_attr)
  4007. +{
  4008. + int err, sbits, icex;
  4009. + unsigned int mnt_flags;
  4010. + unsigned char verbose;
  4011. + struct iattr ia;
  4012. + struct path h_path;
  4013. + struct inode *h_isrc, *h_idst;
  4014. + struct kstat *h_st;
  4015. + struct au_branch *br;
  4016. +
  4017. + br = au_sbr(dst->d_sb, bindex);
  4018. + h_path.mnt = au_br_mnt(br);
  4019. + h_path.dentry = au_h_dptr(dst, bindex);
  4020. + h_idst = d_inode(h_path.dentry);
  4021. + h_isrc = d_inode(h_src->dentry);
  4022. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  4023. + | ATTR_ATIME | ATTR_MTIME
  4024. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  4025. + if (h_src_attr && h_src_attr->valid) {
  4026. + h_st = &h_src_attr->st;
  4027. + ia.ia_uid = h_st->uid;
  4028. + ia.ia_gid = h_st->gid;
  4029. + ia.ia_atime = h_st->atime;
  4030. + ia.ia_mtime = h_st->mtime;
  4031. + if (h_idst->i_mode != h_st->mode
  4032. + && !S_ISLNK(h_idst->i_mode)) {
  4033. + ia.ia_valid |= ATTR_MODE;
  4034. + ia.ia_mode = h_st->mode;
  4035. + }
  4036. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  4037. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  4038. + } else {
  4039. + ia.ia_uid = h_isrc->i_uid;
  4040. + ia.ia_gid = h_isrc->i_gid;
  4041. + ia.ia_atime = h_isrc->i_atime;
  4042. + ia.ia_mtime = h_isrc->i_mtime;
  4043. + if (h_idst->i_mode != h_isrc->i_mode
  4044. + && !S_ISLNK(h_idst->i_mode)) {
  4045. + ia.ia_valid |= ATTR_MODE;
  4046. + ia.ia_mode = h_isrc->i_mode;
  4047. + }
  4048. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  4049. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  4050. + }
  4051. + /* no delegation since it is just created */
  4052. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4053. +
  4054. + /* is this nfs only? */
  4055. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  4056. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  4057. + ia.ia_mode = h_isrc->i_mode;
  4058. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4059. + }
  4060. +
  4061. + icex = br->br_perm & AuBrAttr_ICEX;
  4062. + if (!err) {
  4063. + mnt_flags = au_mntflags(dst->d_sb);
  4064. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  4065. + err = au_cpup_xattr(&h_path, h_src, icex, verbose);
  4066. + }
  4067. +
  4068. + return err;
  4069. +}
  4070. +
  4071. +/* ---------------------------------------------------------------------- */
  4072. +
  4073. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  4074. + char *buf, unsigned long blksize)
  4075. +{
  4076. + int err;
  4077. + size_t sz, rbytes, wbytes;
  4078. + unsigned char all_zero;
  4079. + char *p, *zp;
  4080. + struct inode *h_inode;
  4081. + /* reduce stack usage */
  4082. + struct iattr *ia;
  4083. +
  4084. + zp = page_address(ZERO_PAGE(0));
  4085. + if (unlikely(!zp))
  4086. + return -ENOMEM; /* possible? */
  4087. +
  4088. + err = 0;
  4089. + all_zero = 0;
  4090. + while (len) {
  4091. + AuDbg("len %lld\n", len);
  4092. + sz = blksize;
  4093. + if (len < blksize)
  4094. + sz = len;
  4095. +
  4096. + rbytes = 0;
  4097. + /* todo: signal_pending? */
  4098. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  4099. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  4100. + err = rbytes;
  4101. + }
  4102. + if (unlikely(err < 0))
  4103. + break;
  4104. +
  4105. + all_zero = 0;
  4106. + if (len >= rbytes && rbytes == blksize)
  4107. + all_zero = !memcmp(buf, zp, rbytes);
  4108. + if (!all_zero) {
  4109. + wbytes = rbytes;
  4110. + p = buf;
  4111. + while (wbytes) {
  4112. + size_t b;
  4113. +
  4114. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  4115. + err = b;
  4116. + /* todo: signal_pending? */
  4117. + if (unlikely(err == -EAGAIN || err == -EINTR))
  4118. + continue;
  4119. + if (unlikely(err < 0))
  4120. + break;
  4121. + wbytes -= b;
  4122. + p += b;
  4123. + }
  4124. + if (unlikely(err < 0))
  4125. + break;
  4126. + } else {
  4127. + loff_t res;
  4128. +
  4129. + AuLabel(hole);
  4130. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  4131. + err = res;
  4132. + if (unlikely(res < 0))
  4133. + break;
  4134. + }
  4135. + len -= rbytes;
  4136. + err = 0;
  4137. + }
  4138. +
  4139. + /* the last block may be a hole */
  4140. + if (!err && all_zero) {
  4141. + AuLabel(last hole);
  4142. +
  4143. + err = 1;
  4144. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  4145. + /* nfs requires this step to make last hole */
  4146. + /* is this only nfs? */
  4147. + do {
  4148. + /* todo: signal_pending? */
  4149. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  4150. + } while (err == -EAGAIN || err == -EINTR);
  4151. + if (err == 1)
  4152. + dst->f_pos--;
  4153. + }
  4154. +
  4155. + if (err == 1) {
  4156. + ia = (void *)buf;
  4157. + ia->ia_size = dst->f_pos;
  4158. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  4159. + ia->ia_file = dst;
  4160. + h_inode = file_inode(dst);
  4161. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  4162. + /* no delegation since it is just created */
  4163. + err = vfsub_notify_change(&dst->f_path, ia,
  4164. + /*delegated*/NULL);
  4165. + inode_unlock(h_inode);
  4166. + }
  4167. + }
  4168. +
  4169. + return err;
  4170. +}
  4171. +
  4172. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  4173. +{
  4174. + int err;
  4175. + unsigned long blksize;
  4176. + unsigned char do_kfree;
  4177. + char *buf;
  4178. + struct super_block *h_sb;
  4179. +
  4180. + err = -ENOMEM;
  4181. + h_sb = file_inode(dst)->i_sb;
  4182. + blksize = h_sb->s_blocksize;
  4183. + if (!blksize || PAGE_SIZE < blksize)
  4184. + blksize = PAGE_SIZE;
  4185. + AuDbg("blksize %lu\n", blksize);
  4186. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  4187. + if (do_kfree)
  4188. + buf = kmalloc(blksize, GFP_NOFS);
  4189. + else
  4190. + buf = (void *)__get_free_page(GFP_NOFS);
  4191. + if (unlikely(!buf))
  4192. + goto out;
  4193. +
  4194. + if (len > (1 << 22))
  4195. + AuDbg("copying a large file %lld\n", (long long)len);
  4196. +
  4197. + src->f_pos = 0;
  4198. + dst->f_pos = 0;
  4199. + err = au_do_copy_file(dst, src, len, buf, blksize);
  4200. + if (do_kfree) {
  4201. + AuDebugOn(!au_kfree_do_sz_test(blksize));
  4202. + au_kfree_do_rcu(buf);
  4203. + } else
  4204. + free_page((unsigned long)buf);
  4205. +
  4206. +out:
  4207. + return err;
  4208. +}
  4209. +
  4210. +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
  4211. +{
  4212. + int err;
  4213. + struct super_block *h_src_sb;
  4214. + struct inode *h_src_inode;
  4215. +
  4216. + h_src_inode = file_inode(src);
  4217. + h_src_sb = h_src_inode->i_sb;
  4218. +
  4219. + /* XFS acquires inode_lock */
  4220. + if (!au_test_xfs(h_src_sb))
  4221. + err = au_copy_file(dst, src, len);
  4222. + else {
  4223. + inode_unlock_shared(h_src_inode);
  4224. + err = au_copy_file(dst, src, len);
  4225. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4226. + }
  4227. +
  4228. + return err;
  4229. +}
  4230. +
  4231. +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
  4232. +{
  4233. + int err;
  4234. + loff_t lo;
  4235. + struct super_block *h_src_sb;
  4236. + struct inode *h_src_inode;
  4237. +
  4238. + h_src_inode = file_inode(src);
  4239. + h_src_sb = h_src_inode->i_sb;
  4240. + if (h_src_sb != file_inode(dst)->i_sb
  4241. + || !dst->f_op->remap_file_range) {
  4242. + err = au_do_copy(dst, src, len);
  4243. + goto out;
  4244. + }
  4245. +
  4246. + if (!au_test_nfs(h_src_sb)) {
  4247. + inode_unlock_shared(h_src_inode);
  4248. + lo = vfsub_clone_file_range(src, dst, len);
  4249. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4250. + } else
  4251. + lo = vfsub_clone_file_range(src, dst, len);
  4252. + if (lo == len) {
  4253. + err = 0;
  4254. + goto out; /* success */
  4255. + } else if (lo >= 0)
  4256. + /* todo: possible? */
  4257. + /* paritially succeeded */
  4258. + AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
  4259. + else if (lo != -EOPNOTSUPP) {
  4260. + /* older XFS has a condition in cloning */
  4261. + err = lo;
  4262. + goto out;
  4263. + }
  4264. +
  4265. + /* the backend fs on NFS may not support cloning */
  4266. + err = au_do_copy(dst, src, len);
  4267. +
  4268. +out:
  4269. + AuTraceErr(err);
  4270. + return err;
  4271. +}
  4272. +
  4273. +/*
  4274. + * to support a sparse file which is opened with O_APPEND,
  4275. + * we need to close the file.
  4276. + */
  4277. +static int au_cp_regular(struct au_cp_generic *cpg)
  4278. +{
  4279. + int err, i;
  4280. + enum { SRC, DST };
  4281. + struct {
  4282. + aufs_bindex_t bindex;
  4283. + unsigned int flags;
  4284. + struct dentry *dentry;
  4285. + int force_wr;
  4286. + struct file *file;
  4287. + } *f, file[] = {
  4288. + {
  4289. + .bindex = cpg->bsrc,
  4290. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  4291. + },
  4292. + {
  4293. + .bindex = cpg->bdst,
  4294. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  4295. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  4296. + }
  4297. + };
  4298. + struct au_branch *br;
  4299. + struct super_block *sb, *h_src_sb;
  4300. + struct inode *h_src_inode;
  4301. + struct task_struct *tsk = current;
  4302. +
  4303. + /* bsrc branch can be ro/rw. */
  4304. + sb = cpg->dentry->d_sb;
  4305. + f = file;
  4306. + for (i = 0; i < 2; i++, f++) {
  4307. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  4308. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  4309. + /*file*/NULL, f->force_wr);
  4310. + if (IS_ERR(f->file)) {
  4311. + err = PTR_ERR(f->file);
  4312. + if (i == SRC)
  4313. + goto out;
  4314. + else
  4315. + goto out_src;
  4316. + }
  4317. + }
  4318. +
  4319. + /* try stopping to update while we copyup */
  4320. + h_src_inode = d_inode(file[SRC].dentry);
  4321. + h_src_sb = h_src_inode->i_sb;
  4322. + if (!au_test_nfs(h_src_sb))
  4323. + IMustLock(h_src_inode);
  4324. + err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
  4325. +
  4326. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  4327. + if (tsk->flags & PF_KTHREAD)
  4328. + __fput_sync(file[DST].file);
  4329. + else {
  4330. + /* it happened actually */
  4331. + fput(file[DST].file);
  4332. + /*
  4333. + * too bad.
  4334. + * we have to call both since we don't know which place the file
  4335. + * was added to.
  4336. + */
  4337. + task_work_run();
  4338. + flush_delayed_fput();
  4339. + }
  4340. + br = au_sbr(sb, file[DST].bindex);
  4341. + au_lcnt_dec(&br->br_nfiles);
  4342. +
  4343. +out_src:
  4344. + fput(file[SRC].file);
  4345. + br = au_sbr(sb, file[SRC].bindex);
  4346. + au_lcnt_dec(&br->br_nfiles);
  4347. +out:
  4348. + return err;
  4349. +}
  4350. +
  4351. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  4352. + struct au_cpup_reg_attr *h_src_attr)
  4353. +{
  4354. + int err, rerr;
  4355. + loff_t l;
  4356. + struct path h_path;
  4357. + struct inode *h_src_inode, *h_dst_inode;
  4358. +
  4359. + err = 0;
  4360. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4361. + l = i_size_read(h_src_inode);
  4362. + if (cpg->len == -1 || l < cpg->len)
  4363. + cpg->len = l;
  4364. + if (cpg->len) {
  4365. + /* try stopping to update while we are referencing */
  4366. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4367. + au_pin_hdir_unlock(cpg->pin);
  4368. +
  4369. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4370. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4371. + h_src_attr->iflags = h_src_inode->i_flags;
  4372. + if (!au_test_nfs(h_src_inode->i_sb))
  4373. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4374. + else {
  4375. + inode_unlock_shared(h_src_inode);
  4376. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4377. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4378. + }
  4379. + if (unlikely(err)) {
  4380. + inode_unlock_shared(h_src_inode);
  4381. + goto out;
  4382. + }
  4383. + h_src_attr->valid = 1;
  4384. + if (!au_test_fs_unlock_for_open(h_src_inode->i_sb)) {
  4385. + err = au_cp_regular(cpg);
  4386. + inode_unlock_shared(h_src_inode);
  4387. + } else {
  4388. + inode_unlock_shared(h_src_inode);
  4389. + err = au_cp_regular(cpg);
  4390. + }
  4391. + rerr = au_pin_hdir_relock(cpg->pin);
  4392. + if (!err && rerr)
  4393. + err = rerr;
  4394. + }
  4395. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4396. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4397. + h_dst_inode = d_inode(h_path.dentry);
  4398. + spin_lock(&h_dst_inode->i_lock);
  4399. + h_dst_inode->i_state |= I_LINKABLE;
  4400. + spin_unlock(&h_dst_inode->i_lock);
  4401. + }
  4402. +
  4403. +out:
  4404. + return err;
  4405. +}
  4406. +
  4407. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4408. + struct inode *h_dir)
  4409. +{
  4410. + int err;
  4411. + DEFINE_DELAYED_CALL(done);
  4412. + const char *sym;
  4413. +
  4414. + sym = vfs_get_link(h_src, &done);
  4415. + err = PTR_ERR(sym);
  4416. + if (IS_ERR(sym))
  4417. + goto out;
  4418. +
  4419. + err = vfsub_symlink(h_dir, h_path, sym);
  4420. +
  4421. +out:
  4422. + do_delayed_call(&done);
  4423. + return err;
  4424. +}
  4425. +
  4426. +/*
  4427. + * regardless 'acl' option, reset all ACL.
  4428. + * All ACL will be copied up later from the original entry on the lower branch.
  4429. + */
  4430. +static int au_reset_acl(struct path *h_path, umode_t mode)
  4431. +{
  4432. + int err;
  4433. + struct dentry *h_dentry;
  4434. + /* struct inode *h_inode; */
  4435. + struct mnt_idmap *h_idmap;
  4436. +
  4437. + h_idmap = mnt_idmap(h_path->mnt);
  4438. + h_dentry = h_path->dentry;
  4439. + /* h_inode = d_inode(h_dentry); */
  4440. + /* forget_all_cached_acls(h_inode)); */
  4441. + err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4442. + AuTraceErr(err);
  4443. + if (err == -EOPNOTSUPP)
  4444. + err = 0;
  4445. + if (!err)
  4446. + err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
  4447. +
  4448. + AuTraceErr(err);
  4449. + return err;
  4450. +}
  4451. +
  4452. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4453. + struct inode *h_dir, struct path *h_path)
  4454. +{
  4455. + int err;
  4456. + struct inode *dir, *inode;
  4457. + struct mnt_idmap *h_idmap;
  4458. +
  4459. + h_idmap = mnt_idmap(h_path->mnt);
  4460. + err = vfsub_remove_acl(h_idmap, h_path->dentry,
  4461. + XATTR_NAME_POSIX_ACL_DEFAULT);
  4462. + AuTraceErr(err);
  4463. + if (err == -EOPNOTSUPP)
  4464. + err = 0;
  4465. + if (unlikely(err))
  4466. + goto out;
  4467. +
  4468. + /*
  4469. + * strange behaviour from the users view,
  4470. + * particularly setattr case
  4471. + */
  4472. + dir = d_inode(dst_parent);
  4473. + if (au_ibtop(dir) == cpg->bdst)
  4474. + au_cpup_attr_nlink(dir, /*force*/1);
  4475. + inode = d_inode(cpg->dentry);
  4476. + au_cpup_attr_nlink(inode, /*force*/1);
  4477. +
  4478. +out:
  4479. + return err;
  4480. +}
  4481. +
  4482. +static noinline_for_stack
  4483. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4484. + struct au_cpup_reg_attr *h_src_attr)
  4485. +{
  4486. + int err;
  4487. + umode_t mode;
  4488. + unsigned int mnt_flags;
  4489. + unsigned char isdir, isreg, force;
  4490. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4491. + struct au_dtime dt;
  4492. + struct path h_path;
  4493. + struct dentry *h_src, *h_dst, *h_parent;
  4494. + struct inode *h_inode, *h_dir;
  4495. + struct super_block *sb;
  4496. +
  4497. + /* bsrc branch can be ro/rw. */
  4498. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4499. + h_inode = d_inode(h_src);
  4500. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4501. +
  4502. + /* try stopping to be referenced while we are creating */
  4503. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4504. + if (au_ftest_cpup(cpg->flags, RENAME))
  4505. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4506. + AUFS_WH_PFX_LEN));
  4507. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4508. + h_dir = d_inode(h_parent);
  4509. + IMustLock(h_dir);
  4510. + AuDebugOn(h_parent != h_dst->d_parent);
  4511. +
  4512. + sb = cpg->dentry->d_sb;
  4513. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4514. + if (do_dt) {
  4515. + h_path.dentry = h_parent;
  4516. + au_dtime_store(&dt, dst_parent, &h_path);
  4517. + }
  4518. + h_path.dentry = h_dst;
  4519. +
  4520. + isreg = 0;
  4521. + isdir = 0;
  4522. + mode = h_inode->i_mode;
  4523. + switch (mode & S_IFMT) {
  4524. + case S_IFREG:
  4525. + isreg = 1;
  4526. + err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
  4527. + if (!err)
  4528. + err = au_do_cpup_regular(cpg, h_src_attr);
  4529. + break;
  4530. + case S_IFDIR:
  4531. + isdir = 1;
  4532. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4533. + if (!err)
  4534. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4535. + break;
  4536. + case S_IFLNK:
  4537. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4538. + break;
  4539. + case S_IFCHR:
  4540. + case S_IFBLK:
  4541. + AuDebugOn(!capable(CAP_MKNOD));
  4542. + fallthrough;
  4543. + case S_IFIFO:
  4544. + case S_IFSOCK:
  4545. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4546. + break;
  4547. + default:
  4548. + AuIOErr("Unknown inode type 0%o\n", mode);
  4549. + err = -EIO;
  4550. + }
  4551. + if (!err)
  4552. + err = au_reset_acl(&h_path, mode);
  4553. +
  4554. + mnt_flags = au_mntflags(sb);
  4555. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4556. + && !isdir
  4557. + && au_opt_test(mnt_flags, XINO)
  4558. + && (vfsub_inode_nlink(h_inode, AU_I_BRANCH) == 1
  4559. + || (h_inode->i_state & I_LINKABLE))
  4560. + /* todo: unnecessary? */
  4561. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4562. + && cpg->bdst < cpg->bsrc
  4563. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4564. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4565. + /* ignore this error */
  4566. +
  4567. + if (!err) {
  4568. + force = 0;
  4569. + if (isreg) {
  4570. + force = !!cpg->len;
  4571. + if (cpg->len == -1)
  4572. + force = !!i_size_read(h_inode);
  4573. + }
  4574. + au_fhsm_wrote(sb, cpg->bdst, force);
  4575. + }
  4576. +
  4577. + if (do_dt)
  4578. + au_dtime_revert(&dt);
  4579. + return err;
  4580. +}
  4581. +
  4582. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4583. +{
  4584. + int err;
  4585. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4586. + struct path h_ppath;
  4587. + struct inode *h_dir;
  4588. + aufs_bindex_t bdst;
  4589. +
  4590. + dentry = cpg->dentry;
  4591. + bdst = cpg->bdst;
  4592. + h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  4593. + h_dentry = au_h_dptr(dentry, bdst);
  4594. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4595. + dget(h_dentry);
  4596. + au_set_h_dptr(dentry, bdst, NULL);
  4597. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4598. + if (!err)
  4599. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4600. + au_set_h_dptr(dentry, bdst, h_dentry);
  4601. + } else {
  4602. + err = 0;
  4603. + parent = dget_parent(dentry);
  4604. + h_ppath.dentry = au_h_dptr(parent, bdst);
  4605. + dput(parent);
  4606. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
  4607. + if (IS_ERR(h_path->dentry))
  4608. + err = PTR_ERR(h_path->dentry);
  4609. + }
  4610. + if (unlikely(err))
  4611. + goto out;
  4612. +
  4613. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4614. + h_dir = d_inode(h_parent);
  4615. + IMustLock(h_dir);
  4616. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4617. + /* no delegation since it is just created */
  4618. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
  4619. + /*flags*/0);
  4620. + dput(h_path->dentry);
  4621. +
  4622. +out:
  4623. + return err;
  4624. +}
  4625. +
  4626. +/*
  4627. + * copyup the @dentry from @bsrc to @bdst.
  4628. + * the caller must set the both of lower dentries.
  4629. + * @len is for truncating when it is -1 copyup the entire file.
  4630. + * in link/rename cases, @dst_parent may be different from the real one.
  4631. + * basic->bsrc can be larger than basic->bdst.
  4632. + * aufs doesn't touch the credential so
  4633. + * security_inode_copy_up{,_xattr}() are unnecessary.
  4634. + */
  4635. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4636. +{
  4637. + int err, rerr;
  4638. + aufs_bindex_t old_ibtop;
  4639. + unsigned char isdir, plink;
  4640. + struct dentry *h_src, *h_dst, *h_parent;
  4641. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4642. + struct super_block *sb;
  4643. + struct au_branch *br;
  4644. + struct path h_src_path;
  4645. + /* to reduce stack size */
  4646. + struct {
  4647. + struct au_dtime dt;
  4648. + struct path h_path;
  4649. + struct au_cpup_reg_attr h_src_attr;
  4650. + } *a;
  4651. +
  4652. + err = -ENOMEM;
  4653. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4654. + if (unlikely(!a))
  4655. + goto out;
  4656. + a->h_src_attr.valid = 0;
  4657. +
  4658. + sb = cpg->dentry->d_sb;
  4659. + br = au_sbr(sb, cpg->bdst);
  4660. + a->h_path.mnt = au_br_mnt(br);
  4661. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4662. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4663. + h_dir = d_inode(h_parent);
  4664. + IMustLock(h_dir);
  4665. +
  4666. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4667. + inode = d_inode(cpg->dentry);
  4668. +
  4669. + if (!dst_parent)
  4670. + dst_parent = dget_parent(cpg->dentry);
  4671. + else
  4672. + dget(dst_parent);
  4673. +
  4674. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4675. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4676. + if (dst_inode) {
  4677. + if (unlikely(!plink)) {
  4678. + err = -EIO;
  4679. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4680. + "but plink is disabled\n",
  4681. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4682. + goto out_parent;
  4683. + }
  4684. +
  4685. + if (vfsub_inode_nlink(dst_inode, AU_I_BRANCH)) {
  4686. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4687. +
  4688. + h_src = au_plink_lkup(inode, cpg->bdst);
  4689. + err = PTR_ERR(h_src);
  4690. + if (IS_ERR(h_src))
  4691. + goto out_parent;
  4692. + if (unlikely(d_is_negative(h_src))) {
  4693. + err = -EIO;
  4694. + AuIOErr("i%lu exists on b%d "
  4695. + "but not pseudo-linked\n",
  4696. + inode->i_ino, cpg->bdst);
  4697. + dput(h_src);
  4698. + goto out_parent;
  4699. + }
  4700. +
  4701. + if (do_dt) {
  4702. + a->h_path.dentry = h_parent;
  4703. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4704. + }
  4705. +
  4706. + a->h_path.dentry = h_dst;
  4707. + delegated = NULL;
  4708. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4709. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4710. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4711. + if (do_dt)
  4712. + au_dtime_revert(&a->dt);
  4713. + if (unlikely(err == -EWOULDBLOCK)) {
  4714. + pr_warn("cannot retry for NFSv4 delegation"
  4715. + " for an internal link\n");
  4716. + iput(delegated);
  4717. + }
  4718. + dput(h_src);
  4719. + goto out_parent;
  4720. + } else
  4721. + /* todo: cpup_wh_file? */
  4722. + /* udba work */
  4723. + au_update_ibrange(inode, /*do_put_zero*/1);
  4724. + }
  4725. +
  4726. + isdir = S_ISDIR(inode->i_mode);
  4727. + old_ibtop = au_ibtop(inode);
  4728. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4729. + if (unlikely(err))
  4730. + goto out_rev;
  4731. + dst_inode = d_inode(h_dst);
  4732. + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
  4733. + /* todo: necessary? */
  4734. + /* au_pin_hdir_unlock(cpg->pin); */
  4735. +
  4736. + h_src_path.dentry = h_src;
  4737. + h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
  4738. + err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
  4739. + if (unlikely(err)) {
  4740. + /* todo: necessary? */
  4741. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4742. + inode_unlock(dst_inode);
  4743. + goto out_rev;
  4744. + }
  4745. +
  4746. + if (cpg->bdst < old_ibtop) {
  4747. + if (S_ISREG(inode->i_mode)) {
  4748. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4749. + if (unlikely(err)) {
  4750. + /* ignore an error */
  4751. + /* au_pin_hdir_relock(cpg->pin); */
  4752. + inode_unlock(dst_inode);
  4753. + goto out_rev;
  4754. + }
  4755. + }
  4756. + au_set_ibtop(inode, cpg->bdst);
  4757. + } else
  4758. + au_set_ibbot(inode, cpg->bdst);
  4759. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4760. + au_hi_flags(inode, isdir));
  4761. +
  4762. + /* todo: necessary? */
  4763. + /* err = au_pin_hdir_relock(cpg->pin); */
  4764. + inode_unlock(dst_inode);
  4765. + if (unlikely(err))
  4766. + goto out_rev;
  4767. +
  4768. + src_inode = d_inode(h_src);
  4769. + if (!isdir
  4770. + && (vfsub_inode_nlink(src_inode, AU_I_BRANCH) > 1
  4771. + || src_inode->i_state & I_LINKABLE)
  4772. + && plink)
  4773. + au_plink_append(inode, cpg->bdst, h_dst);
  4774. +
  4775. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4776. + a->h_path.dentry = h_dst;
  4777. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4778. + }
  4779. + if (!err)
  4780. + goto out_parent; /* success */
  4781. +
  4782. + /* revert */
  4783. +out_rev:
  4784. + a->h_path.dentry = h_parent;
  4785. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4786. + a->h_path.dentry = h_dst;
  4787. + rerr = 0;
  4788. + if (d_is_positive(h_dst)) {
  4789. + if (!isdir) {
  4790. + /* no delegation since it is just created */
  4791. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4792. + /*delegated*/NULL, /*force*/0);
  4793. + } else
  4794. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4795. + }
  4796. + au_dtime_revert(&a->dt);
  4797. + if (rerr) {
  4798. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4799. + err = -EIO;
  4800. + }
  4801. +out_parent:
  4802. + dput(dst_parent);
  4803. + au_kfree_rcu(a);
  4804. +out:
  4805. + return err;
  4806. +}
  4807. +
  4808. +#if 0 /* reserved */
  4809. +struct au_cpup_single_args {
  4810. + int *errp;
  4811. + struct au_cp_generic *cpg;
  4812. + struct dentry *dst_parent;
  4813. +};
  4814. +
  4815. +static void au_call_cpup_single(void *args)
  4816. +{
  4817. + struct au_cpup_single_args *a = args;
  4818. +
  4819. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4820. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4821. + au_pin_hdir_release(a->cpg->pin);
  4822. +}
  4823. +#endif
  4824. +
  4825. +/*
  4826. + * prevent SIGXFSZ in copy-up.
  4827. + * testing CAP_MKNOD is for generic fs,
  4828. + * but CAP_FSETID is for xfs only, currently.
  4829. + */
  4830. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4831. +{
  4832. + int do_sio;
  4833. + struct super_block *sb;
  4834. + struct inode *h_dir;
  4835. +
  4836. + do_sio = 0;
  4837. + sb = au_pinned_parent(pin)->d_sb;
  4838. + if (!au_wkq_test()
  4839. + && (!au_sbi(sb)->si_plink_maint_pid
  4840. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4841. + switch (mode & S_IFMT) {
  4842. + case S_IFREG:
  4843. + /* no condition about RLIMIT_FSIZE and the file size */
  4844. + do_sio = 1;
  4845. + break;
  4846. + case S_IFCHR:
  4847. + case S_IFBLK:
  4848. + do_sio = !capable(CAP_MKNOD);
  4849. + break;
  4850. + }
  4851. + if (!do_sio)
  4852. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4853. + && !capable(CAP_FSETID));
  4854. + /* this workaround may be removed in the future */
  4855. + if (!do_sio) {
  4856. + h_dir = au_pinned_h_dir(pin);
  4857. + do_sio = h_dir->i_mode & S_ISVTX;
  4858. + }
  4859. + }
  4860. +
  4861. + return do_sio;
  4862. +}
  4863. +
  4864. +#if 0 /* reserved */
  4865. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4866. +{
  4867. + int err, wkq_err;
  4868. + struct dentry *h_dentry;
  4869. +
  4870. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4871. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4872. + err = au_cpup_single(cpg, dst_parent);
  4873. + else {
  4874. + struct au_cpup_single_args args = {
  4875. + .errp = &err,
  4876. + .cpg = cpg,
  4877. + .dst_parent = dst_parent
  4878. + };
  4879. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4880. + if (unlikely(wkq_err))
  4881. + err = wkq_err;
  4882. + }
  4883. +
  4884. + return err;
  4885. +}
  4886. +#endif
  4887. +
  4888. +/*
  4889. + * copyup the @dentry from the first active lower branch to @bdst,
  4890. + * using au_cpup_single().
  4891. + */
  4892. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4893. +{
  4894. + int err;
  4895. + unsigned int flags_orig;
  4896. + struct dentry *dentry;
  4897. +
  4898. + AuDebugOn(cpg->bsrc < 0);
  4899. +
  4900. + dentry = cpg->dentry;
  4901. + DiMustWriteLock(dentry);
  4902. +
  4903. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4904. + if (!err) {
  4905. + flags_orig = cpg->flags;
  4906. + au_fset_cpup(cpg->flags, RENAME);
  4907. + err = au_cpup_single(cpg, NULL);
  4908. + cpg->flags = flags_orig;
  4909. + if (!err)
  4910. + return 0; /* success */
  4911. +
  4912. + /* revert */
  4913. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4914. + au_set_dbtop(dentry, cpg->bsrc);
  4915. + }
  4916. +
  4917. + return err;
  4918. +}
  4919. +
  4920. +struct au_cpup_simple_args {
  4921. + int *errp;
  4922. + struct au_cp_generic *cpg;
  4923. +};
  4924. +
  4925. +static void au_call_cpup_simple(void *args)
  4926. +{
  4927. + struct au_cpup_simple_args *a = args;
  4928. +
  4929. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4930. + *a->errp = au_cpup_simple(a->cpg);
  4931. + au_pin_hdir_release(a->cpg->pin);
  4932. +}
  4933. +
  4934. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4935. +{
  4936. + int err, wkq_err;
  4937. + struct dentry *dentry, *parent;
  4938. + struct file *h_file;
  4939. + struct inode *h_dir;
  4940. + struct mnt_idmap *h_idmap;
  4941. +
  4942. + dentry = cpg->dentry;
  4943. + h_file = NULL;
  4944. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4945. + AuDebugOn(cpg->bsrc < 0);
  4946. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4947. + err = PTR_ERR(h_file);
  4948. + if (IS_ERR(h_file))
  4949. + goto out;
  4950. + }
  4951. +
  4952. + parent = dget_parent(dentry);
  4953. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4954. + h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
  4955. + if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
  4956. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4957. + err = au_cpup_simple(cpg);
  4958. + else {
  4959. + struct au_cpup_simple_args args = {
  4960. + .errp = &err,
  4961. + .cpg = cpg
  4962. + };
  4963. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4964. + if (unlikely(wkq_err))
  4965. + err = wkq_err;
  4966. + }
  4967. +
  4968. + dput(parent);
  4969. + if (h_file)
  4970. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4971. +
  4972. +out:
  4973. + return err;
  4974. +}
  4975. +
  4976. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4977. +{
  4978. + aufs_bindex_t bsrc, bbot;
  4979. + struct dentry *dentry, *h_dentry;
  4980. +
  4981. + if (cpg->bsrc < 0) {
  4982. + dentry = cpg->dentry;
  4983. + bbot = au_dbbot(dentry);
  4984. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4985. + h_dentry = au_h_dptr(dentry, bsrc);
  4986. + if (h_dentry) {
  4987. + AuDebugOn(d_is_negative(h_dentry));
  4988. + break;
  4989. + }
  4990. + }
  4991. + AuDebugOn(bsrc > bbot);
  4992. + cpg->bsrc = bsrc;
  4993. + }
  4994. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4995. + return au_do_sio_cpup_simple(cpg);
  4996. +}
  4997. +
  4998. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4999. +{
  5000. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  5001. + return au_do_sio_cpup_simple(cpg);
  5002. +}
  5003. +
  5004. +/* ---------------------------------------------------------------------- */
  5005. +
  5006. +/*
  5007. + * copyup the deleted file for writing.
  5008. + */
  5009. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  5010. + struct file *file)
  5011. +{
  5012. + int err;
  5013. + unsigned int flags_orig;
  5014. + aufs_bindex_t bsrc_orig;
  5015. + struct au_dinfo *dinfo;
  5016. + struct {
  5017. + struct au_hdentry *hd;
  5018. + struct dentry *h_dentry;
  5019. + } hdst, hsrc;
  5020. +
  5021. + dinfo = au_di(cpg->dentry);
  5022. + AuRwMustWriteLock(&dinfo->di_rwsem);
  5023. +
  5024. + bsrc_orig = cpg->bsrc;
  5025. + cpg->bsrc = dinfo->di_btop;
  5026. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  5027. + hdst.h_dentry = hdst.hd->hd_dentry;
  5028. + hdst.hd->hd_dentry = wh_dentry;
  5029. + dinfo->di_btop = cpg->bdst;
  5030. +
  5031. + hsrc.h_dentry = NULL;
  5032. + if (file) {
  5033. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  5034. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  5035. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  5036. + }
  5037. + flags_orig = cpg->flags;
  5038. + cpg->flags = !AuCpup_DTIME;
  5039. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  5040. + cpg->flags = flags_orig;
  5041. + if (file) {
  5042. + if (!err)
  5043. + err = au_reopen_nondir(file);
  5044. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  5045. + }
  5046. + hdst.hd->hd_dentry = hdst.h_dentry;
  5047. + dinfo->di_btop = cpg->bsrc;
  5048. + cpg->bsrc = bsrc_orig;
  5049. +
  5050. + return err;
  5051. +}
  5052. +
  5053. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5054. +{
  5055. + int err;
  5056. + aufs_bindex_t bdst;
  5057. + struct au_dtime dt;
  5058. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  5059. + struct au_branch *br;
  5060. + struct path h_path;
  5061. +
  5062. + dentry = cpg->dentry;
  5063. + bdst = cpg->bdst;
  5064. + br = au_sbr(dentry->d_sb, bdst);
  5065. + parent = dget_parent(dentry);
  5066. + h_parent = au_h_dptr(parent, bdst);
  5067. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  5068. + err = PTR_ERR(wh_dentry);
  5069. + if (IS_ERR(wh_dentry))
  5070. + goto out;
  5071. +
  5072. + h_path.dentry = h_parent;
  5073. + h_path.mnt = au_br_mnt(br);
  5074. + au_dtime_store(&dt, parent, &h_path);
  5075. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  5076. + if (unlikely(err))
  5077. + goto out_wh;
  5078. +
  5079. + dget(wh_dentry);
  5080. + h_path.dentry = wh_dentry;
  5081. + if (!d_is_dir(wh_dentry)) {
  5082. + /* no delegation since it is just created */
  5083. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  5084. + /*delegated*/NULL, /*force*/0);
  5085. + } else
  5086. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  5087. + if (unlikely(err)) {
  5088. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  5089. + wh_dentry, err);
  5090. + err = -EIO;
  5091. + }
  5092. + au_dtime_revert(&dt);
  5093. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  5094. +
  5095. +out_wh:
  5096. + dput(wh_dentry);
  5097. +out:
  5098. + dput(parent);
  5099. + return err;
  5100. +}
  5101. +
  5102. +struct au_cpup_wh_args {
  5103. + int *errp;
  5104. + struct au_cp_generic *cpg;
  5105. + struct file *file;
  5106. +};
  5107. +
  5108. +static void au_call_cpup_wh(void *args)
  5109. +{
  5110. + struct au_cpup_wh_args *a = args;
  5111. +
  5112. + au_pin_hdir_acquire_nest(a->cpg->pin);
  5113. + *a->errp = au_cpup_wh(a->cpg, a->file);
  5114. + au_pin_hdir_release(a->cpg->pin);
  5115. +}
  5116. +
  5117. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5118. +{
  5119. + int err, wkq_err;
  5120. + aufs_bindex_t bdst;
  5121. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  5122. + struct inode *dir, *h_dir, *h_tmpdir;
  5123. + struct au_wbr *wbr;
  5124. + struct au_pin wh_pin, *pin_orig;
  5125. + struct mnt_idmap *h_idmap;
  5126. +
  5127. + dentry = cpg->dentry;
  5128. + bdst = cpg->bdst;
  5129. + parent = dget_parent(dentry);
  5130. + dir = d_inode(parent);
  5131. + h_orph = NULL;
  5132. + h_parent = NULL;
  5133. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  5134. + h_tmpdir = h_dir;
  5135. + pin_orig = NULL;
  5136. + if (!vfsub_inode_nlink(h_dir, AU_I_BRANCH)) {
  5137. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  5138. + h_orph = wbr->wbr_orph;
  5139. +
  5140. + h_parent = dget(au_h_dptr(parent, bdst));
  5141. + au_set_h_dptr(parent, bdst, dget(h_orph));
  5142. + h_tmpdir = d_inode(h_orph);
  5143. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  5144. +
  5145. + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
  5146. + /* todo: au_h_open_pre()? */
  5147. +
  5148. + pin_orig = cpg->pin;
  5149. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  5150. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  5151. + cpg->pin = &wh_pin;
  5152. + }
  5153. +
  5154. + h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
  5155. + if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
  5156. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  5157. + err = au_cpup_wh(cpg, file);
  5158. + else {
  5159. + struct au_cpup_wh_args args = {
  5160. + .errp = &err,
  5161. + .cpg = cpg,
  5162. + .file = file
  5163. + };
  5164. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  5165. + if (unlikely(wkq_err))
  5166. + err = wkq_err;
  5167. + }
  5168. +
  5169. + if (h_orph) {
  5170. + inode_unlock(h_tmpdir);
  5171. + /* todo: au_h_open_post()? */
  5172. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  5173. + au_set_h_dptr(parent, bdst, h_parent);
  5174. + AuDebugOn(!pin_orig);
  5175. + cpg->pin = pin_orig;
  5176. + }
  5177. + iput(h_dir);
  5178. + dput(parent);
  5179. +
  5180. + return err;
  5181. +}
  5182. +
  5183. +/* ---------------------------------------------------------------------- */
  5184. +
  5185. +/*
  5186. + * generic routine for both of copy-up and copy-down.
  5187. + */
  5188. +/* cf. revalidate function in file.c */
  5189. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5190. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5191. + struct au_pin *pin,
  5192. + struct dentry *h_parent, void *arg),
  5193. + void *arg)
  5194. +{
  5195. + int err;
  5196. + struct au_pin pin;
  5197. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  5198. +
  5199. + err = 0;
  5200. + parent = dget_parent(dentry);
  5201. + if (IS_ROOT(parent))
  5202. + goto out;
  5203. +
  5204. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  5205. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  5206. +
  5207. + /* do not use au_dpage */
  5208. + real_parent = parent;
  5209. + while (1) {
  5210. + dput(parent);
  5211. + parent = dget_parent(dentry);
  5212. + h_parent = au_h_dptr(parent, bdst);
  5213. + if (h_parent)
  5214. + goto out; /* success */
  5215. +
  5216. + /* find top dir which is necessary to cpup */
  5217. + do {
  5218. + d = parent;
  5219. + dput(parent);
  5220. + parent = dget_parent(d);
  5221. + di_read_lock_parent3(parent, !AuLock_IR);
  5222. + h_parent = au_h_dptr(parent, bdst);
  5223. + di_read_unlock(parent, !AuLock_IR);
  5224. + } while (!h_parent);
  5225. +
  5226. + if (d != real_parent)
  5227. + di_write_lock_child3(d);
  5228. +
  5229. + /* somebody else might create while we were sleeping */
  5230. + h_dentry = au_h_dptr(d, bdst);
  5231. + if (!h_dentry || d_is_negative(h_dentry)) {
  5232. + if (h_dentry)
  5233. + au_update_dbtop(d);
  5234. +
  5235. + au_pin_set_dentry(&pin, d);
  5236. + err = au_do_pin(&pin);
  5237. + if (!err) {
  5238. + err = cp(d, bdst, &pin, h_parent, arg);
  5239. + au_unpin(&pin);
  5240. + }
  5241. + }
  5242. +
  5243. + if (d != real_parent)
  5244. + di_write_unlock(d);
  5245. + if (unlikely(err))
  5246. + break;
  5247. + }
  5248. +
  5249. +out:
  5250. + dput(parent);
  5251. + return err;
  5252. +}
  5253. +
  5254. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  5255. + struct au_pin *pin,
  5256. + struct dentry *h_parent __maybe_unused,
  5257. + void *arg __maybe_unused)
  5258. +{
  5259. + struct au_cp_generic cpg = {
  5260. + .dentry = dentry,
  5261. + .bdst = bdst,
  5262. + .bsrc = -1,
  5263. + .len = 0,
  5264. + .pin = pin,
  5265. + .flags = AuCpup_DTIME
  5266. + };
  5267. + return au_sio_cpup_simple(&cpg);
  5268. +}
  5269. +
  5270. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5271. +{
  5272. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  5273. +}
  5274. +
  5275. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5276. +{
  5277. + int err;
  5278. + struct dentry *parent;
  5279. + struct inode *dir;
  5280. +
  5281. + parent = dget_parent(dentry);
  5282. + dir = d_inode(parent);
  5283. + err = 0;
  5284. + if (au_h_iptr(dir, bdst))
  5285. + goto out;
  5286. +
  5287. + di_read_unlock(parent, AuLock_IR);
  5288. + di_write_lock_parent(parent);
  5289. + /* someone else might change our inode while we were sleeping */
  5290. + if (!au_h_iptr(dir, bdst))
  5291. + err = au_cpup_dirs(dentry, bdst);
  5292. + di_downgrade_lock(parent, AuLock_IR);
  5293. +
  5294. +out:
  5295. + dput(parent);
  5296. + return err;
  5297. +}
  5298. diff -Naur null/fs/aufs/cpup.h linux-6.6.63/fs/aufs/cpup.h
  5299. --- /dev/null
  5300. +++ linux-6.6.63/fs/aufs/cpup.h 2024-12-18 15:12:59.293519613 +0300
  5301. @@ -0,0 +1,100 @@
  5302. +/* SPDX-License-Identifier: GPL-2.0 */
  5303. +/*
  5304. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  5305. + *
  5306. + * This program is free software; you can redistribute it and/or modify
  5307. + * it under the terms of the GNU General Public License as published by
  5308. + * the Free Software Foundation; either version 2 of the License, or
  5309. + * (at your option) any later version.
  5310. + *
  5311. + * This program is distributed in the hope that it will be useful,
  5312. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5313. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5314. + * GNU General Public License for more details.
  5315. + *
  5316. + * You should have received a copy of the GNU General Public License
  5317. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5318. + */
  5319. +
  5320. +/*
  5321. + * copy-up/down functions
  5322. + */
  5323. +
  5324. +#ifndef __AUFS_CPUP_H__
  5325. +#define __AUFS_CPUP_H__
  5326. +
  5327. +#ifdef __KERNEL__
  5328. +
  5329. +#include <linux/path.h>
  5330. +
  5331. +struct inode;
  5332. +struct file;
  5333. +struct au_pin;
  5334. +
  5335. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  5336. +void au_cpup_attr_timesizes(struct inode *inode);
  5337. +void au_cpup_attr_nlink(struct inode *inode, int force);
  5338. +void au_cpup_attr_changeable(struct inode *inode);
  5339. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  5340. +void au_cpup_attr_all(struct inode *inode, int force);
  5341. +
  5342. +/* ---------------------------------------------------------------------- */
  5343. +
  5344. +struct au_cp_generic {
  5345. + struct dentry *dentry;
  5346. + aufs_bindex_t bdst, bsrc;
  5347. + loff_t len;
  5348. + struct au_pin *pin;
  5349. + unsigned int flags;
  5350. +};
  5351. +
  5352. +/* cpup flags */
  5353. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  5354. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  5355. + for link(2) */
  5356. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5357. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5358. + cpup */
  5359. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5360. + existing entry */
  5361. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5362. + the branch is marked as RO */
  5363. +
  5364. +#ifndef CONFIG_AUFS_BR_HFSPLUS
  5365. +#undef AuCpup_HOPEN
  5366. +#define AuCpup_HOPEN 0
  5367. +#endif
  5368. +
  5369. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5370. +#define au_fset_cpup(flags, name) \
  5371. + do { (flags) |= AuCpup_##name; } while (0)
  5372. +#define au_fclr_cpup(flags, name) \
  5373. + do { (flags) &= ~AuCpup_##name; } while (0)
  5374. +
  5375. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5376. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5377. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5378. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5379. +
  5380. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5381. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5382. + struct au_pin *pin,
  5383. + struct dentry *h_parent, void *arg),
  5384. + void *arg);
  5385. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5386. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5387. +
  5388. +/* ---------------------------------------------------------------------- */
  5389. +
  5390. +/* keep timestamps when copyup */
  5391. +struct au_dtime {
  5392. + struct dentry *dt_dentry;
  5393. + struct path dt_h_path;
  5394. + struct timespec64 dt_atime, dt_mtime;
  5395. +};
  5396. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5397. + struct path *h_path);
  5398. +void au_dtime_revert(struct au_dtime *dt);
  5399. +
  5400. +#endif /* __KERNEL__ */
  5401. +#endif /* __AUFS_CPUP_H__ */
  5402. diff -Naur null/fs/aufs/dbgaufs.c linux-6.6.63/fs/aufs/dbgaufs.c
  5403. --- /dev/null
  5404. +++ linux-6.6.63/fs/aufs/dbgaufs.c 2024-12-18 15:12:59.293519613 +0300
  5405. @@ -0,0 +1,526 @@
  5406. +// SPDX-License-Identifier: GPL-2.0
  5407. +/*
  5408. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  5409. + *
  5410. + * This program is free software; you can redistribute it and/or modify
  5411. + * it under the terms of the GNU General Public License as published by
  5412. + * the Free Software Foundation; either version 2 of the License, or
  5413. + * (at your option) any later version.
  5414. + *
  5415. + * This program is distributed in the hope that it will be useful,
  5416. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5417. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5418. + * GNU General Public License for more details.
  5419. + *
  5420. + * You should have received a copy of the GNU General Public License
  5421. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5422. + */
  5423. +
  5424. +/*
  5425. + * debugfs interface
  5426. + */
  5427. +
  5428. +#include <linux/debugfs.h>
  5429. +#include "aufs.h"
  5430. +
  5431. +#ifndef CONFIG_SYSFS
  5432. +#error DEBUG_FS depends upon SYSFS
  5433. +#endif
  5434. +
  5435. +static struct dentry *dbgaufs;
  5436. +static const mode_t dbgaufs_mode = 0444;
  5437. +
  5438. +/* 20 is max digits length of ulong 64 */
  5439. +struct dbgaufs_arg {
  5440. + int n;
  5441. + char a[20 * 4];
  5442. +};
  5443. +
  5444. +/*
  5445. + * common function for all XINO files
  5446. + */
  5447. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5448. + struct file *file)
  5449. +{
  5450. + void *p;
  5451. +
  5452. + p = file->private_data;
  5453. + if (p) {
  5454. + /* this is struct dbgaufs_arg */
  5455. + AuDebugOn(!au_kfree_sz_test(p));
  5456. + au_kfree_do_rcu(p);
  5457. + }
  5458. + return 0;
  5459. +}
  5460. +
  5461. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
  5462. + int cnt)
  5463. +{
  5464. + int err;
  5465. + struct kstat st;
  5466. + struct dbgaufs_arg *p;
  5467. +
  5468. + err = -ENOMEM;
  5469. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5470. + if (unlikely(!p))
  5471. + goto out;
  5472. +
  5473. + err = 0;
  5474. + p->n = 0;
  5475. + file->private_data = p;
  5476. + if (!xf)
  5477. + goto out;
  5478. +
  5479. + err = vfsub_getattr(&xf->f_path, &st);
  5480. + if (!err) {
  5481. + if (do_fcnt)
  5482. + p->n = snprintf
  5483. + (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
  5484. + cnt, st.blocks, st.blksize,
  5485. + (long long)st.size);
  5486. + else
  5487. + p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
  5488. + st.blocks, st.blksize,
  5489. + (long long)st.size);
  5490. + AuDebugOn(p->n >= sizeof(p->a));
  5491. + } else {
  5492. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5493. + err = 0;
  5494. + }
  5495. +
  5496. +out:
  5497. + return err;
  5498. +}
  5499. +
  5500. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5501. + size_t count, loff_t *ppos)
  5502. +{
  5503. + struct dbgaufs_arg *p;
  5504. +
  5505. + p = file->private_data;
  5506. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5507. +}
  5508. +
  5509. +/* ---------------------------------------------------------------------- */
  5510. +
  5511. +struct dbgaufs_plink_arg {
  5512. + int n;
  5513. + char a[];
  5514. +};
  5515. +
  5516. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5517. + struct file *file)
  5518. +{
  5519. + free_page((unsigned long)file->private_data);
  5520. + return 0;
  5521. +}
  5522. +
  5523. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5524. +{
  5525. + int err, i, limit;
  5526. + unsigned long n, sum;
  5527. + struct dbgaufs_plink_arg *p;
  5528. + struct au_sbinfo *sbinfo;
  5529. + struct super_block *sb;
  5530. + struct hlist_bl_head *hbl;
  5531. +
  5532. + err = -ENOMEM;
  5533. + p = (void *)get_zeroed_page(GFP_NOFS);
  5534. + if (unlikely(!p))
  5535. + goto out;
  5536. +
  5537. + err = -EFBIG;
  5538. + sbinfo = inode->i_private;
  5539. + sb = sbinfo->si_sb;
  5540. + si_noflush_read_lock(sb);
  5541. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5542. + limit = PAGE_SIZE - sizeof(p->n);
  5543. +
  5544. + /* the number of buckets */
  5545. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5546. + p->n += n;
  5547. + limit -= n;
  5548. +
  5549. + sum = 0;
  5550. + for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
  5551. + i++, hbl++) {
  5552. + n = au_hbl_count(hbl);
  5553. + sum += n;
  5554. +
  5555. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5556. + p->n += n;
  5557. + limit -= n;
  5558. + if (unlikely(limit <= 0))
  5559. + goto out_free;
  5560. + }
  5561. + p->a[p->n - 1] = '\n';
  5562. +
  5563. + /* the sum of plinks */
  5564. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5565. + p->n += n;
  5566. + limit -= n;
  5567. + if (unlikely(limit <= 0))
  5568. + goto out_free;
  5569. + } else {
  5570. +#define str "1\n0\n0\n"
  5571. + p->n = sizeof(str) - 1;
  5572. + strscpy(p->a, str, sizeof(str));
  5573. +#undef str
  5574. + }
  5575. + si_read_unlock(sb);
  5576. +
  5577. + err = 0;
  5578. + file->private_data = p;
  5579. + goto out; /* success */
  5580. +
  5581. +out_free:
  5582. + free_page((unsigned long)p);
  5583. +out:
  5584. + return err;
  5585. +}
  5586. +
  5587. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5588. + size_t count, loff_t *ppos)
  5589. +{
  5590. + struct dbgaufs_plink_arg *p;
  5591. +
  5592. + p = file->private_data;
  5593. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5594. +}
  5595. +
  5596. +static const struct file_operations dbgaufs_plink_fop = {
  5597. + .owner = THIS_MODULE,
  5598. + .open = dbgaufs_plink_open,
  5599. + .release = dbgaufs_plink_release,
  5600. + .read = dbgaufs_plink_read
  5601. +};
  5602. +
  5603. +/* ---------------------------------------------------------------------- */
  5604. +
  5605. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5606. +{
  5607. + int err;
  5608. + struct au_sbinfo *sbinfo;
  5609. + struct super_block *sb;
  5610. +
  5611. + sbinfo = inode->i_private;
  5612. + sb = sbinfo->si_sb;
  5613. + si_noflush_read_lock(sb);
  5614. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
  5615. + si_read_unlock(sb);
  5616. + return err;
  5617. +}
  5618. +
  5619. +static const struct file_operations dbgaufs_xib_fop = {
  5620. + .owner = THIS_MODULE,
  5621. + .open = dbgaufs_xib_open,
  5622. + .release = dbgaufs_xi_release,
  5623. + .read = dbgaufs_xi_read
  5624. +};
  5625. +
  5626. +/* ---------------------------------------------------------------------- */
  5627. +
  5628. +#define DbgaufsXi_PREFIX "xi"
  5629. +
  5630. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5631. +{
  5632. + int err, idx;
  5633. + long l;
  5634. + aufs_bindex_t bindex;
  5635. + char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
  5636. + struct au_sbinfo *sbinfo;
  5637. + struct super_block *sb;
  5638. + struct au_xino *xi;
  5639. + struct file *xf;
  5640. + struct qstr *name;
  5641. + struct au_branch *br;
  5642. +
  5643. + err = -ENOENT;
  5644. + name = &file->f_path.dentry->d_name;
  5645. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5646. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5647. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5648. + goto out;
  5649. +
  5650. + AuDebugOn(name->len >= sizeof(a));
  5651. + memcpy(a, name->name, name->len);
  5652. + a[name->len] = '\0';
  5653. + p = strchr(a, '-');
  5654. + if (p)
  5655. + *p = '\0';
  5656. + err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5657. + if (unlikely(err))
  5658. + goto out;
  5659. + bindex = l;
  5660. + idx = 0;
  5661. + if (p) {
  5662. + err = kstrtol(p + 1, 10, &l);
  5663. + if (unlikely(err))
  5664. + goto out;
  5665. + idx = l;
  5666. + }
  5667. +
  5668. + err = -ENOENT;
  5669. + sbinfo = inode->i_private;
  5670. + sb = sbinfo->si_sb;
  5671. + si_noflush_read_lock(sb);
  5672. + if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
  5673. + goto out_si;
  5674. + br = au_sbr(sb, bindex);
  5675. + xi = br->br_xino;
  5676. + if (unlikely(idx >= xi->xi_nfile))
  5677. + goto out_si;
  5678. + xf = au_xino_file(xi, idx);
  5679. + if (xf)
  5680. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
  5681. + au_xino_count(br));
  5682. +
  5683. +out_si:
  5684. + si_read_unlock(sb);
  5685. +out:
  5686. + AuTraceErr(err);
  5687. + return err;
  5688. +}
  5689. +
  5690. +static const struct file_operations dbgaufs_xino_fop = {
  5691. + .owner = THIS_MODULE,
  5692. + .open = dbgaufs_xino_open,
  5693. + .release = dbgaufs_xi_release,
  5694. + .read = dbgaufs_xi_read
  5695. +};
  5696. +
  5697. +void dbgaufs_xino_del(struct au_branch *br)
  5698. +{
  5699. + struct dentry *dbgaufs;
  5700. +
  5701. + dbgaufs = br->br_dbgaufs;
  5702. + if (!dbgaufs)
  5703. + return;
  5704. +
  5705. + br->br_dbgaufs = NULL;
  5706. + /* debugfs acquires the parent i_mutex */
  5707. + lockdep_off();
  5708. + debugfs_remove(dbgaufs);
  5709. + lockdep_on();
  5710. +}
  5711. +
  5712. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5713. +{
  5714. + aufs_bindex_t bbot;
  5715. + struct au_branch *br;
  5716. +
  5717. + if (!au_sbi(sb)->si_dbgaufs)
  5718. + return;
  5719. +
  5720. + bbot = au_sbbot(sb);
  5721. + for (; bindex <= bbot; bindex++) {
  5722. + br = au_sbr(sb, bindex);
  5723. + dbgaufs_xino_del(br);
  5724. + }
  5725. +}
  5726. +
  5727. +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
  5728. + unsigned int idx, struct dentry *parent,
  5729. + struct au_sbinfo *sbinfo)
  5730. +{
  5731. + struct au_branch *br;
  5732. + struct dentry *d;
  5733. + /* "xi" bindex(5) "-" idx(2) NULL */
  5734. + char name[sizeof(DbgaufsXi_PREFIX) + 8];
  5735. +
  5736. + if (!idx)
  5737. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5738. + else
  5739. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
  5740. + bindex, idx);
  5741. + br = au_sbr(sb, bindex);
  5742. + if (br->br_dbgaufs) {
  5743. + struct qstr qstr = QSTR_INIT(name, strlen(name));
  5744. +
  5745. + if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
  5746. + /* debugfs acquires the parent i_mutex */
  5747. + lockdep_off();
  5748. + d = debugfs_rename(parent, br->br_dbgaufs, parent,
  5749. + name);
  5750. + lockdep_on();
  5751. + if (unlikely(!d))
  5752. + pr_warn("failed renaming %pd/%s, ignored.\n",
  5753. + parent, name);
  5754. + }
  5755. + } else {
  5756. + lockdep_off();
  5757. + br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5758. + sbinfo, &dbgaufs_xino_fop);
  5759. + lockdep_on();
  5760. + if (unlikely(!br->br_dbgaufs))
  5761. + pr_warn("failed creating %pd/%s, ignored.\n",
  5762. + parent, name);
  5763. + }
  5764. +}
  5765. +
  5766. +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
  5767. + struct dentry *parent, struct au_sbinfo *sbinfo)
  5768. +{
  5769. + struct au_branch *br;
  5770. + struct au_xino *xi;
  5771. + unsigned int u;
  5772. +
  5773. + br = au_sbr(sb, bindex);
  5774. + xi = br->br_xino;
  5775. + for (u = 0; u < xi->xi_nfile; u++)
  5776. + dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
  5777. +}
  5778. +
  5779. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
  5780. +{
  5781. + struct au_sbinfo *sbinfo;
  5782. + struct dentry *parent;
  5783. + aufs_bindex_t bbot;
  5784. +
  5785. + if (!au_opt_test(au_mntflags(sb), XINO))
  5786. + return;
  5787. +
  5788. + sbinfo = au_sbi(sb);
  5789. + parent = sbinfo->si_dbgaufs;
  5790. + if (!parent)
  5791. + return;
  5792. +
  5793. + bbot = au_sbbot(sb);
  5794. + if (topdown)
  5795. + for (; bindex <= bbot; bindex++)
  5796. + dbgaufs_br_add(sb, bindex, parent, sbinfo);
  5797. + else
  5798. + for (; bbot >= bindex; bbot--)
  5799. + dbgaufs_br_add(sb, bbot, parent, sbinfo);
  5800. +}
  5801. +
  5802. +/* ---------------------------------------------------------------------- */
  5803. +
  5804. +#ifdef CONFIG_AUFS_EXPORT
  5805. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5806. +{
  5807. + int err;
  5808. + struct au_sbinfo *sbinfo;
  5809. + struct super_block *sb;
  5810. +
  5811. + sbinfo = inode->i_private;
  5812. + sb = sbinfo->si_sb;
  5813. + si_noflush_read_lock(sb);
  5814. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
  5815. + si_read_unlock(sb);
  5816. + return err;
  5817. +}
  5818. +
  5819. +static const struct file_operations dbgaufs_xigen_fop = {
  5820. + .owner = THIS_MODULE,
  5821. + .open = dbgaufs_xigen_open,
  5822. + .release = dbgaufs_xi_release,
  5823. + .read = dbgaufs_xi_read
  5824. +};
  5825. +
  5826. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5827. +{
  5828. + int err;
  5829. +
  5830. + /*
  5831. + * This function is a dynamic '__init' function actually,
  5832. + * so the tiny check for si_rwsem is unnecessary.
  5833. + */
  5834. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5835. +
  5836. + err = -EIO;
  5837. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5838. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5839. + &dbgaufs_xigen_fop);
  5840. + if (sbinfo->si_dbgaufs_xigen)
  5841. + err = 0;
  5842. +
  5843. + return err;
  5844. +}
  5845. +#else
  5846. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5847. +{
  5848. + return 0;
  5849. +}
  5850. +#endif /* CONFIG_AUFS_EXPORT */
  5851. +
  5852. +/* ---------------------------------------------------------------------- */
  5853. +
  5854. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5855. +{
  5856. + /*
  5857. + * This function is a dynamic '__fin' function actually,
  5858. + * so the tiny check for si_rwsem is unnecessary.
  5859. + */
  5860. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5861. +
  5862. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5863. + sbinfo->si_dbgaufs = NULL;
  5864. +}
  5865. +
  5866. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5867. +{
  5868. + int err;
  5869. + char name[SysaufsSiNameLen];
  5870. +
  5871. + /*
  5872. + * This function is a dynamic '__init' function actually,
  5873. + * so the tiny check for si_rwsem is unnecessary.
  5874. + */
  5875. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5876. +
  5877. + err = -ENOENT;
  5878. + if (!dbgaufs) {
  5879. + AuErr1("/debug/aufs is uninitialized\n");
  5880. + goto out;
  5881. + }
  5882. +
  5883. + err = -EIO;
  5884. + sysaufs_name(sbinfo, name);
  5885. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5886. + if (unlikely(!sbinfo->si_dbgaufs))
  5887. + goto out;
  5888. +
  5889. + /* regardless plink/noplink option */
  5890. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5891. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5892. + &dbgaufs_plink_fop);
  5893. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5894. + goto out_dir;
  5895. +
  5896. + /* regardless xino/noxino option */
  5897. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5898. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5899. + &dbgaufs_xib_fop);
  5900. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5901. + goto out_dir;
  5902. +
  5903. + err = dbgaufs_xigen_init(sbinfo);
  5904. + if (!err)
  5905. + goto out; /* success */
  5906. +
  5907. +out_dir:
  5908. + dbgaufs_si_fin(sbinfo);
  5909. +out:
  5910. + if (unlikely(err))
  5911. + pr_err("debugfs/aufs failed\n");
  5912. + return err;
  5913. +}
  5914. +
  5915. +/* ---------------------------------------------------------------------- */
  5916. +
  5917. +void dbgaufs_fin(void)
  5918. +{
  5919. + debugfs_remove(dbgaufs);
  5920. +}
  5921. +
  5922. +int __init dbgaufs_init(void)
  5923. +{
  5924. + int err;
  5925. +
  5926. + err = -EIO;
  5927. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5928. + if (dbgaufs)
  5929. + err = 0;
  5930. + return err;
  5931. +}
  5932. diff -Naur null/fs/aufs/dbgaufs.h linux-6.6.63/fs/aufs/dbgaufs.h
  5933. --- /dev/null
  5934. +++ linux-6.6.63/fs/aufs/dbgaufs.h 2024-12-18 15:12:59.294519601 +0300
  5935. @@ -0,0 +1,53 @@
  5936. +/* SPDX-License-Identifier: GPL-2.0 */
  5937. +/*
  5938. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  5939. + *
  5940. + * This program is free software; you can redistribute it and/or modify
  5941. + * it under the terms of the GNU General Public License as published by
  5942. + * the Free Software Foundation; either version 2 of the License, or
  5943. + * (at your option) any later version.
  5944. + *
  5945. + * This program is distributed in the hope that it will be useful,
  5946. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5947. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5948. + * GNU General Public License for more details.
  5949. + *
  5950. + * You should have received a copy of the GNU General Public License
  5951. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5952. + */
  5953. +
  5954. +/*
  5955. + * debugfs interface
  5956. + */
  5957. +
  5958. +#ifndef __DBGAUFS_H__
  5959. +#define __DBGAUFS_H__
  5960. +
  5961. +#ifdef __KERNEL__
  5962. +
  5963. +struct super_block;
  5964. +struct au_sbinfo;
  5965. +struct au_branch;
  5966. +
  5967. +#ifdef CONFIG_DEBUG_FS
  5968. +/* dbgaufs.c */
  5969. +void dbgaufs_xino_del(struct au_branch *br);
  5970. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5971. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
  5972. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5973. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5974. +void dbgaufs_fin(void);
  5975. +int __init dbgaufs_init(void);
  5976. +#else
  5977. +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
  5978. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5979. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
  5980. + int topdown)
  5981. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5982. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5983. +AuStubVoid(dbgaufs_fin, void)
  5984. +AuStubInt0(__init dbgaufs_init, void)
  5985. +#endif /* CONFIG_DEBUG_FS */
  5986. +
  5987. +#endif /* __KERNEL__ */
  5988. +#endif /* __DBGAUFS_H__ */
  5989. diff -Naur null/fs/aufs/dcsub.c linux-6.6.63/fs/aufs/dcsub.c
  5990. --- /dev/null
  5991. +++ linux-6.6.63/fs/aufs/dcsub.c 2024-12-18 15:12:59.294519601 +0300
  5992. @@ -0,0 +1,225 @@
  5993. +// SPDX-License-Identifier: GPL-2.0
  5994. +/*
  5995. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  5996. + *
  5997. + * This program is free software; you can redistribute it and/or modify
  5998. + * it under the terms of the GNU General Public License as published by
  5999. + * the Free Software Foundation; either version 2 of the License, or
  6000. + * (at your option) any later version.
  6001. + *
  6002. + * This program is distributed in the hope that it will be useful,
  6003. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6004. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6005. + * GNU General Public License for more details.
  6006. + *
  6007. + * You should have received a copy of the GNU General Public License
  6008. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6009. + */
  6010. +
  6011. +/*
  6012. + * sub-routines for dentry cache
  6013. + */
  6014. +
  6015. +#include "aufs.h"
  6016. +
  6017. +static void au_dpage_free(struct au_dpage *dpage)
  6018. +{
  6019. + int i;
  6020. + struct dentry **p;
  6021. +
  6022. + p = dpage->dentries;
  6023. + for (i = 0; i < dpage->ndentry; i++)
  6024. + dput(*p++);
  6025. + free_page((unsigned long)dpage->dentries);
  6026. +}
  6027. +
  6028. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  6029. +{
  6030. + int err;
  6031. + void *p;
  6032. +
  6033. + err = -ENOMEM;
  6034. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  6035. + if (unlikely(!dpages->dpages))
  6036. + goto out;
  6037. +
  6038. + p = (void *)__get_free_page(gfp);
  6039. + if (unlikely(!p))
  6040. + goto out_dpages;
  6041. +
  6042. + dpages->dpages[0].ndentry = 0;
  6043. + dpages->dpages[0].dentries = p;
  6044. + dpages->ndpage = 1;
  6045. + return 0; /* success */
  6046. +
  6047. +out_dpages:
  6048. + au_kfree_try_rcu(dpages->dpages);
  6049. +out:
  6050. + return err;
  6051. +}
  6052. +
  6053. +void au_dpages_free(struct au_dcsub_pages *dpages)
  6054. +{
  6055. + int i;
  6056. + struct au_dpage *p;
  6057. +
  6058. + p = dpages->dpages;
  6059. + for (i = 0; i < dpages->ndpage; i++)
  6060. + au_dpage_free(p++);
  6061. + au_kfree_try_rcu(dpages->dpages);
  6062. +}
  6063. +
  6064. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  6065. + struct dentry *dentry, gfp_t gfp)
  6066. +{
  6067. + int err, sz;
  6068. + struct au_dpage *dpage;
  6069. + void *p;
  6070. +
  6071. + dpage = dpages->dpages + dpages->ndpage - 1;
  6072. + sz = PAGE_SIZE / sizeof(dentry);
  6073. + if (unlikely(dpage->ndentry >= sz)) {
  6074. + AuLabel(new dpage);
  6075. + err = -ENOMEM;
  6076. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  6077. + p = au_kzrealloc(dpages->dpages, sz,
  6078. + sz + sizeof(*dpages->dpages), gfp,
  6079. + /*may_shrink*/0);
  6080. + if (unlikely(!p))
  6081. + goto out;
  6082. +
  6083. + dpages->dpages = p;
  6084. + dpage = dpages->dpages + dpages->ndpage;
  6085. + p = (void *)__get_free_page(gfp);
  6086. + if (unlikely(!p))
  6087. + goto out;
  6088. +
  6089. + dpage->ndentry = 0;
  6090. + dpage->dentries = p;
  6091. + dpages->ndpage++;
  6092. + }
  6093. +
  6094. + AuDebugOn(au_dcount(dentry) <= 0);
  6095. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  6096. + return 0; /* success */
  6097. +
  6098. +out:
  6099. + return err;
  6100. +}
  6101. +
  6102. +/* todo: BAD approach */
  6103. +/* copied from linux/fs/dcache.c */
  6104. +enum d_walk_ret {
  6105. + D_WALK_CONTINUE,
  6106. + D_WALK_QUIT,
  6107. + D_WALK_NORETRY,
  6108. + D_WALK_SKIP,
  6109. +};
  6110. +
  6111. +extern void d_walk(struct dentry *parent, void *data,
  6112. + enum d_walk_ret (*enter)(void *, struct dentry *));
  6113. +
  6114. +struct ac_dpages_arg {
  6115. + int err;
  6116. + struct au_dcsub_pages *dpages;
  6117. + struct super_block *sb;
  6118. + au_dpages_test test;
  6119. + void *arg;
  6120. +};
  6121. +
  6122. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  6123. +{
  6124. + enum d_walk_ret ret;
  6125. + struct ac_dpages_arg *arg = _arg;
  6126. +
  6127. + ret = D_WALK_CONTINUE;
  6128. + if (dentry->d_sb == arg->sb
  6129. + && !IS_ROOT(dentry)
  6130. + && au_dcount(dentry) > 0
  6131. + && au_di(dentry)
  6132. + && (!arg->test || arg->test(dentry, arg->arg))) {
  6133. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  6134. + if (unlikely(arg->err))
  6135. + ret = D_WALK_QUIT;
  6136. + }
  6137. +
  6138. + return ret;
  6139. +}
  6140. +
  6141. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6142. + au_dpages_test test, void *arg)
  6143. +{
  6144. + struct ac_dpages_arg args = {
  6145. + .err = 0,
  6146. + .dpages = dpages,
  6147. + .sb = root->d_sb,
  6148. + .test = test,
  6149. + .arg = arg
  6150. + };
  6151. +
  6152. + d_walk(root, &args, au_call_dpages_append);
  6153. +
  6154. + return args.err;
  6155. +}
  6156. +
  6157. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6158. + int do_include, au_dpages_test test, void *arg)
  6159. +{
  6160. + int err;
  6161. +
  6162. + err = 0;
  6163. + write_seqlock(&rename_lock);
  6164. + spin_lock(&dentry->d_lock);
  6165. + if (do_include
  6166. + && au_dcount(dentry) > 0
  6167. + && (!test || test(dentry, arg)))
  6168. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6169. + spin_unlock(&dentry->d_lock);
  6170. + if (unlikely(err))
  6171. + goto out;
  6172. +
  6173. + /*
  6174. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  6175. + * mount
  6176. + */
  6177. + while (!IS_ROOT(dentry)) {
  6178. + dentry = dentry->d_parent; /* rename_lock is locked */
  6179. + spin_lock(&dentry->d_lock);
  6180. + if (au_dcount(dentry) > 0
  6181. + && (!test || test(dentry, arg)))
  6182. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6183. + spin_unlock(&dentry->d_lock);
  6184. + if (unlikely(err))
  6185. + break;
  6186. + }
  6187. +
  6188. +out:
  6189. + write_sequnlock(&rename_lock);
  6190. + return err;
  6191. +}
  6192. +
  6193. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  6194. +{
  6195. + return au_di(dentry) && dentry->d_sb == arg;
  6196. +}
  6197. +
  6198. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6199. + struct dentry *dentry, int do_include)
  6200. +{
  6201. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  6202. + au_dcsub_dpages_aufs, dentry->d_sb);
  6203. +}
  6204. +
  6205. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  6206. +{
  6207. + struct path path[2] = {
  6208. + {
  6209. + .dentry = d1
  6210. + },
  6211. + {
  6212. + .dentry = d2
  6213. + }
  6214. + };
  6215. +
  6216. + return path_is_under(path + 0, path + 1);
  6217. +}
  6218. diff -Naur null/fs/aufs/dcsub.h linux-6.6.63/fs/aufs/dcsub.h
  6219. --- /dev/null
  6220. +++ linux-6.6.63/fs/aufs/dcsub.h 2024-12-18 15:12:59.294519601 +0300
  6221. @@ -0,0 +1,139 @@
  6222. +/* SPDX-License-Identifier: GPL-2.0 */
  6223. +/*
  6224. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  6225. + *
  6226. + * This program is free software; you can redistribute it and/or modify
  6227. + * it under the terms of the GNU General Public License as published by
  6228. + * the Free Software Foundation; either version 2 of the License, or
  6229. + * (at your option) any later version.
  6230. + *
  6231. + * This program is distributed in the hope that it will be useful,
  6232. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6233. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6234. + * GNU General Public License for more details.
  6235. + *
  6236. + * You should have received a copy of the GNU General Public License
  6237. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6238. + */
  6239. +
  6240. +/*
  6241. + * sub-routines for dentry cache
  6242. + */
  6243. +
  6244. +#ifndef __AUFS_DCSUB_H__
  6245. +#define __AUFS_DCSUB_H__
  6246. +
  6247. +#ifdef __KERNEL__
  6248. +
  6249. +#include <linux/dcache.h>
  6250. +#include <linux/fs.h>
  6251. +#include "vfsub.h"
  6252. +
  6253. +struct au_dpage {
  6254. + int ndentry;
  6255. + struct dentry **dentries;
  6256. +};
  6257. +
  6258. +struct au_dcsub_pages {
  6259. + int ndpage;
  6260. + struct au_dpage *dpages;
  6261. +};
  6262. +
  6263. +/* ---------------------------------------------------------------------- */
  6264. +
  6265. +/* dcsub.c */
  6266. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  6267. +void au_dpages_free(struct au_dcsub_pages *dpages);
  6268. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  6269. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6270. + au_dpages_test test, void *arg);
  6271. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6272. + int do_include, au_dpages_test test, void *arg);
  6273. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6274. + struct dentry *dentry, int do_include);
  6275. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  6276. +
  6277. +/* ---------------------------------------------------------------------- */
  6278. +
  6279. +/*
  6280. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  6281. + * include/linux/dcache.h. Try them (in the future).
  6282. + */
  6283. +
  6284. +static inline int au_d_hashed_positive(struct dentry *d)
  6285. +{
  6286. + int err;
  6287. + struct inode *inode = d_inode(d);
  6288. +
  6289. + err = 0;
  6290. + if (unlikely(d_unhashed(d)
  6291. + || d_is_negative(d)
  6292. + /* to support both aufs and branches */
  6293. + || !vfsub_inode_nlink(inode, AU_I_UNKNOWN)))
  6294. + err = -ENOENT;
  6295. + return err;
  6296. +}
  6297. +
  6298. +static inline int au_d_linkable(struct dentry *d)
  6299. +{
  6300. + int err;
  6301. + struct inode *inode = d_inode(d);
  6302. +
  6303. + err = au_d_hashed_positive(d);
  6304. + if (err
  6305. + && d_is_positive(d)
  6306. + && (inode->i_state & I_LINKABLE))
  6307. + err = 0;
  6308. + return err;
  6309. +}
  6310. +
  6311. +static inline int au_d_alive(struct dentry *d)
  6312. +{
  6313. + int err;
  6314. + struct inode *inode;
  6315. +
  6316. + err = 0;
  6317. + if (!IS_ROOT(d))
  6318. + err = au_d_hashed_positive(d);
  6319. + else {
  6320. + inode = d_inode(d);
  6321. + if (unlikely(d_unlinked(d)
  6322. + || d_is_negative(d)
  6323. + || !vfsub_inode_nlink(inode, AU_I_UNKNOWN)))
  6324. + err = -ENOENT;
  6325. + }
  6326. + return err;
  6327. +}
  6328. +
  6329. +static inline int au_alive_dir(struct dentry *d)
  6330. +{
  6331. + int err;
  6332. +
  6333. + err = au_d_alive(d);
  6334. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  6335. + err = -ENOENT;
  6336. + return err;
  6337. +}
  6338. +
  6339. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  6340. +{
  6341. + return a->len == b->len
  6342. + && !memcmp(a->name, b->name, a->len);
  6343. +}
  6344. +
  6345. +/*
  6346. + * by the commit
  6347. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  6348. + * taking d_lock
  6349. + * the type of d_lockref.count became int, but the inlined function d_count()
  6350. + * still returns unsigned int.
  6351. + * I don't know why. Maybe it is for every d_count() users?
  6352. + * Anyway au_dcount() lives on.
  6353. + */
  6354. +static inline int au_dcount(struct dentry *d)
  6355. +{
  6356. + return (int)d_count(d);
  6357. +}
  6358. +
  6359. +#endif /* __KERNEL__ */
  6360. +#endif /* __AUFS_DCSUB_H__ */
  6361. diff -Naur null/fs/aufs/debug.c linux-6.6.63/fs/aufs/debug.c
  6362. --- /dev/null
  6363. +++ linux-6.6.63/fs/aufs/debug.c 2024-12-18 15:12:59.294519601 +0300
  6364. @@ -0,0 +1,449 @@
  6365. +// SPDX-License-Identifier: GPL-2.0
  6366. +/*
  6367. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  6368. + *
  6369. + * This program is free software; you can redistribute it and/or modify
  6370. + * it under the terms of the GNU General Public License as published by
  6371. + * the Free Software Foundation; either version 2 of the License, or
  6372. + * (at your option) any later version.
  6373. + *
  6374. + * This program is distributed in the hope that it will be useful,
  6375. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6376. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6377. + * GNU General Public License for more details.
  6378. + *
  6379. + * You should have received a copy of the GNU General Public License
  6380. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6381. + */
  6382. +
  6383. +/*
  6384. + * debug print functions
  6385. + */
  6386. +
  6387. +#include <linux/iversion.h>
  6388. +#include "aufs.h"
  6389. +
  6390. +/* Returns 0, or -errno. arg is in kp->arg. */
  6391. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  6392. +{
  6393. + int err, n;
  6394. +
  6395. + err = kstrtoint(val, 0, &n);
  6396. + if (!err) {
  6397. + if (n > 0)
  6398. + au_debug_on();
  6399. + else
  6400. + au_debug_off();
  6401. + }
  6402. + return err;
  6403. +}
  6404. +
  6405. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  6406. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  6407. +{
  6408. + atomic_t *a;
  6409. +
  6410. + a = kp->arg;
  6411. + return sprintf(buffer, "%d", atomic_read(a));
  6412. +}
  6413. +
  6414. +static const struct kernel_param_ops param_ops_atomic_t = {
  6415. + .set = param_atomic_t_set,
  6416. + .get = param_atomic_t_get
  6417. + /* void (*free)(void *arg) */
  6418. +};
  6419. +
  6420. +atomic_t aufs_debug = ATOMIC_INIT(0);
  6421. +MODULE_PARM_DESC(debug, "debug print");
  6422. +module_param_named(debug, aufs_debug, atomic_t, 0664);
  6423. +
  6424. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  6425. +char *au_plevel = KERN_DEBUG;
  6426. +#define dpri(fmt, ...) do { \
  6427. + if ((au_plevel \
  6428. + && strcmp(au_plevel, KERN_DEBUG)) \
  6429. + || au_debug_test()) \
  6430. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  6431. +} while (0)
  6432. +
  6433. +/* ---------------------------------------------------------------------- */
  6434. +
  6435. +void au_dpri_whlist(struct au_nhash *whlist)
  6436. +{
  6437. + unsigned long ul, n;
  6438. + struct hlist_head *head;
  6439. + struct au_vdir_wh *pos;
  6440. +
  6441. + n = whlist->nh_num;
  6442. + head = whlist->nh_head;
  6443. + for (ul = 0; ul < n; ul++) {
  6444. + hlist_for_each_entry(pos, head, wh_hash)
  6445. + dpri("b%d, %.*s, %d\n",
  6446. + pos->wh_bindex,
  6447. + pos->wh_str.len, pos->wh_str.name,
  6448. + pos->wh_str.len);
  6449. + head++;
  6450. + }
  6451. +}
  6452. +
  6453. +void au_dpri_vdir(struct au_vdir *vdir)
  6454. +{
  6455. + unsigned long ul;
  6456. + union au_vdir_deblk_p p;
  6457. + unsigned char *o;
  6458. +
  6459. + if (!vdir || IS_ERR(vdir)) {
  6460. + dpri("err %ld\n", PTR_ERR(vdir));
  6461. + return;
  6462. + }
  6463. +
  6464. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
  6465. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6466. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6467. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6468. + p.deblk = vdir->vd_deblk[ul];
  6469. + o = p.deblk;
  6470. + dpri("[%lu]: %p\n", ul, o);
  6471. + }
  6472. +}
  6473. +
  6474. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6475. + struct dentry *wh)
  6476. +{
  6477. + char *n = NULL;
  6478. + int l = 0;
  6479. + struct timespec64 ctime;
  6480. +
  6481. + if (!inode || IS_ERR(inode)) {
  6482. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6483. + return -1;
  6484. + }
  6485. +
  6486. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6487. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6488. + && sizeof(inode->i_blocks) != sizeof(u64));
  6489. + if (wh) {
  6490. + n = (void *)wh->d_name.name;
  6491. + l = wh->d_name.len;
  6492. + }
  6493. +
  6494. + ctime = inode_get_ctime(inode);
  6495. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6496. + " acl %p, def_acl %p,"
  6497. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6498. + bindex, inode,
  6499. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6500. + atomic_read(&inode->i_count),
  6501. + vfsub_inode_nlink(inode, AU_I_UNKNOWN), inode->i_mode,
  6502. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6503. + inode->i_acl, inode->i_default_acl,
  6504. + hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
  6505. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6506. + inode->i_state, inode->i_flags, inode_peek_iversion(inode),
  6507. + inode->i_generation,
  6508. + l ? ", wh " : "", l, n);
  6509. + return 0;
  6510. +}
  6511. +
  6512. +void au_dpri_inode(struct inode *inode)
  6513. +{
  6514. + struct au_iinfo *iinfo;
  6515. + struct au_hinode *hi;
  6516. + aufs_bindex_t bindex;
  6517. + int err, hn;
  6518. +
  6519. + err = do_pri_inode(-1, inode, -1, NULL);
  6520. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6521. + return;
  6522. +
  6523. + iinfo = au_ii(inode);
  6524. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6525. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6526. + if (iinfo->ii_btop < 0)
  6527. + return;
  6528. + hn = 0;
  6529. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6530. + hi = au_hinode(iinfo, bindex);
  6531. + hn = !!au_hn(hi);
  6532. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6533. + }
  6534. +}
  6535. +
  6536. +void au_dpri_dalias(struct inode *inode)
  6537. +{
  6538. + struct dentry *d;
  6539. +
  6540. + spin_lock(&inode->i_lock);
  6541. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6542. + au_dpri_dentry(d);
  6543. + spin_unlock(&inode->i_lock);
  6544. +}
  6545. +
  6546. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6547. +{
  6548. + struct dentry *wh = NULL;
  6549. + int hn;
  6550. + struct inode *inode;
  6551. + struct au_iinfo *iinfo;
  6552. + struct au_hinode *hi;
  6553. +
  6554. + if (!dentry || IS_ERR(dentry)) {
  6555. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6556. + return -1;
  6557. + }
  6558. + /* do not call dget_parent() here */
  6559. + /* note: access d_xxx without d_lock */
  6560. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6561. + bindex, dentry, dentry,
  6562. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6563. + au_dcount(dentry), dentry->d_flags,
  6564. + d_unhashed(dentry) ? "un" : "");
  6565. + hn = -1;
  6566. + inode = NULL;
  6567. + if (d_is_positive(dentry))
  6568. + inode = d_inode(dentry);
  6569. + if (inode
  6570. + && au_test_aufs(dentry->d_sb)
  6571. + && bindex >= 0
  6572. + && !au_is_bad_inode(inode)) {
  6573. + iinfo = au_ii(inode);
  6574. + hi = au_hinode(iinfo, bindex);
  6575. + hn = !!au_hn(hi);
  6576. + wh = hi->hi_whdentry;
  6577. + }
  6578. + do_pri_inode(bindex, inode, hn, wh);
  6579. + return 0;
  6580. +}
  6581. +
  6582. +void au_dpri_dentry(struct dentry *dentry)
  6583. +{
  6584. + struct au_dinfo *dinfo;
  6585. + aufs_bindex_t bindex;
  6586. + int err;
  6587. +
  6588. + err = do_pri_dentry(-1, dentry);
  6589. + if (err || !au_test_aufs(dentry->d_sb))
  6590. + return;
  6591. +
  6592. + dinfo = au_di(dentry);
  6593. + if (!dinfo)
  6594. + return;
  6595. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6596. + dinfo->di_btop, dinfo->di_bbot,
  6597. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6598. + dinfo->di_tmpfile);
  6599. + if (dinfo->di_btop < 0)
  6600. + return;
  6601. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6602. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6603. +}
  6604. +
  6605. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6606. +{
  6607. + char a[32];
  6608. +
  6609. + if (!file || IS_ERR(file)) {
  6610. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6611. + return -1;
  6612. + }
  6613. + a[0] = 0;
  6614. + if (bindex < 0
  6615. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6616. + && au_test_aufs(file->f_path.dentry->d_sb)
  6617. + && au_fi(file))
  6618. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6619. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6620. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6621. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6622. + file->f_version, file->f_pos, a);
  6623. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6624. + do_pri_dentry(bindex, file->f_path.dentry);
  6625. + return 0;
  6626. +}
  6627. +
  6628. +void au_dpri_file(struct file *file)
  6629. +{
  6630. + struct au_finfo *finfo;
  6631. + struct au_fidir *fidir;
  6632. + struct au_hfile *hfile;
  6633. + aufs_bindex_t bindex;
  6634. + int err;
  6635. +
  6636. + err = do_pri_file(-1, file);
  6637. + if (err
  6638. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6639. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6640. + return;
  6641. +
  6642. + finfo = au_fi(file);
  6643. + if (!finfo)
  6644. + return;
  6645. + if (finfo->fi_btop < 0)
  6646. + return;
  6647. + fidir = finfo->fi_hdir;
  6648. + if (!fidir)
  6649. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6650. + else
  6651. + for (bindex = finfo->fi_btop;
  6652. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6653. + bindex++) {
  6654. + hfile = fidir->fd_hfile + bindex;
  6655. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6656. + }
  6657. +}
  6658. +
  6659. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6660. +{
  6661. + struct vfsmount *mnt;
  6662. + struct super_block *sb;
  6663. +
  6664. + if (!br || IS_ERR(br))
  6665. + goto out;
  6666. + mnt = au_br_mnt(br);
  6667. + if (!mnt || IS_ERR(mnt))
  6668. + goto out;
  6669. + sb = mnt->mnt_sb;
  6670. + if (!sb || IS_ERR(sb))
  6671. + goto out;
  6672. +
  6673. + dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
  6674. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6675. + "xino %d\n",
  6676. + bindex, br->br_perm, br->br_id, br->br_wbr,
  6677. + au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6678. + sb->s_flags, sb->s_count,
  6679. + atomic_read(&sb->s_active),
  6680. + !!au_xino_file(br->br_xino, /*idx*/-1));
  6681. + return 0;
  6682. +
  6683. +out:
  6684. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6685. + return -1;
  6686. +}
  6687. +
  6688. +void au_dpri_sb(struct super_block *sb)
  6689. +{
  6690. + struct au_sbinfo *sbinfo;
  6691. + aufs_bindex_t bindex;
  6692. + int err;
  6693. + /* to reduce stack size */
  6694. + struct {
  6695. + struct vfsmount mnt;
  6696. + struct au_branch fake;
  6697. + } *a;
  6698. +
  6699. + /* this function can be called from magic sysrq */
  6700. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6701. + if (unlikely(!a)) {
  6702. + dpri("no memory\n");
  6703. + return;
  6704. + }
  6705. +
  6706. + a->mnt.mnt_sb = sb;
  6707. + a->fake.br_path.mnt = &a->mnt;
  6708. + err = do_pri_br(-1, &a->fake);
  6709. + au_kfree_rcu(a);
  6710. + dpri("dev 0x%x\n", sb->s_dev);
  6711. + if (err || !au_test_aufs(sb))
  6712. + return;
  6713. +
  6714. + sbinfo = au_sbi(sb);
  6715. + if (!sbinfo)
  6716. + return;
  6717. + dpri("nw %d, gen %u, kobj %d\n",
  6718. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6719. + kref_read(&sbinfo->si_kobj.kref));
  6720. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6721. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6722. +}
  6723. +
  6724. +/* ---------------------------------------------------------------------- */
  6725. +
  6726. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6727. +{
  6728. + struct inode *h_inode, *inode = d_inode(dentry);
  6729. + struct dentry *h_dentry;
  6730. + aufs_bindex_t bindex, bbot, bi;
  6731. +
  6732. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6733. + return;
  6734. +
  6735. + bbot = au_dbbot(dentry);
  6736. + bi = au_ibbot(inode);
  6737. + if (bi < bbot)
  6738. + bbot = bi;
  6739. + bindex = au_dbtop(dentry);
  6740. + bi = au_ibtop(inode);
  6741. + if (bi > bindex)
  6742. + bindex = bi;
  6743. +
  6744. + for (; bindex <= bbot; bindex++) {
  6745. + h_dentry = au_h_dptr(dentry, bindex);
  6746. + if (!h_dentry)
  6747. + continue;
  6748. + h_inode = au_h_iptr(inode, bindex);
  6749. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6750. + au_debug_on();
  6751. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6752. + AuDbgDentry(dentry);
  6753. + AuDbgInode(inode);
  6754. + au_debug_off();
  6755. + if (au_test_fuse(h_inode->i_sb))
  6756. + WARN_ON_ONCE(1);
  6757. + else
  6758. + BUG();
  6759. + }
  6760. + }
  6761. +}
  6762. +
  6763. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6764. +{
  6765. + int err, i, j;
  6766. + struct au_dcsub_pages dpages;
  6767. + struct au_dpage *dpage;
  6768. + struct dentry **dentries;
  6769. +
  6770. + err = au_dpages_init(&dpages, GFP_NOFS);
  6771. + AuDebugOn(err);
  6772. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6773. + AuDebugOn(err);
  6774. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6775. + dpage = dpages.dpages + i;
  6776. + dentries = dpage->dentries;
  6777. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6778. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6779. + }
  6780. + au_dpages_free(&dpages);
  6781. +}
  6782. +
  6783. +void au_dbg_verify_kthread(void)
  6784. +{
  6785. + if (au_wkq_test()) {
  6786. + au_dbg_blocked();
  6787. + /*
  6788. + * It may be recursive, but udba=notify between two aufs mounts,
  6789. + * where a single ro branch is shared, is not a problem.
  6790. + */
  6791. + /* WARN_ON(1); */
  6792. + }
  6793. +}
  6794. +
  6795. +/* ---------------------------------------------------------------------- */
  6796. +
  6797. +int __init au_debug_init(void)
  6798. +{
  6799. + aufs_bindex_t bindex;
  6800. + struct au_vdir_destr destr;
  6801. +
  6802. + bindex = -1;
  6803. + AuDebugOn(bindex >= 0);
  6804. +
  6805. + destr.len = -1;
  6806. + AuDebugOn(destr.len < NAME_MAX);
  6807. +
  6808. +#ifdef CONFIG_4KSTACKS
  6809. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6810. +#endif
  6811. +
  6812. + return 0;
  6813. +}
  6814. diff -Naur null/fs/aufs/debug.h linux-6.6.63/fs/aufs/debug.h
  6815. --- /dev/null
  6816. +++ linux-6.6.63/fs/aufs/debug.h 2024-12-18 15:12:59.294519601 +0300
  6817. @@ -0,0 +1,226 @@
  6818. +/* SPDX-License-Identifier: GPL-2.0 */
  6819. +/*
  6820. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  6821. + *
  6822. + * This program is free software; you can redistribute it and/or modify
  6823. + * it under the terms of the GNU General Public License as published by
  6824. + * the Free Software Foundation; either version 2 of the License, or
  6825. + * (at your option) any later version.
  6826. + *
  6827. + * This program is distributed in the hope that it will be useful,
  6828. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6829. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6830. + * GNU General Public License for more details.
  6831. + *
  6832. + * You should have received a copy of the GNU General Public License
  6833. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6834. + */
  6835. +
  6836. +/*
  6837. + * debug print functions
  6838. + */
  6839. +
  6840. +#ifndef __AUFS_DEBUG_H__
  6841. +#define __AUFS_DEBUG_H__
  6842. +
  6843. +#ifdef __KERNEL__
  6844. +
  6845. +#include <linux/atomic.h>
  6846. +#include <linux/module.h>
  6847. +#include <linux/kallsyms.h>
  6848. +#include <linux/sysrq.h>
  6849. +
  6850. +#ifdef CONFIG_AUFS_DEBUG
  6851. +#define AuDebugOn(a) BUG_ON(a)
  6852. +
  6853. +/* module parameter */
  6854. +extern atomic_t aufs_debug;
  6855. +static inline void au_debug_on(void)
  6856. +{
  6857. + atomic_inc(&aufs_debug);
  6858. +}
  6859. +static inline void au_debug_off(void)
  6860. +{
  6861. + atomic_dec_if_positive(&aufs_debug);
  6862. +}
  6863. +
  6864. +static inline int au_debug_test(void)
  6865. +{
  6866. + return atomic_read(&aufs_debug) > 0;
  6867. +}
  6868. +#else
  6869. +AuStubVoid(AuDebugOn, bool expr __maybe_unused)
  6870. +AuStubVoid(au_debug_on, void)
  6871. +AuStubVoid(au_debug_off, void)
  6872. +AuStubInt0(au_debug_test, void)
  6873. +#endif /* CONFIG_AUFS_DEBUG */
  6874. +
  6875. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6876. +
  6877. +/* ---------------------------------------------------------------------- */
  6878. +
  6879. +/* debug print */
  6880. +
  6881. +#define AuDbg(fmt, ...) do { \
  6882. + if (au_debug_test()) \
  6883. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6884. +} while (0)
  6885. +#define AuLabel(l) AuDbg(#l "\n")
  6886. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6887. +#define AuWarn1(fmt, ...) do { \
  6888. + static unsigned char _c; \
  6889. + if (!_c++) \
  6890. + pr_warn(fmt, ##__VA_ARGS__); \
  6891. +} while (0)
  6892. +
  6893. +#define AuErr1(fmt, ...) do { \
  6894. + static unsigned char _c; \
  6895. + if (!_c++) \
  6896. + pr_err(fmt, ##__VA_ARGS__); \
  6897. +} while (0)
  6898. +
  6899. +#define AuIOErr1(fmt, ...) do { \
  6900. + static unsigned char _c; \
  6901. + if (!_c++) \
  6902. + AuIOErr(fmt, ##__VA_ARGS__); \
  6903. +} while (0)
  6904. +
  6905. +#define AuUnsupportMsg "This operation is not supported." \
  6906. + " Please report this application to aufs-users ML."
  6907. +#define AuUnsupport(fmt, ...) do { \
  6908. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6909. + dump_stack(); \
  6910. +} while (0)
  6911. +
  6912. +#define AuTraceErr(e) do { \
  6913. + if (unlikely((e) < 0)) \
  6914. + AuDbg("err %d\n", (int)(e)); \
  6915. +} while (0)
  6916. +
  6917. +#define AuTraceErrPtr(p) do { \
  6918. + if (IS_ERR(p)) \
  6919. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6920. +} while (0)
  6921. +
  6922. +/* dirty macros for debug print, use with "%.*s" and caution */
  6923. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6924. +
  6925. +/* ---------------------------------------------------------------------- */
  6926. +
  6927. +struct dentry;
  6928. +struct inode;
  6929. +struct au_nhash;
  6930. +struct au_vdir;
  6931. +#ifdef CONFIG_AUFS_DEBUG
  6932. +extern struct mutex au_dbg_mtx;
  6933. +extern char *au_plevel;
  6934. +void au_dpri_whlist(struct au_nhash *whlist);
  6935. +void au_dpri_vdir(struct au_vdir *vdir);
  6936. +void au_dpri_inode(struct inode *inode);
  6937. +void au_dpri_dalias(struct inode *inode);
  6938. +void au_dpri_dentry(struct dentry *dentry);
  6939. +struct file;
  6940. +void au_dpri_file(struct file *filp);
  6941. +struct super_block;
  6942. +void au_dpri_sb(struct super_block *sb);
  6943. +
  6944. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6945. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6946. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6947. +void au_dbg_verify_kthread(void);
  6948. +
  6949. +int __init au_debug_init(void);
  6950. +
  6951. +#define AuDbgWhlist(w) do { \
  6952. + mutex_lock(&au_dbg_mtx); \
  6953. + AuDbg(#w "\n"); \
  6954. + au_dpri_whlist(w); \
  6955. + mutex_unlock(&au_dbg_mtx); \
  6956. +} while (0)
  6957. +
  6958. +#define AuDbgVdir(v) do { \
  6959. + mutex_lock(&au_dbg_mtx); \
  6960. + AuDbg(#v "\n"); \
  6961. + au_dpri_vdir(v); \
  6962. + mutex_unlock(&au_dbg_mtx); \
  6963. +} while (0)
  6964. +
  6965. +#define AuDbgInode(i) do { \
  6966. + mutex_lock(&au_dbg_mtx); \
  6967. + AuDbg(#i "\n"); \
  6968. + au_dpri_inode(i); \
  6969. + mutex_unlock(&au_dbg_mtx); \
  6970. +} while (0)
  6971. +
  6972. +#define AuDbgDAlias(i) do { \
  6973. + mutex_lock(&au_dbg_mtx); \
  6974. + AuDbg(#i "\n"); \
  6975. + au_dpri_dalias(i); \
  6976. + mutex_unlock(&au_dbg_mtx); \
  6977. +} while (0)
  6978. +
  6979. +#define AuDbgDentry(d) do { \
  6980. + mutex_lock(&au_dbg_mtx); \
  6981. + AuDbg(#d "\n"); \
  6982. + au_dpri_dentry(d); \
  6983. + mutex_unlock(&au_dbg_mtx); \
  6984. +} while (0)
  6985. +
  6986. +#define AuDbgFile(f) do { \
  6987. + mutex_lock(&au_dbg_mtx); \
  6988. + AuDbg(#f "\n"); \
  6989. + au_dpri_file(f); \
  6990. + mutex_unlock(&au_dbg_mtx); \
  6991. +} while (0)
  6992. +
  6993. +#define AuDbgSb(sb) do { \
  6994. + mutex_lock(&au_dbg_mtx); \
  6995. + AuDbg(#sb "\n"); \
  6996. + au_dpri_sb(sb); \
  6997. + mutex_unlock(&au_dbg_mtx); \
  6998. +} while (0)
  6999. +
  7000. +#define AuDbgSym(addr) do { \
  7001. + char sym[KSYM_SYMBOL_LEN]; \
  7002. + sprint_symbol(sym, (unsigned long)addr); \
  7003. + AuDbg("%s\n", sym); \
  7004. +} while (0)
  7005. +#else
  7006. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  7007. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  7008. +AuStubVoid(au_dbg_verify_kthread, void)
  7009. +AuStubInt0(__init au_debug_init, void)
  7010. +
  7011. +AuStubVoid(AuDbgWhlist, struct au_nhash *whlist)
  7012. +AuStubVoid(AuDbgVdir, struct au_vdir *vdir)
  7013. +AuStubVoid(AuDbgInode, struct inode *inode)
  7014. +AuStubVoid(AuDbgDAlias, struct inode *inode)
  7015. +AuStubVoid(AuDbgDentry, struct dentry *dentry)
  7016. +AuStubVoid(AuDbgFile, struct file *filp)
  7017. +AuStubVoid(AuDbgSb, struct super_block *sb)
  7018. +AuStubVoid(AuDbgSym, void *addr)
  7019. +#endif /* CONFIG_AUFS_DEBUG */
  7020. +
  7021. +/* ---------------------------------------------------------------------- */
  7022. +
  7023. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  7024. +int __init au_sysrq_init(void);
  7025. +void au_sysrq_fin(void);
  7026. +
  7027. +#ifdef CONFIG_HW_CONSOLE
  7028. +#define au_dbg_blocked() do { \
  7029. + WARN_ON(1); \
  7030. + handle_sysrq('w'); \
  7031. +} while (0)
  7032. +#else
  7033. +AuStubVoid(au_dbg_blocked, void)
  7034. +#endif
  7035. +
  7036. +#else
  7037. +AuStubInt0(__init au_sysrq_init, void)
  7038. +AuStubVoid(au_sysrq_fin, void)
  7039. +AuStubVoid(au_dbg_blocked, void)
  7040. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  7041. +
  7042. +#endif /* __KERNEL__ */
  7043. +#endif /* __AUFS_DEBUG_H__ */
  7044. diff -Naur null/fs/aufs/dentry.c linux-6.6.63/fs/aufs/dentry.c
  7045. --- /dev/null
  7046. +++ linux-6.6.63/fs/aufs/dentry.c 2024-12-18 15:12:59.294519601 +0300
  7047. @@ -0,0 +1,1176 @@
  7048. +// SPDX-License-Identifier: GPL-2.0
  7049. +/*
  7050. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  7051. + *
  7052. + * This program is free software; you can redistribute it and/or modify
  7053. + * it under the terms of the GNU General Public License as published by
  7054. + * the Free Software Foundation; either version 2 of the License, or
  7055. + * (at your option) any later version.
  7056. + *
  7057. + * This program is distributed in the hope that it will be useful,
  7058. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7059. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7060. + * GNU General Public License for more details.
  7061. + *
  7062. + * You should have received a copy of the GNU General Public License
  7063. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7064. + */
  7065. +
  7066. +/*
  7067. + * lookup and dentry operations
  7068. + */
  7069. +
  7070. +#include <linux/iversion.h>
  7071. +#include "aufs.h"
  7072. +
  7073. +/*
  7074. + * returns positive/negative dentry, NULL or an error.
  7075. + * NULL means whiteout-ed or not-found.
  7076. + */
  7077. +static struct dentry*
  7078. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  7079. + aufs_bindex_t bindex, struct au_do_lookup_args *args)
  7080. +{
  7081. + struct dentry *h_dentry;
  7082. + struct inode *h_inode;
  7083. + struct au_branch *br;
  7084. + struct mnt_idmap *h_idmap;
  7085. + struct path h_path;
  7086. + int wh_found, opq;
  7087. + unsigned char wh_able;
  7088. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  7089. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  7090. + IGNORE_PERM);
  7091. +
  7092. + wh_found = 0;
  7093. + br = au_sbr(dentry->d_sb, bindex);
  7094. + h_path.dentry = h_parent;
  7095. + h_path.mnt = au_br_mnt(br);
  7096. + h_idmap = au_br_idmap(br);
  7097. + wh_able = !!au_br_whable(br->br_perm);
  7098. + if (wh_able)
  7099. + wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
  7100. + ignore_perm);
  7101. + h_dentry = ERR_PTR(wh_found);
  7102. + if (!wh_found)
  7103. + goto real_lookup;
  7104. + if (unlikely(wh_found < 0))
  7105. + goto out;
  7106. +
  7107. + /* We found a whiteout */
  7108. + /* au_set_dbbot(dentry, bindex); */
  7109. + au_set_dbwh(dentry, bindex);
  7110. + if (!allow_neg)
  7111. + return NULL; /* success */
  7112. +
  7113. +real_lookup:
  7114. + if (!ignore_perm)
  7115. + h_dentry = vfsub_lkup_one(args->name, &h_path);
  7116. + else
  7117. + h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
  7118. + if (IS_ERR(h_dentry)) {
  7119. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  7120. + && !allow_neg)
  7121. + h_dentry = NULL;
  7122. + goto out;
  7123. + }
  7124. +
  7125. + h_inode = d_inode(h_dentry);
  7126. + if (d_is_negative(h_dentry)) {
  7127. + if (!allow_neg)
  7128. + goto out_neg;
  7129. + } else if (wh_found
  7130. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  7131. + goto out_neg;
  7132. + else if (au_ftest_lkup(args->flags, DIRREN)
  7133. + /* && h_inode */
  7134. + && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
  7135. + AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
  7136. + (unsigned long long)h_inode->i_ino);
  7137. + goto out_neg;
  7138. + }
  7139. +
  7140. + if (au_dbbot(dentry) <= bindex)
  7141. + au_set_dbbot(dentry, bindex);
  7142. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  7143. + au_set_dbtop(dentry, bindex);
  7144. + au_set_h_dptr(dentry, bindex, h_dentry);
  7145. +
  7146. + if (!d_is_dir(h_dentry)
  7147. + || !wh_able
  7148. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  7149. + goto out; /* success */
  7150. +
  7151. + h_path.dentry = h_dentry;
  7152. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  7153. + opq = au_diropq_test(h_idmap, &h_path);
  7154. + inode_unlock_shared(h_inode);
  7155. + if (opq > 0)
  7156. + au_set_dbdiropq(dentry, bindex);
  7157. + else if (unlikely(opq < 0)) {
  7158. + au_set_h_dptr(dentry, bindex, NULL);
  7159. + h_dentry = ERR_PTR(opq);
  7160. + }
  7161. + goto out;
  7162. +
  7163. +out_neg:
  7164. + dput(h_dentry);
  7165. + h_dentry = NULL;
  7166. +out:
  7167. + return h_dentry;
  7168. +}
  7169. +
  7170. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  7171. +{
  7172. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  7173. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  7174. + return -EPERM;
  7175. + return 0;
  7176. +}
  7177. +
  7178. +/*
  7179. + * returns the number of lower positive dentries,
  7180. + * otherwise an error.
  7181. + * can be called at unlinking with @type is zero.
  7182. + */
  7183. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7184. + unsigned int flags)
  7185. +{
  7186. + int npositive, err;
  7187. + aufs_bindex_t bindex, btail, bdiropq;
  7188. + unsigned char isdir, dirperm1, dirren;
  7189. + struct au_do_lookup_args args = {
  7190. + .flags = flags,
  7191. + .name = &dentry->d_name
  7192. + };
  7193. + struct dentry *parent;
  7194. + struct super_block *sb;
  7195. +
  7196. + sb = dentry->d_sb;
  7197. + err = au_test_shwh(sb, args.name);
  7198. + if (unlikely(err))
  7199. + goto out;
  7200. +
  7201. + err = au_wh_name_alloc(&args.whname, args.name);
  7202. + if (unlikely(err))
  7203. + goto out;
  7204. +
  7205. + isdir = !!d_is_dir(dentry);
  7206. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  7207. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  7208. + if (dirren)
  7209. + au_fset_lkup(args.flags, DIRREN);
  7210. +
  7211. + npositive = 0;
  7212. + parent = dget_parent(dentry);
  7213. + btail = au_dbtaildir(parent);
  7214. + for (bindex = btop; bindex <= btail; bindex++) {
  7215. + struct dentry *h_parent, *h_dentry;
  7216. + struct inode *h_inode, *h_dir;
  7217. + struct au_branch *br;
  7218. +
  7219. + h_dentry = au_h_dptr(dentry, bindex);
  7220. + if (h_dentry) {
  7221. + if (d_is_positive(h_dentry))
  7222. + npositive++;
  7223. + break;
  7224. + }
  7225. + h_parent = au_h_dptr(parent, bindex);
  7226. + if (!h_parent || !d_is_dir(h_parent))
  7227. + continue;
  7228. +
  7229. + if (dirren) {
  7230. + /* if the inum matches, then use the prepared name */
  7231. + err = au_dr_lkup_name(&args, bindex);
  7232. + if (unlikely(err))
  7233. + goto out_parent;
  7234. + }
  7235. +
  7236. + h_dir = d_inode(h_parent);
  7237. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  7238. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
  7239. + inode_unlock_shared(h_dir);
  7240. + err = PTR_ERR(h_dentry);
  7241. + if (IS_ERR(h_dentry))
  7242. + goto out_parent;
  7243. + if (h_dentry)
  7244. + au_fclr_lkup(args.flags, ALLOW_NEG);
  7245. + if (dirperm1)
  7246. + au_fset_lkup(args.flags, IGNORE_PERM);
  7247. +
  7248. + if (au_dbwh(dentry) == bindex)
  7249. + break;
  7250. + if (!h_dentry)
  7251. + continue;
  7252. + if (d_is_negative(h_dentry))
  7253. + continue;
  7254. + h_inode = d_inode(h_dentry);
  7255. + npositive++;
  7256. + if (!args.type)
  7257. + args.type = h_inode->i_mode & S_IFMT;
  7258. + if (args.type != S_IFDIR)
  7259. + break;
  7260. + else if (isdir) {
  7261. + /* the type of lower may be different */
  7262. + bdiropq = au_dbdiropq(dentry);
  7263. + if (bdiropq >= 0 && bdiropq <= bindex)
  7264. + break;
  7265. + }
  7266. + br = au_sbr(sb, bindex);
  7267. + if (dirren
  7268. + && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
  7269. + /*add_ent*/NULL)) {
  7270. + /* prepare next name to lookup */
  7271. + err = au_dr_lkup(&args, dentry, bindex);
  7272. + if (unlikely(err))
  7273. + goto out_parent;
  7274. + }
  7275. + }
  7276. +
  7277. + if (npositive) {
  7278. + AuLabel(positive);
  7279. + au_update_dbtop(dentry);
  7280. + }
  7281. + err = npositive;
  7282. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  7283. + && au_dbtop(dentry) < 0)) {
  7284. + err = -EIO;
  7285. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  7286. + dentry, err);
  7287. + }
  7288. +
  7289. +out_parent:
  7290. + dput(parent);
  7291. + au_kfree_try_rcu(args.whname.name);
  7292. + if (dirren)
  7293. + au_dr_lkup_fin(&args);
  7294. +out:
  7295. + return err;
  7296. +}
  7297. +
  7298. +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
  7299. + struct path *ppath)
  7300. +{
  7301. + struct dentry *dentry;
  7302. + int wkq_err;
  7303. +
  7304. + if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
  7305. + dentry = vfsub_lkup_one(name, ppath);
  7306. + else {
  7307. + struct vfsub_lkup_one_args args = {
  7308. + .errp = &dentry,
  7309. + .name = name,
  7310. + .ppath = ppath
  7311. + };
  7312. +
  7313. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  7314. + if (unlikely(wkq_err))
  7315. + dentry = ERR_PTR(wkq_err);
  7316. + }
  7317. +
  7318. + return dentry;
  7319. +}
  7320. +
  7321. +/*
  7322. + * lookup @dentry on @bindex which should be negative.
  7323. + */
  7324. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  7325. +{
  7326. + int err;
  7327. + struct dentry *parent, *h_dentry;
  7328. + struct au_branch *br;
  7329. + struct mnt_idmap *h_idmap;
  7330. + struct path h_ppath;
  7331. +
  7332. + parent = dget_parent(dentry);
  7333. + br = au_sbr(dentry->d_sb, bindex);
  7334. + h_ppath.dentry = au_h_dptr(parent, bindex);
  7335. + h_ppath.mnt = au_br_mnt(br);
  7336. + h_idmap = au_br_idmap(br);
  7337. + if (wh)
  7338. + h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
  7339. + else
  7340. + h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
  7341. + err = PTR_ERR(h_dentry);
  7342. + if (IS_ERR(h_dentry))
  7343. + goto out;
  7344. + if (unlikely(d_is_positive(h_dentry))) {
  7345. + err = -EIO;
  7346. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  7347. + dput(h_dentry);
  7348. + goto out;
  7349. + }
  7350. +
  7351. + err = 0;
  7352. + if (bindex < au_dbtop(dentry))
  7353. + au_set_dbtop(dentry, bindex);
  7354. + if (au_dbbot(dentry) < bindex)
  7355. + au_set_dbbot(dentry, bindex);
  7356. + au_set_h_dptr(dentry, bindex, h_dentry);
  7357. +
  7358. +out:
  7359. + dput(parent);
  7360. + return err;
  7361. +}
  7362. +
  7363. +/* ---------------------------------------------------------------------- */
  7364. +
  7365. +/* subset of struct inode */
  7366. +struct au_iattr {
  7367. + unsigned long i_ino;
  7368. + /* unsigned int i_nlink; */
  7369. + kuid_t i_uid;
  7370. + kgid_t i_gid;
  7371. + u64 i_version;
  7372. +/*
  7373. + loff_t i_size;
  7374. + blkcnt_t i_blocks;
  7375. +*/
  7376. + umode_t i_mode;
  7377. +};
  7378. +
  7379. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  7380. +{
  7381. + ia->i_ino = h_inode->i_ino;
  7382. + /* ia->i_nlink = h_inode->i_nlink; */
  7383. + ia->i_uid = h_inode->i_uid;
  7384. + ia->i_gid = h_inode->i_gid;
  7385. + ia->i_version = inode_query_iversion(h_inode);
  7386. +/*
  7387. + ia->i_size = h_inode->i_size;
  7388. + ia->i_blocks = h_inode->i_blocks;
  7389. +*/
  7390. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  7391. +}
  7392. +
  7393. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  7394. +{
  7395. + return ia->i_ino != h_inode->i_ino
  7396. + /* || ia->i_nlink != h_inode->i_nlink */
  7397. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  7398. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  7399. + || !inode_eq_iversion(h_inode, ia->i_version)
  7400. +/*
  7401. + || ia->i_size != h_inode->i_size
  7402. + || ia->i_blocks != h_inode->i_blocks
  7403. +*/
  7404. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  7405. +}
  7406. +
  7407. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  7408. + struct au_branch *br)
  7409. +{
  7410. + int err;
  7411. + struct au_iattr ia;
  7412. + struct inode *h_inode;
  7413. + struct dentry *h_d;
  7414. + struct super_block *h_sb;
  7415. + struct path h_ppath;
  7416. +
  7417. + err = 0;
  7418. + memset(&ia, -1, sizeof(ia));
  7419. + h_sb = h_dentry->d_sb;
  7420. + h_inode = NULL;
  7421. + if (d_is_positive(h_dentry)) {
  7422. + h_inode = d_inode(h_dentry);
  7423. + au_iattr_save(&ia, h_inode);
  7424. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  7425. + /* nfs d_revalidate may return 0 for negative dentry */
  7426. + /* fuse d_revalidate always return 0 for negative dentry */
  7427. + goto out;
  7428. +
  7429. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  7430. + h_ppath.dentry = h_parent;
  7431. + h_ppath.mnt = au_br_mnt(br);
  7432. + h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
  7433. + err = PTR_ERR(h_d);
  7434. + if (IS_ERR(h_d))
  7435. + goto out;
  7436. +
  7437. + err = 0;
  7438. + if (unlikely(h_d != h_dentry
  7439. + || d_inode(h_d) != h_inode
  7440. + || (h_inode && au_iattr_test(&ia, h_inode))))
  7441. + err = au_busy_or_stale();
  7442. + dput(h_d);
  7443. +
  7444. +out:
  7445. + AuTraceErr(err);
  7446. + return err;
  7447. +}
  7448. +
  7449. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7450. + struct dentry *h_parent, struct au_branch *br)
  7451. +{
  7452. + int err;
  7453. +
  7454. + err = 0;
  7455. + if (udba == AuOpt_UDBA_REVAL
  7456. + && !au_test_fs_remote(h_dentry->d_sb)) {
  7457. + IMustLock(h_dir);
  7458. + err = (d_inode(h_dentry->d_parent) != h_dir);
  7459. + } else if (udba != AuOpt_UDBA_NONE)
  7460. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  7461. +
  7462. + return err;
  7463. +}
  7464. +
  7465. +/* ---------------------------------------------------------------------- */
  7466. +
  7467. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  7468. +{
  7469. + int err;
  7470. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  7471. + struct au_hdentry tmp, *p, *q;
  7472. + struct au_dinfo *dinfo;
  7473. + struct super_block *sb;
  7474. +
  7475. + DiMustWriteLock(dentry);
  7476. +
  7477. + sb = dentry->d_sb;
  7478. + dinfo = au_di(dentry);
  7479. + bbot = dinfo->di_bbot;
  7480. + bwh = dinfo->di_bwh;
  7481. + bdiropq = dinfo->di_bdiropq;
  7482. + bindex = dinfo->di_btop;
  7483. + p = au_hdentry(dinfo, bindex);
  7484. + for (; bindex <= bbot; bindex++, p++) {
  7485. + if (!p->hd_dentry)
  7486. + continue;
  7487. +
  7488. + new_bindex = au_br_index(sb, p->hd_id);
  7489. + if (new_bindex == bindex)
  7490. + continue;
  7491. +
  7492. + if (dinfo->di_bwh == bindex)
  7493. + bwh = new_bindex;
  7494. + if (dinfo->di_bdiropq == bindex)
  7495. + bdiropq = new_bindex;
  7496. + if (new_bindex < 0) {
  7497. + au_hdput(p);
  7498. + p->hd_dentry = NULL;
  7499. + continue;
  7500. + }
  7501. +
  7502. + /* swap two lower dentries, and loop again */
  7503. + q = au_hdentry(dinfo, new_bindex);
  7504. + tmp = *q;
  7505. + *q = *p;
  7506. + *p = tmp;
  7507. + if (tmp.hd_dentry) {
  7508. + bindex--;
  7509. + p--;
  7510. + }
  7511. + }
  7512. +
  7513. + dinfo->di_bwh = -1;
  7514. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7515. + dinfo->di_bwh = bwh;
  7516. +
  7517. + dinfo->di_bdiropq = -1;
  7518. + if (bdiropq >= 0
  7519. + && bdiropq <= au_sbbot(sb)
  7520. + && au_sbr_whable(sb, bdiropq))
  7521. + dinfo->di_bdiropq = bdiropq;
  7522. +
  7523. + err = -EIO;
  7524. + dinfo->di_btop = -1;
  7525. + dinfo->di_bbot = -1;
  7526. + bbot = au_dbbot(parent);
  7527. + bindex = 0;
  7528. + p = au_hdentry(dinfo, bindex);
  7529. + for (; bindex <= bbot; bindex++, p++)
  7530. + if (p->hd_dentry) {
  7531. + dinfo->di_btop = bindex;
  7532. + break;
  7533. + }
  7534. +
  7535. + if (dinfo->di_btop >= 0) {
  7536. + bindex = bbot;
  7537. + p = au_hdentry(dinfo, bindex);
  7538. + for (; bindex >= 0; bindex--, p--)
  7539. + if (p->hd_dentry) {
  7540. + dinfo->di_bbot = bindex;
  7541. + err = 0;
  7542. + break;
  7543. + }
  7544. + }
  7545. +
  7546. + return err;
  7547. +}
  7548. +
  7549. +static void au_do_hide(struct dentry *dentry)
  7550. +{
  7551. + struct inode *inode;
  7552. +
  7553. + if (d_really_is_positive(dentry)) {
  7554. + inode = d_inode(dentry);
  7555. + if (!d_is_dir(dentry)) {
  7556. + if (vfsub_inode_nlink(inode, AU_I_AUFS)
  7557. + && !d_unhashed(dentry))
  7558. + vfsub_drop_nlink(inode);
  7559. + } else {
  7560. + vfsub_clear_nlink(inode);
  7561. + /* stop next lookup */
  7562. + inode->i_flags |= S_DEAD;
  7563. + }
  7564. + smp_mb(); /* necessary? */
  7565. + }
  7566. + d_drop(dentry);
  7567. +}
  7568. +
  7569. +static int au_hide_children(struct dentry *parent)
  7570. +{
  7571. + int err, i, j, ndentry;
  7572. + struct au_dcsub_pages dpages;
  7573. + struct au_dpage *dpage;
  7574. + struct dentry *dentry;
  7575. +
  7576. + err = au_dpages_init(&dpages, GFP_NOFS);
  7577. + if (unlikely(err))
  7578. + goto out;
  7579. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7580. + if (unlikely(err))
  7581. + goto out_dpages;
  7582. +
  7583. + /* in reverse order */
  7584. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7585. + dpage = dpages.dpages + i;
  7586. + ndentry = dpage->ndentry;
  7587. + for (j = ndentry - 1; j >= 0; j--) {
  7588. + dentry = dpage->dentries[j];
  7589. + if (dentry != parent)
  7590. + au_do_hide(dentry);
  7591. + }
  7592. + }
  7593. +
  7594. +out_dpages:
  7595. + au_dpages_free(&dpages);
  7596. +out:
  7597. + return err;
  7598. +}
  7599. +
  7600. +static void au_hide(struct dentry *dentry)
  7601. +{
  7602. + int err;
  7603. +
  7604. + AuDbgDentry(dentry);
  7605. + if (d_is_dir(dentry)) {
  7606. + /* shrink_dcache_parent(dentry); */
  7607. + err = au_hide_children(dentry);
  7608. + if (unlikely(err))
  7609. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7610. + dentry, err);
  7611. + }
  7612. + au_do_hide(dentry);
  7613. +}
  7614. +
  7615. +/*
  7616. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7617. + *
  7618. + * a dirty branch is added
  7619. + * - on the top of layers
  7620. + * - in the middle of layers
  7621. + * - to the bottom of layers
  7622. + *
  7623. + * on the added branch there exists
  7624. + * - a whiteout
  7625. + * - a diropq
  7626. + * - a same named entry
  7627. + * + exist
  7628. + * * negative --> positive
  7629. + * * positive --> positive
  7630. + * - type is unchanged
  7631. + * - type is changed
  7632. + * + doesn't exist
  7633. + * * negative --> negative
  7634. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7635. + * - none
  7636. + */
  7637. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7638. + struct au_dinfo *tmp)
  7639. +{
  7640. + int err;
  7641. + aufs_bindex_t bindex, bbot;
  7642. + struct {
  7643. + struct dentry *dentry;
  7644. + struct inode *inode;
  7645. + mode_t mode;
  7646. + } orig_h, tmp_h = {
  7647. + .dentry = NULL
  7648. + };
  7649. + struct au_hdentry *hd;
  7650. + struct inode *inode, *h_inode;
  7651. + struct dentry *h_dentry;
  7652. +
  7653. + err = 0;
  7654. + AuDebugOn(dinfo->di_btop < 0);
  7655. + orig_h.mode = 0;
  7656. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7657. + orig_h.inode = NULL;
  7658. + if (d_is_positive(orig_h.dentry)) {
  7659. + orig_h.inode = d_inode(orig_h.dentry);
  7660. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7661. + }
  7662. + if (tmp->di_btop >= 0) {
  7663. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7664. + if (d_is_positive(tmp_h.dentry)) {
  7665. + tmp_h.inode = d_inode(tmp_h.dentry);
  7666. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7667. + }
  7668. + }
  7669. +
  7670. + inode = NULL;
  7671. + if (d_really_is_positive(dentry))
  7672. + inode = d_inode(dentry);
  7673. + if (!orig_h.inode) {
  7674. + AuDbg("negative originally\n");
  7675. + if (inode) {
  7676. + au_hide(dentry);
  7677. + goto out;
  7678. + }
  7679. + AuDebugOn(inode);
  7680. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7681. + AuDebugOn(dinfo->di_bdiropq != -1);
  7682. +
  7683. + if (!tmp_h.inode) {
  7684. + AuDbg("negative --> negative\n");
  7685. + /* should have only one negative lower */
  7686. + if (tmp->di_btop >= 0
  7687. + && tmp->di_btop < dinfo->di_btop) {
  7688. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7689. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7690. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7691. + au_di_cp(dinfo, tmp);
  7692. + hd = au_hdentry(tmp, tmp->di_btop);
  7693. + au_set_h_dptr(dentry, tmp->di_btop,
  7694. + dget(hd->hd_dentry));
  7695. + }
  7696. + au_dbg_verify_dinode(dentry);
  7697. + } else {
  7698. + AuDbg("negative --> positive\n");
  7699. + /*
  7700. + * similar to the behaviour of creating with bypassing
  7701. + * aufs.
  7702. + * unhash it in order to force an error in the
  7703. + * succeeding create operation.
  7704. + * we should not set S_DEAD here.
  7705. + */
  7706. + d_drop(dentry);
  7707. + /* au_di_swap(tmp, dinfo); */
  7708. + au_dbg_verify_dinode(dentry);
  7709. + }
  7710. + } else {
  7711. + AuDbg("positive originally\n");
  7712. + /* inode may be NULL */
  7713. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7714. + if (!tmp_h.inode) {
  7715. + AuDbg("positive --> negative\n");
  7716. + /* or bypassing aufs */
  7717. + au_hide(dentry);
  7718. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7719. + dinfo->di_bwh = tmp->di_bwh;
  7720. + if (inode)
  7721. + err = au_refresh_hinode_self(inode);
  7722. + au_dbg_verify_dinode(dentry);
  7723. + } else if (orig_h.mode == tmp_h.mode) {
  7724. + AuDbg("positive --> positive, same type\n");
  7725. + if (!S_ISDIR(orig_h.mode)
  7726. + && dinfo->di_btop > tmp->di_btop) {
  7727. + /*
  7728. + * similar to the behaviour of removing and
  7729. + * creating.
  7730. + */
  7731. + au_hide(dentry);
  7732. + if (inode)
  7733. + err = au_refresh_hinode_self(inode);
  7734. + au_dbg_verify_dinode(dentry);
  7735. + } else {
  7736. + /* fill empty slots */
  7737. + if (dinfo->di_btop > tmp->di_btop)
  7738. + dinfo->di_btop = tmp->di_btop;
  7739. + if (dinfo->di_bbot < tmp->di_bbot)
  7740. + dinfo->di_bbot = tmp->di_bbot;
  7741. + dinfo->di_bwh = tmp->di_bwh;
  7742. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7743. + bbot = dinfo->di_bbot;
  7744. + bindex = tmp->di_btop;
  7745. + hd = au_hdentry(tmp, bindex);
  7746. + for (; bindex <= bbot; bindex++, hd++) {
  7747. + if (au_h_dptr(dentry, bindex))
  7748. + continue;
  7749. + h_dentry = hd->hd_dentry;
  7750. + if (!h_dentry)
  7751. + continue;
  7752. + AuDebugOn(d_is_negative(h_dentry));
  7753. + h_inode = d_inode(h_dentry);
  7754. + AuDebugOn(orig_h.mode
  7755. + != (h_inode->i_mode
  7756. + & S_IFMT));
  7757. + au_set_h_dptr(dentry, bindex,
  7758. + dget(h_dentry));
  7759. + }
  7760. + if (inode)
  7761. + err = au_refresh_hinode(inode, dentry);
  7762. + au_dbg_verify_dinode(dentry);
  7763. + }
  7764. + } else {
  7765. + AuDbg("positive --> positive, different type\n");
  7766. + /* similar to the behaviour of removing and creating */
  7767. + au_hide(dentry);
  7768. + if (inode)
  7769. + err = au_refresh_hinode_self(inode);
  7770. + au_dbg_verify_dinode(dentry);
  7771. + }
  7772. + }
  7773. +
  7774. +out:
  7775. + return err;
  7776. +}
  7777. +
  7778. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7779. +{
  7780. + const struct dentry_operations *dop
  7781. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7782. + static const unsigned int mask
  7783. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7784. +
  7785. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7786. +
  7787. + if (dentry->d_op == dop)
  7788. + return;
  7789. +
  7790. + AuDbg("%pd\n", dentry);
  7791. + spin_lock(&dentry->d_lock);
  7792. + if (dop == &aufs_dop)
  7793. + dentry->d_flags |= mask;
  7794. + else
  7795. + dentry->d_flags &= ~mask;
  7796. + dentry->d_op = dop;
  7797. + spin_unlock(&dentry->d_lock);
  7798. +}
  7799. +
  7800. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7801. +{
  7802. + int err, ebrange, nbr;
  7803. + unsigned int sigen;
  7804. + struct au_dinfo *dinfo, *tmp;
  7805. + struct super_block *sb;
  7806. + struct inode *inode;
  7807. +
  7808. + DiMustWriteLock(dentry);
  7809. + AuDebugOn(IS_ROOT(dentry));
  7810. + AuDebugOn(d_really_is_negative(parent));
  7811. +
  7812. + sb = dentry->d_sb;
  7813. + sigen = au_sigen(sb);
  7814. + err = au_digen_test(parent, sigen);
  7815. + if (unlikely(err))
  7816. + goto out;
  7817. +
  7818. + nbr = au_sbbot(sb) + 1;
  7819. + dinfo = au_di(dentry);
  7820. + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
  7821. + if (unlikely(err))
  7822. + goto out;
  7823. + ebrange = au_dbrange_test(dentry);
  7824. + if (!ebrange)
  7825. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7826. +
  7827. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7828. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7829. + if (d_really_is_positive(dentry)) {
  7830. + inode = d_inode(dentry);
  7831. + err = au_refresh_hinode_self(inode);
  7832. + }
  7833. + au_dbg_verify_dinode(dentry);
  7834. + if (!err)
  7835. + goto out_dgen; /* success */
  7836. + goto out;
  7837. + }
  7838. +
  7839. + /* temporary dinfo */
  7840. + AuDbgDentry(dentry);
  7841. + err = -ENOMEM;
  7842. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7843. + if (unlikely(!tmp))
  7844. + goto out;
  7845. + au_di_swap(tmp, dinfo);
  7846. + /* returns the number of positive dentries */
  7847. + /*
  7848. + * if current working dir is removed, it returns an error.
  7849. + * but the dentry is legal.
  7850. + */
  7851. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7852. + AuDbgDentry(dentry);
  7853. + au_di_swap(tmp, dinfo);
  7854. + if (err == -ENOENT)
  7855. + err = 0;
  7856. + if (err >= 0) {
  7857. + /* compare/refresh by dinfo */
  7858. + AuDbgDentry(dentry);
  7859. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7860. + au_dbg_verify_dinode(dentry);
  7861. + AuTraceErr(err);
  7862. + }
  7863. + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
  7864. + au_rw_write_unlock(&tmp->di_rwsem);
  7865. + au_di_free(tmp);
  7866. + if (unlikely(err))
  7867. + goto out;
  7868. +
  7869. +out_dgen:
  7870. + au_update_digen(dentry);
  7871. +out:
  7872. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7873. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7874. + AuDbgDentry(dentry);
  7875. + }
  7876. + AuTraceErr(err);
  7877. + return err;
  7878. +}
  7879. +
  7880. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7881. + struct dentry *dentry, aufs_bindex_t bindex)
  7882. +{
  7883. + int err, valid;
  7884. +
  7885. + err = 0;
  7886. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7887. + goto out;
  7888. +
  7889. + AuDbg("b%d\n", bindex);
  7890. + /*
  7891. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7892. + * due to whiteout and branch permission.
  7893. + */
  7894. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7895. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7896. + /* it may return tri-state */
  7897. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7898. +
  7899. + if (unlikely(valid < 0))
  7900. + err = valid;
  7901. + else if (!valid)
  7902. + err = -EINVAL;
  7903. +
  7904. +out:
  7905. + AuTraceErr(err);
  7906. + return err;
  7907. +}
  7908. +
  7909. +/* todo: remove this */
  7910. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7911. + unsigned int flags, int do_udba, int dirren)
  7912. +{
  7913. + int err;
  7914. + umode_t mode, h_mode;
  7915. + aufs_bindex_t bindex, btail, btop, ibs, ibe, bwh;
  7916. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7917. + struct inode *h_inode, *h_cached_inode;
  7918. + struct dentry *h_dentry;
  7919. + struct qstr *name, *h_name;
  7920. +
  7921. + err = 0;
  7922. + plus = 0;
  7923. + mode = 0;
  7924. + ibs = -1;
  7925. + ibe = -1;
  7926. + unhashed = !!d_unhashed(dentry);
  7927. + is_root = !!IS_ROOT(dentry);
  7928. + name = &dentry->d_name;
  7929. + tmpfile = au_di(dentry)->di_tmpfile;
  7930. +
  7931. + /*
  7932. + * Theoretically, REVAL test should be unnecessary in case of
  7933. + * {FS,I}NOTIFY.
  7934. + * But {fs,i}notify doesn't fire some necessary events,
  7935. + * IN_ATTRIB for atime/nlink/pageio
  7936. + * Let's do REVAL test too.
  7937. + */
  7938. + if (do_udba && inode) {
  7939. + mode = (inode->i_mode & S_IFMT);
  7940. + plus = (vfsub_inode_nlink(inode, AU_I_AUFS) > 0);
  7941. + ibs = au_ibtop(inode);
  7942. + ibe = au_ibbot(inode);
  7943. + }
  7944. +
  7945. + btop = au_dbtop(dentry);
  7946. + bwh = au_dbwh(dentry);
  7947. + if (0 <= bwh && bwh < btop)
  7948. + btop = bwh;
  7949. + btail = btop;
  7950. + if (inode && S_ISDIR(inode->i_mode))
  7951. + btail = au_dbtaildir(dentry);
  7952. + for (bindex = btop; bindex <= btail; bindex++) {
  7953. + h_dentry = au_h_dptr(dentry, bindex);
  7954. + if (!h_dentry
  7955. + && (bindex == bwh && inode))
  7956. + h_dentry = au_hi_wh(inode, bindex);
  7957. + if (!h_dentry)
  7958. + continue;
  7959. +
  7960. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7961. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7962. + spin_lock(&h_dentry->d_lock);
  7963. + h_name = &h_dentry->d_name;
  7964. + if (unlikely(do_udba
  7965. + && bindex != bwh
  7966. + && !is_root
  7967. + && ((!h_nfs
  7968. + && (unhashed != !!d_unhashed(h_dentry)
  7969. + || (!tmpfile && !dirren
  7970. + && !au_qstreq(name, h_name))
  7971. + ))
  7972. + || (h_nfs
  7973. + && !(flags & LOOKUP_OPEN)
  7974. + && (h_dentry->d_flags
  7975. + & DCACHE_NFSFS_RENAMED)))
  7976. + )) {
  7977. + int h_unhashed;
  7978. +
  7979. + h_unhashed = d_unhashed(h_dentry);
  7980. + spin_unlock(&h_dentry->d_lock);
  7981. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7982. + unhashed, h_unhashed, dentry, h_dentry);
  7983. + goto err;
  7984. + }
  7985. + spin_unlock(&h_dentry->d_lock);
  7986. +
  7987. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7988. + if (unlikely(err))
  7989. + /* do not goto err, to keep the errno */
  7990. + break;
  7991. +
  7992. + /* todo: plink too? */
  7993. + if (!do_udba)
  7994. + continue;
  7995. +
  7996. + /* UDBA tests */
  7997. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7998. + goto err;
  7999. +
  8000. + h_inode = NULL;
  8001. + if (d_is_positive(h_dentry))
  8002. + h_inode = d_inode(h_dentry);
  8003. + h_plus = plus;
  8004. + h_mode = mode;
  8005. + h_cached_inode = h_inode;
  8006. + if (h_inode && bindex != bwh) {
  8007. + h_mode = (h_inode->i_mode & S_IFMT);
  8008. + h_plus = (vfsub_inode_nlink(h_inode, AU_I_BRANCH) > 0);
  8009. + }
  8010. + if (inode && ibs <= bindex && bindex <= ibe)
  8011. + h_cached_inode = au_h_iptr(inode, bindex);
  8012. +
  8013. + if (!h_nfs) {
  8014. + if (unlikely(plus != h_plus && !tmpfile))
  8015. + goto err;
  8016. + } else {
  8017. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  8018. + && !is_root
  8019. + && !IS_ROOT(h_dentry)
  8020. + && unhashed != d_unhashed(h_dentry)))
  8021. + goto err;
  8022. + }
  8023. + if (unlikely(mode != h_mode
  8024. + || h_cached_inode != h_inode))
  8025. + goto err;
  8026. + continue;
  8027. +
  8028. +err:
  8029. + err = -EINVAL;
  8030. + break;
  8031. + }
  8032. +
  8033. + AuTraceErr(err);
  8034. + return err;
  8035. +}
  8036. +
  8037. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  8038. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  8039. +{
  8040. + int err;
  8041. + struct dentry *parent;
  8042. +
  8043. + if (!au_digen_test(dentry, sigen))
  8044. + return 0;
  8045. +
  8046. + parent = dget_parent(dentry);
  8047. + di_read_lock_parent(parent, AuLock_IR);
  8048. + AuDebugOn(au_digen_test(parent, sigen));
  8049. + au_dbg_verify_gen(parent, sigen);
  8050. + err = au_refresh_dentry(dentry, parent);
  8051. + di_read_unlock(parent, AuLock_IR);
  8052. + dput(parent);
  8053. + AuTraceErr(err);
  8054. + return err;
  8055. +}
  8056. +
  8057. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  8058. +{
  8059. + int err;
  8060. + struct dentry *d, *parent;
  8061. +
  8062. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  8063. + return simple_reval_dpath(dentry, sigen);
  8064. +
  8065. + /* slow loop, keep it simple and stupid */
  8066. + /* cf: au_cpup_dirs() */
  8067. + err = 0;
  8068. + parent = NULL;
  8069. + while (au_digen_test(dentry, sigen)) {
  8070. + d = dentry;
  8071. + while (1) {
  8072. + dput(parent);
  8073. + parent = dget_parent(d);
  8074. + if (!au_digen_test(parent, sigen))
  8075. + break;
  8076. + d = parent;
  8077. + }
  8078. +
  8079. + if (d != dentry)
  8080. + di_write_lock_child2(d);
  8081. +
  8082. + /* someone might update our dentry while we were sleeping */
  8083. + if (au_digen_test(d, sigen)) {
  8084. + /*
  8085. + * todo: consolidate with simple_reval_dpath(),
  8086. + * do_refresh() and au_reval_for_attr().
  8087. + */
  8088. + di_read_lock_parent(parent, AuLock_IR);
  8089. + err = au_refresh_dentry(d, parent);
  8090. + di_read_unlock(parent, AuLock_IR);
  8091. + }
  8092. +
  8093. + if (d != dentry)
  8094. + di_write_unlock(d);
  8095. + dput(parent);
  8096. + if (unlikely(err))
  8097. + break;
  8098. + }
  8099. +
  8100. + return err;
  8101. +}
  8102. +
  8103. +/*
  8104. + * if valid returns 1, otherwise 0.
  8105. + */
  8106. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  8107. +{
  8108. + int valid, err;
  8109. + unsigned int sigen;
  8110. + unsigned char do_udba, dirren;
  8111. + struct super_block *sb;
  8112. + struct inode *inode;
  8113. +
  8114. + /* todo: support rcu-walk? */
  8115. + if (flags & LOOKUP_RCU)
  8116. + return -ECHILD;
  8117. +
  8118. + valid = 0;
  8119. + if (unlikely(!au_di(dentry)))
  8120. + goto out;
  8121. +
  8122. + valid = 1;
  8123. + sb = dentry->d_sb;
  8124. + /*
  8125. + * todo: very ugly
  8126. + * i_mutex of parent dir may be held,
  8127. + * but we should not return 'invalid' due to busy.
  8128. + */
  8129. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  8130. + if (unlikely(err)) {
  8131. + valid = err;
  8132. + AuTraceErr(err);
  8133. + goto out;
  8134. + }
  8135. + inode = NULL;
  8136. + if (d_really_is_positive(dentry))
  8137. + inode = d_inode(dentry);
  8138. + if (unlikely(inode && au_is_bad_inode(inode))) {
  8139. + err = -EINVAL;
  8140. + AuTraceErr(err);
  8141. + goto out_dgrade;
  8142. + }
  8143. + if (unlikely(au_dbrange_test(dentry))) {
  8144. + err = -EINVAL;
  8145. + AuTraceErr(err);
  8146. + goto out_dgrade;
  8147. + }
  8148. +
  8149. + sigen = au_sigen(sb);
  8150. + if (au_digen_test(dentry, sigen)) {
  8151. + AuDebugOn(IS_ROOT(dentry));
  8152. + err = au_reval_dpath(dentry, sigen);
  8153. + if (unlikely(err)) {
  8154. + AuTraceErr(err);
  8155. + goto out_dgrade;
  8156. + }
  8157. + }
  8158. + di_downgrade_lock(dentry, AuLock_IR);
  8159. +
  8160. + err = -EINVAL;
  8161. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  8162. + && inode
  8163. + && !(inode->i_state && I_LINKABLE)
  8164. + && (IS_DEADDIR(inode) || !vfsub_inode_nlink(inode, AU_I_AUFS))) {
  8165. + AuTraceErr(err);
  8166. + goto out_inval;
  8167. + }
  8168. +
  8169. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  8170. + if (do_udba && inode) {
  8171. + aufs_bindex_t btop = au_ibtop(inode);
  8172. + struct inode *h_inode;
  8173. +
  8174. + if (btop >= 0) {
  8175. + h_inode = au_h_iptr(inode, btop);
  8176. + if (h_inode && au_test_higen(inode, h_inode)) {
  8177. + AuTraceErr(err);
  8178. + goto out_inval;
  8179. + }
  8180. + }
  8181. + }
  8182. +
  8183. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  8184. + err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
  8185. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  8186. + err = -EIO;
  8187. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  8188. + dentry, err);
  8189. + }
  8190. + goto out_inval;
  8191. +
  8192. +out_dgrade:
  8193. + di_downgrade_lock(dentry, AuLock_IR);
  8194. +out_inval:
  8195. + aufs_read_unlock(dentry, AuLock_IR);
  8196. + AuTraceErr(err);
  8197. + valid = !err;
  8198. +out:
  8199. + if (!valid) {
  8200. + AuDbg("%pd invalid, %d\n", dentry, valid);
  8201. + d_drop(dentry);
  8202. + }
  8203. + return valid;
  8204. +}
  8205. +
  8206. +static void aufs_d_release(struct dentry *dentry)
  8207. +{
  8208. + if (au_di(dentry)) {
  8209. + au_di_fin(dentry);
  8210. + au_hn_di_reinit(dentry);
  8211. + }
  8212. +}
  8213. +
  8214. +const struct dentry_operations aufs_dop = {
  8215. + .d_revalidate = aufs_d_revalidate,
  8216. + .d_weak_revalidate = aufs_d_revalidate,
  8217. + .d_release = aufs_d_release
  8218. +};
  8219. +
  8220. +/* aufs_dop without d_revalidate */
  8221. +const struct dentry_operations aufs_dop_noreval = {
  8222. + .d_release = aufs_d_release
  8223. +};
  8224. diff -Naur null/fs/aufs/dentry.h linux-6.6.63/fs/aufs/dentry.h
  8225. --- /dev/null
  8226. +++ linux-6.6.63/fs/aufs/dentry.h 2024-12-18 15:12:59.294519601 +0300
  8227. @@ -0,0 +1,270 @@
  8228. +/* SPDX-License-Identifier: GPL-2.0 */
  8229. +/*
  8230. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  8231. + *
  8232. + * This program is free software; you can redistribute it and/or modify
  8233. + * it under the terms of the GNU General Public License as published by
  8234. + * the Free Software Foundation; either version 2 of the License, or
  8235. + * (at your option) any later version.
  8236. + *
  8237. + * This program is distributed in the hope that it will be useful,
  8238. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8239. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8240. + * GNU General Public License for more details.
  8241. + *
  8242. + * You should have received a copy of the GNU General Public License
  8243. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8244. + */
  8245. +
  8246. +/*
  8247. + * lookup and dentry operations
  8248. + */
  8249. +
  8250. +#ifndef __AUFS_DENTRY_H__
  8251. +#define __AUFS_DENTRY_H__
  8252. +
  8253. +#ifdef __KERNEL__
  8254. +
  8255. +#include <linux/dcache.h>
  8256. +#include "dirren.h"
  8257. +#include "rwsem.h"
  8258. +
  8259. +struct au_hdentry {
  8260. + struct dentry *hd_dentry;
  8261. + aufs_bindex_t hd_id;
  8262. +};
  8263. +
  8264. +struct au_dinfo {
  8265. + atomic_t di_generation;
  8266. +
  8267. + struct au_rwsem di_rwsem;
  8268. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  8269. + unsigned char di_tmpfile; /* to allow the different name */
  8270. + struct au_hdentry *di_hdentry;
  8271. + struct file *di_htmpfile;
  8272. + struct rcu_head rcu;
  8273. +} ____cacheline_aligned_in_smp;
  8274. +
  8275. +/* ---------------------------------------------------------------------- */
  8276. +
  8277. +/* flags for au_lkup_dentry() */
  8278. +#define AuLkup_ALLOW_NEG 1
  8279. +#define AuLkup_IGNORE_PERM (1 << 1)
  8280. +#define AuLkup_DIRREN (1 << 2)
  8281. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  8282. +#define au_fset_lkup(flags, name) \
  8283. + do { (flags) |= AuLkup_##name; } while (0)
  8284. +#define au_fclr_lkup(flags, name) \
  8285. + do { (flags) &= ~AuLkup_##name; } while (0)
  8286. +
  8287. +#ifndef CONFIG_AUFS_DIRREN
  8288. +#undef AuLkup_DIRREN
  8289. +#define AuLkup_DIRREN 0
  8290. +#endif
  8291. +
  8292. +struct au_do_lookup_args {
  8293. + unsigned int flags;
  8294. + mode_t type;
  8295. + struct qstr whname, *name;
  8296. + struct au_dr_lookup dirren;
  8297. +};
  8298. +
  8299. +/* ---------------------------------------------------------------------- */
  8300. +
  8301. +/* dentry.c */
  8302. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  8303. +struct au_branch;
  8304. +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
  8305. + struct path *ppath);
  8306. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  8307. + struct dentry *h_parent, struct au_branch *br);
  8308. +
  8309. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  8310. + unsigned int flags);
  8311. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  8312. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  8313. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  8314. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  8315. +
  8316. +/* dinfo.c */
  8317. +void au_di_init_once(void *_di);
  8318. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  8319. +void au_di_free(struct au_dinfo *dinfo);
  8320. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  8321. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  8322. +int au_di_init(struct dentry *dentry);
  8323. +void au_di_fin(struct dentry *dentry);
  8324. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
  8325. +
  8326. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  8327. +void di_read_unlock(struct dentry *d, int flags);
  8328. +void di_downgrade_lock(struct dentry *d, int flags);
  8329. +void di_write_lock(struct dentry *d, unsigned int lsc);
  8330. +void di_write_unlock(struct dentry *d);
  8331. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  8332. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  8333. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  8334. +
  8335. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  8336. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  8337. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  8338. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  8339. +
  8340. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8341. + struct dentry *h_dentry);
  8342. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  8343. +int au_dbrange_test(struct dentry *dentry);
  8344. +void au_update_digen(struct dentry *dentry);
  8345. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  8346. +void au_update_dbtop(struct dentry *dentry);
  8347. +void au_update_dbbot(struct dentry *dentry);
  8348. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  8349. +
  8350. +/* ---------------------------------------------------------------------- */
  8351. +
  8352. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  8353. +{
  8354. + return dentry->d_fsdata;
  8355. +}
  8356. +
  8357. +/* ---------------------------------------------------------------------- */
  8358. +
  8359. +/* lock subclass for dinfo */
  8360. +enum {
  8361. + AuLsc_DI_CHILD, /* child first */
  8362. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  8363. + AuLsc_DI_CHILD3, /* copyup dirs */
  8364. + AuLsc_DI_PARENT,
  8365. + AuLsc_DI_PARENT2,
  8366. + AuLsc_DI_PARENT3,
  8367. + AuLsc_DI_TMP /* temp for replacing dinfo */
  8368. +};
  8369. +
  8370. +/*
  8371. + * di_read_lock_child, di_write_lock_child,
  8372. + * di_read_lock_child2, di_write_lock_child2,
  8373. + * di_read_lock_child3, di_write_lock_child3,
  8374. + * di_read_lock_parent, di_write_lock_parent,
  8375. + * di_read_lock_parent2, di_write_lock_parent2,
  8376. + * di_read_lock_parent3, di_write_lock_parent3,
  8377. + */
  8378. +#define AuReadLockFunc(name, lsc) \
  8379. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  8380. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  8381. +
  8382. +#define AuWriteLockFunc(name, lsc) \
  8383. +static inline void di_write_lock_##name(struct dentry *d) \
  8384. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  8385. +
  8386. +#define AuRWLockFuncs(name, lsc) \
  8387. + AuReadLockFunc(name, lsc) \
  8388. + AuWriteLockFunc(name, lsc)
  8389. +
  8390. +AuRWLockFuncs(child, CHILD);
  8391. +AuRWLockFuncs(child2, CHILD2);
  8392. +AuRWLockFuncs(child3, CHILD3);
  8393. +AuRWLockFuncs(parent, PARENT);
  8394. +AuRWLockFuncs(parent2, PARENT2);
  8395. +AuRWLockFuncs(parent3, PARENT3);
  8396. +
  8397. +#undef AuReadLockFunc
  8398. +#undef AuWriteLockFunc
  8399. +#undef AuRWLockFuncs
  8400. +
  8401. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  8402. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  8403. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  8404. +
  8405. +/* ---------------------------------------------------------------------- */
  8406. +
  8407. +/* todo: memory barrier? */
  8408. +static inline unsigned int au_digen(struct dentry *d)
  8409. +{
  8410. + return atomic_read(&au_di(d)->di_generation);
  8411. +}
  8412. +
  8413. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  8414. +{
  8415. + hdentry->hd_dentry = NULL;
  8416. +}
  8417. +
  8418. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  8419. + aufs_bindex_t bindex)
  8420. +{
  8421. + return di->di_hdentry + bindex;
  8422. +}
  8423. +
  8424. +static inline void au_hdput(struct au_hdentry *hd)
  8425. +{
  8426. + if (hd)
  8427. + dput(hd->hd_dentry);
  8428. +}
  8429. +
  8430. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  8431. +{
  8432. + DiMustAnyLock(dentry);
  8433. + return au_di(dentry)->di_btop;
  8434. +}
  8435. +
  8436. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  8437. +{
  8438. + DiMustAnyLock(dentry);
  8439. + return au_di(dentry)->di_bbot;
  8440. +}
  8441. +
  8442. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  8443. +{
  8444. + DiMustAnyLock(dentry);
  8445. + return au_di(dentry)->di_bwh;
  8446. +}
  8447. +
  8448. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  8449. +{
  8450. + DiMustAnyLock(dentry);
  8451. + return au_di(dentry)->di_bdiropq;
  8452. +}
  8453. +
  8454. +/* todo: hard/soft set? */
  8455. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  8456. +{
  8457. + DiMustWriteLock(dentry);
  8458. + au_di(dentry)->di_btop = bindex;
  8459. +}
  8460. +
  8461. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  8462. +{
  8463. + DiMustWriteLock(dentry);
  8464. + au_di(dentry)->di_bbot = bindex;
  8465. +}
  8466. +
  8467. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  8468. +{
  8469. + DiMustWriteLock(dentry);
  8470. + /* dbwh can be outside of btop - bbot range */
  8471. + au_di(dentry)->di_bwh = bindex;
  8472. +}
  8473. +
  8474. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  8475. +{
  8476. + DiMustWriteLock(dentry);
  8477. + au_di(dentry)->di_bdiropq = bindex;
  8478. +}
  8479. +
  8480. +/* ---------------------------------------------------------------------- */
  8481. +
  8482. +#ifdef CONFIG_AUFS_HNOTIFY
  8483. +static inline void au_digen_dec(struct dentry *d)
  8484. +{
  8485. + atomic_dec(&au_di(d)->di_generation);
  8486. +}
  8487. +
  8488. +static inline void au_hn_di_reinit(struct dentry *dentry)
  8489. +{
  8490. + dentry->d_fsdata = NULL;
  8491. +}
  8492. +#else
  8493. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  8494. +#endif /* CONFIG_AUFS_HNOTIFY */
  8495. +
  8496. +#endif /* __KERNEL__ */
  8497. +#endif /* __AUFS_DENTRY_H__ */
  8498. diff -Naur null/fs/aufs/dinfo.c linux-6.6.63/fs/aufs/dinfo.c
  8499. --- /dev/null
  8500. +++ linux-6.6.63/fs/aufs/dinfo.c 2024-12-18 15:12:59.294519601 +0300
  8501. @@ -0,0 +1,555 @@
  8502. +// SPDX-License-Identifier: GPL-2.0
  8503. +/*
  8504. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  8505. + *
  8506. + * This program is free software; you can redistribute it and/or modify
  8507. + * it under the terms of the GNU General Public License as published by
  8508. + * the Free Software Foundation; either version 2 of the License, or
  8509. + * (at your option) any later version.
  8510. + *
  8511. + * This program is distributed in the hope that it will be useful,
  8512. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8513. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8514. + * GNU General Public License for more details.
  8515. + *
  8516. + * You should have received a copy of the GNU General Public License
  8517. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8518. + */
  8519. +
  8520. +/*
  8521. + * dentry private data
  8522. + */
  8523. +
  8524. +#include "aufs.h"
  8525. +
  8526. +void au_di_init_once(void *_dinfo)
  8527. +{
  8528. + struct au_dinfo *dinfo = _dinfo;
  8529. +
  8530. + au_rw_init(&dinfo->di_rwsem);
  8531. +}
  8532. +
  8533. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  8534. +{
  8535. + struct au_dinfo *dinfo;
  8536. + int nbr, i;
  8537. +
  8538. + dinfo = au_cache_alloc_dinfo();
  8539. + if (unlikely(!dinfo))
  8540. + goto out;
  8541. +
  8542. + nbr = au_sbbot(sb) + 1;
  8543. + if (nbr <= 0)
  8544. + nbr = 1;
  8545. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8546. + if (dinfo->di_hdentry) {
  8547. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8548. + dinfo->di_btop = -1;
  8549. + dinfo->di_bbot = -1;
  8550. + dinfo->di_bwh = -1;
  8551. + dinfo->di_bdiropq = -1;
  8552. + dinfo->di_tmpfile = 0;
  8553. + for (i = 0; i < nbr; i++)
  8554. + dinfo->di_hdentry[i].hd_id = -1;
  8555. + dinfo->di_htmpfile = NULL;
  8556. + goto out;
  8557. + }
  8558. +
  8559. + au_cache_free_dinfo(dinfo);
  8560. + dinfo = NULL;
  8561. +
  8562. +out:
  8563. + return dinfo;
  8564. +}
  8565. +
  8566. +void au_di_free(struct au_dinfo *dinfo)
  8567. +{
  8568. + struct au_hdentry *p;
  8569. + aufs_bindex_t bbot, bindex;
  8570. +
  8571. + /* dentry may not be revalidated */
  8572. + bindex = dinfo->di_btop;
  8573. + if (bindex >= 0) {
  8574. + bbot = dinfo->di_bbot;
  8575. + p = au_hdentry(dinfo, bindex);
  8576. + while (bindex++ <= bbot)
  8577. + au_hdput(p++);
  8578. + }
  8579. + au_kfree_try_rcu(dinfo->di_hdentry);
  8580. + au_cache_free_dinfo(dinfo);
  8581. +}
  8582. +
  8583. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8584. +{
  8585. + struct au_hdentry *p;
  8586. + aufs_bindex_t bi;
  8587. +
  8588. + AuRwMustWriteLock(&a->di_rwsem);
  8589. + AuRwMustWriteLock(&b->di_rwsem);
  8590. +
  8591. +#define DiSwap(v, name) \
  8592. + do { \
  8593. + v = a->di_##name; \
  8594. + a->di_##name = b->di_##name; \
  8595. + b->di_##name = v; \
  8596. + } while (0)
  8597. +
  8598. + DiSwap(p, hdentry);
  8599. + DiSwap(bi, btop);
  8600. + DiSwap(bi, bbot);
  8601. + DiSwap(bi, bwh);
  8602. + DiSwap(bi, bdiropq);
  8603. + /* smp_mb(); */
  8604. +
  8605. +#undef DiSwap
  8606. +}
  8607. +
  8608. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8609. +{
  8610. + AuRwMustWriteLock(&dst->di_rwsem);
  8611. + AuRwMustWriteLock(&src->di_rwsem);
  8612. +
  8613. + dst->di_btop = src->di_btop;
  8614. + dst->di_bbot = src->di_bbot;
  8615. + dst->di_bwh = src->di_bwh;
  8616. + dst->di_bdiropq = src->di_bdiropq;
  8617. + /* smp_mb(); */
  8618. +}
  8619. +
  8620. +int au_di_init(struct dentry *dentry)
  8621. +{
  8622. + int err;
  8623. + struct super_block *sb;
  8624. + struct au_dinfo *dinfo;
  8625. +
  8626. + err = 0;
  8627. + sb = dentry->d_sb;
  8628. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8629. + if (dinfo) {
  8630. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8631. + /* smp_mb(); */ /* atomic_set */
  8632. + dentry->d_fsdata = dinfo;
  8633. + } else
  8634. + err = -ENOMEM;
  8635. +
  8636. + return err;
  8637. +}
  8638. +
  8639. +void au_di_fin(struct dentry *dentry)
  8640. +{
  8641. + struct au_dinfo *dinfo;
  8642. +
  8643. + dinfo = au_di(dentry);
  8644. + AuRwDestroy(&dinfo->di_rwsem);
  8645. + au_di_free(dinfo);
  8646. +}
  8647. +
  8648. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
  8649. +{
  8650. + int err, sz;
  8651. + struct au_hdentry *hdp;
  8652. +
  8653. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8654. +
  8655. + err = -ENOMEM;
  8656. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8657. + if (!sz)
  8658. + sz = sizeof(*hdp);
  8659. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
  8660. + may_shrink);
  8661. + if (hdp) {
  8662. + dinfo->di_hdentry = hdp;
  8663. + err = 0;
  8664. + }
  8665. +
  8666. + return err;
  8667. +}
  8668. +
  8669. +/* ---------------------------------------------------------------------- */
  8670. +
  8671. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8672. +{
  8673. + switch (lsc) {
  8674. + case AuLsc_DI_CHILD:
  8675. + ii_write_lock_child(inode);
  8676. + break;
  8677. + case AuLsc_DI_CHILD2:
  8678. + ii_write_lock_child2(inode);
  8679. + break;
  8680. + case AuLsc_DI_CHILD3:
  8681. + ii_write_lock_child3(inode);
  8682. + break;
  8683. + case AuLsc_DI_PARENT:
  8684. + ii_write_lock_parent(inode);
  8685. + break;
  8686. + case AuLsc_DI_PARENT2:
  8687. + ii_write_lock_parent2(inode);
  8688. + break;
  8689. + case AuLsc_DI_PARENT3:
  8690. + ii_write_lock_parent3(inode);
  8691. + break;
  8692. + default:
  8693. + BUG();
  8694. + }
  8695. +}
  8696. +
  8697. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8698. +{
  8699. + switch (lsc) {
  8700. + case AuLsc_DI_CHILD:
  8701. + ii_read_lock_child(inode);
  8702. + break;
  8703. + case AuLsc_DI_CHILD2:
  8704. + ii_read_lock_child2(inode);
  8705. + break;
  8706. + case AuLsc_DI_CHILD3:
  8707. + ii_read_lock_child3(inode);
  8708. + break;
  8709. + case AuLsc_DI_PARENT:
  8710. + ii_read_lock_parent(inode);
  8711. + break;
  8712. + case AuLsc_DI_PARENT2:
  8713. + ii_read_lock_parent2(inode);
  8714. + break;
  8715. + case AuLsc_DI_PARENT3:
  8716. + ii_read_lock_parent3(inode);
  8717. + break;
  8718. + default:
  8719. + BUG();
  8720. + }
  8721. +}
  8722. +
  8723. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8724. +{
  8725. + struct inode *inode;
  8726. +
  8727. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8728. + if (d_really_is_positive(d)) {
  8729. + inode = d_inode(d);
  8730. + if (au_ftest_lock(flags, IW))
  8731. + do_ii_write_lock(inode, lsc);
  8732. + else if (au_ftest_lock(flags, IR))
  8733. + do_ii_read_lock(inode, lsc);
  8734. + }
  8735. +}
  8736. +
  8737. +void di_read_unlock(struct dentry *d, int flags)
  8738. +{
  8739. + struct inode *inode;
  8740. +
  8741. + if (d_really_is_positive(d)) {
  8742. + inode = d_inode(d);
  8743. + if (au_ftest_lock(flags, IW)) {
  8744. + au_dbg_verify_dinode(d);
  8745. + ii_write_unlock(inode);
  8746. + } else if (au_ftest_lock(flags, IR)) {
  8747. + au_dbg_verify_dinode(d);
  8748. + ii_read_unlock(inode);
  8749. + }
  8750. + }
  8751. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8752. +}
  8753. +
  8754. +void di_downgrade_lock(struct dentry *d, int flags)
  8755. +{
  8756. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8757. + ii_downgrade_lock(d_inode(d));
  8758. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8759. +}
  8760. +
  8761. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8762. +{
  8763. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8764. + if (d_really_is_positive(d))
  8765. + do_ii_write_lock(d_inode(d), lsc);
  8766. +}
  8767. +
  8768. +void di_write_unlock(struct dentry *d)
  8769. +{
  8770. + au_dbg_verify_dinode(d);
  8771. + if (d_really_is_positive(d))
  8772. + ii_write_unlock(d_inode(d));
  8773. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8774. +}
  8775. +
  8776. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8777. +{
  8778. + AuDebugOn(d1 == d2
  8779. + || d_inode(d1) == d_inode(d2)
  8780. + || d1->d_sb != d2->d_sb);
  8781. +
  8782. + if ((isdir && au_test_subdir(d1, d2))
  8783. + || d1 < d2) {
  8784. + di_write_lock_child(d1);
  8785. + di_write_lock_child2(d2);
  8786. + } else {
  8787. + di_write_lock_child(d2);
  8788. + di_write_lock_child2(d1);
  8789. + }
  8790. +}
  8791. +
  8792. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8793. +{
  8794. + AuDebugOn(d1 == d2
  8795. + || d_inode(d1) == d_inode(d2)
  8796. + || d1->d_sb != d2->d_sb);
  8797. +
  8798. + if ((isdir && au_test_subdir(d1, d2))
  8799. + || d1 < d2) {
  8800. + di_write_lock_parent(d1);
  8801. + di_write_lock_parent2(d2);
  8802. + } else {
  8803. + di_write_lock_parent(d2);
  8804. + di_write_lock_parent2(d1);
  8805. + }
  8806. +}
  8807. +
  8808. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8809. +{
  8810. + di_write_unlock(d1);
  8811. + if (d_inode(d1) == d_inode(d2))
  8812. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8813. + else
  8814. + di_write_unlock(d2);
  8815. +}
  8816. +
  8817. +/* ---------------------------------------------------------------------- */
  8818. +
  8819. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8820. +{
  8821. + struct dentry *d;
  8822. +
  8823. + DiMustAnyLock(dentry);
  8824. +
  8825. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8826. + return NULL;
  8827. + AuDebugOn(bindex < 0);
  8828. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8829. + AuDebugOn(d && au_dcount(d) <= 0);
  8830. + return d;
  8831. +}
  8832. +
  8833. +/*
  8834. + * extended version of au_h_dptr().
  8835. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8836. + * error.
  8837. + */
  8838. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8839. +{
  8840. + struct dentry *h_dentry;
  8841. + struct inode *inode, *h_inode;
  8842. +
  8843. + AuDebugOn(d_really_is_negative(dentry));
  8844. +
  8845. + h_dentry = NULL;
  8846. + if (au_dbtop(dentry) <= bindex
  8847. + && bindex <= au_dbbot(dentry))
  8848. + h_dentry = au_h_dptr(dentry, bindex);
  8849. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8850. + dget(h_dentry);
  8851. + goto out; /* success */
  8852. + }
  8853. +
  8854. + inode = d_inode(dentry);
  8855. + AuDebugOn(bindex < au_ibtop(inode));
  8856. + AuDebugOn(au_ibbot(inode) < bindex);
  8857. + h_inode = au_h_iptr(inode, bindex);
  8858. + h_dentry = d_find_alias(h_inode);
  8859. + if (h_dentry) {
  8860. + if (!IS_ERR(h_dentry)) {
  8861. + if (!au_d_linkable(h_dentry))
  8862. + goto out; /* success */
  8863. + dput(h_dentry);
  8864. + } else
  8865. + goto out;
  8866. + }
  8867. +
  8868. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8869. + h_dentry = au_plink_lkup(inode, bindex);
  8870. + AuDebugOn(!h_dentry);
  8871. + if (!IS_ERR(h_dentry)) {
  8872. + if (!au_d_hashed_positive(h_dentry))
  8873. + goto out; /* success */
  8874. + dput(h_dentry);
  8875. + h_dentry = NULL;
  8876. + }
  8877. + }
  8878. +
  8879. +out:
  8880. + AuDbgDentry(h_dentry);
  8881. + return h_dentry;
  8882. +}
  8883. +
  8884. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8885. +{
  8886. + aufs_bindex_t bbot, bwh;
  8887. +
  8888. + bbot = au_dbbot(dentry);
  8889. + if (0 <= bbot) {
  8890. + bwh = au_dbwh(dentry);
  8891. + if (!bwh)
  8892. + return bwh;
  8893. + if (0 < bwh && bwh < bbot)
  8894. + return bwh - 1;
  8895. + }
  8896. + return bbot;
  8897. +}
  8898. +
  8899. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8900. +{
  8901. + aufs_bindex_t bbot, bopq;
  8902. +
  8903. + bbot = au_dbtail(dentry);
  8904. + if (0 <= bbot) {
  8905. + bopq = au_dbdiropq(dentry);
  8906. + if (0 <= bopq && bopq < bbot)
  8907. + bbot = bopq;
  8908. + }
  8909. + return bbot;
  8910. +}
  8911. +
  8912. +/* ---------------------------------------------------------------------- */
  8913. +
  8914. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8915. + struct dentry *h_dentry)
  8916. +{
  8917. + struct au_dinfo *dinfo;
  8918. + struct au_hdentry *hd;
  8919. + struct au_branch *br;
  8920. +
  8921. + DiMustWriteLock(dentry);
  8922. +
  8923. + dinfo = au_di(dentry);
  8924. + hd = au_hdentry(dinfo, bindex);
  8925. + au_hdput(hd);
  8926. + hd->hd_dentry = h_dentry;
  8927. + if (h_dentry) {
  8928. + br = au_sbr(dentry->d_sb, bindex);
  8929. + hd->hd_id = br->br_id;
  8930. + }
  8931. +}
  8932. +
  8933. +int au_dbrange_test(struct dentry *dentry)
  8934. +{
  8935. + int err;
  8936. + aufs_bindex_t btop, bbot;
  8937. +
  8938. + err = 0;
  8939. + btop = au_dbtop(dentry);
  8940. + bbot = au_dbbot(dentry);
  8941. + if (btop >= 0)
  8942. + AuDebugOn(bbot < 0 && btop > bbot);
  8943. + else {
  8944. + err = -EIO;
  8945. + AuDebugOn(bbot >= 0);
  8946. + }
  8947. +
  8948. + return err;
  8949. +}
  8950. +
  8951. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8952. +{
  8953. + int err;
  8954. +
  8955. + err = 0;
  8956. + if (unlikely(au_digen(dentry) != sigen
  8957. + || au_iigen_test(d_inode(dentry), sigen)))
  8958. + err = -EIO;
  8959. +
  8960. + return err;
  8961. +}
  8962. +
  8963. +void au_update_digen(struct dentry *dentry)
  8964. +{
  8965. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8966. + /* smp_mb(); */ /* atomic_set */
  8967. +}
  8968. +
  8969. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8970. +{
  8971. + struct au_dinfo *dinfo;
  8972. + struct dentry *h_d;
  8973. + struct au_hdentry *hdp;
  8974. + aufs_bindex_t bindex, bbot;
  8975. +
  8976. + DiMustWriteLock(dentry);
  8977. +
  8978. + dinfo = au_di(dentry);
  8979. + if (!dinfo || dinfo->di_btop < 0)
  8980. + return;
  8981. +
  8982. + if (do_put_zero) {
  8983. + bbot = dinfo->di_bbot;
  8984. + bindex = dinfo->di_btop;
  8985. + hdp = au_hdentry(dinfo, bindex);
  8986. + for (; bindex <= bbot; bindex++, hdp++) {
  8987. + h_d = hdp->hd_dentry;
  8988. + if (h_d && d_is_negative(h_d))
  8989. + au_set_h_dptr(dentry, bindex, NULL);
  8990. + }
  8991. + }
  8992. +
  8993. + dinfo->di_btop = 0;
  8994. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8995. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8996. + if (hdp->hd_dentry)
  8997. + break;
  8998. + if (dinfo->di_btop > dinfo->di_bbot) {
  8999. + dinfo->di_btop = -1;
  9000. + dinfo->di_bbot = -1;
  9001. + return;
  9002. + }
  9003. +
  9004. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  9005. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  9006. + if (hdp->hd_dentry)
  9007. + break;
  9008. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  9009. +}
  9010. +
  9011. +void au_update_dbtop(struct dentry *dentry)
  9012. +{
  9013. + aufs_bindex_t bindex, bbot;
  9014. + struct dentry *h_dentry;
  9015. +
  9016. + bbot = au_dbbot(dentry);
  9017. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  9018. + h_dentry = au_h_dptr(dentry, bindex);
  9019. + if (!h_dentry)
  9020. + continue;
  9021. + if (d_is_positive(h_dentry)) {
  9022. + au_set_dbtop(dentry, bindex);
  9023. + return;
  9024. + }
  9025. + au_set_h_dptr(dentry, bindex, NULL);
  9026. + }
  9027. +}
  9028. +
  9029. +void au_update_dbbot(struct dentry *dentry)
  9030. +{
  9031. + aufs_bindex_t bindex, btop;
  9032. + struct dentry *h_dentry;
  9033. +
  9034. + btop = au_dbtop(dentry);
  9035. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  9036. + h_dentry = au_h_dptr(dentry, bindex);
  9037. + if (!h_dentry)
  9038. + continue;
  9039. + if (d_is_positive(h_dentry)) {
  9040. + au_set_dbbot(dentry, bindex);
  9041. + return;
  9042. + }
  9043. + au_set_h_dptr(dentry, bindex, NULL);
  9044. + }
  9045. +}
  9046. +
  9047. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  9048. +{
  9049. + aufs_bindex_t bindex, bbot;
  9050. +
  9051. + bbot = au_dbbot(dentry);
  9052. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  9053. + if (au_h_dptr(dentry, bindex) == h_dentry)
  9054. + return bindex;
  9055. + return -1;
  9056. +}
  9057. diff -Naur null/fs/aufs/dir.c linux-6.6.63/fs/aufs/dir.c
  9058. --- /dev/null
  9059. +++ linux-6.6.63/fs/aufs/dir.c 2024-12-18 15:12:59.294519601 +0300
  9060. @@ -0,0 +1,765 @@
  9061. +// SPDX-License-Identifier: GPL-2.0
  9062. +/*
  9063. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  9064. + *
  9065. + * This program is free software; you can redistribute it and/or modify
  9066. + * it under the terms of the GNU General Public License as published by
  9067. + * the Free Software Foundation; either version 2 of the License, or
  9068. + * (at your option) any later version.
  9069. + *
  9070. + * This program is distributed in the hope that it will be useful,
  9071. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9072. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9073. + * GNU General Public License for more details.
  9074. + *
  9075. + * You should have received a copy of the GNU General Public License
  9076. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9077. + */
  9078. +
  9079. +/*
  9080. + * directory operations
  9081. + */
  9082. +
  9083. +#include <linux/fs_stack.h>
  9084. +#include <linux/iversion.h>
  9085. +#include "aufs.h"
  9086. +
  9087. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  9088. +{
  9089. + unsigned int nlink, h_nlink;
  9090. +
  9091. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9092. +
  9093. + nlink = vfsub_inode_nlink(dir, AU_I_AUFS);
  9094. + h_nlink = vfsub_inode_nlink(h_dir, AU_I_BRANCH);
  9095. + nlink += h_nlink - 2;
  9096. + if (h_nlink < 2)
  9097. + nlink += 2;
  9098. + /* 0 can happen in revaliding */
  9099. + vfsub_set_nlink(dir, nlink);
  9100. +}
  9101. +
  9102. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  9103. +{
  9104. + unsigned int nlink, h_nlink;
  9105. +
  9106. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9107. +
  9108. + nlink = vfsub_inode_nlink(dir, AU_I_AUFS);
  9109. + h_nlink = vfsub_inode_nlink(h_dir, AU_I_BRANCH);
  9110. + nlink -= h_nlink - 2;
  9111. + if (h_nlink < 2)
  9112. + nlink -= 2;
  9113. + /* nlink == 0 means the branch-fs is broken */
  9114. + vfsub_set_nlink(dir, nlink);
  9115. +}
  9116. +
  9117. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  9118. +{
  9119. + loff_t sz;
  9120. + aufs_bindex_t bindex, bbot;
  9121. + struct file *h_file;
  9122. + struct dentry *h_dentry;
  9123. +
  9124. + sz = 0;
  9125. + if (file) {
  9126. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  9127. +
  9128. + bbot = au_fbbot_dir(file);
  9129. + for (bindex = au_fbtop(file);
  9130. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9131. + bindex++) {
  9132. + h_file = au_hf_dir(file, bindex);
  9133. + if (h_file && file_inode(h_file))
  9134. + sz += vfsub_f_size_read(h_file);
  9135. + }
  9136. + } else {
  9137. + AuDebugOn(!dentry);
  9138. + AuDebugOn(!d_is_dir(dentry));
  9139. +
  9140. + bbot = au_dbtaildir(dentry);
  9141. + for (bindex = au_dbtop(dentry);
  9142. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9143. + bindex++) {
  9144. + h_dentry = au_h_dptr(dentry, bindex);
  9145. + if (h_dentry && d_is_positive(h_dentry))
  9146. + sz += i_size_read(d_inode(h_dentry));
  9147. + }
  9148. + }
  9149. + if (sz < KMALLOC_MAX_SIZE)
  9150. + sz = roundup_pow_of_two(sz);
  9151. + if (sz > KMALLOC_MAX_SIZE)
  9152. + sz = KMALLOC_MAX_SIZE;
  9153. + else if (sz < NAME_MAX) {
  9154. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  9155. + sz = AUFS_RDBLK_DEF;
  9156. + }
  9157. + return sz;
  9158. +}
  9159. +
  9160. +struct au_dir_ts_arg {
  9161. + struct dentry *dentry;
  9162. + aufs_bindex_t brid;
  9163. +};
  9164. +
  9165. +static void au_do_dir_ts(void *arg)
  9166. +{
  9167. + struct au_dir_ts_arg *a = arg;
  9168. + struct au_dtime dt;
  9169. + struct path h_path;
  9170. + struct inode *dir, *h_dir;
  9171. + struct super_block *sb;
  9172. + struct au_branch *br;
  9173. + struct au_hinode *hdir;
  9174. + int err;
  9175. + aufs_bindex_t btop, bindex;
  9176. +
  9177. + sb = a->dentry->d_sb;
  9178. + if (d_really_is_negative(a->dentry))
  9179. + goto out;
  9180. + /* no dir->i_mutex lock */
  9181. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  9182. +
  9183. + dir = d_inode(a->dentry);
  9184. + btop = au_ibtop(dir);
  9185. + bindex = au_br_index(sb, a->brid);
  9186. + if (bindex < btop)
  9187. + goto out_unlock;
  9188. +
  9189. + br = au_sbr(sb, bindex);
  9190. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  9191. + if (!h_path.dentry)
  9192. + goto out_unlock;
  9193. + h_path.mnt = au_br_mnt(br);
  9194. + au_dtime_store(&dt, a->dentry, &h_path);
  9195. +
  9196. + br = au_sbr(sb, btop);
  9197. + if (!au_br_writable(br->br_perm))
  9198. + goto out_unlock;
  9199. + h_path.dentry = au_h_dptr(a->dentry, btop);
  9200. + h_path.mnt = au_br_mnt(br);
  9201. + err = vfsub_mnt_want_write(h_path.mnt);
  9202. + if (err)
  9203. + goto out_unlock;
  9204. + hdir = au_hi(dir, btop);
  9205. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  9206. + h_dir = au_h_iptr(dir, btop);
  9207. + if (vfsub_inode_nlink(h_dir, AU_I_BRANCH)
  9208. + && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  9209. + dt.dt_h_path = h_path;
  9210. + au_dtime_revert(&dt);
  9211. + }
  9212. + au_hn_inode_unlock(hdir);
  9213. + vfsub_mnt_drop_write(h_path.mnt);
  9214. + au_cpup_attr_timesizes(dir);
  9215. +
  9216. +out_unlock:
  9217. + aufs_read_unlock(a->dentry, AuLock_DW);
  9218. +out:
  9219. + dput(a->dentry);
  9220. + au_nwt_done(&au_sbi(sb)->si_nowait);
  9221. + au_kfree_try_rcu(arg);
  9222. +}
  9223. +
  9224. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  9225. +{
  9226. + int perm, wkq_err;
  9227. + aufs_bindex_t btop;
  9228. + struct au_dir_ts_arg *arg;
  9229. + struct dentry *dentry;
  9230. + struct super_block *sb;
  9231. +
  9232. + IMustLock(dir);
  9233. +
  9234. + dentry = d_find_any_alias(dir);
  9235. + AuDebugOn(!dentry);
  9236. + sb = dentry->d_sb;
  9237. + btop = au_ibtop(dir);
  9238. + if (btop == bindex) {
  9239. + au_cpup_attr_timesizes(dir);
  9240. + goto out;
  9241. + }
  9242. +
  9243. + perm = au_sbr_perm(sb, btop);
  9244. + if (!au_br_writable(perm))
  9245. + goto out;
  9246. +
  9247. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  9248. + if (!arg)
  9249. + goto out;
  9250. +
  9251. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  9252. + arg->brid = au_sbr_id(sb, bindex);
  9253. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  9254. + if (unlikely(wkq_err)) {
  9255. + pr_err("wkq %d\n", wkq_err);
  9256. + dput(dentry);
  9257. + au_kfree_try_rcu(arg);
  9258. + }
  9259. +
  9260. +out:
  9261. + dput(dentry);
  9262. +}
  9263. +
  9264. +/* ---------------------------------------------------------------------- */
  9265. +
  9266. +static int reopen_dir(struct file *file)
  9267. +{
  9268. + int err;
  9269. + unsigned int flags;
  9270. + aufs_bindex_t bindex, btail, btop;
  9271. + struct dentry *dentry, *h_dentry;
  9272. + struct file *h_file;
  9273. +
  9274. + /* open all lower dirs */
  9275. + dentry = file->f_path.dentry;
  9276. + btop = au_dbtop(dentry);
  9277. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  9278. + au_set_h_fptr(file, bindex, NULL);
  9279. + au_set_fbtop(file, btop);
  9280. +
  9281. + btail = au_dbtaildir(dentry);
  9282. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  9283. + au_set_h_fptr(file, bindex, NULL);
  9284. + au_set_fbbot_dir(file, btail);
  9285. +
  9286. + flags = vfsub_file_flags(file);
  9287. + for (bindex = btop; bindex <= btail; bindex++) {
  9288. + h_dentry = au_h_dptr(dentry, bindex);
  9289. + if (!h_dentry)
  9290. + continue;
  9291. + h_file = au_hf_dir(file, bindex);
  9292. + if (h_file)
  9293. + continue;
  9294. +
  9295. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9296. + err = PTR_ERR(h_file);
  9297. + if (IS_ERR(h_file))
  9298. + goto out; /* close all? */
  9299. + au_set_h_fptr(file, bindex, h_file);
  9300. + }
  9301. + au_update_figen(file);
  9302. + /* todo: necessary? */
  9303. + /* file->f_ra = h_file->f_ra; */
  9304. + err = 0;
  9305. +
  9306. +out:
  9307. + return err;
  9308. +}
  9309. +
  9310. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  9311. +{
  9312. + int err;
  9313. + aufs_bindex_t bindex, btail;
  9314. + struct dentry *dentry, *h_dentry;
  9315. + struct vfsmount *mnt;
  9316. +
  9317. + FiMustWriteLock(file);
  9318. + AuDebugOn(h_file);
  9319. +
  9320. + err = 0;
  9321. + mnt = file->f_path.mnt;
  9322. + dentry = file->f_path.dentry;
  9323. + file->f_version = inode_query_iversion(d_inode(dentry));
  9324. + bindex = au_dbtop(dentry);
  9325. + au_set_fbtop(file, bindex);
  9326. + btail = au_dbtaildir(dentry);
  9327. + au_set_fbbot_dir(file, btail);
  9328. + for (; !err && bindex <= btail; bindex++) {
  9329. + h_dentry = au_h_dptr(dentry, bindex);
  9330. + if (!h_dentry)
  9331. + continue;
  9332. +
  9333. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  9334. + if (unlikely(err))
  9335. + break;
  9336. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9337. + if (IS_ERR(h_file)) {
  9338. + err = PTR_ERR(h_file);
  9339. + break;
  9340. + }
  9341. + au_set_h_fptr(file, bindex, h_file);
  9342. + }
  9343. + au_update_figen(file);
  9344. + /* todo: necessary? */
  9345. + /* file->f_ra = h_file->f_ra; */
  9346. + if (!err)
  9347. + return 0; /* success */
  9348. +
  9349. + /* close all */
  9350. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  9351. + au_set_h_fptr(file, bindex, NULL);
  9352. + au_set_fbtop(file, -1);
  9353. + au_set_fbbot_dir(file, -1);
  9354. +
  9355. + return err;
  9356. +}
  9357. +
  9358. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  9359. + struct file *file)
  9360. +{
  9361. + int err;
  9362. + struct super_block *sb;
  9363. + struct au_fidir *fidir;
  9364. +
  9365. + err = -ENOMEM;
  9366. + sb = file->f_path.dentry->d_sb;
  9367. + si_read_lock(sb, AuLock_FLUSH);
  9368. + fidir = au_fidir_alloc(sb);
  9369. + if (fidir) {
  9370. + struct au_do_open_args args = {
  9371. + .open = do_open_dir,
  9372. + .fidir = fidir
  9373. + };
  9374. + err = au_do_open(file, &args);
  9375. + if (unlikely(err))
  9376. + au_kfree_rcu(fidir);
  9377. + }
  9378. + si_read_unlock(sb);
  9379. + return err;
  9380. +}
  9381. +
  9382. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  9383. + struct file *file)
  9384. +{
  9385. + struct au_vdir *vdir_cache;
  9386. + struct au_finfo *finfo;
  9387. + struct au_fidir *fidir;
  9388. + struct au_hfile *hf;
  9389. + aufs_bindex_t bindex, bbot;
  9390. +
  9391. + finfo = au_fi(file);
  9392. + fidir = finfo->fi_hdir;
  9393. + if (fidir) {
  9394. + au_hbl_del(&finfo->fi_hlist,
  9395. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  9396. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  9397. + if (vdir_cache)
  9398. + au_vdir_free(vdir_cache);
  9399. +
  9400. + bindex = finfo->fi_btop;
  9401. + if (bindex >= 0) {
  9402. + hf = fidir->fd_hfile + bindex;
  9403. + /*
  9404. + * calls fput() instead of filp_close(),
  9405. + * since no dnotify or lock for the lower file.
  9406. + */
  9407. + bbot = fidir->fd_bbot;
  9408. + for (; bindex <= bbot; bindex++, hf++)
  9409. + if (hf->hf_file)
  9410. + au_hfput(hf, /*execed*/0);
  9411. + }
  9412. + au_kfree_rcu(fidir);
  9413. + finfo->fi_hdir = NULL;
  9414. + }
  9415. + au_finfo_fin(file);
  9416. + return 0;
  9417. +}
  9418. +
  9419. +/* ---------------------------------------------------------------------- */
  9420. +
  9421. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  9422. +{
  9423. + int err;
  9424. + aufs_bindex_t bindex, bbot;
  9425. + struct file *h_file;
  9426. +
  9427. + err = 0;
  9428. + bbot = au_fbbot_dir(file);
  9429. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9430. + h_file = au_hf_dir(file, bindex);
  9431. + if (h_file)
  9432. + err = vfsub_flush(h_file, id);
  9433. + }
  9434. + return err;
  9435. +}
  9436. +
  9437. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  9438. +{
  9439. + return au_do_flush(file, id, au_do_flush_dir);
  9440. +}
  9441. +
  9442. +/* ---------------------------------------------------------------------- */
  9443. +
  9444. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  9445. +{
  9446. + int err;
  9447. + aufs_bindex_t bbot, bindex;
  9448. + struct inode *inode;
  9449. + struct super_block *sb;
  9450. +
  9451. + err = 0;
  9452. + sb = dentry->d_sb;
  9453. + inode = d_inode(dentry);
  9454. + IMustLock(inode);
  9455. + bbot = au_dbbot(dentry);
  9456. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  9457. + struct path h_path;
  9458. +
  9459. + if (au_test_ro(sb, bindex, inode))
  9460. + continue;
  9461. + h_path.dentry = au_h_dptr(dentry, bindex);
  9462. + if (!h_path.dentry)
  9463. + continue;
  9464. +
  9465. + h_path.mnt = au_sbr_mnt(sb, bindex);
  9466. + err = vfsub_fsync(NULL, &h_path, datasync);
  9467. + }
  9468. +
  9469. + return err;
  9470. +}
  9471. +
  9472. +static int au_do_fsync_dir(struct file *file, int datasync)
  9473. +{
  9474. + int err;
  9475. + aufs_bindex_t bbot, bindex;
  9476. + struct file *h_file;
  9477. + struct super_block *sb;
  9478. + struct inode *inode;
  9479. +
  9480. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9481. + if (unlikely(err))
  9482. + goto out;
  9483. +
  9484. + inode = file_inode(file);
  9485. + sb = inode->i_sb;
  9486. + bbot = au_fbbot_dir(file);
  9487. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9488. + h_file = au_hf_dir(file, bindex);
  9489. + if (!h_file || au_test_ro(sb, bindex, inode))
  9490. + continue;
  9491. +
  9492. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  9493. + }
  9494. +
  9495. +out:
  9496. + return err;
  9497. +}
  9498. +
  9499. +/*
  9500. + * @file may be NULL
  9501. + */
  9502. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  9503. + int datasync)
  9504. +{
  9505. + int err;
  9506. + struct dentry *dentry;
  9507. + struct inode *inode;
  9508. + struct super_block *sb;
  9509. +
  9510. + err = 0;
  9511. + dentry = file->f_path.dentry;
  9512. + inode = d_inode(dentry);
  9513. + inode_lock(inode);
  9514. + sb = dentry->d_sb;
  9515. + si_noflush_read_lock(sb);
  9516. + if (file)
  9517. + err = au_do_fsync_dir(file, datasync);
  9518. + else {
  9519. + di_write_lock_child(dentry);
  9520. + err = au_do_fsync_dir_no_file(dentry, datasync);
  9521. + }
  9522. + au_cpup_attr_timesizes(inode);
  9523. + di_write_unlock(dentry);
  9524. + if (file)
  9525. + fi_write_unlock(file);
  9526. +
  9527. + si_read_unlock(sb);
  9528. + inode_unlock(inode);
  9529. + return err;
  9530. +}
  9531. +
  9532. +/* ---------------------------------------------------------------------- */
  9533. +
  9534. +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
  9535. +{
  9536. + int err;
  9537. + struct dentry *dentry;
  9538. + struct inode *inode, *h_inode;
  9539. + struct super_block *sb;
  9540. +
  9541. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  9542. +
  9543. + dentry = file->f_path.dentry;
  9544. + inode = d_inode(dentry);
  9545. + IMustLock(inode);
  9546. +
  9547. + sb = dentry->d_sb;
  9548. + si_read_lock(sb, AuLock_FLUSH);
  9549. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9550. + if (unlikely(err))
  9551. + goto out;
  9552. + err = au_alive_dir(dentry);
  9553. + if (!err)
  9554. + err = au_vdir_init(file);
  9555. + di_downgrade_lock(dentry, AuLock_IR);
  9556. + if (unlikely(err))
  9557. + goto out_unlock;
  9558. +
  9559. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9560. + if (!au_test_nfsd()) {
  9561. + err = au_vdir_fill_de(file, ctx);
  9562. + fsstack_copy_attr_atime(inode, h_inode);
  9563. + } else {
  9564. + /*
  9565. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9566. + * encode_fh() and others.
  9567. + */
  9568. + atomic_inc(&h_inode->i_count);
  9569. + di_read_unlock(dentry, AuLock_IR);
  9570. + si_read_unlock(sb);
  9571. + err = au_vdir_fill_de(file, ctx);
  9572. + fsstack_copy_attr_atime(inode, h_inode);
  9573. + fi_write_unlock(file);
  9574. + iput(h_inode);
  9575. +
  9576. + AuTraceErr(err);
  9577. + return err;
  9578. + }
  9579. +
  9580. +out_unlock:
  9581. + di_read_unlock(dentry, AuLock_IR);
  9582. + fi_write_unlock(file);
  9583. +out:
  9584. + si_read_unlock(sb);
  9585. + return err;
  9586. +}
  9587. +
  9588. +/* ---------------------------------------------------------------------- */
  9589. +
  9590. +#define AuTestEmpty_WHONLY 1
  9591. +#define AuTestEmpty_CALLED (1 << 1)
  9592. +#define AuTestEmpty_SHWH (1 << 2)
  9593. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9594. +#define au_fset_testempty(flags, name) \
  9595. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9596. +#define au_fclr_testempty(flags, name) \
  9597. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9598. +
  9599. +#ifndef CONFIG_AUFS_SHWH
  9600. +#undef AuTestEmpty_SHWH
  9601. +#define AuTestEmpty_SHWH 0
  9602. +#endif
  9603. +
  9604. +struct test_empty_arg {
  9605. + struct dir_context ctx;
  9606. + struct au_nhash *whlist;
  9607. + unsigned int flags;
  9608. + int err;
  9609. + aufs_bindex_t bindex;
  9610. +};
  9611. +
  9612. +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
  9613. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9614. + unsigned int d_type)
  9615. +{
  9616. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9617. + ctx);
  9618. + char *name = (void *)__name;
  9619. +
  9620. + arg->err = 0;
  9621. + au_fset_testempty(arg->flags, CALLED);
  9622. + /* smp_mb(); */
  9623. + if (name[0] == '.'
  9624. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9625. + goto out; /* success */
  9626. +
  9627. + if (namelen <= AUFS_WH_PFX_LEN
  9628. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9629. + if (au_ftest_testempty(arg->flags, WHONLY)
  9630. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9631. + arg->err = -ENOTEMPTY;
  9632. + goto out;
  9633. + }
  9634. +
  9635. + name += AUFS_WH_PFX_LEN;
  9636. + namelen -= AUFS_WH_PFX_LEN;
  9637. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9638. + arg->err = au_nhash_append_wh
  9639. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9640. + au_ftest_testempty(arg->flags, SHWH));
  9641. +
  9642. +out:
  9643. + /* smp_mb(); */
  9644. + AuTraceErr(arg->err);
  9645. + return !arg->err;
  9646. +}
  9647. +
  9648. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9649. +{
  9650. + int err;
  9651. + struct file *h_file;
  9652. + struct au_branch *br;
  9653. +
  9654. + h_file = au_h_open(dentry, arg->bindex,
  9655. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9656. + /*file*/NULL, /*force_wr*/0);
  9657. + err = PTR_ERR(h_file);
  9658. + if (IS_ERR(h_file))
  9659. + goto out;
  9660. +
  9661. + err = 0;
  9662. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9663. + && !vfsub_inode_nlink(file_inode(h_file), AU_I_BRANCH))
  9664. + goto out_put;
  9665. +
  9666. + do {
  9667. + arg->err = 0;
  9668. + au_fclr_testempty(arg->flags, CALLED);
  9669. + /* smp_mb(); */
  9670. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9671. + if (err >= 0)
  9672. + err = arg->err;
  9673. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9674. +
  9675. +out_put:
  9676. + fput(h_file);
  9677. + br = au_sbr(dentry->d_sb, arg->bindex);
  9678. + au_lcnt_dec(&br->br_nfiles);
  9679. +out:
  9680. + return err;
  9681. +}
  9682. +
  9683. +struct do_test_empty_args {
  9684. + int *errp;
  9685. + struct dentry *dentry;
  9686. + struct test_empty_arg *arg;
  9687. +};
  9688. +
  9689. +static void call_do_test_empty(void *args)
  9690. +{
  9691. + struct do_test_empty_args *a = args;
  9692. + *a->errp = do_test_empty(a->dentry, a->arg);
  9693. +}
  9694. +
  9695. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9696. +{
  9697. + int err, wkq_err;
  9698. + struct dentry *h_dentry;
  9699. + struct inode *h_inode;
  9700. + struct mnt_idmap *h_idmap;
  9701. +
  9702. + h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
  9703. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9704. + h_inode = d_inode(h_dentry);
  9705. + /* todo: i_mode changes anytime? */
  9706. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  9707. + err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
  9708. + inode_unlock_shared(h_inode);
  9709. + if (!err)
  9710. + err = do_test_empty(dentry, arg);
  9711. + else {
  9712. + struct do_test_empty_args args = {
  9713. + .errp = &err,
  9714. + .dentry = dentry,
  9715. + .arg = arg
  9716. + };
  9717. + unsigned int flags = arg->flags;
  9718. +
  9719. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9720. + if (unlikely(wkq_err))
  9721. + err = wkq_err;
  9722. + arg->flags = flags;
  9723. + }
  9724. +
  9725. + return err;
  9726. +}
  9727. +
  9728. +int au_test_empty_lower(struct dentry *dentry)
  9729. +{
  9730. + int err;
  9731. + unsigned int rdhash;
  9732. + aufs_bindex_t bindex, btop, btail;
  9733. + struct au_nhash whlist;
  9734. + struct test_empty_arg arg = {
  9735. + .ctx = {
  9736. + .actor = test_empty_cb
  9737. + }
  9738. + };
  9739. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9740. +
  9741. + SiMustAnyLock(dentry->d_sb);
  9742. +
  9743. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9744. + if (!rdhash)
  9745. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9746. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9747. + if (unlikely(err))
  9748. + goto out;
  9749. +
  9750. + arg.flags = 0;
  9751. + arg.whlist = &whlist;
  9752. + btop = au_dbtop(dentry);
  9753. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9754. + au_fset_testempty(arg.flags, SHWH);
  9755. + test_empty = do_test_empty;
  9756. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9757. + test_empty = sio_test_empty;
  9758. + arg.bindex = btop;
  9759. + err = test_empty(dentry, &arg);
  9760. + if (unlikely(err))
  9761. + goto out_whlist;
  9762. +
  9763. + au_fset_testempty(arg.flags, WHONLY);
  9764. + btail = au_dbtaildir(dentry);
  9765. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9766. + struct dentry *h_dentry;
  9767. +
  9768. + h_dentry = au_h_dptr(dentry, bindex);
  9769. + if (h_dentry && d_is_positive(h_dentry)) {
  9770. + arg.bindex = bindex;
  9771. + err = test_empty(dentry, &arg);
  9772. + }
  9773. + }
  9774. +
  9775. +out_whlist:
  9776. + au_nhash_wh_free(&whlist);
  9777. +out:
  9778. + return err;
  9779. +}
  9780. +
  9781. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9782. +{
  9783. + int err;
  9784. + struct test_empty_arg arg = {
  9785. + .ctx = {
  9786. + .actor = test_empty_cb
  9787. + }
  9788. + };
  9789. + aufs_bindex_t bindex, btail;
  9790. +
  9791. + err = 0;
  9792. + arg.whlist = whlist;
  9793. + arg.flags = AuTestEmpty_WHONLY;
  9794. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9795. + au_fset_testempty(arg.flags, SHWH);
  9796. + btail = au_dbtaildir(dentry);
  9797. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9798. + struct dentry *h_dentry;
  9799. +
  9800. + h_dentry = au_h_dptr(dentry, bindex);
  9801. + if (h_dentry && d_is_positive(h_dentry)) {
  9802. + arg.bindex = bindex;
  9803. + err = sio_test_empty(dentry, &arg);
  9804. + }
  9805. + }
  9806. +
  9807. + return err;
  9808. +}
  9809. +
  9810. +/* ---------------------------------------------------------------------- */
  9811. +
  9812. +const struct file_operations aufs_dir_fop = {
  9813. + .owner = THIS_MODULE,
  9814. + .llseek = default_llseek,
  9815. + .read = generic_read_dir,
  9816. + .iterate_shared = aufs_iterate_shared,
  9817. + .unlocked_ioctl = aufs_ioctl_dir,
  9818. +#ifdef CONFIG_COMPAT
  9819. + .compat_ioctl = aufs_compat_ioctl_dir,
  9820. +#endif
  9821. + .open = aufs_open_dir,
  9822. + .release = aufs_release_dir,
  9823. + .flush = aufs_flush_dir,
  9824. + .fsync = aufs_fsync_dir
  9825. +};
  9826. diff -Naur null/fs/aufs/dir.h linux-6.6.63/fs/aufs/dir.h
  9827. --- /dev/null
  9828. +++ linux-6.6.63/fs/aufs/dir.h 2024-12-18 15:12:59.295519588 +0300
  9829. @@ -0,0 +1,134 @@
  9830. +/* SPDX-License-Identifier: GPL-2.0 */
  9831. +/*
  9832. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  9833. + *
  9834. + * This program is free software; you can redistribute it and/or modify
  9835. + * it under the terms of the GNU General Public License as published by
  9836. + * the Free Software Foundation; either version 2 of the License, or
  9837. + * (at your option) any later version.
  9838. + *
  9839. + * This program is distributed in the hope that it will be useful,
  9840. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9841. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9842. + * GNU General Public License for more details.
  9843. + *
  9844. + * You should have received a copy of the GNU General Public License
  9845. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9846. + */
  9847. +
  9848. +/*
  9849. + * directory operations
  9850. + */
  9851. +
  9852. +#ifndef __AUFS_DIR_H__
  9853. +#define __AUFS_DIR_H__
  9854. +
  9855. +#ifdef __KERNEL__
  9856. +
  9857. +#include <linux/fs.h>
  9858. +
  9859. +/* ---------------------------------------------------------------------- */
  9860. +
  9861. +/* need to be faster and smaller */
  9862. +
  9863. +struct au_nhash {
  9864. + unsigned int nh_num;
  9865. + struct hlist_head *nh_head;
  9866. +};
  9867. +
  9868. +struct au_vdir_destr {
  9869. + unsigned char len;
  9870. + unsigned char name[];
  9871. +} __packed;
  9872. +
  9873. +struct au_vdir_dehstr {
  9874. + struct hlist_node hash;
  9875. + struct au_vdir_destr *str;
  9876. + struct rcu_head rcu;
  9877. +} ____cacheline_aligned_in_smp;
  9878. +
  9879. +struct au_vdir_de {
  9880. + ino_t de_ino;
  9881. + unsigned char de_type;
  9882. + /* caution: packed */
  9883. + struct au_vdir_destr de_str;
  9884. +} __packed;
  9885. +
  9886. +struct au_vdir_wh {
  9887. + struct hlist_node wh_hash;
  9888. +#ifdef CONFIG_AUFS_SHWH
  9889. + ino_t wh_ino;
  9890. + aufs_bindex_t wh_bindex;
  9891. + unsigned char wh_type;
  9892. +#else
  9893. + aufs_bindex_t wh_bindex;
  9894. +#endif
  9895. + /* caution: packed */
  9896. + struct au_vdir_destr wh_str;
  9897. +} __packed;
  9898. +
  9899. +union au_vdir_deblk_p {
  9900. + unsigned char *deblk;
  9901. + struct au_vdir_de *de;
  9902. +};
  9903. +
  9904. +struct au_vdir {
  9905. + unsigned char **vd_deblk;
  9906. + unsigned long vd_nblk;
  9907. + struct {
  9908. + unsigned long ul;
  9909. + union au_vdir_deblk_p p;
  9910. + } vd_last;
  9911. +
  9912. + u64 vd_version;
  9913. + unsigned int vd_deblk_sz;
  9914. + unsigned long vd_jiffy;
  9915. + struct rcu_head rcu;
  9916. +} ____cacheline_aligned_in_smp;
  9917. +
  9918. +/* ---------------------------------------------------------------------- */
  9919. +
  9920. +/* dir.c */
  9921. +extern const struct file_operations aufs_dir_fop;
  9922. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9923. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9924. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9925. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9926. +int au_test_empty_lower(struct dentry *dentry);
  9927. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9928. +
  9929. +/* vdir.c */
  9930. +unsigned int au_rdhash_est(loff_t sz);
  9931. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9932. +void au_nhash_wh_free(struct au_nhash *whlist);
  9933. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9934. + int limit);
  9935. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9936. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9937. + unsigned int d_type, aufs_bindex_t bindex,
  9938. + unsigned char shwh);
  9939. +void au_vdir_free(struct au_vdir *vdir);
  9940. +int au_vdir_init(struct file *file);
  9941. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9942. +
  9943. +/* ioctl.c */
  9944. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9945. +
  9946. +#ifdef CONFIG_AUFS_RDU
  9947. +/* rdu.c */
  9948. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9949. +#ifdef CONFIG_COMPAT
  9950. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9951. + unsigned long arg);
  9952. +#endif
  9953. +#else
  9954. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9955. + unsigned int cmd, unsigned long arg)
  9956. +#ifdef CONFIG_COMPAT
  9957. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9958. + unsigned int cmd, unsigned long arg)
  9959. +#endif
  9960. +#endif
  9961. +
  9962. +#endif /* __KERNEL__ */
  9963. +#endif /* __AUFS_DIR_H__ */
  9964. diff -Naur null/fs/aufs/dirren.c linux-6.6.63/fs/aufs/dirren.c
  9965. --- /dev/null
  9966. +++ linux-6.6.63/fs/aufs/dirren.c 2024-12-18 15:12:59.295519588 +0300
  9967. @@ -0,0 +1,1315 @@
  9968. +// SPDX-License-Identifier: GPL-2.0
  9969. +/*
  9970. + * Copyright (C) 2017-2022 Junjiro R. Okajima
  9971. + *
  9972. + * This program is free software; you can redistribute it and/or modify
  9973. + * it under the terms of the GNU General Public License as published by
  9974. + * the Free Software Foundation; either version 2 of the License, or
  9975. + * (at your option) any later version.
  9976. + *
  9977. + * This program is distributed in the hope that it will be useful,
  9978. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9979. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9980. + * GNU General Public License for more details.
  9981. + *
  9982. + * You should have received a copy of the GNU General Public License
  9983. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9984. + */
  9985. +
  9986. +/*
  9987. + * special handling in renaming a directory
  9988. + * in order to support looking-up the before-renamed name on the lower readonly
  9989. + * branches
  9990. + */
  9991. +
  9992. +#include <linux/byteorder/generic.h>
  9993. +#include "aufs.h"
  9994. +
  9995. +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
  9996. +{
  9997. + int idx;
  9998. +
  9999. + idx = au_dr_ihash(ent->dr_h_ino);
  10000. + au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
  10001. +}
  10002. +
  10003. +static int au_dr_hino_test_empty(struct au_dr_br *dr)
  10004. +{
  10005. + int ret, i;
  10006. + struct hlist_bl_head *hbl;
  10007. +
  10008. + ret = 1;
  10009. + for (i = 0; ret && i < AuDirren_NHASH; i++) {
  10010. + hbl = dr->dr_h_ino + i;
  10011. + hlist_bl_lock(hbl);
  10012. + ret &= hlist_bl_empty(hbl);
  10013. + hlist_bl_unlock(hbl);
  10014. + }
  10015. +
  10016. + return ret;
  10017. +}
  10018. +
  10019. +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
  10020. +{
  10021. + struct au_dr_hino *found, *ent;
  10022. + struct hlist_bl_head *hbl;
  10023. + struct hlist_bl_node *pos;
  10024. + int idx;
  10025. +
  10026. + found = NULL;
  10027. + idx = au_dr_ihash(ino);
  10028. + hbl = dr->dr_h_ino + idx;
  10029. + hlist_bl_lock(hbl);
  10030. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  10031. + if (ent->dr_h_ino == ino) {
  10032. + found = ent;
  10033. + break;
  10034. + }
  10035. + hlist_bl_unlock(hbl);
  10036. +
  10037. + return found;
  10038. +}
  10039. +
  10040. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
  10041. + struct au_dr_hino *add_ent)
  10042. +{
  10043. + int found, idx;
  10044. + struct hlist_bl_head *hbl;
  10045. + struct hlist_bl_node *pos;
  10046. + struct au_dr_hino *ent;
  10047. +
  10048. + found = 0;
  10049. + idx = au_dr_ihash(ino);
  10050. + hbl = dr->dr_h_ino + idx;
  10051. +#if 0 /* debug print */
  10052. + {
  10053. + struct hlist_bl_node *tmp;
  10054. +
  10055. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  10056. + AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
  10057. + }
  10058. +#endif
  10059. + hlist_bl_lock(hbl);
  10060. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  10061. + if (ent->dr_h_ino == ino) {
  10062. + found = 1;
  10063. + break;
  10064. + }
  10065. + if (!found && add_ent)
  10066. + hlist_bl_add_head(&add_ent->dr_hnode, hbl);
  10067. + hlist_bl_unlock(hbl);
  10068. +
  10069. + if (!found && add_ent)
  10070. + AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
  10071. +
  10072. + return found;
  10073. +}
  10074. +
  10075. +void au_dr_hino_free(struct au_dr_br *dr)
  10076. +{
  10077. + int i;
  10078. + struct hlist_bl_head *hbl;
  10079. + struct hlist_bl_node *pos, *tmp;
  10080. + struct au_dr_hino *ent;
  10081. +
  10082. + /* SiMustWriteLock(sb); */
  10083. +
  10084. + for (i = 0; i < AuDirren_NHASH; i++) {
  10085. + hbl = dr->dr_h_ino + i;
  10086. + /* no spinlock since sbinfo must be write-locked */
  10087. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  10088. + au_kfree_rcu(ent);
  10089. + INIT_HLIST_BL_HEAD(hbl);
  10090. + }
  10091. +}
  10092. +
  10093. +/* returns the number of inodes or an error */
  10094. +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
  10095. + struct file *hinofile)
  10096. +{
  10097. + int err, i;
  10098. + ssize_t ssz;
  10099. + loff_t pos, oldsize;
  10100. + __be64 u64;
  10101. + struct inode *hinoinode;
  10102. + struct hlist_bl_head *hbl;
  10103. + struct hlist_bl_node *n1, *n2;
  10104. + struct au_dr_hino *ent;
  10105. +
  10106. + SiMustWriteLock(sb);
  10107. + AuDebugOn(!au_br_writable(br->br_perm));
  10108. +
  10109. + hinoinode = file_inode(hinofile);
  10110. + oldsize = i_size_read(hinoinode);
  10111. +
  10112. + err = 0;
  10113. + pos = 0;
  10114. + hbl = br->br_dirren.dr_h_ino;
  10115. + for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
  10116. + /* no bit-lock since sbinfo must be write-locked */
  10117. + hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
  10118. + AuDbg("hi%llu, %pD2\n",
  10119. + (unsigned long long)ent->dr_h_ino, hinofile);
  10120. + u64 = cpu_to_be64(ent->dr_h_ino);
  10121. + ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
  10122. + if (ssz == sizeof(u64))
  10123. + continue;
  10124. +
  10125. + /* write error */
  10126. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10127. + err = -ENOSPC;
  10128. + if (ssz < 0)
  10129. + err = ssz;
  10130. + break;
  10131. + }
  10132. + }
  10133. + /* regardless the error */
  10134. + if (pos < oldsize) {
  10135. + err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
  10136. + AuTraceErr(err);
  10137. + }
  10138. +
  10139. + AuTraceErr(err);
  10140. + return err;
  10141. +}
  10142. +
  10143. +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
  10144. +{
  10145. + int err, hidx;
  10146. + ssize_t ssz;
  10147. + size_t sz, n;
  10148. + loff_t pos;
  10149. + uint64_t u64;
  10150. + struct au_dr_hino *ent;
  10151. + struct inode *hinoinode;
  10152. + struct hlist_bl_head *hbl;
  10153. +
  10154. + err = 0;
  10155. + pos = 0;
  10156. + hbl = dr->dr_h_ino;
  10157. + hinoinode = file_inode(hinofile);
  10158. + sz = i_size_read(hinoinode);
  10159. + AuDebugOn(sz % sizeof(u64));
  10160. + n = sz / sizeof(u64);
  10161. + while (n--) {
  10162. + ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
  10163. + if (unlikely(ssz != sizeof(u64))) {
  10164. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10165. + err = -EINVAL;
  10166. + if (ssz < 0)
  10167. + err = ssz;
  10168. + goto out_free;
  10169. + }
  10170. +
  10171. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10172. + if (!ent) {
  10173. + err = -ENOMEM;
  10174. + AuTraceErr(err);
  10175. + goto out_free;
  10176. + }
  10177. + ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
  10178. + AuDbg("hi%llu, %pD2\n",
  10179. + (unsigned long long)ent->dr_h_ino, hinofile);
  10180. + hidx = au_dr_ihash(ent->dr_h_ino);
  10181. + au_hbl_add(&ent->dr_hnode, hbl + hidx);
  10182. + }
  10183. + goto out; /* success */
  10184. +
  10185. +out_free:
  10186. + au_dr_hino_free(dr);
  10187. +out:
  10188. + AuTraceErr(err);
  10189. + return err;
  10190. +}
  10191. +
  10192. +/*
  10193. + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
  10194. + * @path is a switch to distinguish load and store.
  10195. + */
  10196. +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
  10197. + struct au_branch *br, const struct path *path)
  10198. +{
  10199. + int err, flags;
  10200. + unsigned char load, suspend;
  10201. + struct file *hinofile;
  10202. + struct au_hinode *hdir;
  10203. + struct inode *dir, *delegated;
  10204. + struct path hinopath;
  10205. + struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
  10206. + sizeof(AUFS_WH_DR_BRHINO) - 1);
  10207. +
  10208. + AuDebugOn(bindex < 0 && !br);
  10209. + AuDebugOn(bindex >= 0 && br);
  10210. +
  10211. + err = -EINVAL;
  10212. + suspend = !br;
  10213. + if (suspend)
  10214. + br = au_sbr(sb, bindex);
  10215. + load = !!path;
  10216. + if (!load) {
  10217. + path = &br->br_path;
  10218. + AuDebugOn(!au_br_writable(br->br_perm));
  10219. + if (unlikely(!au_br_writable(br->br_perm)))
  10220. + goto out;
  10221. + }
  10222. +
  10223. + hdir = NULL;
  10224. + if (suspend) {
  10225. + dir = d_inode(sb->s_root);
  10226. + hdir = au_hinode(au_ii(dir), bindex);
  10227. + dir = hdir->hi_inode;
  10228. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10229. + } else {
  10230. + dir = d_inode(path->dentry);
  10231. + inode_lock_nested(dir, AuLsc_I_CHILD);
  10232. + }
  10233. + hinopath.mnt = path->mnt;
  10234. + hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
  10235. + err = PTR_ERR(hinopath.dentry);
  10236. + if (IS_ERR(hinopath.dentry))
  10237. + goto out_unlock;
  10238. +
  10239. + err = 0;
  10240. + flags = O_RDONLY;
  10241. + if (load) {
  10242. + if (d_is_negative(hinopath.dentry))
  10243. + goto out_dput; /* success */
  10244. + } else {
  10245. + if (au_dr_hino_test_empty(&br->br_dirren)) {
  10246. + if (d_is_positive(hinopath.dentry)) {
  10247. + delegated = NULL;
  10248. + err = vfsub_unlink(dir, &hinopath, &delegated,
  10249. + /*force*/0);
  10250. + AuTraceErr(err);
  10251. + if (unlikely(err))
  10252. + pr_err("ignored err %d, %pd2\n",
  10253. + err, hinopath.dentry);
  10254. + if (unlikely(err == -EWOULDBLOCK))
  10255. + iput(delegated);
  10256. + err = 0;
  10257. + }
  10258. + goto out_dput;
  10259. + } else if (!d_is_positive(hinopath.dentry)) {
  10260. + err = vfsub_create(dir, &hinopath, 0600,
  10261. + /*want_excl*/false);
  10262. + AuTraceErr(err);
  10263. + if (unlikely(err))
  10264. + goto out_dput;
  10265. + }
  10266. + flags = O_WRONLY;
  10267. + }
  10268. + hinofile = vfsub_dentry_open(&hinopath, flags);
  10269. + if (suspend)
  10270. + au_hn_inode_unlock(hdir);
  10271. + else
  10272. + inode_unlock(dir);
  10273. + dput(hinopath.dentry);
  10274. + AuTraceErrPtr(hinofile);
  10275. + if (IS_ERR(hinofile)) {
  10276. + err = PTR_ERR(hinofile);
  10277. + goto out;
  10278. + }
  10279. +
  10280. + if (load)
  10281. + err = au_dr_hino_load(&br->br_dirren, hinofile);
  10282. + else
  10283. + err = au_dr_hino_store(sb, br, hinofile);
  10284. + fput(hinofile);
  10285. + goto out;
  10286. +
  10287. +out_dput:
  10288. + dput(hinopath.dentry);
  10289. +out_unlock:
  10290. + if (suspend)
  10291. + au_hn_inode_unlock(hdir);
  10292. + else
  10293. + inode_unlock(dir);
  10294. +out:
  10295. + AuTraceErr(err);
  10296. + return err;
  10297. +}
  10298. +
  10299. +/* ---------------------------------------------------------------------- */
  10300. +
  10301. +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
  10302. +{
  10303. + int err;
  10304. + struct kstatfs kstfs;
  10305. + dev_t dev;
  10306. + struct dentry *dentry;
  10307. + struct super_block *sb;
  10308. +
  10309. + err = vfs_statfs((void *)path, &kstfs);
  10310. + AuTraceErr(err);
  10311. + if (unlikely(err))
  10312. + goto out;
  10313. +
  10314. + /* todo: support for UUID */
  10315. +
  10316. + if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
  10317. + brid->type = AuBrid_FSID;
  10318. + brid->fsid = kstfs.f_fsid;
  10319. + } else {
  10320. + dentry = path->dentry;
  10321. + sb = dentry->d_sb;
  10322. + dev = sb->s_dev;
  10323. + if (dev) {
  10324. + brid->type = AuBrid_DEV;
  10325. + brid->dev = dev;
  10326. + }
  10327. + }
  10328. +
  10329. +out:
  10330. + return err;
  10331. +}
  10332. +
  10333. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  10334. + const struct path *path)
  10335. +{
  10336. + int err, i;
  10337. + struct au_dr_br *dr;
  10338. + struct hlist_bl_head *hbl;
  10339. +
  10340. + dr = &br->br_dirren;
  10341. + hbl = dr->dr_h_ino;
  10342. + for (i = 0; i < AuDirren_NHASH; i++, hbl++)
  10343. + INIT_HLIST_BL_HEAD(hbl);
  10344. +
  10345. + err = au_dr_brid_init(&dr->dr_brid, path);
  10346. + if (unlikely(err))
  10347. + goto out;
  10348. +
  10349. + if (au_opt_test(au_mntflags(sb), DIRREN))
  10350. + err = au_dr_hino(sb, /*bindex*/-1, br, path);
  10351. +
  10352. +out:
  10353. + AuTraceErr(err);
  10354. + return err;
  10355. +}
  10356. +
  10357. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
  10358. +{
  10359. + int err;
  10360. +
  10361. + err = 0;
  10362. + if (au_br_writable(br->br_perm))
  10363. + err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
  10364. + if (!err)
  10365. + au_dr_hino_free(&br->br_dirren);
  10366. +
  10367. + return err;
  10368. +}
  10369. +
  10370. +/* ---------------------------------------------------------------------- */
  10371. +
  10372. +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
  10373. + char *buf, size_t sz)
  10374. +{
  10375. + int err;
  10376. + unsigned int major, minor;
  10377. + char *p;
  10378. +
  10379. + p = buf;
  10380. + err = snprintf(p, sz, "%d_", brid->type);
  10381. + AuDebugOn(err > sz);
  10382. + p += err;
  10383. + sz -= err;
  10384. + switch (brid->type) {
  10385. + case AuBrid_Unset:
  10386. + return -EINVAL;
  10387. + case AuBrid_UUID:
  10388. + err = snprintf(p, sz, "%pU", brid->uuid.b);
  10389. + break;
  10390. + case AuBrid_FSID:
  10391. + err = snprintf(p, sz, "%08x-%08x",
  10392. + brid->fsid.val[0], brid->fsid.val[1]);
  10393. + break;
  10394. + case AuBrid_DEV:
  10395. + major = MAJOR(brid->dev);
  10396. + minor = MINOR(brid->dev);
  10397. + if (major <= 0xff && minor <= 0xff)
  10398. + err = snprintf(p, sz, "%02x%02x", major, minor);
  10399. + else
  10400. + err = snprintf(p, sz, "%03x:%05x", major, minor);
  10401. + break;
  10402. + }
  10403. + AuDebugOn(err > sz);
  10404. + p += err;
  10405. + sz -= err;
  10406. + err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
  10407. + AuDebugOn(err > sz);
  10408. + p += err;
  10409. + sz -= err;
  10410. +
  10411. + return p - buf;
  10412. +}
  10413. +
  10414. +static int au_drinfo_name(struct au_branch *br, char *name, int len)
  10415. +{
  10416. + int rlen;
  10417. + struct dentry *br_dentry;
  10418. + struct inode *br_inode;
  10419. +
  10420. + br_dentry = au_br_dentry(br);
  10421. + br_inode = d_inode(br_dentry);
  10422. + rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
  10423. + AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
  10424. + AuDebugOn(rlen > len);
  10425. +
  10426. + return rlen;
  10427. +}
  10428. +
  10429. +/* ---------------------------------------------------------------------- */
  10430. +
  10431. +/*
  10432. + * from the given @h_dentry, construct drinfo at @*fdata.
  10433. + * when the size of @*fdata is not enough, reallocate and return new @fdata and
  10434. + * @allocated.
  10435. + */
  10436. +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
  10437. + struct dentry *h_dentry,
  10438. + unsigned char *allocated)
  10439. +{
  10440. + int err, v;
  10441. + struct au_drinfo_fdata *f, *p;
  10442. + struct au_drinfo *drinfo;
  10443. + struct inode *h_inode;
  10444. + struct qstr *qname;
  10445. +
  10446. + err = 0;
  10447. + f = *fdata;
  10448. + h_inode = d_inode(h_dentry);
  10449. + qname = &h_dentry->d_name;
  10450. + drinfo = &f->drinfo;
  10451. + drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
  10452. + drinfo->oldnamelen = qname->len;
  10453. + if (*allocated < sizeof(*f) + qname->len) {
  10454. + v = roundup_pow_of_two(*allocated + qname->len);
  10455. + p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
  10456. + if (unlikely(!p)) {
  10457. + err = -ENOMEM;
  10458. + AuTraceErr(err);
  10459. + goto out;
  10460. + }
  10461. + f = p;
  10462. + *fdata = f;
  10463. + *allocated = v;
  10464. + drinfo = &f->drinfo;
  10465. + }
  10466. + memcpy(drinfo->oldname, qname->name, qname->len);
  10467. + AuDbg("i%llu, %.*s\n",
  10468. + be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
  10469. + drinfo->oldname);
  10470. +
  10471. +out:
  10472. + AuTraceErr(err);
  10473. + return err;
  10474. +}
  10475. +
  10476. +/* callers have to free the return value */
  10477. +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
  10478. +{
  10479. + struct au_drinfo *ret, *drinfo;
  10480. + struct au_drinfo_fdata fdata;
  10481. + int len;
  10482. + loff_t pos;
  10483. + ssize_t ssz;
  10484. +
  10485. + ret = ERR_PTR(-EIO);
  10486. + pos = 0;
  10487. + ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
  10488. + if (unlikely(ssz != sizeof(fdata))) {
  10489. + AuIOErr("ssz %zd, %u, %pD2\n",
  10490. + ssz, (unsigned int)sizeof(fdata), file);
  10491. + goto out;
  10492. + }
  10493. +
  10494. + fdata.magic = ntohl((__force __be32)fdata.magic);
  10495. + switch (fdata.magic) {
  10496. + case AUFS_DRINFO_MAGIC_V1:
  10497. + break;
  10498. + default:
  10499. + AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
  10500. + fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
  10501. + goto out;
  10502. + }
  10503. +
  10504. + drinfo = &fdata.drinfo;
  10505. + len = drinfo->oldnamelen;
  10506. + if (!len) {
  10507. + AuIOErr("broken drinfo %pD2\n", file);
  10508. + goto out;
  10509. + }
  10510. +
  10511. + ret = NULL;
  10512. + drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
  10513. + if (unlikely(h_ino && drinfo->ino != h_ino)) {
  10514. + AuDbg("ignored i%llu, i%llu, %pD2\n",
  10515. + (unsigned long long)drinfo->ino,
  10516. + (unsigned long long)h_ino, file);
  10517. + goto out; /* success */
  10518. + }
  10519. +
  10520. + ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
  10521. + if (unlikely(!ret)) {
  10522. + ret = ERR_PTR(-ENOMEM);
  10523. + AuTraceErrPtr(ret);
  10524. + goto out;
  10525. + }
  10526. +
  10527. + *ret = *drinfo;
  10528. + ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
  10529. + if (unlikely(ssz != len)) {
  10530. + au_kfree_rcu(ret);
  10531. + ret = ERR_PTR(-EIO);
  10532. + AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
  10533. + goto out;
  10534. + }
  10535. +
  10536. + AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
  10537. +
  10538. +out:
  10539. + return ret;
  10540. +}
  10541. +
  10542. +/* ---------------------------------------------------------------------- */
  10543. +
  10544. +/* in order to be revertible */
  10545. +struct au_drinfo_rev_elm {
  10546. + int created;
  10547. + struct dentry *info_dentry;
  10548. + struct au_drinfo *info_last;
  10549. +};
  10550. +
  10551. +struct au_drinfo_rev {
  10552. + unsigned char already;
  10553. + aufs_bindex_t nelm;
  10554. + struct au_drinfo_rev_elm elm[];
  10555. +};
  10556. +
  10557. +/* todo: isn't it too large? */
  10558. +struct au_drinfo_store {
  10559. + struct path h_ppath;
  10560. + struct dentry *h_dentry;
  10561. + struct au_drinfo_fdata *fdata;
  10562. + char *infoname; /* inside of whname, just after PFX */
  10563. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
  10564. + aufs_bindex_t btgt, btail;
  10565. + unsigned char no_sio,
  10566. + allocated, /* current size of *fdata */
  10567. + infonamelen, /* room size for p */
  10568. + whnamelen, /* length of the generated name */
  10569. + renameback; /* renamed back */
  10570. +};
  10571. +
  10572. +/* on rename(2) error, the caller should revert it using @elm */
  10573. +static int au_drinfo_do_store(struct au_drinfo_store *w,
  10574. + struct au_drinfo_rev_elm *elm)
  10575. +{
  10576. + int err, len;
  10577. + ssize_t ssz;
  10578. + loff_t pos;
  10579. + struct path infopath = {
  10580. + .mnt = w->h_ppath.mnt
  10581. + };
  10582. + struct inode *h_dir, *h_inode, *delegated;
  10583. + struct file *infofile;
  10584. + struct qstr *qname;
  10585. +
  10586. + AuDebugOn(elm
  10587. + && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
  10588. +
  10589. + infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
  10590. + w->whnamelen);
  10591. + AuTraceErrPtr(infopath.dentry);
  10592. + if (IS_ERR(infopath.dentry)) {
  10593. + err = PTR_ERR(infopath.dentry);
  10594. + goto out;
  10595. + }
  10596. +
  10597. + err = 0;
  10598. + h_dir = d_inode(w->h_ppath.dentry);
  10599. + if (elm && d_is_negative(infopath.dentry)) {
  10600. + err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
  10601. + AuTraceErr(err);
  10602. + if (unlikely(err))
  10603. + goto out_dput;
  10604. + elm->created = 1;
  10605. + elm->info_dentry = dget(infopath.dentry);
  10606. + }
  10607. +
  10608. + infofile = vfsub_dentry_open(&infopath, O_RDWR);
  10609. + AuTraceErrPtr(infofile);
  10610. + if (IS_ERR(infofile)) {
  10611. + err = PTR_ERR(infofile);
  10612. + goto out_dput;
  10613. + }
  10614. +
  10615. + h_inode = d_inode(infopath.dentry);
  10616. + if (elm && i_size_read(h_inode)) {
  10617. + h_inode = d_inode(w->h_dentry);
  10618. + elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
  10619. + AuTraceErrPtr(elm->info_last);
  10620. + if (IS_ERR(elm->info_last)) {
  10621. + err = PTR_ERR(elm->info_last);
  10622. + elm->info_last = NULL;
  10623. + AuDebugOn(elm->info_dentry);
  10624. + goto out_fput;
  10625. + }
  10626. + }
  10627. +
  10628. + if (elm && w->renameback) {
  10629. + delegated = NULL;
  10630. + err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
  10631. + AuTraceErr(err);
  10632. + if (unlikely(err == -EWOULDBLOCK))
  10633. + iput(delegated);
  10634. + goto out_fput;
  10635. + }
  10636. +
  10637. + pos = 0;
  10638. + qname = &w->h_dentry->d_name;
  10639. + len = sizeof(*w->fdata) + qname->len;
  10640. + if (!elm)
  10641. + len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
  10642. + ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
  10643. + if (ssz == len) {
  10644. + AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
  10645. + w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
  10646. + goto out_fput; /* success */
  10647. + } else {
  10648. + err = -EIO;
  10649. + if (ssz < 0)
  10650. + err = ssz;
  10651. + /* the caller should revert it using @elm */
  10652. + }
  10653. +
  10654. +out_fput:
  10655. + fput(infofile);
  10656. +out_dput:
  10657. + dput(infopath.dentry);
  10658. +out:
  10659. + AuTraceErr(err);
  10660. + return err;
  10661. +}
  10662. +
  10663. +struct au_call_drinfo_do_store_args {
  10664. + int *errp;
  10665. + struct au_drinfo_store *w;
  10666. + struct au_drinfo_rev_elm *elm;
  10667. +};
  10668. +
  10669. +static void au_call_drinfo_do_store(void *args)
  10670. +{
  10671. + struct au_call_drinfo_do_store_args *a = args;
  10672. +
  10673. + *a->errp = au_drinfo_do_store(a->w, a->elm);
  10674. +}
  10675. +
  10676. +static int au_drinfo_store_sio(struct au_drinfo_store *w,
  10677. + struct au_drinfo_rev_elm *elm)
  10678. +{
  10679. + int err, wkq_err;
  10680. +
  10681. + if (w->no_sio)
  10682. + err = au_drinfo_do_store(w, elm);
  10683. + else {
  10684. + struct au_call_drinfo_do_store_args a = {
  10685. + .errp = &err,
  10686. + .w = w,
  10687. + .elm = elm
  10688. + };
  10689. + wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
  10690. + if (unlikely(wkq_err))
  10691. + err = wkq_err;
  10692. + }
  10693. + AuTraceErr(err);
  10694. +
  10695. + return err;
  10696. +}
  10697. +
  10698. +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
  10699. + aufs_bindex_t btgt)
  10700. +{
  10701. + int err;
  10702. +
  10703. + memset(w, 0, sizeof(*w));
  10704. + w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
  10705. + strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
  10706. + w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10707. + w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
  10708. + w->btgt = btgt;
  10709. + w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  10710. +
  10711. + err = -ENOMEM;
  10712. + w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
  10713. + if (unlikely(!w->fdata)) {
  10714. + AuTraceErr(err);
  10715. + goto out;
  10716. + }
  10717. + w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
  10718. + err = 0;
  10719. +
  10720. +out:
  10721. + return err;
  10722. +}
  10723. +
  10724. +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
  10725. +{
  10726. + au_kfree_rcu(w->fdata);
  10727. +}
  10728. +
  10729. +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
  10730. + struct au_drinfo_store *w)
  10731. +{
  10732. + struct au_drinfo_rev_elm *elm;
  10733. + struct inode *h_dir, *delegated;
  10734. + int err, nelm;
  10735. + struct path infopath = {
  10736. + .mnt = w->h_ppath.mnt
  10737. + };
  10738. +
  10739. + h_dir = d_inode(w->h_ppath.dentry);
  10740. + IMustLock(h_dir);
  10741. +
  10742. + err = 0;
  10743. + elm = rev->elm;
  10744. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10745. + AuDebugOn(elm->created && elm->info_last);
  10746. + if (elm->created) {
  10747. + AuDbg("here\n");
  10748. + delegated = NULL;
  10749. + infopath.dentry = elm->info_dentry;
  10750. + err = vfsub_unlink(h_dir, &infopath, &delegated,
  10751. + !w->no_sio);
  10752. + AuTraceErr(err);
  10753. + if (unlikely(err == -EWOULDBLOCK))
  10754. + iput(delegated);
  10755. + dput(elm->info_dentry);
  10756. + } else if (elm->info_last) {
  10757. + AuDbg("here\n");
  10758. + w->fdata->drinfo = *elm->info_last;
  10759. + memcpy(w->fdata->drinfo.oldname,
  10760. + elm->info_last->oldname,
  10761. + elm->info_last->oldnamelen);
  10762. + err = au_drinfo_store_sio(w, /*elm*/NULL);
  10763. + au_kfree_rcu(elm->info_last);
  10764. + }
  10765. + if (unlikely(err))
  10766. + AuIOErr("%d, %s\n", err, w->whname);
  10767. + /* go on even if err */
  10768. + }
  10769. +}
  10770. +
  10771. +/* caller has to call au_dr_rename_fin() later */
  10772. +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
  10773. + struct qstr *dst_name, void *_rev)
  10774. +{
  10775. + int err, sz, nelm;
  10776. + aufs_bindex_t bindex, btail;
  10777. + struct au_drinfo_store work;
  10778. + struct au_drinfo_rev *rev, **p;
  10779. + struct au_drinfo_rev_elm *elm;
  10780. + struct super_block *sb;
  10781. + struct au_branch *br;
  10782. + struct au_hinode *hdir;
  10783. +
  10784. + err = au_drinfo_store_work_init(&work, btgt);
  10785. + AuTraceErr(err);
  10786. + if (unlikely(err))
  10787. + goto out;
  10788. +
  10789. + err = -ENOMEM;
  10790. + btail = au_dbtaildir(dentry);
  10791. + nelm = btail - btgt;
  10792. + sz = sizeof(*rev) + sizeof(*elm) * nelm;
  10793. + rev = kcalloc(1, sz, GFP_NOFS);
  10794. + if (unlikely(!rev)) {
  10795. + AuTraceErr(err);
  10796. + goto out_args;
  10797. + }
  10798. + rev->nelm = nelm;
  10799. + elm = rev->elm;
  10800. + p = _rev;
  10801. + *p = rev;
  10802. +
  10803. + err = 0;
  10804. + sb = dentry->d_sb;
  10805. + work.h_ppath.dentry = au_h_dptr(dentry, btgt);
  10806. + work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  10807. + hdir = au_hi(d_inode(dentry), btgt);
  10808. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10809. + for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
  10810. + work.h_dentry = au_h_dptr(dentry, bindex);
  10811. + if (!work.h_dentry)
  10812. + continue;
  10813. +
  10814. + err = au_drinfo_construct(&work.fdata, work.h_dentry,
  10815. + &work.allocated);
  10816. + AuTraceErr(err);
  10817. + if (unlikely(err))
  10818. + break;
  10819. +
  10820. + work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
  10821. + br = au_sbr(sb, bindex);
  10822. + work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10823. + work.whnamelen += au_drinfo_name(br, work.infoname,
  10824. + work.infonamelen);
  10825. + AuDbg("whname %.*s, i%llu, %.*s\n",
  10826. + work.whnamelen, work.whname,
  10827. + be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
  10828. + work.fdata->drinfo.oldnamelen,
  10829. + work.fdata->drinfo.oldname);
  10830. +
  10831. + err = au_drinfo_store_sio(&work, elm);
  10832. + AuTraceErr(err);
  10833. + if (unlikely(err))
  10834. + break;
  10835. + }
  10836. + if (unlikely(err)) {
  10837. + /* revert all drinfo */
  10838. + au_drinfo_store_rev(rev, &work);
  10839. + au_kfree_try_rcu(rev);
  10840. + *p = NULL;
  10841. + }
  10842. + au_hn_inode_unlock(hdir);
  10843. +
  10844. +out_args:
  10845. + au_drinfo_store_work_fin(&work);
  10846. +out:
  10847. + return err;
  10848. +}
  10849. +
  10850. +/* ---------------------------------------------------------------------- */
  10851. +
  10852. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  10853. + struct qstr *dst_name, void *_rev)
  10854. +{
  10855. + int err, already;
  10856. + ino_t ino;
  10857. + struct super_block *sb;
  10858. + struct au_branch *br;
  10859. + struct au_dr_br *dr;
  10860. + struct dentry *h_dentry;
  10861. + struct inode *h_inode;
  10862. + struct au_dr_hino *ent;
  10863. + struct au_drinfo_rev *rev, **p;
  10864. +
  10865. + AuDbg("bindex %d\n", bindex);
  10866. +
  10867. + err = -ENOMEM;
  10868. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10869. + if (unlikely(!ent))
  10870. + goto out;
  10871. +
  10872. + sb = src->d_sb;
  10873. + br = au_sbr(sb, bindex);
  10874. + dr = &br->br_dirren;
  10875. + h_dentry = au_h_dptr(src, bindex);
  10876. + h_inode = d_inode(h_dentry);
  10877. + ino = h_inode->i_ino;
  10878. + ent->dr_h_ino = ino;
  10879. + already = au_dr_hino_test_add(dr, ino, ent);
  10880. + AuDbg("b%d, hi%llu, already %d\n",
  10881. + bindex, (unsigned long long)ino, already);
  10882. +
  10883. + err = au_drinfo_store(src, bindex, dst_name, _rev);
  10884. + AuTraceErr(err);
  10885. + if (!err) {
  10886. + p = _rev;
  10887. + rev = *p;
  10888. + rev->already = already;
  10889. + goto out; /* success */
  10890. + }
  10891. +
  10892. + /* revert */
  10893. + if (!already)
  10894. + au_dr_hino_del(dr, ent);
  10895. + au_kfree_rcu(ent);
  10896. +
  10897. +out:
  10898. + AuTraceErr(err);
  10899. + return err;
  10900. +}
  10901. +
  10902. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10903. +{
  10904. + struct au_drinfo_rev *rev;
  10905. + struct au_drinfo_rev_elm *elm;
  10906. + int nelm;
  10907. +
  10908. + rev = _rev;
  10909. + elm = rev->elm;
  10910. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10911. + dput(elm->info_dentry);
  10912. + au_kfree_rcu(elm->info_last);
  10913. + }
  10914. + au_kfree_try_rcu(rev);
  10915. +}
  10916. +
  10917. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10918. +{
  10919. + int err;
  10920. + struct au_drinfo_store work;
  10921. + struct au_drinfo_rev *rev = _rev;
  10922. + struct super_block *sb;
  10923. + struct au_branch *br;
  10924. + struct inode *h_inode;
  10925. + struct au_dr_br *dr;
  10926. + struct au_dr_hino *ent;
  10927. +
  10928. + err = au_drinfo_store_work_init(&work, btgt);
  10929. + if (unlikely(err))
  10930. + goto out;
  10931. +
  10932. + sb = src->d_sb;
  10933. + br = au_sbr(sb, btgt);
  10934. + work.h_ppath.dentry = au_h_dptr(src, btgt);
  10935. + work.h_ppath.mnt = au_br_mnt(br);
  10936. + au_drinfo_store_rev(rev, &work);
  10937. + au_drinfo_store_work_fin(&work);
  10938. + if (rev->already)
  10939. + goto out;
  10940. +
  10941. + dr = &br->br_dirren;
  10942. + h_inode = d_inode(work.h_ppath.dentry);
  10943. + ent = au_dr_hino_find(dr, h_inode->i_ino);
  10944. + BUG_ON(!ent);
  10945. + au_dr_hino_del(dr, ent);
  10946. + au_kfree_rcu(ent);
  10947. +
  10948. +out:
  10949. + au_kfree_try_rcu(rev);
  10950. + if (unlikely(err))
  10951. + pr_err("failed to remove dirren info\n");
  10952. +}
  10953. +
  10954. +/* ---------------------------------------------------------------------- */
  10955. +
  10956. +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
  10957. + char *whname, int whnamelen,
  10958. + struct dentry **info_dentry)
  10959. +{
  10960. + struct au_drinfo *drinfo;
  10961. + struct file *f;
  10962. + struct inode *h_dir;
  10963. + struct path infopath;
  10964. + int unlocked;
  10965. +
  10966. + AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
  10967. +
  10968. + *info_dentry = NULL;
  10969. + drinfo = NULL;
  10970. + unlocked = 0;
  10971. + h_dir = d_inode(h_ppath->dentry);
  10972. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  10973. + infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
  10974. + if (IS_ERR(infopath.dentry)) {
  10975. + drinfo = (void *)infopath.dentry;
  10976. + goto out;
  10977. + }
  10978. +
  10979. + if (d_is_negative(infopath.dentry))
  10980. + goto out_dput; /* success */
  10981. +
  10982. + infopath.mnt = h_ppath->mnt;
  10983. + f = vfsub_dentry_open(&infopath, O_RDONLY);
  10984. + inode_unlock_shared(h_dir);
  10985. + unlocked = 1;
  10986. + if (IS_ERR(f)) {
  10987. + drinfo = (void *)f;
  10988. + goto out_dput;
  10989. + }
  10990. +
  10991. + drinfo = au_drinfo_read_k(f, /*h_ino*/0);
  10992. + if (IS_ERR_OR_NULL(drinfo))
  10993. + goto out_fput;
  10994. +
  10995. + AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
  10996. + *info_dentry = dget(infopath.dentry); /* keep it alive */
  10997. +
  10998. +out_fput:
  10999. + fput(f);
  11000. +out_dput:
  11001. + dput(infopath.dentry);
  11002. +out:
  11003. + if (!unlocked)
  11004. + inode_unlock_shared(h_dir);
  11005. + AuTraceErrPtr(drinfo);
  11006. + return drinfo;
  11007. +}
  11008. +
  11009. +struct au_drinfo_do_load_args {
  11010. + struct au_drinfo **drinfop;
  11011. + struct path *h_ppath;
  11012. + char *whname;
  11013. + int whnamelen;
  11014. + struct dentry **info_dentry;
  11015. +};
  11016. +
  11017. +static void au_call_drinfo_do_load(void *args)
  11018. +{
  11019. + struct au_drinfo_do_load_args *a = args;
  11020. +
  11021. + *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
  11022. + a->info_dentry);
  11023. +}
  11024. +
  11025. +struct au_drinfo_load {
  11026. + struct path h_ppath;
  11027. + struct qstr *qname;
  11028. + unsigned char no_sio;
  11029. +
  11030. + aufs_bindex_t ninfo;
  11031. + struct au_drinfo **drinfo;
  11032. +};
  11033. +
  11034. +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
  11035. + struct au_branch *br)
  11036. +{
  11037. + int err, wkq_err, whnamelen, e;
  11038. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
  11039. + = AUFS_WH_DR_INFO_PFX;
  11040. + struct au_drinfo *drinfo;
  11041. + struct qstr oldname;
  11042. + struct inode *h_dir, *delegated;
  11043. + struct dentry *info_dentry;
  11044. + struct path infopath;
  11045. +
  11046. + whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  11047. + whnamelen += au_drinfo_name(br, whname + whnamelen,
  11048. + sizeof(whname) - whnamelen);
  11049. + if (w->no_sio)
  11050. + drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
  11051. + &info_dentry);
  11052. + else {
  11053. + struct au_drinfo_do_load_args args = {
  11054. + .drinfop = &drinfo,
  11055. + .h_ppath = &w->h_ppath,
  11056. + .whname = whname,
  11057. + .whnamelen = whnamelen,
  11058. + .info_dentry = &info_dentry
  11059. + };
  11060. + wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
  11061. + if (unlikely(wkq_err))
  11062. + drinfo = ERR_PTR(wkq_err);
  11063. + }
  11064. + err = PTR_ERR(drinfo);
  11065. + if (IS_ERR_OR_NULL(drinfo))
  11066. + goto out;
  11067. +
  11068. + err = 0;
  11069. + oldname.len = drinfo->oldnamelen;
  11070. + oldname.name = drinfo->oldname;
  11071. + if (au_qstreq(w->qname, &oldname)) {
  11072. + /* the name is renamed back */
  11073. + au_kfree_rcu(drinfo);
  11074. + drinfo = NULL;
  11075. +
  11076. + infopath.dentry = info_dentry;
  11077. + infopath.mnt = w->h_ppath.mnt;
  11078. + h_dir = d_inode(w->h_ppath.dentry);
  11079. + delegated = NULL;
  11080. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  11081. + e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
  11082. + inode_unlock(h_dir);
  11083. + if (unlikely(e))
  11084. + AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
  11085. + if (unlikely(e == -EWOULDBLOCK))
  11086. + iput(delegated);
  11087. + }
  11088. + au_kfree_rcu(w->drinfo[bindex]);
  11089. + w->drinfo[bindex] = drinfo;
  11090. + dput(info_dentry);
  11091. +
  11092. +out:
  11093. + AuTraceErr(err);
  11094. + return err;
  11095. +}
  11096. +
  11097. +/* ---------------------------------------------------------------------- */
  11098. +
  11099. +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
  11100. +{
  11101. + struct au_drinfo **p = drinfo;
  11102. +
  11103. + while (n-- > 0)
  11104. + au_kfree_rcu(*drinfo++);
  11105. + au_kfree_try_rcu(p);
  11106. +}
  11107. +
  11108. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11109. + aufs_bindex_t btgt)
  11110. +{
  11111. + int err, ninfo;
  11112. + struct au_drinfo_load w;
  11113. + aufs_bindex_t bindex, bbot;
  11114. + struct au_branch *br;
  11115. + struct inode *h_dir;
  11116. + struct au_dr_hino *ent;
  11117. + struct super_block *sb;
  11118. +
  11119. + AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
  11120. + AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
  11121. + AuLNPair(&lkup->whname), btgt);
  11122. +
  11123. + sb = dentry->d_sb;
  11124. + bbot = au_sbbot(sb);
  11125. + w.ninfo = bbot + 1;
  11126. + if (!lkup->dirren.drinfo) {
  11127. + lkup->dirren.drinfo = kcalloc(w.ninfo,
  11128. + sizeof(*lkup->dirren.drinfo),
  11129. + GFP_NOFS);
  11130. + if (unlikely(!lkup->dirren.drinfo)) {
  11131. + err = -ENOMEM;
  11132. + goto out;
  11133. + }
  11134. + lkup->dirren.ninfo = w.ninfo;
  11135. + }
  11136. + w.drinfo = lkup->dirren.drinfo;
  11137. + w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  11138. + w.h_ppath.dentry = au_h_dptr(dentry, btgt);
  11139. + AuDebugOn(!w.h_ppath.dentry);
  11140. + w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  11141. + w.qname = &dentry->d_name;
  11142. +
  11143. + ninfo = 0;
  11144. + for (bindex = btgt + 1; bindex <= bbot; bindex++) {
  11145. + br = au_sbr(sb, bindex);
  11146. + err = au_drinfo_load(&w, bindex, br);
  11147. + if (unlikely(err))
  11148. + goto out_free;
  11149. + if (w.drinfo[bindex])
  11150. + ninfo++;
  11151. + }
  11152. + if (!ninfo) {
  11153. + br = au_sbr(sb, btgt);
  11154. + h_dir = d_inode(w.h_ppath.dentry);
  11155. + ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
  11156. + AuDebugOn(!ent);
  11157. + au_dr_hino_del(&br->br_dirren, ent);
  11158. + au_kfree_rcu(ent);
  11159. + }
  11160. + goto out; /* success */
  11161. +
  11162. +out_free:
  11163. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11164. + lkup->dirren.ninfo = 0;
  11165. + lkup->dirren.drinfo = NULL;
  11166. +out:
  11167. + AuTraceErr(err);
  11168. + return err;
  11169. +}
  11170. +
  11171. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
  11172. +{
  11173. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11174. +}
  11175. +
  11176. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
  11177. +{
  11178. + int err;
  11179. + struct au_drinfo *drinfo;
  11180. +
  11181. + err = 0;
  11182. + if (!lkup->dirren.drinfo)
  11183. + goto out;
  11184. + AuDebugOn(lkup->dirren.ninfo <= btgt);
  11185. + drinfo = lkup->dirren.drinfo[btgt];
  11186. + if (!drinfo)
  11187. + goto out;
  11188. +
  11189. + au_kfree_try_rcu(lkup->whname.name);
  11190. + lkup->whname.name = NULL;
  11191. + lkup->dirren.dr_name.len = drinfo->oldnamelen;
  11192. + lkup->dirren.dr_name.name = drinfo->oldname;
  11193. + lkup->name = &lkup->dirren.dr_name;
  11194. + err = au_wh_name_alloc(&lkup->whname, lkup->name);
  11195. + if (!err)
  11196. + AuDbg("name %.*s, whname %.*s, b%d\n",
  11197. + AuLNPair(lkup->name), AuLNPair(&lkup->whname),
  11198. + btgt);
  11199. +
  11200. +out:
  11201. + AuTraceErr(err);
  11202. + return err;
  11203. +}
  11204. +
  11205. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11206. + ino_t h_ino)
  11207. +{
  11208. + int match;
  11209. + struct au_drinfo *drinfo;
  11210. +
  11211. + match = 1;
  11212. + if (!lkup->dirren.drinfo)
  11213. + goto out;
  11214. + AuDebugOn(lkup->dirren.ninfo <= bindex);
  11215. + drinfo = lkup->dirren.drinfo[bindex];
  11216. + if (!drinfo)
  11217. + goto out;
  11218. +
  11219. + match = (drinfo->ino == h_ino);
  11220. + AuDbg("match %d\n", match);
  11221. +
  11222. +out:
  11223. + return match;
  11224. +}
  11225. +
  11226. +/* ---------------------------------------------------------------------- */
  11227. +
  11228. +int au_dr_opt_set(struct super_block *sb)
  11229. +{
  11230. + int err;
  11231. + aufs_bindex_t bindex, bbot;
  11232. + struct au_branch *br;
  11233. +
  11234. + err = 0;
  11235. + bbot = au_sbbot(sb);
  11236. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11237. + br = au_sbr(sb, bindex);
  11238. + err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
  11239. + }
  11240. +
  11241. + return err;
  11242. +}
  11243. +
  11244. +int au_dr_opt_flush(struct super_block *sb)
  11245. +{
  11246. + int err;
  11247. + aufs_bindex_t bindex, bbot;
  11248. + struct au_branch *br;
  11249. +
  11250. + err = 0;
  11251. + bbot = au_sbbot(sb);
  11252. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11253. + br = au_sbr(sb, bindex);
  11254. + if (au_br_writable(br->br_perm))
  11255. + err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
  11256. + }
  11257. +
  11258. + return err;
  11259. +}
  11260. +
  11261. +int au_dr_opt_clr(struct super_block *sb, int no_flush)
  11262. +{
  11263. + int err;
  11264. + aufs_bindex_t bindex, bbot;
  11265. + struct au_branch *br;
  11266. +
  11267. + err = 0;
  11268. + if (!no_flush) {
  11269. + err = au_dr_opt_flush(sb);
  11270. + if (unlikely(err))
  11271. + goto out;
  11272. + }
  11273. +
  11274. + bbot = au_sbbot(sb);
  11275. + for (bindex = 0; bindex <= bbot; bindex++) {
  11276. + br = au_sbr(sb, bindex);
  11277. + au_dr_hino_free(&br->br_dirren);
  11278. + }
  11279. +
  11280. +out:
  11281. + return err;
  11282. +}
  11283. diff -Naur null/fs/aufs/dirren.h linux-6.6.63/fs/aufs/dirren.h
  11284. --- /dev/null
  11285. +++ linux-6.6.63/fs/aufs/dirren.h 2024-12-18 15:12:59.295519588 +0300
  11286. @@ -0,0 +1,140 @@
  11287. +/* SPDX-License-Identifier: GPL-2.0 */
  11288. +/*
  11289. + * Copyright (C) 2017-2022 Junjiro R. Okajima
  11290. + *
  11291. + * This program is free software; you can redistribute it and/or modify
  11292. + * it under the terms of the GNU General Public License as published by
  11293. + * the Free Software Foundation; either version 2 of the License, or
  11294. + * (at your option) any later version.
  11295. + *
  11296. + * This program is distributed in the hope that it will be useful,
  11297. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11298. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11299. + * GNU General Public License for more details.
  11300. + *
  11301. + * You should have received a copy of the GNU General Public License
  11302. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11303. + */
  11304. +
  11305. +/*
  11306. + * renamed dir info
  11307. + */
  11308. +
  11309. +#ifndef __AUFS_DIRREN_H__
  11310. +#define __AUFS_DIRREN_H__
  11311. +
  11312. +#ifdef __KERNEL__
  11313. +
  11314. +#include <linux/dcache.h>
  11315. +#include <linux/statfs.h>
  11316. +#include <linux/uuid.h>
  11317. +#include "hbl.h"
  11318. +
  11319. +#define AuDirren_NHASH 100
  11320. +
  11321. +#ifdef CONFIG_AUFS_DIRREN
  11322. +enum au_brid_type {
  11323. + AuBrid_Unset,
  11324. + AuBrid_UUID,
  11325. + AuBrid_FSID,
  11326. + AuBrid_DEV
  11327. +};
  11328. +
  11329. +struct au_dr_brid {
  11330. + enum au_brid_type type;
  11331. + union {
  11332. + uuid_t uuid; /* unimplemented yet */
  11333. + fsid_t fsid;
  11334. + dev_t dev;
  11335. + };
  11336. +};
  11337. +
  11338. +/* 20 is the max digits length of ulong 64 */
  11339. +/* brid-type "_" uuid "_" inum */
  11340. +#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
  11341. +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
  11342. +
  11343. +struct au_dr_hino {
  11344. + struct hlist_bl_node dr_hnode;
  11345. + ino_t dr_h_ino;
  11346. +};
  11347. +
  11348. +struct au_dr_br {
  11349. + struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
  11350. + struct au_dr_brid dr_brid;
  11351. +};
  11352. +
  11353. +struct au_dr_lookup {
  11354. + /* dr_name is pointed by struct au_do_lookup_args.name */
  11355. + struct qstr dr_name; /* subset of dr_info */
  11356. + aufs_bindex_t ninfo;
  11357. + struct au_drinfo **drinfo;
  11358. +};
  11359. +#else
  11360. +struct au_dr_hino;
  11361. +/* empty */
  11362. +struct au_dr_br { };
  11363. +struct au_dr_lookup { };
  11364. +#endif
  11365. +
  11366. +/* ---------------------------------------------------------------------- */
  11367. +
  11368. +struct au_branch;
  11369. +struct au_do_lookup_args;
  11370. +struct au_hinode;
  11371. +#ifdef CONFIG_AUFS_DIRREN
  11372. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
  11373. + struct au_dr_hino *add_ent);
  11374. +void au_dr_hino_free(struct au_dr_br *dr);
  11375. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  11376. + const struct path *path);
  11377. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
  11378. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  11379. + struct qstr *dst_name, void *_rev);
  11380. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
  11381. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
  11382. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11383. + aufs_bindex_t bindex);
  11384. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11385. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11386. + ino_t h_ino);
  11387. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
  11388. +int au_dr_opt_set(struct super_block *sb);
  11389. +int au_dr_opt_flush(struct super_block *sb);
  11390. +int au_dr_opt_clr(struct super_block *sb, int no_flush);
  11391. +#else
  11392. +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
  11393. + struct au_dr_hino *add_ent);
  11394. +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
  11395. +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
  11396. + const struct path *path);
  11397. +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
  11398. +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
  11399. + struct qstr *dst_name, void *_rev);
  11400. +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
  11401. +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
  11402. + void *rev);
  11403. +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
  11404. + aufs_bindex_t bindex);
  11405. +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11406. +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
  11407. + aufs_bindex_t bindex, ino_t h_ino);
  11408. +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
  11409. +AuStubInt0(au_dr_opt_set, struct super_block *sb);
  11410. +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
  11411. +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
  11412. +#endif
  11413. +
  11414. +/* ---------------------------------------------------------------------- */
  11415. +
  11416. +#ifdef CONFIG_AUFS_DIRREN
  11417. +static inline int au_dr_ihash(ino_t h_ino)
  11418. +{
  11419. + return h_ino % AuDirren_NHASH;
  11420. +}
  11421. +#else
  11422. +AuStubInt0(au_dr_ihash, ino_t h_ino);
  11423. +#endif
  11424. +
  11425. +#endif /* __KERNEL__ */
  11426. +#endif /* __AUFS_DIRREN_H__ */
  11427. diff -Naur null/fs/aufs/dynop.c linux-6.6.63/fs/aufs/dynop.c
  11428. --- /dev/null
  11429. +++ linux-6.6.63/fs/aufs/dynop.c 2024-12-18 15:12:59.295519588 +0300
  11430. @@ -0,0 +1,365 @@
  11431. +// SPDX-License-Identifier: GPL-2.0
  11432. +/*
  11433. + * Copyright (C) 2010-2022 Junjiro R. Okajima
  11434. + *
  11435. + * This program is free software; you can redistribute it and/or modify
  11436. + * it under the terms of the GNU General Public License as published by
  11437. + * the Free Software Foundation; either version 2 of the License, or
  11438. + * (at your option) any later version.
  11439. + *
  11440. + * This program is distributed in the hope that it will be useful,
  11441. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11442. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11443. + * GNU General Public License for more details.
  11444. + *
  11445. + * You should have received a copy of the GNU General Public License
  11446. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11447. + */
  11448. +
  11449. +/*
  11450. + * dynamically customizable operations for regular files
  11451. + */
  11452. +
  11453. +#include "aufs.h"
  11454. +
  11455. +#define DyPrSym(key) AuDbgSym((void*)(key->dk_op.dy_hop))
  11456. +
  11457. +/*
  11458. + * How large will these lists be?
  11459. + * Usually just a few elements, 20-30 at most for each, I guess.
  11460. + */
  11461. +static struct hlist_bl_head dynop[AuDyLast];
  11462. +
  11463. +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
  11464. + const void *h_op)
  11465. +{
  11466. + struct au_dykey *key, *tmp;
  11467. + struct hlist_bl_node *pos;
  11468. +
  11469. + key = NULL;
  11470. + hlist_bl_lock(hbl);
  11471. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11472. + if (tmp->dk_op.dy_hop == h_op) {
  11473. + if (kref_get_unless_zero(&tmp->dk_kref))
  11474. + key = tmp;
  11475. + break;
  11476. + }
  11477. + hlist_bl_unlock(hbl);
  11478. +
  11479. + return key;
  11480. +}
  11481. +
  11482. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  11483. +{
  11484. + struct au_dykey **k, *found;
  11485. + const void *h_op = key->dk_op.dy_hop;
  11486. + int i;
  11487. +
  11488. + found = NULL;
  11489. + k = br->br_dykey;
  11490. + for (i = 0; i < AuBrDynOp; i++)
  11491. + if (k[i]) {
  11492. + if (k[i]->dk_op.dy_hop == h_op) {
  11493. + found = k[i];
  11494. + break;
  11495. + }
  11496. + } else
  11497. + break;
  11498. + if (!found) {
  11499. + spin_lock(&br->br_dykey_lock);
  11500. + for (; i < AuBrDynOp; i++)
  11501. + if (k[i]) {
  11502. + if (k[i]->dk_op.dy_hop == h_op) {
  11503. + found = k[i];
  11504. + break;
  11505. + }
  11506. + } else {
  11507. + k[i] = key;
  11508. + break;
  11509. + }
  11510. + spin_unlock(&br->br_dykey_lock);
  11511. + BUG_ON(i == AuBrDynOp); /* expand the array */
  11512. + }
  11513. +
  11514. + return found;
  11515. +}
  11516. +
  11517. +/* kref_get() if @key is already added */
  11518. +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
  11519. +{
  11520. + struct au_dykey *tmp, *found;
  11521. + struct hlist_bl_node *pos;
  11522. + const void *h_op = key->dk_op.dy_hop;
  11523. +
  11524. + found = NULL;
  11525. + hlist_bl_lock(hbl);
  11526. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11527. + if (tmp->dk_op.dy_hop == h_op) {
  11528. + if (kref_get_unless_zero(&tmp->dk_kref))
  11529. + found = tmp;
  11530. + break;
  11531. + }
  11532. + if (!found)
  11533. + hlist_bl_add_head(&key->dk_hnode, hbl);
  11534. + hlist_bl_unlock(hbl);
  11535. +
  11536. + if (!found)
  11537. + DyPrSym(key);
  11538. + return found;
  11539. +}
  11540. +
  11541. +static void dy_free_rcu(struct rcu_head *rcu)
  11542. +{
  11543. + struct au_dykey *key;
  11544. +
  11545. + key = container_of(rcu, struct au_dykey, dk_rcu);
  11546. + DyPrSym(key);
  11547. + kfree(key);
  11548. +}
  11549. +
  11550. +static void dy_free(struct kref *kref)
  11551. +{
  11552. + struct au_dykey *key;
  11553. + struct hlist_bl_head *hbl;
  11554. +
  11555. + key = container_of(kref, struct au_dykey, dk_kref);
  11556. + hbl = dynop + key->dk_op.dy_type;
  11557. + au_hbl_del(&key->dk_hnode, hbl);
  11558. + call_rcu(&key->dk_rcu, dy_free_rcu);
  11559. +}
  11560. +
  11561. +void au_dy_put(struct au_dykey *key)
  11562. +{
  11563. + kref_put(&key->dk_kref, dy_free);
  11564. +}
  11565. +
  11566. +/* ---------------------------------------------------------------------- */
  11567. +
  11568. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  11569. +
  11570. +#define DyDbgDeclare(cnt) unsigned int cnt __maybe_unused = 0
  11571. +#ifdef CONFIG_AUFS_DEBUG
  11572. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  11573. +#else
  11574. +AuStubVoid(DyDbgInc, unsigned int cnt __maybe_unused);
  11575. +#endif
  11576. +
  11577. +#define DySet(func, dst, src, h_op, h_sb) do { \
  11578. + DyDbgInc(cnt); \
  11579. + if (h_op->func) { \
  11580. + if (src.func) \
  11581. + dst.func = src.func; \
  11582. + else \
  11583. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  11584. + } \
  11585. +} while (0)
  11586. +
  11587. +#define DySetForce(func, dst, src) do { \
  11588. + AuDebugOn(!src.func); \
  11589. + DyDbgInc(cnt); \
  11590. + dst.func = src.func; \
  11591. +} while (0)
  11592. +
  11593. +#define DySetAop(func) \
  11594. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  11595. +#define DySetAopForce(func) \
  11596. + DySetForce(func, dyaop->da_op, aufs_aop)
  11597. +
  11598. +static void dy_aop(struct au_dykey *key, const void *h_op,
  11599. + struct super_block *h_sb __maybe_unused)
  11600. +{
  11601. + struct au_dyaop *dyaop = (void *)key;
  11602. + const struct address_space_operations *h_aop = h_op;
  11603. + DyDbgDeclare(cnt);
  11604. +
  11605. + AuDbg("%s\n", au_sbtype(h_sb));
  11606. +
  11607. + DySetAop(writepage);
  11608. + DySetAopForce(read_folio); /* force */
  11609. + DySetAop(writepages);
  11610. + DySetAop(dirty_folio);
  11611. + DySetAop(invalidate_folio);
  11612. + DySetAop(readahead);
  11613. + DySetAop(write_begin);
  11614. + DySetAop(write_end);
  11615. + DySetAop(bmap);
  11616. + DySetAop(release_folio);
  11617. + DySetAop(free_folio);
  11618. + /* this one will be changed according to an aufs mount option */
  11619. + DySetAop(direct_IO);
  11620. + DySetAop(migrate_folio);
  11621. + DySetAop(launder_folio);
  11622. + DySetAop(is_partially_uptodate);
  11623. + DySetAop(is_dirty_writeback);
  11624. + DySetAop(error_remove_page);
  11625. + DySetAop(swap_activate);
  11626. + DySetAop(swap_deactivate);
  11627. + DySetAop(swap_rw);
  11628. +
  11629. + DyDbgSize(cnt, *h_aop);
  11630. +}
  11631. +
  11632. +/* ---------------------------------------------------------------------- */
  11633. +
  11634. +static void dy_bug(struct kref *kref)
  11635. +{
  11636. + BUG();
  11637. +}
  11638. +
  11639. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  11640. +{
  11641. + struct au_dykey *key, *old;
  11642. + struct hlist_bl_head *hbl;
  11643. + struct op {
  11644. + unsigned int sz;
  11645. + void (*set)(struct au_dykey *key, const void *h_op,
  11646. + struct super_block *h_sb __maybe_unused);
  11647. + };
  11648. + static const struct op a[] = {
  11649. + [AuDy_AOP] = {
  11650. + .sz = sizeof(struct au_dyaop),
  11651. + .set = dy_aop
  11652. + }
  11653. + };
  11654. + const struct op *p;
  11655. +
  11656. + hbl = dynop + op->dy_type;
  11657. + key = dy_gfind_get(hbl, op->dy_hop);
  11658. + if (key)
  11659. + goto out_add; /* success */
  11660. +
  11661. + p = a + op->dy_type;
  11662. + key = kzalloc(p->sz, GFP_NOFS);
  11663. + if (unlikely(!key)) {
  11664. + key = ERR_PTR(-ENOMEM);
  11665. + goto out;
  11666. + }
  11667. +
  11668. + key->dk_op.dy_hop = op->dy_hop;
  11669. + kref_init(&key->dk_kref);
  11670. + p->set(key, op->dy_hop, au_br_sb(br));
  11671. + old = dy_gadd(hbl, key);
  11672. + if (old) {
  11673. + au_kfree_rcu(key);
  11674. + key = old;
  11675. + }
  11676. +
  11677. +out_add:
  11678. + old = dy_bradd(br, key);
  11679. + if (old)
  11680. + /* its ref-count should never be zero here */
  11681. + kref_put(&key->dk_kref, dy_bug);
  11682. +out:
  11683. + return key;
  11684. +}
  11685. +
  11686. +/* ---------------------------------------------------------------------- */
  11687. +/*
  11688. + * Aufs prohibits O_DIRECT by default even if the branch supports it.
  11689. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  11690. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  11691. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  11692. + * See the aufs manual in detail.
  11693. + */
  11694. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  11695. +{
  11696. + if (!do_dx)
  11697. + dyaop->da_op.direct_IO = NULL;
  11698. + else
  11699. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  11700. +}
  11701. +
  11702. +static struct au_dyaop *dy_aget(struct au_branch *br,
  11703. + const struct address_space_operations *h_aop,
  11704. + int do_dx)
  11705. +{
  11706. + struct au_dyaop *dyaop;
  11707. + struct au_dynop op;
  11708. +
  11709. + op.dy_type = AuDy_AOP;
  11710. + op.dy_haop = h_aop;
  11711. + dyaop = (void *)dy_get(&op, br);
  11712. + if (IS_ERR(dyaop))
  11713. + goto out;
  11714. + dy_adx(dyaop, do_dx);
  11715. +
  11716. +out:
  11717. + return dyaop;
  11718. +}
  11719. +
  11720. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11721. + struct inode *h_inode)
  11722. +{
  11723. + int err, do_dx;
  11724. + struct super_block *sb;
  11725. + struct au_branch *br;
  11726. + struct au_dyaop *dyaop;
  11727. +
  11728. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  11729. + IiMustWriteLock(inode);
  11730. +
  11731. + sb = inode->i_sb;
  11732. + br = au_sbr(sb, bindex);
  11733. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  11734. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  11735. + err = PTR_ERR(dyaop);
  11736. + if (IS_ERR(dyaop))
  11737. + /* unnecessary to call dy_fput() */
  11738. + goto out;
  11739. +
  11740. + err = 0;
  11741. + inode->i_mapping->a_ops = &dyaop->da_op;
  11742. +
  11743. +out:
  11744. + return err;
  11745. +}
  11746. +
  11747. +/*
  11748. + * Is it safe to replace a_ops during the inode/file is in operation?
  11749. + * Yes, I hope so.
  11750. + */
  11751. +int au_dy_irefresh(struct inode *inode)
  11752. +{
  11753. + int err;
  11754. + aufs_bindex_t btop;
  11755. + struct inode *h_inode;
  11756. +
  11757. + err = 0;
  11758. + if (S_ISREG(inode->i_mode)) {
  11759. + btop = au_ibtop(inode);
  11760. + h_inode = au_h_iptr(inode, btop);
  11761. + err = au_dy_iaop(inode, btop, h_inode);
  11762. + }
  11763. + return err;
  11764. +}
  11765. +
  11766. +void au_dy_arefresh(int do_dx)
  11767. +{
  11768. + struct hlist_bl_head *hbl;
  11769. + struct hlist_bl_node *pos;
  11770. + struct au_dykey *key;
  11771. +
  11772. + hbl = dynop + AuDy_AOP;
  11773. + hlist_bl_lock(hbl);
  11774. + hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
  11775. + dy_adx((void *)key, do_dx);
  11776. + hlist_bl_unlock(hbl);
  11777. +}
  11778. +
  11779. +/* ---------------------------------------------------------------------- */
  11780. +
  11781. +void __init au_dy_init(void)
  11782. +{
  11783. + int i;
  11784. +
  11785. + for (i = 0; i < AuDyLast; i++)
  11786. + INIT_HLIST_BL_HEAD(dynop + i);
  11787. +}
  11788. +
  11789. +void au_dy_fin(void)
  11790. +{
  11791. + int i;
  11792. +
  11793. + for (i = 0; i < AuDyLast; i++)
  11794. + WARN_ON(!hlist_bl_empty(dynop + i));
  11795. +}
  11796. diff -Naur null/fs/aufs/dynop.h linux-6.6.63/fs/aufs/dynop.h
  11797. --- /dev/null
  11798. +++ linux-6.6.63/fs/aufs/dynop.h 2024-12-18 15:12:59.295519588 +0300
  11799. @@ -0,0 +1,77 @@
  11800. +/* SPDX-License-Identifier: GPL-2.0 */
  11801. +/*
  11802. + * Copyright (C) 2010-2022 Junjiro R. Okajima
  11803. + *
  11804. + * This program is free software; you can redistribute it and/or modify
  11805. + * it under the terms of the GNU General Public License as published by
  11806. + * the Free Software Foundation; either version 2 of the License, or
  11807. + * (at your option) any later version.
  11808. + *
  11809. + * This program is distributed in the hope that it will be useful,
  11810. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11811. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11812. + * GNU General Public License for more details.
  11813. + *
  11814. + * You should have received a copy of the GNU General Public License
  11815. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11816. + */
  11817. +
  11818. +/*
  11819. + * dynamically customizable operations (for regular files only)
  11820. + */
  11821. +
  11822. +#ifndef __AUFS_DYNOP_H__
  11823. +#define __AUFS_DYNOP_H__
  11824. +
  11825. +#ifdef __KERNEL__
  11826. +
  11827. +#include <linux/fs.h>
  11828. +#include <linux/kref.h>
  11829. +
  11830. +enum {AuDy_AOP, AuDyLast};
  11831. +
  11832. +struct au_dynop {
  11833. + int dy_type;
  11834. + union {
  11835. + const void *dy_hop;
  11836. + const struct address_space_operations *dy_haop;
  11837. + };
  11838. +};
  11839. +
  11840. +struct au_dykey {
  11841. + union {
  11842. + struct hlist_bl_node dk_hnode;
  11843. + struct rcu_head dk_rcu;
  11844. + };
  11845. + struct au_dynop dk_op;
  11846. +
  11847. + /*
  11848. + * during I am in the branch local array, kref is gotten. when the
  11849. + * branch is removed, kref is put.
  11850. + */
  11851. + struct kref dk_kref;
  11852. +};
  11853. +
  11854. +/* stop unioning since their sizes are very different from each other */
  11855. +struct au_dyaop {
  11856. + struct au_dykey da_key;
  11857. + struct address_space_operations da_op; /* not const */
  11858. +};
  11859. +/* make sure that 'struct au_dykey *' can be any type */
  11860. +static_assert(!offsetof(struct au_dyaop, da_key));
  11861. +
  11862. +/* ---------------------------------------------------------------------- */
  11863. +
  11864. +/* dynop.c */
  11865. +struct au_branch;
  11866. +void au_dy_put(struct au_dykey *key);
  11867. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11868. + struct inode *h_inode);
  11869. +int au_dy_irefresh(struct inode *inode);
  11870. +void au_dy_arefresh(int do_dio);
  11871. +
  11872. +void __init au_dy_init(void);
  11873. +void au_dy_fin(void);
  11874. +
  11875. +#endif /* __KERNEL__ */
  11876. +#endif /* __AUFS_DYNOP_H__ */
  11877. diff -Naur null/fs/aufs/export.c linux-6.6.63/fs/aufs/export.c
  11878. --- /dev/null
  11879. +++ linux-6.6.63/fs/aufs/export.c 2024-12-18 15:12:59.295519588 +0300
  11880. @@ -0,0 +1,830 @@
  11881. +// SPDX-License-Identifier: GPL-2.0
  11882. +/*
  11883. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  11884. + *
  11885. + * This program is free software; you can redistribute it and/or modify
  11886. + * it under the terms of the GNU General Public License as published by
  11887. + * the Free Software Foundation; either version 2 of the License, or
  11888. + * (at your option) any later version.
  11889. + *
  11890. + * This program is distributed in the hope that it will be useful,
  11891. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11892. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11893. + * GNU General Public License for more details.
  11894. + *
  11895. + * You should have received a copy of the GNU General Public License
  11896. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11897. + */
  11898. +
  11899. +/*
  11900. + * export via nfs
  11901. + */
  11902. +
  11903. +#include <linux/exportfs.h>
  11904. +#include <linux/fs_struct.h>
  11905. +#include <linux/nsproxy.h>
  11906. +#include <linux/random.h>
  11907. +#include <linux/writeback.h>
  11908. +#include "aufs.h"
  11909. +
  11910. +union conv {
  11911. +#ifdef CONFIG_AUFS_INO_T_64
  11912. + __u32 a[2];
  11913. +#else
  11914. + __u32 a[1];
  11915. +#endif
  11916. + ino_t ino;
  11917. +};
  11918. +
  11919. +static ino_t decode_ino(__u32 *a)
  11920. +{
  11921. + union conv u;
  11922. +
  11923. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  11924. + u.a[0] = a[0];
  11925. +#ifdef CONFIG_AUFS_INO_T_64
  11926. + u.a[1] = a[1];
  11927. +#endif
  11928. + return u.ino;
  11929. +}
  11930. +
  11931. +static void encode_ino(__u32 *a, ino_t ino)
  11932. +{
  11933. + union conv u;
  11934. +
  11935. + u.ino = ino;
  11936. + a[0] = u.a[0];
  11937. +#ifdef CONFIG_AUFS_INO_T_64
  11938. + a[1] = u.a[1];
  11939. +#endif
  11940. +}
  11941. +
  11942. +/* NFS file handle */
  11943. +enum {
  11944. + Fh_br_id,
  11945. + Fh_sigen,
  11946. +#ifdef CONFIG_AUFS_INO_T_64
  11947. + /* support 64bit inode number */
  11948. + Fh_ino1,
  11949. + Fh_ino2,
  11950. + Fh_dir_ino1,
  11951. + Fh_dir_ino2,
  11952. +#else
  11953. + Fh_ino1,
  11954. + Fh_dir_ino1,
  11955. +#endif
  11956. + Fh_igen,
  11957. + Fh_h_type,
  11958. + Fh_tail,
  11959. +
  11960. + Fh_ino = Fh_ino1,
  11961. + Fh_dir_ino = Fh_dir_ino1
  11962. +};
  11963. +
  11964. +static int au_test_anon(struct dentry *dentry)
  11965. +{
  11966. + /* note: read d_flags without d_lock */
  11967. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  11968. +}
  11969. +
  11970. +int au_test_nfsd(void)
  11971. +{
  11972. + int ret;
  11973. + struct task_struct *tsk = current;
  11974. + char comm[sizeof(tsk->comm)];
  11975. +
  11976. + ret = 0;
  11977. + if (tsk->flags & PF_KTHREAD) {
  11978. + get_task_comm(comm, tsk);
  11979. + ret = !strcmp(comm, "nfsd");
  11980. + }
  11981. +
  11982. + return ret;
  11983. +}
  11984. +
  11985. +/* ---------------------------------------------------------------------- */
  11986. +/* inode generation external table */
  11987. +
  11988. +void au_xigen_inc(struct inode *inode)
  11989. +{
  11990. + loff_t pos;
  11991. + ssize_t sz;
  11992. + __u32 igen;
  11993. + struct super_block *sb;
  11994. + struct au_sbinfo *sbinfo;
  11995. +
  11996. + sb = inode->i_sb;
  11997. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  11998. +
  11999. + sbinfo = au_sbi(sb);
  12000. + pos = inode->i_ino;
  12001. + pos *= sizeof(igen);
  12002. + igen = inode->i_generation + 1;
  12003. + sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
  12004. + if (sz == sizeof(igen))
  12005. + return; /* success */
  12006. +
  12007. + if (unlikely(sz >= 0))
  12008. + AuIOErr("xigen error (%zd)\n", sz);
  12009. +}
  12010. +
  12011. +int au_xigen_new(struct inode *inode)
  12012. +{
  12013. + int err;
  12014. + loff_t pos;
  12015. + ssize_t sz;
  12016. + struct super_block *sb;
  12017. + struct au_sbinfo *sbinfo;
  12018. + struct file *file;
  12019. +
  12020. + err = 0;
  12021. + /* todo: dirty, at mount time */
  12022. + if (inode->i_ino == AUFS_ROOT_INO)
  12023. + goto out;
  12024. + sb = inode->i_sb;
  12025. + SiMustAnyLock(sb);
  12026. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12027. + goto out;
  12028. +
  12029. + err = -EFBIG;
  12030. + pos = inode->i_ino;
  12031. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  12032. + AuIOErr1("too large i%lld\n", pos);
  12033. + goto out;
  12034. + }
  12035. + pos *= sizeof(inode->i_generation);
  12036. +
  12037. + err = 0;
  12038. + sbinfo = au_sbi(sb);
  12039. + file = sbinfo->si_xigen;
  12040. + BUG_ON(!file);
  12041. +
  12042. + if (vfsub_f_size_read(file)
  12043. + < pos + sizeof(inode->i_generation)) {
  12044. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  12045. + sz = xino_fwrite(file, &inode->i_generation,
  12046. + sizeof(inode->i_generation), &pos);
  12047. + } else
  12048. + sz = xino_fread(file, &inode->i_generation,
  12049. + sizeof(inode->i_generation), &pos);
  12050. + if (sz == sizeof(inode->i_generation))
  12051. + goto out; /* success */
  12052. +
  12053. + err = sz;
  12054. + if (unlikely(sz >= 0)) {
  12055. + err = -EIO;
  12056. + AuIOErr("xigen error (%zd)\n", sz);
  12057. + }
  12058. +
  12059. +out:
  12060. + return err;
  12061. +}
  12062. +
  12063. +int au_xigen_set(struct super_block *sb, struct path *path)
  12064. +{
  12065. + int err;
  12066. + struct au_sbinfo *sbinfo;
  12067. + struct file *file;
  12068. +
  12069. + SiMustWriteLock(sb);
  12070. +
  12071. + sbinfo = au_sbi(sb);
  12072. + file = au_xino_create2(sb, path, sbinfo->si_xigen);
  12073. + err = PTR_ERR(file);
  12074. + if (IS_ERR(file))
  12075. + goto out;
  12076. + err = 0;
  12077. + if (sbinfo->si_xigen)
  12078. + fput(sbinfo->si_xigen);
  12079. + sbinfo->si_xigen = file;
  12080. +
  12081. +out:
  12082. + AuTraceErr(err);
  12083. + return err;
  12084. +}
  12085. +
  12086. +void au_xigen_clr(struct super_block *sb)
  12087. +{
  12088. + struct au_sbinfo *sbinfo;
  12089. +
  12090. + SiMustWriteLock(sb);
  12091. +
  12092. + sbinfo = au_sbi(sb);
  12093. + if (sbinfo->si_xigen) {
  12094. + fput(sbinfo->si_xigen);
  12095. + sbinfo->si_xigen = NULL;
  12096. + }
  12097. +}
  12098. +
  12099. +/* ---------------------------------------------------------------------- */
  12100. +
  12101. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  12102. + ino_t dir_ino)
  12103. +{
  12104. + struct dentry *dentry, *d;
  12105. + struct inode *inode;
  12106. + unsigned int sigen;
  12107. +
  12108. + dentry = NULL;
  12109. + inode = ilookup(sb, ino);
  12110. + if (!inode)
  12111. + goto out;
  12112. +
  12113. + dentry = ERR_PTR(-ESTALE);
  12114. + sigen = au_sigen(sb);
  12115. + if (unlikely(au_is_bad_inode(inode)
  12116. + || IS_DEADDIR(inode)
  12117. + || sigen != au_iigen(inode, NULL)))
  12118. + goto out_iput;
  12119. +
  12120. + dentry = NULL;
  12121. + if (!dir_ino || S_ISDIR(inode->i_mode))
  12122. + dentry = d_find_alias(inode);
  12123. + else {
  12124. + spin_lock(&inode->i_lock);
  12125. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  12126. + spin_lock(&d->d_lock);
  12127. + if (!au_test_anon(d)
  12128. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  12129. + dentry = dget_dlock(d);
  12130. + spin_unlock(&d->d_lock);
  12131. + break;
  12132. + }
  12133. + spin_unlock(&d->d_lock);
  12134. + }
  12135. + spin_unlock(&inode->i_lock);
  12136. + }
  12137. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  12138. + /* need to refresh */
  12139. + dput(dentry);
  12140. + dentry = NULL;
  12141. + }
  12142. +
  12143. +out_iput:
  12144. + iput(inode);
  12145. +out:
  12146. + AuTraceErrPtr(dentry);
  12147. + return dentry;
  12148. +}
  12149. +
  12150. +/* ---------------------------------------------------------------------- */
  12151. +
  12152. +/* todo: dirty? */
  12153. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  12154. +
  12155. +struct au_compare_mnt_args {
  12156. + /* input */
  12157. + struct super_block *sb;
  12158. +
  12159. + /* output */
  12160. + struct vfsmount *mnt;
  12161. +};
  12162. +
  12163. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  12164. +{
  12165. + struct au_compare_mnt_args *a = arg;
  12166. +
  12167. + if (mnt->mnt_sb != a->sb)
  12168. + return 0;
  12169. + a->mnt = mntget(mnt);
  12170. + return 1;
  12171. +}
  12172. +
  12173. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  12174. +{
  12175. + int err;
  12176. + struct path root;
  12177. + struct au_compare_mnt_args args = {
  12178. + .sb = sb
  12179. + };
  12180. +
  12181. + get_fs_root(current->fs, &root);
  12182. + rcu_read_lock();
  12183. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  12184. + rcu_read_unlock();
  12185. + path_put(&root);
  12186. + AuDebugOn(!err);
  12187. + AuDebugOn(!args.mnt);
  12188. + return args.mnt;
  12189. +}
  12190. +
  12191. +struct au_nfsd_si_lock {
  12192. + unsigned int sigen;
  12193. + aufs_bindex_t bindex, br_id;
  12194. + unsigned char force_lock;
  12195. +};
  12196. +
  12197. +static int si_nfsd_read_lock(struct super_block *sb,
  12198. + struct au_nfsd_si_lock *nsi_lock)
  12199. +{
  12200. + int err;
  12201. + aufs_bindex_t bindex;
  12202. +
  12203. + si_read_lock(sb, AuLock_FLUSH);
  12204. +
  12205. + /* branch id may be wrapped around */
  12206. + err = 0;
  12207. + bindex = au_br_index(sb, nsi_lock->br_id);
  12208. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  12209. + goto out; /* success */
  12210. +
  12211. + err = -ESTALE;
  12212. + bindex = -1;
  12213. + if (!nsi_lock->force_lock)
  12214. + si_read_unlock(sb);
  12215. +
  12216. +out:
  12217. + nsi_lock->bindex = bindex;
  12218. + return err;
  12219. +}
  12220. +
  12221. +struct find_name_by_ino {
  12222. + struct dir_context ctx;
  12223. + int called, found;
  12224. + ino_t ino;
  12225. + char *name;
  12226. + int namelen;
  12227. +};
  12228. +
  12229. +static bool
  12230. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  12231. + loff_t offset, u64 ino, unsigned int d_type)
  12232. +{
  12233. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  12234. + ctx);
  12235. +
  12236. + a->called++;
  12237. + if (a->ino != ino)
  12238. + return true;
  12239. +
  12240. + memcpy(a->name, name, namelen);
  12241. + a->namelen = namelen;
  12242. + a->found = 1;
  12243. + return false;
  12244. +}
  12245. +
  12246. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  12247. + struct au_nfsd_si_lock *nsi_lock)
  12248. +{
  12249. + struct dentry *dentry, *parent;
  12250. + struct file *file;
  12251. + struct inode *dir;
  12252. + struct find_name_by_ino arg = {
  12253. + .ctx = {
  12254. + .actor = find_name_by_ino
  12255. + }
  12256. + };
  12257. + int err;
  12258. +
  12259. + parent = path->dentry;
  12260. + if (nsi_lock)
  12261. + si_read_unlock(parent->d_sb);
  12262. + file = vfsub_dentry_open(path, au_dir_roflags);
  12263. + dentry = (void *)file;
  12264. + if (IS_ERR(file))
  12265. + goto out;
  12266. +
  12267. + dentry = ERR_PTR(-ENOMEM);
  12268. + arg.name = (void *)__get_free_page(GFP_NOFS);
  12269. + if (unlikely(!arg.name))
  12270. + goto out_file;
  12271. + arg.ino = ino;
  12272. + arg.found = 0;
  12273. + do {
  12274. + arg.called = 0;
  12275. + /* smp_mb(); */
  12276. + err = vfsub_iterate_dir(file, &arg.ctx);
  12277. + } while (!err && !arg.found && arg.called);
  12278. + dentry = ERR_PTR(err);
  12279. + if (unlikely(err))
  12280. + goto out_name;
  12281. + /* instead of ENOENT */
  12282. + dentry = ERR_PTR(-ESTALE);
  12283. + if (!arg.found)
  12284. + goto out_name;
  12285. +
  12286. + /* do not call vfsub_lkup_one() */
  12287. + dir = d_inode(parent);
  12288. + dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
  12289. + AuTraceErrPtr(dentry);
  12290. + if (IS_ERR(dentry))
  12291. + goto out_name;
  12292. + AuDebugOn(au_test_anon(dentry));
  12293. + if (unlikely(d_really_is_negative(dentry))) {
  12294. + dput(dentry);
  12295. + dentry = ERR_PTR(-ENOENT);
  12296. + }
  12297. +
  12298. +out_name:
  12299. + free_page((unsigned long)arg.name);
  12300. +out_file:
  12301. + fput(file);
  12302. +out:
  12303. + if (unlikely(nsi_lock
  12304. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  12305. + if (!IS_ERR(dentry)) {
  12306. + dput(dentry);
  12307. + dentry = ERR_PTR(-ESTALE);
  12308. + }
  12309. + AuTraceErrPtr(dentry);
  12310. + return dentry;
  12311. +}
  12312. +
  12313. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  12314. + ino_t dir_ino,
  12315. + struct au_nfsd_si_lock *nsi_lock)
  12316. +{
  12317. + struct dentry *dentry;
  12318. + struct path path;
  12319. +
  12320. + if (dir_ino != AUFS_ROOT_INO) {
  12321. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  12322. + dentry = path.dentry;
  12323. + if (!path.dentry || IS_ERR(path.dentry))
  12324. + goto out;
  12325. + AuDebugOn(au_test_anon(path.dentry));
  12326. + } else
  12327. + path.dentry = dget(sb->s_root);
  12328. +
  12329. + path.mnt = au_mnt_get(sb);
  12330. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  12331. + path_put(&path);
  12332. +
  12333. +out:
  12334. + AuTraceErrPtr(dentry);
  12335. + return dentry;
  12336. +}
  12337. +
  12338. +/* ---------------------------------------------------------------------- */
  12339. +
  12340. +static int h_acceptable(void *expv, struct dentry *dentry)
  12341. +{
  12342. + return 1;
  12343. +}
  12344. +
  12345. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  12346. + char *buf, int len, struct super_block *sb)
  12347. +{
  12348. + char *p;
  12349. + int n;
  12350. + struct path path;
  12351. +
  12352. + p = d_path(h_rootpath, buf, len);
  12353. + if (IS_ERR(p))
  12354. + goto out;
  12355. + n = strlen(p);
  12356. +
  12357. + path.mnt = h_rootpath->mnt;
  12358. + path.dentry = h_parent;
  12359. + p = d_path(&path, buf, len);
  12360. + if (IS_ERR(p))
  12361. + goto out;
  12362. + if (n != 1)
  12363. + p += n;
  12364. +
  12365. + path.mnt = au_mnt_get(sb);
  12366. + path.dentry = sb->s_root;
  12367. + p = d_path(&path, buf, len - strlen(p));
  12368. + mntput(path.mnt);
  12369. + if (IS_ERR(p))
  12370. + goto out;
  12371. + if (n != 1)
  12372. + p[strlen(p)] = '/';
  12373. +
  12374. +out:
  12375. + AuTraceErrPtr(p);
  12376. + return p;
  12377. +}
  12378. +
  12379. +static
  12380. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  12381. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  12382. +{
  12383. + struct dentry *dentry, *h_parent, *root;
  12384. + struct super_block *h_sb;
  12385. + char *pathname, *p;
  12386. + struct vfsmount *h_mnt;
  12387. + struct au_branch *br;
  12388. + int err;
  12389. + struct path path;
  12390. +
  12391. + br = au_sbr(sb, nsi_lock->bindex);
  12392. + h_mnt = au_br_mnt(br);
  12393. + h_sb = h_mnt->mnt_sb;
  12394. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  12395. + lockdep_off();
  12396. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  12397. + fh_len - Fh_tail, fh[Fh_h_type],
  12398. + h_acceptable, /*context*/NULL);
  12399. + lockdep_on();
  12400. + dentry = h_parent;
  12401. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  12402. + AuWarn1("%s decode_fh failed, %ld\n",
  12403. + au_sbtype(h_sb), PTR_ERR(h_parent));
  12404. + goto out;
  12405. + }
  12406. + dentry = NULL;
  12407. + if (unlikely(au_test_anon(h_parent))) {
  12408. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  12409. + au_sbtype(h_sb));
  12410. + goto out_h_parent;
  12411. + }
  12412. +
  12413. + dentry = ERR_PTR(-ENOMEM);
  12414. + pathname = (void *)__get_free_page(GFP_NOFS);
  12415. + if (unlikely(!pathname))
  12416. + goto out_h_parent;
  12417. +
  12418. + root = sb->s_root;
  12419. + path.mnt = h_mnt;
  12420. + di_read_lock_parent(root, !AuLock_IR);
  12421. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  12422. + di_read_unlock(root, !AuLock_IR);
  12423. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  12424. + dentry = (void *)p;
  12425. + if (IS_ERR(p))
  12426. + goto out_pathname;
  12427. +
  12428. + si_read_unlock(sb);
  12429. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  12430. + dentry = ERR_PTR(err);
  12431. + if (unlikely(err))
  12432. + goto out_relock;
  12433. +
  12434. + dentry = ERR_PTR(-ENOENT);
  12435. + AuDebugOn(au_test_anon(path.dentry));
  12436. + if (unlikely(d_really_is_negative(path.dentry)))
  12437. + goto out_path;
  12438. +
  12439. + if (ino != d_inode(path.dentry)->i_ino)
  12440. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  12441. + else
  12442. + dentry = dget(path.dentry);
  12443. +
  12444. +out_path:
  12445. + path_put(&path);
  12446. +out_relock:
  12447. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  12448. + if (!IS_ERR(dentry)) {
  12449. + dput(dentry);
  12450. + dentry = ERR_PTR(-ESTALE);
  12451. + }
  12452. +out_pathname:
  12453. + free_page((unsigned long)pathname);
  12454. +out_h_parent:
  12455. + dput(h_parent);
  12456. +out:
  12457. + AuTraceErrPtr(dentry);
  12458. + return dentry;
  12459. +}
  12460. +
  12461. +/* ---------------------------------------------------------------------- */
  12462. +
  12463. +static struct dentry *
  12464. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  12465. + int fh_type)
  12466. +{
  12467. + struct dentry *dentry;
  12468. + __u32 *fh = fid->raw;
  12469. + struct au_branch *br;
  12470. + ino_t ino, dir_ino;
  12471. + struct au_nfsd_si_lock nsi_lock = {
  12472. + .force_lock = 0
  12473. + };
  12474. +
  12475. + dentry = ERR_PTR(-ESTALE);
  12476. + /* it should never happen, but the file handle is unreliable */
  12477. + if (unlikely(fh_len < Fh_tail))
  12478. + goto out;
  12479. + nsi_lock.sigen = fh[Fh_sigen];
  12480. + nsi_lock.br_id = fh[Fh_br_id];
  12481. +
  12482. + /* branch id may be wrapped around */
  12483. + br = NULL;
  12484. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  12485. + goto out;
  12486. + nsi_lock.force_lock = 1;
  12487. +
  12488. + /* is this inode still cached? */
  12489. + ino = decode_ino(fh + Fh_ino);
  12490. + /* it should never happen */
  12491. + if (unlikely(ino == AUFS_ROOT_INO))
  12492. + goto out_unlock;
  12493. +
  12494. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12495. + dentry = decode_by_ino(sb, ino, dir_ino);
  12496. + if (IS_ERR(dentry))
  12497. + goto out_unlock;
  12498. + if (dentry)
  12499. + goto accept;
  12500. +
  12501. + /* is the parent dir cached? */
  12502. + br = au_sbr(sb, nsi_lock.bindex);
  12503. + au_lcnt_inc(&br->br_nfiles);
  12504. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  12505. + if (IS_ERR(dentry))
  12506. + goto out_unlock;
  12507. + if (dentry)
  12508. + goto accept;
  12509. +
  12510. + /* lookup path */
  12511. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  12512. + if (IS_ERR(dentry))
  12513. + goto out_unlock;
  12514. + if (unlikely(!dentry))
  12515. + /* todo?: make it ESTALE */
  12516. + goto out_unlock;
  12517. +
  12518. +accept:
  12519. + if (!au_digen_test(dentry, au_sigen(sb))
  12520. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  12521. + goto out_unlock; /* success */
  12522. +
  12523. + dput(dentry);
  12524. + dentry = ERR_PTR(-ESTALE);
  12525. +out_unlock:
  12526. + if (br)
  12527. + au_lcnt_dec(&br->br_nfiles);
  12528. + si_read_unlock(sb);
  12529. +out:
  12530. + AuTraceErrPtr(dentry);
  12531. + return dentry;
  12532. +}
  12533. +
  12534. +#if 0 /* reserved for future use */
  12535. +/* support subtreecheck option */
  12536. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  12537. + int fh_len, int fh_type)
  12538. +{
  12539. + struct dentry *parent;
  12540. + __u32 *fh = fid->raw;
  12541. + ino_t dir_ino;
  12542. +
  12543. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12544. + parent = decode_by_ino(sb, dir_ino, 0);
  12545. + if (IS_ERR(parent))
  12546. + goto out;
  12547. + if (!parent)
  12548. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  12549. + dir_ino, fh, fh_len);
  12550. +
  12551. +out:
  12552. + AuTraceErrPtr(parent);
  12553. + return parent;
  12554. +}
  12555. +#endif
  12556. +
  12557. +/* ---------------------------------------------------------------------- */
  12558. +
  12559. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  12560. + struct inode *dir)
  12561. +{
  12562. + int err;
  12563. + aufs_bindex_t bindex;
  12564. + struct super_block *sb, *h_sb;
  12565. + struct dentry *dentry, *parent, *h_parent;
  12566. + struct inode *h_dir;
  12567. + struct au_branch *br;
  12568. +
  12569. + err = -ENOSPC;
  12570. + if (unlikely(*max_len <= Fh_tail)) {
  12571. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  12572. + goto out;
  12573. + }
  12574. +
  12575. + err = FILEID_ROOT;
  12576. + if (inode->i_ino == AUFS_ROOT_INO) {
  12577. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  12578. + goto out;
  12579. + }
  12580. +
  12581. + h_parent = NULL;
  12582. + sb = inode->i_sb;
  12583. + err = si_read_lock(sb, AuLock_FLUSH);
  12584. + if (unlikely(err))
  12585. + goto out;
  12586. +
  12587. +#ifdef CONFIG_AUFS_DEBUG
  12588. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12589. + AuWarn1("NFS-exporting requires xino\n");
  12590. +#endif
  12591. + err = -EIO;
  12592. + parent = NULL;
  12593. + ii_read_lock_child(inode);
  12594. + bindex = au_ibtop(inode);
  12595. + if (!dir) {
  12596. + dentry = d_find_any_alias(inode);
  12597. + if (unlikely(!dentry))
  12598. + goto out_unlock;
  12599. + AuDebugOn(au_test_anon(dentry));
  12600. + parent = dget_parent(dentry);
  12601. + dput(dentry);
  12602. + if (unlikely(!parent))
  12603. + goto out_unlock;
  12604. + if (d_really_is_positive(parent))
  12605. + dir = d_inode(parent);
  12606. + }
  12607. +
  12608. + ii_read_lock_parent(dir);
  12609. + h_dir = au_h_iptr(dir, bindex);
  12610. + ii_read_unlock(dir);
  12611. + if (unlikely(!h_dir))
  12612. + goto out_parent;
  12613. + h_parent = d_find_any_alias(h_dir);
  12614. + if (unlikely(!h_parent))
  12615. + goto out_hparent;
  12616. +
  12617. + err = -EPERM;
  12618. + br = au_sbr(sb, bindex);
  12619. + h_sb = au_br_sb(br);
  12620. + if (unlikely(!h_sb->s_export_op)) {
  12621. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  12622. + goto out_hparent;
  12623. + }
  12624. +
  12625. + fh[Fh_br_id] = br->br_id;
  12626. + fh[Fh_sigen] = au_sigen(sb);
  12627. + encode_ino(fh + Fh_ino, inode->i_ino);
  12628. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  12629. + fh[Fh_igen] = inode->i_generation;
  12630. +
  12631. + *max_len -= Fh_tail;
  12632. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  12633. + max_len,
  12634. + /*connectable or subtreecheck*/0);
  12635. + err = fh[Fh_h_type];
  12636. + *max_len += Fh_tail;
  12637. + /* todo: macros? */
  12638. + if (err != FILEID_INVALID)
  12639. + err = 99;
  12640. + else
  12641. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  12642. +
  12643. +out_hparent:
  12644. + dput(h_parent);
  12645. +out_parent:
  12646. + dput(parent);
  12647. +out_unlock:
  12648. + ii_read_unlock(inode);
  12649. + si_read_unlock(sb);
  12650. +out:
  12651. + if (unlikely(err < 0))
  12652. + err = FILEID_INVALID;
  12653. + return err;
  12654. +}
  12655. +
  12656. +/* ---------------------------------------------------------------------- */
  12657. +
  12658. +static int aufs_commit_metadata(struct inode *inode)
  12659. +{
  12660. + int err;
  12661. + aufs_bindex_t bindex;
  12662. + struct super_block *sb;
  12663. + struct inode *h_inode;
  12664. + int (*f)(struct inode *inode);
  12665. +
  12666. + sb = inode->i_sb;
  12667. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12668. + ii_write_lock_child(inode);
  12669. + bindex = au_ibtop(inode);
  12670. + AuDebugOn(bindex < 0);
  12671. + h_inode = au_h_iptr(inode, bindex);
  12672. +
  12673. + f = h_inode->i_sb->s_export_op->commit_metadata;
  12674. + if (f)
  12675. + err = f(h_inode);
  12676. + else
  12677. + err = sync_inode_metadata(h_inode, /*wait*/1);
  12678. +
  12679. + au_cpup_attr_timesizes(inode);
  12680. + ii_write_unlock(inode);
  12681. + si_read_unlock(sb);
  12682. + return err;
  12683. +}
  12684. +
  12685. +/* ---------------------------------------------------------------------- */
  12686. +
  12687. +static struct export_operations aufs_export_op = {
  12688. + .fh_to_dentry = aufs_fh_to_dentry,
  12689. + /* .fh_to_parent = aufs_fh_to_parent, */
  12690. + .encode_fh = aufs_encode_fh,
  12691. + .commit_metadata = aufs_commit_metadata
  12692. +};
  12693. +
  12694. +void au_export_init(struct super_block *sb)
  12695. +{
  12696. + struct au_sbinfo *sbinfo;
  12697. + __u32 u;
  12698. +
  12699. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  12700. + && IS_MODULE(CONFIG_EXPORTFS),
  12701. + AUFS_NAME ": unsupported configuration "
  12702. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  12703. +
  12704. + sb->s_export_op = &aufs_export_op;
  12705. + sbinfo = au_sbi(sb);
  12706. + sbinfo->si_xigen = NULL;
  12707. + get_random_bytes(&u, sizeof(u));
  12708. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  12709. + atomic_set(&sbinfo->si_xigen_next, u);
  12710. +}
  12711. diff -Naur null/fs/aufs/fhsm.c linux-6.6.63/fs/aufs/fhsm.c
  12712. --- /dev/null
  12713. +++ linux-6.6.63/fs/aufs/fhsm.c 2024-12-18 15:12:59.295519588 +0300
  12714. @@ -0,0 +1,426 @@
  12715. +// SPDX-License-Identifier: GPL-2.0
  12716. +/*
  12717. + * Copyright (C) 2011-2022 Junjiro R. Okajima
  12718. + *
  12719. + * This program is free software; you can redistribute it and/or modify
  12720. + * it under the terms of the GNU General Public License as published by
  12721. + * the Free Software Foundation; either version 2 of the License, or
  12722. + * (at your option) any later version.
  12723. + *
  12724. + * This program is distributed in the hope that it will be useful,
  12725. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12726. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12727. + * GNU General Public License for more details.
  12728. + *
  12729. + * You should have received a copy of the GNU General Public License
  12730. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12731. + */
  12732. +
  12733. +/*
  12734. + * File-based Hierarchy Storage Management
  12735. + */
  12736. +
  12737. +#include <linux/anon_inodes.h>
  12738. +#include <linux/poll.h>
  12739. +#include <linux/seq_file.h>
  12740. +#include <linux/statfs.h>
  12741. +#include "aufs.h"
  12742. +
  12743. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  12744. +{
  12745. + struct au_sbinfo *sbinfo;
  12746. + struct au_fhsm *fhsm;
  12747. +
  12748. + SiMustAnyLock(sb);
  12749. +
  12750. + sbinfo = au_sbi(sb);
  12751. + fhsm = &sbinfo->si_fhsm;
  12752. + AuDebugOn(!fhsm);
  12753. + return fhsm->fhsm_bottom;
  12754. +}
  12755. +
  12756. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  12757. +{
  12758. + struct au_sbinfo *sbinfo;
  12759. + struct au_fhsm *fhsm;
  12760. +
  12761. + SiMustWriteLock(sb);
  12762. +
  12763. + sbinfo = au_sbi(sb);
  12764. + fhsm = &sbinfo->si_fhsm;
  12765. + AuDebugOn(!fhsm);
  12766. + fhsm->fhsm_bottom = bindex;
  12767. +}
  12768. +
  12769. +/* ---------------------------------------------------------------------- */
  12770. +
  12771. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  12772. +{
  12773. + struct au_br_fhsm *bf;
  12774. +
  12775. + bf = br->br_fhsm;
  12776. + MtxMustLock(&bf->bf_lock);
  12777. +
  12778. + return !bf->bf_readable
  12779. + || time_after(jiffies,
  12780. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  12781. +}
  12782. +
  12783. +/* ---------------------------------------------------------------------- */
  12784. +
  12785. +static void au_fhsm_notify(struct super_block *sb, int val)
  12786. +{
  12787. + struct au_sbinfo *sbinfo;
  12788. + struct au_fhsm *fhsm;
  12789. +
  12790. + SiMustAnyLock(sb);
  12791. +
  12792. + sbinfo = au_sbi(sb);
  12793. + fhsm = &sbinfo->si_fhsm;
  12794. + if (au_fhsm_pid(fhsm)
  12795. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  12796. + atomic_set(&fhsm->fhsm_readable, val);
  12797. + if (val)
  12798. + wake_up(&fhsm->fhsm_wqh);
  12799. + }
  12800. +}
  12801. +
  12802. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  12803. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  12804. +{
  12805. + int err;
  12806. + struct au_branch *br;
  12807. + struct au_br_fhsm *bf;
  12808. +
  12809. + br = au_sbr(sb, bindex);
  12810. + AuDebugOn(au_br_rdonly(br));
  12811. + bf = br->br_fhsm;
  12812. + AuDebugOn(!bf);
  12813. +
  12814. + if (do_lock)
  12815. + mutex_lock(&bf->bf_lock);
  12816. + else
  12817. + MtxMustLock(&bf->bf_lock);
  12818. +
  12819. + /* sb->s_root for NFS is unreliable */
  12820. + err = au_br_stfs(br, &bf->bf_stfs);
  12821. + if (unlikely(err)) {
  12822. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  12823. + goto out;
  12824. + }
  12825. +
  12826. + bf->bf_jiffy = jiffies;
  12827. + bf->bf_readable = 1;
  12828. + if (do_notify)
  12829. + au_fhsm_notify(sb, /*val*/1);
  12830. + if (rstfs)
  12831. + *rstfs = bf->bf_stfs;
  12832. +
  12833. +out:
  12834. + if (do_lock)
  12835. + mutex_unlock(&bf->bf_lock);
  12836. + au_fhsm_notify(sb, /*val*/1);
  12837. +
  12838. + return err;
  12839. +}
  12840. +
  12841. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  12842. +{
  12843. + int err;
  12844. + struct au_sbinfo *sbinfo;
  12845. + struct au_fhsm *fhsm;
  12846. + struct au_branch *br;
  12847. + struct au_br_fhsm *bf;
  12848. +
  12849. + AuDbg("b%d, force %d\n", bindex, force);
  12850. + SiMustAnyLock(sb);
  12851. +
  12852. + sbinfo = au_sbi(sb);
  12853. + fhsm = &sbinfo->si_fhsm;
  12854. + if (!au_ftest_si(sbinfo, FHSM)
  12855. + || fhsm->fhsm_bottom == bindex)
  12856. + return;
  12857. +
  12858. + br = au_sbr(sb, bindex);
  12859. + bf = br->br_fhsm;
  12860. + AuDebugOn(!bf);
  12861. + mutex_lock(&bf->bf_lock);
  12862. + if (force
  12863. + || au_fhsm_pid(fhsm)
  12864. + || au_fhsm_test_jiffy(sbinfo, br))
  12865. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  12866. + /*do_notify*/1);
  12867. + mutex_unlock(&bf->bf_lock);
  12868. +}
  12869. +
  12870. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  12871. +{
  12872. + aufs_bindex_t bindex, bbot;
  12873. + struct au_branch *br;
  12874. +
  12875. + /* exclude the bottom */
  12876. + bbot = au_fhsm_bottom(sb);
  12877. + for (bindex = 0; bindex < bbot; bindex++) {
  12878. + br = au_sbr(sb, bindex);
  12879. + if (au_br_fhsm(br->br_perm))
  12880. + au_fhsm_wrote(sb, bindex, force);
  12881. + }
  12882. +}
  12883. +
  12884. +/* ---------------------------------------------------------------------- */
  12885. +
  12886. +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
  12887. +{
  12888. + __poll_t mask;
  12889. + struct au_sbinfo *sbinfo;
  12890. + struct au_fhsm *fhsm;
  12891. +
  12892. + mask = 0;
  12893. + sbinfo = file->private_data;
  12894. + fhsm = &sbinfo->si_fhsm;
  12895. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  12896. + if (atomic_read(&fhsm->fhsm_readable))
  12897. + mask = EPOLLIN /* | EPOLLRDNORM */;
  12898. +
  12899. + if (!mask)
  12900. + AuDbg("mask 0x%x\n", mask);
  12901. + return mask;
  12902. +}
  12903. +
  12904. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  12905. + struct aufs_stfs *stfs, __s16 brid)
  12906. +{
  12907. + int err;
  12908. +
  12909. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  12910. + if (!err)
  12911. + err = __put_user(brid, &stbr->brid);
  12912. + if (unlikely(err))
  12913. + err = -EFAULT;
  12914. +
  12915. + return err;
  12916. +}
  12917. +
  12918. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  12919. + struct aufs_stbr __user *stbr, size_t count)
  12920. +{
  12921. + ssize_t err;
  12922. + int nstbr;
  12923. + aufs_bindex_t bindex, bbot;
  12924. + struct au_branch *br;
  12925. + struct au_br_fhsm *bf;
  12926. +
  12927. + /* except the bottom branch */
  12928. + err = 0;
  12929. + nstbr = 0;
  12930. + bbot = au_fhsm_bottom(sb);
  12931. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  12932. + br = au_sbr(sb, bindex);
  12933. + if (!au_br_fhsm(br->br_perm))
  12934. + continue;
  12935. +
  12936. + bf = br->br_fhsm;
  12937. + mutex_lock(&bf->bf_lock);
  12938. + if (bf->bf_readable) {
  12939. + err = -EFAULT;
  12940. + if (count >= sizeof(*stbr))
  12941. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  12942. + br->br_id);
  12943. + if (!err) {
  12944. + bf->bf_readable = 0;
  12945. + count -= sizeof(*stbr);
  12946. + nstbr++;
  12947. + }
  12948. + }
  12949. + mutex_unlock(&bf->bf_lock);
  12950. + }
  12951. + if (!err)
  12952. + err = sizeof(*stbr) * nstbr;
  12953. +
  12954. + return err;
  12955. +}
  12956. +
  12957. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  12958. + loff_t *pos)
  12959. +{
  12960. + ssize_t err;
  12961. + int readable;
  12962. + aufs_bindex_t nfhsm, bindex, bbot;
  12963. + struct au_sbinfo *sbinfo;
  12964. + struct au_fhsm *fhsm;
  12965. + struct au_branch *br;
  12966. + struct super_block *sb;
  12967. +
  12968. + err = 0;
  12969. + sbinfo = file->private_data;
  12970. + fhsm = &sbinfo->si_fhsm;
  12971. +need_data:
  12972. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  12973. + if (!atomic_read(&fhsm->fhsm_readable)) {
  12974. + if (vfsub_file_flags(file) & O_NONBLOCK)
  12975. + err = -EAGAIN;
  12976. + else
  12977. + err = wait_event_interruptible_locked_irq
  12978. + (fhsm->fhsm_wqh,
  12979. + atomic_read(&fhsm->fhsm_readable));
  12980. + }
  12981. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  12982. + if (unlikely(err))
  12983. + goto out;
  12984. +
  12985. + /* sb may already be dead */
  12986. + au_rw_read_lock(&sbinfo->si_rwsem);
  12987. + readable = atomic_read(&fhsm->fhsm_readable);
  12988. + if (readable > 0) {
  12989. + sb = sbinfo->si_sb;
  12990. + AuDebugOn(!sb);
  12991. + /* exclude the bottom branch */
  12992. + nfhsm = 0;
  12993. + bbot = au_fhsm_bottom(sb);
  12994. + for (bindex = 0; bindex < bbot; bindex++) {
  12995. + br = au_sbr(sb, bindex);
  12996. + if (au_br_fhsm(br->br_perm))
  12997. + nfhsm++;
  12998. + }
  12999. + err = -EMSGSIZE;
  13000. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  13001. + atomic_set(&fhsm->fhsm_readable, 0);
  13002. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  13003. + count);
  13004. + }
  13005. + }
  13006. + au_rw_read_unlock(&sbinfo->si_rwsem);
  13007. + if (!readable)
  13008. + goto need_data;
  13009. +
  13010. +out:
  13011. + return err;
  13012. +}
  13013. +
  13014. +static int au_fhsm_release(struct inode *inode, struct file *file)
  13015. +{
  13016. + struct au_sbinfo *sbinfo;
  13017. + struct au_fhsm *fhsm;
  13018. +
  13019. + /* sb may already be dead */
  13020. + sbinfo = file->private_data;
  13021. + fhsm = &sbinfo->si_fhsm;
  13022. + spin_lock(&fhsm->fhsm_spin);
  13023. + fhsm->fhsm_pid = 0;
  13024. + spin_unlock(&fhsm->fhsm_spin);
  13025. + kobject_put(&sbinfo->si_kobj);
  13026. +
  13027. + return 0;
  13028. +}
  13029. +
  13030. +static const struct file_operations au_fhsm_fops = {
  13031. + .owner = THIS_MODULE,
  13032. + .llseek = noop_llseek,
  13033. + .read = au_fhsm_read,
  13034. + .poll = au_fhsm_poll,
  13035. + .release = au_fhsm_release
  13036. +};
  13037. +
  13038. +int au_fhsm_fd(struct super_block *sb, int oflags)
  13039. +{
  13040. + int err, fd;
  13041. + struct au_sbinfo *sbinfo;
  13042. + struct au_fhsm *fhsm;
  13043. +
  13044. + err = -EPERM;
  13045. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  13046. + goto out;
  13047. +
  13048. + err = -EINVAL;
  13049. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  13050. + goto out;
  13051. +
  13052. + err = 0;
  13053. + sbinfo = au_sbi(sb);
  13054. + fhsm = &sbinfo->si_fhsm;
  13055. + spin_lock(&fhsm->fhsm_spin);
  13056. + if (!fhsm->fhsm_pid)
  13057. + fhsm->fhsm_pid = current->pid;
  13058. + else
  13059. + err = -EBUSY;
  13060. + spin_unlock(&fhsm->fhsm_spin);
  13061. + if (unlikely(err))
  13062. + goto out;
  13063. +
  13064. + oflags |= O_RDONLY;
  13065. + /* oflags |= FMODE_NONOTIFY; */
  13066. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  13067. + err = fd;
  13068. + if (unlikely(fd < 0))
  13069. + goto out_pid;
  13070. +
  13071. + /* succeed regardless 'fhsm' status */
  13072. + kobject_get(&sbinfo->si_kobj);
  13073. + si_noflush_read_lock(sb);
  13074. + if (au_ftest_si(sbinfo, FHSM))
  13075. + au_fhsm_wrote_all(sb, /*force*/0);
  13076. + si_read_unlock(sb);
  13077. + goto out; /* success */
  13078. +
  13079. +out_pid:
  13080. + spin_lock(&fhsm->fhsm_spin);
  13081. + fhsm->fhsm_pid = 0;
  13082. + spin_unlock(&fhsm->fhsm_spin);
  13083. +out:
  13084. + AuTraceErr(err);
  13085. + return err;
  13086. +}
  13087. +
  13088. +/* ---------------------------------------------------------------------- */
  13089. +
  13090. +int au_fhsm_br_alloc(struct au_branch *br)
  13091. +{
  13092. + int err;
  13093. +
  13094. + err = 0;
  13095. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  13096. + if (br->br_fhsm)
  13097. + au_br_fhsm_init(br->br_fhsm);
  13098. + else
  13099. + err = -ENOMEM;
  13100. +
  13101. + return err;
  13102. +}
  13103. +
  13104. +/* ---------------------------------------------------------------------- */
  13105. +
  13106. +void au_fhsm_fin(struct super_block *sb)
  13107. +{
  13108. + au_fhsm_notify(sb, /*val*/-1);
  13109. +}
  13110. +
  13111. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  13112. +{
  13113. + struct au_fhsm *fhsm;
  13114. +
  13115. + fhsm = &sbinfo->si_fhsm;
  13116. + spin_lock_init(&fhsm->fhsm_spin);
  13117. + init_waitqueue_head(&fhsm->fhsm_wqh);
  13118. + atomic_set(&fhsm->fhsm_readable, 0);
  13119. + fhsm->fhsm_expire
  13120. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  13121. + fhsm->fhsm_bottom = -1;
  13122. +}
  13123. +
  13124. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  13125. +{
  13126. + sbinfo->si_fhsm.fhsm_expire
  13127. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  13128. +}
  13129. +
  13130. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  13131. +{
  13132. + unsigned int u;
  13133. +
  13134. + if (!au_ftest_si(sbinfo, FHSM))
  13135. + return;
  13136. +
  13137. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  13138. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  13139. + seq_printf(seq, ",fhsm_sec=%u", u);
  13140. +}
  13141. diff -Naur null/fs/aufs/file.c linux-6.6.63/fs/aufs/file.c
  13142. --- /dev/null
  13143. +++ linux-6.6.63/fs/aufs/file.c 2024-12-18 15:12:59.296519576 +0300
  13144. @@ -0,0 +1,866 @@
  13145. +// SPDX-License-Identifier: GPL-2.0
  13146. +/*
  13147. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  13148. + *
  13149. + * This program is free software; you can redistribute it and/or modify
  13150. + * it under the terms of the GNU General Public License as published by
  13151. + * the Free Software Foundation; either version 2 of the License, or
  13152. + * (at your option) any later version.
  13153. + *
  13154. + * This program is distributed in the hope that it will be useful,
  13155. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13156. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13157. + * GNU General Public License for more details.
  13158. + *
  13159. + * You should have received a copy of the GNU General Public License
  13160. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13161. + */
  13162. +
  13163. +/*
  13164. + * handling file/dir, and address_space operation
  13165. + */
  13166. +
  13167. +#ifdef CONFIG_AUFS_DEBUG
  13168. +#include <linux/migrate.h>
  13169. +#endif
  13170. +#include <linux/pagemap.h>
  13171. +#include "aufs.h"
  13172. +
  13173. +/* drop flags for writing */
  13174. +unsigned int au_file_roflags(unsigned int flags)
  13175. +{
  13176. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  13177. + flags |= O_RDONLY | O_NOATIME;
  13178. + return flags;
  13179. +}
  13180. +
  13181. +/* common functions to regular file and dir */
  13182. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  13183. + struct file *file, int force_wr)
  13184. +{
  13185. + struct file *h_file;
  13186. + struct dentry *h_dentry;
  13187. + struct inode *h_inode;
  13188. + struct super_block *sb;
  13189. + struct au_branch *br;
  13190. + struct path h_path;
  13191. + int err;
  13192. +
  13193. + /* a race condition can happen between open and unlink/rmdir */
  13194. + h_file = ERR_PTR(-ENOENT);
  13195. + h_dentry = au_h_dptr(dentry, bindex);
  13196. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  13197. + goto out;
  13198. + h_inode = d_inode(h_dentry);
  13199. + spin_lock(&h_dentry->d_lock);
  13200. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  13201. + /* || !d_inode(dentry)->i_nlink */
  13202. + ;
  13203. + spin_unlock(&h_dentry->d_lock);
  13204. + if (unlikely(err))
  13205. + goto out;
  13206. +
  13207. + sb = dentry->d_sb;
  13208. + br = au_sbr(sb, bindex);
  13209. + err = au_br_test_oflag(flags, br);
  13210. + h_file = ERR_PTR(err);
  13211. + if (unlikely(err))
  13212. + goto out;
  13213. +
  13214. + /* drop flags for writing */
  13215. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  13216. + if (force_wr && !(flags & O_WRONLY))
  13217. + force_wr = 0;
  13218. + flags = au_file_roflags(flags);
  13219. + if (force_wr) {
  13220. + h_file = ERR_PTR(-EROFS);
  13221. + flags = au_file_roflags(flags);
  13222. + if (unlikely(vfsub_native_ro(h_inode)
  13223. + || IS_APPEND(h_inode)))
  13224. + goto out;
  13225. + flags &= ~O_ACCMODE;
  13226. + flags |= O_WRONLY;
  13227. + }
  13228. + }
  13229. + flags &= ~O_CREAT;
  13230. + au_lcnt_inc(&br->br_nfiles);
  13231. + h_path.dentry = h_dentry;
  13232. + h_path.mnt = au_br_mnt(br);
  13233. + /*
  13234. + * vfs::backing_file_open() looks promising since it can get rid of
  13235. + * mm::vm_prfile approach from my mind.
  13236. + * but I keep current approach for a while.
  13237. + */
  13238. + h_file = vfsub_dentry_open(&h_path, flags);
  13239. + if (IS_ERR(h_file))
  13240. + goto out_br;
  13241. +
  13242. + if (flags & __FMODE_EXEC) {
  13243. + err = deny_write_access(h_file);
  13244. + if (unlikely(err)) {
  13245. + fput(h_file);
  13246. + h_file = ERR_PTR(err);
  13247. + goto out_br;
  13248. + }
  13249. + }
  13250. + fsnotify_open(h_file);
  13251. + goto out; /* success */
  13252. +
  13253. +out_br:
  13254. + au_lcnt_dec(&br->br_nfiles);
  13255. +out:
  13256. + return h_file;
  13257. +}
  13258. +
  13259. +static int au_cmoo(struct dentry *dentry)
  13260. +{
  13261. + int err, cmoo, matched;
  13262. + unsigned int udba;
  13263. + struct path h_path;
  13264. + struct au_pin pin;
  13265. + struct au_cp_generic cpg = {
  13266. + .dentry = dentry,
  13267. + .bdst = -1,
  13268. + .bsrc = -1,
  13269. + .len = -1,
  13270. + .pin = &pin,
  13271. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  13272. + };
  13273. + struct inode *delegated;
  13274. + struct super_block *sb;
  13275. + struct au_sbinfo *sbinfo;
  13276. + struct au_fhsm *fhsm;
  13277. + pid_t pid;
  13278. + struct au_branch *br;
  13279. + struct dentry *parent;
  13280. + struct au_hinode *hdir;
  13281. +
  13282. + DiMustWriteLock(dentry);
  13283. + IiMustWriteLock(d_inode(dentry));
  13284. +
  13285. + err = 0;
  13286. + if (IS_ROOT(dentry))
  13287. + goto out;
  13288. + cpg.bsrc = au_dbtop(dentry);
  13289. + if (!cpg.bsrc)
  13290. + goto out;
  13291. +
  13292. + sb = dentry->d_sb;
  13293. + sbinfo = au_sbi(sb);
  13294. + fhsm = &sbinfo->si_fhsm;
  13295. + pid = au_fhsm_pid(fhsm);
  13296. + rcu_read_lock();
  13297. + matched = (pid
  13298. + && (current->pid == pid
  13299. + || rcu_dereference(current->real_parent)->pid == pid));
  13300. + rcu_read_unlock();
  13301. + if (matched)
  13302. + goto out;
  13303. +
  13304. + br = au_sbr(sb, cpg.bsrc);
  13305. + cmoo = au_br_cmoo(br->br_perm);
  13306. + if (!cmoo)
  13307. + goto out;
  13308. + if (!d_is_reg(dentry))
  13309. + cmoo &= AuBrAttr_COO_ALL;
  13310. + if (!cmoo)
  13311. + goto out;
  13312. +
  13313. + parent = dget_parent(dentry);
  13314. + di_write_lock_parent(parent);
  13315. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  13316. + cpg.bdst = err;
  13317. + if (unlikely(err < 0)) {
  13318. + err = 0; /* there is no upper writable branch */
  13319. + goto out_dgrade;
  13320. + }
  13321. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  13322. +
  13323. + /* do not respect the coo attrib for the target branch */
  13324. + err = au_cpup_dirs(dentry, cpg.bdst);
  13325. + if (unlikely(err))
  13326. + goto out_dgrade;
  13327. +
  13328. + di_downgrade_lock(parent, AuLock_IR);
  13329. + udba = au_opt_udba(sb);
  13330. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  13331. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13332. + if (unlikely(err))
  13333. + goto out_parent;
  13334. +
  13335. + err = au_sio_cpup_simple(&cpg);
  13336. + au_unpin(&pin);
  13337. + if (unlikely(err))
  13338. + goto out_parent;
  13339. + if (!(cmoo & AuBrWAttr_MOO))
  13340. + goto out_parent; /* success */
  13341. +
  13342. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  13343. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13344. + if (unlikely(err))
  13345. + goto out_parent;
  13346. +
  13347. + h_path.mnt = au_br_mnt(br);
  13348. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  13349. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  13350. + delegated = NULL;
  13351. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  13352. + au_unpin(&pin);
  13353. + /* todo: keep h_dentry or not? */
  13354. + if (unlikely(err == -EWOULDBLOCK)) {
  13355. + pr_warn("cannot retry for NFSv4 delegation"
  13356. + " for an internal unlink\n");
  13357. + iput(delegated);
  13358. + }
  13359. + if (unlikely(err)) {
  13360. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  13361. + dentry, err);
  13362. + err = 0;
  13363. + }
  13364. + goto out_parent; /* success */
  13365. +
  13366. +out_dgrade:
  13367. + di_downgrade_lock(parent, AuLock_IR);
  13368. +out_parent:
  13369. + di_read_unlock(parent, AuLock_IR);
  13370. + dput(parent);
  13371. +out:
  13372. + AuTraceErr(err);
  13373. + return err;
  13374. +}
  13375. +
  13376. +int au_do_open(struct file *file, struct au_do_open_args *args)
  13377. +{
  13378. + int err, aopen = args->aopen;
  13379. + struct dentry *dentry;
  13380. + struct au_finfo *finfo;
  13381. +
  13382. + if (!aopen)
  13383. + err = au_finfo_init(file, args->fidir);
  13384. + else {
  13385. + lockdep_off();
  13386. + err = au_finfo_init(file, args->fidir);
  13387. + lockdep_on();
  13388. + }
  13389. + if (unlikely(err))
  13390. + goto out;
  13391. +
  13392. + dentry = file->f_path.dentry;
  13393. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  13394. + di_write_lock_child(dentry);
  13395. + err = au_cmoo(dentry);
  13396. + if (!err) {
  13397. + if (!aopen) {
  13398. + err = args->open(file, vfsub_file_flags(file),
  13399. + au_di(dentry)->di_htmpfile);
  13400. + di_write_unlock(dentry);
  13401. + } else {
  13402. + di_downgrade_lock(dentry, AuLock_IR);
  13403. + lockdep_off();
  13404. + err = args->open(file, vfsub_file_flags(file),
  13405. + args->h_file);
  13406. + lockdep_on();
  13407. + di_read_unlock(dentry, AuLock_IR);
  13408. + }
  13409. + }
  13410. +
  13411. + finfo = au_fi(file);
  13412. + if (!err) {
  13413. + finfo->fi_file = file;
  13414. + au_hbl_add(&finfo->fi_hlist,
  13415. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  13416. + }
  13417. + if (!aopen)
  13418. + fi_write_unlock(file);
  13419. + else {
  13420. + lockdep_off();
  13421. + fi_write_unlock(file);
  13422. + lockdep_on();
  13423. + }
  13424. + if (unlikely(err)) {
  13425. + finfo->fi_hdir = NULL;
  13426. + au_finfo_fin(file);
  13427. + }
  13428. +
  13429. +out:
  13430. + AuTraceErr(err);
  13431. + return err;
  13432. +}
  13433. +
  13434. +int au_reopen_nondir(struct file *file)
  13435. +{
  13436. + int err;
  13437. + aufs_bindex_t btop;
  13438. + struct dentry *dentry;
  13439. + struct au_branch *br;
  13440. + struct file *h_file, *h_file_tmp;
  13441. +
  13442. + dentry = file->f_path.dentry;
  13443. + btop = au_dbtop(dentry);
  13444. + br = au_sbr(dentry->d_sb, btop);
  13445. + h_file_tmp = NULL;
  13446. + if (au_fbtop(file) == btop) {
  13447. + h_file = au_hf_top(file);
  13448. + if (file->f_mode == h_file->f_mode)
  13449. + return 0; /* success */
  13450. + h_file_tmp = h_file;
  13451. + get_file(h_file_tmp);
  13452. + au_lcnt_inc(&br->br_nfiles);
  13453. + au_set_h_fptr(file, btop, NULL);
  13454. + }
  13455. + AuDebugOn(au_fi(file)->fi_hdir);
  13456. + /*
  13457. + * it can happen
  13458. + * file exists on both of rw and ro
  13459. + * open --> dbtop and fbtop are both 0
  13460. + * prepend a branch as rw, "rw" become ro
  13461. + * remove rw/file
  13462. + * delete the top branch, "rw" becomes rw again
  13463. + * --> dbtop is 1, fbtop is still 0
  13464. + * write --> fbtop is 0 but dbtop is 1
  13465. + */
  13466. + /* AuDebugOn(au_fbtop(file) < btop); */
  13467. +
  13468. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  13469. + file, /*force_wr*/0);
  13470. + err = PTR_ERR(h_file);
  13471. + if (IS_ERR(h_file)) {
  13472. + if (h_file_tmp) {
  13473. + /* revert */
  13474. + au_set_h_fptr(file, btop, h_file_tmp);
  13475. + h_file_tmp = NULL;
  13476. + }
  13477. + goto out; /* todo: close all? */
  13478. + }
  13479. +
  13480. + err = 0;
  13481. + au_set_fbtop(file, btop);
  13482. + au_set_h_fptr(file, btop, h_file);
  13483. + au_update_figen(file);
  13484. + file->f_mode |= h_file->f_mode & FMODE_CAN_ODIRECT;
  13485. + /* todo: necessary? */
  13486. + /* file->f_ra = h_file->f_ra; */
  13487. +
  13488. +out:
  13489. + if (h_file_tmp) {
  13490. + fput(h_file_tmp);
  13491. + au_lcnt_dec(&br->br_nfiles);
  13492. + }
  13493. + return err;
  13494. +}
  13495. +
  13496. +/* ---------------------------------------------------------------------- */
  13497. +
  13498. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  13499. + struct dentry *hi_wh)
  13500. +{
  13501. + int err;
  13502. + aufs_bindex_t btop;
  13503. + struct au_dinfo *dinfo;
  13504. + struct dentry *h_dentry;
  13505. + struct au_hdentry *hdp;
  13506. +
  13507. + dinfo = au_di(file->f_path.dentry);
  13508. + AuRwMustWriteLock(&dinfo->di_rwsem);
  13509. +
  13510. + btop = dinfo->di_btop;
  13511. + dinfo->di_btop = btgt;
  13512. + hdp = au_hdentry(dinfo, btgt);
  13513. + h_dentry = hdp->hd_dentry;
  13514. + hdp->hd_dentry = hi_wh;
  13515. + err = au_reopen_nondir(file);
  13516. + hdp->hd_dentry = h_dentry;
  13517. + dinfo->di_btop = btop;
  13518. +
  13519. + return err;
  13520. +}
  13521. +
  13522. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  13523. + aufs_bindex_t bcpup, struct au_pin *pin)
  13524. +{
  13525. + int err;
  13526. + struct inode *inode, *h_inode;
  13527. + struct dentry *h_dentry, *hi_wh;
  13528. + struct au_cp_generic cpg = {
  13529. + .dentry = file->f_path.dentry,
  13530. + .bdst = bcpup,
  13531. + .bsrc = -1,
  13532. + .len = len,
  13533. + .pin = pin
  13534. + };
  13535. +
  13536. + au_update_dbtop(cpg.dentry);
  13537. + inode = d_inode(cpg.dentry);
  13538. + h_inode = NULL;
  13539. + if (au_dbtop(cpg.dentry) <= bcpup
  13540. + && au_dbbot(cpg.dentry) >= bcpup) {
  13541. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  13542. + if (h_dentry && d_is_positive(h_dentry))
  13543. + h_inode = d_inode(h_dentry);
  13544. + }
  13545. + hi_wh = au_hi_wh(inode, bcpup);
  13546. + if (!hi_wh && !h_inode)
  13547. + err = au_sio_cpup_wh(&cpg, file);
  13548. + else
  13549. + /* already copied-up after unlink */
  13550. + err = au_reopen_wh(file, bcpup, hi_wh);
  13551. +
  13552. + if (!err
  13553. + && (vfsub_inode_nlink(inode, AU_I_AUFS) > 1
  13554. + || (inode->i_state & I_LINKABLE))
  13555. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  13556. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  13557. +
  13558. + return err;
  13559. +}
  13560. +
  13561. +/*
  13562. + * prepare the @file for writing.
  13563. + */
  13564. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  13565. +{
  13566. + int err;
  13567. + aufs_bindex_t dbtop;
  13568. + struct dentry *parent;
  13569. + struct inode *inode;
  13570. + struct super_block *sb;
  13571. + struct file *h_file;
  13572. + struct au_cp_generic cpg = {
  13573. + .dentry = file->f_path.dentry,
  13574. + .bdst = -1,
  13575. + .bsrc = -1,
  13576. + .len = len,
  13577. + .pin = pin,
  13578. + .flags = AuCpup_DTIME
  13579. + };
  13580. +
  13581. + sb = cpg.dentry->d_sb;
  13582. + inode = d_inode(cpg.dentry);
  13583. + cpg.bsrc = au_fbtop(file);
  13584. + err = au_test_ro(sb, cpg.bsrc, inode);
  13585. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  13586. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  13587. + /*flags*/0);
  13588. + goto out;
  13589. + }
  13590. +
  13591. + /* need to cpup or reopen */
  13592. + parent = dget_parent(cpg.dentry);
  13593. + di_write_lock_parent(parent);
  13594. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13595. + cpg.bdst = err;
  13596. + if (unlikely(err < 0))
  13597. + goto out_dgrade;
  13598. + err = 0;
  13599. +
  13600. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  13601. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  13602. + if (unlikely(err))
  13603. + goto out_dgrade;
  13604. + }
  13605. +
  13606. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13607. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13608. + if (unlikely(err))
  13609. + goto out_dgrade;
  13610. +
  13611. + dbtop = au_dbtop(cpg.dentry);
  13612. + if (dbtop <= cpg.bdst)
  13613. + cpg.bsrc = cpg.bdst;
  13614. +
  13615. + if (dbtop <= cpg.bdst /* just reopen */
  13616. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  13617. + ) {
  13618. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  13619. + if (IS_ERR(h_file))
  13620. + err = PTR_ERR(h_file);
  13621. + else {
  13622. + di_downgrade_lock(parent, AuLock_IR);
  13623. + if (dbtop > cpg.bdst)
  13624. + err = au_sio_cpup_simple(&cpg);
  13625. + if (!err)
  13626. + err = au_reopen_nondir(file);
  13627. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  13628. + }
  13629. + } else { /* copyup as wh and reopen */
  13630. + /*
  13631. + * since writable hfsplus branch is not supported,
  13632. + * h_open_pre/post() are unnecessary.
  13633. + */
  13634. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  13635. + di_downgrade_lock(parent, AuLock_IR);
  13636. + }
  13637. +
  13638. + if (!err) {
  13639. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  13640. + goto out_dput; /* success */
  13641. + }
  13642. + au_unpin(pin);
  13643. + goto out_unlock;
  13644. +
  13645. +out_dgrade:
  13646. + di_downgrade_lock(parent, AuLock_IR);
  13647. +out_unlock:
  13648. + di_read_unlock(parent, AuLock_IR);
  13649. +out_dput:
  13650. + dput(parent);
  13651. +out:
  13652. + return err;
  13653. +}
  13654. +
  13655. +/* ---------------------------------------------------------------------- */
  13656. +
  13657. +int au_do_flush(struct file *file, fl_owner_t id,
  13658. + int (*flush)(struct file *file, fl_owner_t id))
  13659. +{
  13660. + int err;
  13661. + struct super_block *sb;
  13662. + struct inode *inode;
  13663. +
  13664. + inode = file_inode(file);
  13665. + sb = inode->i_sb;
  13666. + si_noflush_read_lock(sb);
  13667. + fi_read_lock(file);
  13668. + ii_read_lock_child(inode);
  13669. +
  13670. + err = flush(file, id);
  13671. + au_cpup_attr_timesizes(inode);
  13672. +
  13673. + ii_read_unlock(inode);
  13674. + fi_read_unlock(file);
  13675. + si_read_unlock(sb);
  13676. + return err;
  13677. +}
  13678. +
  13679. +/* ---------------------------------------------------------------------- */
  13680. +
  13681. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  13682. +{
  13683. + int err;
  13684. + struct au_pin pin;
  13685. + struct au_finfo *finfo;
  13686. + struct dentry *parent, *hi_wh;
  13687. + struct inode *inode;
  13688. + struct super_block *sb;
  13689. + struct au_cp_generic cpg = {
  13690. + .dentry = file->f_path.dentry,
  13691. + .bdst = -1,
  13692. + .bsrc = -1,
  13693. + .len = -1,
  13694. + .pin = &pin,
  13695. + .flags = AuCpup_DTIME
  13696. + };
  13697. +
  13698. + FiMustWriteLock(file);
  13699. +
  13700. + err = 0;
  13701. + finfo = au_fi(file);
  13702. + sb = cpg.dentry->d_sb;
  13703. + inode = d_inode(cpg.dentry);
  13704. + cpg.bdst = au_ibtop(inode);
  13705. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  13706. + goto out;
  13707. +
  13708. + parent = dget_parent(cpg.dentry);
  13709. + if (au_test_ro(sb, cpg.bdst, inode)) {
  13710. + di_read_lock_parent(parent, !AuLock_IR);
  13711. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13712. + cpg.bdst = err;
  13713. + di_read_unlock(parent, !AuLock_IR);
  13714. + if (unlikely(err < 0))
  13715. + goto out_parent;
  13716. + err = 0;
  13717. + }
  13718. +
  13719. + di_read_lock_parent(parent, AuLock_IR);
  13720. + hi_wh = au_hi_wh(inode, cpg.bdst);
  13721. + if (!S_ISDIR(inode->i_mode)
  13722. + && au_opt_test(au_mntflags(sb), PLINK)
  13723. + && au_plink_test(inode)
  13724. + && !d_unhashed(cpg.dentry)
  13725. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  13726. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  13727. + if (unlikely(err))
  13728. + goto out_unlock;
  13729. +
  13730. + /* always superio. */
  13731. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13732. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13733. + if (!err) {
  13734. + err = au_sio_cpup_simple(&cpg);
  13735. + au_unpin(&pin);
  13736. + }
  13737. + } else if (hi_wh) {
  13738. + /* already copied-up after unlink */
  13739. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  13740. + *need_reopen = 0;
  13741. + }
  13742. +
  13743. +out_unlock:
  13744. + di_read_unlock(parent, AuLock_IR);
  13745. +out_parent:
  13746. + dput(parent);
  13747. +out:
  13748. + return err;
  13749. +}
  13750. +
  13751. +static void au_do_refresh_dir(struct file *file)
  13752. +{
  13753. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  13754. + struct au_hfile *p, tmp, *q;
  13755. + struct au_finfo *finfo;
  13756. + struct super_block *sb;
  13757. + struct au_fidir *fidir;
  13758. +
  13759. + FiMustWriteLock(file);
  13760. +
  13761. + sb = file->f_path.dentry->d_sb;
  13762. + finfo = au_fi(file);
  13763. + fidir = finfo->fi_hdir;
  13764. + AuDebugOn(!fidir);
  13765. + p = fidir->fd_hfile + finfo->fi_btop;
  13766. + brid = p->hf_br->br_id;
  13767. + bbot = fidir->fd_bbot;
  13768. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  13769. + if (!p->hf_file)
  13770. + continue;
  13771. +
  13772. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  13773. + if (new_bindex == bindex)
  13774. + continue;
  13775. + if (new_bindex < 0) {
  13776. + au_set_h_fptr(file, bindex, NULL);
  13777. + continue;
  13778. + }
  13779. +
  13780. + /* swap two lower inode, and loop again */
  13781. + q = fidir->fd_hfile + new_bindex;
  13782. + tmp = *q;
  13783. + *q = *p;
  13784. + *p = tmp;
  13785. + if (tmp.hf_file) {
  13786. + bindex--;
  13787. + p--;
  13788. + }
  13789. + }
  13790. +
  13791. + p = fidir->fd_hfile;
  13792. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  13793. + bbot = au_sbbot(sb);
  13794. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  13795. + finfo->fi_btop++, p++)
  13796. + if (p->hf_file) {
  13797. + if (file_inode(p->hf_file))
  13798. + break;
  13799. + au_hfput(p, /*execed*/0);
  13800. + }
  13801. + } else {
  13802. + bbot = au_br_index(sb, brid);
  13803. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  13804. + finfo->fi_btop++, p++)
  13805. + if (p->hf_file)
  13806. + au_hfput(p, /*execed*/0);
  13807. + bbot = au_sbbot(sb);
  13808. + }
  13809. +
  13810. + p = fidir->fd_hfile + bbot;
  13811. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  13812. + fidir->fd_bbot--, p--)
  13813. + if (p->hf_file) {
  13814. + if (file_inode(p->hf_file))
  13815. + break;
  13816. + au_hfput(p, /*execed*/0);
  13817. + }
  13818. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  13819. +}
  13820. +
  13821. +/*
  13822. + * after branch manipulating, refresh the file.
  13823. + */
  13824. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  13825. +{
  13826. + int err, need_reopen, nbr;
  13827. + aufs_bindex_t bbot, bindex;
  13828. + struct dentry *dentry;
  13829. + struct super_block *sb;
  13830. + struct au_finfo *finfo;
  13831. + struct au_hfile *hfile;
  13832. +
  13833. + dentry = file->f_path.dentry;
  13834. + sb = dentry->d_sb;
  13835. + nbr = au_sbbot(sb) + 1;
  13836. + finfo = au_fi(file);
  13837. + if (!finfo->fi_hdir) {
  13838. + hfile = &finfo->fi_htop;
  13839. + AuDebugOn(!hfile->hf_file);
  13840. + bindex = au_br_index(sb, hfile->hf_br->br_id);
  13841. + AuDebugOn(bindex < 0);
  13842. + if (bindex != finfo->fi_btop)
  13843. + au_set_fbtop(file, bindex);
  13844. + } else {
  13845. + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
  13846. + if (unlikely(err))
  13847. + goto out;
  13848. + au_do_refresh_dir(file);
  13849. + }
  13850. +
  13851. + err = 0;
  13852. + need_reopen = 1;
  13853. + if (!au_test_mmapped(file))
  13854. + err = au_file_refresh_by_inode(file, &need_reopen);
  13855. + if (finfo->fi_hdir)
  13856. + /* harmless if err */
  13857. + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
  13858. + if (!err && need_reopen && !d_unlinked(dentry))
  13859. + err = reopen(file);
  13860. + if (!err) {
  13861. + au_update_figen(file);
  13862. + goto out; /* success */
  13863. + }
  13864. +
  13865. + /* error, close all lower files */
  13866. + if (finfo->fi_hdir) {
  13867. + bbot = au_fbbot_dir(file);
  13868. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  13869. + au_set_h_fptr(file, bindex, NULL);
  13870. + }
  13871. +
  13872. +out:
  13873. + return err;
  13874. +}
  13875. +
  13876. +/* common function to regular file and dir */
  13877. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  13878. + int wlock, unsigned int fi_lsc)
  13879. +{
  13880. + int err;
  13881. + unsigned int sigen, figen;
  13882. + aufs_bindex_t btop;
  13883. + unsigned char pseudo_link;
  13884. + struct dentry *dentry;
  13885. + struct inode *inode;
  13886. +
  13887. + err = 0;
  13888. + dentry = file->f_path.dentry;
  13889. + inode = d_inode(dentry);
  13890. + sigen = au_sigen(dentry->d_sb);
  13891. + fi_write_lock_nested(file, fi_lsc);
  13892. + figen = au_figen(file);
  13893. + if (!fi_lsc)
  13894. + di_write_lock_child(dentry);
  13895. + else
  13896. + di_write_lock_child2(dentry);
  13897. + btop = au_dbtop(dentry);
  13898. + pseudo_link = (btop != au_ibtop(inode));
  13899. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  13900. + if (!wlock) {
  13901. + di_downgrade_lock(dentry, AuLock_IR);
  13902. + fi_downgrade_lock(file);
  13903. + }
  13904. + goto out; /* success */
  13905. + }
  13906. +
  13907. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  13908. + if (au_digen_test(dentry, sigen)) {
  13909. + err = au_reval_dpath(dentry, sigen);
  13910. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  13911. + }
  13912. +
  13913. + if (!err)
  13914. + err = refresh_file(file, reopen);
  13915. + if (!err) {
  13916. + if (!wlock) {
  13917. + di_downgrade_lock(dentry, AuLock_IR);
  13918. + fi_downgrade_lock(file);
  13919. + }
  13920. + } else {
  13921. + di_write_unlock(dentry);
  13922. + fi_write_unlock(file);
  13923. + }
  13924. +
  13925. +out:
  13926. + return err;
  13927. +}
  13928. +
  13929. +/* ---------------------------------------------------------------------- */
  13930. +
  13931. +/* cf. aufs_nopage() */
  13932. +/* for madvise(2) */
  13933. +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
  13934. +{
  13935. + folio_unlock(folio);
  13936. + return 0;
  13937. +}
  13938. +
  13939. +/* it will never be called, but necessary to support O_DIRECT */
  13940. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  13941. +{ BUG(); return 0; }
  13942. +
  13943. +/* they will never be called. */
  13944. +#ifdef CONFIG_AUFS_DEBUG
  13945. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  13946. + loff_t pos, unsigned len,
  13947. + struct page **pagep, void **fsdata)
  13948. +{ AuUnsupport(); return 0; }
  13949. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  13950. + loff_t pos, unsigned len, unsigned copied,
  13951. + struct page *page, void *fsdata)
  13952. +{ AuUnsupport(); return 0; }
  13953. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  13954. +{ AuUnsupport(); return 0; }
  13955. +
  13956. +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
  13957. +{ AuUnsupport(); return true; }
  13958. +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
  13959. +{ AuUnsupport(); }
  13960. +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
  13961. +{ AuUnsupport(); return true; }
  13962. +#if 0 /* called by memory compaction regardless file */
  13963. +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
  13964. + struct folio *src, enum migrate_mode mode)
  13965. +{ AuUnsupport(); return 0; }
  13966. +#endif
  13967. +static int aufs_launder_folio(struct folio *folio)
  13968. +{ AuUnsupport(); return 0; }
  13969. +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
  13970. + size_t count)
  13971. +{ AuUnsupport(); return true; }
  13972. +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
  13973. + bool *writeback)
  13974. +{ AuUnsupport(); }
  13975. +static int aufs_error_remove_page(struct address_space *mapping,
  13976. + struct page *page)
  13977. +{ AuUnsupport(); return 0; }
  13978. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  13979. + sector_t *span)
  13980. +{ AuUnsupport(); return 0; }
  13981. +static void aufs_swap_deactivate(struct file *file)
  13982. +{ AuUnsupport(); }
  13983. +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
  13984. +{ AuUnsupport(); return 0; }
  13985. +#endif /* CONFIG_AUFS_DEBUG */
  13986. +
  13987. +const struct address_space_operations aufs_aop = {
  13988. + .read_folio = aufs_read_folio,
  13989. + .direct_IO = aufs_direct_IO,
  13990. +#ifdef CONFIG_AUFS_DEBUG
  13991. + .writepage = aufs_writepage,
  13992. + /* no writepages, because of writepage */
  13993. + .dirty_folio = aufs_dirty_folio,
  13994. + /* no readpages, because of readpage */
  13995. + .write_begin = aufs_write_begin,
  13996. + .write_end = aufs_write_end,
  13997. + /* no bmap, no block device */
  13998. + .invalidate_folio = aufs_invalidate_folio,
  13999. + .release_folio = aufs_release_folio,
  14000. + /* is fallback_migrate_page ok? */
  14001. + /* .migrate_folio = aufs_migrate_folio, */
  14002. + .launder_folio = aufs_launder_folio,
  14003. + .is_partially_uptodate = aufs_is_partially_uptodate,
  14004. + .is_dirty_writeback = aufs_is_dirty_writeback,
  14005. + .error_remove_page = aufs_error_remove_page,
  14006. + .swap_activate = aufs_swap_activate,
  14007. + .swap_deactivate = aufs_swap_deactivate,
  14008. + .swap_rw = aufs_swap_rw
  14009. +#endif /* CONFIG_AUFS_DEBUG */
  14010. +};
  14011. diff -Naur null/fs/aufs/file.h linux-6.6.63/fs/aufs/file.h
  14012. --- /dev/null
  14013. +++ linux-6.6.63/fs/aufs/file.h 2024-12-18 15:12:59.296519576 +0300
  14014. @@ -0,0 +1,342 @@
  14015. +/* SPDX-License-Identifier: GPL-2.0 */
  14016. +/*
  14017. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  14018. + *
  14019. + * This program is free software; you can redistribute it and/or modify
  14020. + * it under the terms of the GNU General Public License as published by
  14021. + * the Free Software Foundation; either version 2 of the License, or
  14022. + * (at your option) any later version.
  14023. + *
  14024. + * This program is distributed in the hope that it will be useful,
  14025. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14026. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14027. + * GNU General Public License for more details.
  14028. + *
  14029. + * You should have received a copy of the GNU General Public License
  14030. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14031. + */
  14032. +
  14033. +/*
  14034. + * file operations
  14035. + */
  14036. +
  14037. +#ifndef __AUFS_FILE_H__
  14038. +#define __AUFS_FILE_H__
  14039. +
  14040. +#ifdef __KERNEL__
  14041. +
  14042. +#include <linux/file.h>
  14043. +#include <linux/fs.h>
  14044. +#include <linux/mm_types.h>
  14045. +#include <linux/poll.h>
  14046. +#include "rwsem.h"
  14047. +
  14048. +struct au_branch;
  14049. +struct au_hfile {
  14050. + struct file *hf_file;
  14051. + struct au_branch *hf_br;
  14052. +};
  14053. +
  14054. +struct au_vdir;
  14055. +struct au_fidir {
  14056. + aufs_bindex_t fd_bbot;
  14057. + aufs_bindex_t fd_nent;
  14058. + struct au_vdir *fd_vdir_cache;
  14059. + struct au_hfile fd_hfile[];
  14060. +};
  14061. +
  14062. +static inline int au_fidir_sz(int nent)
  14063. +{
  14064. + AuDebugOn(nent < 0);
  14065. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  14066. +}
  14067. +
  14068. +struct au_finfo {
  14069. + atomic_t fi_generation;
  14070. +
  14071. + struct au_rwsem fi_rwsem;
  14072. + aufs_bindex_t fi_btop;
  14073. +
  14074. + /* do not union them */
  14075. + struct { /* for non-dir */
  14076. + struct au_hfile fi_htop;
  14077. + atomic_t fi_mmapped;
  14078. + };
  14079. + struct au_fidir *fi_hdir; /* for dir only */
  14080. +
  14081. + struct hlist_bl_node fi_hlist;
  14082. + struct file *fi_file; /* very ugly */
  14083. + struct rcu_head rcu;
  14084. +} ____cacheline_aligned_in_smp;
  14085. +
  14086. +/* ---------------------------------------------------------------------- */
  14087. +
  14088. +/* file.c */
  14089. +extern const struct address_space_operations aufs_aop;
  14090. +unsigned int au_file_roflags(unsigned int flags);
  14091. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  14092. + struct file *file, int force_wr);
  14093. +struct au_do_open_args {
  14094. + int aopen;
  14095. + int (*open)(struct file *file, int flags,
  14096. + struct file *h_file);
  14097. + struct au_fidir *fidir;
  14098. + struct file *h_file;
  14099. +};
  14100. +int au_do_open(struct file *file, struct au_do_open_args *args);
  14101. +int au_reopen_nondir(struct file *file);
  14102. +struct au_pin;
  14103. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  14104. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  14105. + int wlock, unsigned int fi_lsc);
  14106. +int au_do_flush(struct file *file, fl_owner_t id,
  14107. + int (*flush)(struct file *file, fl_owner_t id));
  14108. +
  14109. +/* poll.c */
  14110. +#ifdef CONFIG_AUFS_POLL
  14111. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
  14112. +#endif
  14113. +
  14114. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  14115. +/* hfsplus.c */
  14116. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  14117. + int force_wr);
  14118. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  14119. + struct file *h_file);
  14120. +#else
  14121. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  14122. + aufs_bindex_t bindex, int force_wr)
  14123. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  14124. + struct file *h_file);
  14125. +#endif
  14126. +
  14127. +/* f_op.c */
  14128. +extern const struct file_operations aufs_file_fop;
  14129. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  14130. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  14131. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
  14132. +
  14133. +/* finfo.c */
  14134. +void au_hfput(struct au_hfile *hf, int execed);
  14135. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  14136. + struct file *h_file);
  14137. +
  14138. +void au_update_figen(struct file *file);
  14139. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  14140. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
  14141. +
  14142. +void au_fi_init_once(void *_fi);
  14143. +void au_finfo_fin(struct file *file);
  14144. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  14145. +
  14146. +/* ioctl.c */
  14147. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  14148. +#ifdef CONFIG_COMPAT
  14149. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  14150. + unsigned long arg);
  14151. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  14152. + unsigned long arg);
  14153. +#endif
  14154. +
  14155. +/* ---------------------------------------------------------------------- */
  14156. +
  14157. +static inline struct au_finfo *au_fi(struct file *file)
  14158. +{
  14159. + return file->private_data;
  14160. +}
  14161. +
  14162. +/* ---------------------------------------------------------------------- */
  14163. +
  14164. +#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
  14165. +#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
  14166. +#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
  14167. +#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
  14168. +/*
  14169. +#define fi_read_trylock_nested(f) \
  14170. + au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
  14171. +#define fi_write_trylock_nested(f) \
  14172. + au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
  14173. +*/
  14174. +
  14175. +#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
  14176. +#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
  14177. +#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
  14178. +
  14179. +/* lock subclass for finfo */
  14180. +enum {
  14181. + AuLsc_FI_1,
  14182. + AuLsc_FI_2
  14183. +};
  14184. +
  14185. +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
  14186. +{
  14187. + au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14188. +}
  14189. +
  14190. +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
  14191. +{
  14192. + au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14193. +}
  14194. +
  14195. +/*
  14196. + * fi_read_lock_1, fi_write_lock_1,
  14197. + * fi_read_lock_2, fi_write_lock_2
  14198. + */
  14199. +#define AuReadLockFunc(name) \
  14200. +static inline void fi_read_lock_##name(struct file *f) \
  14201. +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
  14202. +
  14203. +#define AuWriteLockFunc(name) \
  14204. +static inline void fi_write_lock_##name(struct file *f) \
  14205. +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
  14206. +
  14207. +#define AuRWLockFuncs(name) \
  14208. + AuReadLockFunc(name) \
  14209. + AuWriteLockFunc(name)
  14210. +
  14211. +AuRWLockFuncs(1);
  14212. +AuRWLockFuncs(2);
  14213. +
  14214. +#undef AuReadLockFunc
  14215. +#undef AuWriteLockFunc
  14216. +#undef AuRWLockFuncs
  14217. +
  14218. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  14219. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  14220. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  14221. +
  14222. +/* ---------------------------------------------------------------------- */
  14223. +
  14224. +/* todo: hard/soft set? */
  14225. +static inline aufs_bindex_t au_fbtop(struct file *file)
  14226. +{
  14227. + FiMustAnyLock(file);
  14228. + return au_fi(file)->fi_btop;
  14229. +}
  14230. +
  14231. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  14232. +{
  14233. + FiMustAnyLock(file);
  14234. + AuDebugOn(!au_fi(file)->fi_hdir);
  14235. + return au_fi(file)->fi_hdir->fd_bbot;
  14236. +}
  14237. +
  14238. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  14239. +{
  14240. + FiMustAnyLock(file);
  14241. + AuDebugOn(!au_fi(file)->fi_hdir);
  14242. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  14243. +}
  14244. +
  14245. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  14246. +{
  14247. + FiMustWriteLock(file);
  14248. + au_fi(file)->fi_btop = bindex;
  14249. +}
  14250. +
  14251. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  14252. +{
  14253. + FiMustWriteLock(file);
  14254. + AuDebugOn(!au_fi(file)->fi_hdir);
  14255. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  14256. +}
  14257. +
  14258. +static inline void au_set_fvdir_cache(struct file *file,
  14259. + struct au_vdir *vdir_cache)
  14260. +{
  14261. + FiMustWriteLock(file);
  14262. + AuDebugOn(!au_fi(file)->fi_hdir);
  14263. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  14264. +}
  14265. +
  14266. +static inline struct file *au_hf_top(struct file *file)
  14267. +{
  14268. + FiMustAnyLock(file);
  14269. + AuDebugOn(au_fi(file)->fi_hdir);
  14270. + return au_fi(file)->fi_htop.hf_file;
  14271. +}
  14272. +
  14273. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  14274. +{
  14275. + FiMustAnyLock(file);
  14276. + AuDebugOn(!au_fi(file)->fi_hdir);
  14277. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  14278. +}
  14279. +
  14280. +/* todo: memory barrier? */
  14281. +static inline unsigned int au_figen(struct file *f)
  14282. +{
  14283. + return atomic_read(&au_fi(f)->fi_generation);
  14284. +}
  14285. +
  14286. +static inline void au_set_mmapped(struct file *f)
  14287. +{
  14288. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  14289. + return;
  14290. + pr_warn("fi_mmapped wrapped around\n");
  14291. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  14292. + ;
  14293. +}
  14294. +
  14295. +static inline void au_unset_mmapped(struct file *f)
  14296. +{
  14297. + atomic_dec(&au_fi(f)->fi_mmapped);
  14298. +}
  14299. +
  14300. +static inline int au_test_mmapped(struct file *f)
  14301. +{
  14302. + return atomic_read(&au_fi(f)->fi_mmapped);
  14303. +}
  14304. +
  14305. +/* customize vma->vm_file */
  14306. +
  14307. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  14308. + struct file *file)
  14309. +{
  14310. + struct file *f;
  14311. +
  14312. + f = vma->vm_file;
  14313. + get_file(file);
  14314. + vma->vm_file = file;
  14315. + fput(f);
  14316. +}
  14317. +
  14318. +#ifdef CONFIG_MMU
  14319. +AuStubVoid(AuDbgVmRegion, struct file *file, struct vm_area_struct *vma)
  14320. +
  14321. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14322. + struct file *file)
  14323. +{
  14324. + au_do_vm_file_reset(vma, file);
  14325. +}
  14326. +#else
  14327. +#define AuDbgVmRegion(file, vma) \
  14328. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  14329. +
  14330. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14331. + struct file *file)
  14332. +{
  14333. + struct file *f;
  14334. +
  14335. + au_do_vm_file_reset(vma, file);
  14336. + f = vma->vm_region->vm_file;
  14337. + get_file(file);
  14338. + vma->vm_region->vm_file = file;
  14339. + fput(f);
  14340. +}
  14341. +#endif /* CONFIG_MMU */
  14342. +
  14343. +/* handle vma->vm_prfile */
  14344. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  14345. + struct file *file)
  14346. +{
  14347. + get_file(file);
  14348. + vma->vm_prfile = file;
  14349. +#ifndef CONFIG_MMU
  14350. + get_file(file);
  14351. + vma->vm_region->vm_prfile = file;
  14352. +#endif
  14353. +}
  14354. +
  14355. +#endif /* __KERNEL__ */
  14356. +#endif /* __AUFS_FILE_H__ */
  14357. diff -Naur null/fs/aufs/finfo.c linux-6.6.63/fs/aufs/finfo.c
  14358. --- /dev/null
  14359. +++ linux-6.6.63/fs/aufs/finfo.c 2024-12-18 15:12:59.296519576 +0300
  14360. @@ -0,0 +1,149 @@
  14361. +// SPDX-License-Identifier: GPL-2.0
  14362. +/*
  14363. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  14364. + *
  14365. + * This program is free software; you can redistribute it and/or modify
  14366. + * it under the terms of the GNU General Public License as published by
  14367. + * the Free Software Foundation; either version 2 of the License, or
  14368. + * (at your option) any later version.
  14369. + *
  14370. + * This program is distributed in the hope that it will be useful,
  14371. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14372. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14373. + * GNU General Public License for more details.
  14374. + *
  14375. + * You should have received a copy of the GNU General Public License
  14376. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14377. + */
  14378. +
  14379. +/*
  14380. + * file private data
  14381. + */
  14382. +
  14383. +#include "aufs.h"
  14384. +
  14385. +void au_hfput(struct au_hfile *hf, int execed)
  14386. +{
  14387. + if (execed)
  14388. + allow_write_access(hf->hf_file);
  14389. + fput(hf->hf_file);
  14390. + hf->hf_file = NULL;
  14391. + au_lcnt_dec(&hf->hf_br->br_nfiles);
  14392. + hf->hf_br = NULL;
  14393. +}
  14394. +
  14395. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  14396. +{
  14397. + struct au_finfo *finfo = au_fi(file);
  14398. + struct au_hfile *hf;
  14399. + struct au_fidir *fidir;
  14400. +
  14401. + fidir = finfo->fi_hdir;
  14402. + if (!fidir) {
  14403. + AuDebugOn(finfo->fi_btop != bindex);
  14404. + hf = &finfo->fi_htop;
  14405. + } else
  14406. + hf = fidir->fd_hfile + bindex;
  14407. +
  14408. + if (hf && hf->hf_file)
  14409. + au_hfput(hf, vfsub_file_execed(file));
  14410. + if (val) {
  14411. + FiMustWriteLock(file);
  14412. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  14413. + hf->hf_file = val;
  14414. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  14415. + }
  14416. +}
  14417. +
  14418. +void au_update_figen(struct file *file)
  14419. +{
  14420. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  14421. + /* smp_mb(); */ /* atomic_set */
  14422. +}
  14423. +
  14424. +/* ---------------------------------------------------------------------- */
  14425. +
  14426. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  14427. +{
  14428. + struct au_fidir *fidir;
  14429. + int nbr;
  14430. +
  14431. + nbr = au_sbbot(sb) + 1;
  14432. + if (nbr < 2)
  14433. + nbr = 2; /* initial allocate for 2 branches */
  14434. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  14435. + if (fidir) {
  14436. + fidir->fd_bbot = -1;
  14437. + fidir->fd_nent = nbr;
  14438. + }
  14439. +
  14440. + return fidir;
  14441. +}
  14442. +
  14443. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
  14444. +{
  14445. + int err;
  14446. + struct au_fidir *fidir, *p;
  14447. +
  14448. + AuRwMustWriteLock(&finfo->fi_rwsem);
  14449. + fidir = finfo->fi_hdir;
  14450. + AuDebugOn(!fidir);
  14451. +
  14452. + err = -ENOMEM;
  14453. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  14454. + GFP_NOFS, may_shrink);
  14455. + if (p) {
  14456. + p->fd_nent = nbr;
  14457. + finfo->fi_hdir = p;
  14458. + err = 0;
  14459. + }
  14460. +
  14461. + return err;
  14462. +}
  14463. +
  14464. +/* ---------------------------------------------------------------------- */
  14465. +
  14466. +void au_finfo_fin(struct file *file)
  14467. +{
  14468. + struct au_finfo *finfo;
  14469. +
  14470. + au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
  14471. +
  14472. + finfo = au_fi(file);
  14473. + AuDebugOn(finfo->fi_hdir);
  14474. + AuRwDestroy(&finfo->fi_rwsem);
  14475. + au_cache_free_finfo(finfo);
  14476. +}
  14477. +
  14478. +void au_fi_init_once(void *_finfo)
  14479. +{
  14480. + struct au_finfo *finfo = _finfo;
  14481. +
  14482. + au_rw_init(&finfo->fi_rwsem);
  14483. +}
  14484. +
  14485. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  14486. +{
  14487. + int err;
  14488. + struct au_finfo *finfo;
  14489. + struct dentry *dentry;
  14490. +
  14491. + err = -ENOMEM;
  14492. + dentry = file->f_path.dentry;
  14493. + finfo = au_cache_alloc_finfo();
  14494. + if (unlikely(!finfo))
  14495. + goto out;
  14496. +
  14497. + err = 0;
  14498. + au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
  14499. + au_rw_write_lock(&finfo->fi_rwsem);
  14500. + finfo->fi_btop = -1;
  14501. + finfo->fi_hdir = fidir;
  14502. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  14503. + /* smp_mb(); */ /* atomic_set */
  14504. +
  14505. + file->private_data = finfo;
  14506. +
  14507. +out:
  14508. + return err;
  14509. +}
  14510. diff -Naur null/fs/aufs/f_op.c linux-6.6.63/fs/aufs/f_op.c
  14511. --- /dev/null
  14512. +++ linux-6.6.63/fs/aufs/f_op.c 2024-12-18 15:12:59.295519588 +0300
  14513. @@ -0,0 +1,782 @@
  14514. +// SPDX-License-Identifier: GPL-2.0
  14515. +/*
  14516. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  14517. + *
  14518. + * This program is free software; you can redistribute it and/or modify
  14519. + * it under the terms of the GNU General Public License as published by
  14520. + * the Free Software Foundation; either version 2 of the License, or
  14521. + * (at your option) any later version.
  14522. + *
  14523. + * This program is distributed in the hope that it will be useful,
  14524. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14525. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14526. + * GNU General Public License for more details.
  14527. + *
  14528. + * You should have received a copy of the GNU General Public License
  14529. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14530. + */
  14531. +
  14532. +/*
  14533. + * file and vm operations
  14534. + */
  14535. +
  14536. +#include <linux/aio.h>
  14537. +#include <linux/fs_stack.h>
  14538. +#include <linux/mman.h>
  14539. +#include <linux/security.h>
  14540. +#include "aufs.h"
  14541. +
  14542. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  14543. +{
  14544. + int err;
  14545. + aufs_bindex_t bindex;
  14546. + struct dentry *dentry, *h_dentry;
  14547. + struct au_finfo *finfo;
  14548. + struct inode *h_inode;
  14549. +
  14550. + FiMustWriteLock(file);
  14551. +
  14552. + err = 0;
  14553. + dentry = file->f_path.dentry;
  14554. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  14555. + finfo = au_fi(file);
  14556. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  14557. + atomic_set(&finfo->fi_mmapped, 0);
  14558. + bindex = au_dbtop(dentry);
  14559. + if (!h_file) {
  14560. + h_dentry = au_h_dptr(dentry, bindex);
  14561. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14562. + if (unlikely(err))
  14563. + goto out;
  14564. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  14565. + if (IS_ERR(h_file)) {
  14566. + err = PTR_ERR(h_file);
  14567. + goto out;
  14568. + }
  14569. + } else {
  14570. + h_dentry = h_file->f_path.dentry;
  14571. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14572. + if (unlikely(err))
  14573. + goto out;
  14574. + /* br ref is already inc-ed */
  14575. + }
  14576. +
  14577. + if (flags & __O_TMPFILE) {
  14578. + AuDebugOn(!h_file);
  14579. + AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
  14580. + au_di(dentry)->di_htmpfile = NULL;
  14581. +
  14582. + if (!(flags & O_EXCL)) {
  14583. + h_inode = file_inode(h_file);
  14584. + spin_lock(&h_inode->i_lock);
  14585. + h_inode->i_state |= I_LINKABLE;
  14586. + spin_unlock(&h_inode->i_lock);
  14587. + }
  14588. + }
  14589. + au_set_fbtop(file, bindex);
  14590. + au_set_h_fptr(file, bindex, h_file);
  14591. + au_update_figen(file);
  14592. + file->f_mode |= h_file->f_mode & FMODE_CAN_ODIRECT;
  14593. + /* todo: necessary? */
  14594. + /* file->f_ra = h_file->f_ra; */
  14595. +
  14596. +out:
  14597. + return err;
  14598. +}
  14599. +
  14600. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  14601. + struct file *file)
  14602. +{
  14603. + int err;
  14604. + struct super_block *sb;
  14605. + struct au_do_open_args args = {
  14606. + .open = au_do_open_nondir
  14607. + };
  14608. +
  14609. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  14610. + file, vfsub_file_flags(file), file->f_mode);
  14611. +
  14612. + sb = file->f_path.dentry->d_sb;
  14613. + si_read_lock(sb, AuLock_FLUSH);
  14614. + err = au_do_open(file, &args);
  14615. + si_read_unlock(sb);
  14616. + return err;
  14617. +}
  14618. +
  14619. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  14620. +{
  14621. + struct au_finfo *finfo;
  14622. + aufs_bindex_t bindex;
  14623. +
  14624. + finfo = au_fi(file);
  14625. + au_hbl_del(&finfo->fi_hlist,
  14626. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  14627. + bindex = finfo->fi_btop;
  14628. + if (bindex >= 0)
  14629. + au_set_h_fptr(file, bindex, NULL);
  14630. +
  14631. + au_finfo_fin(file);
  14632. + return 0;
  14633. +}
  14634. +
  14635. +/* ---------------------------------------------------------------------- */
  14636. +
  14637. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  14638. +{
  14639. + int err;
  14640. + struct file *h_file;
  14641. +
  14642. + err = 0;
  14643. + h_file = au_hf_top(file);
  14644. + if (h_file)
  14645. + err = vfsub_flush(h_file, id);
  14646. + return err;
  14647. +}
  14648. +
  14649. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  14650. +{
  14651. + return au_do_flush(file, id, au_do_flush_nondir);
  14652. +}
  14653. +
  14654. +/* ---------------------------------------------------------------------- */
  14655. +/*
  14656. + * read and write functions acquire [fdi]_rwsem once, but release before
  14657. + * mmap_sem. This is because to stop a race condition between mmap(2).
  14658. + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
  14659. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  14660. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  14661. + */
  14662. +
  14663. +/* Callers should call au_read_post() or fput() in the end */
  14664. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
  14665. +{
  14666. + struct file *h_file;
  14667. + int err;
  14668. +
  14669. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
  14670. + if (!err) {
  14671. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  14672. + h_file = au_hf_top(file);
  14673. + get_file(h_file);
  14674. + if (!keep_fi)
  14675. + fi_read_unlock(file);
  14676. + } else
  14677. + h_file = ERR_PTR(err);
  14678. +
  14679. + return h_file;
  14680. +}
  14681. +
  14682. +static void au_read_post(struct inode *inode, struct file *h_file)
  14683. +{
  14684. + /* update without lock, I don't think it a problem */
  14685. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  14686. + fput(h_file);
  14687. +}
  14688. +
  14689. +struct au_write_pre {
  14690. + /* input */
  14691. + unsigned int lsc;
  14692. +
  14693. + /* output */
  14694. + blkcnt_t blks;
  14695. + aufs_bindex_t btop;
  14696. +};
  14697. +
  14698. +/*
  14699. + * return with iinfo is write-locked
  14700. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  14701. + * end
  14702. + */
  14703. +static struct file *au_write_pre(struct file *file, int do_ready,
  14704. + struct au_write_pre *wpre)
  14705. +{
  14706. + struct file *h_file;
  14707. + struct dentry *dentry;
  14708. + int err;
  14709. + unsigned int lsc;
  14710. + struct au_pin pin;
  14711. +
  14712. + lsc = 0;
  14713. + if (wpre)
  14714. + lsc = wpre->lsc;
  14715. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
  14716. + h_file = ERR_PTR(err);
  14717. + if (unlikely(err))
  14718. + goto out;
  14719. +
  14720. + dentry = file->f_path.dentry;
  14721. + if (do_ready) {
  14722. + err = au_ready_to_write(file, -1, &pin);
  14723. + if (unlikely(err)) {
  14724. + h_file = ERR_PTR(err);
  14725. + di_write_unlock(dentry);
  14726. + goto out_fi;
  14727. + }
  14728. + }
  14729. +
  14730. + di_downgrade_lock(dentry, /*flags*/0);
  14731. + if (wpre)
  14732. + wpre->btop = au_fbtop(file);
  14733. + h_file = au_hf_top(file);
  14734. + get_file(h_file);
  14735. + if (wpre)
  14736. + wpre->blks = file_inode(h_file)->i_blocks;
  14737. + if (do_ready)
  14738. + au_unpin(&pin);
  14739. + di_read_unlock(dentry, /*flags*/0);
  14740. + vfsub_file_start_write(h_file);
  14741. +
  14742. +out_fi:
  14743. + fi_write_unlock(file);
  14744. +out:
  14745. + return h_file;
  14746. +}
  14747. +
  14748. +static void au_write_post(struct inode *inode, struct file *h_file,
  14749. + struct au_write_pre *wpre, ssize_t written)
  14750. +{
  14751. + struct inode *h_inode;
  14752. +
  14753. + vfsub_file_end_write(h_file);
  14754. + au_cpup_attr_timesizes(inode);
  14755. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  14756. + h_inode = file_inode(h_file);
  14757. + inode->i_mode = h_inode->i_mode;
  14758. + ii_write_unlock(inode);
  14759. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  14760. + if (written > 0)
  14761. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  14762. + /*force*/h_inode->i_blocks > wpre->blks);
  14763. + fput(h_file);
  14764. +}
  14765. +
  14766. +/*
  14767. + * todo: very ugly
  14768. + * it locks both of i_mutex and si_rwsem for read in safe.
  14769. + * if the plink maintenance mode continues forever (that is the problem),
  14770. + * may loop forever.
  14771. + */
  14772. +static void au_mtx_and_read_lock(struct inode *inode)
  14773. +{
  14774. + int err;
  14775. + struct super_block *sb = inode->i_sb;
  14776. +
  14777. + while (1) {
  14778. + inode_lock(inode);
  14779. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  14780. + if (!err)
  14781. + break;
  14782. + inode_unlock(inode);
  14783. + si_read_lock(sb, AuLock_NOPLMW);
  14784. + si_read_unlock(sb);
  14785. + }
  14786. +}
  14787. +
  14788. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  14789. + struct iov_iter *iov_iter)
  14790. +{
  14791. + ssize_t err;
  14792. + struct file *file;
  14793. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  14794. +
  14795. + err = security_file_permission(h_file, rw);
  14796. + if (unlikely(err))
  14797. + goto out;
  14798. +
  14799. + err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
  14800. + iter = NULL;
  14801. + if (rw == MAY_READ)
  14802. + iter = h_file->f_op->read_iter;
  14803. + else if (rw == MAY_WRITE)
  14804. + iter = h_file->f_op->write_iter;
  14805. +
  14806. + file = kio->ki_filp;
  14807. + kio->ki_filp = h_file;
  14808. + if (iter) {
  14809. + lockdep_off();
  14810. + err = iter(kio, iov_iter);
  14811. + lockdep_on();
  14812. + } else
  14813. + /* currently there is no such fs */
  14814. + WARN_ON_ONCE(1);
  14815. + if (!is_sync_kiocb(kio)) {
  14816. + /* do not restore ki_filp */
  14817. + fput(file);
  14818. + get_file(h_file);
  14819. + } else
  14820. + kio->ki_filp = file;
  14821. +
  14822. +out:
  14823. + return err;
  14824. +}
  14825. +
  14826. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14827. +{
  14828. + ssize_t err;
  14829. + struct file *file, *h_file;
  14830. + struct inode *inode;
  14831. + struct super_block *sb;
  14832. +
  14833. + file = kio->ki_filp;
  14834. + inode = file_inode(file);
  14835. + sb = inode->i_sb;
  14836. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14837. +
  14838. + h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
  14839. + err = PTR_ERR(h_file);
  14840. + if (IS_ERR(h_file))
  14841. + goto out;
  14842. +
  14843. + if (au_test_loopback_kthread()) {
  14844. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  14845. + if (file->f_mapping != h_file->f_mapping) {
  14846. + file->f_mapping = h_file->f_mapping;
  14847. + smp_mb(); /* unnecessary? */
  14848. + }
  14849. + }
  14850. + fi_read_unlock(file);
  14851. +
  14852. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  14853. + /* todo: necessary? */
  14854. + /* file->f_ra = h_file->f_ra; */
  14855. + au_read_post(inode, h_file);
  14856. +
  14857. +out:
  14858. + si_read_unlock(sb);
  14859. + return err;
  14860. +}
  14861. +
  14862. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14863. +{
  14864. + ssize_t err;
  14865. + struct au_write_pre wpre;
  14866. + struct inode *inode;
  14867. + struct file *file, *h_file;
  14868. +
  14869. + file = kio->ki_filp;
  14870. + inode = file_inode(file);
  14871. + au_mtx_and_read_lock(inode);
  14872. +
  14873. + wpre.lsc = 0;
  14874. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14875. + err = PTR_ERR(h_file);
  14876. + if (IS_ERR(h_file))
  14877. + goto out;
  14878. + if (!is_sync_kiocb(kio)) {
  14879. + /* .vs. sb_start_write() in aio_write() */
  14880. + if (S_ISREG(inode->i_mode))
  14881. + __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
  14882. + file_end_write(file);
  14883. + }
  14884. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  14885. + au_write_post(inode, h_file, &wpre, err);
  14886. +
  14887. +out:
  14888. + si_read_unlock(inode->i_sb);
  14889. + inode_unlock(inode);
  14890. + return err;
  14891. +}
  14892. +
  14893. +/*
  14894. + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
  14895. + * don't have their own .splice_{read,write} implimentations, and they use
  14896. + * generic_file_splice_read() and iter_file_splice_write() who can act like the
  14897. + * simple converters to f_op->iter_read() and ->iter_write().
  14898. + * But we keep our own implementations because some non-mainlined FSes may have
  14899. + * their own .splice_{read,write} implimentations and aufs doesn't want to take
  14900. + * away an opportunity to co-work with aufs from them.
  14901. + */
  14902. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  14903. + struct pipe_inode_info *pipe, size_t len,
  14904. + unsigned int flags)
  14905. +{
  14906. + ssize_t err;
  14907. + struct file *h_file;
  14908. + struct inode *inode;
  14909. + struct super_block *sb;
  14910. +
  14911. + inode = file_inode(file);
  14912. + sb = inode->i_sb;
  14913. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14914. +
  14915. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14916. + err = PTR_ERR(h_file);
  14917. + if (IS_ERR(h_file))
  14918. + goto out;
  14919. +
  14920. + err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
  14921. + /* todo: necessary? */
  14922. + /* file->f_ra = h_file->f_ra; */
  14923. + au_read_post(inode, h_file);
  14924. +
  14925. +out:
  14926. + si_read_unlock(sb);
  14927. + return err;
  14928. +}
  14929. +
  14930. +static ssize_t
  14931. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  14932. + size_t len, unsigned int flags)
  14933. +{
  14934. + ssize_t err;
  14935. + struct au_write_pre wpre;
  14936. + struct inode *inode;
  14937. + struct file *h_file;
  14938. +
  14939. + inode = file_inode(file);
  14940. + au_mtx_and_read_lock(inode);
  14941. +
  14942. + wpre.lsc = 0;
  14943. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14944. + err = PTR_ERR(h_file);
  14945. + if (IS_ERR(h_file))
  14946. + goto out;
  14947. +
  14948. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  14949. + au_write_post(inode, h_file, &wpre, err);
  14950. +
  14951. +out:
  14952. + si_read_unlock(inode->i_sb);
  14953. + inode_unlock(inode);
  14954. + return err;
  14955. +}
  14956. +
  14957. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  14958. + loff_t len)
  14959. +{
  14960. + long err;
  14961. + struct au_write_pre wpre;
  14962. + struct inode *inode;
  14963. + struct file *h_file;
  14964. +
  14965. + inode = file_inode(file);
  14966. + au_mtx_and_read_lock(inode);
  14967. +
  14968. + wpre.lsc = 0;
  14969. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14970. + err = PTR_ERR(h_file);
  14971. + if (IS_ERR(h_file))
  14972. + goto out;
  14973. +
  14974. + lockdep_off();
  14975. + err = vfs_fallocate(h_file, mode, offset, len);
  14976. + lockdep_on();
  14977. + /*
  14978. + * we don't need to call file_modifed() here since au_write_post()
  14979. + * is equivalent and copies-up all timestamps and permission bits.
  14980. + */
  14981. + au_write_post(inode, h_file, &wpre, /*written*/1);
  14982. +
  14983. +out:
  14984. + si_read_unlock(inode->i_sb);
  14985. + inode_unlock(inode);
  14986. + return err;
  14987. +}
  14988. +
  14989. +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
  14990. + struct file *dst, loff_t dst_pos,
  14991. + size_t len, unsigned int flags)
  14992. +{
  14993. + ssize_t err;
  14994. + struct au_write_pre wpre;
  14995. + enum { SRC, DST };
  14996. + struct {
  14997. + struct inode *inode;
  14998. + struct file *h_file;
  14999. + struct super_block *h_sb;
  15000. + } a[2];
  15001. +#define a_src a[SRC]
  15002. +#define a_dst a[DST]
  15003. +
  15004. + err = -EINVAL;
  15005. + a_src.inode = file_inode(src);
  15006. + if (unlikely(!S_ISREG(a_src.inode->i_mode)))
  15007. + goto out;
  15008. + a_dst.inode = file_inode(dst);
  15009. + if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
  15010. + goto out;
  15011. +
  15012. + au_mtx_and_read_lock(a_dst.inode);
  15013. + /*
  15014. + * in order to match the order in di_write_lock2_{child,parent}(),
  15015. + * use f_path.dentry for this comparison.
  15016. + */
  15017. + if (src->f_path.dentry < dst->f_path.dentry) {
  15018. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
  15019. + err = PTR_ERR(a_src.h_file);
  15020. + if (IS_ERR(a_src.h_file))
  15021. + goto out_si;
  15022. +
  15023. + wpre.lsc = AuLsc_FI_2;
  15024. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  15025. + err = PTR_ERR(a_dst.h_file);
  15026. + if (IS_ERR(a_dst.h_file)) {
  15027. + au_read_post(a_src.inode, a_src.h_file);
  15028. + goto out_si;
  15029. + }
  15030. + } else {
  15031. + wpre.lsc = AuLsc_FI_1;
  15032. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  15033. + err = PTR_ERR(a_dst.h_file);
  15034. + if (IS_ERR(a_dst.h_file))
  15035. + goto out_si;
  15036. +
  15037. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
  15038. + err = PTR_ERR(a_src.h_file);
  15039. + if (IS_ERR(a_src.h_file)) {
  15040. + au_write_post(a_dst.inode, a_dst.h_file, &wpre,
  15041. + /*written*/0);
  15042. + goto out_si;
  15043. + }
  15044. + }
  15045. +
  15046. + err = -EXDEV;
  15047. + a_src.h_sb = file_inode(a_src.h_file)->i_sb;
  15048. + a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
  15049. + if (unlikely(a_src.h_sb != a_dst.h_sb)) {
  15050. + AuDbgFile(src);
  15051. + AuDbgFile(dst);
  15052. + goto out_file;
  15053. + }
  15054. +
  15055. + err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
  15056. + dst_pos, len, flags);
  15057. +
  15058. +out_file:
  15059. + au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
  15060. + fi_read_unlock(src);
  15061. + au_read_post(a_src.inode, a_src.h_file);
  15062. +out_si:
  15063. + si_read_unlock(a_dst.inode->i_sb);
  15064. + inode_unlock(a_dst.inode);
  15065. +out:
  15066. + return err;
  15067. +#undef a_src
  15068. +#undef a_dst
  15069. +}
  15070. +
  15071. +/* ---------------------------------------------------------------------- */
  15072. +
  15073. +/*
  15074. + * The locking order around current->mmap_sem.
  15075. + * - in most and regular cases
  15076. + * file I/O syscall -- aufs_read() or something
  15077. + * -- si_rwsem for read -- mmap_sem
  15078. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  15079. + * - in mmap case
  15080. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  15081. + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
  15082. + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
  15083. + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  15084. + * It means that when aufs acquires si_rwsem for write, the process should never
  15085. + * acquire mmap_sem.
  15086. + *
  15087. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  15088. + * problem either since any directory is not able to be mmap-ed.
  15089. + * The similar scenario is applied to aufs_readlink() too.
  15090. + */
  15091. +
  15092. +#if 0 /* stop calling security_file_mmap() */
  15093. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  15094. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  15095. +
  15096. +static unsigned long au_arch_prot_conv(unsigned long flags)
  15097. +{
  15098. + /* currently ppc64 only */
  15099. +#ifdef CONFIG_PPC64
  15100. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  15101. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  15102. + return AuConv_VM_PROT(flags, SAO);
  15103. +#else
  15104. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  15105. + return 0;
  15106. +#endif
  15107. +}
  15108. +
  15109. +static unsigned long au_prot_conv(unsigned long flags)
  15110. +{
  15111. + return AuConv_VM_PROT(flags, READ)
  15112. + | AuConv_VM_PROT(flags, WRITE)
  15113. + | AuConv_VM_PROT(flags, EXEC)
  15114. + | au_arch_prot_conv(flags);
  15115. +}
  15116. +
  15117. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  15118. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  15119. +
  15120. +static unsigned long au_flag_conv(unsigned long flags)
  15121. +{
  15122. + return AuConv_VM_MAP(flags, GROWSDOWN)
  15123. + | AuConv_VM_MAP(flags, DENYWRITE)
  15124. + | AuConv_VM_MAP(flags, LOCKED);
  15125. +}
  15126. +#endif
  15127. +
  15128. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  15129. +{
  15130. + int err;
  15131. + const unsigned char wlock
  15132. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  15133. + struct super_block *sb;
  15134. + struct file *h_file;
  15135. + struct inode *inode;
  15136. +
  15137. + AuDbgVmRegion(file, vma);
  15138. +
  15139. + inode = file_inode(file);
  15140. + sb = inode->i_sb;
  15141. + lockdep_off();
  15142. + si_read_lock(sb, AuLock_NOPLMW);
  15143. +
  15144. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  15145. + lockdep_on();
  15146. + err = PTR_ERR(h_file);
  15147. + if (IS_ERR(h_file))
  15148. + goto out;
  15149. +
  15150. + err = 0;
  15151. + au_set_mmapped(file);
  15152. + au_vm_file_reset(vma, h_file);
  15153. + /*
  15154. + * we cannot call security_mmap_file() here since it may acquire
  15155. + * mmap_sem or i_mutex.
  15156. + *
  15157. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  15158. + * au_flag_conv(vma->vm_flags));
  15159. + */
  15160. + if (!err)
  15161. + err = call_mmap(h_file, vma);
  15162. + if (!err) {
  15163. + au_vm_prfile_set(vma, file);
  15164. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  15165. + goto out_fput; /* success */
  15166. + }
  15167. + au_unset_mmapped(file);
  15168. + au_vm_file_reset(vma, file);
  15169. +
  15170. +out_fput:
  15171. + lockdep_off();
  15172. + ii_write_unlock(inode);
  15173. + lockdep_on();
  15174. + fput(h_file);
  15175. +out:
  15176. + lockdep_off();
  15177. + si_read_unlock(sb);
  15178. + lockdep_on();
  15179. + AuTraceErr(err);
  15180. + return err;
  15181. +}
  15182. +
  15183. +/* ---------------------------------------------------------------------- */
  15184. +
  15185. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  15186. + int datasync)
  15187. +{
  15188. + int err;
  15189. + struct au_write_pre wpre;
  15190. + struct inode *inode;
  15191. + struct file *h_file;
  15192. +
  15193. + err = 0; /* -EBADF; */ /* posix? */
  15194. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  15195. + goto out;
  15196. +
  15197. + inode = file_inode(file);
  15198. + au_mtx_and_read_lock(inode);
  15199. +
  15200. + wpre.lsc = 0;
  15201. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  15202. + err = PTR_ERR(h_file);
  15203. + if (IS_ERR(h_file))
  15204. + goto out_unlock;
  15205. +
  15206. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  15207. + au_write_post(inode, h_file, &wpre, /*written*/0);
  15208. +
  15209. +out_unlock:
  15210. + si_read_unlock(inode->i_sb);
  15211. + inode_unlock(inode);
  15212. +out:
  15213. + return err;
  15214. +}
  15215. +
  15216. +static int aufs_fasync(int fd, struct file *file, int flag)
  15217. +{
  15218. + int err;
  15219. + struct file *h_file;
  15220. + struct super_block *sb;
  15221. +
  15222. + sb = file->f_path.dentry->d_sb;
  15223. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15224. +
  15225. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15226. + err = PTR_ERR(h_file);
  15227. + if (IS_ERR(h_file))
  15228. + goto out;
  15229. +
  15230. + if (h_file->f_op->fasync)
  15231. + err = h_file->f_op->fasync(fd, h_file, flag);
  15232. + fput(h_file); /* instead of au_read_post() */
  15233. +
  15234. +out:
  15235. + si_read_unlock(sb);
  15236. + return err;
  15237. +}
  15238. +
  15239. +static int aufs_setfl(struct file *file, unsigned long arg)
  15240. +{
  15241. + int err;
  15242. + struct file *h_file;
  15243. + struct super_block *sb;
  15244. +
  15245. + sb = file->f_path.dentry->d_sb;
  15246. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15247. +
  15248. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15249. + err = PTR_ERR(h_file);
  15250. + if (IS_ERR(h_file))
  15251. + goto out;
  15252. +
  15253. + /* stop calling h_file->fasync */
  15254. + arg |= vfsub_file_flags(file) & FASYNC;
  15255. + err = setfl(/*unused fd*/-1, h_file, arg);
  15256. + fput(h_file); /* instead of au_read_post() */
  15257. +
  15258. +out:
  15259. + si_read_unlock(sb);
  15260. + return err;
  15261. +}
  15262. +
  15263. +/* ---------------------------------------------------------------------- */
  15264. +
  15265. +const struct file_operations aufs_file_fop = {
  15266. + .owner = THIS_MODULE,
  15267. +
  15268. + .llseek = default_llseek,
  15269. +
  15270. + .read_iter = aufs_read_iter,
  15271. + .write_iter = aufs_write_iter,
  15272. +
  15273. +#ifdef CONFIG_AUFS_POLL
  15274. + .poll = aufs_poll,
  15275. +#endif
  15276. + .unlocked_ioctl = aufs_ioctl_nondir,
  15277. +#ifdef CONFIG_COMPAT
  15278. + .compat_ioctl = aufs_compat_ioctl_nondir,
  15279. +#endif
  15280. + .mmap = aufs_mmap,
  15281. + .open = aufs_open_nondir,
  15282. + .flush = aufs_flush_nondir,
  15283. + .release = aufs_release_nondir,
  15284. + .fsync = aufs_fsync_nondir,
  15285. + .fasync = aufs_fasync,
  15286. + .setfl = aufs_setfl,
  15287. + .splice_write = aufs_splice_write,
  15288. + .splice_read = aufs_splice_read,
  15289. +#if 0 /* reserved for future use */
  15290. + .aio_splice_write = aufs_aio_splice_write,
  15291. + .aio_splice_read = aufs_aio_splice_read,
  15292. +#endif
  15293. + .fallocate = aufs_fallocate,
  15294. + .copy_file_range = aufs_copy_file_range
  15295. +};
  15296. diff -Naur null/fs/aufs/fsctx.c linux-6.6.63/fs/aufs/fsctx.c
  15297. --- /dev/null
  15298. +++ linux-6.6.63/fs/aufs/fsctx.c 2024-12-18 15:12:59.296519576 +0300
  15299. @@ -0,0 +1,1244 @@
  15300. +// SPDX-License-Identifier: GPL-2.0
  15301. +/*
  15302. + * Copyright (C) 2022 Junjiro R. Okajima
  15303. + *
  15304. + * This program is free software; you can redistribute it and/or modify
  15305. + * it under the terms of the GNU General Public License as published by
  15306. + * the Free Software Foundation; either version 2 of the License, or
  15307. + * (at your option) any later version.
  15308. + *
  15309. + * This program is distributed in the hope that it will be useful,
  15310. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15311. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15312. + * GNU General Public License for more details.
  15313. + *
  15314. + * You should have received a copy of the GNU General Public License
  15315. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15316. + */
  15317. +
  15318. +/*
  15319. + * fs context, aka new mount api
  15320. + */
  15321. +
  15322. +#include <linux/fs_context.h>
  15323. +#include "aufs.h"
  15324. +
  15325. +struct au_fsctx_opts {
  15326. + aufs_bindex_t bindex;
  15327. + unsigned char skipped;
  15328. + struct au_opt *opt, *opt_tail;
  15329. + struct super_block *sb;
  15330. + struct au_sbinfo *sbinfo;
  15331. + struct au_opts opts;
  15332. +};
  15333. +
  15334. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  15335. +static int cvt_err(int err)
  15336. +{
  15337. + AuTraceErr(err);
  15338. +
  15339. + switch (err) {
  15340. + case -ENOENT:
  15341. + case -ENOTDIR:
  15342. + case -EEXIST:
  15343. + case -EIO:
  15344. + err = -EINVAL;
  15345. + }
  15346. + return err;
  15347. +}
  15348. +
  15349. +static int au_fsctx_reconfigure(struct fs_context *fc)
  15350. +{
  15351. + int err, do_dx;
  15352. + unsigned int mntflags;
  15353. + struct dentry *root;
  15354. + struct super_block *sb;
  15355. + struct inode *inode;
  15356. + struct au_fsctx_opts *a = fc->fs_private;
  15357. +
  15358. + AuDbg("fc %p\n", fc);
  15359. +
  15360. + root = fc->root;
  15361. + sb = root->d_sb;
  15362. + root = sb->s_root; /* "bind"-mount may give us non-root */
  15363. + AuDebugOn(!IS_ROOT(root));
  15364. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  15365. + if (!err) {
  15366. + di_write_lock_child(root);
  15367. + err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
  15368. + aufs_write_unlock(root);
  15369. + }
  15370. +
  15371. + inode = d_inode(root);
  15372. + inode_lock(inode);
  15373. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  15374. + if (unlikely(err))
  15375. + goto out;
  15376. + di_write_lock_child(root);
  15377. +
  15378. + /* au_opts_remount() may return an error */
  15379. + err = au_opts_remount(sb, &a->opts);
  15380. +
  15381. + if (au_ftest_opts(a->opts.flags, REFRESH))
  15382. + au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
  15383. + REFRESH_IDOP));
  15384. +
  15385. + if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
  15386. + mntflags = au_mntflags(sb);
  15387. + do_dx = !!au_opt_test(mntflags, DIO);
  15388. + au_dy_arefresh(do_dx);
  15389. + }
  15390. +
  15391. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  15392. + aufs_write_unlock(root);
  15393. +
  15394. +out:
  15395. + inode_unlock(inode);
  15396. + err = cvt_err(err);
  15397. + AuTraceErr(err);
  15398. +
  15399. + return err;
  15400. +}
  15401. +
  15402. +/* ---------------------------------------------------------------------- */
  15403. +
  15404. +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
  15405. +{
  15406. + int err;
  15407. + struct au_fsctx_opts *a = fc->fs_private;
  15408. + struct au_sbinfo *sbinfo = a->sbinfo;
  15409. + struct dentry *root;
  15410. + struct inode *inode;
  15411. +
  15412. + sbinfo->si_sb = sb;
  15413. + sb->s_fs_info = sbinfo;
  15414. + kobject_get(&sbinfo->si_kobj);
  15415. +
  15416. + __si_write_lock(sb);
  15417. + si_pid_set(sb);
  15418. + au_sbilist_add(sb);
  15419. +
  15420. + /* all timestamps always follow the ones on the branch */
  15421. + sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
  15422. + sb->s_flags |= SB_I_VERSION; /* do we really need this? */
  15423. + sb->s_op = &aufs_sop;
  15424. + sb->s_d_op = &aufs_dop;
  15425. + sb->s_magic = AUFS_SUPER_MAGIC;
  15426. + sb->s_maxbytes = 0;
  15427. + sb->s_stack_depth = 1;
  15428. + au_export_init(sb);
  15429. + au_xattr_init(sb);
  15430. +
  15431. + err = au_alloc_root(sb);
  15432. + if (unlikely(err)) {
  15433. + si_write_unlock(sb);
  15434. + goto out;
  15435. + }
  15436. + root = sb->s_root;
  15437. + inode = d_inode(root);
  15438. + ii_write_lock_parent(inode);
  15439. + aufs_write_unlock(root);
  15440. +
  15441. + /* lock vfs_inode first, then aufs. */
  15442. + inode_lock(inode);
  15443. + aufs_write_lock(root);
  15444. + err = au_opts_mount(sb, &a->opts);
  15445. + AuTraceErr(err);
  15446. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  15447. + sb->s_d_op = &aufs_dop_noreval;
  15448. + /* infofc(fc, "%ps", sb->s_d_op); */
  15449. + pr_info("%ps\n", sb->s_d_op);
  15450. + au_refresh_dop(root, /*force_reval*/0);
  15451. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  15452. + au_refresh_iop(inode, /*force_getattr*/0);
  15453. + }
  15454. + aufs_write_unlock(root);
  15455. + inode_unlock(inode);
  15456. + if (!err)
  15457. + goto out; /* success */
  15458. +
  15459. + dput(root);
  15460. + sb->s_root = NULL;
  15461. +
  15462. +out:
  15463. + if (unlikely(err))
  15464. + kobject_put(&sbinfo->si_kobj);
  15465. + AuTraceErr(err);
  15466. + err = cvt_err(err);
  15467. + AuTraceErr(err);
  15468. + return err;
  15469. +}
  15470. +
  15471. +static int au_fsctx_get_tree(struct fs_context *fc)
  15472. +{
  15473. + int err;
  15474. +
  15475. + AuDbg("fc %p\n", fc);
  15476. + err = get_tree_nodev(fc, au_fsctx_fill_super);
  15477. +
  15478. + AuTraceErr(err);
  15479. + return err;
  15480. +}
  15481. +
  15482. +/* ---------------------------------------------------------------------- */
  15483. +
  15484. +static void au_fsctx_dump(struct au_opts *opts)
  15485. +{
  15486. +#ifdef CONFIG_AUFS_DEBUG
  15487. + /* reduce stack space */
  15488. + union {
  15489. + struct au_opt_add *add;
  15490. + struct au_opt_del *del;
  15491. + struct au_opt_mod *mod;
  15492. + struct au_opt_xino *xino;
  15493. + struct au_opt_xino_itrunc *xino_itrunc;
  15494. + struct au_opt_wbr_create *create;
  15495. + } u;
  15496. + struct au_opt *opt;
  15497. +
  15498. + opt = opts->opt;
  15499. + while (opt->type != Opt_tail) {
  15500. + switch (opt->type) {
  15501. + case Opt_add:
  15502. + u.add = &opt->add;
  15503. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  15504. + u.add->bindex, u.add->pathname, u.add->perm,
  15505. + u.add->path.dentry);
  15506. + break;
  15507. + case Opt_del:
  15508. + fallthrough;
  15509. + case Opt_idel:
  15510. + u.del = &opt->del;
  15511. + AuDbg("del {%s, %p}\n",
  15512. + u.del->pathname, u.del->h_path.dentry);
  15513. + break;
  15514. + case Opt_mod:
  15515. + fallthrough;
  15516. + case Opt_imod:
  15517. + u.mod = &opt->mod;
  15518. + AuDbg("mod {%s, 0x%x, %p}\n",
  15519. + u.mod->path, u.mod->perm, u.mod->h_root);
  15520. + break;
  15521. + case Opt_append:
  15522. + u.add = &opt->add;
  15523. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  15524. + u.add->bindex, u.add->pathname, u.add->perm,
  15525. + u.add->path.dentry);
  15526. + break;
  15527. + case Opt_prepend:
  15528. + u.add = &opt->add;
  15529. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  15530. + u.add->bindex, u.add->pathname, u.add->perm,
  15531. + u.add->path.dentry);
  15532. + break;
  15533. +
  15534. + case Opt_dirwh:
  15535. + AuDbg("dirwh %d\n", opt->dirwh);
  15536. + break;
  15537. + case Opt_rdcache:
  15538. + AuDbg("rdcache %d\n", opt->rdcache);
  15539. + break;
  15540. + case Opt_rdblk:
  15541. + AuDbg("rdblk %d\n", opt->rdblk);
  15542. + break;
  15543. + case Opt_rdhash:
  15544. + AuDbg("rdhash %u\n", opt->rdhash);
  15545. + break;
  15546. +
  15547. + case Opt_xino:
  15548. + u.xino = &opt->xino;
  15549. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  15550. + break;
  15551. +
  15552. +#define au_fsctx_TF(name) \
  15553. + case Opt_##name: \
  15554. + if (opt->tf) \
  15555. + AuLabel(name); \
  15556. + else \
  15557. + AuLabel(no##name); \
  15558. + break;
  15559. +
  15560. + /* simple true/false flag */
  15561. + au_fsctx_TF(trunc_xino);
  15562. + au_fsctx_TF(trunc_xib);
  15563. + au_fsctx_TF(dirperm1);
  15564. + au_fsctx_TF(plink);
  15565. + au_fsctx_TF(shwh);
  15566. + au_fsctx_TF(dio);
  15567. + au_fsctx_TF(warn_perm);
  15568. + au_fsctx_TF(verbose);
  15569. + au_fsctx_TF(sum);
  15570. + au_fsctx_TF(dirren);
  15571. + au_fsctx_TF(acl);
  15572. +#undef au_fsctx_TF
  15573. +
  15574. + case Opt_trunc_xino_path:
  15575. + fallthrough;
  15576. + case Opt_itrunc_xino:
  15577. + u.xino_itrunc = &opt->xino_itrunc;
  15578. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  15579. + break;
  15580. + case Opt_noxino:
  15581. + AuLabel(noxino);
  15582. + break;
  15583. +
  15584. + case Opt_list_plink:
  15585. + AuLabel(list_plink);
  15586. + break;
  15587. + case Opt_udba:
  15588. + AuDbg("udba %d, %s\n",
  15589. + opt->udba, au_optstr_udba(opt->udba));
  15590. + break;
  15591. + case Opt_diropq_a:
  15592. + AuLabel(diropq_a);
  15593. + break;
  15594. + case Opt_diropq_w:
  15595. + AuLabel(diropq_w);
  15596. + break;
  15597. + case Opt_wsum:
  15598. + AuLabel(wsum);
  15599. + break;
  15600. + case Opt_wbr_create:
  15601. + u.create = &opt->wbr_create;
  15602. + AuDbg("create %d, %s\n", u.create->wbr_create,
  15603. + au_optstr_wbr_create(u.create->wbr_create));
  15604. + switch (u.create->wbr_create) {
  15605. + case AuWbrCreate_MFSV:
  15606. + fallthrough;
  15607. + case AuWbrCreate_PMFSV:
  15608. + AuDbg("%d sec\n", u.create->mfs_second);
  15609. + break;
  15610. + case AuWbrCreate_MFSRR:
  15611. + fallthrough;
  15612. + case AuWbrCreate_TDMFS:
  15613. + AuDbg("%llu watermark\n",
  15614. + u.create->mfsrr_watermark);
  15615. + break;
  15616. + case AuWbrCreate_MFSRRV:
  15617. + fallthrough;
  15618. + case AuWbrCreate_TDMFSV:
  15619. + fallthrough;
  15620. + case AuWbrCreate_PMFSRRV:
  15621. + AuDbg("%llu watermark, %d sec\n",
  15622. + u.create->mfsrr_watermark,
  15623. + u.create->mfs_second);
  15624. + break;
  15625. + }
  15626. + break;
  15627. + case Opt_wbr_copyup:
  15628. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  15629. + au_optstr_wbr_copyup(opt->wbr_copyup));
  15630. + break;
  15631. + case Opt_fhsm_sec:
  15632. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  15633. + break;
  15634. +
  15635. + default:
  15636. + AuDbg("type %d\n", opt->type);
  15637. + BUG();
  15638. + }
  15639. + opt++;
  15640. + }
  15641. +#endif
  15642. +}
  15643. +
  15644. +/* ---------------------------------------------------------------------- */
  15645. +
  15646. +/*
  15647. + * For conditionally compiled mount options.
  15648. + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
  15649. + */
  15650. +#define au_ignore_flag(name, action) \
  15651. + fsparam_flag(name, action), \
  15652. + fsparam_flag("no" name, Opt_ignore_silent)
  15653. +
  15654. +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
  15655. + fsparam_string("br", Opt_br),
  15656. +
  15657. + /* "add=%d:%s" or "ins=%d:%s" */
  15658. + fsparam_string("add", Opt_add),
  15659. + fsparam_string("ins", Opt_add),
  15660. + fsparam_path("append", Opt_append),
  15661. + fsparam_path("prepend", Opt_prepend),
  15662. +
  15663. + fsparam_path("del", Opt_del),
  15664. + /* fsparam_s32("idel", Opt_idel), */
  15665. + fsparam_path("mod", Opt_mod),
  15666. + /* fsparam_string("imod", Opt_imod), */
  15667. +
  15668. + fsparam_s32("dirwh", Opt_dirwh),
  15669. +
  15670. + fsparam_path("xino", Opt_xino),
  15671. + fsparam_flag("noxino", Opt_noxino),
  15672. + fsparam_flag_no("trunc_xino", Opt_trunc_xino),
  15673. + /* "trunc_xino_v=%d:%d" */
  15674. + /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
  15675. + fsparam_path("trunc_xino", Opt_trunc_xino_path),
  15676. + fsparam_s32("itrunc_xino", Opt_itrunc_xino),
  15677. + /* fsparam_path("zxino", Opt_zxino), */
  15678. + fsparam_flag_no("trunc_xib", Opt_trunc_xib),
  15679. +
  15680. +#ifdef CONFIG_PROC_FS
  15681. + fsparam_flag_no("plink", Opt_plink),
  15682. +#else
  15683. + au_ignore_flag("plink", Opt_ignore),
  15684. +#endif
  15685. +
  15686. +#ifdef CONFIG_AUFS_DEBUG
  15687. + fsparam_flag("list_plink", Opt_list_plink),
  15688. +#endif
  15689. +
  15690. + fsparam_string("udba", Opt_udba),
  15691. +
  15692. + fsparam_flag_no("dio", Opt_dio),
  15693. +
  15694. +#ifdef CONFIG_AUFS_DIRREN
  15695. + fsparam_flag_no("dirren", Opt_dirren),
  15696. +#else
  15697. + au_ignore_flag("dirren", Opt_ignore),
  15698. +#endif
  15699. +
  15700. +#ifdef CONFIG_AUFS_FHSM
  15701. + fsparam_s32("fhsm_sec", Opt_fhsm_sec),
  15702. +#else
  15703. + fsparam_s32("fhsm_sec", Opt_ignore),
  15704. +#endif
  15705. +
  15706. + /* always | a | whiteouted | w */
  15707. + fsparam_string("diropq", Opt_diropq),
  15708. +
  15709. + fsparam_flag_no("warn_perm", Opt_warn_perm),
  15710. +
  15711. +#ifdef CONFIG_AUFS_SHWH
  15712. + fsparam_flag_no("shwh", Opt_shwh),
  15713. +#else
  15714. + au_ignore_flag("shwh", Opt_err),
  15715. +#endif
  15716. +
  15717. + fsparam_flag_no("dirperm1", Opt_dirperm1),
  15718. +
  15719. + fsparam_flag_no("verbose", Opt_verbose),
  15720. + fsparam_flag("v", Opt_verbose),
  15721. + fsparam_flag("quiet", Opt_noverbose),
  15722. + fsparam_flag("q", Opt_noverbose),
  15723. + /* user-space may handle this */
  15724. + fsparam_flag("silent", Opt_noverbose),
  15725. +
  15726. + fsparam_flag_no("sum", Opt_sum),
  15727. + fsparam_flag("wsum", Opt_wsum),
  15728. +
  15729. + fsparam_s32("rdcache", Opt_rdcache),
  15730. + /* "def" or s32 */
  15731. + fsparam_string("rdblk", Opt_rdblk),
  15732. + /* "def" or s32 */
  15733. + fsparam_string("rdhash", Opt_rdhash),
  15734. +
  15735. + fsparam_string("create", Opt_wbr_create),
  15736. + fsparam_string("create_policy", Opt_wbr_create),
  15737. + fsparam_string("cpup", Opt_wbr_copyup),
  15738. + fsparam_string("copyup", Opt_wbr_copyup),
  15739. + fsparam_string("copyup_policy", Opt_wbr_copyup),
  15740. +
  15741. + /* generic VFS flag */
  15742. +#ifdef CONFIG_FS_POSIX_ACL
  15743. + fsparam_flag_no("acl", Opt_acl),
  15744. +#else
  15745. + au_ignore_flag("acl", Opt_ignore),
  15746. +#endif
  15747. +
  15748. + /* internal use for the scripts */
  15749. + fsparam_string("si", Opt_ignore_silent),
  15750. +
  15751. + /* obsoleted, keep them temporary */
  15752. + fsparam_flag("nodlgt", Opt_ignore_silent),
  15753. + fsparam_flag("clean_plink", Opt_ignore),
  15754. + fsparam_string("dirs", Opt_br),
  15755. + fsparam_u32("debug", Opt_ignore),
  15756. + /* "whiteout" or "all" */
  15757. + fsparam_string("delete", Opt_ignore),
  15758. + fsparam_string("imap", Opt_ignore),
  15759. +
  15760. + /* temporary workaround, due to old mount(8)? */
  15761. + fsparam_flag("relatime", Opt_ignore_silent),
  15762. +
  15763. + {}
  15764. +};
  15765. +
  15766. +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
  15767. + char *brspec, size_t speclen,
  15768. + aufs_bindex_t bindex)
  15769. +{
  15770. + int err;
  15771. + char *p;
  15772. +
  15773. + AuDbg("brspec %s\n", brspec);
  15774. +
  15775. + err = -ENOMEM;
  15776. + if (!speclen)
  15777. + speclen = strlen(brspec);
  15778. + /* will be freed by au_fsctx_free() */
  15779. + p = kmemdup_nul(brspec, speclen, GFP_NOFS);
  15780. + if (unlikely(!p)) {
  15781. + errorfc(fc, "failed in %s", brspec);
  15782. + goto out;
  15783. + }
  15784. + err = au_opt_add(opt, p, fc->sb_flags, bindex);
  15785. +
  15786. +out:
  15787. + AuTraceErr(err);
  15788. + return err;
  15789. +}
  15790. +
  15791. +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
  15792. +{
  15793. + int err;
  15794. + char *p;
  15795. + struct au_fsctx_opts *a = fc->fs_private;
  15796. + struct au_opt *opt = a->opt;
  15797. + aufs_bindex_t bindex = a->bindex;
  15798. +
  15799. + AuDbg("brspec %s\n", brspec);
  15800. +
  15801. + err = -EINVAL;
  15802. + while ((p = strsep(&brspec, ":")) && *p) {
  15803. + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
  15804. + AuTraceErr(err);
  15805. + if (unlikely(err))
  15806. + break;
  15807. + bindex++;
  15808. + opt++;
  15809. + if (unlikely(opt > a->opt_tail)) {
  15810. + err = -E2BIG;
  15811. + bindex--;
  15812. + opt--;
  15813. + break;
  15814. + }
  15815. + opt->type = Opt_tail;
  15816. + a->skipped = 1;
  15817. + }
  15818. + a->bindex = bindex;
  15819. + a->opt = opt;
  15820. +
  15821. + AuTraceErr(err);
  15822. + return err;
  15823. +}
  15824. +
  15825. +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
  15826. +{
  15827. + int err, n;
  15828. + char *p;
  15829. + struct au_fsctx_opts *a = fc->fs_private;
  15830. + struct au_opt *opt = a->opt;
  15831. +
  15832. + err = -EINVAL;
  15833. + p = strchr(addspec, ':');
  15834. + if (unlikely(!p)) {
  15835. + errorfc(fc, "bad arg in %s", addspec);
  15836. + goto out;
  15837. + }
  15838. + *p++ = '\0';
  15839. + err = kstrtoint(addspec, 0, &n);
  15840. + if (unlikely(err)) {
  15841. + errorfc(fc, "bad integer in %s", addspec);
  15842. + goto out;
  15843. + }
  15844. + AuDbg("n %d\n", n);
  15845. + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
  15846. +
  15847. +out:
  15848. + AuTraceErr(err);
  15849. + return err;
  15850. +}
  15851. +
  15852. +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
  15853. + struct fs_parameter *param)
  15854. +{
  15855. + int err;
  15856. +
  15857. + err = -ENOMEM;
  15858. + /* will be freed by au_fsctx_free() */
  15859. + del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15860. + if (unlikely(!del->pathname))
  15861. + goto out;
  15862. + AuDbg("del %s\n", del->pathname);
  15863. + err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
  15864. + if (unlikely(err))
  15865. + errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
  15866. +
  15867. +out:
  15868. + AuTraceErr(err);
  15869. + return err;
  15870. +}
  15871. +
  15872. +#if 0 /* reserved for future use */
  15873. +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
  15874. + aufs_bindex_t bindex)
  15875. +{
  15876. + int err;
  15877. + struct super_block *sb;
  15878. + struct dentry *root;
  15879. + struct au_fsctx_opts *a = fc->fs_private;
  15880. +
  15881. + sb = a->sb;
  15882. + AuDebugOn(!sb);
  15883. +
  15884. + err = -EINVAL;
  15885. + root = sb->s_root;
  15886. + aufs_read_lock(root, AuLock_FLUSH);
  15887. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  15888. + errorfc(fc, "out of bounds, %d", bindex);
  15889. + goto out;
  15890. + }
  15891. +
  15892. + err = 0;
  15893. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  15894. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  15895. +
  15896. +out:
  15897. + aufs_read_unlock(root, !AuLock_IR);
  15898. + AuTraceErr(err);
  15899. + return err;
  15900. +}
  15901. +#endif
  15902. +
  15903. +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
  15904. + struct fs_parameter *param)
  15905. +{
  15906. + int err;
  15907. + struct path path;
  15908. + char *p;
  15909. +
  15910. + err = -ENOMEM;
  15911. + /* will be freed by au_fsctx_free() */
  15912. + mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15913. + if (unlikely(!mod->path))
  15914. + goto out;
  15915. +
  15916. + err = -EINVAL;
  15917. + p = strchr(mod->path, '=');
  15918. + if (unlikely(!p)) {
  15919. + errorfc(fc, "no permission %s", mod->path);
  15920. + goto out;
  15921. + }
  15922. +
  15923. + *p++ = 0;
  15924. + err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
  15925. + if (unlikely(err)) {
  15926. + errorfc(fc, "lookup failed %s (%d)", mod->path, err);
  15927. + goto out;
  15928. + }
  15929. +
  15930. + mod->perm = au_br_perm_val(p);
  15931. + AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
  15932. + mod->h_root = dget(path.dentry);
  15933. + path_put(&path);
  15934. +
  15935. +out:
  15936. + AuTraceErr(err);
  15937. + return err;
  15938. +}
  15939. +
  15940. +#if 0 /* reserved for future use */
  15941. +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
  15942. + char *ibrspec)
  15943. +{
  15944. + int err, n;
  15945. + char *p;
  15946. + struct super_block *sb;
  15947. + struct dentry *root;
  15948. + struct au_fsctx_opts *a = fc->fs_private;
  15949. +
  15950. + sb = a->sb;
  15951. + AuDebugOn(!sb);
  15952. +
  15953. + err = -EINVAL;
  15954. + p = strchr(ibrspec, ':');
  15955. + if (unlikely(!p)) {
  15956. + errorfc(fc, "no index, %s", ibrspec);
  15957. + goto out;
  15958. + }
  15959. + *p++ = '\0';
  15960. + err = kstrtoint(ibrspec, 0, &n);
  15961. + if (unlikely(err)) {
  15962. + errorfc(fc, "bad integer in %s", ibrspec);
  15963. + goto out;
  15964. + }
  15965. + AuDbg("n %d\n", n);
  15966. +
  15967. + root = sb->s_root;
  15968. + aufs_read_lock(root, AuLock_FLUSH);
  15969. + if (n < 0 || au_sbbot(sb) < n) {
  15970. + errorfc(fc, "out of bounds, %d", bindex);
  15971. + goto out_root;
  15972. + }
  15973. +
  15974. + err = 0;
  15975. + mod->perm = au_br_perm_val(p);
  15976. + AuDbg("mod path %s, perm 0x%x, %s\n",
  15977. + mod->path, mod->perm, p);
  15978. + mod->h_root = dget(au_h_dptr(root, bindex));
  15979. +
  15980. +out_root:
  15981. + aufs_read_unlock(root, !AuLock_IR);
  15982. +out:
  15983. + AuTraceErr(err);
  15984. + return err;
  15985. +}
  15986. +#endif
  15987. +
  15988. +static int au_fsctx_parse_xino(struct fs_context *fc,
  15989. + struct au_opt_xino *xino,
  15990. + struct fs_parameter *param)
  15991. +{
  15992. + int err;
  15993. + struct au_fsctx_opts *a = fc->fs_private;
  15994. +
  15995. + err = -ENOMEM;
  15996. + /* will be freed by au_opts_free() */
  15997. + xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15998. + if (unlikely(!xino->path))
  15999. + goto out;
  16000. + AuDbg("path %s\n", xino->path);
  16001. +
  16002. + xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
  16003. + /*wbrtop*/0);
  16004. + err = PTR_ERR(xino->file);
  16005. + if (IS_ERR(xino->file)) {
  16006. + xino->file = NULL;
  16007. + goto out;
  16008. + }
  16009. +
  16010. + err = 0;
  16011. + if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
  16012. + err = -EINVAL;
  16013. + errorfc(fc, "%s must be outside", xino->path);
  16014. + }
  16015. +
  16016. +out:
  16017. + AuTraceErr(err);
  16018. + return err;
  16019. +}
  16020. +
  16021. +static
  16022. +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
  16023. + struct au_opt_xino_itrunc *xino_itrunc,
  16024. + char *pathname)
  16025. +{
  16026. + int err;
  16027. + aufs_bindex_t bbot, bindex;
  16028. + struct path path;
  16029. + struct dentry *root;
  16030. + struct au_fsctx_opts *a = fc->fs_private;
  16031. +
  16032. + AuDebugOn(!a->sb);
  16033. +
  16034. + err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
  16035. + if (unlikely(err)) {
  16036. + errorfc(fc, "lookup failed %s (%d)", pathname, err);
  16037. + goto out;
  16038. + }
  16039. +
  16040. + xino_itrunc->bindex = -1;
  16041. + root = a->sb->s_root;
  16042. + aufs_read_lock(root, AuLock_FLUSH);
  16043. + bbot = au_sbbot(a->sb);
  16044. + for (bindex = 0; bindex <= bbot; bindex++) {
  16045. + if (au_h_dptr(root, bindex) == path.dentry) {
  16046. + xino_itrunc->bindex = bindex;
  16047. + break;
  16048. + }
  16049. + }
  16050. + aufs_read_unlock(root, !AuLock_IR);
  16051. + path_put(&path);
  16052. +
  16053. + if (unlikely(xino_itrunc->bindex < 0)) {
  16054. + err = -EINVAL;
  16055. + errorfc(fc, "no such branch %s", pathname);
  16056. + }
  16057. +
  16058. +out:
  16059. + AuTraceErr(err);
  16060. + return err;
  16061. +}
  16062. +
  16063. +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
  16064. + struct au_opt_xino_itrunc *xino_itrunc,
  16065. + unsigned int bindex)
  16066. +{
  16067. + int err;
  16068. + aufs_bindex_t bbot;
  16069. + struct super_block *sb;
  16070. + struct au_fsctx_opts *a = fc->fs_private;
  16071. +
  16072. + sb = a->sb;
  16073. + AuDebugOn(!sb);
  16074. +
  16075. + err = 0;
  16076. + si_noflush_read_lock(sb);
  16077. + bbot = au_sbbot(sb);
  16078. + si_read_unlock(sb);
  16079. + if (bindex <= bbot)
  16080. + xino_itrunc->bindex = bindex;
  16081. + else {
  16082. + err = -EINVAL;
  16083. + errorfc(fc, "out of bounds, %u", bindex);
  16084. + }
  16085. +
  16086. + AuTraceErr(err);
  16087. + return err;
  16088. +}
  16089. +
  16090. +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
  16091. +{
  16092. + int err, token;
  16093. + struct fs_parse_result result;
  16094. + struct au_fsctx_opts *a = fc->fs_private;
  16095. + struct au_opt *opt = a->opt;
  16096. +
  16097. + AuDbg("fc %p, param {key %s, string %s}\n",
  16098. + fc, param->key, param->string);
  16099. + err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
  16100. + if (unlikely(err < 0))
  16101. + goto out;
  16102. + token = err;
  16103. + AuDbg("token %d, res{negated %d, uint64 %llu}\n",
  16104. + token, result.negated, result.uint_64);
  16105. +
  16106. + err = -EINVAL;
  16107. + a->skipped = 0;
  16108. + switch (token) {
  16109. + case Opt_br:
  16110. + err = au_fsctx_parse_br(fc, param->string);
  16111. + break;
  16112. + case Opt_add:
  16113. + err = au_fsctx_parse_add(fc, param->string);
  16114. + break;
  16115. + case Opt_append:
  16116. + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
  16117. + /*dummy bindex*/1);
  16118. + break;
  16119. + case Opt_prepend:
  16120. + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
  16121. + /*bindex*/0);
  16122. + break;
  16123. +
  16124. + case Opt_del:
  16125. + err = au_fsctx_parse_del(fc, &opt->del, param);
  16126. + break;
  16127. +#if 0 /* reserved for future use */
  16128. + case Opt_idel:
  16129. + if (!a->sb) {
  16130. + err = 0;
  16131. + a->skipped = 1;
  16132. + break;
  16133. + }
  16134. + del->pathname = "(indexed)";
  16135. + err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
  16136. + break;
  16137. +#endif
  16138. +
  16139. + case Opt_mod:
  16140. + err = au_fsctx_parse_mod(fc, &opt->mod, param);
  16141. + break;
  16142. +#ifdef IMOD /* reserved for future use */
  16143. + case Opt_imod:
  16144. + if (!a->sb) {
  16145. + err = 0;
  16146. + a->skipped = 1;
  16147. + break;
  16148. + }
  16149. + u.mod->path = "(indexed)";
  16150. + err = au_opts_parse_imod(fc, &opt->mod, param->string);
  16151. + break;
  16152. +#endif
  16153. +
  16154. + case Opt_xino:
  16155. + err = au_fsctx_parse_xino(fc, &opt->xino, param);
  16156. + break;
  16157. + case Opt_trunc_xino_path:
  16158. + if (!a->sb) {
  16159. + errorfc(fc, "no such branch %s", param->string);
  16160. + break;
  16161. + }
  16162. + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
  16163. + param->string);
  16164. + break;
  16165. +#if 0
  16166. + case Opt_trunc_xino_v:
  16167. + if (!a->sb) {
  16168. + err = 0;
  16169. + a->skipped = 1;
  16170. + break;
  16171. + }
  16172. + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
  16173. + param->string);
  16174. + break;
  16175. +#endif
  16176. + case Opt_itrunc_xino:
  16177. + if (!a->sb) {
  16178. + errorfc(fc, "out of bounds %s", param->string);
  16179. + break;
  16180. + }
  16181. + err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
  16182. + result.int_32);
  16183. + break;
  16184. +
  16185. + case Opt_dirwh:
  16186. + err = 0;
  16187. + opt->dirwh = result.int_32;
  16188. + break;
  16189. +
  16190. + case Opt_rdcache:
  16191. + if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
  16192. + errorfc(fc, "rdcache must be smaller than %d",
  16193. + AUFS_RDCACHE_MAX);
  16194. + break;
  16195. + }
  16196. + err = 0;
  16197. + opt->rdcache = result.int_32;
  16198. + break;
  16199. +
  16200. + case Opt_rdblk:
  16201. + err = 0;
  16202. + opt->rdblk = AUFS_RDBLK_DEF;
  16203. + if (!strcmp(param->string, "def"))
  16204. + break;
  16205. +
  16206. + err = kstrtoint(param->string, 0, &result.int_32);
  16207. + if (unlikely(err)) {
  16208. + errorfc(fc, "bad value in %s", param->key);
  16209. + break;
  16210. + }
  16211. + err = -EINVAL;
  16212. + if (unlikely(result.int_32 < 0
  16213. + || result.int_32 > KMALLOC_MAX_SIZE)) {
  16214. + errorfc(fc, "bad value in %s", param->key);
  16215. + break;
  16216. + }
  16217. + if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
  16218. + errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
  16219. + break;
  16220. + }
  16221. + err = 0;
  16222. + opt->rdblk = result.int_32;
  16223. + break;
  16224. +
  16225. + case Opt_rdhash:
  16226. + err = 0;
  16227. + opt->rdhash = AUFS_RDHASH_DEF;
  16228. + if (!strcmp(param->string, "def"))
  16229. + break;
  16230. +
  16231. + err = kstrtoint(param->string, 0, &result.int_32);
  16232. + if (unlikely(err)) {
  16233. + errorfc(fc, "bad value in %s", param->key);
  16234. + break;
  16235. + }
  16236. + /* how about zero? */
  16237. + if (result.int_32 < 0
  16238. + || result.int_32 * sizeof(struct hlist_head)
  16239. + > KMALLOC_MAX_SIZE) {
  16240. + err = -EINVAL;
  16241. + errorfc(fc, "bad integer in %s", param->key);
  16242. + break;
  16243. + }
  16244. + opt->rdhash = result.int_32;
  16245. + break;
  16246. +
  16247. + case Opt_diropq:
  16248. + /*
  16249. + * As other options, fs/aufs/opts.c can handle these strings by
  16250. + * match_token(). But "diropq=" is deprecated now and will
  16251. + * never have other value. So simple strcmp() is enough here.
  16252. + */
  16253. + if (!strcmp(param->string, "a") ||
  16254. + !strcmp(param->string, "always")) {
  16255. + err = 0;
  16256. + opt->type = Opt_diropq_a;
  16257. + } else if (!strcmp(param->string, "w") ||
  16258. + !strcmp(param->string, "whiteouted")) {
  16259. + err = 0;
  16260. + opt->type = Opt_diropq_w;
  16261. + } else
  16262. + errorfc(fc, "unknown value %s", param->string);
  16263. + break;
  16264. +
  16265. + case Opt_udba:
  16266. + opt->udba = au_udba_val(param->string);
  16267. + if (opt->udba >= 0)
  16268. + err = 0;
  16269. + else
  16270. + errorf(fc, "wrong value, %s", param->string);
  16271. + break;
  16272. +
  16273. + case Opt_wbr_create:
  16274. + opt->wbr_create.wbr_create
  16275. + = au_wbr_create_val(param->string, &opt->wbr_create);
  16276. + if (opt->wbr_create.wbr_create >= 0)
  16277. + err = 0;
  16278. + else
  16279. + errorf(fc, "wrong value, %s", param->key);
  16280. + break;
  16281. +
  16282. + case Opt_wbr_copyup:
  16283. + opt->wbr_copyup = au_wbr_copyup_val(param->string);
  16284. + if (opt->wbr_copyup >= 0)
  16285. + err = 0;
  16286. + else
  16287. + errorfc(fc, "wrong value, %s", param->key);
  16288. + break;
  16289. +
  16290. + case Opt_fhsm_sec:
  16291. + if (unlikely(result.int_32 < 0)) {
  16292. + errorfc(fc, "bad integer in %s\n", param->key);
  16293. + break;
  16294. + }
  16295. + err = 0;
  16296. + if (sysaufs_brs)
  16297. + opt->fhsm_second = result.int_32;
  16298. + else
  16299. + warnfc(fc, "ignored %s %s", param->key, param->string);
  16300. + break;
  16301. +
  16302. + /* simple true/false flag */
  16303. +#define au_fsctx_TF(name) \
  16304. + case Opt_##name: \
  16305. + err = 0; \
  16306. + opt->tf = !result.negated; \
  16307. + break
  16308. + au_fsctx_TF(trunc_xino);
  16309. + au_fsctx_TF(trunc_xib);
  16310. + au_fsctx_TF(dirperm1);
  16311. + au_fsctx_TF(plink);
  16312. + au_fsctx_TF(shwh);
  16313. + au_fsctx_TF(dio);
  16314. + au_fsctx_TF(warn_perm);
  16315. + au_fsctx_TF(verbose);
  16316. + au_fsctx_TF(sum);
  16317. + au_fsctx_TF(dirren);
  16318. + au_fsctx_TF(acl);
  16319. +#undef au_fsctx_TF
  16320. +
  16321. + case Opt_noverbose:
  16322. + err = 0;
  16323. + opt->type = Opt_verbose;
  16324. + opt->tf = false;
  16325. + break;
  16326. +
  16327. + case Opt_noxino:
  16328. + fallthrough;
  16329. + case Opt_list_plink:
  16330. + fallthrough;
  16331. + case Opt_wsum:
  16332. + err = 0;
  16333. + break;
  16334. +
  16335. + case Opt_ignore:
  16336. + warnfc(fc, "ignored %s", param->key);
  16337. + fallthrough;
  16338. + case Opt_ignore_silent:
  16339. + a->skipped = 1;
  16340. + err = 0;
  16341. + break;
  16342. + default:
  16343. + a->skipped = 1;
  16344. + err = -ENOPARAM;
  16345. + break;
  16346. + }
  16347. + if (unlikely(err))
  16348. + goto out;
  16349. + if (a->skipped)
  16350. + goto out;
  16351. +
  16352. + switch (token) {
  16353. + case Opt_br:
  16354. + fallthrough;
  16355. + case Opt_noverbose:
  16356. + fallthrough;
  16357. + case Opt_diropq:
  16358. + break;
  16359. + default:
  16360. + opt->type = token;
  16361. + break;
  16362. + }
  16363. + opt++;
  16364. + if (unlikely(opt > a->opt_tail)) {
  16365. + err = -E2BIG;
  16366. + opt--;
  16367. + }
  16368. + opt->type = Opt_tail;
  16369. + a->opt = opt;
  16370. +
  16371. +out:
  16372. + return err;
  16373. +}
  16374. +
  16375. +/*
  16376. + * these options accept both 'name=val' and 'name:val' form.
  16377. + * some accept optional '=' in its value.
  16378. + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
  16379. + */
  16380. +static inline unsigned int is_colonopt(char *str)
  16381. +{
  16382. +#define do_test(name) \
  16383. + if (!strncmp(str, name ":", sizeof(name))) \
  16384. + return sizeof(name) - 1
  16385. + do_test("br");
  16386. + do_test("add");
  16387. + do_test("ins");
  16388. + do_test("append");
  16389. + do_test("prepend");
  16390. + do_test("del");
  16391. + /* do_test("idel"); */
  16392. + do_test("mod");
  16393. + /* do_test("imod"); */
  16394. +#undef do_test
  16395. +
  16396. + return 0;
  16397. +}
  16398. +
  16399. +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
  16400. +{
  16401. + int err;
  16402. + unsigned int u;
  16403. + char *str;
  16404. + struct au_fsctx_opts *a = fc->fs_private;
  16405. +
  16406. + str = data;
  16407. + AuDbg("str %s\n", str);
  16408. + while (str) {
  16409. + u = is_colonopt(str);
  16410. + if (u)
  16411. + str[u] = '=';
  16412. + str = strchr(str, ',');
  16413. + if (!str)
  16414. + break;
  16415. + str++;
  16416. + }
  16417. + str = data;
  16418. + AuDbg("str %s\n", str);
  16419. +
  16420. + err = generic_parse_monolithic(fc, str);
  16421. + AuTraceErr(err);
  16422. + au_fsctx_dump(&a->opts);
  16423. +
  16424. + return err;
  16425. +}
  16426. +
  16427. +/* ---------------------------------------------------------------------- */
  16428. +
  16429. +static void au_fsctx_opts_free(struct au_opts *opts)
  16430. +{
  16431. + struct au_opt *opt;
  16432. +
  16433. + opt = opts->opt;
  16434. + while (opt->type != Opt_tail) {
  16435. + switch (opt->type) {
  16436. + case Opt_add:
  16437. + fallthrough;
  16438. + case Opt_append:
  16439. + fallthrough;
  16440. + case Opt_prepend:
  16441. + kfree(opt->add.pathname);
  16442. + path_put(&opt->add.path);
  16443. + break;
  16444. + case Opt_del:
  16445. + kfree(opt->del.pathname);
  16446. + fallthrough;
  16447. + case Opt_idel:
  16448. + path_put(&opt->del.h_path);
  16449. + break;
  16450. + case Opt_mod:
  16451. + kfree(opt->mod.path);
  16452. + fallthrough;
  16453. + case Opt_imod:
  16454. + dput(opt->mod.h_root);
  16455. + break;
  16456. + case Opt_xino:
  16457. + kfree(opt->xino.path);
  16458. + fput(opt->xino.file);
  16459. + break;
  16460. + }
  16461. + opt++;
  16462. + }
  16463. +}
  16464. +
  16465. +static void au_fsctx_free(struct fs_context *fc)
  16466. +{
  16467. + struct au_fsctx_opts *a = fc->fs_private;
  16468. +
  16469. + /* fs_type=%p, root=%pD */
  16470. + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
  16471. + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
  16472. +
  16473. + kobject_put(&a->sbinfo->si_kobj);
  16474. + au_fsctx_opts_free(&a->opts);
  16475. + free_page((unsigned long)a->opts.opt);
  16476. + au_kfree_rcu(a);
  16477. +}
  16478. +
  16479. +static const struct fs_context_operations au_fsctx_ops = {
  16480. + .free = au_fsctx_free,
  16481. + .parse_param = au_fsctx_parse_param,
  16482. + .parse_monolithic = au_fsctx_parse_monolithic,
  16483. + .get_tree = au_fsctx_get_tree,
  16484. + .reconfigure = au_fsctx_reconfigure
  16485. + /*
  16486. + * nfs4 requires ->dup()? No.
  16487. + * I don't know what is this ->dup() for.
  16488. + */
  16489. +};
  16490. +
  16491. +int aufs_fsctx_init(struct fs_context *fc)
  16492. +{
  16493. + int err;
  16494. + struct au_fsctx_opts *a;
  16495. +
  16496. + /* fs_type=%p, root=%pD */
  16497. + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
  16498. + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
  16499. +
  16500. + /* they will be freed by au_fsctx_free() */
  16501. + err = -ENOMEM;
  16502. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16503. + if (unlikely(!a))
  16504. + goto out;
  16505. + a->bindex = 0;
  16506. + a->opts.opt = (void *)__get_free_page(GFP_NOFS);
  16507. + if (unlikely(!a->opts.opt))
  16508. + goto out_a;
  16509. + a->opt = a->opts.opt;
  16510. + a->opt->type = Opt_tail;
  16511. + a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
  16512. + a->opt_tail = a->opt + a->opts.max_opt - 1;
  16513. + a->opts.sb_flags = fc->sb_flags;
  16514. +
  16515. + a->sb = NULL;
  16516. + if (fc->root) {
  16517. + AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
  16518. + a->opts.flags = AuOpts_REMOUNT;
  16519. + a->sb = fc->root->d_sb;
  16520. + a->sbinfo = au_sbi(a->sb);
  16521. + kobject_get(&a->sbinfo->si_kobj);
  16522. + } else {
  16523. + a->sbinfo = au_si_alloc(a->sb);
  16524. + AuDebugOn(!a->sbinfo);
  16525. + err = PTR_ERR(a->sbinfo);
  16526. + if (IS_ERR(a->sbinfo))
  16527. + goto out_opt;
  16528. + au_rw_write_unlock(&a->sbinfo->si_rwsem);
  16529. + }
  16530. +
  16531. + err = 0;
  16532. + fc->fs_private = a;
  16533. + fc->ops = &au_fsctx_ops;
  16534. + goto out; /* success */
  16535. +
  16536. +out_opt:
  16537. + free_page((unsigned long)a->opts.opt);
  16538. +out_a:
  16539. + au_kfree_rcu(a);
  16540. +out:
  16541. + AuTraceErr(err);
  16542. + return err;
  16543. +}
  16544. diff -Naur null/fs/aufs/fstype.h linux-6.6.63/fs/aufs/fstype.h
  16545. --- /dev/null
  16546. +++ linux-6.6.63/fs/aufs/fstype.h 2024-12-18 15:12:59.296519576 +0300
  16547. @@ -0,0 +1,419 @@
  16548. +/* SPDX-License-Identifier: GPL-2.0 */
  16549. +/*
  16550. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  16551. + *
  16552. + * This program is free software; you can redistribute it and/or modify
  16553. + * it under the terms of the GNU General Public License as published by
  16554. + * the Free Software Foundation; either version 2 of the License, or
  16555. + * (at your option) any later version.
  16556. + *
  16557. + * This program is distributed in the hope that it will be useful,
  16558. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16559. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16560. + * GNU General Public License for more details.
  16561. + *
  16562. + * You should have received a copy of the GNU General Public License
  16563. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16564. + */
  16565. +
  16566. +/*
  16567. + * judging filesystem type
  16568. + */
  16569. +
  16570. +#ifndef __AUFS_FSTYPE_H__
  16571. +#define __AUFS_FSTYPE_H__
  16572. +
  16573. +#ifdef __KERNEL__
  16574. +
  16575. +#include <linux/fs.h>
  16576. +#include <linux/magic.h>
  16577. +#include <linux/nfs_fs.h>
  16578. +#include <linux/romfs_fs.h>
  16579. +
  16580. +static inline int au_test_aufs(struct super_block *sb)
  16581. +{
  16582. + return sb->s_magic == AUFS_SUPER_MAGIC;
  16583. +}
  16584. +
  16585. +static inline const char *au_sbtype(struct super_block *sb)
  16586. +{
  16587. + return sb->s_type->name;
  16588. +}
  16589. +
  16590. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  16591. +{
  16592. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  16593. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  16594. +#else
  16595. + return 0;
  16596. +#endif
  16597. +}
  16598. +
  16599. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  16600. +{
  16601. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  16602. + return sb->s_magic == ROMFS_MAGIC;
  16603. +#else
  16604. + return 0;
  16605. +#endif
  16606. +}
  16607. +
  16608. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  16609. +{
  16610. +#if IS_ENABLED(CONFIG_CRAMFS)
  16611. + return sb->s_magic == CRAMFS_MAGIC;
  16612. +#endif
  16613. + return 0;
  16614. +}
  16615. +
  16616. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  16617. +{
  16618. +#if IS_ENABLED(CONFIG_NFS_FS)
  16619. + return sb->s_magic == NFS_SUPER_MAGIC;
  16620. +#else
  16621. + return 0;
  16622. +#endif
  16623. +}
  16624. +
  16625. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  16626. +{
  16627. +#if IS_ENABLED(CONFIG_FUSE_FS)
  16628. + return sb->s_magic == FUSE_SUPER_MAGIC;
  16629. +#else
  16630. + return 0;
  16631. +#endif
  16632. +}
  16633. +
  16634. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  16635. +{
  16636. +#if IS_ENABLED(CONFIG_XFS_FS)
  16637. + return sb->s_magic == XFS_SB_MAGIC;
  16638. +#else
  16639. + return 0;
  16640. +#endif
  16641. +}
  16642. +
  16643. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  16644. +{
  16645. +#ifdef CONFIG_TMPFS
  16646. + return sb->s_magic == TMPFS_MAGIC;
  16647. +#else
  16648. + return 0;
  16649. +#endif
  16650. +}
  16651. +
  16652. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  16653. +{
  16654. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  16655. + return !strcmp(au_sbtype(sb), "ecryptfs");
  16656. +#else
  16657. + return 0;
  16658. +#endif
  16659. +}
  16660. +
  16661. +static inline int au_test_ramfs(struct super_block *sb)
  16662. +{
  16663. + return sb->s_magic == RAMFS_MAGIC;
  16664. +}
  16665. +
  16666. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  16667. +{
  16668. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  16669. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  16670. +#else
  16671. + return 0;
  16672. +#endif
  16673. +}
  16674. +
  16675. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  16676. +{
  16677. +#ifdef CONFIG_PROC_FS
  16678. + return sb->s_magic == PROC_SUPER_MAGIC;
  16679. +#else
  16680. + return 0;
  16681. +#endif
  16682. +}
  16683. +
  16684. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  16685. +{
  16686. +#ifdef CONFIG_SYSFS
  16687. + return sb->s_magic == SYSFS_MAGIC;
  16688. +#else
  16689. + return 0;
  16690. +#endif
  16691. +}
  16692. +
  16693. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  16694. +{
  16695. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  16696. + return sb->s_magic == CONFIGFS_MAGIC;
  16697. +#else
  16698. + return 0;
  16699. +#endif
  16700. +}
  16701. +
  16702. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  16703. +{
  16704. +#if IS_ENABLED(CONFIG_MINIX_FS)
  16705. + return sb->s_magic == MINIX3_SUPER_MAGIC
  16706. + || sb->s_magic == MINIX2_SUPER_MAGIC
  16707. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  16708. + || sb->s_magic == MINIX_SUPER_MAGIC
  16709. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  16710. +#else
  16711. + return 0;
  16712. +#endif
  16713. +}
  16714. +
  16715. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  16716. +{
  16717. +#if IS_ENABLED(CONFIG_FAT_FS)
  16718. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  16719. +#else
  16720. + return 0;
  16721. +#endif
  16722. +}
  16723. +
  16724. +static inline int au_test_msdos(struct super_block *sb)
  16725. +{
  16726. + return au_test_fat(sb);
  16727. +}
  16728. +
  16729. +static inline int au_test_vfat(struct super_block *sb)
  16730. +{
  16731. + return au_test_fat(sb);
  16732. +}
  16733. +
  16734. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  16735. +{
  16736. +#ifdef CONFIG_SECURITYFS
  16737. + return sb->s_magic == SECURITYFS_MAGIC;
  16738. +#else
  16739. + return 0;
  16740. +#endif
  16741. +}
  16742. +
  16743. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  16744. +{
  16745. +#if IS_ENABLED(CONFIG_SQUASHFS)
  16746. + return sb->s_magic == SQUASHFS_MAGIC;
  16747. +#else
  16748. + return 0;
  16749. +#endif
  16750. +}
  16751. +
  16752. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  16753. +{
  16754. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  16755. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  16756. +#else
  16757. + return 0;
  16758. +#endif
  16759. +}
  16760. +
  16761. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  16762. +{
  16763. +#if IS_ENABLED(CONFIG_XENFS)
  16764. + return sb->s_magic == XENFS_SUPER_MAGIC;
  16765. +#else
  16766. + return 0;
  16767. +#endif
  16768. +}
  16769. +
  16770. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  16771. +{
  16772. +#ifdef CONFIG_DEBUG_FS
  16773. + return sb->s_magic == DEBUGFS_MAGIC;
  16774. +#else
  16775. + return 0;
  16776. +#endif
  16777. +}
  16778. +
  16779. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  16780. +{
  16781. +#if IS_ENABLED(CONFIG_NILFS)
  16782. + return sb->s_magic == NILFS_SUPER_MAGIC;
  16783. +#else
  16784. + return 0;
  16785. +#endif
  16786. +}
  16787. +
  16788. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  16789. +{
  16790. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  16791. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  16792. +#else
  16793. + return 0;
  16794. +#endif
  16795. +}
  16796. +
  16797. +static inline int au_test_f2fs(struct super_block *sb __maybe_unused)
  16798. +{
  16799. +#if IS_ENABLED(CONFIG_F2FS_FS)
  16800. + return sb->s_magic == F2FS_SUPER_MAGIC;
  16801. +#else
  16802. + return 0;
  16803. +#endif
  16804. +}
  16805. +
  16806. +/* ---------------------------------------------------------------------- */
  16807. +/*
  16808. + * they can't be an aufs branch.
  16809. + */
  16810. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  16811. +{
  16812. + return
  16813. +#ifndef CONFIG_AUFS_BR_RAMFS
  16814. + au_test_ramfs(sb) ||
  16815. +#endif
  16816. + au_test_procfs(sb)
  16817. + || au_test_sysfs(sb)
  16818. + || au_test_configfs(sb)
  16819. + || au_test_debugfs(sb)
  16820. + || au_test_securityfs(sb)
  16821. + || au_test_xenfs(sb)
  16822. + || au_test_ecryptfs(sb)
  16823. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  16824. + || au_test_aufs(sb); /* will be supported in next version */
  16825. +}
  16826. +
  16827. +static inline int au_test_fs_remote(struct super_block *sb)
  16828. +{
  16829. + return !au_test_tmpfs(sb)
  16830. +#ifdef CONFIG_AUFS_BR_RAMFS
  16831. + && !au_test_ramfs(sb)
  16832. +#endif
  16833. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  16834. +}
  16835. +
  16836. +/* ---------------------------------------------------------------------- */
  16837. +
  16838. +/*
  16839. + * Note: these functions (below) are created after reading ->getattr() in all
  16840. + * filesystems under linux/fs. it means we have to do so in every update...
  16841. + */
  16842. +
  16843. +/*
  16844. + * some filesystems require getattr to refresh the inode attributes before
  16845. + * referencing.
  16846. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  16847. + * and leave the work for d_revalidate()
  16848. + */
  16849. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  16850. +{
  16851. + return au_test_nfs(sb)
  16852. + || au_test_fuse(sb)
  16853. + /* || au_test_btrfs(sb) */ /* untested */
  16854. + ;
  16855. +}
  16856. +
  16857. +/*
  16858. + * filesystems which don't maintain i_size or i_blocks.
  16859. + */
  16860. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  16861. +{
  16862. + return au_test_xfs(sb)
  16863. + || au_test_btrfs(sb)
  16864. + || au_test_ubifs(sb)
  16865. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  16866. + /* || au_test_minix(sb) */ /* untested */
  16867. + ;
  16868. +}
  16869. +
  16870. +/*
  16871. + * filesystems which don't store the correct value in some of their inode
  16872. + * attributes.
  16873. + */
  16874. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  16875. +{
  16876. + return au_test_fs_bad_iattr_size(sb)
  16877. + || au_test_fat(sb)
  16878. + || au_test_msdos(sb)
  16879. + || au_test_vfat(sb);
  16880. +}
  16881. +
  16882. +/* they don't check i_nlink in link(2) */
  16883. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  16884. +{
  16885. + return au_test_tmpfs(sb)
  16886. +#ifdef CONFIG_AUFS_BR_RAMFS
  16887. + || au_test_ramfs(sb)
  16888. +#endif
  16889. + || au_test_ubifs(sb)
  16890. + || au_test_hfsplus(sb);
  16891. +}
  16892. +
  16893. +/*
  16894. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  16895. + */
  16896. +static inline int au_test_fs_notime(struct super_block *sb)
  16897. +{
  16898. + return au_test_nfs(sb)
  16899. + || au_test_fuse(sb)
  16900. + || au_test_ubifs(sb)
  16901. + ;
  16902. +}
  16903. +
  16904. +/* temporary support for i#1 in cramfs */
  16905. +static inline int au_test_fs_unique_ino(struct inode *inode)
  16906. +{
  16907. + if (au_test_cramfs(inode->i_sb))
  16908. + return inode->i_ino != 1;
  16909. + return 1;
  16910. +}
  16911. +
  16912. +/* ---------------------------------------------------------------------- */
  16913. +
  16914. +/*
  16915. + * the filesystem where the xino files placed must support i/o after unlink and
  16916. + * maintain i_size and i_blocks.
  16917. + */
  16918. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  16919. +{
  16920. + return au_test_fs_remote(sb)
  16921. + || au_test_fs_bad_iattr_size(sb)
  16922. + /* don't want unnecessary work for xino */
  16923. + || au_test_aufs(sb)
  16924. + || au_test_ecryptfs(sb)
  16925. + || au_test_nilfs(sb);
  16926. +}
  16927. +
  16928. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  16929. +{
  16930. + return au_test_tmpfs(sb)
  16931. + || au_test_ramfs(sb);
  16932. +}
  16933. +
  16934. +/*
  16935. + * test if the @sb is real-readonly.
  16936. + */
  16937. +static inline int au_test_fs_rr(struct super_block *sb)
  16938. +{
  16939. + return au_test_squashfs(sb)
  16940. + || au_test_iso9660(sb)
  16941. + || au_test_cramfs(sb)
  16942. + || au_test_romfs(sb);
  16943. +}
  16944. +
  16945. +/*
  16946. + * test if the @inode is nfs with 'noacl' option
  16947. + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
  16948. + */
  16949. +static inline int au_test_nfs_noacl(struct inode *inode)
  16950. +{
  16951. + return au_test_nfs(inode->i_sb)
  16952. + /* && IS_POSIXACL(inode) */
  16953. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  16954. +}
  16955. +
  16956. +/*
  16957. + * filesystems which requires inode-lock in open.
  16958. + */
  16959. +static inline int au_test_fs_unlock_for_open(struct super_block *sb)
  16960. +{
  16961. + return au_test_nfs(sb)
  16962. + || au_test_f2fs(sb);
  16963. +}
  16964. +
  16965. +#endif /* __KERNEL__ */
  16966. +#endif /* __AUFS_FSTYPE_H__ */
  16967. diff -Naur null/fs/aufs/hbl.h linux-6.6.63/fs/aufs/hbl.h
  16968. --- /dev/null
  16969. +++ linux-6.6.63/fs/aufs/hbl.h 2024-12-18 15:12:59.296519576 +0300
  16970. @@ -0,0 +1,65 @@
  16971. +/* SPDX-License-Identifier: GPL-2.0 */
  16972. +/*
  16973. + * Copyright (C) 2017-2022 Junjiro R. Okajima
  16974. + *
  16975. + * This program is free software; you can redistribute it and/or modify
  16976. + * it under the terms of the GNU General Public License as published by
  16977. + * the Free Software Foundation; either version 2 of the License, or
  16978. + * (at your option) any later version.
  16979. + *
  16980. + * This program is distributed in the hope that it will be useful,
  16981. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16982. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16983. + * GNU General Public License for more details.
  16984. + *
  16985. + * You should have received a copy of the GNU General Public License
  16986. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16987. + */
  16988. +
  16989. +/*
  16990. + * helpers for hlist_bl.h
  16991. + */
  16992. +
  16993. +#ifndef __AUFS_HBL_H__
  16994. +#define __AUFS_HBL_H__
  16995. +
  16996. +#ifdef __KERNEL__
  16997. +
  16998. +#include <linux/list_bl.h>
  16999. +
  17000. +static inline void au_hbl_add(struct hlist_bl_node *node,
  17001. + struct hlist_bl_head *hbl)
  17002. +{
  17003. + hlist_bl_lock(hbl);
  17004. + hlist_bl_add_head(node, hbl);
  17005. + hlist_bl_unlock(hbl);
  17006. +}
  17007. +
  17008. +static inline void au_hbl_del(struct hlist_bl_node *node,
  17009. + struct hlist_bl_head *hbl)
  17010. +{
  17011. + hlist_bl_lock(hbl);
  17012. + hlist_bl_del(node);
  17013. + hlist_bl_unlock(hbl);
  17014. +}
  17015. +
  17016. +#define au_hbl_for_each(pos, head) \
  17017. + for (pos = hlist_bl_first(head); \
  17018. + pos; \
  17019. + pos = pos->next)
  17020. +
  17021. +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
  17022. +{
  17023. + unsigned long cnt;
  17024. + struct hlist_bl_node *pos;
  17025. +
  17026. + cnt = 0;
  17027. + hlist_bl_lock(hbl);
  17028. + au_hbl_for_each(pos, hbl)
  17029. + cnt++;
  17030. + hlist_bl_unlock(hbl);
  17031. + return cnt;
  17032. +}
  17033. +
  17034. +#endif /* __KERNEL__ */
  17035. +#endif /* __AUFS_HBL_H__ */
  17036. diff -Naur null/fs/aufs/hfsnotify.c linux-6.6.63/fs/aufs/hfsnotify.c
  17037. --- /dev/null
  17038. +++ linux-6.6.63/fs/aufs/hfsnotify.c 2024-12-18 15:12:59.296519576 +0300
  17039. @@ -0,0 +1,290 @@
  17040. +// SPDX-License-Identifier: GPL-2.0
  17041. +/*
  17042. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  17043. + *
  17044. + * This program is free software; you can redistribute it and/or modify
  17045. + * it under the terms of the GNU General Public License as published by
  17046. + * the Free Software Foundation; either version 2 of the License, or
  17047. + * (at your option) any later version.
  17048. + *
  17049. + * This program is distributed in the hope that it will be useful,
  17050. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17051. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17052. + * GNU General Public License for more details.
  17053. + *
  17054. + * You should have received a copy of the GNU General Public License
  17055. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17056. + */
  17057. +
  17058. +/*
  17059. + * fsnotify for the lower directories
  17060. + */
  17061. +
  17062. +#include "aufs.h"
  17063. +
  17064. +/* FS_IN_IGNORED is unnecessary */
  17065. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  17066. + | FS_CREATE | FS_EVENT_ON_CHILD);
  17067. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  17068. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  17069. +
  17070. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  17071. +{
  17072. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  17073. + hn_mark);
  17074. + /* AuDbg("here\n"); */
  17075. + au_cache_free_hnotify(hn);
  17076. + smp_mb__before_atomic(); /* for atomic64_dec */
  17077. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  17078. + wake_up(&au_hfsn_wq);
  17079. +}
  17080. +
  17081. +static int au_hfsn_alloc(struct au_hinode *hinode)
  17082. +{
  17083. + int err;
  17084. + struct au_hnotify *hn;
  17085. + struct super_block *sb;
  17086. + struct au_branch *br;
  17087. + struct fsnotify_mark *mark;
  17088. + aufs_bindex_t bindex;
  17089. +
  17090. + hn = hinode->hi_notify;
  17091. + sb = hn->hn_aufs_inode->i_sb;
  17092. + bindex = au_br_index(sb, hinode->hi_id);
  17093. + br = au_sbr(sb, bindex);
  17094. + AuDebugOn(!br->br_hfsn);
  17095. +
  17096. + mark = &hn->hn_mark;
  17097. + fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
  17098. + mark->mask = AuHfsnMask;
  17099. + /*
  17100. + * by udba rename or rmdir, aufs assign a new inode to the known
  17101. + * h_inode, so specify 1 to allow dups.
  17102. + */
  17103. + lockdep_off();
  17104. + err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
  17105. + lockdep_on();
  17106. +
  17107. + return err;
  17108. +}
  17109. +
  17110. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  17111. +{
  17112. + struct fsnotify_mark *mark;
  17113. + unsigned long long ull;
  17114. + struct fsnotify_group *group;
  17115. +
  17116. + ull = atomic64_inc_return(&au_hfsn_ifree);
  17117. + BUG_ON(!ull);
  17118. +
  17119. + mark = &hn->hn_mark;
  17120. + spin_lock(&mark->lock);
  17121. + group = mark->group;
  17122. + fsnotify_get_group(group);
  17123. + spin_unlock(&mark->lock);
  17124. + lockdep_off();
  17125. + fsnotify_destroy_mark(mark, group);
  17126. + fsnotify_put_mark(mark);
  17127. + fsnotify_put_group(group);
  17128. + lockdep_on();
  17129. +
  17130. + /* free hn by myself */
  17131. + return 0;
  17132. +}
  17133. +
  17134. +/* ---------------------------------------------------------------------- */
  17135. +
  17136. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  17137. +{
  17138. + struct fsnotify_mark *mark;
  17139. +
  17140. + mark = &hinode->hi_notify->hn_mark;
  17141. + spin_lock(&mark->lock);
  17142. + if (do_set) {
  17143. + AuDebugOn(mark->mask & AuHfsnMask);
  17144. + mark->mask |= AuHfsnMask;
  17145. + } else {
  17146. + AuDebugOn(!(mark->mask & AuHfsnMask));
  17147. + mark->mask &= ~AuHfsnMask;
  17148. + }
  17149. + spin_unlock(&mark->lock);
  17150. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  17151. +}
  17152. +
  17153. +/* ---------------------------------------------------------------------- */
  17154. +
  17155. +/* #define AuDbgHnotify */
  17156. +#ifdef AuDbgHnotify
  17157. +static char *au_hfsn_name(u32 mask)
  17158. +{
  17159. +#ifdef CONFIG_AUFS_DEBUG
  17160. +#define test_ret(flag) \
  17161. + do { \
  17162. + if (mask & flag) \
  17163. + return #flag; \
  17164. + } while (0)
  17165. + test_ret(FS_ACCESS);
  17166. + test_ret(FS_MODIFY);
  17167. + test_ret(FS_ATTRIB);
  17168. + test_ret(FS_CLOSE_WRITE);
  17169. + test_ret(FS_CLOSE_NOWRITE);
  17170. + test_ret(FS_OPEN);
  17171. + test_ret(FS_MOVED_FROM);
  17172. + test_ret(FS_MOVED_TO);
  17173. + test_ret(FS_CREATE);
  17174. + test_ret(FS_DELETE);
  17175. + test_ret(FS_DELETE_SELF);
  17176. + test_ret(FS_MOVE_SELF);
  17177. + test_ret(FS_UNMOUNT);
  17178. + test_ret(FS_Q_OVERFLOW);
  17179. + test_ret(FS_IN_IGNORED);
  17180. + test_ret(FS_ISDIR);
  17181. + test_ret(FS_IN_ONESHOT);
  17182. + test_ret(FS_EVENT_ON_CHILD);
  17183. + return "";
  17184. +#undef test_ret
  17185. +#else
  17186. + return "??";
  17187. +#endif
  17188. +}
  17189. +#endif
  17190. +
  17191. +/* ---------------------------------------------------------------------- */
  17192. +
  17193. +static void au_hfsn_free_group(struct fsnotify_group *group)
  17194. +{
  17195. + struct au_br_hfsnotify *hfsn = group->private;
  17196. +
  17197. + /* AuDbg("here\n"); */
  17198. + au_kfree_try_rcu(hfsn);
  17199. +}
  17200. +
  17201. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  17202. + u32 mask, const void *data, int data_type,
  17203. + struct inode *dir,
  17204. + const struct qstr *file_name, u32 cookie,
  17205. + struct fsnotify_iter_info *iter_info)
  17206. +{
  17207. + int err;
  17208. + struct au_hnotify *hnotify;
  17209. + struct inode *h_dir, *h_inode;
  17210. + struct fsnotify_mark *inode_mark;
  17211. +
  17212. + AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
  17213. + || data_type == FSNOTIFY_EVENT_DENTRY));
  17214. +
  17215. + err = 0;
  17216. + /* if FS_UNMOUNT happens, there must be another bug */
  17217. + AuDebugOn(mask & FS_UNMOUNT);
  17218. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  17219. + goto out;
  17220. +
  17221. + h_dir = dir;
  17222. + h_inode = NULL;
  17223. +#ifdef AuDbgHnotify
  17224. + au_debug_on();
  17225. + if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
  17226. + || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
  17227. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  17228. + h_dir->i_ino, mask, au_hfsn_name(mask),
  17229. + AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
  17230. + /* WARN_ON(1); */
  17231. + }
  17232. + au_debug_off();
  17233. +#endif
  17234. +
  17235. + inode_mark = fsnotify_iter_inode_mark(iter_info);
  17236. + AuDebugOn(!inode_mark);
  17237. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  17238. + err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
  17239. +
  17240. +out:
  17241. + return err;
  17242. +}
  17243. +
  17244. +static struct fsnotify_ops au_hfsn_ops = {
  17245. + .handle_event = au_hfsn_handle_event,
  17246. + .free_group_priv = au_hfsn_free_group,
  17247. + .free_mark = au_hfsn_free_mark
  17248. +};
  17249. +
  17250. +/* ---------------------------------------------------------------------- */
  17251. +
  17252. +static void au_hfsn_fin_br(struct au_branch *br)
  17253. +{
  17254. + struct au_br_hfsnotify *hfsn;
  17255. +
  17256. + hfsn = br->br_hfsn;
  17257. + if (hfsn) {
  17258. + lockdep_off();
  17259. + fsnotify_put_group(hfsn->hfsn_group);
  17260. + lockdep_on();
  17261. + }
  17262. +}
  17263. +
  17264. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  17265. +{
  17266. + int err;
  17267. + struct fsnotify_group *group;
  17268. + struct au_br_hfsnotify *hfsn;
  17269. +
  17270. + err = 0;
  17271. + br->br_hfsn = NULL;
  17272. + if (!au_br_hnotifyable(perm))
  17273. + goto out;
  17274. +
  17275. + err = -ENOMEM;
  17276. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  17277. + if (unlikely(!hfsn))
  17278. + goto out;
  17279. +
  17280. + err = 0;
  17281. + group = fsnotify_alloc_group(&au_hfsn_ops,
  17282. + /*flags - not for userspace*/0);
  17283. + if (IS_ERR(group)) {
  17284. + err = PTR_ERR(group);
  17285. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  17286. + goto out_hfsn;
  17287. + }
  17288. +
  17289. + group->private = hfsn;
  17290. + hfsn->hfsn_group = group;
  17291. + br->br_hfsn = hfsn;
  17292. + goto out; /* success */
  17293. +
  17294. +out_hfsn:
  17295. + au_kfree_try_rcu(hfsn);
  17296. +out:
  17297. + return err;
  17298. +}
  17299. +
  17300. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  17301. +{
  17302. + int err;
  17303. +
  17304. + err = 0;
  17305. + if (!br->br_hfsn)
  17306. + err = au_hfsn_init_br(br, perm);
  17307. +
  17308. + return err;
  17309. +}
  17310. +
  17311. +/* ---------------------------------------------------------------------- */
  17312. +
  17313. +static void au_hfsn_fin(void)
  17314. +{
  17315. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  17316. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  17317. +}
  17318. +
  17319. +const struct au_hnotify_op au_hnotify_op = {
  17320. + .ctl = au_hfsn_ctl,
  17321. + .alloc = au_hfsn_alloc,
  17322. + .free = au_hfsn_free,
  17323. +
  17324. + .fin = au_hfsn_fin,
  17325. +
  17326. + .reset_br = au_hfsn_reset_br,
  17327. + .fin_br = au_hfsn_fin_br,
  17328. + .init_br = au_hfsn_init_br
  17329. +};
  17330. diff -Naur null/fs/aufs/hfsplus.c linux-6.6.63/fs/aufs/hfsplus.c
  17331. --- /dev/null
  17332. +++ linux-6.6.63/fs/aufs/hfsplus.c 2024-12-18 15:12:59.296519576 +0300
  17333. @@ -0,0 +1,60 @@
  17334. +// SPDX-License-Identifier: GPL-2.0
  17335. +/*
  17336. + * Copyright (C) 2010-2022 Junjiro R. Okajima
  17337. + *
  17338. + * This program is free software; you can redistribute it and/or modify
  17339. + * it under the terms of the GNU General Public License as published by
  17340. + * the Free Software Foundation; either version 2 of the License, or
  17341. + * (at your option) any later version.
  17342. + *
  17343. + * This program is distributed in the hope that it will be useful,
  17344. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17345. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17346. + * GNU General Public License for more details.
  17347. + *
  17348. + * You should have received a copy of the GNU General Public License
  17349. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17350. + */
  17351. +
  17352. +/*
  17353. + * special support for filesystems which acquires an inode mutex
  17354. + * at final closing a file, eg, hfsplus.
  17355. + *
  17356. + * This trick is very simple and stupid, just to open the file before really
  17357. + * necessary open to tell hfsplus that this is not the final closing.
  17358. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  17359. + * and au_h_open_post() after releasing it.
  17360. + */
  17361. +
  17362. +#include "aufs.h"
  17363. +
  17364. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  17365. + int force_wr)
  17366. +{
  17367. + struct file *h_file;
  17368. + struct dentry *h_dentry;
  17369. +
  17370. + h_dentry = au_h_dptr(dentry, bindex);
  17371. + AuDebugOn(!h_dentry);
  17372. + AuDebugOn(d_is_negative(h_dentry));
  17373. +
  17374. + h_file = NULL;
  17375. + if (au_test_hfsplus(h_dentry->d_sb)
  17376. + && d_is_reg(h_dentry))
  17377. + h_file = au_h_open(dentry, bindex,
  17378. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  17379. + /*file*/NULL, force_wr);
  17380. + return h_file;
  17381. +}
  17382. +
  17383. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  17384. + struct file *h_file)
  17385. +{
  17386. + struct au_branch *br;
  17387. +
  17388. + if (h_file) {
  17389. + fput(h_file);
  17390. + br = au_sbr(dentry->d_sb, bindex);
  17391. + au_lcnt_dec(&br->br_nfiles);
  17392. + }
  17393. +}
  17394. diff -Naur null/fs/aufs/hnotify.c linux-6.6.63/fs/aufs/hnotify.c
  17395. --- /dev/null
  17396. +++ linux-6.6.63/fs/aufs/hnotify.c 2024-12-18 15:12:59.296519576 +0300
  17397. @@ -0,0 +1,715 @@
  17398. +// SPDX-License-Identifier: GPL-2.0
  17399. +/*
  17400. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  17401. + *
  17402. + * This program is free software; you can redistribute it and/or modify
  17403. + * it under the terms of the GNU General Public License as published by
  17404. + * the Free Software Foundation; either version 2 of the License, or
  17405. + * (at your option) any later version.
  17406. + *
  17407. + * This program is distributed in the hope that it will be useful,
  17408. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17409. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17410. + * GNU General Public License for more details.
  17411. + *
  17412. + * You should have received a copy of the GNU General Public License
  17413. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17414. + */
  17415. +
  17416. +/*
  17417. + * abstraction to notify the direct changes on lower directories
  17418. + */
  17419. +
  17420. +/* #include <linux/iversion.h> */
  17421. +#include "aufs.h"
  17422. +
  17423. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  17424. +{
  17425. + int err;
  17426. + struct au_hnotify *hn;
  17427. +
  17428. + err = -ENOMEM;
  17429. + hn = au_cache_alloc_hnotify();
  17430. + if (hn) {
  17431. + hn->hn_aufs_inode = inode;
  17432. + hinode->hi_notify = hn;
  17433. + err = au_hnotify_op.alloc(hinode);
  17434. + AuTraceErr(err);
  17435. + if (unlikely(err)) {
  17436. + hinode->hi_notify = NULL;
  17437. + au_cache_free_hnotify(hn);
  17438. + /*
  17439. + * The upper dir was removed by udba, but the same named
  17440. + * dir left. In this case, aufs assigns a new inode
  17441. + * number and set the monitor again.
  17442. + * For the lower dir, the old monitor is still left.
  17443. + */
  17444. + if (err == -EEXIST)
  17445. + err = 0;
  17446. + }
  17447. + }
  17448. +
  17449. + AuTraceErr(err);
  17450. + return err;
  17451. +}
  17452. +
  17453. +void au_hn_free(struct au_hinode *hinode)
  17454. +{
  17455. + struct au_hnotify *hn;
  17456. +
  17457. + hn = hinode->hi_notify;
  17458. + if (hn) {
  17459. + hinode->hi_notify = NULL;
  17460. + if (au_hnotify_op.free(hinode, hn))
  17461. + au_cache_free_hnotify(hn);
  17462. + }
  17463. +}
  17464. +
  17465. +/* ---------------------------------------------------------------------- */
  17466. +
  17467. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  17468. +{
  17469. + if (hinode->hi_notify)
  17470. + au_hnotify_op.ctl(hinode, do_set);
  17471. +}
  17472. +
  17473. +void au_hn_reset(struct inode *inode, unsigned int flags)
  17474. +{
  17475. + aufs_bindex_t bindex, bbot;
  17476. + struct inode *hi;
  17477. + struct dentry *iwhdentry;
  17478. +
  17479. + bbot = au_ibbot(inode);
  17480. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  17481. + hi = au_h_iptr(inode, bindex);
  17482. + if (!hi)
  17483. + continue;
  17484. +
  17485. + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
  17486. + iwhdentry = au_hi_wh(inode, bindex);
  17487. + if (iwhdentry)
  17488. + dget(iwhdentry);
  17489. + au_igrab(hi);
  17490. + au_set_h_iptr(inode, bindex, NULL, 0);
  17491. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  17492. + flags & ~AuHi_XINO);
  17493. + iput(hi);
  17494. + dput(iwhdentry);
  17495. + /* inode_unlock(hi); */
  17496. + }
  17497. +}
  17498. +
  17499. +/* ---------------------------------------------------------------------- */
  17500. +
  17501. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  17502. +{
  17503. + int err;
  17504. + aufs_bindex_t bindex, bbot, bfound, btop;
  17505. + struct inode *h_i;
  17506. +
  17507. + err = 0;
  17508. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17509. + pr_warn("branch root dir was changed\n");
  17510. + goto out;
  17511. + }
  17512. +
  17513. + bfound = -1;
  17514. + bbot = au_ibbot(inode);
  17515. + btop = au_ibtop(inode);
  17516. +#if 0 /* reserved for future use */
  17517. + if (bindex == bbot) {
  17518. + /* keep this ino in rename case */
  17519. + goto out;
  17520. + }
  17521. +#endif
  17522. + for (bindex = btop; bindex <= bbot; bindex++)
  17523. + if (au_h_iptr(inode, bindex) == h_inode) {
  17524. + bfound = bindex;
  17525. + break;
  17526. + }
  17527. + if (bfound < 0)
  17528. + goto out;
  17529. +
  17530. + for (bindex = btop; bindex <= bbot; bindex++) {
  17531. + h_i = au_h_iptr(inode, bindex);
  17532. + if (!h_i)
  17533. + continue;
  17534. +
  17535. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  17536. + /* ignore this error */
  17537. + /* bad action? */
  17538. + }
  17539. +
  17540. + /* children inode number will be broken */
  17541. +
  17542. +out:
  17543. + AuTraceErr(err);
  17544. + return err;
  17545. +}
  17546. +
  17547. +static int hn_gen_tree(struct dentry *dentry)
  17548. +{
  17549. + int err, i, j, ndentry;
  17550. + struct au_dcsub_pages dpages;
  17551. + struct au_dpage *dpage;
  17552. + struct dentry **dentries;
  17553. +
  17554. + err = au_dpages_init(&dpages, GFP_NOFS);
  17555. + if (unlikely(err))
  17556. + goto out;
  17557. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  17558. + if (unlikely(err))
  17559. + goto out_dpages;
  17560. +
  17561. + for (i = 0; i < dpages.ndpage; i++) {
  17562. + dpage = dpages.dpages + i;
  17563. + dentries = dpage->dentries;
  17564. + ndentry = dpage->ndentry;
  17565. + for (j = 0; j < ndentry; j++) {
  17566. + struct dentry *d;
  17567. +
  17568. + d = dentries[j];
  17569. + if (IS_ROOT(d))
  17570. + continue;
  17571. +
  17572. + au_digen_dec(d);
  17573. + if (d_really_is_positive(d))
  17574. + /* todo: reset children xino?
  17575. + cached children only? */
  17576. + au_iigen_dec(d_inode(d));
  17577. + }
  17578. + }
  17579. +
  17580. +out_dpages:
  17581. + au_dpages_free(&dpages);
  17582. +out:
  17583. + return err;
  17584. +}
  17585. +
  17586. +/*
  17587. + * return 0 if processed.
  17588. + */
  17589. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  17590. + const unsigned int isdir)
  17591. +{
  17592. + int err;
  17593. + struct dentry *d;
  17594. + struct qstr *dname;
  17595. +
  17596. + err = 1;
  17597. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17598. + pr_warn("branch root dir was changed\n");
  17599. + err = 0;
  17600. + goto out;
  17601. + }
  17602. +
  17603. + if (!isdir) {
  17604. + AuDebugOn(!name);
  17605. + au_iigen_dec(inode);
  17606. + spin_lock(&inode->i_lock);
  17607. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  17608. + spin_lock(&d->d_lock);
  17609. + dname = &d->d_name;
  17610. + if (dname->len != nlen
  17611. + && memcmp(dname->name, name, nlen)) {
  17612. + spin_unlock(&d->d_lock);
  17613. + continue;
  17614. + }
  17615. + err = 0;
  17616. + au_digen_dec(d);
  17617. + spin_unlock(&d->d_lock);
  17618. + break;
  17619. + }
  17620. + spin_unlock(&inode->i_lock);
  17621. + } else {
  17622. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  17623. + d = d_find_any_alias(inode);
  17624. + if (!d) {
  17625. + au_iigen_dec(inode);
  17626. + goto out;
  17627. + }
  17628. +
  17629. + spin_lock(&d->d_lock);
  17630. + dname = &d->d_name;
  17631. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  17632. + spin_unlock(&d->d_lock);
  17633. + err = hn_gen_tree(d);
  17634. + spin_lock(&d->d_lock);
  17635. + }
  17636. + spin_unlock(&d->d_lock);
  17637. + dput(d);
  17638. + }
  17639. +
  17640. +out:
  17641. + AuTraceErr(err);
  17642. + return err;
  17643. +}
  17644. +
  17645. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  17646. +{
  17647. + int err;
  17648. +
  17649. + if (IS_ROOT(dentry)) {
  17650. + pr_warn("branch root dir was changed\n");
  17651. + return 0;
  17652. + }
  17653. +
  17654. + err = 0;
  17655. + if (!isdir) {
  17656. + au_digen_dec(dentry);
  17657. + if (d_really_is_positive(dentry))
  17658. + au_iigen_dec(d_inode(dentry));
  17659. + } else {
  17660. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  17661. + if (d_really_is_positive(dentry))
  17662. + err = hn_gen_tree(dentry);
  17663. + }
  17664. +
  17665. + AuTraceErr(err);
  17666. + return err;
  17667. +}
  17668. +
  17669. +/* ---------------------------------------------------------------------- */
  17670. +
  17671. +/* hnotify job flags */
  17672. +#define AuHnJob_XINO0 1
  17673. +#define AuHnJob_GEN (1 << 1)
  17674. +#define AuHnJob_DIRENT (1 << 2)
  17675. +#define AuHnJob_ISDIR (1 << 3)
  17676. +#define AuHnJob_TRYXINO0 (1 << 4)
  17677. +#define AuHnJob_MNTPNT (1 << 5)
  17678. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  17679. +#define au_fset_hnjob(flags, name) \
  17680. + do { (flags) |= AuHnJob_##name; } while (0)
  17681. +#define au_fclr_hnjob(flags, name) \
  17682. + do { (flags) &= ~AuHnJob_##name; } while (0)
  17683. +
  17684. +enum {
  17685. + AuHn_CHILD,
  17686. + AuHn_PARENT,
  17687. + AuHnLast
  17688. +};
  17689. +
  17690. +struct au_hnotify_args {
  17691. + struct inode *h_dir, *dir, *h_child_inode;
  17692. + u32 mask;
  17693. + unsigned int flags[AuHnLast];
  17694. + unsigned int h_child_nlen;
  17695. + char h_child_name[];
  17696. +};
  17697. +
  17698. +struct hn_job_args {
  17699. + unsigned int flags;
  17700. + struct inode *inode, *h_inode, *dir, *h_dir;
  17701. + struct dentry *dentry;
  17702. + char *h_name;
  17703. + int h_nlen;
  17704. +};
  17705. +
  17706. +static int hn_job(struct hn_job_args *a)
  17707. +{
  17708. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  17709. + int e;
  17710. +
  17711. + /* reset xino */
  17712. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  17713. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  17714. +
  17715. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  17716. + && a->inode
  17717. + && a->h_inode) {
  17718. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  17719. + if (!vfsub_inode_nlink(a->h_inode, AU_I_BRANCH)
  17720. + && !(a->h_inode->i_state & I_LINKABLE))
  17721. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  17722. + inode_unlock_shared(a->h_inode);
  17723. + }
  17724. +
  17725. + /* make the generation obsolete */
  17726. + if (au_ftest_hnjob(a->flags, GEN)) {
  17727. + e = -1;
  17728. + if (a->inode)
  17729. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  17730. + isdir);
  17731. + if (e && a->dentry)
  17732. + hn_gen_by_name(a->dentry, isdir);
  17733. + /* ignore this error */
  17734. + }
  17735. +
  17736. + /* make dir entries obsolete */
  17737. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  17738. + struct au_vdir *vdir;
  17739. +
  17740. + vdir = au_ivdir(a->inode);
  17741. + if (vdir)
  17742. + vdir->vd_jiffy = 0;
  17743. + /* IMustLock(a->inode); */
  17744. + /* inode_inc_iversion(a->inode); */
  17745. + }
  17746. +
  17747. + /* can do nothing but warn */
  17748. + if (au_ftest_hnjob(a->flags, MNTPNT)
  17749. + && a->dentry
  17750. + && d_mountpoint(a->dentry))
  17751. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  17752. +
  17753. + return 0;
  17754. +}
  17755. +
  17756. +/* ---------------------------------------------------------------------- */
  17757. +
  17758. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  17759. + struct inode *dir)
  17760. +{
  17761. + struct dentry *dentry, *d, *parent;
  17762. + struct qstr *dname;
  17763. +
  17764. + parent = d_find_any_alias(dir);
  17765. + if (!parent)
  17766. + return NULL;
  17767. +
  17768. + dentry = NULL;
  17769. + spin_lock(&parent->d_lock);
  17770. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  17771. + /* AuDbg("%pd\n", d); */
  17772. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  17773. + dname = &d->d_name;
  17774. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  17775. + goto cont_unlock;
  17776. + if (au_di(d))
  17777. + au_digen_dec(d);
  17778. + else
  17779. + goto cont_unlock;
  17780. + if (au_dcount(d) > 0) {
  17781. + dentry = dget_dlock(d);
  17782. + spin_unlock(&d->d_lock);
  17783. + break;
  17784. + }
  17785. +
  17786. +cont_unlock:
  17787. + spin_unlock(&d->d_lock);
  17788. + }
  17789. + spin_unlock(&parent->d_lock);
  17790. + dput(parent);
  17791. +
  17792. + if (dentry)
  17793. + di_write_lock_child(dentry);
  17794. +
  17795. + return dentry;
  17796. +}
  17797. +
  17798. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  17799. + aufs_bindex_t bindex, ino_t h_ino)
  17800. +{
  17801. + struct inode *inode;
  17802. + ino_t ino;
  17803. + int err;
  17804. +
  17805. + inode = NULL;
  17806. + err = au_xino_read(sb, bindex, h_ino, &ino);
  17807. + if (!err && ino)
  17808. + inode = ilookup(sb, ino);
  17809. + if (!inode)
  17810. + goto out;
  17811. +
  17812. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17813. + pr_warn("wrong root branch\n");
  17814. + iput(inode);
  17815. + inode = NULL;
  17816. + goto out;
  17817. + }
  17818. +
  17819. + ii_write_lock_child(inode);
  17820. +
  17821. +out:
  17822. + return inode;
  17823. +}
  17824. +
  17825. +static void au_hn_bh(void *_args)
  17826. +{
  17827. + struct au_hnotify_args *a = _args;
  17828. + struct super_block *sb;
  17829. + aufs_bindex_t bindex, bbot, bfound;
  17830. + unsigned char xino, try_iput;
  17831. + int err;
  17832. + struct inode *inode;
  17833. + ino_t h_ino;
  17834. + struct hn_job_args args;
  17835. + struct dentry *dentry;
  17836. + struct au_sbinfo *sbinfo;
  17837. +
  17838. + AuDebugOn(!_args);
  17839. + AuDebugOn(!a->h_dir);
  17840. + AuDebugOn(!a->dir);
  17841. + AuDebugOn(!a->mask);
  17842. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  17843. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  17844. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  17845. +
  17846. + inode = NULL;
  17847. + dentry = NULL;
  17848. + /*
  17849. + * do not lock a->dir->i_mutex here
  17850. + * because of d_revalidate() may cause a deadlock.
  17851. + */
  17852. + sb = a->dir->i_sb;
  17853. + AuDebugOn(!sb);
  17854. + sbinfo = au_sbi(sb);
  17855. + AuDebugOn(!sbinfo);
  17856. + si_write_lock(sb, AuLock_NOPLMW);
  17857. +
  17858. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  17859. + switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
  17860. + case FS_MOVED_FROM:
  17861. + case FS_MOVED_TO:
  17862. + AuWarn1("DIRREN with UDBA may not work correctly "
  17863. + "for the direct rename(2)\n");
  17864. + }
  17865. +
  17866. + ii_read_lock_parent(a->dir);
  17867. + bfound = -1;
  17868. + bbot = au_ibbot(a->dir);
  17869. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  17870. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  17871. + bfound = bindex;
  17872. + break;
  17873. + }
  17874. + ii_read_unlock(a->dir);
  17875. + if (unlikely(bfound < 0))
  17876. + goto out;
  17877. +
  17878. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  17879. + h_ino = 0;
  17880. + if (a->h_child_inode)
  17881. + h_ino = a->h_child_inode->i_ino;
  17882. +
  17883. + if (a->h_child_nlen
  17884. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  17885. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  17886. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  17887. + a->dir);
  17888. + try_iput = 0;
  17889. + if (dentry && d_really_is_positive(dentry))
  17890. + inode = d_inode(dentry);
  17891. + if (xino && !inode && h_ino
  17892. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  17893. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  17894. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  17895. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  17896. + try_iput = 1;
  17897. + }
  17898. +
  17899. + args.flags = a->flags[AuHn_CHILD];
  17900. + args.dentry = dentry;
  17901. + args.inode = inode;
  17902. + args.h_inode = a->h_child_inode;
  17903. + args.dir = a->dir;
  17904. + args.h_dir = a->h_dir;
  17905. + args.h_name = a->h_child_name;
  17906. + args.h_nlen = a->h_child_nlen;
  17907. + err = hn_job(&args);
  17908. + if (dentry) {
  17909. + if (au_di(dentry))
  17910. + di_write_unlock(dentry);
  17911. + dput(dentry);
  17912. + }
  17913. + if (inode && try_iput) {
  17914. + ii_write_unlock(inode);
  17915. + iput(inode);
  17916. + }
  17917. +
  17918. + ii_write_lock_parent(a->dir);
  17919. + args.flags = a->flags[AuHn_PARENT];
  17920. + args.dentry = NULL;
  17921. + args.inode = a->dir;
  17922. + args.h_inode = a->h_dir;
  17923. + args.dir = NULL;
  17924. + args.h_dir = NULL;
  17925. + args.h_name = NULL;
  17926. + args.h_nlen = 0;
  17927. + err = hn_job(&args);
  17928. + ii_write_unlock(a->dir);
  17929. +
  17930. +out:
  17931. + iput(a->h_child_inode);
  17932. + iput(a->h_dir);
  17933. + iput(a->dir);
  17934. + si_write_unlock(sb);
  17935. + au_nwt_done(&sbinfo->si_nowait);
  17936. + au_kfree_rcu(a);
  17937. +}
  17938. +
  17939. +/* ---------------------------------------------------------------------- */
  17940. +
  17941. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  17942. + const struct qstr *h_child_qstr, struct inode *h_child_inode)
  17943. +{
  17944. + int err, len;
  17945. + unsigned int flags[AuHnLast], f;
  17946. + unsigned char isdir, isroot, wh;
  17947. + struct inode *dir;
  17948. + struct au_hnotify_args *args;
  17949. + char *p, *h_child_name;
  17950. +
  17951. + err = 0;
  17952. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  17953. + dir = igrab(hnotify->hn_aufs_inode);
  17954. + if (!dir)
  17955. + goto out;
  17956. +
  17957. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  17958. + wh = 0;
  17959. + h_child_name = (void *)h_child_qstr->name;
  17960. + len = h_child_qstr->len;
  17961. + if (h_child_name) {
  17962. + if (len > AUFS_WH_PFX_LEN
  17963. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  17964. + h_child_name += AUFS_WH_PFX_LEN;
  17965. + len -= AUFS_WH_PFX_LEN;
  17966. + wh = 1;
  17967. + }
  17968. + }
  17969. +
  17970. + isdir = 0;
  17971. + if (h_child_inode)
  17972. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  17973. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  17974. + flags[AuHn_CHILD] = 0;
  17975. + if (isdir)
  17976. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  17977. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  17978. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  17979. + switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
  17980. + case FS_MOVED_FROM:
  17981. + case FS_MOVED_TO:
  17982. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  17983. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  17984. + fallthrough;
  17985. + case FS_CREATE:
  17986. + AuDebugOn(!h_child_name);
  17987. + break;
  17988. +
  17989. + case FS_DELETE:
  17990. + /*
  17991. + * aufs never be able to get this child inode.
  17992. + * revalidation should be in d_revalidate()
  17993. + * by checking i_nlink, i_generation or d_unhashed().
  17994. + */
  17995. + AuDebugOn(!h_child_name);
  17996. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  17997. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  17998. + break;
  17999. +
  18000. + default:
  18001. + AuDebugOn(1);
  18002. + }
  18003. +
  18004. + if (wh)
  18005. + h_child_inode = NULL;
  18006. +
  18007. + err = -ENOMEM;
  18008. + /* iput() and kfree() will be called in au_hnotify() */
  18009. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  18010. + if (unlikely(!args)) {
  18011. + AuErr1("no memory\n");
  18012. + iput(dir);
  18013. + goto out;
  18014. + }
  18015. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  18016. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  18017. + args->mask = mask;
  18018. + args->dir = dir;
  18019. + args->h_dir = igrab(h_dir);
  18020. + if (h_child_inode)
  18021. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  18022. + args->h_child_inode = h_child_inode;
  18023. + args->h_child_nlen = len;
  18024. + if (len) {
  18025. + p = (void *)args;
  18026. + p += sizeof(*args);
  18027. + memcpy(p, h_child_name, len);
  18028. + p[len] = 0;
  18029. + }
  18030. +
  18031. + /* NFS fires the event for silly-renamed one from kworker */
  18032. + f = 0;
  18033. + if (!vfsub_inode_nlink(dir, AU_I_AUFS)
  18034. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  18035. + f = AuWkq_NEST;
  18036. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  18037. + if (unlikely(err)) {
  18038. + pr_err("wkq %d\n", err);
  18039. + iput(args->h_child_inode);
  18040. + iput(args->h_dir);
  18041. + iput(args->dir);
  18042. + au_kfree_rcu(args);
  18043. + }
  18044. +
  18045. +out:
  18046. + return err;
  18047. +}
  18048. +
  18049. +/* ---------------------------------------------------------------------- */
  18050. +
  18051. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  18052. +{
  18053. + int err;
  18054. +
  18055. + AuDebugOn(!(udba & AuOptMask_UDBA));
  18056. +
  18057. + err = 0;
  18058. + if (au_hnotify_op.reset_br)
  18059. + err = au_hnotify_op.reset_br(udba, br, perm);
  18060. +
  18061. + return err;
  18062. +}
  18063. +
  18064. +int au_hnotify_init_br(struct au_branch *br, int perm)
  18065. +{
  18066. + int err;
  18067. +
  18068. + err = 0;
  18069. + if (au_hnotify_op.init_br)
  18070. + err = au_hnotify_op.init_br(br, perm);
  18071. +
  18072. + return err;
  18073. +}
  18074. +
  18075. +void au_hnotify_fin_br(struct au_branch *br)
  18076. +{
  18077. + if (au_hnotify_op.fin_br)
  18078. + au_hnotify_op.fin_br(br);
  18079. +}
  18080. +
  18081. +static void au_hn_destroy_cache(void)
  18082. +{
  18083. + kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
  18084. + au_cache[AuCache_HNOTIFY] = NULL;
  18085. +}
  18086. +
  18087. +int __init au_hnotify_init(void)
  18088. +{
  18089. + int err;
  18090. +
  18091. + err = -ENOMEM;
  18092. + au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
  18093. + if (au_cache[AuCache_HNOTIFY]) {
  18094. + err = 0;
  18095. + if (au_hnotify_op.init)
  18096. + err = au_hnotify_op.init();
  18097. + if (unlikely(err))
  18098. + au_hn_destroy_cache();
  18099. + }
  18100. + AuTraceErr(err);
  18101. + return err;
  18102. +}
  18103. +
  18104. +void au_hnotify_fin(void)
  18105. +{
  18106. + if (au_hnotify_op.fin)
  18107. + au_hnotify_op.fin();
  18108. +
  18109. + /* cf. au_cache_fin() */
  18110. + if (au_cache[AuCache_HNOTIFY])
  18111. + au_hn_destroy_cache();
  18112. +}
  18113. diff -Naur null/fs/aufs/iinfo.c linux-6.6.63/fs/aufs/iinfo.c
  18114. --- /dev/null
  18115. +++ linux-6.6.63/fs/aufs/iinfo.c 2024-12-18 15:12:59.297519563 +0300
  18116. @@ -0,0 +1,287 @@
  18117. +// SPDX-License-Identifier: GPL-2.0
  18118. +/*
  18119. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  18120. + *
  18121. + * This program is free software; you can redistribute it and/or modify
  18122. + * it under the terms of the GNU General Public License as published by
  18123. + * the Free Software Foundation; either version 2 of the License, or
  18124. + * (at your option) any later version.
  18125. + *
  18126. + * This program is distributed in the hope that it will be useful,
  18127. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18128. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18129. + * GNU General Public License for more details.
  18130. + *
  18131. + * You should have received a copy of the GNU General Public License
  18132. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18133. + */
  18134. +
  18135. +/*
  18136. + * inode private data
  18137. + */
  18138. +
  18139. +#include "aufs.h"
  18140. +
  18141. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  18142. +{
  18143. + struct inode *h_inode;
  18144. + struct au_hinode *hinode;
  18145. +
  18146. + IiMustAnyLock(inode);
  18147. +
  18148. + hinode = au_hinode(au_ii(inode), bindex);
  18149. + h_inode = hinode->hi_inode;
  18150. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  18151. + return h_inode;
  18152. +}
  18153. +
  18154. +/* todo: hard/soft set? */
  18155. +void au_hiput(struct au_hinode *hinode)
  18156. +{
  18157. + au_hn_free(hinode);
  18158. + dput(hinode->hi_whdentry);
  18159. + iput(hinode->hi_inode);
  18160. +}
  18161. +
  18162. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  18163. +{
  18164. + unsigned int flags;
  18165. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  18166. +
  18167. + flags = 0;
  18168. + if (au_opt_test(mnt_flags, XINO))
  18169. + au_fset_hi(flags, XINO);
  18170. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  18171. + au_fset_hi(flags, HNOTIFY);
  18172. + return flags;
  18173. +}
  18174. +
  18175. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  18176. + struct inode *h_inode, unsigned int flags)
  18177. +{
  18178. + struct au_hinode *hinode;
  18179. + struct inode *hi;
  18180. + struct au_iinfo *iinfo = au_ii(inode);
  18181. +
  18182. + IiMustWriteLock(inode);
  18183. +
  18184. + hinode = au_hinode(iinfo, bindex);
  18185. + hi = hinode->hi_inode;
  18186. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  18187. +
  18188. + if (hi)
  18189. + au_hiput(hinode);
  18190. + hinode->hi_inode = h_inode;
  18191. + if (h_inode) {
  18192. + int err;
  18193. + struct super_block *sb = inode->i_sb;
  18194. + struct au_branch *br;
  18195. +
  18196. + AuDebugOn(inode->i_mode
  18197. + && (h_inode->i_mode & S_IFMT)
  18198. + != (inode->i_mode & S_IFMT));
  18199. + if (bindex == iinfo->ii_btop)
  18200. + au_cpup_igen(inode, h_inode);
  18201. + br = au_sbr(sb, bindex);
  18202. + hinode->hi_id = br->br_id;
  18203. + if (au_ftest_hi(flags, XINO)) {
  18204. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  18205. + inode->i_ino);
  18206. + if (unlikely(err))
  18207. + AuIOErr1("failed au_xino_write() %d\n", err);
  18208. + }
  18209. +
  18210. + if (au_ftest_hi(flags, HNOTIFY)
  18211. + && au_br_hnotifyable(br->br_perm)) {
  18212. + err = au_hn_alloc(hinode, inode);
  18213. + if (unlikely(err))
  18214. + AuIOErr1("au_hn_alloc() %d\n", err);
  18215. + }
  18216. + }
  18217. +}
  18218. +
  18219. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  18220. + struct dentry *h_wh)
  18221. +{
  18222. + struct au_hinode *hinode;
  18223. +
  18224. + IiMustWriteLock(inode);
  18225. +
  18226. + hinode = au_hinode(au_ii(inode), bindex);
  18227. + AuDebugOn(hinode->hi_whdentry);
  18228. + hinode->hi_whdentry = h_wh;
  18229. +}
  18230. +
  18231. +void au_update_iigen(struct inode *inode, int half)
  18232. +{
  18233. + struct au_iinfo *iinfo;
  18234. + struct au_iigen *iigen;
  18235. + unsigned int sigen;
  18236. +
  18237. + sigen = au_sigen(inode->i_sb);
  18238. + iinfo = au_ii(inode);
  18239. + iigen = &iinfo->ii_generation;
  18240. + spin_lock(&iigen->ig_spin);
  18241. + iigen->ig_generation = sigen;
  18242. + if (half)
  18243. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  18244. + else
  18245. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  18246. + spin_unlock(&iigen->ig_spin);
  18247. +}
  18248. +
  18249. +/* it may be called at remount time, too */
  18250. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  18251. +{
  18252. + struct au_iinfo *iinfo;
  18253. + aufs_bindex_t bindex, bbot;
  18254. +
  18255. + AuDebugOn(au_is_bad_inode(inode));
  18256. + IiMustWriteLock(inode);
  18257. +
  18258. + iinfo = au_ii(inode);
  18259. + if (do_put_zero && iinfo->ii_btop >= 0) {
  18260. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  18261. + bindex++) {
  18262. + struct inode *h_i;
  18263. +
  18264. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  18265. + if (h_i
  18266. + && !vfsub_inode_nlink(h_i, AU_I_BRANCH)
  18267. + && !(h_i->i_state & I_LINKABLE))
  18268. + au_set_h_iptr(inode, bindex, NULL, 0);
  18269. + }
  18270. + }
  18271. +
  18272. + iinfo->ii_btop = -1;
  18273. + iinfo->ii_bbot = -1;
  18274. + bbot = au_sbbot(inode->i_sb);
  18275. + for (bindex = 0; bindex <= bbot; bindex++)
  18276. + if (au_hinode(iinfo, bindex)->hi_inode) {
  18277. + iinfo->ii_btop = bindex;
  18278. + break;
  18279. + }
  18280. + if (iinfo->ii_btop >= 0)
  18281. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  18282. + if (au_hinode(iinfo, bindex)->hi_inode) {
  18283. + iinfo->ii_bbot = bindex;
  18284. + break;
  18285. + }
  18286. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  18287. +}
  18288. +
  18289. +/* ---------------------------------------------------------------------- */
  18290. +
  18291. +void au_icntnr_init_once(void *_c)
  18292. +{
  18293. + struct au_icntnr *c = _c;
  18294. + struct au_iinfo *iinfo = &c->iinfo;
  18295. +
  18296. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  18297. + au_rw_init(&iinfo->ii_rwsem);
  18298. + inode_init_once(&c->vfs_inode);
  18299. + spin_lock_init(&c->nlink_spin);
  18300. +}
  18301. +
  18302. +void au_hinode_init(struct au_hinode *hinode)
  18303. +{
  18304. + hinode->hi_inode = NULL;
  18305. + hinode->hi_id = -1;
  18306. + au_hn_init(hinode);
  18307. + hinode->hi_whdentry = NULL;
  18308. +}
  18309. +
  18310. +int au_iinfo_init(struct inode *inode)
  18311. +{
  18312. + struct au_iinfo *iinfo;
  18313. + struct super_block *sb;
  18314. + struct au_hinode *hi;
  18315. + int nbr, i;
  18316. +
  18317. + sb = inode->i_sb;
  18318. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  18319. + nbr = au_sbbot(sb) + 1;
  18320. + if (unlikely(nbr <= 0))
  18321. + nbr = 1;
  18322. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  18323. + if (hi) {
  18324. + au_lcnt_inc(&au_sbi(sb)->si_ninodes);
  18325. +
  18326. + iinfo->ii_hinode = hi;
  18327. + for (i = 0; i < nbr; i++, hi++)
  18328. + au_hinode_init(hi);
  18329. +
  18330. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  18331. + iinfo->ii_btop = -1;
  18332. + iinfo->ii_bbot = -1;
  18333. + iinfo->ii_vdir = NULL;
  18334. + return 0;
  18335. + }
  18336. + return -ENOMEM;
  18337. +}
  18338. +
  18339. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
  18340. +{
  18341. + int err, i;
  18342. + struct au_hinode *hip;
  18343. +
  18344. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  18345. +
  18346. + err = -ENOMEM;
  18347. + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
  18348. + may_shrink);
  18349. + if (hip) {
  18350. + iinfo->ii_hinode = hip;
  18351. + i = iinfo->ii_bbot + 1;
  18352. + hip += i;
  18353. + for (; i < nbr; i++, hip++)
  18354. + au_hinode_init(hip);
  18355. + err = 0;
  18356. + }
  18357. +
  18358. + return err;
  18359. +}
  18360. +
  18361. +void au_iinfo_fin(struct inode *inode)
  18362. +{
  18363. + struct au_iinfo *iinfo;
  18364. + struct au_hinode *hi;
  18365. + struct super_block *sb;
  18366. + aufs_bindex_t bindex, bbot;
  18367. + const unsigned char unlinked = !vfsub_inode_nlink(inode, AU_I_AUFS);
  18368. +
  18369. + AuDebugOn(au_is_bad_inode(inode));
  18370. +
  18371. + sb = inode->i_sb;
  18372. + au_lcnt_dec(&au_sbi(sb)->si_ninodes);
  18373. + if (si_pid_test(sb))
  18374. + au_xino_delete_inode(inode, unlinked);
  18375. + else {
  18376. + /*
  18377. + * it is safe to hide the dependency between sbinfo and
  18378. + * sb->s_umount.
  18379. + */
  18380. + lockdep_off();
  18381. + si_noflush_read_lock(sb);
  18382. + au_xino_delete_inode(inode, unlinked);
  18383. + si_read_unlock(sb);
  18384. + lockdep_on();
  18385. + }
  18386. +
  18387. + iinfo = au_ii(inode);
  18388. + if (iinfo->ii_vdir)
  18389. + au_vdir_free(iinfo->ii_vdir);
  18390. +
  18391. + bindex = iinfo->ii_btop;
  18392. + if (bindex >= 0) {
  18393. + hi = au_hinode(iinfo, bindex);
  18394. + bbot = iinfo->ii_bbot;
  18395. + while (bindex++ <= bbot) {
  18396. + if (hi->hi_inode)
  18397. + au_hiput(hi);
  18398. + hi++;
  18399. + }
  18400. + }
  18401. + au_kfree_rcu(iinfo->ii_hinode);
  18402. + AuRwDestroy(&iinfo->ii_rwsem);
  18403. +}
  18404. diff -Naur null/fs/aufs/inode.c linux-6.6.63/fs/aufs/inode.c
  18405. --- /dev/null
  18406. +++ linux-6.6.63/fs/aufs/inode.c 2024-12-18 15:12:59.297519563 +0300
  18407. @@ -0,0 +1,532 @@
  18408. +// SPDX-License-Identifier: GPL-2.0
  18409. +/*
  18410. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  18411. + *
  18412. + * This program is free software; you can redistribute it and/or modify
  18413. + * it under the terms of the GNU General Public License as published by
  18414. + * the Free Software Foundation; either version 2 of the License, or
  18415. + * (at your option) any later version.
  18416. + *
  18417. + * This program is distributed in the hope that it will be useful,
  18418. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18419. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18420. + * GNU General Public License for more details.
  18421. + *
  18422. + * You should have received a copy of the GNU General Public License
  18423. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18424. + */
  18425. +
  18426. +/*
  18427. + * inode functions
  18428. + */
  18429. +
  18430. +#include <linux/iversion.h>
  18431. +#include "aufs.h"
  18432. +
  18433. +struct inode *au_igrab(struct inode *inode)
  18434. +{
  18435. + if (inode) {
  18436. + AuDebugOn(!atomic_read(&inode->i_count));
  18437. + ihold(inode);
  18438. + }
  18439. + return inode;
  18440. +}
  18441. +
  18442. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  18443. +{
  18444. + au_cpup_attr_all(inode, /*force*/0);
  18445. + au_update_iigen(inode, /*half*/1);
  18446. + if (do_version)
  18447. + inode_inc_iversion(inode);
  18448. +}
  18449. +
  18450. +static int au_ii_refresh(struct inode *inode, int *update)
  18451. +{
  18452. + int err, e, nbr;
  18453. + umode_t type;
  18454. + aufs_bindex_t bindex, new_bindex;
  18455. + struct super_block *sb;
  18456. + struct au_iinfo *iinfo;
  18457. + struct au_hinode *p, *q, tmp;
  18458. +
  18459. + AuDebugOn(au_is_bad_inode(inode));
  18460. + IiMustWriteLock(inode);
  18461. +
  18462. + *update = 0;
  18463. + sb = inode->i_sb;
  18464. + nbr = au_sbbot(sb) + 1;
  18465. + type = inode->i_mode & S_IFMT;
  18466. + iinfo = au_ii(inode);
  18467. + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
  18468. + if (unlikely(err))
  18469. + goto out;
  18470. +
  18471. + AuDebugOn(iinfo->ii_btop < 0);
  18472. + p = au_hinode(iinfo, iinfo->ii_btop);
  18473. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  18474. + bindex++, p++) {
  18475. + if (!p->hi_inode)
  18476. + continue;
  18477. +
  18478. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  18479. + new_bindex = au_br_index(sb, p->hi_id);
  18480. + if (new_bindex == bindex)
  18481. + continue;
  18482. +
  18483. + if (new_bindex < 0) {
  18484. + *update = 1;
  18485. + au_hiput(p);
  18486. + p->hi_inode = NULL;
  18487. + continue;
  18488. + }
  18489. +
  18490. + if (new_bindex < iinfo->ii_btop)
  18491. + iinfo->ii_btop = new_bindex;
  18492. + if (iinfo->ii_bbot < new_bindex)
  18493. + iinfo->ii_bbot = new_bindex;
  18494. + /* swap two lower inode, and loop again */
  18495. + q = au_hinode(iinfo, new_bindex);
  18496. + tmp = *q;
  18497. + *q = *p;
  18498. + *p = tmp;
  18499. + if (tmp.hi_inode) {
  18500. + bindex--;
  18501. + p--;
  18502. + }
  18503. + }
  18504. + au_update_ibrange(inode, /*do_put_zero*/0);
  18505. + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
  18506. + e = au_dy_irefresh(inode);
  18507. + if (unlikely(e && !err))
  18508. + err = e;
  18509. +
  18510. +out:
  18511. + AuTraceErr(err);
  18512. + return err;
  18513. +}
  18514. +
  18515. +void au_refresh_iop(struct inode *inode, int force_getattr)
  18516. +{
  18517. + int type;
  18518. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  18519. + const struct inode_operations *iop
  18520. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  18521. +
  18522. + if (inode->i_op == iop)
  18523. + return;
  18524. +
  18525. + switch (inode->i_mode & S_IFMT) {
  18526. + case S_IFDIR:
  18527. + type = AuIop_DIR;
  18528. + break;
  18529. + case S_IFLNK:
  18530. + type = AuIop_SYMLINK;
  18531. + break;
  18532. + default:
  18533. + type = AuIop_OTHER;
  18534. + break;
  18535. + }
  18536. +
  18537. + inode->i_op = iop + type;
  18538. + /* unnecessary smp_wmb() */
  18539. +}
  18540. +
  18541. +int au_refresh_hinode_self(struct inode *inode)
  18542. +{
  18543. + int err, update;
  18544. +
  18545. + err = au_ii_refresh(inode, &update);
  18546. + if (!err)
  18547. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  18548. +
  18549. + AuTraceErr(err);
  18550. + return err;
  18551. +}
  18552. +
  18553. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  18554. +{
  18555. + int err, e, update;
  18556. + unsigned int flags;
  18557. + umode_t mode;
  18558. + aufs_bindex_t bindex, bbot;
  18559. + unsigned char isdir;
  18560. + struct au_hinode *p;
  18561. + struct au_iinfo *iinfo;
  18562. +
  18563. + err = au_ii_refresh(inode, &update);
  18564. + if (unlikely(err))
  18565. + goto out;
  18566. +
  18567. + update = 0;
  18568. + iinfo = au_ii(inode);
  18569. + p = au_hinode(iinfo, iinfo->ii_btop);
  18570. + mode = (inode->i_mode & S_IFMT);
  18571. + isdir = S_ISDIR(mode);
  18572. + flags = au_hi_flags(inode, isdir);
  18573. + bbot = au_dbbot(dentry);
  18574. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  18575. + struct inode *h_i, *h_inode;
  18576. + struct dentry *h_d;
  18577. +
  18578. + h_d = au_h_dptr(dentry, bindex);
  18579. + if (!h_d || d_is_negative(h_d))
  18580. + continue;
  18581. +
  18582. + h_inode = d_inode(h_d);
  18583. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  18584. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  18585. + h_i = au_h_iptr(inode, bindex);
  18586. + if (h_i) {
  18587. + if (h_i == h_inode)
  18588. + continue;
  18589. + err = -EIO;
  18590. + break;
  18591. + }
  18592. + }
  18593. + if (bindex < iinfo->ii_btop)
  18594. + iinfo->ii_btop = bindex;
  18595. + if (iinfo->ii_bbot < bindex)
  18596. + iinfo->ii_bbot = bindex;
  18597. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  18598. + update = 1;
  18599. + }
  18600. + au_update_ibrange(inode, /*do_put_zero*/0);
  18601. + e = au_dy_irefresh(inode);
  18602. + if (unlikely(e && !err))
  18603. + err = e;
  18604. + if (!err)
  18605. + au_refresh_hinode_attr(inode, update && isdir);
  18606. +
  18607. +out:
  18608. + AuTraceErr(err);
  18609. + return err;
  18610. +}
  18611. +
  18612. +static int set_inode(struct inode *inode, struct dentry *dentry)
  18613. +{
  18614. + int err;
  18615. + unsigned int flags;
  18616. + umode_t mode;
  18617. + aufs_bindex_t bindex, btop, btail;
  18618. + unsigned char isdir;
  18619. + struct dentry *h_dentry;
  18620. + struct inode *h_inode;
  18621. + struct au_iinfo *iinfo;
  18622. + const struct inode_operations *iop;
  18623. +
  18624. + IiMustWriteLock(inode);
  18625. +
  18626. + err = 0;
  18627. + isdir = 0;
  18628. + iop = au_sbi(inode->i_sb)->si_iop_array;
  18629. + btop = au_dbtop(dentry);
  18630. + h_dentry = au_h_dptr(dentry, btop);
  18631. + h_inode = d_inode(h_dentry);
  18632. + mode = h_inode->i_mode;
  18633. + switch (mode & S_IFMT) {
  18634. + case S_IFREG:
  18635. + btail = au_dbtail(dentry);
  18636. + inode->i_op = iop + AuIop_OTHER;
  18637. + inode->i_fop = &aufs_file_fop;
  18638. + err = au_dy_iaop(inode, btop, h_inode);
  18639. + if (unlikely(err))
  18640. + goto out;
  18641. + break;
  18642. + case S_IFDIR:
  18643. + isdir = 1;
  18644. + btail = au_dbtaildir(dentry);
  18645. + inode->i_op = iop + AuIop_DIR;
  18646. + inode->i_fop = &aufs_dir_fop;
  18647. + break;
  18648. + case S_IFLNK:
  18649. + btail = au_dbtail(dentry);
  18650. + inode->i_op = iop + AuIop_SYMLINK;
  18651. + break;
  18652. + case S_IFBLK:
  18653. + case S_IFCHR:
  18654. + case S_IFIFO:
  18655. + case S_IFSOCK:
  18656. + btail = au_dbtail(dentry);
  18657. + inode->i_op = iop + AuIop_OTHER;
  18658. + init_special_inode(inode, mode, h_inode->i_rdev);
  18659. + break;
  18660. + default:
  18661. + AuIOErr("Unknown file type 0%o\n", mode);
  18662. + err = -EIO;
  18663. + goto out;
  18664. + }
  18665. +
  18666. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  18667. + flags = au_hi_flags(inode, isdir);
  18668. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  18669. + && au_ftest_hi(flags, HNOTIFY)
  18670. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  18671. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  18672. + au_fclr_hi(flags, HNOTIFY);
  18673. + iinfo = au_ii(inode);
  18674. + iinfo->ii_btop = btop;
  18675. + iinfo->ii_bbot = btail;
  18676. + for (bindex = btop; bindex <= btail; bindex++) {
  18677. + h_dentry = au_h_dptr(dentry, bindex);
  18678. + if (h_dentry)
  18679. + au_set_h_iptr(inode, bindex,
  18680. + au_igrab(d_inode(h_dentry)), flags);
  18681. + }
  18682. + au_cpup_attr_all(inode, /*force*/1);
  18683. + /*
  18684. + * to force calling aufs_get_inode_acl() every time,
  18685. + * do not call cache_no_acl() for aufs inode.
  18686. + */
  18687. +
  18688. +out:
  18689. + return err;
  18690. +}
  18691. +
  18692. +/*
  18693. + * successful returns with iinfo write_locked
  18694. + * minus: errno
  18695. + * zero: success, matched
  18696. + * plus: no error, but unmatched
  18697. + */
  18698. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  18699. +{
  18700. + int err;
  18701. + unsigned int gen, igflags;
  18702. + aufs_bindex_t bindex, bbot;
  18703. + struct inode *h_inode, *h_dinode;
  18704. + struct dentry *h_dentry;
  18705. +
  18706. + /*
  18707. + * before this function, if aufs got any iinfo lock, it must be only
  18708. + * one, the parent dir.
  18709. + * it can happen by UDBA and the obsoleted inode number.
  18710. + */
  18711. + err = -EIO;
  18712. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  18713. + goto out;
  18714. +
  18715. + err = 1;
  18716. + ii_write_lock_new_child(inode);
  18717. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  18718. + h_dinode = d_inode(h_dentry);
  18719. + bbot = au_ibbot(inode);
  18720. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  18721. + h_inode = au_h_iptr(inode, bindex);
  18722. + if (!h_inode || h_inode != h_dinode)
  18723. + continue;
  18724. +
  18725. + err = 0;
  18726. + gen = au_iigen(inode, &igflags);
  18727. + if (gen == au_digen(dentry)
  18728. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  18729. + break;
  18730. +
  18731. + /* fully refresh inode using dentry */
  18732. + err = au_refresh_hinode(inode, dentry);
  18733. + if (!err)
  18734. + au_update_iigen(inode, /*half*/0);
  18735. + break;
  18736. + }
  18737. +
  18738. + if (unlikely(err))
  18739. + ii_write_unlock(inode);
  18740. +out:
  18741. + return err;
  18742. +}
  18743. +
  18744. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  18745. + unsigned int d_type, ino_t *ino)
  18746. +{
  18747. + int err, idx;
  18748. + const int isnondir = d_type != DT_DIR;
  18749. +
  18750. + /* prevent hardlinked inode number from race condition */
  18751. + if (isnondir) {
  18752. + err = au_xinondir_enter(sb, bindex, h_ino, &idx);
  18753. + if (unlikely(err))
  18754. + goto out;
  18755. + }
  18756. +
  18757. + err = au_xino_read(sb, bindex, h_ino, ino);
  18758. + if (unlikely(err))
  18759. + goto out_xinondir;
  18760. +
  18761. + if (!*ino) {
  18762. + err = -EIO;
  18763. + *ino = au_xino_new_ino(sb);
  18764. + if (unlikely(!*ino))
  18765. + goto out_xinondir;
  18766. + err = au_xino_write(sb, bindex, h_ino, *ino);
  18767. + if (unlikely(err))
  18768. + goto out_xinondir;
  18769. + }
  18770. +
  18771. +out_xinondir:
  18772. + if (isnondir && idx >= 0)
  18773. + au_xinondir_leave(sb, bindex, h_ino, idx);
  18774. +out:
  18775. + return err;
  18776. +}
  18777. +
  18778. +/* successful returns with iinfo write_locked */
  18779. +/* todo: return with unlocked? */
  18780. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  18781. +{
  18782. + struct inode *inode, *h_inode;
  18783. + struct dentry *h_dentry;
  18784. + struct super_block *sb;
  18785. + ino_t h_ino, ino;
  18786. + int err, idx, hlinked;
  18787. + aufs_bindex_t btop;
  18788. +
  18789. + sb = dentry->d_sb;
  18790. + btop = au_dbtop(dentry);
  18791. + h_dentry = au_h_dptr(dentry, btop);
  18792. + h_inode = d_inode(h_dentry);
  18793. + h_ino = h_inode->i_ino;
  18794. + hlinked = !d_is_dir(h_dentry) && vfsub_inode_nlink(h_inode, AU_I_BRANCH) > 1;
  18795. +
  18796. +new_ino:
  18797. + /*
  18798. + * stop 'race'-ing between hardlinks under different
  18799. + * parents.
  18800. + */
  18801. + if (hlinked) {
  18802. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  18803. + inode = ERR_PTR(err);
  18804. + if (unlikely(err))
  18805. + goto out;
  18806. + }
  18807. +
  18808. + err = au_xino_read(sb, btop, h_ino, &ino);
  18809. + inode = ERR_PTR(err);
  18810. + if (unlikely(err))
  18811. + goto out_xinondir;
  18812. +
  18813. + if (!ino) {
  18814. + ino = au_xino_new_ino(sb);
  18815. + if (unlikely(!ino)) {
  18816. + inode = ERR_PTR(-EIO);
  18817. + goto out_xinondir;
  18818. + }
  18819. + }
  18820. +
  18821. + AuDbg("i%lu\n", (unsigned long)ino);
  18822. + inode = au_iget_locked(sb, ino);
  18823. + err = PTR_ERR(inode);
  18824. + if (IS_ERR(inode))
  18825. + goto out_xinondir;
  18826. +
  18827. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  18828. + if (inode->i_state & I_NEW) {
  18829. + ii_write_lock_new_child(inode);
  18830. + err = set_inode(inode, dentry);
  18831. + if (!err) {
  18832. + unlock_new_inode(inode);
  18833. + goto out_xinondir; /* success */
  18834. + }
  18835. +
  18836. + /*
  18837. + * iget_failed() calls iput(), but we need to call
  18838. + * ii_write_unlock() after iget_failed(). so dirty hack for
  18839. + * i_count.
  18840. + */
  18841. + atomic_inc(&inode->i_count);
  18842. + iget_failed(inode);
  18843. + ii_write_unlock(inode);
  18844. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  18845. + /* ignore this error */
  18846. + goto out_iput;
  18847. + } else if (!must_new && !IS_DEADDIR(inode)
  18848. + && vfsub_inode_nlink(inode, AU_I_AUFS)) {
  18849. + /*
  18850. + * horrible race condition between lookup, readdir and copyup
  18851. + * (or something).
  18852. + */
  18853. + if (hlinked && idx >= 0)
  18854. + au_xinondir_leave(sb, btop, h_ino, idx);
  18855. + err = reval_inode(inode, dentry);
  18856. + if (unlikely(err < 0)) {
  18857. + hlinked = 0;
  18858. + goto out_iput;
  18859. + }
  18860. + if (!err)
  18861. + goto out; /* success */
  18862. + else if (hlinked && idx >= 0) {
  18863. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  18864. + if (unlikely(err)) {
  18865. + iput(inode);
  18866. + inode = ERR_PTR(err);
  18867. + goto out;
  18868. + }
  18869. + }
  18870. + }
  18871. +
  18872. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  18873. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  18874. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  18875. + btop, au_sbtype(h_dentry->d_sb), dentry,
  18876. + (unsigned long)h_ino, (unsigned long)ino);
  18877. + ino = 0;
  18878. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  18879. + if (!err) {
  18880. + iput(inode);
  18881. + if (hlinked && idx >= 0)
  18882. + au_xinondir_leave(sb, btop, h_ino, idx);
  18883. + goto new_ino;
  18884. + }
  18885. +
  18886. +out_iput:
  18887. + iput(inode);
  18888. + inode = ERR_PTR(err);
  18889. +out_xinondir:
  18890. + if (hlinked && idx >= 0)
  18891. + au_xinondir_leave(sb, btop, h_ino, idx);
  18892. +out:
  18893. + return inode;
  18894. +}
  18895. +
  18896. +/* ---------------------------------------------------------------------- */
  18897. +
  18898. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  18899. + struct inode *inode)
  18900. +{
  18901. + int err;
  18902. + struct inode *hi;
  18903. +
  18904. + err = au_br_rdonly(au_sbr(sb, bindex));
  18905. +
  18906. + /* pseudo-link after flushed may happen out of bounds */
  18907. + if (!err
  18908. + && inode
  18909. + && au_ibtop(inode) <= bindex
  18910. + && bindex <= au_ibbot(inode)) {
  18911. + /*
  18912. + * permission check is unnecessary since vfsub routine
  18913. + * will be called later
  18914. + */
  18915. + hi = au_h_iptr(inode, bindex);
  18916. + if (hi)
  18917. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  18918. + }
  18919. +
  18920. + return err;
  18921. +}
  18922. +
  18923. +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
  18924. + int mask)
  18925. +{
  18926. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  18927. + return 0;
  18928. + return inode_permission(h_idmap, h_inode, mask);
  18929. +}
  18930. +
  18931. +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
  18932. + int mask)
  18933. +{
  18934. + if (au_test_nfs(h_inode->i_sb)
  18935. + && (mask & MAY_WRITE)
  18936. + && S_ISDIR(h_inode->i_mode))
  18937. + mask |= MAY_READ; /* force permission check */
  18938. + return au_test_h_perm(h_idmap, h_inode, mask);
  18939. +}
  18940. diff -Naur null/fs/aufs/inode.h linux-6.6.63/fs/aufs/inode.h
  18941. --- /dev/null
  18942. +++ linux-6.6.63/fs/aufs/inode.h 2024-12-18 15:12:59.298519551 +0300
  18943. @@ -0,0 +1,727 @@
  18944. +/* SPDX-License-Identifier: GPL-2.0 */
  18945. +/*
  18946. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  18947. + *
  18948. + * This program is free software; you can redistribute it and/or modify
  18949. + * it under the terms of the GNU General Public License as published by
  18950. + * the Free Software Foundation; either version 2 of the License, or
  18951. + * (at your option) any later version.
  18952. + *
  18953. + * This program is distributed in the hope that it will be useful,
  18954. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18955. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18956. + * GNU General Public License for more details.
  18957. + *
  18958. + * You should have received a copy of the GNU General Public License
  18959. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18960. + */
  18961. +
  18962. +/*
  18963. + * inode operations
  18964. + */
  18965. +
  18966. +#ifndef __AUFS_INODE_H__
  18967. +#define __AUFS_INODE_H__
  18968. +
  18969. +#ifdef __KERNEL__
  18970. +
  18971. +#include <linux/fsnotify.h>
  18972. +#include "fstype.h"
  18973. +#include "rwsem.h"
  18974. +
  18975. +struct vfsmount;
  18976. +
  18977. +struct au_hnotify {
  18978. +#ifdef CONFIG_AUFS_HNOTIFY
  18979. +#ifdef CONFIG_AUFS_HFSNOTIFY
  18980. + /* never use fsnotify_add_vfsmount_mark() */
  18981. + struct fsnotify_mark hn_mark;
  18982. +#endif
  18983. + struct inode *hn_aufs_inode; /* no get/put */
  18984. + struct rcu_head rcu;
  18985. +#endif
  18986. +} ____cacheline_aligned_in_smp;
  18987. +
  18988. +struct au_hinode {
  18989. + struct inode *hi_inode;
  18990. + aufs_bindex_t hi_id;
  18991. +#ifdef CONFIG_AUFS_HNOTIFY
  18992. + struct au_hnotify *hi_notify;
  18993. +#endif
  18994. +
  18995. + /* reference to the copied-up whiteout with get/put */
  18996. + struct dentry *hi_whdentry;
  18997. +};
  18998. +
  18999. +/* ig_flags */
  19000. +#define AuIG_HALF_REFRESHED 1
  19001. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  19002. +#define au_ig_fset(flags, name) \
  19003. + do { (flags) |= AuIG_##name; } while (0)
  19004. +#define au_ig_fclr(flags, name) \
  19005. + do { (flags) &= ~AuIG_##name; } while (0)
  19006. +
  19007. +struct au_iigen {
  19008. + spinlock_t ig_spin;
  19009. + __u32 ig_generation, ig_flags;
  19010. +};
  19011. +
  19012. +struct au_vdir;
  19013. +struct au_iinfo {
  19014. + struct au_iigen ii_generation;
  19015. + struct super_block *ii_hsb1; /* no get/put */
  19016. +
  19017. + struct au_rwsem ii_rwsem;
  19018. + aufs_bindex_t ii_btop, ii_bbot;
  19019. + __u32 ii_higen;
  19020. + struct au_hinode *ii_hinode;
  19021. + struct au_vdir *ii_vdir;
  19022. +};
  19023. +
  19024. +struct au_icntnr {
  19025. + struct au_iinfo iinfo;
  19026. + struct inode vfs_inode;
  19027. + spinlock_t nlink_spin; /* protects vfs_inode.i_nlink */
  19028. + struct hlist_bl_node plink;
  19029. + struct rcu_head rcu;
  19030. +} ____cacheline_aligned_in_smp;
  19031. +
  19032. +/* au_pin flags */
  19033. +#define AuPin_DI_LOCKED 1
  19034. +#define AuPin_MNT_WRITE (1 << 1)
  19035. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  19036. +#define au_fset_pin(flags, name) \
  19037. + do { (flags) |= AuPin_##name; } while (0)
  19038. +#define au_fclr_pin(flags, name) \
  19039. + do { (flags) &= ~AuPin_##name; } while (0)
  19040. +
  19041. +struct au_pin {
  19042. + /* input */
  19043. + struct dentry *dentry;
  19044. + unsigned int udba;
  19045. + unsigned char lsc_di, lsc_hi, flags;
  19046. + aufs_bindex_t bindex;
  19047. +
  19048. + /* output */
  19049. + struct dentry *parent;
  19050. + struct au_hinode *hdir;
  19051. + struct vfsmount *h_mnt;
  19052. +
  19053. + /* temporary unlock/relock for copyup */
  19054. + struct dentry *h_dentry, *h_parent;
  19055. + struct au_branch *br;
  19056. + struct task_struct *task;
  19057. +};
  19058. +
  19059. +void au_pin_hdir_unlock(struct au_pin *p);
  19060. +int au_pin_hdir_lock(struct au_pin *p);
  19061. +int au_pin_hdir_relock(struct au_pin *p);
  19062. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  19063. +void au_pin_hdir_release(struct au_pin *p);
  19064. +
  19065. +/* ---------------------------------------------------------------------- */
  19066. +
  19067. +static inline struct au_iinfo *au_ii(struct inode *inode)
  19068. +{
  19069. + BUG_ON(is_bad_inode(inode));
  19070. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  19071. +}
  19072. +
  19073. +static inline void au_nlink_lock(struct inode *inode)
  19074. +{
  19075. + spinlock_t *spin;
  19076. +
  19077. + AuDebugOn(!au_test_aufs(inode->i_sb));
  19078. + AuDebugOn(is_bad_inode(inode));
  19079. + spin = &(container_of(inode, struct au_icntnr, vfs_inode)->nlink_spin);
  19080. + spin_lock(spin);
  19081. +}
  19082. +
  19083. +static inline void au_nlink_unlock(struct inode *inode)
  19084. +{
  19085. + spinlock_t *spin;
  19086. +
  19087. + spin = &(container_of(inode, struct au_icntnr, vfs_inode)->nlink_spin);
  19088. + spin_unlock(spin);
  19089. +}
  19090. +
  19091. +/* ---------------------------------------------------------------------- */
  19092. +
  19093. +/* inode.c */
  19094. +struct inode *au_igrab(struct inode *inode);
  19095. +void au_refresh_iop(struct inode *inode, int force_getattr);
  19096. +int au_refresh_hinode_self(struct inode *inode);
  19097. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  19098. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  19099. + unsigned int d_type, ino_t *ino);
  19100. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  19101. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  19102. + struct inode *inode);
  19103. +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
  19104. + int mask);
  19105. +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
  19106. + int mask);
  19107. +
  19108. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  19109. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  19110. +{
  19111. +#ifdef CONFIG_AUFS_SHWH
  19112. + return au_ino(sb, bindex, h_ino, d_type, ino);
  19113. +#else
  19114. + return 0;
  19115. +#endif
  19116. +}
  19117. +
  19118. +/* i_op.c */
  19119. +enum {
  19120. + AuIop_SYMLINK,
  19121. + AuIop_DIR,
  19122. + AuIop_OTHER,
  19123. + AuIop_Last
  19124. +};
  19125. +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
  19126. + aufs_iop_nogetattr[AuIop_Last];
  19127. +
  19128. +/* au_wr_dir flags */
  19129. +#define AuWrDir_ADD_ENTRY 1
  19130. +#define AuWrDir_ISDIR (1 << 1)
  19131. +#define AuWrDir_TMPFILE (1 << 2)
  19132. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  19133. +#define au_fset_wrdir(flags, name) \
  19134. + do { (flags) |= AuWrDir_##name; } while (0)
  19135. +#define au_fclr_wrdir(flags, name) \
  19136. + do { (flags) &= ~AuWrDir_##name; } while (0)
  19137. +
  19138. +struct au_wr_dir_args {
  19139. + aufs_bindex_t force_btgt;
  19140. + unsigned char flags;
  19141. +};
  19142. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  19143. + struct au_wr_dir_args *args);
  19144. +
  19145. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  19146. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  19147. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  19148. + unsigned int udba, unsigned char flags);
  19149. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  19150. + unsigned int udba, unsigned char flags) __must_check;
  19151. +int au_do_pin(struct au_pin *pin) __must_check;
  19152. +void au_unpin(struct au_pin *pin);
  19153. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  19154. +
  19155. +#define AuIcpup_DID_CPUP 1
  19156. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  19157. +#define au_fset_icpup(flags, name) \
  19158. + do { (flags) |= AuIcpup_##name; } while (0)
  19159. +#define au_fclr_icpup(flags, name) \
  19160. + do { (flags) &= ~AuIcpup_##name; } while (0)
  19161. +
  19162. +struct au_icpup_args {
  19163. + unsigned char flags;
  19164. + unsigned char pin_flags;
  19165. + aufs_bindex_t btgt;
  19166. + unsigned int udba;
  19167. + struct au_pin pin;
  19168. + struct path h_path;
  19169. + struct inode *h_inode;
  19170. +};
  19171. +
  19172. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  19173. + struct au_icpup_args *a);
  19174. +
  19175. +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
  19176. + struct path *h_path, int locked);
  19177. +
  19178. +/* i_op_add.c */
  19179. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  19180. + struct dentry *h_parent, int isdir);
  19181. +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  19182. + struct dentry *dentry, umode_t mode, dev_t dev);
  19183. +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  19184. + struct dentry *dentry, const char *symname);
  19185. +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
  19186. + struct dentry *dentry, umode_t mode, bool want_excl);
  19187. +struct vfsub_aopen_args;
  19188. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  19189. + struct vfsub_aopen_args *args);
  19190. +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
  19191. + struct file *file, umode_t mode);
  19192. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  19193. + struct dentry *dentry);
  19194. +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  19195. + struct dentry *dentry, umode_t mode);
  19196. +
  19197. +/* i_op_del.c */
  19198. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  19199. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  19200. + struct dentry *h_parent, int isdir);
  19201. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  19202. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  19203. +
  19204. +/* i_op_ren.c */
  19205. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  19206. +int aufs_rename(struct mnt_idmap *idmap,
  19207. + struct inode *_src_dir, struct dentry *_src_dentry,
  19208. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  19209. + unsigned int _flags);
  19210. +
  19211. +/* iinfo.c */
  19212. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  19213. +void au_hiput(struct au_hinode *hinode);
  19214. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  19215. + struct dentry *h_wh);
  19216. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  19217. +
  19218. +/* hinode flags */
  19219. +#define AuHi_XINO 1
  19220. +#define AuHi_HNOTIFY (1 << 1)
  19221. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  19222. +#define au_fset_hi(flags, name) \
  19223. + do { (flags) |= AuHi_##name; } while (0)
  19224. +#define au_fclr_hi(flags, name) \
  19225. + do { (flags) &= ~AuHi_##name; } while (0)
  19226. +
  19227. +#ifndef CONFIG_AUFS_HNOTIFY
  19228. +#undef AuHi_HNOTIFY
  19229. +#define AuHi_HNOTIFY 0
  19230. +#endif
  19231. +
  19232. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  19233. + struct inode *h_inode, unsigned int flags);
  19234. +
  19235. +void au_update_iigen(struct inode *inode, int half);
  19236. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  19237. +
  19238. +void au_icntnr_init_once(void *_c);
  19239. +void au_hinode_init(struct au_hinode *hinode);
  19240. +int au_iinfo_init(struct inode *inode);
  19241. +void au_iinfo_fin(struct inode *inode);
  19242. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
  19243. +
  19244. +#ifdef CONFIG_PROC_FS
  19245. +/* plink.c */
  19246. +int au_plink_maint(struct super_block *sb, int flags);
  19247. +struct au_sbinfo;
  19248. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  19249. +int au_plink_maint_enter(struct super_block *sb);
  19250. +#ifdef CONFIG_AUFS_DEBUG
  19251. +void au_plink_list(struct super_block *sb);
  19252. +#else
  19253. +AuStubVoid(au_plink_list, struct super_block *sb)
  19254. +#endif
  19255. +int au_plink_test(struct inode *inode);
  19256. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  19257. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  19258. + struct dentry *h_dentry);
  19259. +void au_plink_put(struct super_block *sb, int verbose);
  19260. +void au_plink_clean(struct super_block *sb, int verbose);
  19261. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  19262. +#else
  19263. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  19264. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  19265. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  19266. +AuStubVoid(au_plink_list, struct super_block *sb);
  19267. +AuStubInt0(au_plink_test, struct inode *inode);
  19268. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  19269. + struct inode *inode, aufs_bindex_t bindex);
  19270. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  19271. + struct dentry *h_dentry);
  19272. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  19273. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  19274. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  19275. +#endif /* CONFIG_PROC_FS */
  19276. +
  19277. +#ifdef CONFIG_AUFS_XATTR
  19278. +/* xattr.c */
  19279. +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
  19280. + unsigned int verbose);
  19281. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  19282. +void au_xattr_init(struct super_block *sb);
  19283. +#else
  19284. +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
  19285. + int ignore_flags, unsigned int verbose);
  19286. +AuStubVoid(au_xattr_init, struct super_block *sb);
  19287. +#endif
  19288. +
  19289. +#ifdef CONFIG_FS_POSIX_ACL
  19290. +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
  19291. +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
  19292. + struct dentry *dentry, int type);
  19293. +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  19294. + struct posix_acl *acl, int type);
  19295. +#endif
  19296. +
  19297. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  19298. +enum {
  19299. + AU_XATTR_SET,
  19300. + AU_ACL_SET
  19301. +};
  19302. +
  19303. +struct au_sxattr {
  19304. + int type;
  19305. + union {
  19306. + struct {
  19307. + const char *name;
  19308. + const void *value;
  19309. + size_t size;
  19310. + int flags;
  19311. + } set;
  19312. + struct {
  19313. + struct posix_acl *acl;
  19314. + int type;
  19315. + } acl_set;
  19316. + } u;
  19317. +};
  19318. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  19319. + struct au_sxattr *arg);
  19320. +#endif
  19321. +
  19322. +/* ---------------------------------------------------------------------- */
  19323. +
  19324. +/* lock subclass for iinfo */
  19325. +enum {
  19326. + AuLsc_II_CHILD, /* child first */
  19327. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  19328. + AuLsc_II_CHILD3, /* copyup dirs */
  19329. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  19330. + AuLsc_II_PARENT2,
  19331. + AuLsc_II_PARENT3, /* copyup dirs */
  19332. + AuLsc_II_NEW_CHILD
  19333. +};
  19334. +
  19335. +/*
  19336. + * ii_read_lock_child, ii_write_lock_child,
  19337. + * ii_read_lock_child2, ii_write_lock_child2,
  19338. + * ii_read_lock_child3, ii_write_lock_child3,
  19339. + * ii_read_lock_parent, ii_write_lock_parent,
  19340. + * ii_read_lock_parent2, ii_write_lock_parent2,
  19341. + * ii_read_lock_parent3, ii_write_lock_parent3,
  19342. + * ii_read_lock_new_child, ii_write_lock_new_child,
  19343. + */
  19344. +#define AuReadLockFunc(name, lsc) \
  19345. +static inline void ii_read_lock_##name(struct inode *i) \
  19346. +{ \
  19347. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  19348. +}
  19349. +
  19350. +#define AuWriteLockFunc(name, lsc) \
  19351. +static inline void ii_write_lock_##name(struct inode *i) \
  19352. +{ \
  19353. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  19354. +}
  19355. +
  19356. +#define AuRWLockFuncs(name, lsc) \
  19357. + AuReadLockFunc(name, lsc) \
  19358. + AuWriteLockFunc(name, lsc)
  19359. +
  19360. +AuRWLockFuncs(child, CHILD);
  19361. +AuRWLockFuncs(child2, CHILD2);
  19362. +AuRWLockFuncs(child3, CHILD3);
  19363. +AuRWLockFuncs(parent, PARENT);
  19364. +AuRWLockFuncs(parent2, PARENT2);
  19365. +AuRWLockFuncs(parent3, PARENT3);
  19366. +AuRWLockFuncs(new_child, NEW_CHILD);
  19367. +
  19368. +#undef AuReadLockFunc
  19369. +#undef AuWriteLockFunc
  19370. +#undef AuRWLockFuncs
  19371. +
  19372. +#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
  19373. +#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
  19374. +#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
  19375. +
  19376. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  19377. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  19378. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  19379. +
  19380. +/* ---------------------------------------------------------------------- */
  19381. +
  19382. +static inline void au_icntnr_init(struct au_icntnr *c)
  19383. +{
  19384. +#ifdef CONFIG_AUFS_DEBUG
  19385. + c->vfs_inode.i_mode = 0;
  19386. +#endif
  19387. +}
  19388. +
  19389. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  19390. +{
  19391. + unsigned int gen;
  19392. + struct au_iinfo *iinfo;
  19393. + struct au_iigen *iigen;
  19394. +
  19395. + iinfo = au_ii(inode);
  19396. + iigen = &iinfo->ii_generation;
  19397. + spin_lock(&iigen->ig_spin);
  19398. + if (igflags)
  19399. + *igflags = iigen->ig_flags;
  19400. + gen = iigen->ig_generation;
  19401. + spin_unlock(&iigen->ig_spin);
  19402. +
  19403. + return gen;
  19404. +}
  19405. +
  19406. +/* tiny test for inode number */
  19407. +/* tmpfs generation is too rough */
  19408. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  19409. +{
  19410. + struct au_iinfo *iinfo;
  19411. +
  19412. + iinfo = au_ii(inode);
  19413. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  19414. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  19415. + && iinfo->ii_higen == h_inode->i_generation);
  19416. +}
  19417. +
  19418. +static inline void au_iigen_dec(struct inode *inode)
  19419. +{
  19420. + struct au_iinfo *iinfo;
  19421. + struct au_iigen *iigen;
  19422. +
  19423. + iinfo = au_ii(inode);
  19424. + iigen = &iinfo->ii_generation;
  19425. + spin_lock(&iigen->ig_spin);
  19426. + iigen->ig_generation--;
  19427. + spin_unlock(&iigen->ig_spin);
  19428. +}
  19429. +
  19430. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  19431. +{
  19432. + int err;
  19433. +
  19434. + err = 0;
  19435. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  19436. + err = -EIO;
  19437. +
  19438. + return err;
  19439. +}
  19440. +
  19441. +/* ---------------------------------------------------------------------- */
  19442. +
  19443. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  19444. + aufs_bindex_t bindex)
  19445. +{
  19446. + return iinfo->ii_hinode + bindex;
  19447. +}
  19448. +
  19449. +static inline int au_is_bad_inode(struct inode *inode)
  19450. +{
  19451. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  19452. +}
  19453. +
  19454. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  19455. + aufs_bindex_t bindex)
  19456. +{
  19457. + IiMustAnyLock(inode);
  19458. + return au_hinode(au_ii(inode), bindex)->hi_id;
  19459. +}
  19460. +
  19461. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  19462. +{
  19463. + IiMustAnyLock(inode);
  19464. + return au_ii(inode)->ii_btop;
  19465. +}
  19466. +
  19467. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  19468. +{
  19469. + IiMustAnyLock(inode);
  19470. + return au_ii(inode)->ii_bbot;
  19471. +}
  19472. +
  19473. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  19474. +{
  19475. + IiMustAnyLock(inode);
  19476. + return au_ii(inode)->ii_vdir;
  19477. +}
  19478. +
  19479. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  19480. +{
  19481. + IiMustAnyLock(inode);
  19482. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  19483. +}
  19484. +
  19485. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  19486. +{
  19487. + IiMustWriteLock(inode);
  19488. + au_ii(inode)->ii_btop = bindex;
  19489. +}
  19490. +
  19491. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  19492. +{
  19493. + IiMustWriteLock(inode);
  19494. + au_ii(inode)->ii_bbot = bindex;
  19495. +}
  19496. +
  19497. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  19498. +{
  19499. + IiMustWriteLock(inode);
  19500. + au_ii(inode)->ii_vdir = vdir;
  19501. +}
  19502. +
  19503. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  19504. +{
  19505. + IiMustAnyLock(inode);
  19506. + return au_hinode(au_ii(inode), bindex);
  19507. +}
  19508. +
  19509. +/* ---------------------------------------------------------------------- */
  19510. +
  19511. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  19512. +{
  19513. + if (pin)
  19514. + return pin->parent;
  19515. + return NULL;
  19516. +}
  19517. +
  19518. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  19519. +{
  19520. + if (pin && pin->hdir)
  19521. + return pin->hdir->hi_inode;
  19522. + return NULL;
  19523. +}
  19524. +
  19525. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  19526. +{
  19527. + if (pin)
  19528. + return pin->hdir;
  19529. + return NULL;
  19530. +}
  19531. +
  19532. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  19533. +{
  19534. + if (pin)
  19535. + pin->dentry = dentry;
  19536. +}
  19537. +
  19538. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  19539. + unsigned char lflag)
  19540. +{
  19541. + if (pin) {
  19542. + if (lflag)
  19543. + au_fset_pin(pin->flags, DI_LOCKED);
  19544. + else
  19545. + au_fclr_pin(pin->flags, DI_LOCKED);
  19546. + }
  19547. +}
  19548. +
  19549. +#if 0 /* reserved */
  19550. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  19551. +{
  19552. + if (pin) {
  19553. + dput(pin->parent);
  19554. + pin->parent = dget(parent);
  19555. + }
  19556. +}
  19557. +#endif
  19558. +
  19559. +/* ---------------------------------------------------------------------- */
  19560. +
  19561. +struct au_branch;
  19562. +#ifdef CONFIG_AUFS_HNOTIFY
  19563. +struct au_hnotify_op {
  19564. + void (*ctl)(struct au_hinode *hinode, int do_set);
  19565. + int (*alloc)(struct au_hinode *hinode);
  19566. +
  19567. + /*
  19568. + * if it returns true, the caller should free hinode->hi_notify,
  19569. + * otherwise ->free() frees it.
  19570. + */
  19571. + int (*free)(struct au_hinode *hinode,
  19572. + struct au_hnotify *hn) __must_check;
  19573. +
  19574. + void (*fin)(void);
  19575. + int (*init)(void);
  19576. +
  19577. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  19578. + void (*fin_br)(struct au_branch *br);
  19579. + int (*init_br)(struct au_branch *br, int perm);
  19580. +};
  19581. +
  19582. +/* hnotify.c */
  19583. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  19584. +void au_hn_free(struct au_hinode *hinode);
  19585. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  19586. +void au_hn_reset(struct inode *inode, unsigned int flags);
  19587. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  19588. + const struct qstr *h_child_qstr, struct inode *h_child_inode);
  19589. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  19590. +int au_hnotify_init_br(struct au_branch *br, int perm);
  19591. +void au_hnotify_fin_br(struct au_branch *br);
  19592. +int __init au_hnotify_init(void);
  19593. +void au_hnotify_fin(void);
  19594. +
  19595. +/* hfsnotify.c */
  19596. +extern const struct au_hnotify_op au_hnotify_op;
  19597. +
  19598. +static inline
  19599. +void au_hn_init(struct au_hinode *hinode)
  19600. +{
  19601. + hinode->hi_notify = NULL;
  19602. +}
  19603. +
  19604. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  19605. +{
  19606. + return hinode->hi_notify;
  19607. +}
  19608. +
  19609. +#else
  19610. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  19611. + struct au_hinode *hinode __maybe_unused,
  19612. + struct inode *inode __maybe_unused)
  19613. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  19614. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  19615. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  19616. + int do_set __maybe_unused)
  19617. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  19618. + unsigned int flags __maybe_unused)
  19619. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  19620. + struct au_branch *br __maybe_unused,
  19621. + int perm __maybe_unused)
  19622. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  19623. + int perm __maybe_unused)
  19624. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  19625. +AuStubInt0(__init au_hnotify_init, void)
  19626. +AuStubVoid(au_hnotify_fin, void)
  19627. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  19628. +#endif /* CONFIG_AUFS_HNOTIFY */
  19629. +
  19630. +static inline void au_hn_suspend(struct au_hinode *hdir)
  19631. +{
  19632. + au_hn_ctl(hdir, /*do_set*/0);
  19633. +}
  19634. +
  19635. +static inline void au_hn_resume(struct au_hinode *hdir)
  19636. +{
  19637. + au_hn_ctl(hdir, /*do_set*/1);
  19638. +}
  19639. +
  19640. +static inline void au_hn_inode_lock(struct au_hinode *hdir)
  19641. +{
  19642. + inode_lock(hdir->hi_inode);
  19643. + au_hn_suspend(hdir);
  19644. +}
  19645. +
  19646. +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
  19647. + unsigned int sc __maybe_unused)
  19648. +{
  19649. + inode_lock_nested(hdir->hi_inode, sc);
  19650. + au_hn_suspend(hdir);
  19651. +}
  19652. +
  19653. +#if 0 /* unused */
  19654. +#include "vfsub.h"
  19655. +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
  19656. + unsigned int sc)
  19657. +{
  19658. + inode_lock_shared_nested(hdir->hi_inode, sc);
  19659. + au_hn_suspend(hdir);
  19660. +}
  19661. +#endif
  19662. +
  19663. +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
  19664. +{
  19665. + au_hn_resume(hdir);
  19666. + inode_unlock(hdir->hi_inode);
  19667. +}
  19668. +
  19669. +#endif /* __KERNEL__ */
  19670. +#endif /* __AUFS_INODE_H__ */
  19671. diff -Naur null/fs/aufs/ioctl.c linux-6.6.63/fs/aufs/ioctl.c
  19672. --- /dev/null
  19673. +++ linux-6.6.63/fs/aufs/ioctl.c 2024-12-18 15:12:59.298519551 +0300
  19674. @@ -0,0 +1,220 @@
  19675. +// SPDX-License-Identifier: GPL-2.0
  19676. +/*
  19677. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  19678. + *
  19679. + * This program is free software; you can redistribute it and/or modify
  19680. + * it under the terms of the GNU General Public License as published by
  19681. + * the Free Software Foundation; either version 2 of the License, or
  19682. + * (at your option) any later version.
  19683. + *
  19684. + * This program is distributed in the hope that it will be useful,
  19685. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19686. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19687. + * GNU General Public License for more details.
  19688. + *
  19689. + * You should have received a copy of the GNU General Public License
  19690. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19691. + */
  19692. +
  19693. +/*
  19694. + * ioctl
  19695. + * plink-management and readdir in userspace.
  19696. + * assist the pathconf(3) wrapper library.
  19697. + * move-down
  19698. + * File-based Hierarchical Storage Management.
  19699. + */
  19700. +
  19701. +#include <linux/compat.h>
  19702. +#include <linux/file.h>
  19703. +#include "aufs.h"
  19704. +
  19705. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  19706. +{
  19707. + int err, fd;
  19708. + aufs_bindex_t wbi, bindex, bbot;
  19709. + struct file *h_file;
  19710. + struct super_block *sb;
  19711. + struct dentry *root;
  19712. + struct au_branch *br;
  19713. + struct aufs_wbr_fd wbrfd = {
  19714. + .oflags = au_dir_roflags,
  19715. + .brid = -1
  19716. + };
  19717. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  19718. + | O_NOATIME | O_CLOEXEC;
  19719. +
  19720. + AuDebugOn(wbrfd.oflags & ~valid);
  19721. +
  19722. + if (arg) {
  19723. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  19724. + if (unlikely(err)) {
  19725. + err = -EFAULT;
  19726. + goto out;
  19727. + }
  19728. +
  19729. + err = -EINVAL;
  19730. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  19731. + wbrfd.oflags |= au_dir_roflags;
  19732. + AuDbg("0%o\n", wbrfd.oflags);
  19733. + if (unlikely(wbrfd.oflags & ~valid))
  19734. + goto out;
  19735. + }
  19736. +
  19737. + fd = get_unused_fd_flags(0);
  19738. + err = fd;
  19739. + if (unlikely(fd < 0))
  19740. + goto out;
  19741. +
  19742. + h_file = ERR_PTR(-EINVAL);
  19743. + wbi = 0;
  19744. + br = NULL;
  19745. + sb = path->dentry->d_sb;
  19746. + root = sb->s_root;
  19747. + aufs_read_lock(root, AuLock_IR);
  19748. + bbot = au_sbbot(sb);
  19749. + if (wbrfd.brid >= 0) {
  19750. + wbi = au_br_index(sb, wbrfd.brid);
  19751. + if (unlikely(wbi < 0 || wbi > bbot))
  19752. + goto out_unlock;
  19753. + }
  19754. +
  19755. + h_file = ERR_PTR(-ENOENT);
  19756. + br = au_sbr(sb, wbi);
  19757. + if (!au_br_writable(br->br_perm)) {
  19758. + if (arg)
  19759. + goto out_unlock;
  19760. +
  19761. + bindex = wbi + 1;
  19762. + wbi = -1;
  19763. + for (; bindex <= bbot; bindex++) {
  19764. + br = au_sbr(sb, bindex);
  19765. + if (au_br_writable(br->br_perm)) {
  19766. + wbi = bindex;
  19767. + br = au_sbr(sb, wbi);
  19768. + break;
  19769. + }
  19770. + }
  19771. + }
  19772. + AuDbg("wbi %d\n", wbi);
  19773. + if (wbi >= 0)
  19774. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  19775. + /*force_wr*/0);
  19776. +
  19777. +out_unlock:
  19778. + aufs_read_unlock(root, AuLock_IR);
  19779. + err = PTR_ERR(h_file);
  19780. + if (IS_ERR(h_file))
  19781. + goto out_fd;
  19782. +
  19783. + au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
  19784. + fd_install(fd, h_file);
  19785. + err = fd;
  19786. + goto out; /* success */
  19787. +
  19788. +out_fd:
  19789. + put_unused_fd(fd);
  19790. +out:
  19791. + AuTraceErr(err);
  19792. + return err;
  19793. +}
  19794. +
  19795. +/* ---------------------------------------------------------------------- */
  19796. +
  19797. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  19798. +{
  19799. + long err;
  19800. + struct dentry *dentry;
  19801. +
  19802. + switch (cmd) {
  19803. + case AUFS_CTL_RDU:
  19804. + case AUFS_CTL_RDU_INO:
  19805. + err = au_rdu_ioctl(file, cmd, arg);
  19806. + break;
  19807. +
  19808. + case AUFS_CTL_WBR_FD:
  19809. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  19810. + break;
  19811. +
  19812. + case AUFS_CTL_IBUSY:
  19813. + err = au_ibusy_ioctl(file, arg);
  19814. + break;
  19815. +
  19816. + case AUFS_CTL_BRINFO:
  19817. + err = au_brinfo_ioctl(file, arg);
  19818. + break;
  19819. +
  19820. + case AUFS_CTL_FHSM_FD:
  19821. + dentry = file->f_path.dentry;
  19822. + if (IS_ROOT(dentry))
  19823. + err = au_fhsm_fd(dentry->d_sb, arg);
  19824. + else
  19825. + err = -ENOTTY;
  19826. + break;
  19827. +
  19828. + default:
  19829. + /* do not call the lower */
  19830. + AuDbg("0x%x\n", cmd);
  19831. + err = -ENOTTY;
  19832. + }
  19833. +
  19834. + AuTraceErr(err);
  19835. + return err;
  19836. +}
  19837. +
  19838. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  19839. +{
  19840. + long err;
  19841. +
  19842. + switch (cmd) {
  19843. + case AUFS_CTL_MVDOWN:
  19844. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  19845. + break;
  19846. +
  19847. + case AUFS_CTL_WBR_FD:
  19848. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  19849. + break;
  19850. +
  19851. + default:
  19852. + /* do not call the lower */
  19853. + AuDbg("0x%x\n", cmd);
  19854. + err = -ENOTTY;
  19855. + }
  19856. +
  19857. + AuTraceErr(err);
  19858. + return err;
  19859. +}
  19860. +
  19861. +#ifdef CONFIG_COMPAT
  19862. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  19863. + unsigned long arg)
  19864. +{
  19865. + long err;
  19866. +
  19867. + switch (cmd) {
  19868. + case AUFS_CTL_RDU:
  19869. + case AUFS_CTL_RDU_INO:
  19870. + err = au_rdu_compat_ioctl(file, cmd, arg);
  19871. + break;
  19872. +
  19873. + case AUFS_CTL_IBUSY:
  19874. + err = au_ibusy_compat_ioctl(file, arg);
  19875. + break;
  19876. +
  19877. + case AUFS_CTL_BRINFO:
  19878. + err = au_brinfo_compat_ioctl(file, arg);
  19879. + break;
  19880. +
  19881. + default:
  19882. + err = aufs_ioctl_dir(file, cmd, arg);
  19883. + }
  19884. +
  19885. + AuTraceErr(err);
  19886. + return err;
  19887. +}
  19888. +
  19889. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  19890. + unsigned long arg)
  19891. +{
  19892. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  19893. +}
  19894. +#endif
  19895. diff -Naur null/fs/aufs/i_op_add.c linux-6.6.63/fs/aufs/i_op_add.c
  19896. --- /dev/null
  19897. +++ linux-6.6.63/fs/aufs/i_op_add.c 2024-12-18 15:12:59.297519563 +0300
  19898. @@ -0,0 +1,971 @@
  19899. +// SPDX-License-Identifier: GPL-2.0
  19900. +/*
  19901. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  19902. + *
  19903. + * This program is free software; you can redistribute it and/or modify
  19904. + * it under the terms of the GNU General Public License as published by
  19905. + * the Free Software Foundation; either version 2 of the License, or
  19906. + * (at your option) any later version.
  19907. + *
  19908. + * This program is distributed in the hope that it will be useful,
  19909. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19910. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19911. + * GNU General Public License for more details.
  19912. + *
  19913. + * You should have received a copy of the GNU General Public License
  19914. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19915. + */
  19916. +
  19917. +/*
  19918. + * inode operations (add entry)
  19919. + */
  19920. +
  19921. +#include <linux/iversion.h>
  19922. +#include "aufs.h"
  19923. +
  19924. +/*
  19925. + * final procedure of adding a new entry, except link(2).
  19926. + * remove whiteout, instantiate, copyup the parent dir's times and size
  19927. + * and update version.
  19928. + * if it failed, re-create the removed whiteout.
  19929. + */
  19930. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  19931. + struct dentry *wh_dentry, struct dentry *dentry)
  19932. +{
  19933. + int err, rerr;
  19934. + aufs_bindex_t bwh;
  19935. + struct path h_path;
  19936. + struct super_block *sb;
  19937. + struct inode *inode, *h_dir;
  19938. + struct dentry *wh;
  19939. +
  19940. + bwh = -1;
  19941. + sb = dir->i_sb;
  19942. + if (wh_dentry) {
  19943. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  19944. + IMustLock(h_dir);
  19945. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  19946. + bwh = au_dbwh(dentry);
  19947. + h_path.dentry = wh_dentry;
  19948. + h_path.mnt = au_sbr_mnt(sb, bindex);
  19949. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  19950. + dentry);
  19951. + if (unlikely(err))
  19952. + goto out;
  19953. + }
  19954. +
  19955. + inode = au_new_inode(dentry, /*must_new*/1);
  19956. + if (!IS_ERR(inode)) {
  19957. + d_instantiate(dentry, inode);
  19958. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  19959. + IMustLock(dir);
  19960. + au_dir_ts(dir, bindex);
  19961. + inode_inc_iversion(dir);
  19962. + au_fhsm_wrote(sb, bindex, /*force*/0);
  19963. + return 0; /* success */
  19964. + }
  19965. +
  19966. + err = PTR_ERR(inode);
  19967. + if (!wh_dentry)
  19968. + goto out;
  19969. +
  19970. + /* revert */
  19971. + /* dir inode is locked */
  19972. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  19973. + rerr = PTR_ERR(wh);
  19974. + if (IS_ERR(wh)) {
  19975. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  19976. + dentry, err, rerr);
  19977. + err = -EIO;
  19978. + } else
  19979. + dput(wh);
  19980. +
  19981. +out:
  19982. + return err;
  19983. +}
  19984. +
  19985. +static int au_d_may_add(struct dentry *dentry)
  19986. +{
  19987. + int err;
  19988. +
  19989. + err = 0;
  19990. + if (unlikely(d_unhashed(dentry)))
  19991. + err = -ENOENT;
  19992. + if (unlikely(d_really_is_positive(dentry)))
  19993. + err = -EEXIST;
  19994. + return err;
  19995. +}
  19996. +
  19997. +/*
  19998. + * simple tests for the adding inode operations.
  19999. + * following the checks in vfs, plus the parent-child relationship.
  20000. + */
  20001. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  20002. + struct dentry *h_parent, int isdir)
  20003. +{
  20004. + int err;
  20005. + umode_t h_mode;
  20006. + struct dentry *h_dentry;
  20007. + struct inode *h_inode;
  20008. +
  20009. + err = -ENAMETOOLONG;
  20010. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  20011. + goto out;
  20012. +
  20013. + h_dentry = au_h_dptr(dentry, bindex);
  20014. + if (d_really_is_negative(dentry)) {
  20015. + err = -EEXIST;
  20016. + if (unlikely(d_is_positive(h_dentry)))
  20017. + goto out;
  20018. + } else {
  20019. + /* rename(2) case */
  20020. + err = -EIO;
  20021. + if (unlikely(d_is_negative(h_dentry)))
  20022. + goto out;
  20023. + h_inode = d_inode(h_dentry);
  20024. + if (unlikely(!vfsub_inode_nlink(h_inode, AU_I_BRANCH)))
  20025. + goto out;
  20026. +
  20027. + h_mode = h_inode->i_mode;
  20028. + if (!isdir) {
  20029. + err = -EISDIR;
  20030. + if (unlikely(S_ISDIR(h_mode)))
  20031. + goto out;
  20032. + } else if (unlikely(!S_ISDIR(h_mode))) {
  20033. + err = -ENOTDIR;
  20034. + goto out;
  20035. + }
  20036. + }
  20037. +
  20038. + err = 0;
  20039. + /* expected parent dir is locked */
  20040. + if (unlikely(h_parent != h_dentry->d_parent))
  20041. + err = -EIO;
  20042. +
  20043. +out:
  20044. + AuTraceErr(err);
  20045. + return err;
  20046. +}
  20047. +
  20048. +/*
  20049. + * initial procedure of adding a new entry.
  20050. + * prepare writable branch and the parent dir, lock it,
  20051. + * and lookup whiteout for the new entry.
  20052. + */
  20053. +static struct dentry*
  20054. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  20055. + struct dentry *src_dentry, struct au_pin *pin,
  20056. + struct au_wr_dir_args *wr_dir_args)
  20057. +{
  20058. + struct dentry *wh_dentry, *h_parent;
  20059. + struct super_block *sb;
  20060. + struct au_branch *br;
  20061. + int err;
  20062. + unsigned int udba;
  20063. + aufs_bindex_t bcpup;
  20064. +
  20065. + AuDbg("%pd\n", dentry);
  20066. +
  20067. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  20068. + bcpup = err;
  20069. + wh_dentry = ERR_PTR(err);
  20070. + if (unlikely(err < 0))
  20071. + goto out;
  20072. +
  20073. + sb = dentry->d_sb;
  20074. + udba = au_opt_udba(sb);
  20075. + err = au_pin(pin, dentry, bcpup, udba,
  20076. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20077. + wh_dentry = ERR_PTR(err);
  20078. + if (unlikely(err))
  20079. + goto out;
  20080. +
  20081. + h_parent = au_pinned_h_parent(pin);
  20082. + if (udba != AuOpt_UDBA_NONE
  20083. + && au_dbtop(dentry) == bcpup)
  20084. + err = au_may_add(dentry, bcpup, h_parent,
  20085. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  20086. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  20087. + err = -ENAMETOOLONG;
  20088. + wh_dentry = ERR_PTR(err);
  20089. + if (unlikely(err))
  20090. + goto out_unpin;
  20091. +
  20092. + br = au_sbr(sb, bcpup);
  20093. + if (dt) {
  20094. + struct path tmp = {
  20095. + .dentry = h_parent,
  20096. + .mnt = au_br_mnt(br)
  20097. + };
  20098. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  20099. + }
  20100. +
  20101. + wh_dentry = NULL;
  20102. + if (bcpup != au_dbwh(dentry))
  20103. + goto out; /* success */
  20104. +
  20105. + /*
  20106. + * ENAMETOOLONG here means that if we allowed create such name, then it
  20107. + * would not be able to removed in the future. So we don't allow such
  20108. + * name here and we don't handle ENAMETOOLONG differently here.
  20109. + */
  20110. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  20111. +
  20112. +out_unpin:
  20113. + if (IS_ERR(wh_dentry))
  20114. + au_unpin(pin);
  20115. +out:
  20116. + return wh_dentry;
  20117. +}
  20118. +
  20119. +/* ---------------------------------------------------------------------- */
  20120. +
  20121. +enum { Mknod, Symlink, Creat };
  20122. +struct simple_arg {
  20123. + int type;
  20124. + union {
  20125. + struct {
  20126. + umode_t mode;
  20127. + bool want_excl;
  20128. + bool try_aopen;
  20129. + struct vfsub_aopen_args *aopen;
  20130. + } c;
  20131. + struct {
  20132. + const char *symname;
  20133. + } s;
  20134. + struct {
  20135. + umode_t mode;
  20136. + dev_t dev;
  20137. + } m;
  20138. + } u;
  20139. +};
  20140. +
  20141. +static int add_simple(struct inode *dir, struct dentry *dentry,
  20142. + struct simple_arg *arg)
  20143. +{
  20144. + int err, rerr;
  20145. + aufs_bindex_t btop;
  20146. + unsigned char created;
  20147. + const unsigned char try_aopen
  20148. + = (arg->type == Creat && arg->u.c.try_aopen);
  20149. + struct vfsub_aopen_args *aopen = arg->u.c.aopen;
  20150. + struct dentry *wh_dentry, *parent;
  20151. + struct inode *h_dir;
  20152. + struct super_block *sb;
  20153. + struct au_branch *br;
  20154. + /* to reduce stack size */
  20155. + struct {
  20156. + struct au_dtime dt;
  20157. + struct au_pin pin;
  20158. + struct path h_path;
  20159. + struct au_wr_dir_args wr_dir_args;
  20160. + } *a;
  20161. +
  20162. + AuDbg("%pd\n", dentry);
  20163. + IMustLock(dir);
  20164. +
  20165. + err = -ENOMEM;
  20166. + a = kmalloc(sizeof(*a), GFP_NOFS);
  20167. + if (unlikely(!a))
  20168. + goto out;
  20169. + a->wr_dir_args.force_btgt = -1;
  20170. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  20171. +
  20172. + parent = dentry->d_parent; /* dir inode is locked */
  20173. + if (!try_aopen) {
  20174. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  20175. + if (unlikely(err))
  20176. + goto out_free;
  20177. + }
  20178. + err = au_d_may_add(dentry);
  20179. + if (unlikely(err))
  20180. + goto out_unlock;
  20181. + if (!try_aopen)
  20182. + di_write_lock_parent(parent);
  20183. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  20184. + &a->pin, &a->wr_dir_args);
  20185. + err = PTR_ERR(wh_dentry);
  20186. + if (IS_ERR(wh_dentry))
  20187. + goto out_parent;
  20188. +
  20189. + btop = au_dbtop(dentry);
  20190. + sb = dentry->d_sb;
  20191. + br = au_sbr(sb, btop);
  20192. + a->h_path.dentry = au_h_dptr(dentry, btop);
  20193. + a->h_path.mnt = au_br_mnt(br);
  20194. + h_dir = au_pinned_h_dir(&a->pin);
  20195. + switch (arg->type) {
  20196. + case Creat:
  20197. + if (!try_aopen || !h_dir->i_op->atomic_open) {
  20198. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  20199. + arg->u.c.want_excl);
  20200. + created = !err;
  20201. + if (!err && try_aopen)
  20202. + aopen->file->f_mode |= FMODE_CREATED;
  20203. + } else {
  20204. + aopen->br = br;
  20205. + err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
  20206. + AuDbg("err %d\n", err);
  20207. + AuDbgFile(aopen->file);
  20208. + created = err >= 0
  20209. + && !!(aopen->file->f_mode & FMODE_CREATED);
  20210. + }
  20211. + break;
  20212. + case Symlink:
  20213. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  20214. + created = !err;
  20215. + break;
  20216. + case Mknod:
  20217. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  20218. + arg->u.m.dev);
  20219. + created = !err;
  20220. + break;
  20221. + default:
  20222. + BUG();
  20223. + }
  20224. + if (unlikely(err < 0))
  20225. + goto out_unpin;
  20226. +
  20227. + err = epilog(dir, btop, wh_dentry, dentry);
  20228. + if (!err)
  20229. + goto out_unpin; /* success */
  20230. +
  20231. + /* revert */
  20232. + if (created /* && d_is_positive(a->h_path.dentry) */) {
  20233. + /* no delegation since it is just created */
  20234. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  20235. + /*force*/0);
  20236. + if (rerr) {
  20237. + AuIOErr("%pd revert failure(%d, %d)\n",
  20238. + dentry, err, rerr);
  20239. + err = -EIO;
  20240. + }
  20241. + au_dtime_revert(&a->dt);
  20242. + }
  20243. + if (try_aopen && h_dir->i_op->atomic_open
  20244. + && (aopen->file->f_mode & FMODE_OPENED))
  20245. + /* aopen->file is still opened */
  20246. + au_lcnt_dec(&aopen->br->br_nfiles);
  20247. +
  20248. +out_unpin:
  20249. + au_unpin(&a->pin);
  20250. + dput(wh_dentry);
  20251. +out_parent:
  20252. + if (!try_aopen)
  20253. + di_write_unlock(parent);
  20254. +out_unlock:
  20255. + if (unlikely(err)) {
  20256. + au_update_dbtop(dentry);
  20257. + d_drop(dentry);
  20258. + }
  20259. + if (!try_aopen)
  20260. + aufs_read_unlock(dentry, AuLock_DW);
  20261. +out_free:
  20262. + au_kfree_rcu(a);
  20263. +out:
  20264. + return err;
  20265. +}
  20266. +
  20267. +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  20268. + struct dentry *dentry, umode_t mode, dev_t dev)
  20269. +{
  20270. + struct simple_arg arg = {
  20271. + .type = Mknod,
  20272. + .u.m = {
  20273. + .mode = mode,
  20274. + .dev = dev
  20275. + }
  20276. + };
  20277. + return add_simple(dir, dentry, &arg);
  20278. +}
  20279. +
  20280. +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
  20281. + struct dentry *dentry, const char *symname)
  20282. +{
  20283. + struct simple_arg arg = {
  20284. + .type = Symlink,
  20285. + .u.s.symname = symname
  20286. + };
  20287. + return add_simple(dir, dentry, &arg);
  20288. +}
  20289. +
  20290. +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
  20291. + struct dentry *dentry, umode_t mode, bool want_excl)
  20292. +{
  20293. + struct simple_arg arg = {
  20294. + .type = Creat,
  20295. + .u.c = {
  20296. + .mode = mode,
  20297. + .want_excl = want_excl
  20298. + }
  20299. + };
  20300. + return add_simple(dir, dentry, &arg);
  20301. +}
  20302. +
  20303. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  20304. + struct vfsub_aopen_args *aopen_args)
  20305. +{
  20306. + struct simple_arg arg = {
  20307. + .type = Creat,
  20308. + .u.c = {
  20309. + .mode = aopen_args->create_mode,
  20310. + .want_excl = aopen_args->open_flag & O_EXCL,
  20311. + .try_aopen = true,
  20312. + .aopen = aopen_args
  20313. + }
  20314. + };
  20315. + return add_simple(dir, dentry, &arg);
  20316. +}
  20317. +
  20318. +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
  20319. + struct file *file, umode_t mode)
  20320. +{
  20321. + int err;
  20322. + aufs_bindex_t bindex;
  20323. + struct path h_ppath;
  20324. + struct super_block *sb;
  20325. + struct au_branch *br;
  20326. + struct dentry *dentry, *parent, *h_parent, *h_dentry;
  20327. + struct inode *h_dir, *inode;
  20328. + struct vfsmount *h_mnt;
  20329. + struct mnt_idmap *h_idmap;
  20330. + struct file *h_file;
  20331. + struct au_wr_dir_args wr_dir_args = {
  20332. + .force_btgt = -1,
  20333. + .flags = AuWrDir_TMPFILE
  20334. + };
  20335. +
  20336. + /* copy-up may happen */
  20337. + inode_lock(dir);
  20338. +
  20339. + h_file = NULL;
  20340. + sb = dir->i_sb;
  20341. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20342. + if (unlikely(err))
  20343. + goto out;
  20344. +
  20345. + dentry = file->f_path.dentry;
  20346. + err = au_di_init(dentry);
  20347. + if (unlikely(err))
  20348. + goto out_si;
  20349. +
  20350. + err = -EBUSY;
  20351. + parent = d_find_any_alias(dir);
  20352. + AuDebugOn(!parent);
  20353. + di_write_lock_parent(parent);
  20354. + if (unlikely(d_inode(parent) != dir))
  20355. + goto out_parent;
  20356. +
  20357. + err = au_digen_test(parent, au_sigen(sb));
  20358. + if (unlikely(err))
  20359. + goto out_parent;
  20360. +
  20361. + bindex = au_dbtop(parent);
  20362. + au_set_dbtop(dentry, bindex);
  20363. + au_set_dbbot(dentry, bindex);
  20364. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  20365. + bindex = err;
  20366. + if (unlikely(err < 0))
  20367. + goto out_parent;
  20368. +
  20369. + err = -EOPNOTSUPP;
  20370. + h_dir = au_h_iptr(dir, bindex);
  20371. + if (unlikely(!h_dir->i_op->tmpfile))
  20372. + goto out_parent;
  20373. +
  20374. + br = au_sbr(sb, bindex);
  20375. + h_mnt = au_br_mnt(br);
  20376. + err = vfsub_mnt_want_write(h_mnt);
  20377. + if (unlikely(err))
  20378. + goto out_parent;
  20379. +
  20380. + h_idmap = mnt_idmap(h_mnt);
  20381. + h_parent = au_h_dptr(parent, bindex);
  20382. + h_ppath.mnt = h_mnt;
  20383. + h_ppath.dentry = h_parent;
  20384. + h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
  20385. + current_cred());
  20386. + if (IS_ERR(h_file)) {
  20387. + err = PTR_ERR(h_file);
  20388. + h_file = NULL;
  20389. + goto out_mnt;
  20390. + }
  20391. +
  20392. + h_dentry = h_file->f_path.dentry;
  20393. + au_set_dbtop(dentry, bindex);
  20394. + au_set_dbbot(dentry, bindex);
  20395. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  20396. + inode = au_new_inode(dentry, /*must_new*/1);
  20397. + if (IS_ERR(inode)) {
  20398. + err = PTR_ERR(inode);
  20399. + au_set_h_dptr(dentry, bindex, NULL);
  20400. + au_set_dbtop(dentry, -1);
  20401. + au_set_dbbot(dentry, -1);
  20402. + goto out_h_file;
  20403. + }
  20404. +
  20405. + vfsub_inode_nlink_init(inode, 1);
  20406. + d_tmpfile(file, inode);
  20407. + au_di(dentry)->di_tmpfile = 1;
  20408. + get_file(h_file);
  20409. + au_di(dentry)->di_htmpfile = h_file;
  20410. +
  20411. + /* update without i_mutex */
  20412. + if (au_ibtop(dir) == au_dbtop(dentry))
  20413. + au_cpup_attr_timesizes(dir);
  20414. +
  20415. +out_h_file:
  20416. + fput(h_file);
  20417. +out_mnt:
  20418. + vfsub_mnt_drop_write(h_mnt);
  20419. +out_parent:
  20420. + di_write_unlock(parent);
  20421. + dput(parent);
  20422. + di_write_unlock(dentry);
  20423. + if (!err)
  20424. + goto out_si;
  20425. + if (h_file)
  20426. + fput(h_file);
  20427. + au_di(dentry)->di_htmpfile = NULL;
  20428. + au_di_fin(dentry);
  20429. + dentry->d_fsdata = NULL;
  20430. +out_si:
  20431. + si_read_unlock(sb);
  20432. + if (!err && h_file) {
  20433. + /* finally... */
  20434. + err = finish_open_simple(file, err);
  20435. + if (!err)
  20436. + au_lcnt_inc(&br->br_nfiles);
  20437. + else {
  20438. + fput(h_file);
  20439. + au_di(dentry)->di_htmpfile = NULL;
  20440. + au_di_fin(dentry);
  20441. + dentry->d_fsdata = NULL;
  20442. + }
  20443. + }
  20444. +out:
  20445. + inode_unlock(dir);
  20446. + AuTraceErr(err);
  20447. + return err;
  20448. +}
  20449. +
  20450. +/* ---------------------------------------------------------------------- */
  20451. +
  20452. +struct au_link_args {
  20453. + aufs_bindex_t bdst, bsrc;
  20454. + struct au_pin pin;
  20455. + struct path h_path;
  20456. + struct dentry *src_parent, *parent;
  20457. +};
  20458. +
  20459. +static int au_cpup_before_link(struct dentry *src_dentry,
  20460. + struct au_link_args *a)
  20461. +{
  20462. + int err;
  20463. + struct dentry *h_src_dentry;
  20464. + struct au_cp_generic cpg = {
  20465. + .dentry = src_dentry,
  20466. + .bdst = a->bdst,
  20467. + .bsrc = a->bsrc,
  20468. + .len = -1,
  20469. + .pin = &a->pin,
  20470. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  20471. + };
  20472. +
  20473. + di_read_lock_parent(a->src_parent, AuLock_IR);
  20474. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  20475. + if (unlikely(err))
  20476. + goto out;
  20477. +
  20478. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  20479. + err = au_pin(&a->pin, src_dentry, a->bdst,
  20480. + au_opt_udba(src_dentry->d_sb),
  20481. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20482. + if (unlikely(err))
  20483. + goto out;
  20484. +
  20485. + err = au_sio_cpup_simple(&cpg);
  20486. + au_unpin(&a->pin);
  20487. +
  20488. +out:
  20489. + di_read_unlock(a->src_parent, AuLock_IR);
  20490. + return err;
  20491. +}
  20492. +
  20493. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  20494. + struct au_link_args *a)
  20495. +{
  20496. + int err;
  20497. + unsigned char plink;
  20498. + aufs_bindex_t bbot;
  20499. + struct dentry *h_src_dentry;
  20500. + struct inode *h_inode, *inode, *delegated;
  20501. + struct super_block *sb;
  20502. + struct file *h_file;
  20503. +
  20504. + plink = 0;
  20505. + h_inode = NULL;
  20506. + sb = src_dentry->d_sb;
  20507. + inode = d_inode(src_dentry);
  20508. + if (au_ibtop(inode) <= a->bdst)
  20509. + h_inode = au_h_iptr(inode, a->bdst);
  20510. + if (!h_inode || !vfsub_inode_nlink(h_inode, AU_I_BRANCH)) {
  20511. + /* copyup src_dentry as the name of dentry. */
  20512. + bbot = au_dbbot(dentry);
  20513. + if (bbot < a->bsrc)
  20514. + au_set_dbbot(dentry, a->bsrc);
  20515. + au_set_h_dptr(dentry, a->bsrc,
  20516. + dget(au_h_dptr(src_dentry, a->bsrc)));
  20517. + dget(a->h_path.dentry);
  20518. + au_set_h_dptr(dentry, a->bdst, NULL);
  20519. + AuDbg("temporary d_inode...\n");
  20520. + spin_lock(&dentry->d_lock);
  20521. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  20522. + spin_unlock(&dentry->d_lock);
  20523. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  20524. + if (IS_ERR(h_file))
  20525. + err = PTR_ERR(h_file);
  20526. + else {
  20527. + struct au_cp_generic cpg = {
  20528. + .dentry = dentry,
  20529. + .bdst = a->bdst,
  20530. + .bsrc = -1,
  20531. + .len = -1,
  20532. + .pin = &a->pin,
  20533. + .flags = AuCpup_KEEPLINO
  20534. + };
  20535. + err = au_sio_cpup_simple(&cpg);
  20536. + au_h_open_post(dentry, a->bsrc, h_file);
  20537. + if (!err) {
  20538. + dput(a->h_path.dentry);
  20539. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  20540. + } else
  20541. + au_set_h_dptr(dentry, a->bdst,
  20542. + a->h_path.dentry);
  20543. + }
  20544. + spin_lock(&dentry->d_lock);
  20545. + dentry->d_inode = NULL; /* restore */
  20546. + spin_unlock(&dentry->d_lock);
  20547. + AuDbg("temporary d_inode...done\n");
  20548. + au_set_h_dptr(dentry, a->bsrc, NULL);
  20549. + au_set_dbbot(dentry, bbot);
  20550. + } else {
  20551. + /* the inode of src_dentry already exists on a.bdst branch */
  20552. + h_src_dentry = d_find_alias(h_inode);
  20553. + if (!h_src_dentry && au_plink_test(inode)) {
  20554. + plink = 1;
  20555. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  20556. + err = PTR_ERR(h_src_dentry);
  20557. + if (IS_ERR(h_src_dentry))
  20558. + goto out;
  20559. +
  20560. + if (unlikely(d_is_negative(h_src_dentry))) {
  20561. + dput(h_src_dentry);
  20562. + h_src_dentry = NULL;
  20563. + }
  20564. +
  20565. + }
  20566. + if (h_src_dentry) {
  20567. + delegated = NULL;
  20568. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  20569. + &a->h_path, &delegated);
  20570. + if (unlikely(err == -EWOULDBLOCK)) {
  20571. + pr_warn("cannot retry for NFSv4 delegation"
  20572. + " for an internal link\n");
  20573. + iput(delegated);
  20574. + }
  20575. + dput(h_src_dentry);
  20576. + } else {
  20577. + AuIOErr("no dentry found for hi%lu on b%d\n",
  20578. + h_inode->i_ino, a->bdst);
  20579. + err = -EIO;
  20580. + }
  20581. + }
  20582. +
  20583. + if (!err && !plink)
  20584. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  20585. +
  20586. +out:
  20587. + AuTraceErr(err);
  20588. + return err;
  20589. +}
  20590. +
  20591. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  20592. + struct dentry *dentry)
  20593. +{
  20594. + int err, rerr;
  20595. + struct au_dtime dt;
  20596. + struct au_link_args *a;
  20597. + struct dentry *wh_dentry, *h_src_dentry;
  20598. + struct inode *inode, *delegated;
  20599. + struct super_block *sb;
  20600. + struct au_wr_dir_args wr_dir_args = {
  20601. + /* .force_btgt = -1, */
  20602. + .flags = AuWrDir_ADD_ENTRY
  20603. + };
  20604. +
  20605. + IMustLock(dir);
  20606. + inode = d_inode(src_dentry);
  20607. + IMustLock(inode);
  20608. +
  20609. + err = -ENOMEM;
  20610. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20611. + if (unlikely(!a))
  20612. + goto out;
  20613. +
  20614. + a->parent = dentry->d_parent; /* dir inode is locked */
  20615. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  20616. + AuLock_NOPLM | AuLock_GEN);
  20617. + if (unlikely(err))
  20618. + goto out_kfree;
  20619. + err = au_d_linkable(src_dentry);
  20620. + if (unlikely(err))
  20621. + goto out_unlock;
  20622. + err = au_d_may_add(dentry);
  20623. + if (unlikely(err))
  20624. + goto out_unlock;
  20625. +
  20626. + a->src_parent = dget_parent(src_dentry);
  20627. + wr_dir_args.force_btgt = au_ibtop(inode);
  20628. +
  20629. + di_write_lock_parent(a->parent);
  20630. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  20631. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  20632. + &wr_dir_args);
  20633. + err = PTR_ERR(wh_dentry);
  20634. + if (IS_ERR(wh_dentry))
  20635. + goto out_parent;
  20636. +
  20637. + err = 0;
  20638. + sb = dentry->d_sb;
  20639. + a->bdst = au_dbtop(dentry);
  20640. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  20641. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  20642. + a->bsrc = au_ibtop(inode);
  20643. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  20644. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  20645. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  20646. + if (!h_src_dentry) {
  20647. + a->bsrc = au_dbtop(src_dentry);
  20648. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  20649. + AuDebugOn(!h_src_dentry);
  20650. + } else if (IS_ERR(h_src_dentry)) {
  20651. + err = PTR_ERR(h_src_dentry);
  20652. + goto out_parent;
  20653. + }
  20654. +
  20655. + /*
  20656. + * aufs doesn't touch the credential so
  20657. + * security_dentry_create_files_as() is unnecessary.
  20658. + */
  20659. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  20660. + if (a->bdst < a->bsrc
  20661. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  20662. + err = au_cpup_or_link(src_dentry, dentry, a);
  20663. + else {
  20664. + delegated = NULL;
  20665. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  20666. + &a->h_path, &delegated);
  20667. + if (unlikely(err == -EWOULDBLOCK)) {
  20668. + pr_warn("cannot retry for NFSv4 delegation"
  20669. + " for an internal link\n");
  20670. + iput(delegated);
  20671. + }
  20672. + }
  20673. + dput(h_src_dentry);
  20674. + } else {
  20675. + /*
  20676. + * copyup src_dentry to the branch we process,
  20677. + * and then link(2) to it.
  20678. + */
  20679. + dput(h_src_dentry);
  20680. + if (a->bdst < a->bsrc
  20681. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  20682. + au_unpin(&a->pin);
  20683. + di_write_unlock(a->parent);
  20684. + err = au_cpup_before_link(src_dentry, a);
  20685. + di_write_lock_parent(a->parent);
  20686. + if (!err)
  20687. + err = au_pin(&a->pin, dentry, a->bdst,
  20688. + au_opt_udba(sb),
  20689. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20690. + if (unlikely(err))
  20691. + goto out_wh;
  20692. + }
  20693. + if (!err) {
  20694. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  20695. + err = -ENOENT;
  20696. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  20697. + delegated = NULL;
  20698. + err = vfsub_link(h_src_dentry,
  20699. + au_pinned_h_dir(&a->pin),
  20700. + &a->h_path, &delegated);
  20701. + if (unlikely(err == -EWOULDBLOCK)) {
  20702. + pr_warn("cannot retry"
  20703. + " for NFSv4 delegation"
  20704. + " for an internal link\n");
  20705. + iput(delegated);
  20706. + }
  20707. + }
  20708. + }
  20709. + }
  20710. + if (unlikely(err))
  20711. + goto out_unpin;
  20712. +
  20713. + if (wh_dentry) {
  20714. + a->h_path.dentry = wh_dentry;
  20715. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  20716. + dentry);
  20717. + if (unlikely(err))
  20718. + goto out_revert;
  20719. + }
  20720. +
  20721. + au_dir_ts(dir, a->bdst);
  20722. + inode_inc_iversion(dir);
  20723. + vfsub_inc_nlink(inode);
  20724. + inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
  20725. + d_instantiate(dentry, au_igrab(inode));
  20726. + if (d_unhashed(a->h_path.dentry))
  20727. + /* some filesystem calls d_drop() */
  20728. + d_drop(dentry);
  20729. + /* some filesystems consume an inode even hardlink */
  20730. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  20731. + goto out_unpin; /* success */
  20732. +
  20733. +out_revert:
  20734. + /* no delegation since it is just created */
  20735. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  20736. + /*delegated*/NULL, /*force*/0);
  20737. + if (unlikely(rerr)) {
  20738. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  20739. + err = -EIO;
  20740. + }
  20741. + au_dtime_revert(&dt);
  20742. +out_unpin:
  20743. + au_unpin(&a->pin);
  20744. +out_wh:
  20745. + dput(wh_dentry);
  20746. +out_parent:
  20747. + di_write_unlock(a->parent);
  20748. + dput(a->src_parent);
  20749. +out_unlock:
  20750. + if (unlikely(err)) {
  20751. + au_update_dbtop(dentry);
  20752. + d_drop(dentry);
  20753. + }
  20754. + aufs_read_and_write_unlock2(dentry, src_dentry);
  20755. +out_kfree:
  20756. + au_kfree_rcu(a);
  20757. +out:
  20758. + AuTraceErr(err);
  20759. + return err;
  20760. +}
  20761. +
  20762. +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
  20763. + struct dentry *dentry, umode_t mode)
  20764. +{
  20765. + int err, rerr;
  20766. + aufs_bindex_t bindex;
  20767. + unsigned char diropq;
  20768. + struct path h_path;
  20769. + struct dentry *wh_dentry, *parent, *opq_dentry;
  20770. + struct inode *h_inode;
  20771. + struct super_block *sb;
  20772. + struct {
  20773. + struct au_pin pin;
  20774. + struct au_dtime dt;
  20775. + } *a; /* reduce the stack usage */
  20776. + struct au_wr_dir_args wr_dir_args = {
  20777. + .force_btgt = -1,
  20778. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  20779. + };
  20780. +
  20781. + IMustLock(dir);
  20782. +
  20783. + err = -ENOMEM;
  20784. + a = kmalloc(sizeof(*a), GFP_NOFS);
  20785. + if (unlikely(!a))
  20786. + goto out;
  20787. +
  20788. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  20789. + if (unlikely(err))
  20790. + goto out_free;
  20791. + err = au_d_may_add(dentry);
  20792. + if (unlikely(err))
  20793. + goto out_unlock;
  20794. +
  20795. + parent = dentry->d_parent; /* dir inode is locked */
  20796. + di_write_lock_parent(parent);
  20797. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  20798. + &a->pin, &wr_dir_args);
  20799. + err = PTR_ERR(wh_dentry);
  20800. + if (IS_ERR(wh_dentry))
  20801. + goto out_parent;
  20802. +
  20803. + sb = dentry->d_sb;
  20804. + bindex = au_dbtop(dentry);
  20805. + h_path.dentry = au_h_dptr(dentry, bindex);
  20806. + h_path.mnt = au_sbr_mnt(sb, bindex);
  20807. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  20808. + if (unlikely(err))
  20809. + goto out_unpin;
  20810. +
  20811. + /* make the dir opaque */
  20812. + diropq = 0;
  20813. + h_inode = d_inode(h_path.dentry);
  20814. + if (wh_dentry
  20815. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  20816. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20817. + opq_dentry = au_diropq_create(dentry, bindex);
  20818. + inode_unlock(h_inode);
  20819. + err = PTR_ERR(opq_dentry);
  20820. + if (IS_ERR(opq_dentry))
  20821. + goto out_dir;
  20822. + dput(opq_dentry);
  20823. + diropq = 1;
  20824. + }
  20825. +
  20826. + err = epilog(dir, bindex, wh_dentry, dentry);
  20827. + if (!err) {
  20828. + vfsub_inc_nlink(dir);
  20829. + goto out_unpin; /* success */
  20830. + }
  20831. +
  20832. + /* revert */
  20833. + if (diropq) {
  20834. + AuLabel(revert opq);
  20835. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20836. + rerr = au_diropq_remove(dentry, bindex);
  20837. + inode_unlock(h_inode);
  20838. + if (rerr) {
  20839. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  20840. + dentry, err, rerr);
  20841. + err = -EIO;
  20842. + }
  20843. + }
  20844. +
  20845. +out_dir:
  20846. + AuLabel(revert dir);
  20847. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  20848. + if (rerr) {
  20849. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  20850. + dentry, err, rerr);
  20851. + err = -EIO;
  20852. + }
  20853. + au_dtime_revert(&a->dt);
  20854. +out_unpin:
  20855. + au_unpin(&a->pin);
  20856. + dput(wh_dentry);
  20857. +out_parent:
  20858. + di_write_unlock(parent);
  20859. +out_unlock:
  20860. + if (unlikely(err)) {
  20861. + au_update_dbtop(dentry);
  20862. + d_drop(dentry);
  20863. + }
  20864. + aufs_read_unlock(dentry, AuLock_DW);
  20865. +out_free:
  20866. + au_kfree_rcu(a);
  20867. +out:
  20868. + return err;
  20869. +}
  20870. diff -Naur null/fs/aufs/i_op.c linux-6.6.63/fs/aufs/i_op.c
  20871. --- /dev/null
  20872. +++ linux-6.6.63/fs/aufs/i_op.c 2024-12-18 15:12:59.297519563 +0300
  20873. @@ -0,0 +1,1520 @@
  20874. +// SPDX-License-Identifier: GPL-2.0
  20875. +/*
  20876. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  20877. + *
  20878. + * This program is free software; you can redistribute it and/or modify
  20879. + * it under the terms of the GNU General Public License as published by
  20880. + * the Free Software Foundation; either version 2 of the License, or
  20881. + * (at your option) any later version.
  20882. + *
  20883. + * This program is distributed in the hope that it will be useful,
  20884. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20885. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20886. + * GNU General Public License for more details.
  20887. + *
  20888. + * You should have received a copy of the GNU General Public License
  20889. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20890. + */
  20891. +
  20892. +/*
  20893. + * inode operations (except add/del/rename)
  20894. + */
  20895. +
  20896. +#include <linux/device_cgroup.h>
  20897. +#include <linux/filelock.h>
  20898. +#include <linux/fs_stack.h>
  20899. +#include <linux/iversion.h>
  20900. +#include <linux/security.h>
  20901. +#include "aufs.h"
  20902. +
  20903. +static int h_permission(struct inode *h_inode, int mask,
  20904. + struct path *h_path, int brperm)
  20905. +{
  20906. + int err;
  20907. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  20908. + struct mnt_idmap *h_idmap;
  20909. +
  20910. + err = -EPERM;
  20911. + if (write_mask && IS_IMMUTABLE(h_inode))
  20912. + goto out;
  20913. +
  20914. + err = -EACCES;
  20915. + if (((mask & MAY_EXEC)
  20916. + && S_ISREG(h_inode->i_mode)
  20917. + && (path_noexec(h_path)
  20918. + || !(h_inode->i_mode & 0111))))
  20919. + goto out;
  20920. +
  20921. + /*
  20922. + * - skip the lower fs test in the case of write to ro branch.
  20923. + * - nfs dir permission write check is optimized, but a policy for
  20924. + * link/rename requires a real check.
  20925. + * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
  20926. + * in this case, generic_permission() returns -EOPNOTSUPP.
  20927. + */
  20928. + h_idmap = mnt_idmap(h_path->mnt);
  20929. + if ((write_mask && !au_br_writable(brperm))
  20930. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  20931. + && write_mask && !(mask & MAY_READ))
  20932. + || !h_inode->i_op->permission) {
  20933. + /* AuLabel(generic_permission); */
  20934. + /* AuDbg("get_inode_acl %ps\n",
  20935. + h_inode->i_op->get_inode_acl); */
  20936. + err = generic_permission(h_idmap, h_inode, mask);
  20937. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  20938. + err = h_inode->i_op->permission(h_idmap, h_inode,
  20939. + mask);
  20940. + AuTraceErr(err);
  20941. + } else {
  20942. + /* AuLabel(h_inode->permission); */
  20943. + err = h_inode->i_op->permission(h_idmap, h_inode, mask);
  20944. + AuTraceErr(err);
  20945. + }
  20946. +
  20947. + if (!err)
  20948. + err = devcgroup_inode_permission(h_inode, mask);
  20949. + if (!err)
  20950. + err = security_inode_permission(h_inode, mask);
  20951. +
  20952. +out:
  20953. + return err;
  20954. +}
  20955. +
  20956. +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
  20957. + int mask)
  20958. +{
  20959. + int err;
  20960. + aufs_bindex_t bindex, bbot;
  20961. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  20962. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  20963. + struct inode *h_inode;
  20964. + struct super_block *sb;
  20965. + struct au_branch *br;
  20966. +
  20967. + /* todo: support rcu-walk? */
  20968. + if (mask & MAY_NOT_BLOCK)
  20969. + return -ECHILD;
  20970. +
  20971. + sb = inode->i_sb;
  20972. + si_read_lock(sb, AuLock_FLUSH);
  20973. + ii_read_lock_child(inode);
  20974. +#if 0 /* reserved for future use */
  20975. + /*
  20976. + * This test may be rather 'too much' since the test is essentially done
  20977. + * in the aufs_lookup(). Theoretically it is possible that the inode
  20978. + * generation doesn't match to the superblock's here. But it isn't a
  20979. + * big deal I suppose.
  20980. + */
  20981. + err = au_iigen_test(inode, au_sigen(sb));
  20982. + if (unlikely(err))
  20983. + goto out;
  20984. +#endif
  20985. +
  20986. + if (!isdir
  20987. + || write_mask
  20988. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  20989. + err = au_busy_or_stale();
  20990. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  20991. + if (unlikely(!h_inode
  20992. + || (h_inode->i_mode & S_IFMT)
  20993. + != (inode->i_mode & S_IFMT)))
  20994. + goto out;
  20995. +
  20996. + err = 0;
  20997. + bindex = au_ibtop(inode);
  20998. + br = au_sbr(sb, bindex);
  20999. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  21000. + if (write_mask
  21001. + && !err
  21002. + && !special_file(h_inode->i_mode)) {
  21003. + /* test whether the upper writable branch exists */
  21004. + err = -EROFS;
  21005. + for (; bindex >= 0; bindex--)
  21006. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  21007. + err = 0;
  21008. + break;
  21009. + }
  21010. + }
  21011. + goto out;
  21012. + }
  21013. +
  21014. + /* non-write to dir */
  21015. + err = 0;
  21016. + bbot = au_ibbot(inode);
  21017. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  21018. + h_inode = au_h_iptr(inode, bindex);
  21019. + if (h_inode) {
  21020. + err = au_busy_or_stale();
  21021. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  21022. + break;
  21023. +
  21024. + br = au_sbr(sb, bindex);
  21025. + err = h_permission(h_inode, mask, &br->br_path,
  21026. + br->br_perm);
  21027. + }
  21028. + }
  21029. +
  21030. +out:
  21031. + ii_read_unlock(inode);
  21032. + si_read_unlock(sb);
  21033. + return err;
  21034. +}
  21035. +
  21036. +/* ---------------------------------------------------------------------- */
  21037. +
  21038. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  21039. + unsigned int flags)
  21040. +{
  21041. + struct dentry *ret, *parent;
  21042. + struct inode *inode;
  21043. + struct super_block *sb;
  21044. + int err, npositive;
  21045. +
  21046. + IMustLock(dir);
  21047. +
  21048. + /* todo: support rcu-walk? */
  21049. + ret = ERR_PTR(-ECHILD);
  21050. + if (flags & LOOKUP_RCU)
  21051. + goto out;
  21052. +
  21053. + ret = ERR_PTR(-ENAMETOOLONG);
  21054. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  21055. + goto out;
  21056. +
  21057. + sb = dir->i_sb;
  21058. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  21059. + ret = ERR_PTR(err);
  21060. + if (unlikely(err))
  21061. + goto out;
  21062. +
  21063. + err = au_di_init(dentry);
  21064. + ret = ERR_PTR(err);
  21065. + if (unlikely(err))
  21066. + goto out_si;
  21067. +
  21068. + inode = NULL;
  21069. + npositive = 0; /* suppress a warning */
  21070. + parent = dentry->d_parent; /* dir inode is locked */
  21071. + di_read_lock_parent(parent, AuLock_IR);
  21072. + err = au_alive_dir(parent);
  21073. + if (!err)
  21074. + err = au_digen_test(parent, au_sigen(sb));
  21075. + if (!err) {
  21076. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  21077. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  21078. + AuLkup_ALLOW_NEG);
  21079. + err = npositive;
  21080. + }
  21081. + di_read_unlock(parent, AuLock_IR);
  21082. + ret = ERR_PTR(err);
  21083. + if (unlikely(err < 0))
  21084. + goto out_unlock;
  21085. +
  21086. + if (npositive) {
  21087. + inode = au_new_inode(dentry, /*must_new*/0);
  21088. + if (IS_ERR(inode)) {
  21089. + ret = (void *)inode;
  21090. + inode = NULL;
  21091. + goto out_unlock;
  21092. + }
  21093. + }
  21094. +
  21095. + if (inode)
  21096. + atomic_inc(&inode->i_count);
  21097. + ret = d_splice_alias(inode, dentry);
  21098. +#if 0 /* reserved for future use */
  21099. + if (unlikely(d_need_lookup(dentry))) {
  21100. + spin_lock(&dentry->d_lock);
  21101. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  21102. + spin_unlock(&dentry->d_lock);
  21103. + } else
  21104. +#endif
  21105. + if (inode) {
  21106. + if (!IS_ERR(ret)) {
  21107. + iput(inode);
  21108. + if (ret && ret != dentry)
  21109. + ii_write_unlock(inode);
  21110. + } else {
  21111. + ii_write_unlock(inode);
  21112. + iput(inode);
  21113. + inode = NULL;
  21114. + }
  21115. + }
  21116. +
  21117. +out_unlock:
  21118. + di_write_unlock(dentry);
  21119. +out_si:
  21120. + si_read_unlock(sb);
  21121. +out:
  21122. + return ret;
  21123. +}
  21124. +
  21125. +/* ---------------------------------------------------------------------- */
  21126. +
  21127. +/*
  21128. + * very dirty and complicated aufs ->atomic_open().
  21129. + * aufs_atomic_open()
  21130. + * + au_aopen_or_create()
  21131. + * + add_simple()
  21132. + * + vfsub_atomic_open()
  21133. + * + branch fs ->atomic_open()
  21134. + * may call the actual 'open' for h_file
  21135. + * + inc br_nfiles only if opened
  21136. + * + au_aopen_no_open() or au_aopen_do_open()
  21137. + *
  21138. + * au_aopen_do_open()
  21139. + * + finish_open()
  21140. + * + au_do_aopen()
  21141. + * + au_do_open() the body of all 'open'
  21142. + * + au_do_open_nondir()
  21143. + * set the passed h_file
  21144. + *
  21145. + * au_aopen_no_open()
  21146. + * + finish_no_open()
  21147. + */
  21148. +
  21149. +struct aopen_node {
  21150. + struct hlist_bl_node hblist;
  21151. + struct file *file, *h_file;
  21152. +};
  21153. +
  21154. +static int au_do_aopen(struct inode *inode, struct file *file)
  21155. +{
  21156. + struct hlist_bl_head *aopen;
  21157. + struct hlist_bl_node *pos;
  21158. + struct aopen_node *node;
  21159. + struct au_do_open_args args = {
  21160. + .aopen = 1,
  21161. + .open = au_do_open_nondir
  21162. + };
  21163. +
  21164. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  21165. + hlist_bl_lock(aopen);
  21166. + hlist_bl_for_each_entry(node, pos, aopen, hblist)
  21167. + if (node->file == file) {
  21168. + args.h_file = node->h_file;
  21169. + break;
  21170. + }
  21171. + hlist_bl_unlock(aopen);
  21172. + /* AuDebugOn(!args.h_file); */
  21173. +
  21174. + return au_do_open(file, &args);
  21175. +}
  21176. +
  21177. +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
  21178. + struct aopen_node *aopen_node)
  21179. +{
  21180. + int err;
  21181. + struct hlist_bl_head *aopen;
  21182. +
  21183. + AuLabel(here);
  21184. + aopen = &au_sbi(dentry->d_sb)->si_aopen;
  21185. + au_hbl_add(&aopen_node->hblist, aopen);
  21186. + err = finish_open(file, dentry, au_do_aopen);
  21187. + au_hbl_del(&aopen_node->hblist, aopen);
  21188. + /* AuDbgFile(file); */
  21189. + AuDbg("%pd%s%s\n", dentry,
  21190. + (file->f_mode & FMODE_CREATED) ? " created" : "",
  21191. + (file->f_mode & FMODE_OPENED) ? " opened" : "");
  21192. +
  21193. + AuTraceErr(err);
  21194. + return err;
  21195. +}
  21196. +
  21197. +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
  21198. +{
  21199. + int err;
  21200. +
  21201. + AuLabel(here);
  21202. + dget(dentry);
  21203. + err = finish_no_open(file, dentry);
  21204. +
  21205. + AuTraceErr(err);
  21206. + return err;
  21207. +}
  21208. +
  21209. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  21210. + struct file *file, unsigned int open_flag,
  21211. + umode_t create_mode)
  21212. +{
  21213. + int err, did_open;
  21214. + unsigned int lkup_flags;
  21215. + aufs_bindex_t bindex;
  21216. + struct super_block *sb;
  21217. + struct dentry *parent, *d;
  21218. + struct vfsub_aopen_args args = {
  21219. + .open_flag = open_flag,
  21220. + .create_mode = create_mode
  21221. + };
  21222. + struct aopen_node aopen_node = {
  21223. + .file = file
  21224. + };
  21225. +
  21226. + IMustLock(dir);
  21227. + AuDbg("open_flag 0%o\n", open_flag);
  21228. + AuDbgDentry(dentry);
  21229. +
  21230. + err = 0;
  21231. + if (!au_di(dentry)) {
  21232. + lkup_flags = LOOKUP_OPEN;
  21233. + if (open_flag & O_CREAT)
  21234. + lkup_flags |= LOOKUP_CREATE;
  21235. + d = aufs_lookup(dir, dentry, lkup_flags);
  21236. + if (IS_ERR(d)) {
  21237. + err = PTR_ERR(d);
  21238. + AuTraceErr(err);
  21239. + goto out;
  21240. + } else if (d) {
  21241. + /*
  21242. + * obsoleted dentry found.
  21243. + * another error will be returned later.
  21244. + */
  21245. + d_drop(d);
  21246. + AuDbgDentry(d);
  21247. + dput(d);
  21248. + }
  21249. + AuDbgDentry(dentry);
  21250. + }
  21251. +
  21252. + if (d_is_positive(dentry)
  21253. + || d_unhashed(dentry)
  21254. + || d_unlinked(dentry)
  21255. + || !(open_flag & O_CREAT)) {
  21256. + err = au_aopen_no_open(file, dentry);
  21257. + goto out; /* success */
  21258. + }
  21259. +
  21260. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  21261. + if (unlikely(err))
  21262. + goto out;
  21263. +
  21264. + sb = dentry->d_sb;
  21265. + parent = dentry->d_parent; /* dir is locked */
  21266. + di_write_lock_parent(parent);
  21267. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  21268. + if (unlikely(err < 0))
  21269. + goto out_parent;
  21270. +
  21271. + AuDbgDentry(dentry);
  21272. + if (d_is_positive(dentry)) {
  21273. + err = au_aopen_no_open(file, dentry);
  21274. + goto out_parent; /* success */
  21275. + }
  21276. +
  21277. + args.file = alloc_empty_file(file->f_flags, current_cred());
  21278. + err = PTR_ERR(args.file);
  21279. + if (IS_ERR(args.file))
  21280. + goto out_parent;
  21281. +
  21282. + bindex = au_dbtop(dentry);
  21283. + err = au_aopen_or_create(dir, dentry, &args);
  21284. + AuTraceErr(err);
  21285. + AuDbgFile(args.file);
  21286. + file->f_mode = args.file->f_mode & ~FMODE_OPENED;
  21287. + did_open = !!(args.file->f_mode & FMODE_OPENED);
  21288. + if (!did_open) {
  21289. + fput(args.file);
  21290. + args.file = NULL;
  21291. + }
  21292. + di_write_unlock(parent);
  21293. + di_write_unlock(dentry);
  21294. + if (unlikely(err < 0)) {
  21295. + if (args.file)
  21296. + fput(args.file);
  21297. + goto out_sb;
  21298. + }
  21299. +
  21300. + if (!did_open)
  21301. + err = au_aopen_no_open(file, dentry);
  21302. + else {
  21303. + aopen_node.h_file = args.file;
  21304. + err = au_aopen_do_open(file, dentry, &aopen_node);
  21305. + }
  21306. + if (unlikely(err < 0)) {
  21307. + if (args.file)
  21308. + fput(args.file);
  21309. + if (did_open)
  21310. + au_lcnt_dec(&args.br->br_nfiles);
  21311. + }
  21312. + goto out_sb; /* success */
  21313. +
  21314. +out_parent:
  21315. + di_write_unlock(parent);
  21316. + di_write_unlock(dentry);
  21317. +out_sb:
  21318. + si_read_unlock(sb);
  21319. +out:
  21320. + AuTraceErr(err);
  21321. + AuDbgFile(file);
  21322. + return err;
  21323. +}
  21324. +
  21325. +
  21326. +/* ---------------------------------------------------------------------- */
  21327. +
  21328. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  21329. + const unsigned char add_entry, aufs_bindex_t bcpup,
  21330. + aufs_bindex_t btop)
  21331. +{
  21332. + int err;
  21333. + struct dentry *h_parent;
  21334. + struct inode *h_dir;
  21335. +
  21336. + if (add_entry)
  21337. + IMustLock(d_inode(parent));
  21338. + else
  21339. + di_write_lock_parent(parent);
  21340. +
  21341. + err = 0;
  21342. + if (!au_h_dptr(parent, bcpup)) {
  21343. + if (btop > bcpup)
  21344. + err = au_cpup_dirs(dentry, bcpup);
  21345. + else if (btop < bcpup)
  21346. + err = au_cpdown_dirs(dentry, bcpup);
  21347. + else
  21348. + BUG();
  21349. + }
  21350. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  21351. + h_parent = au_h_dptr(parent, bcpup);
  21352. + h_dir = d_inode(h_parent);
  21353. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  21354. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  21355. + /* todo: no unlock here */
  21356. + inode_unlock_shared(h_dir);
  21357. +
  21358. + AuDbg("bcpup %d\n", bcpup);
  21359. + if (!err) {
  21360. + if (d_really_is_negative(dentry))
  21361. + au_set_h_dptr(dentry, btop, NULL);
  21362. + au_update_dbrange(dentry, /*do_put_zero*/0);
  21363. + }
  21364. + }
  21365. +
  21366. + if (!add_entry)
  21367. + di_write_unlock(parent);
  21368. + if (!err)
  21369. + err = bcpup; /* success */
  21370. +
  21371. + AuTraceErr(err);
  21372. + return err;
  21373. +}
  21374. +
  21375. +/*
  21376. + * decide the branch and the parent dir where we will create a new entry.
  21377. + * returns new bindex or an error.
  21378. + * copyup the parent dir if needed.
  21379. + */
  21380. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  21381. + struct au_wr_dir_args *args)
  21382. +{
  21383. + int err;
  21384. + unsigned int flags;
  21385. + aufs_bindex_t bcpup, btop, src_btop;
  21386. + const unsigned char add_entry
  21387. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  21388. + | au_ftest_wrdir(args->flags, TMPFILE);
  21389. + struct super_block *sb;
  21390. + struct dentry *parent;
  21391. + struct au_sbinfo *sbinfo;
  21392. +
  21393. + sb = dentry->d_sb;
  21394. + sbinfo = au_sbi(sb);
  21395. + parent = dget_parent(dentry);
  21396. + btop = au_dbtop(dentry);
  21397. + bcpup = btop;
  21398. + if (args->force_btgt < 0) {
  21399. + if (src_dentry) {
  21400. + src_btop = au_dbtop(src_dentry);
  21401. + if (src_btop < btop)
  21402. + bcpup = src_btop;
  21403. + } else if (add_entry) {
  21404. + flags = 0;
  21405. + if (au_ftest_wrdir(args->flags, ISDIR))
  21406. + au_fset_wbr(flags, DIR);
  21407. + err = AuWbrCreate(sbinfo, dentry, flags);
  21408. + bcpup = err;
  21409. + }
  21410. +
  21411. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  21412. + if (add_entry)
  21413. + err = AuWbrCopyup(sbinfo, dentry);
  21414. + else {
  21415. + if (!IS_ROOT(dentry)) {
  21416. + di_read_lock_parent(parent, !AuLock_IR);
  21417. + err = AuWbrCopyup(sbinfo, dentry);
  21418. + di_read_unlock(parent, !AuLock_IR);
  21419. + } else
  21420. + err = AuWbrCopyup(sbinfo, dentry);
  21421. + }
  21422. + bcpup = err;
  21423. + if (unlikely(err < 0))
  21424. + goto out;
  21425. + }
  21426. + } else {
  21427. + bcpup = args->force_btgt;
  21428. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  21429. + }
  21430. +
  21431. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  21432. + err = bcpup;
  21433. + if (bcpup == btop)
  21434. + goto out; /* success */
  21435. +
  21436. + /* copyup the new parent into the branch we process */
  21437. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  21438. + if (err >= 0) {
  21439. + if (d_really_is_negative(dentry)) {
  21440. + au_set_h_dptr(dentry, btop, NULL);
  21441. + au_set_dbtop(dentry, bcpup);
  21442. + au_set_dbbot(dentry, bcpup);
  21443. + }
  21444. + AuDebugOn(add_entry
  21445. + && !au_ftest_wrdir(args->flags, TMPFILE)
  21446. + && !au_h_dptr(dentry, bcpup));
  21447. + }
  21448. +
  21449. +out:
  21450. + dput(parent);
  21451. + return err;
  21452. +}
  21453. +
  21454. +/* ---------------------------------------------------------------------- */
  21455. +
  21456. +void au_pin_hdir_unlock(struct au_pin *p)
  21457. +{
  21458. + if (p->hdir)
  21459. + au_hn_inode_unlock(p->hdir);
  21460. +}
  21461. +
  21462. +int au_pin_hdir_lock(struct au_pin *p)
  21463. +{
  21464. + int err;
  21465. +
  21466. + err = 0;
  21467. + if (!p->hdir)
  21468. + goto out;
  21469. +
  21470. + /* even if an error happens later, keep this lock */
  21471. + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
  21472. +
  21473. + err = -EBUSY;
  21474. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  21475. + goto out;
  21476. +
  21477. + err = 0;
  21478. + if (p->h_dentry)
  21479. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  21480. + p->h_parent, p->br);
  21481. +
  21482. +out:
  21483. + return err;
  21484. +}
  21485. +
  21486. +int au_pin_hdir_relock(struct au_pin *p)
  21487. +{
  21488. + int err, i;
  21489. + struct inode *h_i;
  21490. + struct dentry *h_d[] = {
  21491. + p->h_dentry,
  21492. + p->h_parent
  21493. + };
  21494. +
  21495. + err = au_pin_hdir_lock(p);
  21496. + if (unlikely(err))
  21497. + goto out;
  21498. +
  21499. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  21500. + if (!h_d[i])
  21501. + continue;
  21502. + if (d_is_positive(h_d[i])) {
  21503. + h_i = d_inode(h_d[i]);
  21504. + err = !vfsub_inode_nlink(h_i, AU_I_BRANCH);
  21505. + }
  21506. + }
  21507. +
  21508. +out:
  21509. + return err;
  21510. +}
  21511. +
  21512. +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  21513. +{
  21514. + atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
  21515. +}
  21516. +
  21517. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  21518. +{
  21519. + if (p->hdir) {
  21520. + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
  21521. + p->lsc_hi, 0, NULL, _RET_IP_);
  21522. + au_pin_hdir_set_owner(p, current);
  21523. + }
  21524. +}
  21525. +
  21526. +void au_pin_hdir_release(struct au_pin *p)
  21527. +{
  21528. + if (p->hdir) {
  21529. + au_pin_hdir_set_owner(p, p->task);
  21530. + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
  21531. + }
  21532. +}
  21533. +
  21534. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  21535. +{
  21536. + if (pin && pin->parent)
  21537. + return au_h_dptr(pin->parent, pin->bindex);
  21538. + return NULL;
  21539. +}
  21540. +
  21541. +void au_unpin(struct au_pin *p)
  21542. +{
  21543. + if (p->hdir)
  21544. + au_pin_hdir_unlock(p);
  21545. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  21546. + vfsub_mnt_drop_write(p->h_mnt);
  21547. + if (!p->hdir)
  21548. + return;
  21549. +
  21550. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21551. + di_read_unlock(p->parent, AuLock_IR);
  21552. + iput(p->hdir->hi_inode);
  21553. + dput(p->parent);
  21554. + p->parent = NULL;
  21555. + p->hdir = NULL;
  21556. + p->h_mnt = NULL;
  21557. + /* do not clear p->task */
  21558. +}
  21559. +
  21560. +int au_do_pin(struct au_pin *p)
  21561. +{
  21562. + int err;
  21563. + struct super_block *sb;
  21564. + struct inode *h_dir;
  21565. +
  21566. + err = 0;
  21567. + sb = p->dentry->d_sb;
  21568. + p->br = au_sbr(sb, p->bindex);
  21569. + if (IS_ROOT(p->dentry)) {
  21570. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  21571. + p->h_mnt = au_br_mnt(p->br);
  21572. + err = vfsub_mnt_want_write(p->h_mnt);
  21573. + if (unlikely(err)) {
  21574. + au_fclr_pin(p->flags, MNT_WRITE);
  21575. + goto out_err;
  21576. + }
  21577. + }
  21578. + goto out;
  21579. + }
  21580. +
  21581. + p->h_dentry = NULL;
  21582. + if (p->bindex <= au_dbbot(p->dentry))
  21583. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  21584. +
  21585. + p->parent = dget_parent(p->dentry);
  21586. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21587. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  21588. +
  21589. + h_dir = NULL;
  21590. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  21591. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  21592. + if (p->hdir)
  21593. + h_dir = p->hdir->hi_inode;
  21594. +
  21595. + /*
  21596. + * udba case, or
  21597. + * if DI_LOCKED is not set, then p->parent may be different
  21598. + * and h_parent can be NULL.
  21599. + */
  21600. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  21601. + err = -EBUSY;
  21602. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21603. + di_read_unlock(p->parent, AuLock_IR);
  21604. + dput(p->parent);
  21605. + p->parent = NULL;
  21606. + goto out_err;
  21607. + }
  21608. +
  21609. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  21610. + p->h_mnt = au_br_mnt(p->br);
  21611. + err = vfsub_mnt_want_write(p->h_mnt);
  21612. + if (unlikely(err)) {
  21613. + au_fclr_pin(p->flags, MNT_WRITE);
  21614. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21615. + di_read_unlock(p->parent, AuLock_IR);
  21616. + dput(p->parent);
  21617. + p->parent = NULL;
  21618. + goto out_err;
  21619. + }
  21620. + }
  21621. +
  21622. + au_igrab(h_dir);
  21623. + err = au_pin_hdir_lock(p);
  21624. + if (!err)
  21625. + goto out; /* success */
  21626. +
  21627. + au_unpin(p);
  21628. +
  21629. +out_err:
  21630. + pr_err("err %d\n", err);
  21631. + err = au_busy_or_stale();
  21632. +out:
  21633. + return err;
  21634. +}
  21635. +
  21636. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  21637. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  21638. + unsigned int udba, unsigned char flags)
  21639. +{
  21640. + p->dentry = dentry;
  21641. + p->udba = udba;
  21642. + p->lsc_di = lsc_di;
  21643. + p->lsc_hi = lsc_hi;
  21644. + p->flags = flags;
  21645. + p->bindex = bindex;
  21646. +
  21647. + p->parent = NULL;
  21648. + p->hdir = NULL;
  21649. + p->h_mnt = NULL;
  21650. +
  21651. + p->h_dentry = NULL;
  21652. + p->h_parent = NULL;
  21653. + p->br = NULL;
  21654. + p->task = current;
  21655. +}
  21656. +
  21657. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  21658. + unsigned int udba, unsigned char flags)
  21659. +{
  21660. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  21661. + udba, flags);
  21662. + return au_do_pin(pin);
  21663. +}
  21664. +
  21665. +/* ---------------------------------------------------------------------- */
  21666. +
  21667. +/*
  21668. + * ->setattr() and ->getattr() are called in various cases.
  21669. + * chmod, stat: dentry is revalidated.
  21670. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  21671. + * unhashed.
  21672. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  21673. + */
  21674. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  21675. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  21676. +{
  21677. + int err;
  21678. + struct dentry *parent;
  21679. +
  21680. + err = 0;
  21681. + if (au_digen_test(dentry, sigen)) {
  21682. + parent = dget_parent(dentry);
  21683. + di_read_lock_parent(parent, AuLock_IR);
  21684. + err = au_refresh_dentry(dentry, parent);
  21685. + di_read_unlock(parent, AuLock_IR);
  21686. + dput(parent);
  21687. + }
  21688. +
  21689. + AuTraceErr(err);
  21690. + return err;
  21691. +}
  21692. +
  21693. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  21694. + struct au_icpup_args *a)
  21695. +{
  21696. + int err;
  21697. + loff_t sz;
  21698. + aufs_bindex_t btop, ibtop;
  21699. + struct dentry *hi_wh, *parent;
  21700. + struct inode *inode;
  21701. + struct au_wr_dir_args wr_dir_args = {
  21702. + .force_btgt = -1,
  21703. + .flags = 0
  21704. + };
  21705. +
  21706. + if (d_is_dir(dentry))
  21707. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  21708. + /* plink or hi_wh() case */
  21709. + btop = au_dbtop(dentry);
  21710. + inode = d_inode(dentry);
  21711. + ibtop = au_ibtop(inode);
  21712. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  21713. + wr_dir_args.force_btgt = ibtop;
  21714. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  21715. + if (unlikely(err < 0))
  21716. + goto out;
  21717. + a->btgt = err;
  21718. + if (err != btop)
  21719. + au_fset_icpup(a->flags, DID_CPUP);
  21720. +
  21721. + err = 0;
  21722. + a->pin_flags = AuPin_MNT_WRITE;
  21723. + parent = NULL;
  21724. + if (!IS_ROOT(dentry)) {
  21725. + au_fset_pin(a->pin_flags, DI_LOCKED);
  21726. + parent = dget_parent(dentry);
  21727. + di_write_lock_parent(parent);
  21728. + }
  21729. +
  21730. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  21731. + if (unlikely(err))
  21732. + goto out_parent;
  21733. +
  21734. + sz = -1;
  21735. + a->h_path.dentry = au_h_dptr(dentry, btop);
  21736. + a->h_inode = d_inode(a->h_path.dentry);
  21737. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  21738. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  21739. + if (ia->ia_size < i_size_read(a->h_inode))
  21740. + sz = ia->ia_size;
  21741. + inode_unlock_shared(a->h_inode);
  21742. + }
  21743. +
  21744. + hi_wh = NULL;
  21745. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  21746. + hi_wh = au_hi_wh(inode, a->btgt);
  21747. + if (!hi_wh) {
  21748. + struct au_cp_generic cpg = {
  21749. + .dentry = dentry,
  21750. + .bdst = a->btgt,
  21751. + .bsrc = -1,
  21752. + .len = sz,
  21753. + .pin = &a->pin
  21754. + };
  21755. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  21756. + if (unlikely(err))
  21757. + goto out_unlock;
  21758. + hi_wh = au_hi_wh(inode, a->btgt);
  21759. + /* todo: revalidate hi_wh? */
  21760. + }
  21761. + }
  21762. +
  21763. + if (parent) {
  21764. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  21765. + di_downgrade_lock(parent, AuLock_IR);
  21766. + dput(parent);
  21767. + parent = NULL;
  21768. + }
  21769. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  21770. + goto out; /* success */
  21771. +
  21772. + if (!d_unhashed(dentry)) {
  21773. + struct au_cp_generic cpg = {
  21774. + .dentry = dentry,
  21775. + .bdst = a->btgt,
  21776. + .bsrc = btop,
  21777. + .len = sz,
  21778. + .pin = &a->pin,
  21779. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  21780. + };
  21781. + err = au_sio_cpup_simple(&cpg);
  21782. + if (!err)
  21783. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  21784. + } else if (!hi_wh)
  21785. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  21786. + else
  21787. + a->h_path.dentry = hi_wh; /* do not dget here */
  21788. +
  21789. +out_unlock:
  21790. + a->h_inode = d_inode(a->h_path.dentry);
  21791. + if (!err)
  21792. + goto out; /* success */
  21793. + au_unpin(&a->pin);
  21794. +out_parent:
  21795. + if (parent) {
  21796. + di_write_unlock(parent);
  21797. + dput(parent);
  21798. + }
  21799. +out:
  21800. + if (!err)
  21801. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  21802. + return err;
  21803. +}
  21804. +
  21805. +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  21806. + struct iattr *ia)
  21807. +{
  21808. + int err;
  21809. + struct inode *inode, *delegated;
  21810. + struct super_block *sb;
  21811. + struct file *file;
  21812. + struct au_icpup_args *a;
  21813. + struct mnt_idmap *h_idmap;
  21814. +
  21815. + inode = d_inode(dentry);
  21816. + IMustLock(inode);
  21817. +
  21818. + err = setattr_prepare(idmap, dentry, ia);
  21819. + if (unlikely(err))
  21820. + goto out;
  21821. +
  21822. + err = -ENOMEM;
  21823. + a = kzalloc(sizeof(*a), GFP_NOFS);
  21824. + if (unlikely(!a))
  21825. + goto out;
  21826. +
  21827. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  21828. + ia->ia_valid &= ~ATTR_MODE;
  21829. +
  21830. + file = NULL;
  21831. + sb = dentry->d_sb;
  21832. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  21833. + if (unlikely(err))
  21834. + goto out_kfree;
  21835. +
  21836. + if (ia->ia_valid & ATTR_FILE) {
  21837. + /* currently ftruncate(2) only */
  21838. + AuDebugOn(!d_is_reg(dentry));
  21839. + file = ia->ia_file;
  21840. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
  21841. + /*fi_lsc*/0);
  21842. + if (unlikely(err))
  21843. + goto out_si;
  21844. + ia->ia_file = au_hf_top(file);
  21845. + a->udba = AuOpt_UDBA_NONE;
  21846. + } else {
  21847. + /* fchmod() doesn't pass ia_file */
  21848. + a->udba = au_opt_udba(sb);
  21849. + di_write_lock_child(dentry);
  21850. + /* no d_unlinked(), to set UDBA_NONE for root */
  21851. + if (d_unhashed(dentry))
  21852. + a->udba = AuOpt_UDBA_NONE;
  21853. + if (a->udba != AuOpt_UDBA_NONE) {
  21854. + AuDebugOn(IS_ROOT(dentry));
  21855. + err = au_reval_for_attr(dentry, au_sigen(sb));
  21856. + if (unlikely(err))
  21857. + goto out_dentry;
  21858. + }
  21859. + }
  21860. +
  21861. + err = au_pin_and_icpup(dentry, ia, a);
  21862. + if (unlikely(err < 0))
  21863. + goto out_dentry;
  21864. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  21865. + ia->ia_file = NULL;
  21866. + ia->ia_valid &= ~ATTR_FILE;
  21867. + }
  21868. +
  21869. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  21870. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  21871. + == (ATTR_MODE | ATTR_CTIME)) {
  21872. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  21873. + if (unlikely(err))
  21874. + goto out_unlock;
  21875. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  21876. + && (ia->ia_valid & ATTR_CTIME)) {
  21877. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  21878. + if (unlikely(err))
  21879. + goto out_unlock;
  21880. + }
  21881. +
  21882. + if (ia->ia_valid & ATTR_SIZE) {
  21883. + struct file *f;
  21884. +
  21885. + if (ia->ia_size < i_size_read(inode))
  21886. + /* unmap only */
  21887. + truncate_setsize(inode, ia->ia_size);
  21888. +
  21889. + f = NULL;
  21890. + if (ia->ia_valid & ATTR_FILE)
  21891. + f = ia->ia_file;
  21892. + inode_unlock(a->h_inode);
  21893. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  21894. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  21895. + } else {
  21896. + delegated = NULL;
  21897. + while (1) {
  21898. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  21899. + if (delegated) {
  21900. + err = break_deleg_wait(&delegated);
  21901. + if (!err)
  21902. + continue;
  21903. + }
  21904. + break;
  21905. + }
  21906. + }
  21907. + /*
  21908. + * regardless aufs 'acl' option setting.
  21909. + * why don't all acl-aware fs call this func from their ->setattr()?
  21910. + */
  21911. + if (!err && (ia->ia_valid & ATTR_MODE)) {
  21912. + h_idmap = mnt_idmap(a->h_path.mnt);
  21913. + err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
  21914. + }
  21915. + if (!err)
  21916. + au_cpup_attr_changeable(inode);
  21917. +
  21918. +out_unlock:
  21919. + inode_unlock(a->h_inode);
  21920. + au_unpin(&a->pin);
  21921. + if (unlikely(err))
  21922. + au_update_dbtop(dentry);
  21923. +out_dentry:
  21924. + di_write_unlock(dentry);
  21925. + if (file) {
  21926. + fi_write_unlock(file);
  21927. + ia->ia_file = file;
  21928. + ia->ia_valid |= ATTR_FILE;
  21929. + }
  21930. +out_si:
  21931. + si_read_unlock(sb);
  21932. +out_kfree:
  21933. + au_kfree_rcu(a);
  21934. +out:
  21935. + AuTraceErr(err);
  21936. + return err;
  21937. +}
  21938. +
  21939. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  21940. +static int au_h_path_to_set_attr(struct dentry *dentry,
  21941. + struct au_icpup_args *a, struct path *h_path)
  21942. +{
  21943. + int err;
  21944. + struct super_block *sb;
  21945. +
  21946. + sb = dentry->d_sb;
  21947. + a->udba = au_opt_udba(sb);
  21948. + /* no d_unlinked(), to set UDBA_NONE for root */
  21949. + if (d_unhashed(dentry))
  21950. + a->udba = AuOpt_UDBA_NONE;
  21951. + if (a->udba != AuOpt_UDBA_NONE) {
  21952. + AuDebugOn(IS_ROOT(dentry));
  21953. + err = au_reval_for_attr(dentry, au_sigen(sb));
  21954. + if (unlikely(err))
  21955. + goto out;
  21956. + }
  21957. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  21958. + if (unlikely(err < 0))
  21959. + goto out;
  21960. +
  21961. + h_path->dentry = a->h_path.dentry;
  21962. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  21963. +
  21964. +out:
  21965. + return err;
  21966. +}
  21967. +
  21968. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  21969. + struct au_sxattr *arg)
  21970. +{
  21971. + int err;
  21972. + struct path h_path;
  21973. + struct super_block *sb;
  21974. + struct au_icpup_args *a;
  21975. + struct inode *h_inode;
  21976. + struct mnt_idmap *h_idmap;
  21977. +
  21978. + IMustLock(inode);
  21979. +
  21980. + err = -ENOMEM;
  21981. + a = kzalloc(sizeof(*a), GFP_NOFS);
  21982. + if (unlikely(!a))
  21983. + goto out;
  21984. +
  21985. + sb = dentry->d_sb;
  21986. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  21987. + if (unlikely(err))
  21988. + goto out_kfree;
  21989. +
  21990. + h_path.dentry = NULL; /* silence gcc */
  21991. + di_write_lock_child(dentry);
  21992. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  21993. + if (unlikely(err))
  21994. + goto out_di;
  21995. + h_idmap = mnt_idmap(h_path.mnt);
  21996. +
  21997. + inode_unlock(a->h_inode);
  21998. + switch (arg->type) {
  21999. + case AU_XATTR_SET:
  22000. + AuDebugOn(d_is_negative(h_path.dentry));
  22001. + err = vfsub_setxattr(h_idmap, h_path.dentry,
  22002. + arg->u.set.name, arg->u.set.value,
  22003. + arg->u.set.size, arg->u.set.flags);
  22004. + break;
  22005. + case AU_ACL_SET:
  22006. + err = -EOPNOTSUPP;
  22007. + h_inode = d_inode(h_path.dentry);
  22008. + if (h_inode->i_op->set_acl) {
  22009. + /* this will call posix_acl_update_mode */
  22010. + err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
  22011. + arg->u.acl_set.acl,
  22012. + arg->u.acl_set.type);
  22013. + }
  22014. + break;
  22015. + }
  22016. + if (!err)
  22017. + au_cpup_attr_timesizes(inode);
  22018. +
  22019. + au_unpin(&a->pin);
  22020. + if (unlikely(err))
  22021. + au_update_dbtop(dentry);
  22022. +
  22023. +out_di:
  22024. + di_write_unlock(dentry);
  22025. + si_read_unlock(sb);
  22026. +out_kfree:
  22027. + au_kfree_rcu(a);
  22028. +out:
  22029. + AuTraceErr(err);
  22030. + return err;
  22031. +}
  22032. +#endif
  22033. +
  22034. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  22035. + unsigned int nlink)
  22036. +{
  22037. + unsigned int n;
  22038. +
  22039. + inode->i_mode = st->mode;
  22040. + /* don't i_[ug]id_write() here */
  22041. + inode->i_uid = st->uid;
  22042. + inode->i_gid = st->gid;
  22043. + inode->i_atime = st->atime;
  22044. + inode->i_mtime = st->mtime;
  22045. + inode_set_ctime_to_ts(inode, st->ctime);
  22046. +
  22047. + au_cpup_attr_nlink(inode, /*force*/0);
  22048. + if (S_ISDIR(inode->i_mode)) {
  22049. + n = vfsub_inode_nlink(inode, AU_I_AUFS);
  22050. + n -= nlink;
  22051. + n += st->nlink;
  22052. + /* 0 can happen */
  22053. + vfsub_set_nlink(inode, n);
  22054. + }
  22055. +
  22056. + spin_lock(&inode->i_lock);
  22057. + inode->i_blocks = st->blocks;
  22058. + i_size_write(inode, st->size);
  22059. + spin_unlock(&inode->i_lock);
  22060. +}
  22061. +
  22062. +/*
  22063. + * common routine for aufs_getattr() and au_getxattr().
  22064. + * returns zero or negative (an error).
  22065. + * @dentry will be read-locked in success.
  22066. + */
  22067. +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
  22068. + struct path *h_path, int locked)
  22069. +{
  22070. + int err;
  22071. + unsigned int mnt_flags, sigen;
  22072. + unsigned char udba_none;
  22073. + aufs_bindex_t bindex;
  22074. + struct super_block *sb, *h_sb;
  22075. +
  22076. + h_path->mnt = NULL;
  22077. + h_path->dentry = NULL;
  22078. +
  22079. + err = 0;
  22080. + sb = dentry->d_sb;
  22081. + mnt_flags = au_mntflags(sb);
  22082. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  22083. +
  22084. + if (unlikely(locked))
  22085. + goto body; /* skip locking dinfo */
  22086. +
  22087. + /* support fstat(2) */
  22088. + if (!d_unlinked(dentry) && !udba_none) {
  22089. + sigen = au_sigen(sb);
  22090. + err = au_digen_test(dentry, sigen);
  22091. + if (!err) {
  22092. + di_read_lock_child(dentry, AuLock_IR);
  22093. + err = au_dbrange_test(dentry);
  22094. + if (unlikely(err)) {
  22095. + di_read_unlock(dentry, AuLock_IR);
  22096. + goto out;
  22097. + }
  22098. + } else {
  22099. + AuDebugOn(IS_ROOT(dentry));
  22100. + di_write_lock_child(dentry);
  22101. + err = au_dbrange_test(dentry);
  22102. + if (!err)
  22103. + err = au_reval_for_attr(dentry, sigen);
  22104. + if (!err)
  22105. + di_downgrade_lock(dentry, AuLock_IR);
  22106. + else {
  22107. + di_write_unlock(dentry);
  22108. + goto out;
  22109. + }
  22110. + }
  22111. + } else
  22112. + di_read_lock_child(dentry, AuLock_IR);
  22113. +
  22114. +body:
  22115. + if (!inode) {
  22116. + inode = d_inode(dentry);
  22117. + if (unlikely(!inode))
  22118. + goto out;
  22119. + }
  22120. + bindex = au_ibtop(inode);
  22121. + h_path->mnt = au_sbr_mnt(sb, bindex);
  22122. + h_sb = h_path->mnt->mnt_sb;
  22123. + if (!force
  22124. + && !au_test_fs_bad_iattr(h_sb)
  22125. + && udba_none)
  22126. + goto out; /* success */
  22127. +
  22128. + if (au_dbtop(dentry) == bindex)
  22129. + h_path->dentry = au_h_dptr(dentry, bindex);
  22130. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  22131. + h_path->dentry = au_plink_lkup(inode, bindex);
  22132. + if (IS_ERR(h_path->dentry))
  22133. + /* pretending success */
  22134. + h_path->dentry = NULL;
  22135. + else
  22136. + dput(h_path->dentry);
  22137. + }
  22138. +
  22139. +out:
  22140. + return err;
  22141. +}
  22142. +
  22143. +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
  22144. + struct kstat *st, u32 request, unsigned int query)
  22145. +{
  22146. + int err;
  22147. + unsigned char positive;
  22148. + struct path h_path;
  22149. + struct dentry *dentry;
  22150. + struct inode *inode;
  22151. + struct super_block *sb;
  22152. +
  22153. + dentry = path->dentry;
  22154. + inode = d_inode(dentry);
  22155. + sb = dentry->d_sb;
  22156. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  22157. + if (unlikely(err))
  22158. + goto out;
  22159. + err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
  22160. + /*locked*/0);
  22161. + if (unlikely(err))
  22162. + goto out_si;
  22163. + if (unlikely(!h_path.dentry))
  22164. + /* illegally overlapped or something */
  22165. + goto out_fill; /* pretending success */
  22166. +
  22167. + positive = d_is_positive(h_path.dentry);
  22168. + if (positive) {
  22169. + /* no vfsub version */
  22170. + if (query & AT_GETATTR_NOSEC)
  22171. + err = vfs_getattr_nosec(&h_path, st, request, query);
  22172. + else
  22173. + err = vfs_getattr(&h_path, st, request, query);
  22174. + }
  22175. + if (!err) {
  22176. + if (positive)
  22177. + au_refresh_iattr(inode, st,
  22178. + vfsub_inode_nlink(d_inode(h_path.dentry),
  22179. + AU_I_BRANCH));
  22180. + goto out_fill; /* success */
  22181. + }
  22182. + AuTraceErr(err);
  22183. + goto out_di;
  22184. +
  22185. +out_fill:
  22186. + generic_fillattr(idmap, request, inode, st);
  22187. +out_di:
  22188. + di_read_unlock(dentry, AuLock_IR);
  22189. +out_si:
  22190. + si_read_unlock(sb);
  22191. +out:
  22192. + AuTraceErr(err);
  22193. + return err;
  22194. +}
  22195. +
  22196. +/* ---------------------------------------------------------------------- */
  22197. +
  22198. +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
  22199. + struct delayed_call *done)
  22200. +{
  22201. + const char *ret;
  22202. + struct dentry *h_dentry;
  22203. + struct inode *h_inode;
  22204. + int err;
  22205. + aufs_bindex_t bindex;
  22206. +
  22207. + ret = NULL; /* suppress a warning */
  22208. + err = -ECHILD;
  22209. + if (!dentry)
  22210. + goto out;
  22211. +
  22212. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  22213. + if (unlikely(err))
  22214. + goto out;
  22215. +
  22216. + err = au_d_hashed_positive(dentry);
  22217. + if (unlikely(err))
  22218. + goto out_unlock;
  22219. +
  22220. + err = -EINVAL;
  22221. + inode = d_inode(dentry);
  22222. + bindex = au_ibtop(inode);
  22223. + h_inode = au_h_iptr(inode, bindex);
  22224. + if (unlikely(!h_inode->i_op->get_link))
  22225. + goto out_unlock;
  22226. +
  22227. + err = -EBUSY;
  22228. + h_dentry = NULL;
  22229. + if (au_dbtop(dentry) <= bindex) {
  22230. + h_dentry = au_h_dptr(dentry, bindex);
  22231. + if (h_dentry)
  22232. + dget(h_dentry);
  22233. + }
  22234. + if (!h_dentry) {
  22235. + h_dentry = d_find_any_alias(h_inode);
  22236. + if (IS_ERR(h_dentry)) {
  22237. + err = PTR_ERR(h_dentry);
  22238. + goto out_unlock;
  22239. + }
  22240. + }
  22241. + if (unlikely(!h_dentry))
  22242. + goto out_unlock;
  22243. +
  22244. + err = 0;
  22245. + AuDbg("%ps\n", h_inode->i_op->get_link);
  22246. + AuDbgDentry(h_dentry);
  22247. + ret = vfs_get_link(h_dentry, done);
  22248. + dput(h_dentry);
  22249. + if (IS_ERR(ret))
  22250. + err = PTR_ERR(ret);
  22251. +
  22252. +out_unlock:
  22253. + aufs_read_unlock(dentry, AuLock_IR);
  22254. +out:
  22255. + if (unlikely(err))
  22256. + ret = ERR_PTR(err);
  22257. + AuTraceErrPtr(ret);
  22258. + return ret;
  22259. +}
  22260. +
  22261. +/* ---------------------------------------------------------------------- */
  22262. +
  22263. +static int au_is_special(struct inode *inode)
  22264. +{
  22265. + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
  22266. +}
  22267. +
  22268. +static int aufs_update_time(struct inode *inode, int flags)
  22269. +{
  22270. + int err;
  22271. + aufs_bindex_t bindex;
  22272. + struct super_block *sb;
  22273. + struct inode *h_inode;
  22274. + struct vfsmount *h_mnt;
  22275. +
  22276. + sb = inode->i_sb;
  22277. + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
  22278. + "unexpected s_flags 0x%lx", sb->s_flags);
  22279. +
  22280. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  22281. + lockdep_off();
  22282. + si_read_lock(sb, AuLock_FLUSH);
  22283. + ii_write_lock_child(inode);
  22284. +
  22285. + err = 0;
  22286. + bindex = au_ibtop(inode);
  22287. + h_inode = au_h_iptr(inode, bindex);
  22288. + if (!au_test_ro(sb, bindex, inode)) {
  22289. + h_mnt = au_sbr_mnt(sb, bindex);
  22290. + err = vfsub_mnt_want_write(h_mnt);
  22291. + if (!err) {
  22292. + err = vfsub_update_time(h_inode, flags);
  22293. + vfsub_mnt_drop_write(h_mnt);
  22294. + }
  22295. + } else if (au_is_special(h_inode)) {
  22296. + /*
  22297. + * Never copy-up here.
  22298. + * These special files may already be opened and used for
  22299. + * communicating. If we copied it up, then the communication
  22300. + * would be corrupted.
  22301. + */
  22302. + AuWarn1("timestamps for i%lu are ignored "
  22303. + "since it is on readonly branch (hi%lu).\n",
  22304. + inode->i_ino, h_inode->i_ino);
  22305. + } else if (flags & ~S_ATIME) {
  22306. + err = -EIO;
  22307. + AuIOErr1("unexpected flags 0x%x\n", flags);
  22308. + AuDebugOn(1);
  22309. + }
  22310. +
  22311. + if (!err)
  22312. + au_cpup_attr_timesizes(inode);
  22313. + ii_write_unlock(inode);
  22314. + si_read_unlock(sb);
  22315. + lockdep_on();
  22316. +
  22317. + if (!err && (flags & S_VERSION))
  22318. + inode_inc_iversion(inode);
  22319. +
  22320. + return err;
  22321. +}
  22322. +
  22323. +/* ---------------------------------------------------------------------- */
  22324. +
  22325. +/* no getattr version will be set by module.c:aufs_init() */
  22326. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  22327. + aufs_iop[] = {
  22328. + [AuIop_SYMLINK] = {
  22329. + .permission = aufs_permission,
  22330. +#ifdef CONFIG_FS_POSIX_ACL
  22331. + .get_inode_acl = aufs_get_inode_acl,
  22332. + .get_acl = aufs_get_acl,
  22333. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  22334. +#endif
  22335. +
  22336. + .setattr = aufs_setattr,
  22337. + .getattr = aufs_getattr,
  22338. +
  22339. +#ifdef CONFIG_AUFS_XATTR
  22340. + .listxattr = aufs_listxattr,
  22341. +#endif
  22342. +
  22343. + .get_link = aufs_get_link
  22344. +
  22345. + /* .update_time = aufs_update_time */
  22346. + },
  22347. + [AuIop_DIR] = {
  22348. + .create = aufs_create,
  22349. + .lookup = aufs_lookup,
  22350. + .link = aufs_link,
  22351. + .unlink = aufs_unlink,
  22352. + .symlink = aufs_symlink,
  22353. + .mkdir = aufs_mkdir,
  22354. + .rmdir = aufs_rmdir,
  22355. + .mknod = aufs_mknod,
  22356. + .rename = aufs_rename,
  22357. +
  22358. + .permission = aufs_permission,
  22359. +#ifdef CONFIG_FS_POSIX_ACL
  22360. + .get_inode_acl = aufs_get_inode_acl,
  22361. + .get_acl = aufs_get_acl,
  22362. + .set_acl = aufs_set_acl,
  22363. +#endif
  22364. +
  22365. + .setattr = aufs_setattr,
  22366. + .getattr = aufs_getattr,
  22367. +
  22368. +#ifdef CONFIG_AUFS_XATTR
  22369. + .listxattr = aufs_listxattr,
  22370. +#endif
  22371. +
  22372. + .update_time = aufs_update_time,
  22373. + .atomic_open = aufs_atomic_open,
  22374. + .tmpfile = aufs_tmpfile
  22375. + },
  22376. + [AuIop_OTHER] = {
  22377. + .permission = aufs_permission,
  22378. +#ifdef CONFIG_FS_POSIX_ACL
  22379. + .get_inode_acl = aufs_get_inode_acl,
  22380. + .get_acl = aufs_get_acl,
  22381. + .set_acl = aufs_set_acl,
  22382. +#endif
  22383. +
  22384. + .setattr = aufs_setattr,
  22385. + .getattr = aufs_getattr,
  22386. +
  22387. +#ifdef CONFIG_AUFS_XATTR
  22388. + .listxattr = aufs_listxattr,
  22389. +#endif
  22390. +
  22391. + .update_time = aufs_update_time
  22392. + }
  22393. +};
  22394. diff -Naur null/fs/aufs/i_op_del.c linux-6.6.63/fs/aufs/i_op_del.c
  22395. --- /dev/null
  22396. +++ linux-6.6.63/fs/aufs/i_op_del.c 2024-12-18 15:12:59.297519563 +0300
  22397. @@ -0,0 +1,523 @@
  22398. +// SPDX-License-Identifier: GPL-2.0
  22399. +/*
  22400. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  22401. + *
  22402. + * This program is free software; you can redistribute it and/or modify
  22403. + * it under the terms of the GNU General Public License as published by
  22404. + * the Free Software Foundation; either version 2 of the License, or
  22405. + * (at your option) any later version.
  22406. + *
  22407. + * This program is distributed in the hope that it will be useful,
  22408. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22409. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22410. + * GNU General Public License for more details.
  22411. + *
  22412. + * You should have received a copy of the GNU General Public License
  22413. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22414. + */
  22415. +
  22416. +/*
  22417. + * inode operations (del entry)
  22418. + */
  22419. +
  22420. +#include <linux/iversion.h>
  22421. +#include "aufs.h"
  22422. +
  22423. +/*
  22424. + * decide if a new whiteout for @dentry is necessary or not.
  22425. + * when it is necessary, prepare the parent dir for the upper branch whose
  22426. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  22427. + * be done by caller.
  22428. + * return value:
  22429. + * 0: wh is unnecessary
  22430. + * plus: wh is necessary
  22431. + * minus: error
  22432. + */
  22433. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  22434. +{
  22435. + int need_wh, err;
  22436. + aufs_bindex_t btop;
  22437. + struct super_block *sb;
  22438. +
  22439. + sb = dentry->d_sb;
  22440. + btop = au_dbtop(dentry);
  22441. + if (*bcpup < 0) {
  22442. + *bcpup = btop;
  22443. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  22444. + err = AuWbrCopyup(au_sbi(sb), dentry);
  22445. + *bcpup = err;
  22446. + if (unlikely(err < 0))
  22447. + goto out;
  22448. + }
  22449. + } else
  22450. + AuDebugOn(btop < *bcpup
  22451. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  22452. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  22453. +
  22454. + if (*bcpup != btop) {
  22455. + err = au_cpup_dirs(dentry, *bcpup);
  22456. + if (unlikely(err))
  22457. + goto out;
  22458. + need_wh = 1;
  22459. + } else {
  22460. + struct au_dinfo *dinfo, *tmp;
  22461. +
  22462. + need_wh = -ENOMEM;
  22463. + dinfo = au_di(dentry);
  22464. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  22465. + if (tmp) {
  22466. + au_di_cp(tmp, dinfo);
  22467. + au_di_swap(tmp, dinfo);
  22468. + /* returns the number of positive dentries */
  22469. + need_wh = au_lkup_dentry(dentry, btop + 1,
  22470. + /* AuLkup_IGNORE_PERM */ 0);
  22471. + au_di_swap(tmp, dinfo);
  22472. + au_rw_write_unlock(&tmp->di_rwsem);
  22473. + au_di_free(tmp);
  22474. + }
  22475. + }
  22476. + AuDbg("need_wh %d\n", need_wh);
  22477. + err = need_wh;
  22478. +
  22479. +out:
  22480. + return err;
  22481. +}
  22482. +
  22483. +/*
  22484. + * simple tests for the del-entry operations.
  22485. + * following the checks in vfs, plus the parent-child relationship.
  22486. + */
  22487. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  22488. + struct dentry *h_parent, int isdir)
  22489. +{
  22490. + int err;
  22491. + umode_t h_mode;
  22492. + struct dentry *h_dentry, *h_latest;
  22493. + struct inode *h_inode;
  22494. + struct path h_ppath;
  22495. + struct super_block *sb;
  22496. + struct au_branch *br;
  22497. + struct mnt_idmap *h_idmap;
  22498. +
  22499. + h_dentry = au_h_dptr(dentry, bindex);
  22500. + if (d_really_is_positive(dentry)) {
  22501. + err = -ENOENT;
  22502. + if (unlikely(d_is_negative(h_dentry)))
  22503. + goto out;
  22504. + h_inode = d_inode(h_dentry);
  22505. + if (unlikely(!vfsub_inode_nlink(h_inode, AU_I_BRANCH)))
  22506. + goto out;
  22507. +
  22508. + h_mode = h_inode->i_mode;
  22509. + if (!isdir) {
  22510. + err = -EISDIR;
  22511. + if (unlikely(S_ISDIR(h_mode)))
  22512. + goto out;
  22513. + } else if (unlikely(!S_ISDIR(h_mode))) {
  22514. + err = -ENOTDIR;
  22515. + goto out;
  22516. + }
  22517. + } else {
  22518. + /* rename(2) case */
  22519. + err = -EIO;
  22520. + if (unlikely(d_is_positive(h_dentry)))
  22521. + goto out;
  22522. + }
  22523. +
  22524. + err = -ENOENT;
  22525. + /* expected parent dir is locked */
  22526. + if (unlikely(h_parent != h_dentry->d_parent))
  22527. + goto out;
  22528. + err = 0;
  22529. +
  22530. + /*
  22531. + * rmdir a dir may break the consistency on some filesystem.
  22532. + * let's try heavy test.
  22533. + */
  22534. + err = -EACCES;
  22535. + sb = dentry->d_sb;
  22536. + br = au_sbr(sb, bindex);
  22537. + h_idmap = au_br_idmap(br);
  22538. + if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
  22539. + && au_test_h_perm(h_idmap, d_inode(h_parent),
  22540. + MAY_EXEC | MAY_WRITE)))
  22541. + goto out;
  22542. +
  22543. + h_ppath.dentry = h_parent;
  22544. + h_ppath.mnt = au_br_mnt(br);
  22545. + h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
  22546. + err = -EIO;
  22547. + if (IS_ERR(h_latest))
  22548. + goto out;
  22549. + if (h_latest == h_dentry)
  22550. + err = 0;
  22551. + dput(h_latest);
  22552. +
  22553. +out:
  22554. + return err;
  22555. +}
  22556. +
  22557. +/*
  22558. + * decide the branch where we operate for @dentry. the branch index will be set
  22559. + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
  22560. + * dir for reverting.
  22561. + * when a new whiteout is necessary, create it.
  22562. + */
  22563. +static struct dentry*
  22564. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  22565. + struct au_dtime *dt, struct au_pin *pin)
  22566. +{
  22567. + struct dentry *wh_dentry;
  22568. + struct super_block *sb;
  22569. + struct path h_path;
  22570. + int err, need_wh;
  22571. + unsigned int udba;
  22572. + aufs_bindex_t bcpup;
  22573. +
  22574. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  22575. + wh_dentry = ERR_PTR(need_wh);
  22576. + if (unlikely(need_wh < 0))
  22577. + goto out;
  22578. +
  22579. + sb = dentry->d_sb;
  22580. + udba = au_opt_udba(sb);
  22581. + bcpup = *rbcpup;
  22582. + err = au_pin(pin, dentry, bcpup, udba,
  22583. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22584. + wh_dentry = ERR_PTR(err);
  22585. + if (unlikely(err))
  22586. + goto out;
  22587. +
  22588. + h_path.dentry = au_pinned_h_parent(pin);
  22589. + if (udba != AuOpt_UDBA_NONE
  22590. + && au_dbtop(dentry) == bcpup) {
  22591. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  22592. + wh_dentry = ERR_PTR(err);
  22593. + if (unlikely(err))
  22594. + goto out_unpin;
  22595. + }
  22596. +
  22597. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  22598. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  22599. + wh_dentry = NULL;
  22600. + if (!need_wh)
  22601. + goto out; /* success, no need to create whiteout */
  22602. +
  22603. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  22604. + if (IS_ERR(wh_dentry))
  22605. + goto out_unpin;
  22606. +
  22607. + /* returns with the parent is locked and wh_dentry is dget-ed */
  22608. + goto out; /* success */
  22609. +
  22610. +out_unpin:
  22611. + au_unpin(pin);
  22612. +out:
  22613. + return wh_dentry;
  22614. +}
  22615. +
  22616. +/*
  22617. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  22618. + * in order to be revertible and save time for removing many child whiteouts
  22619. + * under the dir.
  22620. + * returns 1 when there are too many child whiteout and caller should remove
  22621. + * them asynchronously. returns 0 when the number of children is enough small to
  22622. + * remove now or the branch fs is a remote fs.
  22623. + * otherwise return an error.
  22624. + */
  22625. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  22626. + struct au_nhash *whlist, struct inode *dir)
  22627. +{
  22628. + int rmdir_later, err, dirwh;
  22629. + struct dentry *h_dentry;
  22630. + struct super_block *sb;
  22631. + struct inode *inode;
  22632. +
  22633. + sb = dentry->d_sb;
  22634. + SiMustAnyLock(sb);
  22635. + h_dentry = au_h_dptr(dentry, bindex);
  22636. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  22637. + if (unlikely(err))
  22638. + goto out;
  22639. +
  22640. + /* stop monitoring */
  22641. + inode = d_inode(dentry);
  22642. + au_hn_free(au_hi(inode, bindex));
  22643. +
  22644. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  22645. + dirwh = au_sbi(sb)->si_dirwh;
  22646. + rmdir_later = (dirwh <= 1);
  22647. + if (!rmdir_later)
  22648. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  22649. + dirwh);
  22650. + if (rmdir_later)
  22651. + return rmdir_later;
  22652. + }
  22653. +
  22654. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  22655. + if (unlikely(err)) {
  22656. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  22657. + h_dentry, bindex, err);
  22658. + err = 0;
  22659. + }
  22660. +
  22661. +out:
  22662. + AuTraceErr(err);
  22663. + return err;
  22664. +}
  22665. +
  22666. +/*
  22667. + * final procedure for deleting a entry.
  22668. + * maintain dentry and iattr.
  22669. + */
  22670. +static void epilog(struct inode *dir, struct dentry *dentry,
  22671. + aufs_bindex_t bindex)
  22672. +{
  22673. + struct inode *inode;
  22674. +
  22675. + inode = d_inode(dentry);
  22676. + d_drop(dentry);
  22677. + inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
  22678. +
  22679. + au_dir_ts(dir, bindex);
  22680. + inode_inc_iversion(dir);
  22681. +}
  22682. +
  22683. +/*
  22684. + * when an error happened, remove the created whiteout and revert everything.
  22685. + */
  22686. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  22687. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  22688. + struct dentry *dentry, struct au_dtime *dt)
  22689. +{
  22690. + int rerr;
  22691. + struct path h_path = {
  22692. + .dentry = wh_dentry,
  22693. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  22694. + };
  22695. +
  22696. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  22697. + if (!rerr) {
  22698. + au_set_dbwh(dentry, bwh);
  22699. + au_dtime_revert(dt);
  22700. + return 0;
  22701. + }
  22702. +
  22703. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  22704. + return -EIO;
  22705. +}
  22706. +
  22707. +/* ---------------------------------------------------------------------- */
  22708. +
  22709. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  22710. +{
  22711. + int err;
  22712. + aufs_bindex_t bwh, bindex, btop;
  22713. + struct inode *inode, *h_dir, *delegated, *h_inode;
  22714. + struct dentry *parent, *wh_dentry;
  22715. + /* to reduce stack size */
  22716. + struct {
  22717. + struct au_dtime dt;
  22718. + struct au_pin pin;
  22719. + struct path h_path;
  22720. + } *a;
  22721. +
  22722. + IMustLock(dir);
  22723. +
  22724. + err = -ENOMEM;
  22725. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22726. + if (unlikely(!a))
  22727. + goto out;
  22728. +
  22729. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  22730. + if (unlikely(err))
  22731. + goto out_free;
  22732. + err = au_d_hashed_positive(dentry);
  22733. + if (unlikely(err))
  22734. + goto out_unlock;
  22735. + inode = d_inode(dentry);
  22736. + IMustLock(inode);
  22737. + err = -EISDIR;
  22738. + if (unlikely(d_is_dir(dentry)))
  22739. + goto out_unlock; /* possible? */
  22740. +
  22741. + btop = au_dbtop(dentry);
  22742. + bwh = au_dbwh(dentry);
  22743. + bindex = -1;
  22744. + parent = dentry->d_parent; /* dir inode is locked */
  22745. + di_write_lock_parent(parent);
  22746. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  22747. + &a->pin);
  22748. + err = PTR_ERR(wh_dentry);
  22749. + if (IS_ERR(wh_dentry))
  22750. + goto out_parent;
  22751. +
  22752. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  22753. + a->h_path.dentry = au_h_dptr(dentry, btop);
  22754. + dget(a->h_path.dentry);
  22755. + if (bindex == btop) {
  22756. + h_dir = au_pinned_h_dir(&a->pin);
  22757. + delegated = NULL;
  22758. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  22759. + if (unlikely(err == -EWOULDBLOCK)) {
  22760. + pr_warn("cannot retry for NFSv4 delegation"
  22761. + " for an internal unlink\n");
  22762. + iput(delegated);
  22763. + }
  22764. + } else {
  22765. + /* dir inode is locked */
  22766. + h_dir = d_inode(wh_dentry->d_parent);
  22767. + IMustLock(h_dir);
  22768. + err = 0;
  22769. + }
  22770. +
  22771. + if (!err) {
  22772. + vfsub_drop_nlink(inode);
  22773. + epilog(dir, dentry, bindex);
  22774. +
  22775. + /* update target timestamps */
  22776. + if (bindex == btop) {
  22777. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  22778. + /*ignore*/
  22779. + h_inode = d_inode(a->h_path.dentry);
  22780. + inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
  22781. + } else
  22782. + /* todo: this timestamp may be reverted later */
  22783. + inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
  22784. + goto out_unpin; /* success */
  22785. + }
  22786. +
  22787. + /* revert */
  22788. + if (wh_dentry) {
  22789. + int rerr;
  22790. +
  22791. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  22792. + &a->dt);
  22793. + if (rerr)
  22794. + err = rerr;
  22795. + }
  22796. +
  22797. +out_unpin:
  22798. + au_unpin(&a->pin);
  22799. + dput(wh_dentry);
  22800. + dput(a->h_path.dentry);
  22801. +out_parent:
  22802. + di_write_unlock(parent);
  22803. +out_unlock:
  22804. + aufs_read_unlock(dentry, AuLock_DW);
  22805. +out_free:
  22806. + au_kfree_rcu(a);
  22807. +out:
  22808. + return err;
  22809. +}
  22810. +
  22811. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  22812. +{
  22813. + int err, rmdir_later;
  22814. + aufs_bindex_t bwh, bindex, btop;
  22815. + struct inode *inode;
  22816. + struct dentry *parent, *wh_dentry, *h_dentry;
  22817. + struct au_whtmp_rmdir *args;
  22818. + /* to reduce stack size */
  22819. + struct {
  22820. + struct au_dtime dt;
  22821. + struct au_pin pin;
  22822. + } *a;
  22823. +
  22824. + IMustLock(dir);
  22825. +
  22826. + err = -ENOMEM;
  22827. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22828. + if (unlikely(!a))
  22829. + goto out;
  22830. +
  22831. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  22832. + if (unlikely(err))
  22833. + goto out_free;
  22834. + err = au_alive_dir(dentry);
  22835. + if (unlikely(err))
  22836. + goto out_unlock;
  22837. + inode = d_inode(dentry);
  22838. + IMustLock(inode);
  22839. + err = -ENOTDIR;
  22840. + if (unlikely(!d_is_dir(dentry)))
  22841. + goto out_unlock; /* possible? */
  22842. +
  22843. + err = -ENOMEM;
  22844. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  22845. + if (unlikely(!args))
  22846. + goto out_unlock;
  22847. +
  22848. + parent = dentry->d_parent; /* dir inode is locked */
  22849. + di_write_lock_parent(parent);
  22850. + err = au_test_empty(dentry, &args->whlist);
  22851. + if (unlikely(err))
  22852. + goto out_parent;
  22853. +
  22854. + btop = au_dbtop(dentry);
  22855. + bwh = au_dbwh(dentry);
  22856. + bindex = -1;
  22857. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  22858. + &a->pin);
  22859. + err = PTR_ERR(wh_dentry);
  22860. + if (IS_ERR(wh_dentry))
  22861. + goto out_parent;
  22862. +
  22863. + h_dentry = au_h_dptr(dentry, btop);
  22864. + dget(h_dentry);
  22865. + rmdir_later = 0;
  22866. + if (bindex == btop) {
  22867. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  22868. + if (err > 0) {
  22869. + rmdir_later = err;
  22870. + err = 0;
  22871. + }
  22872. + } else {
  22873. + /* stop monitoring */
  22874. + au_hn_free(au_hi(inode, btop));
  22875. +
  22876. + /* dir inode is locked */
  22877. + IMustLock(d_inode(wh_dentry->d_parent));
  22878. + err = 0;
  22879. + }
  22880. +
  22881. + if (!err) {
  22882. + vfsub_dead_dir(inode);
  22883. + au_set_dbdiropq(dentry, -1);
  22884. + epilog(dir, dentry, bindex);
  22885. +
  22886. + if (rmdir_later) {
  22887. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  22888. + args = NULL;
  22889. + }
  22890. +
  22891. + goto out_unpin; /* success */
  22892. + }
  22893. +
  22894. + /* revert */
  22895. + AuLabel(revert);
  22896. + if (wh_dentry) {
  22897. + int rerr;
  22898. +
  22899. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  22900. + &a->dt);
  22901. + if (rerr)
  22902. + err = rerr;
  22903. + }
  22904. +
  22905. +out_unpin:
  22906. + au_unpin(&a->pin);
  22907. + dput(wh_dentry);
  22908. + dput(h_dentry);
  22909. +out_parent:
  22910. + di_write_unlock(parent);
  22911. + if (args)
  22912. + au_whtmp_rmdir_free(args);
  22913. +out_unlock:
  22914. + aufs_read_unlock(dentry, AuLock_DW);
  22915. +out_free:
  22916. + au_kfree_rcu(a);
  22917. +out:
  22918. + AuTraceErr(err);
  22919. + return err;
  22920. +}
  22921. diff -Naur null/fs/aufs/i_op_ren.c linux-6.6.63/fs/aufs/i_op_ren.c
  22922. --- /dev/null
  22923. +++ linux-6.6.63/fs/aufs/i_op_ren.c 2024-12-18 15:12:59.297519563 +0300
  22924. @@ -0,0 +1,1260 @@
  22925. +// SPDX-License-Identifier: GPL-2.0
  22926. +/*
  22927. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  22928. + *
  22929. + * This program is free software; you can redistribute it and/or modify
  22930. + * it under the terms of the GNU General Public License as published by
  22931. + * the Free Software Foundation; either version 2 of the License, or
  22932. + * (at your option) any later version.
  22933. + *
  22934. + * This program is distributed in the hope that it will be useful,
  22935. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22936. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22937. + * GNU General Public License for more details.
  22938. + *
  22939. + * You should have received a copy of the GNU General Public License
  22940. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22941. + */
  22942. +
  22943. +/*
  22944. + * inode operation (rename entry)
  22945. + * todo: this is crazy monster
  22946. + */
  22947. +
  22948. +#include <linux/iversion.h>
  22949. +#include "aufs.h"
  22950. +
  22951. +enum { AuSRC, AuDST, AuSrcDst };
  22952. +enum { AuPARENT, AuCHILD, AuParentChild };
  22953. +
  22954. +#define AuRen_ISDIR_SRC 1
  22955. +#define AuRen_ISDIR_DST (1 << 1)
  22956. +#define AuRen_ISSAMEDIR (1 << 2)
  22957. +#define AuRen_WHSRC (1 << 3)
  22958. +#define AuRen_WHDST (1 << 4)
  22959. +#define AuRen_MNT_WRITE (1 << 5)
  22960. +#define AuRen_DT_DSTDIR (1 << 6)
  22961. +#define AuRen_DIROPQ_SRC (1 << 7)
  22962. +#define AuRen_DIROPQ_DST (1 << 8)
  22963. +#define AuRen_DIRREN (1 << 9)
  22964. +#define AuRen_DROPPED_SRC (1 << 10)
  22965. +#define AuRen_DROPPED_DST (1 << 11)
  22966. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  22967. +#define au_fset_ren(flags, name) \
  22968. + do { (flags) |= AuRen_##name; } while (0)
  22969. +#define au_fclr_ren(flags, name) \
  22970. + do { (flags) &= ~AuRen_##name; } while (0)
  22971. +
  22972. +#ifndef CONFIG_AUFS_DIRREN
  22973. +#undef AuRen_DIRREN
  22974. +#define AuRen_DIRREN 0
  22975. +#endif
  22976. +
  22977. +struct au_ren_args {
  22978. + struct {
  22979. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  22980. + *wh_dentry;
  22981. + struct inode *dir, *inode;
  22982. + struct au_hinode *hdir, *hinode;
  22983. + struct au_dtime dt[AuParentChild];
  22984. + aufs_bindex_t btop, bdiropq;
  22985. + } sd[AuSrcDst];
  22986. +
  22987. +#define src_dentry sd[AuSRC].dentry
  22988. +#define src_dir sd[AuSRC].dir
  22989. +#define src_inode sd[AuSRC].inode
  22990. +#define src_h_dentry sd[AuSRC].h_dentry
  22991. +#define src_parent sd[AuSRC].parent
  22992. +#define src_h_parent sd[AuSRC].h_parent
  22993. +#define src_wh_dentry sd[AuSRC].wh_dentry
  22994. +#define src_hdir sd[AuSRC].hdir
  22995. +#define src_hinode sd[AuSRC].hinode
  22996. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  22997. +#define src_dt sd[AuSRC].dt
  22998. +#define src_btop sd[AuSRC].btop
  22999. +#define src_bdiropq sd[AuSRC].bdiropq
  23000. +
  23001. +#define dst_dentry sd[AuDST].dentry
  23002. +#define dst_dir sd[AuDST].dir
  23003. +#define dst_inode sd[AuDST].inode
  23004. +#define dst_h_dentry sd[AuDST].h_dentry
  23005. +#define dst_parent sd[AuDST].parent
  23006. +#define dst_h_parent sd[AuDST].h_parent
  23007. +#define dst_wh_dentry sd[AuDST].wh_dentry
  23008. +#define dst_hdir sd[AuDST].hdir
  23009. +#define dst_hinode sd[AuDST].hinode
  23010. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  23011. +#define dst_dt sd[AuDST].dt
  23012. +#define dst_btop sd[AuDST].btop
  23013. +#define dst_bdiropq sd[AuDST].bdiropq
  23014. +
  23015. + struct dentry *h_trap;
  23016. + struct au_branch *br;
  23017. + struct path h_path;
  23018. + struct au_nhash whlist;
  23019. + aufs_bindex_t btgt, src_bwh;
  23020. +
  23021. + struct {
  23022. + unsigned short auren_flags;
  23023. + unsigned char flags; /* syscall parameter */
  23024. + unsigned char exchange;
  23025. + } __packed;
  23026. +
  23027. + struct au_whtmp_rmdir *thargs;
  23028. + struct dentry *h_dst;
  23029. + struct au_hinode *h_root;
  23030. +};
  23031. +
  23032. +/* ---------------------------------------------------------------------- */
  23033. +
  23034. +/*
  23035. + * functions for reverting.
  23036. + * when an error happened in a single rename systemcall, we should revert
  23037. + * everything as if nothing happened.
  23038. + * we don't need to revert the copied-up/down the parent dir since they are
  23039. + * harmless.
  23040. + */
  23041. +
  23042. +#define RevertFailure(fmt, ...) do { \
  23043. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  23044. + ##__VA_ARGS__, err, rerr); \
  23045. + err = -EIO; \
  23046. +} while (0)
  23047. +
  23048. +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
  23049. +{
  23050. + int rerr;
  23051. + struct dentry *d;
  23052. +#define src_or_dst(member) a->sd[idx].member
  23053. +
  23054. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  23055. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  23056. + rerr = au_diropq_remove(d, a->btgt);
  23057. + au_hn_inode_unlock(src_or_dst(hinode));
  23058. + au_set_dbdiropq(d, src_or_dst(bdiropq));
  23059. + if (rerr)
  23060. + RevertFailure("remove diropq %pd", d);
  23061. +
  23062. +#undef src_or_dst_
  23063. +}
  23064. +
  23065. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  23066. +{
  23067. + if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
  23068. + au_ren_do_rev_diropq(err, a, AuSRC);
  23069. + if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
  23070. + au_ren_do_rev_diropq(err, a, AuDST);
  23071. +}
  23072. +
  23073. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  23074. +{
  23075. + int rerr;
  23076. + struct inode *delegated;
  23077. + struct path h_ppath = {
  23078. + .dentry = a->src_h_parent,
  23079. + .mnt = a->h_path.mnt
  23080. + };
  23081. +
  23082. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
  23083. + rerr = PTR_ERR(a->h_path.dentry);
  23084. + if (IS_ERR(a->h_path.dentry)) {
  23085. + RevertFailure("lkup one %pd", a->src_dentry);
  23086. + return;
  23087. + }
  23088. +
  23089. + delegated = NULL;
  23090. + rerr = vfsub_rename(a->dst_h_dir,
  23091. + au_h_dptr(a->src_dentry, a->btgt),
  23092. + a->src_h_dir, &a->h_path, &delegated, a->flags);
  23093. + if (unlikely(rerr == -EWOULDBLOCK)) {
  23094. + pr_warn("cannot retry for NFSv4 delegation"
  23095. + " for an internal rename\n");
  23096. + iput(delegated);
  23097. + }
  23098. + d_drop(a->h_path.dentry);
  23099. + dput(a->h_path.dentry);
  23100. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  23101. + if (rerr)
  23102. + RevertFailure("rename %pd", a->src_dentry);
  23103. +}
  23104. +
  23105. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  23106. +{
  23107. + int rerr;
  23108. + struct inode *delegated;
  23109. + struct path h_ppath = {
  23110. + .dentry = a->dst_h_parent,
  23111. + .mnt = a->h_path.mnt
  23112. + };
  23113. +
  23114. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
  23115. + rerr = PTR_ERR(a->h_path.dentry);
  23116. + if (IS_ERR(a->h_path.dentry)) {
  23117. + RevertFailure("lkup one %pd", a->dst_dentry);
  23118. + return;
  23119. + }
  23120. + if (d_is_positive(a->h_path.dentry)) {
  23121. + d_drop(a->h_path.dentry);
  23122. + dput(a->h_path.dentry);
  23123. + return;
  23124. + }
  23125. +
  23126. + delegated = NULL;
  23127. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  23128. + &delegated, a->flags);
  23129. + if (unlikely(rerr == -EWOULDBLOCK)) {
  23130. + pr_warn("cannot retry for NFSv4 delegation"
  23131. + " for an internal rename\n");
  23132. + iput(delegated);
  23133. + }
  23134. + d_drop(a->h_path.dentry);
  23135. + dput(a->h_path.dentry);
  23136. + if (!rerr)
  23137. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  23138. + else
  23139. + RevertFailure("rename %pd", a->h_dst);
  23140. +}
  23141. +
  23142. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  23143. +{
  23144. + int rerr;
  23145. +
  23146. + a->h_path.dentry = a->src_wh_dentry;
  23147. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  23148. + au_set_dbwh(a->src_dentry, a->src_bwh);
  23149. + if (rerr)
  23150. + RevertFailure("unlink %pd", a->src_wh_dentry);
  23151. +}
  23152. +#undef RevertFailure
  23153. +
  23154. +/* ---------------------------------------------------------------------- */
  23155. +
  23156. +/*
  23157. + * when we have to copyup the renaming entry, do it with the rename-target name
  23158. + * in order to minimize the cost (the later actual rename is unnecessary).
  23159. + * otherwise rename it on the target branch.
  23160. + */
  23161. +static int au_ren_or_cpup(struct au_ren_args *a)
  23162. +{
  23163. + int err;
  23164. + struct dentry *d;
  23165. + struct inode *delegated;
  23166. +
  23167. + d = a->src_dentry;
  23168. + if (au_dbtop(d) == a->btgt) {
  23169. + a->h_path.dentry = a->dst_h_dentry;
  23170. + AuDebugOn(au_dbtop(d) != a->btgt);
  23171. + delegated = NULL;
  23172. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  23173. + a->dst_h_dir, &a->h_path, &delegated,
  23174. + a->flags);
  23175. + if (unlikely(err == -EWOULDBLOCK)) {
  23176. + pr_warn("cannot retry for NFSv4 delegation"
  23177. + " for an internal rename\n");
  23178. + iput(delegated);
  23179. + }
  23180. + } else
  23181. + BUG();
  23182. +
  23183. + if (!err && a->h_dst)
  23184. + /* it will be set to dinfo later */
  23185. + dget(a->h_dst);
  23186. +
  23187. + return err;
  23188. +}
  23189. +
  23190. +/* cf. aufs_rmdir() */
  23191. +static int au_ren_del_whtmp(struct au_ren_args *a)
  23192. +{
  23193. + int err;
  23194. + struct inode *dir;
  23195. +
  23196. + dir = a->dst_dir;
  23197. + SiMustAnyLock(dir->i_sb);
  23198. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  23199. + au_sbi(dir->i_sb)->si_dirwh)
  23200. + || au_test_fs_remote(a->h_dst->d_sb)) {
  23201. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  23202. + if (unlikely(err))
  23203. + pr_warn("failed removing whtmp dir %pd (%d), "
  23204. + "ignored.\n", a->h_dst, err);
  23205. + } else {
  23206. + au_nhash_wh_free(&a->thargs->whlist);
  23207. + a->thargs->whlist = a->whlist;
  23208. + a->whlist.nh_num = 0;
  23209. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  23210. + dput(a->h_dst);
  23211. + a->thargs = NULL;
  23212. + }
  23213. +
  23214. + return 0;
  23215. +}
  23216. +
  23217. +/* make it 'opaque' dir. */
  23218. +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
  23219. +{
  23220. + int err;
  23221. + struct dentry *d, *diropq;
  23222. +#define src_or_dst(member) a->sd[idx].member
  23223. +
  23224. + err = 0;
  23225. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  23226. + src_or_dst(bdiropq) = au_dbdiropq(d);
  23227. + src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
  23228. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  23229. + diropq = au_diropq_create(d, a->btgt);
  23230. + au_hn_inode_unlock(src_or_dst(hinode));
  23231. + if (IS_ERR(diropq))
  23232. + err = PTR_ERR(diropq);
  23233. + else
  23234. + dput(diropq);
  23235. +
  23236. +#undef src_or_dst_
  23237. + return err;
  23238. +}
  23239. +
  23240. +static int au_ren_diropq(struct au_ren_args *a)
  23241. +{
  23242. + int err;
  23243. + unsigned char always;
  23244. + struct dentry *d;
  23245. +
  23246. + err = 0;
  23247. + d = a->dst_dentry; /* already renamed on the branch */
  23248. + always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
  23249. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23250. + && !au_ftest_ren(a->auren_flags, DIRREN)
  23251. + && a->btgt != au_dbdiropq(a->src_dentry)
  23252. + && (a->dst_wh_dentry
  23253. + || a->btgt <= au_dbdiropq(d)
  23254. + /* hide the lower to keep xino */
  23255. + /* the lowers may not be a dir, but we hide them anyway */
  23256. + || a->btgt < au_dbbot(d)
  23257. + || always)) {
  23258. + AuDbg("here\n");
  23259. + err = au_ren_do_diropq(a, AuSRC);
  23260. + if (unlikely(err))
  23261. + goto out;
  23262. + au_fset_ren(a->auren_flags, DIROPQ_SRC);
  23263. + }
  23264. + if (!a->exchange)
  23265. + goto out; /* success */
  23266. +
  23267. + d = a->src_dentry; /* already renamed on the branch */
  23268. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  23269. + && a->btgt != au_dbdiropq(a->dst_dentry)
  23270. + && (a->btgt < au_dbdiropq(d)
  23271. + || a->btgt < au_dbbot(d)
  23272. + || always)) {
  23273. + AuDbgDentry(a->src_dentry);
  23274. + AuDbgDentry(a->dst_dentry);
  23275. + err = au_ren_do_diropq(a, AuDST);
  23276. + if (unlikely(err))
  23277. + goto out_rev_src;
  23278. + au_fset_ren(a->auren_flags, DIROPQ_DST);
  23279. + }
  23280. + goto out; /* success */
  23281. +
  23282. +out_rev_src:
  23283. + AuDbg("err %d, reverting src\n", err);
  23284. + au_ren_rev_diropq(err, a);
  23285. +out:
  23286. + return err;
  23287. +}
  23288. +
  23289. +static int do_rename(struct au_ren_args *a)
  23290. +{
  23291. + int err;
  23292. + struct dentry *d, *h_d;
  23293. + struct inode *h_inode;
  23294. +
  23295. + if (!a->exchange) {
  23296. + /* prepare workqueue args for asynchronous rmdir */
  23297. + h_d = a->dst_h_dentry;
  23298. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  23299. + /* && !au_ftest_ren(a->auren_flags, DIRREN) */
  23300. + && d_is_positive(h_d)) {
  23301. + err = -ENOMEM;
  23302. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
  23303. + GFP_NOFS);
  23304. + if (unlikely(!a->thargs))
  23305. + goto out;
  23306. + a->h_dst = dget(h_d);
  23307. + }
  23308. +
  23309. + /* create whiteout for src_dentry */
  23310. + if (au_ftest_ren(a->auren_flags, WHSRC)) {
  23311. + a->src_bwh = au_dbwh(a->src_dentry);
  23312. + AuDebugOn(a->src_bwh >= 0);
  23313. + a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
  23314. + a->src_h_parent);
  23315. + err = PTR_ERR(a->src_wh_dentry);
  23316. + if (IS_ERR(a->src_wh_dentry))
  23317. + goto out_thargs;
  23318. + }
  23319. +
  23320. + /* lookup whiteout for dentry */
  23321. + if (au_ftest_ren(a->auren_flags, WHDST)) {
  23322. + h_d = au_wh_lkup(a->dst_h_parent,
  23323. + &a->dst_dentry->d_name, a->br);
  23324. + err = PTR_ERR(h_d);
  23325. + if (IS_ERR(h_d))
  23326. + goto out_whsrc;
  23327. + if (d_is_negative(h_d))
  23328. + dput(h_d);
  23329. + else
  23330. + a->dst_wh_dentry = h_d;
  23331. + }
  23332. +
  23333. + /* rename dentry to tmpwh */
  23334. + if (a->thargs) {
  23335. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  23336. + if (unlikely(err))
  23337. + goto out_whdst;
  23338. +
  23339. + d = a->dst_dentry;
  23340. + au_set_h_dptr(d, a->btgt, NULL);
  23341. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  23342. + if (unlikely(err))
  23343. + goto out_whtmp;
  23344. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  23345. + }
  23346. + }
  23347. +
  23348. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  23349. +#if 0 /* debugging */
  23350. + BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
  23351. + && d_is_positive(a->dst_h_dentry)
  23352. + && a->src_btop != a->btgt);
  23353. +#endif
  23354. +
  23355. + /* rename by vfs_rename or cpup */
  23356. + err = au_ren_or_cpup(a);
  23357. + if (unlikely(err))
  23358. + /* leave the copied-up one */
  23359. + goto out_whtmp;
  23360. +
  23361. + /* make dir opaque */
  23362. + err = au_ren_diropq(a);
  23363. + if (unlikely(err))
  23364. + goto out_rename;
  23365. +
  23366. + /* update target timestamps */
  23367. + if (a->exchange) {
  23368. + AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
  23369. + a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
  23370. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  23371. + h_inode = d_inode(a->h_path.dentry);
  23372. + inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
  23373. + }
  23374. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  23375. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  23376. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  23377. + h_inode = d_inode(a->h_path.dentry);
  23378. + inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
  23379. +
  23380. + if (!a->exchange) {
  23381. + /* remove whiteout for dentry */
  23382. + if (a->dst_wh_dentry) {
  23383. + a->h_path.dentry = a->dst_wh_dentry;
  23384. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  23385. + a->dst_dentry);
  23386. + if (unlikely(err))
  23387. + goto out_diropq;
  23388. + }
  23389. +
  23390. + /* remove whtmp */
  23391. + if (a->thargs)
  23392. + au_ren_del_whtmp(a); /* ignore this error */
  23393. +
  23394. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  23395. + }
  23396. + err = 0;
  23397. + goto out_success;
  23398. +
  23399. +out_diropq:
  23400. + au_ren_rev_diropq(err, a);
  23401. +out_rename:
  23402. + au_ren_rev_rename(err, a);
  23403. + dput(a->h_dst);
  23404. +out_whtmp:
  23405. + if (a->thargs)
  23406. + au_ren_rev_whtmp(err, a);
  23407. +out_whdst:
  23408. + dput(a->dst_wh_dentry);
  23409. + a->dst_wh_dentry = NULL;
  23410. +out_whsrc:
  23411. + if (a->src_wh_dentry)
  23412. + au_ren_rev_whsrc(err, a);
  23413. +out_success:
  23414. + dput(a->src_wh_dentry);
  23415. + dput(a->dst_wh_dentry);
  23416. +out_thargs:
  23417. + if (a->thargs) {
  23418. + dput(a->h_dst);
  23419. + au_whtmp_rmdir_free(a->thargs);
  23420. + a->thargs = NULL;
  23421. + }
  23422. +out:
  23423. + return err;
  23424. +}
  23425. +
  23426. +/* ---------------------------------------------------------------------- */
  23427. +
  23428. +/*
  23429. + * test if @dentry dir can be rename destination or not.
  23430. + * success means, it is a logically empty dir.
  23431. + */
  23432. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  23433. +{
  23434. + return au_test_empty(dentry, whlist);
  23435. +}
  23436. +
  23437. +/*
  23438. + * test if @a->src_dentry dir can be rename source or not.
  23439. + * if it can, return 0.
  23440. + * success means,
  23441. + * - it is a logically empty dir.
  23442. + * - or, it exists on writable branch and has no children including whiteouts
  23443. + * on the lower branch unless DIRREN is on.
  23444. + */
  23445. +static int may_rename_srcdir(struct au_ren_args *a)
  23446. +{
  23447. + int err;
  23448. + unsigned int rdhash;
  23449. + aufs_bindex_t btop, btgt;
  23450. + struct dentry *dentry;
  23451. + struct super_block *sb;
  23452. + struct au_sbinfo *sbinfo;
  23453. +
  23454. + dentry = a->src_dentry;
  23455. + sb = dentry->d_sb;
  23456. + sbinfo = au_sbi(sb);
  23457. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  23458. + au_fset_ren(a->auren_flags, DIRREN);
  23459. +
  23460. + btgt = a->btgt;
  23461. + btop = au_dbtop(dentry);
  23462. + if (btop != btgt) {
  23463. + struct au_nhash whlist;
  23464. +
  23465. + SiMustAnyLock(sb);
  23466. + rdhash = sbinfo->si_rdhash;
  23467. + if (!rdhash)
  23468. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  23469. + dentry));
  23470. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  23471. + if (unlikely(err))
  23472. + goto out;
  23473. + err = au_test_empty(dentry, &whlist);
  23474. + au_nhash_wh_free(&whlist);
  23475. + goto out;
  23476. + }
  23477. +
  23478. + if (btop == au_dbtaildir(dentry))
  23479. + return 0; /* success */
  23480. +
  23481. + err = au_test_empty_lower(dentry);
  23482. +
  23483. +out:
  23484. + if (err == -ENOTEMPTY) {
  23485. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  23486. + err = 0;
  23487. + } else {
  23488. + AuWarn1("renaming dir who has child(ren) on multiple "
  23489. + "branches, is not supported\n");
  23490. + err = -EXDEV;
  23491. + }
  23492. + }
  23493. + return err;
  23494. +}
  23495. +
  23496. +/* side effect: sets whlist and h_dentry */
  23497. +static int au_ren_may_dir(struct au_ren_args *a)
  23498. +{
  23499. + int err;
  23500. + unsigned int rdhash;
  23501. + struct dentry *d;
  23502. +
  23503. + d = a->dst_dentry;
  23504. + SiMustAnyLock(d->d_sb);
  23505. +
  23506. + err = 0;
  23507. + if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
  23508. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  23509. + if (!rdhash)
  23510. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  23511. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  23512. + if (unlikely(err))
  23513. + goto out;
  23514. +
  23515. + if (!a->exchange) {
  23516. + au_set_dbtop(d, a->dst_btop);
  23517. + err = may_rename_dstdir(d, &a->whlist);
  23518. + au_set_dbtop(d, a->btgt);
  23519. + } else
  23520. + err = may_rename_srcdir(a);
  23521. + }
  23522. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  23523. + if (unlikely(err))
  23524. + goto out;
  23525. +
  23526. + d = a->src_dentry;
  23527. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  23528. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  23529. + err = may_rename_srcdir(a);
  23530. + if (unlikely(err)) {
  23531. + au_nhash_wh_free(&a->whlist);
  23532. + a->whlist.nh_num = 0;
  23533. + }
  23534. + }
  23535. +out:
  23536. + return err;
  23537. +}
  23538. +
  23539. +/* ---------------------------------------------------------------------- */
  23540. +
  23541. +/*
  23542. + * simple tests for rename.
  23543. + * following the checks in vfs, plus the parent-child relationship.
  23544. + */
  23545. +static int au_may_ren(struct au_ren_args *a)
  23546. +{
  23547. + int err, isdir;
  23548. + struct inode *h_inode;
  23549. +
  23550. + if (a->src_btop == a->btgt) {
  23551. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  23552. + au_ftest_ren(a->auren_flags, ISDIR_SRC));
  23553. + if (unlikely(err))
  23554. + goto out;
  23555. + err = -EINVAL;
  23556. + if (unlikely(a->src_h_dentry == a->h_trap))
  23557. + goto out;
  23558. + }
  23559. +
  23560. + err = 0;
  23561. + if (a->dst_btop != a->btgt)
  23562. + goto out;
  23563. +
  23564. + err = -ENOTEMPTY;
  23565. + if (unlikely(a->dst_h_dentry == a->h_trap))
  23566. + goto out;
  23567. +
  23568. + err = -EIO;
  23569. + isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
  23570. + if (d_really_is_negative(a->dst_dentry)) {
  23571. + if (d_is_negative(a->dst_h_dentry))
  23572. + err = au_may_add(a->dst_dentry, a->btgt,
  23573. + a->dst_h_parent, isdir);
  23574. + } else {
  23575. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  23576. + goto out;
  23577. + h_inode = d_inode(a->dst_h_dentry);
  23578. + if (vfsub_inode_nlink(h_inode, AU_I_BRANCH))
  23579. + err = au_may_del(a->dst_dentry, a->btgt,
  23580. + a->dst_h_parent, isdir);
  23581. + }
  23582. +
  23583. +out:
  23584. + if (unlikely(err == -ENOENT || err == -EEXIST))
  23585. + err = -EIO;
  23586. + AuTraceErr(err);
  23587. + return err;
  23588. +}
  23589. +
  23590. +/* ---------------------------------------------------------------------- */
  23591. +
  23592. +/*
  23593. + * locking order
  23594. + * (VFS)
  23595. + * - src_dir and dir by lock_rename()
  23596. + * - inode if exists
  23597. + * (aufs)
  23598. + * - lock all
  23599. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  23600. + * + si_read_lock
  23601. + * + di_write_lock2_child()
  23602. + * + di_write_lock_child()
  23603. + * + ii_write_lock_child()
  23604. + * + di_write_lock_child2()
  23605. + * + ii_write_lock_child2()
  23606. + * + src_parent and parent
  23607. + * + di_write_lock_parent()
  23608. + * + ii_write_lock_parent()
  23609. + * + di_write_lock_parent2()
  23610. + * + ii_write_lock_parent2()
  23611. + * + lower src_dir and dir by vfsub_lock_rename()
  23612. + * + verify the every relationships between child and parent. if any
  23613. + * of them failed, unlock all and return -EBUSY.
  23614. + */
  23615. +static void au_ren_unlock(struct au_ren_args *a)
  23616. +{
  23617. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  23618. + a->dst_h_parent, a->dst_hdir);
  23619. + if (au_ftest_ren(a->auren_flags, DIRREN)
  23620. + && a->h_root)
  23621. + au_hn_inode_unlock(a->h_root);
  23622. + if (au_ftest_ren(a->auren_flags, MNT_WRITE))
  23623. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  23624. +}
  23625. +
  23626. +static int au_ren_lock(struct au_ren_args *a)
  23627. +{
  23628. + int err;
  23629. + unsigned int udba;
  23630. +
  23631. + err = 0;
  23632. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  23633. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  23634. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  23635. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  23636. +
  23637. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  23638. + if (unlikely(err))
  23639. + goto out;
  23640. + au_fset_ren(a->auren_flags, MNT_WRITE);
  23641. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  23642. + struct dentry *root;
  23643. + struct inode *dir;
  23644. +
  23645. + /*
  23646. + * sbinfo is already locked, so this ii_read_lock is
  23647. + * unnecessary. but our debugging feature checks it.
  23648. + */
  23649. + root = a->src_inode->i_sb->s_root;
  23650. + if (root != a->src_parent && root != a->dst_parent) {
  23651. + dir = d_inode(root);
  23652. + ii_read_lock_parent3(dir);
  23653. + a->h_root = au_hi(dir, a->btgt);
  23654. + ii_read_unlock(dir);
  23655. + au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
  23656. + }
  23657. + }
  23658. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  23659. + a->dst_h_parent, a->dst_hdir);
  23660. + udba = au_opt_udba(a->src_dentry->d_sb);
  23661. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  23662. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  23663. + err = au_busy_or_stale();
  23664. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  23665. + err = au_h_verify(a->src_h_dentry, udba,
  23666. + d_inode(a->src_h_parent), a->src_h_parent,
  23667. + a->br);
  23668. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  23669. + err = au_h_verify(a->dst_h_dentry, udba,
  23670. + d_inode(a->dst_h_parent), a->dst_h_parent,
  23671. + a->br);
  23672. + if (!err)
  23673. + goto out; /* success */
  23674. +
  23675. + err = au_busy_or_stale();
  23676. + au_ren_unlock(a);
  23677. +
  23678. +out:
  23679. + return err;
  23680. +}
  23681. +
  23682. +/* ---------------------------------------------------------------------- */
  23683. +
  23684. +static void au_ren_refresh_dir(struct au_ren_args *a)
  23685. +{
  23686. + struct inode *dir;
  23687. +
  23688. + dir = a->dst_dir;
  23689. + inode_inc_iversion(dir);
  23690. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  23691. + /* is this updating defined in POSIX? */
  23692. + au_cpup_attr_timesizes(a->src_inode);
  23693. + au_cpup_attr_nlink(dir, /*force*/1);
  23694. + }
  23695. + au_dir_ts(dir, a->btgt);
  23696. +
  23697. + if (a->exchange) {
  23698. + dir = a->src_dir;
  23699. + inode_inc_iversion(dir);
  23700. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  23701. + /* is this updating defined in POSIX? */
  23702. + au_cpup_attr_timesizes(a->dst_inode);
  23703. + au_cpup_attr_nlink(dir, /*force*/1);
  23704. + }
  23705. + au_dir_ts(dir, a->btgt);
  23706. + }
  23707. +
  23708. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  23709. + return;
  23710. +
  23711. + dir = a->src_dir;
  23712. + inode_inc_iversion(dir);
  23713. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
  23714. + au_cpup_attr_nlink(dir, /*force*/1);
  23715. + au_dir_ts(dir, a->btgt);
  23716. +}
  23717. +
  23718. +static void au_ren_refresh(struct au_ren_args *a)
  23719. +{
  23720. + aufs_bindex_t bbot, bindex;
  23721. + struct dentry *d, *h_d;
  23722. + struct inode *i, *h_i;
  23723. + struct super_block *sb;
  23724. +
  23725. + d = a->dst_dentry;
  23726. + d_drop(d);
  23727. + if (a->h_dst)
  23728. + /* already dget-ed by au_ren_or_cpup() */
  23729. + au_set_h_dptr(d, a->btgt, a->h_dst);
  23730. +
  23731. + i = a->dst_inode;
  23732. + if (i) {
  23733. + if (!a->exchange) {
  23734. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
  23735. + vfsub_drop_nlink(i);
  23736. + else {
  23737. + vfsub_dead_dir(i);
  23738. + au_cpup_attr_timesizes(i);
  23739. + }
  23740. + au_update_dbrange(d, /*do_put_zero*/1);
  23741. + } else
  23742. + au_cpup_attr_nlink(i, /*force*/1);
  23743. + } else {
  23744. + bbot = a->btgt;
  23745. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  23746. + au_set_h_dptr(d, bindex, NULL);
  23747. + bbot = au_dbbot(d);
  23748. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  23749. + au_set_h_dptr(d, bindex, NULL);
  23750. + au_update_dbrange(d, /*do_put_zero*/0);
  23751. + }
  23752. +
  23753. + if (a->exchange
  23754. + || au_ftest_ren(a->auren_flags, DIRREN)) {
  23755. + d_drop(a->src_dentry);
  23756. + if (au_ftest_ren(a->auren_flags, DIRREN))
  23757. + au_set_dbwh(a->src_dentry, -1);
  23758. + return;
  23759. + }
  23760. +
  23761. + d = a->src_dentry;
  23762. + au_set_dbwh(d, -1);
  23763. + bbot = au_dbbot(d);
  23764. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  23765. + h_d = au_h_dptr(d, bindex);
  23766. + if (h_d)
  23767. + au_set_h_dptr(d, bindex, NULL);
  23768. + }
  23769. + au_set_dbbot(d, a->btgt);
  23770. +
  23771. + sb = d->d_sb;
  23772. + i = a->src_inode;
  23773. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  23774. + return; /* success */
  23775. +
  23776. + bbot = au_ibbot(i);
  23777. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  23778. + h_i = au_h_iptr(i, bindex);
  23779. + if (h_i) {
  23780. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  23781. + /* ignore this error */
  23782. + au_set_h_iptr(i, bindex, NULL, 0);
  23783. + }
  23784. + }
  23785. + au_set_ibbot(i, a->btgt);
  23786. +}
  23787. +
  23788. +/* ---------------------------------------------------------------------- */
  23789. +
  23790. +/* mainly for link(2) and rename(2) */
  23791. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  23792. +{
  23793. + aufs_bindex_t bdiropq, bwh;
  23794. + struct dentry *parent;
  23795. + struct au_branch *br;
  23796. +
  23797. + parent = dentry->d_parent;
  23798. + IMustLock(d_inode(parent)); /* dir is locked */
  23799. +
  23800. + bdiropq = au_dbdiropq(parent);
  23801. + bwh = au_dbwh(dentry);
  23802. + br = au_sbr(dentry->d_sb, btgt);
  23803. + if (au_br_rdonly(br)
  23804. + || (0 <= bdiropq && bdiropq < btgt)
  23805. + || (0 <= bwh && bwh < btgt))
  23806. + btgt = -1;
  23807. +
  23808. + AuDbg("btgt %d\n", btgt);
  23809. + return btgt;
  23810. +}
  23811. +
  23812. +/* sets src_btop, dst_btop and btgt */
  23813. +static int au_ren_wbr(struct au_ren_args *a)
  23814. +{
  23815. + int err;
  23816. + struct au_wr_dir_args wr_dir_args = {
  23817. + /* .force_btgt = -1, */
  23818. + .flags = AuWrDir_ADD_ENTRY
  23819. + };
  23820. +
  23821. + a->src_btop = au_dbtop(a->src_dentry);
  23822. + a->dst_btop = au_dbtop(a->dst_dentry);
  23823. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23824. + || au_ftest_ren(a->auren_flags, ISDIR_DST))
  23825. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  23826. + wr_dir_args.force_btgt = a->src_btop;
  23827. + if (a->dst_inode && a->dst_btop < a->src_btop)
  23828. + wr_dir_args.force_btgt = a->dst_btop;
  23829. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  23830. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  23831. + a->btgt = err;
  23832. + if (a->exchange)
  23833. + au_update_dbtop(a->dst_dentry);
  23834. +
  23835. + return err;
  23836. +}
  23837. +
  23838. +static void au_ren_dt(struct au_ren_args *a)
  23839. +{
  23840. + a->h_path.dentry = a->src_h_parent;
  23841. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  23842. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
  23843. + a->h_path.dentry = a->dst_h_parent;
  23844. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  23845. + }
  23846. +
  23847. + au_fclr_ren(a->auren_flags, DT_DSTDIR);
  23848. + if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23849. + && !a->exchange)
  23850. + return;
  23851. +
  23852. + a->h_path.dentry = a->src_h_dentry;
  23853. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  23854. + if (d_is_positive(a->dst_h_dentry)) {
  23855. + au_fset_ren(a->auren_flags, DT_DSTDIR);
  23856. + a->h_path.dentry = a->dst_h_dentry;
  23857. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  23858. + }
  23859. +}
  23860. +
  23861. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  23862. +{
  23863. + struct dentry *h_d;
  23864. + struct inode *h_inode;
  23865. +
  23866. + au_dtime_revert(a->src_dt + AuPARENT);
  23867. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
  23868. + au_dtime_revert(a->dst_dt + AuPARENT);
  23869. +
  23870. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
  23871. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  23872. + h_inode = d_inode(h_d);
  23873. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  23874. + au_dtime_revert(a->src_dt + AuCHILD);
  23875. + inode_unlock(h_inode);
  23876. +
  23877. + if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
  23878. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  23879. + h_inode = d_inode(h_d);
  23880. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  23881. + au_dtime_revert(a->dst_dt + AuCHILD);
  23882. + inode_unlock(h_inode);
  23883. + }
  23884. + }
  23885. +}
  23886. +
  23887. +/* ---------------------------------------------------------------------- */
  23888. +
  23889. +int aufs_rename(struct mnt_idmap *idmap,
  23890. + struct inode *_src_dir, struct dentry *_src_dentry,
  23891. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  23892. + unsigned int _flags)
  23893. +{
  23894. + int err, lock_flags;
  23895. + void *rev;
  23896. + /* reduce stack space */
  23897. + struct au_ren_args *a;
  23898. + struct au_pin pin;
  23899. +
  23900. + AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
  23901. + IMustLock(_src_dir);
  23902. + IMustLock(_dst_dir);
  23903. +
  23904. + err = -EINVAL;
  23905. + if (unlikely(_flags & RENAME_WHITEOUT))
  23906. + goto out;
  23907. +
  23908. + err = -ENOMEM;
  23909. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  23910. + a = kzalloc(sizeof(*a), GFP_NOFS);
  23911. + if (unlikely(!a))
  23912. + goto out;
  23913. +
  23914. + a->flags = _flags;
  23915. + BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
  23916. + && RENAME_EXCHANGE > U8_MAX);
  23917. + a->exchange = _flags & RENAME_EXCHANGE;
  23918. + a->src_dir = _src_dir;
  23919. + a->src_dentry = _src_dentry;
  23920. + a->src_inode = NULL;
  23921. + if (d_really_is_positive(a->src_dentry))
  23922. + a->src_inode = d_inode(a->src_dentry);
  23923. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  23924. + a->dst_dir = _dst_dir;
  23925. + a->dst_dentry = _dst_dentry;
  23926. + a->dst_inode = NULL;
  23927. + if (d_really_is_positive(a->dst_dentry))
  23928. + a->dst_inode = d_inode(a->dst_dentry);
  23929. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  23930. + if (a->dst_inode) {
  23931. + /*
  23932. + * if EXCHANGE && src is non-dir && dst is dir,
  23933. + * dst is not locked.
  23934. + */
  23935. + /* IMustLock(a->dst_inode); */
  23936. + au_igrab(a->dst_inode);
  23937. + }
  23938. +
  23939. + err = -ENOTDIR;
  23940. + lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  23941. + if (d_is_dir(a->src_dentry)) {
  23942. + au_fset_ren(a->auren_flags, ISDIR_SRC);
  23943. + if (unlikely(!a->exchange
  23944. + && d_really_is_positive(a->dst_dentry)
  23945. + && !d_is_dir(a->dst_dentry)))
  23946. + goto out_free;
  23947. + lock_flags |= AuLock_DIRS;
  23948. + }
  23949. + if (a->dst_inode && d_is_dir(a->dst_dentry)) {
  23950. + au_fset_ren(a->auren_flags, ISDIR_DST);
  23951. + if (unlikely(!a->exchange
  23952. + && d_really_is_positive(a->src_dentry)
  23953. + && !d_is_dir(a->src_dentry)))
  23954. + goto out_free;
  23955. + lock_flags |= AuLock_DIRS;
  23956. + }
  23957. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  23958. + lock_flags);
  23959. + if (unlikely(err))
  23960. + goto out_free;
  23961. +
  23962. + err = au_d_hashed_positive(a->src_dentry);
  23963. + if (unlikely(err))
  23964. + goto out_unlock;
  23965. + err = -ENOENT;
  23966. + if (a->dst_inode) {
  23967. + /*
  23968. + * If it is a dir, VFS unhash it before this
  23969. + * function. It means we cannot rely upon d_unhashed().
  23970. + */
  23971. + if (unlikely(!vfsub_inode_nlink(a->dst_inode, AU_I_AUFS)))
  23972. + goto out_unlock;
  23973. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  23974. + err = au_d_hashed_positive(a->dst_dentry);
  23975. + if (unlikely(err && !a->exchange))
  23976. + goto out_unlock;
  23977. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  23978. + goto out_unlock;
  23979. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  23980. + goto out_unlock;
  23981. +
  23982. + /*
  23983. + * is it possible?
  23984. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  23985. + * there may exist a problem somewhere else.
  23986. + */
  23987. + err = -EINVAL;
  23988. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  23989. + goto out_unlock;
  23990. +
  23991. + au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
  23992. + di_write_lock_parent(a->dst_parent);
  23993. +
  23994. + /* which branch we process */
  23995. + err = au_ren_wbr(a);
  23996. + if (unlikely(err < 0))
  23997. + goto out_parent;
  23998. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  23999. + a->h_path.mnt = au_br_mnt(a->br);
  24000. +
  24001. + /* are they available to be renamed */
  24002. + err = au_ren_may_dir(a);
  24003. + if (unlikely(err))
  24004. + goto out_children;
  24005. +
  24006. + /* prepare the writable parent dir on the same branch */
  24007. + if (a->dst_btop == a->btgt) {
  24008. + au_fset_ren(a->auren_flags, WHDST);
  24009. + } else {
  24010. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  24011. + if (unlikely(err))
  24012. + goto out_children;
  24013. + }
  24014. +
  24015. + err = 0;
  24016. + if (!a->exchange) {
  24017. + if (a->src_dir != a->dst_dir) {
  24018. + /*
  24019. + * this temporary unlock is safe,
  24020. + * because both dir->i_mutex are locked.
  24021. + */
  24022. + di_write_unlock(a->dst_parent);
  24023. + di_write_lock_parent(a->src_parent);
  24024. + err = au_wr_dir_need_wh(a->src_dentry,
  24025. + au_ftest_ren(a->auren_flags,
  24026. + ISDIR_SRC),
  24027. + &a->btgt);
  24028. + di_write_unlock(a->src_parent);
  24029. + di_write_lock2_parent(a->src_parent, a->dst_parent,
  24030. + /*isdir*/1);
  24031. + au_fclr_ren(a->auren_flags, ISSAMEDIR);
  24032. + } else
  24033. + err = au_wr_dir_need_wh(a->src_dentry,
  24034. + au_ftest_ren(a->auren_flags,
  24035. + ISDIR_SRC),
  24036. + &a->btgt);
  24037. + }
  24038. + if (unlikely(err < 0))
  24039. + goto out_children;
  24040. + if (err)
  24041. + au_fset_ren(a->auren_flags, WHSRC);
  24042. +
  24043. + /* cpup src */
  24044. + if (a->src_btop != a->btgt) {
  24045. + err = au_pin(&pin, a->src_dentry, a->btgt,
  24046. + au_opt_udba(a->src_dentry->d_sb),
  24047. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  24048. + if (!err) {
  24049. + struct au_cp_generic cpg = {
  24050. + .dentry = a->src_dentry,
  24051. + .bdst = a->btgt,
  24052. + .bsrc = a->src_btop,
  24053. + .len = -1,
  24054. + .pin = &pin,
  24055. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  24056. + };
  24057. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  24058. + err = au_sio_cpup_simple(&cpg);
  24059. + au_unpin(&pin);
  24060. + }
  24061. + if (unlikely(err))
  24062. + goto out_children;
  24063. + a->src_btop = a->btgt;
  24064. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  24065. + if (!a->exchange)
  24066. + au_fset_ren(a->auren_flags, WHSRC);
  24067. + }
  24068. +
  24069. + /* cpup dst */
  24070. + if (a->exchange && a->dst_inode
  24071. + && a->dst_btop != a->btgt) {
  24072. + err = au_pin(&pin, a->dst_dentry, a->btgt,
  24073. + au_opt_udba(a->dst_dentry->d_sb),
  24074. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  24075. + if (!err) {
  24076. + struct au_cp_generic cpg = {
  24077. + .dentry = a->dst_dentry,
  24078. + .bdst = a->btgt,
  24079. + .bsrc = a->dst_btop,
  24080. + .len = -1,
  24081. + .pin = &pin,
  24082. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  24083. + };
  24084. + err = au_sio_cpup_simple(&cpg);
  24085. + au_unpin(&pin);
  24086. + }
  24087. + if (unlikely(err))
  24088. + goto out_children;
  24089. + a->dst_btop = a->btgt;
  24090. + a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
  24091. + }
  24092. +
  24093. + /* lock them all */
  24094. + err = au_ren_lock(a);
  24095. + if (unlikely(err))
  24096. + /* leave the copied-up one */
  24097. + goto out_children;
  24098. +
  24099. + if (!a->exchange) {
  24100. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  24101. + err = au_may_ren(a);
  24102. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  24103. + err = -ENAMETOOLONG;
  24104. + if (unlikely(err))
  24105. + goto out_hdir;
  24106. + }
  24107. +
  24108. + /* store timestamps to be revertible */
  24109. + au_ren_dt(a);
  24110. +
  24111. + /* store dirren info */
  24112. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  24113. + err = au_dr_rename(a->src_dentry, a->btgt,
  24114. + &a->dst_dentry->d_name, &rev);
  24115. + AuTraceErr(err);
  24116. + if (unlikely(err))
  24117. + goto out_dt;
  24118. + }
  24119. +
  24120. + /* here we go */
  24121. + err = do_rename(a);
  24122. + if (unlikely(err))
  24123. + goto out_dirren;
  24124. +
  24125. + if (au_ftest_ren(a->auren_flags, DIRREN))
  24126. + au_dr_rename_fin(a->src_dentry, a->btgt, rev);
  24127. +
  24128. + /* update dir attributes */
  24129. + au_ren_refresh_dir(a);
  24130. +
  24131. + /* dput/iput all lower dentries */
  24132. + au_ren_refresh(a);
  24133. +
  24134. + goto out_hdir; /* success */
  24135. +
  24136. +out_dirren:
  24137. + if (au_ftest_ren(a->auren_flags, DIRREN))
  24138. + au_dr_rename_rev(a->src_dentry, a->btgt, rev);
  24139. +out_dt:
  24140. + au_ren_rev_dt(err, a);
  24141. +out_hdir:
  24142. + au_ren_unlock(a);
  24143. +out_children:
  24144. + au_nhash_wh_free(&a->whlist);
  24145. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  24146. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  24147. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  24148. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  24149. + }
  24150. +out_parent:
  24151. + if (!err) {
  24152. + if (d_unhashed(a->src_dentry))
  24153. + au_fset_ren(a->auren_flags, DROPPED_SRC);
  24154. + if (d_unhashed(a->dst_dentry))
  24155. + au_fset_ren(a->auren_flags, DROPPED_DST);
  24156. + if (!a->exchange)
  24157. + d_move(a->src_dentry, a->dst_dentry);
  24158. + else {
  24159. + d_exchange(a->src_dentry, a->dst_dentry);
  24160. + if (au_ftest_ren(a->auren_flags, DROPPED_DST))
  24161. + d_drop(a->dst_dentry);
  24162. + }
  24163. + if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
  24164. + d_drop(a->src_dentry);
  24165. + } else {
  24166. + au_update_dbtop(a->dst_dentry);
  24167. + if (!a->dst_inode)
  24168. + d_drop(a->dst_dentry);
  24169. + }
  24170. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  24171. + di_write_unlock(a->dst_parent);
  24172. + else
  24173. + di_write_unlock2(a->src_parent, a->dst_parent);
  24174. +out_unlock:
  24175. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  24176. +out_free:
  24177. + iput(a->dst_inode);
  24178. + if (a->thargs)
  24179. + au_whtmp_rmdir_free(a->thargs);
  24180. + au_kfree_rcu(a);
  24181. +out:
  24182. + AuTraceErr(err);
  24183. + return err;
  24184. +}
  24185. diff -Naur null/fs/aufs/Kconfig linux-6.6.63/fs/aufs/Kconfig
  24186. --- /dev/null
  24187. +++ linux-6.6.63/fs/aufs/Kconfig 2024-12-18 15:12:59.292519626 +0300
  24188. @@ -0,0 +1,199 @@
  24189. +# SPDX-License-Identifier: GPL-2.0
  24190. +config AUFS_FS
  24191. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  24192. + help
  24193. + Aufs is a stackable unification filesystem such as Unionfs,
  24194. + which unifies several directories and provides a merged single
  24195. + directory.
  24196. + In the early days, aufs was entirely re-designed and
  24197. + re-implemented Unionfs Version 1.x series. Introducing many
  24198. + original ideas, approaches and improvements, it becomes totally
  24199. + different from Unionfs while keeping the basic features.
  24200. +
  24201. +if AUFS_FS
  24202. +choice
  24203. + prompt "Maximum number of branches"
  24204. + default AUFS_BRANCH_MAX_127
  24205. + help
  24206. + Specifies the maximum number of branches (or member directories)
  24207. + in a single aufs. The larger value consumes more system
  24208. + resources and has a minor impact to performance.
  24209. +config AUFS_BRANCH_MAX_127
  24210. + bool "127"
  24211. + help
  24212. + Specifies the maximum number of branches (or member directories)
  24213. + in a single aufs. The larger value consumes more system
  24214. + resources and has a minor impact to performance.
  24215. +config AUFS_BRANCH_MAX_511
  24216. + bool "511"
  24217. + help
  24218. + Specifies the maximum number of branches (or member directories)
  24219. + in a single aufs. The larger value consumes more system
  24220. + resources and has a minor impact to performance.
  24221. +config AUFS_BRANCH_MAX_1023
  24222. + bool "1023"
  24223. + help
  24224. + Specifies the maximum number of branches (or member directories)
  24225. + in a single aufs. The larger value consumes more system
  24226. + resources and has a minor impact to performance.
  24227. +config AUFS_BRANCH_MAX_32767
  24228. + bool "32767"
  24229. + help
  24230. + Specifies the maximum number of branches (or member directories)
  24231. + in a single aufs. The larger value consumes more system
  24232. + resources and has a minor impact to performance.
  24233. +endchoice
  24234. +
  24235. +config AUFS_SBILIST
  24236. + bool
  24237. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  24238. + default y
  24239. + help
  24240. + Automatic configuration for internal use.
  24241. + When aufs supports Magic SysRq or /proc, enabled automatically.
  24242. +
  24243. +config AUFS_HNOTIFY
  24244. + bool "Detect direct branch access (bypassing aufs)"
  24245. + help
  24246. + If you want to modify files on branches directly, eg. bypassing aufs,
  24247. + and want aufs to detect the changes of them fully, then enable this
  24248. + option and use 'udba=notify' mount option.
  24249. + Currently there is only one available configuration, "fsnotify".
  24250. + It will have a negative impact to the performance.
  24251. + See detail in aufs.5.
  24252. +
  24253. +choice
  24254. + prompt "method" if AUFS_HNOTIFY
  24255. + default AUFS_HFSNOTIFY
  24256. +config AUFS_HFSNOTIFY
  24257. + bool "fsnotify"
  24258. + select FSNOTIFY
  24259. +endchoice
  24260. +
  24261. +config AUFS_EXPORT
  24262. + bool "NFS-exportable aufs"
  24263. + depends on EXPORTFS
  24264. + help
  24265. + If you want to export your mounted aufs via NFS, then enable this
  24266. + option. There are several requirements for this configuration.
  24267. + See detail in aufs.5.
  24268. +
  24269. +config AUFS_INO_T_64
  24270. + bool
  24271. + depends on AUFS_EXPORT
  24272. + depends on 64BIT && !(ALPHA || S390)
  24273. + default y
  24274. + help
  24275. + Automatic configuration for internal use.
  24276. + /* typedef unsigned long/int __kernel_ino_t */
  24277. + /* alpha and s390x are int */
  24278. +
  24279. +config AUFS_XATTR
  24280. + bool "support for XATTR/EA (including Security Labels)"
  24281. + help
  24282. + If your branch fs supports XATTR/EA and you want to make them
  24283. + available in aufs too, then enable this opsion and specify the
  24284. + branch attributes for EA.
  24285. + See detail in aufs.5.
  24286. +
  24287. +config AUFS_FHSM
  24288. + bool "File-based Hierarchical Storage Management"
  24289. + help
  24290. + Hierarchical Storage Management (or HSM) is a well-known feature
  24291. + in the storage world. Aufs provides this feature as file-based.
  24292. + with multiple branches.
  24293. + These multiple branches are prioritized, ie. the topmost one
  24294. + should be the fastest drive and be used heavily.
  24295. +
  24296. +config AUFS_RDU
  24297. + bool "Readdir in userspace"
  24298. + help
  24299. + Aufs has two methods to provide a merged view for a directory,
  24300. + by a user-space library and by kernel-space natively. The latter
  24301. + is always enabled but sometimes large and slow.
  24302. + If you enable this option, install the library in aufs2-util
  24303. + package, and set some environment variables for your readdir(3),
  24304. + then the work will be handled in user-space which generally
  24305. + shows better performance in most cases.
  24306. + See detail in aufs.5.
  24307. +
  24308. +config AUFS_DIRREN
  24309. + bool "Workaround for rename(2)-ing a directory"
  24310. + help
  24311. + By default, aufs returns EXDEV error in renameing a dir who has
  24312. + his child on the lower branch, since it is a bad idea to issue
  24313. + rename(2) internally for every lower branch. But user may not
  24314. + accept this behaviour. So here is a workaround to allow such
  24315. + rename(2) and store some extra information on the writable
  24316. + branch. Obviously this costs high (and I don't like it).
  24317. + To use this feature, you need to enable this configuration AND
  24318. + to specify the mount option `dirren.'
  24319. + See details in aufs.5 and the design documents.
  24320. +
  24321. +config AUFS_SHWH
  24322. + bool "Show whiteouts"
  24323. + help
  24324. + If you want to make the whiteouts in aufs visible, then enable
  24325. + this option and specify 'shwh' mount option. Although it may
  24326. + sounds like philosophy or something, but in technically it
  24327. + simply shows the name of whiteout with keeping its behaviour.
  24328. +
  24329. +config AUFS_BR_RAMFS
  24330. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  24331. + help
  24332. + If you want to use ramfs as an aufs branch fs, then enable this
  24333. + option. Generally tmpfs is recommended.
  24334. + Aufs prohibited them to be a branch fs by default, because
  24335. + initramfs becomes unusable after switch_root or something
  24336. + generally. If you sets initramfs as an aufs branch and boot your
  24337. + system by switch_root, you will meet a problem easily since the
  24338. + files in initramfs may be inaccessible.
  24339. + Unless you are going to use ramfs as an aufs branch fs without
  24340. + switch_root or something, leave it N.
  24341. +
  24342. +config AUFS_BR_FUSE
  24343. + bool "Fuse fs as an aufs branch"
  24344. + depends on FUSE_FS
  24345. + select AUFS_POLL
  24346. + help
  24347. + If you want to use fuse-based userspace filesystem as an aufs
  24348. + branch fs, then enable this option.
  24349. + It implements the internal poll(2) operation which is
  24350. + implemented by fuse only (curretnly).
  24351. +
  24352. +config AUFS_POLL
  24353. + bool
  24354. + help
  24355. + Automatic configuration for internal use.
  24356. +
  24357. +config AUFS_BR_HFSPLUS
  24358. + bool "Hfsplus as an aufs branch"
  24359. + depends on HFSPLUS_FS
  24360. + default y
  24361. + help
  24362. + If you want to use hfsplus fs as an aufs branch fs, then enable
  24363. + this option. This option introduces a small overhead at
  24364. + copying-up a file on hfsplus.
  24365. +
  24366. +config AUFS_BDEV_LOOP
  24367. + bool
  24368. + depends on BLK_DEV_LOOP
  24369. + default y
  24370. + help
  24371. + Automatic configuration for internal use.
  24372. + Convert =[ym] into =y.
  24373. +
  24374. +config AUFS_DEBUG
  24375. + bool "Debug aufs"
  24376. + help
  24377. + Enable this to compile aufs internal debug code.
  24378. + It will have a negative impact to the performance.
  24379. +
  24380. +config AUFS_MAGIC_SYSRQ
  24381. + bool
  24382. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  24383. + default y
  24384. + help
  24385. + Automatic configuration for internal use.
  24386. + When aufs supports Magic SysRq, enabled automatically.
  24387. +endif
  24388. diff -Naur null/fs/aufs/lcnt.h linux-6.6.63/fs/aufs/lcnt.h
  24389. --- /dev/null
  24390. +++ linux-6.6.63/fs/aufs/lcnt.h 2024-12-18 15:12:59.298519551 +0300
  24391. @@ -0,0 +1,186 @@
  24392. +/* SPDX-License-Identifier: GPL-2.0 */
  24393. +/*
  24394. + * Copyright (C) 2018-2022 Junjiro R. Okajima
  24395. + *
  24396. + * This program is free software; you can redistribute it and/or modify
  24397. + * it under the terms of the GNU General Public License as published by
  24398. + * the Free Software Foundation; either version 2 of the License, or
  24399. + * (at your option) any later version.
  24400. + *
  24401. + * This program is distributed in the hope that it will be useful,
  24402. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24403. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24404. + * GNU General Public License for more details.
  24405. + *
  24406. + * You should have received a copy of the GNU General Public License
  24407. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24408. + */
  24409. +
  24410. +/*
  24411. + * simple long counter wrapper
  24412. + */
  24413. +
  24414. +#ifndef __AUFS_LCNT_H__
  24415. +#define __AUFS_LCNT_H__
  24416. +
  24417. +#ifdef __KERNEL__
  24418. +
  24419. +#include "debug.h"
  24420. +
  24421. +#define AuLCntATOMIC 1
  24422. +#define AuLCntPCPUCNT 2
  24423. +/*
  24424. + * why does percpu_refcount require extra synchronize_rcu()s in
  24425. + * au_br_do_free()
  24426. + */
  24427. +#define AuLCntPCPUREF 3
  24428. +
  24429. +/* #define AuLCntChosen AuLCntATOMIC */
  24430. +#define AuLCntChosen AuLCntPCPUCNT
  24431. +/* #define AuLCntChosen AuLCntPCPUREF */
  24432. +
  24433. +#if AuLCntChosen == AuLCntATOMIC
  24434. +#include <linux/atomic.h>
  24435. +
  24436. +typedef atomic_long_t au_lcnt_t;
  24437. +
  24438. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  24439. +{
  24440. + atomic_long_set(cnt, 0);
  24441. + return 0;
  24442. +}
  24443. +
  24444. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24445. +{
  24446. + /* empty */
  24447. +}
  24448. +
  24449. +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
  24450. + int do_sync __maybe_unused)
  24451. +{
  24452. + /* empty */
  24453. +}
  24454. +
  24455. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24456. +{
  24457. + atomic_long_inc(cnt);
  24458. +}
  24459. +
  24460. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24461. +{
  24462. + atomic_long_dec(cnt);
  24463. +}
  24464. +
  24465. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  24466. +{
  24467. + return atomic_long_read(cnt);
  24468. +}
  24469. +#endif
  24470. +
  24471. +#if AuLCntChosen == AuLCntPCPUCNT
  24472. +#include <linux/percpu_counter.h>
  24473. +
  24474. +typedef struct percpu_counter au_lcnt_t;
  24475. +
  24476. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  24477. +{
  24478. + return percpu_counter_init(cnt, 0, GFP_NOFS);
  24479. +}
  24480. +
  24481. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24482. +{
  24483. + /* empty */
  24484. +}
  24485. +
  24486. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
  24487. +{
  24488. + percpu_counter_destroy(cnt);
  24489. +}
  24490. +
  24491. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24492. +{
  24493. + percpu_counter_inc(cnt);
  24494. +}
  24495. +
  24496. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24497. +{
  24498. + percpu_counter_dec(cnt);
  24499. +}
  24500. +
  24501. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  24502. +{
  24503. + s64 n;
  24504. +
  24505. + n = percpu_counter_sum(cnt);
  24506. + BUG_ON(n < 0);
  24507. + if (LONG_MAX != LLONG_MAX
  24508. + && n > LONG_MAX)
  24509. + AuWarn1("%s\n", "wrap-around");
  24510. +
  24511. + return n;
  24512. +}
  24513. +#endif
  24514. +
  24515. +#if AuLCntChosen == AuLCntPCPUREF
  24516. +#include <linux/percpu-refcount.h>
  24517. +
  24518. +typedef struct percpu_ref au_lcnt_t;
  24519. +
  24520. +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
  24521. +{
  24522. + if (!release)
  24523. + release = percpu_ref_exit;
  24524. + return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
  24525. +}
  24526. +
  24527. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24528. +{
  24529. + synchronize_rcu();
  24530. +}
  24531. +
  24532. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
  24533. +{
  24534. + percpu_ref_kill(cnt);
  24535. + if (do_sync)
  24536. + au_lcnt_wait_for_fin(cnt);
  24537. +}
  24538. +
  24539. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24540. +{
  24541. + percpu_ref_get(cnt);
  24542. +}
  24543. +
  24544. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24545. +{
  24546. + percpu_ref_put(cnt);
  24547. +}
  24548. +
  24549. +/*
  24550. + * avoid calling this func as possible.
  24551. + */
  24552. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
  24553. +{
  24554. + long l;
  24555. +
  24556. + percpu_ref_switch_to_atomic_sync(cnt);
  24557. + l = atomic_long_read(&cnt->count);
  24558. + if (do_rev)
  24559. + percpu_ref_switch_to_percpu(cnt);
  24560. +
  24561. + /* percpu_ref is initialized by 1 instead of 0 */
  24562. + return l - 1;
  24563. +}
  24564. +#endif
  24565. +
  24566. +#ifdef CONFIG_AUFS_DEBUG
  24567. +#define AuLCntZero(val) do { \
  24568. + long l = val; \
  24569. + if (l) \
  24570. + AuDbg("%s = %ld\n", #val, l); \
  24571. +} while (0)
  24572. +#else
  24573. +AuStubVoid(AuLCntZero, long val __maybe_unused);
  24574. +#endif
  24575. +
  24576. +#endif /* __KERNEL__ */
  24577. +#endif /* __AUFS_LCNT_H__ */
  24578. diff -Naur null/fs/aufs/loop.c linux-6.6.63/fs/aufs/loop.c
  24579. --- /dev/null
  24580. +++ linux-6.6.63/fs/aufs/loop.c 2024-12-18 15:12:59.298519551 +0300
  24581. @@ -0,0 +1,148 @@
  24582. +// SPDX-License-Identifier: GPL-2.0
  24583. +/*
  24584. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  24585. + *
  24586. + * This program is free software; you can redistribute it and/or modify
  24587. + * it under the terms of the GNU General Public License as published by
  24588. + * the Free Software Foundation; either version 2 of the License, or
  24589. + * (at your option) any later version.
  24590. + *
  24591. + * This program is distributed in the hope that it will be useful,
  24592. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24593. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24594. + * GNU General Public License for more details.
  24595. + *
  24596. + * You should have received a copy of the GNU General Public License
  24597. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24598. + */
  24599. +
  24600. +/*
  24601. + * support for loopback block device as a branch
  24602. + */
  24603. +
  24604. +#include "aufs.h"
  24605. +
  24606. +/* added into drivers/block/loop.c */
  24607. +static struct file *(*backing_file_func)(struct super_block *sb);
  24608. +
  24609. +/*
  24610. + * test if two lower dentries have overlapping branches.
  24611. + */
  24612. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  24613. +{
  24614. + struct super_block *h_sb;
  24615. + struct file *backing_file;
  24616. +
  24617. + if (unlikely(!backing_file_func)) {
  24618. + /* don't load "loop" module here */
  24619. + backing_file_func = symbol_get(loop_backing_file);
  24620. + if (unlikely(!backing_file_func))
  24621. + /* "loop" module is not loaded */
  24622. + return 0;
  24623. + }
  24624. +
  24625. + h_sb = h_adding->d_sb;
  24626. + backing_file = backing_file_func(h_sb);
  24627. + if (!backing_file)
  24628. + return 0;
  24629. +
  24630. + h_adding = backing_file->f_path.dentry;
  24631. + /*
  24632. + * h_adding can be local NFS.
  24633. + * in this case aufs cannot detect the loop.
  24634. + */
  24635. + if (unlikely(h_adding->d_sb == sb))
  24636. + return 1;
  24637. + return !!au_test_subdir(h_adding, sb->s_root);
  24638. +}
  24639. +
  24640. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  24641. +int au_test_loopback_kthread(void)
  24642. +{
  24643. + int ret;
  24644. + struct task_struct *tsk = current;
  24645. + char c, comm[sizeof(tsk->comm)];
  24646. +
  24647. + ret = 0;
  24648. + if (tsk->flags & PF_KTHREAD) {
  24649. + get_task_comm(comm, tsk);
  24650. + c = comm[4];
  24651. + ret = ('0' <= c && c <= '9'
  24652. + && !strncmp(comm, "loop", 4));
  24653. + }
  24654. +
  24655. + return ret;
  24656. +}
  24657. +
  24658. +/* ---------------------------------------------------------------------- */
  24659. +
  24660. +#define au_warn_loopback_step 16
  24661. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  24662. +static unsigned long *au_warn_loopback_array;
  24663. +
  24664. +void au_warn_loopback(struct super_block *h_sb)
  24665. +{
  24666. + int i, new_nelem;
  24667. + unsigned long *a, magic;
  24668. + static DEFINE_SPINLOCK(spin);
  24669. +
  24670. + magic = h_sb->s_magic;
  24671. + spin_lock(&spin);
  24672. + a = au_warn_loopback_array;
  24673. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  24674. + if (a[i] == magic) {
  24675. + spin_unlock(&spin);
  24676. + return;
  24677. + }
  24678. +
  24679. + /* h_sb is new to us, print it */
  24680. + if (i < au_warn_loopback_nelem) {
  24681. + a[i] = magic;
  24682. + goto pr;
  24683. + }
  24684. +
  24685. + /* expand the array */
  24686. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  24687. + a = au_kzrealloc(au_warn_loopback_array,
  24688. + au_warn_loopback_nelem * sizeof(unsigned long),
  24689. + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
  24690. + /*may_shrink*/0);
  24691. + if (a) {
  24692. + au_warn_loopback_nelem = new_nelem;
  24693. + au_warn_loopback_array = a;
  24694. + a[i] = magic;
  24695. + goto pr;
  24696. + }
  24697. +
  24698. + spin_unlock(&spin);
  24699. + AuWarn1("realloc failed, ignored\n");
  24700. + return;
  24701. +
  24702. +pr:
  24703. + spin_unlock(&spin);
  24704. + pr_warn("you may want to try another patch for loopback file "
  24705. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  24706. +}
  24707. +
  24708. +int au_loopback_init(void)
  24709. +{
  24710. + int err;
  24711. + struct super_block *sb __maybe_unused;
  24712. +
  24713. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
  24714. +
  24715. + err = 0;
  24716. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  24717. + sizeof(unsigned long), GFP_NOFS);
  24718. + if (unlikely(!au_warn_loopback_array))
  24719. + err = -ENOMEM;
  24720. +
  24721. + return err;
  24722. +}
  24723. +
  24724. +void au_loopback_fin(void)
  24725. +{
  24726. + if (backing_file_func)
  24727. + symbol_put(loop_backing_file);
  24728. + au_kfree_try_rcu(au_warn_loopback_array);
  24729. +}
  24730. diff -Naur null/fs/aufs/loop.h linux-6.6.63/fs/aufs/loop.h
  24731. --- /dev/null
  24732. +++ linux-6.6.63/fs/aufs/loop.h 2024-12-18 15:12:59.298519551 +0300
  24733. @@ -0,0 +1,55 @@
  24734. +/* SPDX-License-Identifier: GPL-2.0 */
  24735. +/*
  24736. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  24737. + *
  24738. + * This program is free software; you can redistribute it and/or modify
  24739. + * it under the terms of the GNU General Public License as published by
  24740. + * the Free Software Foundation; either version 2 of the License, or
  24741. + * (at your option) any later version.
  24742. + *
  24743. + * This program is distributed in the hope that it will be useful,
  24744. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24745. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24746. + * GNU General Public License for more details.
  24747. + *
  24748. + * You should have received a copy of the GNU General Public License
  24749. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24750. + */
  24751. +
  24752. +/*
  24753. + * support for loopback mount as a branch
  24754. + */
  24755. +
  24756. +#ifndef __AUFS_LOOP_H__
  24757. +#define __AUFS_LOOP_H__
  24758. +
  24759. +#ifdef __KERNEL__
  24760. +
  24761. +struct dentry;
  24762. +struct super_block;
  24763. +
  24764. +#ifdef CONFIG_AUFS_BDEV_LOOP
  24765. +/* drivers/block/loop.c */
  24766. +struct file *loop_backing_file(struct super_block *sb);
  24767. +
  24768. +/* loop.c */
  24769. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  24770. +int au_test_loopback_kthread(void);
  24771. +void au_warn_loopback(struct super_block *h_sb);
  24772. +
  24773. +int au_loopback_init(void);
  24774. +void au_loopback_fin(void);
  24775. +#else
  24776. +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
  24777. +
  24778. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  24779. + struct dentry *h_adding)
  24780. +AuStubInt0(au_test_loopback_kthread, void)
  24781. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  24782. +
  24783. +AuStubInt0(au_loopback_init, void)
  24784. +AuStubVoid(au_loopback_fin, void)
  24785. +#endif /* BLK_DEV_LOOP */
  24786. +
  24787. +#endif /* __KERNEL__ */
  24788. +#endif /* __AUFS_LOOP_H__ */
  24789. diff -Naur null/fs/aufs/magic.mk linux-6.6.63/fs/aufs/magic.mk
  24790. --- /dev/null
  24791. +++ linux-6.6.63/fs/aufs/magic.mk 2024-12-18 15:12:59.298519551 +0300
  24792. @@ -0,0 +1,31 @@
  24793. +# SPDX-License-Identifier: GPL-2.0
  24794. +
  24795. +# defined in ${srctree}/fs/fuse/inode.c
  24796. +# tristate
  24797. +ifdef CONFIG_FUSE_FS
  24798. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  24799. +endif
  24800. +
  24801. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  24802. +# tristate
  24803. +ifdef CONFIG_XFS_FS
  24804. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  24805. +endif
  24806. +
  24807. +# defined in ${srctree}/fs/configfs/mount.c
  24808. +# tristate
  24809. +ifdef CONFIG_CONFIGFS_FS
  24810. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  24811. +endif
  24812. +
  24813. +# defined in ${srctree}/fs/ubifs/ubifs.h
  24814. +# tristate
  24815. +ifdef CONFIG_UBIFS_FS
  24816. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  24817. +endif
  24818. +
  24819. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  24820. +# tristate
  24821. +ifdef CONFIG_HFSPLUS_FS
  24822. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  24823. +endif
  24824. diff -Naur null/fs/aufs/Makefile linux-6.6.63/fs/aufs/Makefile
  24825. --- /dev/null
  24826. +++ linux-6.6.63/fs/aufs/Makefile 2024-12-18 15:12:59.293519613 +0300
  24827. @@ -0,0 +1,46 @@
  24828. +# SPDX-License-Identifier: GPL-2.0
  24829. +
  24830. +include ${src}/magic.mk
  24831. +ifeq (${CONFIG_AUFS_FS},m)
  24832. +include ${src}/conf.mk
  24833. +endif
  24834. +-include ${src}/priv_def.mk
  24835. +
  24836. +# cf. include/linux/kernel.h
  24837. +# enable pr_debug
  24838. +ccflags-y += -DDEBUG
  24839. +# sparse requires the full pathname
  24840. +ifdef M
  24841. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  24842. +else
  24843. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  24844. +endif
  24845. +
  24846. +obj-$(CONFIG_AUFS_FS) += aufs.o
  24847. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
  24848. + wkq.o vfsub.o dcsub.o \
  24849. + cpup.o whout.o wbr_policy.o \
  24850. + dinfo.o dentry.o \
  24851. + dynop.o \
  24852. + finfo.o file.o f_op.o \
  24853. + dir.o vdir.o \
  24854. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  24855. + mvdown.o ioctl.o
  24856. +
  24857. +# all are boolean
  24858. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  24859. +aufs-$(CONFIG_SYSFS) += sysfs.o
  24860. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  24861. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  24862. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  24863. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  24864. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  24865. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  24866. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  24867. +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
  24868. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  24869. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  24870. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  24871. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  24872. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  24873. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  24874. diff -Naur null/fs/aufs/module.c linux-6.6.63/fs/aufs/module.c
  24875. --- /dev/null
  24876. +++ linux-6.6.63/fs/aufs/module.c 2024-12-18 15:12:59.298519551 +0300
  24877. @@ -0,0 +1,273 @@
  24878. +// SPDX-License-Identifier: GPL-2.0
  24879. +/*
  24880. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  24881. + *
  24882. + * This program is free software; you can redistribute it and/or modify
  24883. + * it under the terms of the GNU General Public License as published by
  24884. + * the Free Software Foundation; either version 2 of the License, or
  24885. + * (at your option) any later version.
  24886. + *
  24887. + * This program is distributed in the hope that it will be useful,
  24888. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24889. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24890. + * GNU General Public License for more details.
  24891. + *
  24892. + * You should have received a copy of the GNU General Public License
  24893. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24894. + */
  24895. +
  24896. +/*
  24897. + * module global variables and operations
  24898. + */
  24899. +
  24900. +#include <linux/module.h>
  24901. +#include <linux/seq_file.h>
  24902. +#include "aufs.h"
  24903. +
  24904. +/* shrinkable realloc */
  24905. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
  24906. +{
  24907. + size_t sz;
  24908. + int diff;
  24909. +
  24910. + sz = 0;
  24911. + diff = -1;
  24912. + if (p) {
  24913. +#if 0 /* unused */
  24914. + if (!new_sz) {
  24915. + au_kfree_rcu(p);
  24916. + p = NULL;
  24917. + goto out;
  24918. + }
  24919. +#else
  24920. + AuDebugOn(!new_sz);
  24921. +#endif
  24922. + sz = ksize(p);
  24923. + diff = au_kmidx_sub(sz, new_sz);
  24924. + }
  24925. + if (sz && !diff)
  24926. + goto out;
  24927. +
  24928. + if (sz < new_sz)
  24929. + /* expand or SLOB */
  24930. + p = krealloc(p, new_sz, gfp);
  24931. + else if (new_sz < sz && may_shrink) {
  24932. + /* shrink */
  24933. + void *q;
  24934. +
  24935. + q = kmalloc(new_sz, gfp);
  24936. + if (q) {
  24937. + if (p) {
  24938. + memcpy(q, p, new_sz);
  24939. + au_kfree_try_rcu(p);
  24940. + }
  24941. + p = q;
  24942. + } else
  24943. + p = NULL;
  24944. + }
  24945. +
  24946. +out:
  24947. + return p;
  24948. +}
  24949. +
  24950. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  24951. + int may_shrink)
  24952. +{
  24953. + p = au_krealloc(p, new_sz, gfp, may_shrink);
  24954. + if (p && new_sz > nused)
  24955. + memset(p + nused, 0, new_sz - nused);
  24956. + return p;
  24957. +}
  24958. +
  24959. +/* ---------------------------------------------------------------------- */
  24960. +/*
  24961. + * aufs caches
  24962. + */
  24963. +struct kmem_cache *au_cache[AuCache_Last];
  24964. +
  24965. +static void au_cache_fin(void)
  24966. +{
  24967. + int i;
  24968. +
  24969. + /*
  24970. + * Make sure all delayed rcu free inodes are flushed before we
  24971. + * destroy cache.
  24972. + */
  24973. + rcu_barrier();
  24974. +
  24975. + /* excluding AuCache_HNOTIFY */
  24976. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  24977. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  24978. + kmem_cache_destroy(au_cache[i]);
  24979. + au_cache[i] = NULL;
  24980. + }
  24981. +}
  24982. +
  24983. +static int __init au_cache_init(void)
  24984. +{
  24985. + au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  24986. + if (au_cache[AuCache_DINFO])
  24987. + /* SLAB_DESTROY_BY_RCU */
  24988. + au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  24989. + au_icntnr_init_once);
  24990. + if (au_cache[AuCache_ICNTNR])
  24991. + au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
  24992. + au_fi_init_once);
  24993. + if (au_cache[AuCache_FINFO])
  24994. + au_cache[AuCache_VDIR] = AuCache(au_vdir);
  24995. + if (au_cache[AuCache_VDIR])
  24996. + au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  24997. + if (au_cache[AuCache_DEHSTR])
  24998. + return 0;
  24999. +
  25000. + au_cache_fin();
  25001. + return -ENOMEM;
  25002. +}
  25003. +
  25004. +/* ---------------------------------------------------------------------- */
  25005. +
  25006. +int au_dir_roflags;
  25007. +
  25008. +#ifdef CONFIG_AUFS_SBILIST
  25009. +/*
  25010. + * iterate_supers_type() doesn't protect us from
  25011. + * remounting (branch management)
  25012. + */
  25013. +struct hlist_bl_head au_sbilist;
  25014. +#endif
  25015. +
  25016. +/*
  25017. + * functions for module interface.
  25018. + */
  25019. +MODULE_LICENSE("GPL");
  25020. +/* MODULE_LICENSE("GPL v2"); */
  25021. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  25022. +MODULE_DESCRIPTION(AUFS_NAME
  25023. + " -- Advanced multi layered unification filesystem");
  25024. +MODULE_VERSION(AUFS_VERSION);
  25025. +MODULE_ALIAS_FS(AUFS_NAME);
  25026. +
  25027. +/* this module parameter has no meaning when SYSFS is disabled */
  25028. +int sysaufs_brs = 1;
  25029. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  25030. +module_param_named(brs, sysaufs_brs, int, 0444);
  25031. +
  25032. +/* this module parameter has no meaning when USER_NS is disabled */
  25033. +bool au_userns;
  25034. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  25035. +module_param_named(allow_userns, au_userns, bool, 0444);
  25036. +
  25037. +/* ---------------------------------------------------------------------- */
  25038. +
  25039. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  25040. +
  25041. +int au_seq_path(struct seq_file *seq, struct path *path)
  25042. +{
  25043. + int err;
  25044. +
  25045. + err = seq_path(seq, path, au_esc_chars);
  25046. + if (err >= 0)
  25047. + err = 0;
  25048. + else
  25049. + err = -ENOMEM;
  25050. +
  25051. + return err;
  25052. +}
  25053. +
  25054. +/* ---------------------------------------------------------------------- */
  25055. +
  25056. +static int __init aufs_init(void)
  25057. +{
  25058. + int err, i;
  25059. + char *p;
  25060. +
  25061. + p = au_esc_chars;
  25062. + for (i = 1; i <= ' '; i++)
  25063. + *p++ = i;
  25064. + *p++ = '\\';
  25065. + *p++ = '\x7f';
  25066. + *p = 0;
  25067. +
  25068. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  25069. +
  25070. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  25071. + for (i = 0; i < AuIop_Last; i++)
  25072. + aufs_iop_nogetattr[i].getattr = NULL;
  25073. +
  25074. + memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
  25075. +
  25076. + au_sbilist_init();
  25077. + sysaufs_brs_init();
  25078. + au_debug_init();
  25079. + au_dy_init();
  25080. + err = sysaufs_init();
  25081. + if (unlikely(err))
  25082. + goto out;
  25083. + err = dbgaufs_init();
  25084. + if (unlikely(err))
  25085. + goto out_sysaufs;
  25086. + err = au_procfs_init();
  25087. + if (unlikely(err))
  25088. + goto out_dbgaufs;
  25089. + err = au_wkq_init();
  25090. + if (unlikely(err))
  25091. + goto out_procfs;
  25092. + err = au_loopback_init();
  25093. + if (unlikely(err))
  25094. + goto out_wkq;
  25095. + err = au_hnotify_init();
  25096. + if (unlikely(err))
  25097. + goto out_loopback;
  25098. + err = au_sysrq_init();
  25099. + if (unlikely(err))
  25100. + goto out_hin;
  25101. + err = au_cache_init();
  25102. + if (unlikely(err))
  25103. + goto out_sysrq;
  25104. +
  25105. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  25106. + err = register_filesystem(&aufs_fs_type);
  25107. + if (unlikely(err))
  25108. + goto out_cache;
  25109. +
  25110. + /* since we define pr_fmt, call printk directly */
  25111. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  25112. + goto out; /* success */
  25113. +
  25114. +out_cache:
  25115. + au_cache_fin();
  25116. +out_sysrq:
  25117. + au_sysrq_fin();
  25118. +out_hin:
  25119. + au_hnotify_fin();
  25120. +out_loopback:
  25121. + au_loopback_fin();
  25122. +out_wkq:
  25123. + au_wkq_fin();
  25124. +out_procfs:
  25125. + au_procfs_fin();
  25126. +out_dbgaufs:
  25127. + dbgaufs_fin();
  25128. +out_sysaufs:
  25129. + sysaufs_fin();
  25130. + au_dy_fin();
  25131. +out:
  25132. + return err;
  25133. +}
  25134. +
  25135. +static void __exit aufs_exit(void)
  25136. +{
  25137. + unregister_filesystem(&aufs_fs_type);
  25138. + au_cache_fin();
  25139. + au_sysrq_fin();
  25140. + au_hnotify_fin();
  25141. + au_loopback_fin();
  25142. + au_wkq_fin();
  25143. + au_procfs_fin();
  25144. + dbgaufs_fin();
  25145. + sysaufs_fin();
  25146. + au_dy_fin();
  25147. +}
  25148. +
  25149. +module_init(aufs_init);
  25150. +module_exit(aufs_exit);
  25151. diff -Naur null/fs/aufs/module.h linux-6.6.63/fs/aufs/module.h
  25152. --- /dev/null
  25153. +++ linux-6.6.63/fs/aufs/module.h 2024-12-18 15:12:59.298519551 +0300
  25154. @@ -0,0 +1,180 @@
  25155. +/* SPDX-License-Identifier: GPL-2.0 */
  25156. +/*
  25157. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  25158. + *
  25159. + * This program is free software; you can redistribute it and/or modify
  25160. + * it under the terms of the GNU General Public License as published by
  25161. + * the Free Software Foundation; either version 2 of the License, or
  25162. + * (at your option) any later version.
  25163. + *
  25164. + * This program is distributed in the hope that it will be useful,
  25165. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25166. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25167. + * GNU General Public License for more details.
  25168. + *
  25169. + * You should have received a copy of the GNU General Public License
  25170. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25171. + */
  25172. +
  25173. +/*
  25174. + * module initialization and module-global
  25175. + */
  25176. +
  25177. +#ifndef __AUFS_MODULE_H__
  25178. +#define __AUFS_MODULE_H__
  25179. +
  25180. +#ifdef __KERNEL__
  25181. +
  25182. +#include <linux/slab.h>
  25183. +#include "debug.h"
  25184. +#include "dentry.h"
  25185. +#include "dir.h"
  25186. +#include "file.h"
  25187. +#include "inode.h"
  25188. +
  25189. +struct path;
  25190. +struct seq_file;
  25191. +
  25192. +/* module parameters */
  25193. +extern int sysaufs_brs;
  25194. +extern bool au_userns;
  25195. +
  25196. +/* ---------------------------------------------------------------------- */
  25197. +
  25198. +extern int au_dir_roflags;
  25199. +
  25200. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
  25201. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  25202. + int may_shrink);
  25203. +
  25204. +/*
  25205. + * Comparing the size of the object with sizeof(struct rcu_head)
  25206. + * case 1: object is always larger
  25207. + * --> au_kfree_rcu() or au_kfree_do_rcu()
  25208. + * case 2: object is always smaller
  25209. + * --> au_kfree_small()
  25210. + * case 3: object can be any size
  25211. + * --> au_kfree_try_rcu()
  25212. + */
  25213. +
  25214. +static inline void au_kfree_do_rcu(const void *p)
  25215. +{
  25216. + struct {
  25217. + struct rcu_head rcu;
  25218. + } *a = (void *)p;
  25219. +
  25220. + kfree_rcu(a, rcu);
  25221. +}
  25222. +
  25223. +#define au_kfree_rcu(_p) do { \
  25224. + typeof(_p) p = (_p); \
  25225. + BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
  25226. + if (p) \
  25227. + au_kfree_do_rcu(p); \
  25228. + } while (0)
  25229. +
  25230. +#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
  25231. +#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
  25232. +
  25233. +static inline void au_kfree_try_rcu(const void *p)
  25234. +{
  25235. + if (!p)
  25236. + return;
  25237. + if (au_kfree_sz_test(p))
  25238. + au_kfree_do_rcu(p);
  25239. + else
  25240. + kfree(p);
  25241. +}
  25242. +
  25243. +static inline void au_kfree_small(const void *p)
  25244. +{
  25245. + if (!p)
  25246. + return;
  25247. + AuDebugOn(au_kfree_sz_test(p));
  25248. + kfree(p);
  25249. +}
  25250. +
  25251. +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
  25252. +{
  25253. +#ifndef CONFIG_SLOB
  25254. + return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
  25255. +#else
  25256. + return -1; /* SLOB is untested */
  25257. +#endif
  25258. +}
  25259. +
  25260. +int au_seq_path(struct seq_file *seq, struct path *path);
  25261. +
  25262. +#ifdef CONFIG_PROC_FS
  25263. +/* procfs.c */
  25264. +int __init au_procfs_init(void);
  25265. +void au_procfs_fin(void);
  25266. +#else
  25267. +AuStubInt0(au_procfs_init, void);
  25268. +AuStubVoid(au_procfs_fin, void);
  25269. +#endif
  25270. +
  25271. +/* ---------------------------------------------------------------------- */
  25272. +
  25273. +/* kmem cache */
  25274. +enum {
  25275. + AuCache_DINFO,
  25276. + AuCache_ICNTNR,
  25277. + AuCache_FINFO,
  25278. + AuCache_VDIR,
  25279. + AuCache_DEHSTR,
  25280. + AuCache_HNOTIFY, /* must be last */
  25281. + AuCache_Last
  25282. +};
  25283. +
  25284. +extern struct kmem_cache *au_cache[AuCache_Last];
  25285. +
  25286. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  25287. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  25288. +#define AuCacheCtor(type, ctor) \
  25289. + kmem_cache_create(#type, sizeof(struct type), \
  25290. + __alignof__(struct type), AuCacheFlags, ctor)
  25291. +
  25292. +#define AuCacheFuncAlloc(name, index) \
  25293. + static inline struct au_##name *au_cache_alloc_##name(void) \
  25294. + { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
  25295. +
  25296. +#define AuCacheFuncs(name, index) \
  25297. + static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
  25298. + { kmem_cache_free(au_cache[AuCache_##index], p); } \
  25299. + \
  25300. + static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
  25301. + { void *p = rcu; \
  25302. + p -= offsetof(struct au_##name, rcu); \
  25303. + kmem_cache_free(au_cache[AuCache_##index], p); } \
  25304. + static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
  25305. + { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
  25306. + call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
  25307. + \
  25308. + static inline void au_cache_free_##name(struct au_##name *p) \
  25309. + { /* au_cache_free_##name##_norcu(p); */ \
  25310. + au_cache_free_##name##_rcu(p); }
  25311. +
  25312. +AuCacheFuncs(dinfo, DINFO);
  25313. +AuCacheFuncAlloc(dinfo, DINFO);
  25314. +
  25315. +AuCacheFuncs(icntnr, ICNTNR);
  25316. +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
  25317. +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
  25318. +
  25319. +AuCacheFuncs(finfo, FINFO);
  25320. +AuCacheFuncAlloc(finfo, FINFO);
  25321. +
  25322. +AuCacheFuncs(vdir, VDIR);
  25323. +AuCacheFuncAlloc(vdir, VDIR);
  25324. +
  25325. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  25326. +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
  25327. +
  25328. +#ifdef CONFIG_AUFS_HNOTIFY
  25329. +AuCacheFuncs(hnotify, HNOTIFY);
  25330. +AuCacheFuncAlloc(hnotify, HNOTIFY);
  25331. +#endif
  25332. +
  25333. +#endif /* __KERNEL__ */
  25334. +#endif /* __AUFS_MODULE_H__ */
  25335. diff -Naur null/fs/aufs/mvdown.c linux-6.6.63/fs/aufs/mvdown.c
  25336. --- /dev/null
  25337. +++ linux-6.6.63/fs/aufs/mvdown.c 2024-12-18 15:12:59.298519551 +0300
  25338. @@ -0,0 +1,707 @@
  25339. +// SPDX-License-Identifier: GPL-2.0
  25340. +/*
  25341. + * Copyright (C) 2011-2022 Junjiro R. Okajima
  25342. + *
  25343. + * This program is free software; you can redistribute it and/or modify
  25344. + * it under the terms of the GNU General Public License as published by
  25345. + * the Free Software Foundation; either version 2 of the License, or
  25346. + * (at your option) any later version.
  25347. + *
  25348. + * This program is distributed in the hope that it will be useful,
  25349. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25350. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25351. + * GNU General Public License for more details.
  25352. + *
  25353. + * You should have received a copy of the GNU General Public License
  25354. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25355. + */
  25356. +
  25357. +/*
  25358. + * move-down, opposite of copy-up
  25359. + */
  25360. +
  25361. +#include "aufs.h"
  25362. +
  25363. +struct au_mvd_args {
  25364. + struct {
  25365. + struct super_block *h_sb;
  25366. + struct dentry *h_parent;
  25367. + struct au_hinode *hdir;
  25368. + struct inode *h_dir, *h_inode;
  25369. + struct au_pin pin;
  25370. + } info[AUFS_MVDOWN_NARRAY];
  25371. +
  25372. + struct aufs_mvdown mvdown;
  25373. + struct dentry *dentry, *parent;
  25374. + struct inode *inode, *dir;
  25375. + struct super_block *sb;
  25376. + aufs_bindex_t bopq, bwh, bfound;
  25377. + unsigned char rename_lock;
  25378. +};
  25379. +
  25380. +#define mvd_errno mvdown.au_errno
  25381. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  25382. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  25383. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  25384. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  25385. +
  25386. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  25387. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  25388. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  25389. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  25390. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  25391. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  25392. +
  25393. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  25394. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  25395. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  25396. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  25397. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  25398. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  25399. +
  25400. +#define AU_MVD_PR(flag, ...) do { \
  25401. + if (flag) \
  25402. + pr_err(__VA_ARGS__); \
  25403. + } while (0)
  25404. +
  25405. +static int find_lower_writable(struct au_mvd_args *a)
  25406. +{
  25407. + struct super_block *sb;
  25408. + aufs_bindex_t bindex, bbot;
  25409. + struct au_branch *br;
  25410. +
  25411. + sb = a->sb;
  25412. + bindex = a->mvd_bsrc;
  25413. + bbot = au_sbbot(sb);
  25414. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  25415. + for (bindex++; bindex <= bbot; bindex++) {
  25416. + br = au_sbr(sb, bindex);
  25417. + if (au_br_fhsm(br->br_perm)
  25418. + && !sb_rdonly(au_br_sb(br)))
  25419. + return bindex;
  25420. + }
  25421. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  25422. + for (bindex++; bindex <= bbot; bindex++) {
  25423. + br = au_sbr(sb, bindex);
  25424. + if (!au_br_rdonly(br))
  25425. + return bindex;
  25426. + }
  25427. + else
  25428. + for (bindex++; bindex <= bbot; bindex++) {
  25429. + br = au_sbr(sb, bindex);
  25430. + if (!sb_rdonly(au_br_sb(br))) {
  25431. + if (au_br_rdonly(br))
  25432. + a->mvdown.flags
  25433. + |= AUFS_MVDOWN_ROLOWER_R;
  25434. + return bindex;
  25435. + }
  25436. + }
  25437. +
  25438. + return -1;
  25439. +}
  25440. +
  25441. +/* make the parent dir on bdst */
  25442. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  25443. +{
  25444. + int err;
  25445. +
  25446. + err = 0;
  25447. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  25448. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  25449. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  25450. + a->mvd_h_dst_parent = NULL;
  25451. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  25452. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  25453. + if (!a->mvd_h_dst_parent) {
  25454. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  25455. + if (unlikely(err)) {
  25456. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  25457. + goto out;
  25458. + }
  25459. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  25460. + }
  25461. +
  25462. +out:
  25463. + AuTraceErr(err);
  25464. + return err;
  25465. +}
  25466. +
  25467. +/* lock them all */
  25468. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  25469. +{
  25470. + int err;
  25471. + struct dentry *h_trap;
  25472. +
  25473. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  25474. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  25475. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  25476. + au_opt_udba(a->sb),
  25477. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25478. + AuTraceErr(err);
  25479. + if (unlikely(err)) {
  25480. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  25481. + goto out;
  25482. + }
  25483. +
  25484. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  25485. + a->rename_lock = 0;
  25486. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  25487. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  25488. + au_opt_udba(a->sb),
  25489. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25490. + err = au_do_pin(&a->mvd_pin_src);
  25491. + AuTraceErr(err);
  25492. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  25493. + if (unlikely(err)) {
  25494. + AU_MVD_PR(dmsg, "pin_src failed\n");
  25495. + goto out_dst;
  25496. + }
  25497. + goto out; /* success */
  25498. + }
  25499. +
  25500. + a->rename_lock = 1;
  25501. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  25502. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  25503. + au_opt_udba(a->sb),
  25504. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25505. + AuTraceErr(err);
  25506. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  25507. + if (unlikely(err)) {
  25508. + AU_MVD_PR(dmsg, "pin_src failed\n");
  25509. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25510. + goto out_dst;
  25511. + }
  25512. + au_pin_hdir_unlock(&a->mvd_pin_src);
  25513. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25514. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25515. + if (h_trap) {
  25516. + err = (h_trap != a->mvd_h_src_parent);
  25517. + if (err)
  25518. + err = (h_trap != a->mvd_h_dst_parent);
  25519. + }
  25520. + BUG_ON(err); /* it should never happen */
  25521. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  25522. + err = -EBUSY;
  25523. + AuTraceErr(err);
  25524. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25525. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25526. + au_pin_hdir_lock(&a->mvd_pin_src);
  25527. + au_unpin(&a->mvd_pin_src);
  25528. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25529. + goto out_dst;
  25530. + }
  25531. + goto out; /* success */
  25532. +
  25533. +out_dst:
  25534. + au_unpin(&a->mvd_pin_dst);
  25535. +out:
  25536. + AuTraceErr(err);
  25537. + return err;
  25538. +}
  25539. +
  25540. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  25541. +{
  25542. + if (!a->rename_lock)
  25543. + au_unpin(&a->mvd_pin_src);
  25544. + else {
  25545. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25546. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25547. + au_pin_hdir_lock(&a->mvd_pin_src);
  25548. + au_unpin(&a->mvd_pin_src);
  25549. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25550. + }
  25551. + au_unpin(&a->mvd_pin_dst);
  25552. +}
  25553. +
  25554. +/* copy-down the file */
  25555. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  25556. +{
  25557. + int err;
  25558. + struct au_cp_generic cpg = {
  25559. + .dentry = a->dentry,
  25560. + .bdst = a->mvd_bdst,
  25561. + .bsrc = a->mvd_bsrc,
  25562. + .len = -1,
  25563. + .pin = &a->mvd_pin_dst,
  25564. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  25565. + };
  25566. +
  25567. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  25568. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  25569. + au_fset_cpup(cpg.flags, OVERWRITE);
  25570. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  25571. + au_fset_cpup(cpg.flags, RWDST);
  25572. + err = au_sio_cpdown_simple(&cpg);
  25573. + if (unlikely(err))
  25574. + AU_MVD_PR(dmsg, "cpdown failed\n");
  25575. +
  25576. + AuTraceErr(err);
  25577. + return err;
  25578. +}
  25579. +
  25580. +/*
  25581. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  25582. + * were sleeping
  25583. + */
  25584. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  25585. +{
  25586. + int err;
  25587. + struct path h_path;
  25588. + struct au_branch *br;
  25589. + struct inode *delegated;
  25590. +
  25591. + br = au_sbr(a->sb, a->mvd_bdst);
  25592. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  25593. + err = PTR_ERR(h_path.dentry);
  25594. + if (IS_ERR(h_path.dentry)) {
  25595. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  25596. + goto out;
  25597. + }
  25598. +
  25599. + err = 0;
  25600. + if (d_is_positive(h_path.dentry)) {
  25601. + h_path.mnt = au_br_mnt(br);
  25602. + delegated = NULL;
  25603. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  25604. + &delegated, /*force*/0);
  25605. + if (unlikely(err == -EWOULDBLOCK)) {
  25606. + pr_warn("cannot retry for NFSv4 delegation"
  25607. + " for an internal unlink\n");
  25608. + iput(delegated);
  25609. + }
  25610. + if (unlikely(err))
  25611. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  25612. + }
  25613. + dput(h_path.dentry);
  25614. +
  25615. +out:
  25616. + AuTraceErr(err);
  25617. + return err;
  25618. +}
  25619. +
  25620. +/*
  25621. + * unlink the topmost h_dentry
  25622. + */
  25623. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  25624. +{
  25625. + int err;
  25626. + struct path h_path;
  25627. + struct inode *delegated;
  25628. +
  25629. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  25630. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  25631. + delegated = NULL;
  25632. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  25633. + if (unlikely(err == -EWOULDBLOCK)) {
  25634. + pr_warn("cannot retry for NFSv4 delegation"
  25635. + " for an internal unlink\n");
  25636. + iput(delegated);
  25637. + }
  25638. + if (unlikely(err))
  25639. + AU_MVD_PR(dmsg, "unlink failed\n");
  25640. +
  25641. + AuTraceErr(err);
  25642. + return err;
  25643. +}
  25644. +
  25645. +/* Since mvdown succeeded, we ignore an error of this function */
  25646. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  25647. +{
  25648. + int err;
  25649. + struct au_branch *br;
  25650. +
  25651. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  25652. + br = au_sbr(a->sb, a->mvd_bsrc);
  25653. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  25654. + if (!err) {
  25655. + br = au_sbr(a->sb, a->mvd_bdst);
  25656. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  25657. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  25658. + }
  25659. + if (!err)
  25660. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  25661. + else
  25662. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  25663. +}
  25664. +
  25665. +/*
  25666. + * copy-down the file and unlink the bsrc file.
  25667. + * - unlink the bdst whout if exist
  25668. + * - copy-down the file (with whtmp name and rename)
  25669. + * - unlink the bsrc file
  25670. + */
  25671. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  25672. +{
  25673. + int err;
  25674. +
  25675. + err = au_do_mkdir(dmsg, a);
  25676. + if (!err)
  25677. + err = au_do_lock(dmsg, a);
  25678. + if (unlikely(err))
  25679. + goto out;
  25680. +
  25681. + /*
  25682. + * do not revert the activities we made on bdst since they should be
  25683. + * harmless in aufs.
  25684. + */
  25685. +
  25686. + err = au_do_cpdown(dmsg, a);
  25687. + if (!err)
  25688. + err = au_do_unlink_wh(dmsg, a);
  25689. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  25690. + err = au_do_unlink(dmsg, a);
  25691. + if (unlikely(err))
  25692. + goto out_unlock;
  25693. +
  25694. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  25695. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  25696. + if (find_lower_writable(a) < 0)
  25697. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  25698. +
  25699. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  25700. + au_do_stfs(dmsg, a);
  25701. +
  25702. + /* maintain internal array */
  25703. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  25704. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  25705. + au_set_dbtop(a->dentry, a->mvd_bdst);
  25706. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  25707. + au_set_ibtop(a->inode, a->mvd_bdst);
  25708. + } else {
  25709. + /* hide the lower */
  25710. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  25711. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  25712. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  25713. + au_set_ibbot(a->inode, a->mvd_bsrc);
  25714. + }
  25715. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  25716. + au_set_dbbot(a->dentry, a->mvd_bdst);
  25717. + if (au_ibbot(a->inode) < a->mvd_bdst)
  25718. + au_set_ibbot(a->inode, a->mvd_bdst);
  25719. +
  25720. +out_unlock:
  25721. + au_do_unlock(dmsg, a);
  25722. +out:
  25723. + AuTraceErr(err);
  25724. + return err;
  25725. +}
  25726. +
  25727. +/* ---------------------------------------------------------------------- */
  25728. +
  25729. +/* make sure the file is idle */
  25730. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  25731. +{
  25732. + int err, plinked;
  25733. +
  25734. + err = 0;
  25735. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  25736. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  25737. + && au_dcount(a->dentry) == 1
  25738. + && atomic_read(&a->inode->i_count) == 1
  25739. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  25740. + && (!plinked || !au_plink_test(a->inode))
  25741. + && vfsub_inode_nlink(a->inode, AU_I_AUFS) == 1)
  25742. + goto out;
  25743. +
  25744. + err = -EBUSY;
  25745. + AU_MVD_PR(dmsg,
  25746. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  25747. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  25748. + atomic_read(&a->inode->i_count),
  25749. + vfsub_inode_nlink(a->inode, AU_I_AUFS),
  25750. + vfsub_inode_nlink(a->mvd_h_src_inode, AU_I_BRANCH),
  25751. + plinked, plinked ? au_plink_test(a->inode) : 0);
  25752. +
  25753. +out:
  25754. + AuTraceErr(err);
  25755. + return err;
  25756. +}
  25757. +
  25758. +/* make sure the parent dir is fine */
  25759. +static int au_mvd_args_parent(const unsigned char dmsg,
  25760. + struct au_mvd_args *a)
  25761. +{
  25762. + int err;
  25763. + aufs_bindex_t bindex;
  25764. +
  25765. + err = 0;
  25766. + if (unlikely(au_alive_dir(a->parent))) {
  25767. + err = -ENOENT;
  25768. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  25769. + goto out;
  25770. + }
  25771. +
  25772. + a->bopq = au_dbdiropq(a->parent);
  25773. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  25774. + AuDbg("b%d\n", bindex);
  25775. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  25776. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  25777. + err = -EINVAL;
  25778. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  25779. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  25780. + a->bopq, a->mvd_bdst);
  25781. + }
  25782. +
  25783. +out:
  25784. + AuTraceErr(err);
  25785. + return err;
  25786. +}
  25787. +
  25788. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  25789. + struct au_mvd_args *a)
  25790. +{
  25791. + int err;
  25792. + struct au_dinfo *dinfo, *tmp;
  25793. +
  25794. + /* lookup the next lower positive entry */
  25795. + err = -ENOMEM;
  25796. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  25797. + if (unlikely(!tmp))
  25798. + goto out;
  25799. +
  25800. + a->bfound = -1;
  25801. + a->bwh = -1;
  25802. + dinfo = au_di(a->dentry);
  25803. + au_di_cp(tmp, dinfo);
  25804. + au_di_swap(tmp, dinfo);
  25805. +
  25806. + /* returns the number of positive dentries */
  25807. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  25808. + /* AuLkup_IGNORE_PERM */ 0);
  25809. + if (!err)
  25810. + a->bwh = au_dbwh(a->dentry);
  25811. + else if (err > 0)
  25812. + a->bfound = au_dbtop(a->dentry);
  25813. +
  25814. + au_di_swap(tmp, dinfo);
  25815. + au_rw_write_unlock(&tmp->di_rwsem);
  25816. + au_di_free(tmp);
  25817. + if (unlikely(err < 0))
  25818. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  25819. +
  25820. + /*
  25821. + * here, we have these cases.
  25822. + * bfound == -1
  25823. + * no positive dentry under bsrc. there are more sub-cases.
  25824. + * bwh < 0
  25825. + * there no whiteout, we can safely move-down.
  25826. + * bwh <= bsrc
  25827. + * impossible
  25828. + * bsrc < bwh && bwh < bdst
  25829. + * there is a whiteout on RO branch. cannot proceed.
  25830. + * bwh == bdst
  25831. + * there is a whiteout on the RW target branch. it should
  25832. + * be removed.
  25833. + * bdst < bwh
  25834. + * there is a whiteout somewhere unrelated branch.
  25835. + * -1 < bfound && bfound <= bsrc
  25836. + * impossible.
  25837. + * bfound < bdst
  25838. + * found, but it is on RO branch between bsrc and bdst. cannot
  25839. + * proceed.
  25840. + * bfound == bdst
  25841. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  25842. + * error.
  25843. + * bdst < bfound
  25844. + * found, after we create the file on bdst, it will be hidden.
  25845. + */
  25846. +
  25847. + AuDebugOn(a->bfound == -1
  25848. + && a->bwh != -1
  25849. + && a->bwh <= a->mvd_bsrc);
  25850. + AuDebugOn(-1 < a->bfound
  25851. + && a->bfound <= a->mvd_bsrc);
  25852. +
  25853. + err = -EINVAL;
  25854. + if (a->bfound == -1
  25855. + && a->mvd_bsrc < a->bwh
  25856. + && a->bwh != -1
  25857. + && a->bwh < a->mvd_bdst) {
  25858. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  25859. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  25860. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  25861. + goto out;
  25862. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  25863. + a->mvd_errno = EAU_MVDOWN_UPPER;
  25864. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  25865. + a->mvd_bdst, a->bfound);
  25866. + goto out;
  25867. + }
  25868. +
  25869. + err = 0; /* success */
  25870. +
  25871. +out:
  25872. + AuTraceErr(err);
  25873. + return err;
  25874. +}
  25875. +
  25876. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  25877. +{
  25878. + int err;
  25879. +
  25880. + err = 0;
  25881. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  25882. + && a->bfound == a->mvd_bdst)
  25883. + err = -EEXIST;
  25884. + AuTraceErr(err);
  25885. + return err;
  25886. +}
  25887. +
  25888. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  25889. +{
  25890. + int err;
  25891. + struct au_branch *br;
  25892. +
  25893. + err = -EISDIR;
  25894. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  25895. + goto out;
  25896. +
  25897. + err = -EINVAL;
  25898. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  25899. + a->mvd_bsrc = au_ibtop(a->inode);
  25900. + else {
  25901. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  25902. + if (unlikely(a->mvd_bsrc < 0
  25903. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  25904. + || au_dbbot(a->dentry) < a->mvd_bsrc
  25905. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  25906. + || (a->mvd_bsrc < au_ibtop(a->inode)
  25907. + || au_ibbot(a->inode) < a->mvd_bsrc
  25908. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  25909. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  25910. + AU_MVD_PR(dmsg, "no upper\n");
  25911. + goto out;
  25912. + }
  25913. + }
  25914. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  25915. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  25916. + AU_MVD_PR(dmsg, "on the bottom\n");
  25917. + goto out;
  25918. + }
  25919. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  25920. + br = au_sbr(a->sb, a->mvd_bsrc);
  25921. + err = au_br_rdonly(br);
  25922. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  25923. + if (unlikely(err))
  25924. + goto out;
  25925. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  25926. + || IS_APPEND(a->mvd_h_src_inode))) {
  25927. + if (err)
  25928. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  25929. + /* go on */
  25930. + } else
  25931. + goto out;
  25932. +
  25933. + err = -EINVAL;
  25934. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  25935. + a->mvd_bdst = find_lower_writable(a);
  25936. + if (unlikely(a->mvd_bdst < 0)) {
  25937. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  25938. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  25939. + goto out;
  25940. + }
  25941. + } else {
  25942. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  25943. + if (unlikely(a->mvd_bdst < 0
  25944. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  25945. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  25946. + AU_MVD_PR(dmsg, "no lower brid\n");
  25947. + goto out;
  25948. + }
  25949. + }
  25950. +
  25951. + err = au_mvd_args_busy(dmsg, a);
  25952. + if (!err)
  25953. + err = au_mvd_args_parent(dmsg, a);
  25954. + if (!err)
  25955. + err = au_mvd_args_intermediate(dmsg, a);
  25956. + if (!err)
  25957. + err = au_mvd_args_exist(dmsg, a);
  25958. + if (!err)
  25959. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  25960. +
  25961. +out:
  25962. + AuTraceErr(err);
  25963. + return err;
  25964. +}
  25965. +
  25966. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  25967. +{
  25968. + int err, e;
  25969. + unsigned char dmsg;
  25970. + struct au_mvd_args *args;
  25971. + struct inode *inode;
  25972. +
  25973. + inode = d_inode(dentry);
  25974. + err = -EPERM;
  25975. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  25976. + goto out;
  25977. +
  25978. + err = -ENOMEM;
  25979. + args = kmalloc(sizeof(*args), GFP_NOFS);
  25980. + if (unlikely(!args))
  25981. + goto out;
  25982. +
  25983. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  25984. + if (!err)
  25985. + /* VERIFY_WRITE */
  25986. + err = !access_ok(uarg, sizeof(*uarg));
  25987. + if (unlikely(err)) {
  25988. + err = -EFAULT;
  25989. + AuTraceErr(err);
  25990. + goto out_free;
  25991. + }
  25992. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  25993. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  25994. + args->mvdown.au_errno = 0;
  25995. + args->dentry = dentry;
  25996. + args->inode = inode;
  25997. + args->sb = dentry->d_sb;
  25998. +
  25999. + err = -ENOENT;
  26000. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  26001. + args->parent = dget_parent(dentry);
  26002. + args->dir = d_inode(args->parent);
  26003. + inode_lock_nested(args->dir, I_MUTEX_PARENT);
  26004. + dput(args->parent);
  26005. + if (unlikely(args->parent != dentry->d_parent)) {
  26006. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  26007. + goto out_dir;
  26008. + }
  26009. +
  26010. + inode_lock_nested(inode, I_MUTEX_CHILD);
  26011. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  26012. + if (unlikely(err))
  26013. + goto out_inode;
  26014. +
  26015. + di_write_lock_parent(args->parent);
  26016. + err = au_mvd_args(dmsg, args);
  26017. + if (unlikely(err))
  26018. + goto out_parent;
  26019. +
  26020. + err = au_do_mvdown(dmsg, args);
  26021. + if (unlikely(err))
  26022. + goto out_parent;
  26023. +
  26024. + au_cpup_attr_timesizes(args->dir);
  26025. + au_cpup_attr_timesizes(inode);
  26026. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  26027. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  26028. + /* au_digen_dec(dentry); */
  26029. +
  26030. +out_parent:
  26031. + di_write_unlock(args->parent);
  26032. + aufs_read_unlock(dentry, AuLock_DW);
  26033. +out_inode:
  26034. + inode_unlock(inode);
  26035. +out_dir:
  26036. + inode_unlock(args->dir);
  26037. +out_free:
  26038. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  26039. + if (unlikely(e))
  26040. + err = -EFAULT;
  26041. + au_kfree_rcu(args);
  26042. +out:
  26043. + AuTraceErr(err);
  26044. + return err;
  26045. +}
  26046. diff -Naur null/fs/aufs/opts.c linux-6.6.63/fs/aufs/opts.c
  26047. --- /dev/null
  26048. +++ linux-6.6.63/fs/aufs/opts.c 2024-12-18 15:12:59.298519551 +0300
  26049. @@ -0,0 +1,1032 @@
  26050. +// SPDX-License-Identifier: GPL-2.0
  26051. +/*
  26052. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  26053. + *
  26054. + * This program is free software; you can redistribute it and/or modify
  26055. + * it under the terms of the GNU General Public License as published by
  26056. + * the Free Software Foundation; either version 2 of the License, or
  26057. + * (at your option) any later version.
  26058. + *
  26059. + * This program is distributed in the hope that it will be useful,
  26060. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26061. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26062. + * GNU General Public License for more details.
  26063. + *
  26064. + * You should have received a copy of the GNU General Public License
  26065. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26066. + */
  26067. +
  26068. +/*
  26069. + * mount options/flags
  26070. + */
  26071. +
  26072. +#include <linux/types.h> /* a distribution requires */
  26073. +#include <linux/parser.h>
  26074. +#include "aufs.h"
  26075. +
  26076. +/* ---------------------------------------------------------------------- */
  26077. +
  26078. +static const char *au_parser_pattern(int val, match_table_t tbl)
  26079. +{
  26080. + struct match_token *p;
  26081. +
  26082. + p = tbl;
  26083. + while (p->pattern) {
  26084. + if (p->token == val)
  26085. + return p->pattern;
  26086. + p++;
  26087. + }
  26088. + BUG();
  26089. + return "??";
  26090. +}
  26091. +
  26092. +static const char *au_optstr(int *val, match_table_t tbl)
  26093. +{
  26094. + struct match_token *p;
  26095. + int v;
  26096. +
  26097. + v = *val;
  26098. + if (!v)
  26099. + goto out;
  26100. + p = tbl;
  26101. + while (p->pattern) {
  26102. + if (p->token
  26103. + && (v & p->token) == p->token) {
  26104. + *val &= ~p->token;
  26105. + return p->pattern;
  26106. + }
  26107. + p++;
  26108. + }
  26109. +
  26110. +out:
  26111. + return NULL;
  26112. +}
  26113. +
  26114. +/* ---------------------------------------------------------------------- */
  26115. +
  26116. +static match_table_t brperm = {
  26117. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  26118. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  26119. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  26120. + {0, NULL}
  26121. +};
  26122. +
  26123. +static match_table_t brattr = {
  26124. + /* general */
  26125. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  26126. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  26127. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  26128. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  26129. +#ifdef CONFIG_AUFS_FHSM
  26130. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  26131. +#endif
  26132. +#ifdef CONFIG_AUFS_XATTR
  26133. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  26134. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  26135. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  26136. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  26137. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  26138. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  26139. +#endif
  26140. +
  26141. + /* ro/rr branch */
  26142. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  26143. +
  26144. + /* rw branch */
  26145. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  26146. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  26147. +
  26148. + {0, NULL}
  26149. +};
  26150. +
  26151. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  26152. +{
  26153. + int attr, v;
  26154. + char *p;
  26155. +
  26156. + attr = 0;
  26157. + do {
  26158. + p = strchr(str, '+');
  26159. + if (p)
  26160. + *p = 0;
  26161. + v = match_token(str, table, args);
  26162. + if (v) {
  26163. + if (v & AuBrAttr_CMOO_Mask)
  26164. + attr &= ~AuBrAttr_CMOO_Mask;
  26165. + attr |= v;
  26166. + } else {
  26167. + if (p)
  26168. + *p = '+';
  26169. + pr_warn("ignored branch attribute %s\n", str);
  26170. + break;
  26171. + }
  26172. + if (p)
  26173. + str = p + 1;
  26174. + } while (p);
  26175. +
  26176. + return attr;
  26177. +}
  26178. +
  26179. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  26180. +{
  26181. + int sz;
  26182. + const char *p;
  26183. + char *q;
  26184. +
  26185. + q = str->a;
  26186. + *q = 0;
  26187. + p = au_optstr(&perm, brattr);
  26188. + if (p) {
  26189. + sz = strlen(p);
  26190. + memcpy(q, p, sz + 1);
  26191. + q += sz;
  26192. + } else
  26193. + goto out;
  26194. +
  26195. + do {
  26196. + p = au_optstr(&perm, brattr);
  26197. + if (p) {
  26198. + *q++ = '+';
  26199. + sz = strlen(p);
  26200. + memcpy(q, p, sz + 1);
  26201. + q += sz;
  26202. + }
  26203. + } while (p);
  26204. +
  26205. +out:
  26206. + return q - str->a;
  26207. +}
  26208. +
  26209. +int au_br_perm_val(char *perm)
  26210. +{
  26211. + int val, bad, sz;
  26212. + char *p;
  26213. + substring_t args[MAX_OPT_ARGS];
  26214. + au_br_perm_str_t attr;
  26215. +
  26216. + p = strchr(perm, '+');
  26217. + if (p)
  26218. + *p = 0;
  26219. + val = match_token(perm, brperm, args);
  26220. + if (!val) {
  26221. + if (p)
  26222. + *p = '+';
  26223. + pr_warn("ignored branch permission %s\n", perm);
  26224. + val = AuBrPerm_RO;
  26225. + goto out;
  26226. + }
  26227. + if (!p)
  26228. + goto out;
  26229. +
  26230. + val |= br_attr_val(p + 1, brattr, args);
  26231. +
  26232. + bad = 0;
  26233. + switch (val & AuBrPerm_Mask) {
  26234. + case AuBrPerm_RO:
  26235. + case AuBrPerm_RR:
  26236. + bad = val & AuBrWAttr_Mask;
  26237. + val &= ~AuBrWAttr_Mask;
  26238. + break;
  26239. + case AuBrPerm_RW:
  26240. + bad = val & AuBrRAttr_Mask;
  26241. + val &= ~AuBrRAttr_Mask;
  26242. + break;
  26243. + }
  26244. +
  26245. + /*
  26246. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  26247. + * does not treat it as an error, just warning.
  26248. + * this is a tiny guard for the user operation.
  26249. + */
  26250. + if (val & AuBrAttr_UNPIN) {
  26251. + bad |= AuBrAttr_UNPIN;
  26252. + val &= ~AuBrAttr_UNPIN;
  26253. + }
  26254. +
  26255. + if (unlikely(bad)) {
  26256. + sz = au_do_optstr_br_attr(&attr, bad);
  26257. + AuDebugOn(!sz);
  26258. + pr_warn("ignored branch attribute %s\n", attr.a);
  26259. + }
  26260. +
  26261. +out:
  26262. + return val;
  26263. +}
  26264. +
  26265. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  26266. +{
  26267. + au_br_perm_str_t attr;
  26268. + const char *p;
  26269. + char *q;
  26270. + int sz;
  26271. +
  26272. + q = str->a;
  26273. + p = au_optstr(&perm, brperm);
  26274. + AuDebugOn(!p || !*p);
  26275. + sz = strlen(p);
  26276. + memcpy(q, p, sz + 1);
  26277. + q += sz;
  26278. +
  26279. + sz = au_do_optstr_br_attr(&attr, perm);
  26280. + if (sz) {
  26281. + *q++ = '+';
  26282. + memcpy(q, attr.a, sz + 1);
  26283. + }
  26284. +
  26285. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  26286. +}
  26287. +
  26288. +/* ---------------------------------------------------------------------- */
  26289. +
  26290. +static match_table_t udbalevel = {
  26291. + {AuOpt_UDBA_REVAL, "reval"},
  26292. + {AuOpt_UDBA_NONE, "none"},
  26293. +#ifdef CONFIG_AUFS_HNOTIFY
  26294. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  26295. +#ifdef CONFIG_AUFS_HFSNOTIFY
  26296. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  26297. +#endif
  26298. +#endif
  26299. + {-1, NULL}
  26300. +};
  26301. +
  26302. +int au_udba_val(char *str)
  26303. +{
  26304. + substring_t args[MAX_OPT_ARGS];
  26305. +
  26306. + return match_token(str, udbalevel, args);
  26307. +}
  26308. +
  26309. +const char *au_optstr_udba(int udba)
  26310. +{
  26311. + return au_parser_pattern(udba, udbalevel);
  26312. +}
  26313. +
  26314. +/* ---------------------------------------------------------------------- */
  26315. +
  26316. +static match_table_t au_wbr_create_policy = {
  26317. + {AuWbrCreate_TDP, "tdp"},
  26318. + {AuWbrCreate_TDP, "top-down-parent"},
  26319. + {AuWbrCreate_RR, "rr"},
  26320. + {AuWbrCreate_RR, "round-robin"},
  26321. + {AuWbrCreate_MFS, "mfs"},
  26322. + {AuWbrCreate_MFS, "most-free-space"},
  26323. + {AuWbrCreate_MFSV, "mfs:%d"},
  26324. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  26325. +
  26326. + /* top-down regardless the parent, and then mfs */
  26327. + {AuWbrCreate_TDMFS, "tdmfs:%d"},
  26328. + {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
  26329. +
  26330. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  26331. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  26332. + {AuWbrCreate_PMFS, "pmfs"},
  26333. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  26334. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  26335. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  26336. +
  26337. + {-1, NULL}
  26338. +};
  26339. +
  26340. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  26341. + struct au_opt_wbr_create *create)
  26342. +{
  26343. + int err;
  26344. + unsigned long long ull;
  26345. +
  26346. + err = 0;
  26347. + if (!match_u64(arg, &ull))
  26348. + create->mfsrr_watermark = ull;
  26349. + else {
  26350. + pr_err("bad integer in %s\n", str);
  26351. + err = -EINVAL;
  26352. + }
  26353. +
  26354. + return err;
  26355. +}
  26356. +
  26357. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  26358. + struct au_opt_wbr_create *create)
  26359. +{
  26360. + int n, err;
  26361. +
  26362. + err = 0;
  26363. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  26364. + create->mfs_second = n;
  26365. + else {
  26366. + pr_err("bad integer in %s\n", str);
  26367. + err = -EINVAL;
  26368. + }
  26369. +
  26370. + return err;
  26371. +}
  26372. +
  26373. +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  26374. +{
  26375. + int err, e;
  26376. + substring_t args[MAX_OPT_ARGS];
  26377. +
  26378. + err = match_token(str, au_wbr_create_policy, args);
  26379. + create->wbr_create = err;
  26380. + switch (err) {
  26381. + case AuWbrCreate_MFSRRV:
  26382. + case AuWbrCreate_TDMFSV:
  26383. + case AuWbrCreate_PMFSRRV:
  26384. + e = au_wbr_mfs_wmark(&args[0], str, create);
  26385. + if (!e)
  26386. + e = au_wbr_mfs_sec(&args[1], str, create);
  26387. + if (unlikely(e))
  26388. + err = e;
  26389. + break;
  26390. + case AuWbrCreate_MFSRR:
  26391. + case AuWbrCreate_TDMFS:
  26392. + case AuWbrCreate_PMFSRR:
  26393. + e = au_wbr_mfs_wmark(&args[0], str, create);
  26394. + if (unlikely(e)) {
  26395. + err = e;
  26396. + break;
  26397. + }
  26398. + fallthrough;
  26399. + case AuWbrCreate_MFS:
  26400. + case AuWbrCreate_PMFS:
  26401. + create->mfs_second = AUFS_MFS_DEF_SEC;
  26402. + break;
  26403. + case AuWbrCreate_MFSV:
  26404. + case AuWbrCreate_PMFSV:
  26405. + e = au_wbr_mfs_sec(&args[0], str, create);
  26406. + if (unlikely(e))
  26407. + err = e;
  26408. + break;
  26409. + }
  26410. +
  26411. + return err;
  26412. +}
  26413. +
  26414. +const char *au_optstr_wbr_create(int wbr_create)
  26415. +{
  26416. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  26417. +}
  26418. +
  26419. +static match_table_t au_wbr_copyup_policy = {
  26420. + {AuWbrCopyup_TDP, "tdp"},
  26421. + {AuWbrCopyup_TDP, "top-down-parent"},
  26422. + {AuWbrCopyup_BUP, "bup"},
  26423. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  26424. + {AuWbrCopyup_BU, "bu"},
  26425. + {AuWbrCopyup_BU, "bottom-up"},
  26426. + {-1, NULL}
  26427. +};
  26428. +
  26429. +int au_wbr_copyup_val(char *str)
  26430. +{
  26431. + substring_t args[MAX_OPT_ARGS];
  26432. +
  26433. + return match_token(str, au_wbr_copyup_policy, args);
  26434. +}
  26435. +
  26436. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  26437. +{
  26438. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  26439. +}
  26440. +
  26441. +/* ---------------------------------------------------------------------- */
  26442. +
  26443. +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  26444. + aufs_bindex_t bindex)
  26445. +{
  26446. + int err;
  26447. + struct au_opt_add *add = &opt->add;
  26448. + char *p;
  26449. +
  26450. + add->bindex = bindex;
  26451. + add->perm = AuBrPerm_RO;
  26452. + add->pathname = opt_str;
  26453. + p = strchr(opt_str, '=');
  26454. + if (p) {
  26455. + *p++ = 0;
  26456. + if (*p)
  26457. + add->perm = au_br_perm_val(p);
  26458. + }
  26459. +
  26460. + err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
  26461. + if (!err) {
  26462. + if (!p) {
  26463. + add->perm = AuBrPerm_RO;
  26464. + if (au_test_fs_rr(add->path.dentry->d_sb))
  26465. + add->perm = AuBrPerm_RR;
  26466. + else if (!bindex && !(sb_flags & SB_RDONLY))
  26467. + add->perm = AuBrPerm_RW;
  26468. + }
  26469. + opt->type = Opt_add;
  26470. + goto out;
  26471. + }
  26472. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  26473. + err = -EINVAL;
  26474. +
  26475. +out:
  26476. + return err;
  26477. +}
  26478. +
  26479. +static int au_opt_wbr_create(struct super_block *sb,
  26480. + struct au_opt_wbr_create *create)
  26481. +{
  26482. + int err;
  26483. + struct au_sbinfo *sbinfo;
  26484. +
  26485. + SiMustWriteLock(sb);
  26486. +
  26487. + err = 1; /* handled */
  26488. + sbinfo = au_sbi(sb);
  26489. + if (sbinfo->si_wbr_create_ops->fin) {
  26490. + err = sbinfo->si_wbr_create_ops->fin(sb);
  26491. + if (!err)
  26492. + err = 1;
  26493. + }
  26494. +
  26495. + sbinfo->si_wbr_create = create->wbr_create;
  26496. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  26497. + switch (create->wbr_create) {
  26498. + case AuWbrCreate_MFSRRV:
  26499. + case AuWbrCreate_MFSRR:
  26500. + case AuWbrCreate_TDMFS:
  26501. + case AuWbrCreate_TDMFSV:
  26502. + case AuWbrCreate_PMFSRR:
  26503. + case AuWbrCreate_PMFSRRV:
  26504. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  26505. + fallthrough;
  26506. + case AuWbrCreate_MFS:
  26507. + case AuWbrCreate_MFSV:
  26508. + case AuWbrCreate_PMFS:
  26509. + case AuWbrCreate_PMFSV:
  26510. + sbinfo->si_wbr_mfs.mfs_expire
  26511. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  26512. + break;
  26513. + }
  26514. +
  26515. + if (sbinfo->si_wbr_create_ops->init)
  26516. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  26517. +
  26518. + return err;
  26519. +}
  26520. +
  26521. +/*
  26522. + * returns,
  26523. + * plus: processed without an error
  26524. + * zero: unprocessed
  26525. + */
  26526. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  26527. + struct au_opts *opts)
  26528. +{
  26529. + int err;
  26530. + struct au_sbinfo *sbinfo;
  26531. +
  26532. + SiMustWriteLock(sb);
  26533. +
  26534. + err = 1; /* handled */
  26535. + sbinfo = au_sbi(sb);
  26536. + switch (opt->type) {
  26537. + case Opt_udba:
  26538. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26539. + sbinfo->si_mntflags |= opt->udba;
  26540. + opts->given_udba |= opt->udba;
  26541. + break;
  26542. +
  26543. + case Opt_plink:
  26544. + if (opt->tf)
  26545. + au_opt_set(sbinfo->si_mntflags, PLINK);
  26546. + else {
  26547. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26548. + au_plink_put(sb, /*verbose*/1);
  26549. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  26550. + }
  26551. + break;
  26552. + case Opt_list_plink:
  26553. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26554. + au_plink_list(sb);
  26555. + break;
  26556. +
  26557. + case Opt_dio:
  26558. + if (opt->tf) {
  26559. + au_opt_set(sbinfo->si_mntflags, DIO);
  26560. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  26561. + } else {
  26562. + au_opt_clr(sbinfo->si_mntflags, DIO);
  26563. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  26564. + }
  26565. + break;
  26566. +
  26567. + case Opt_fhsm_sec:
  26568. + au_fhsm_set(sbinfo, opt->fhsm_second);
  26569. + break;
  26570. +
  26571. + case Opt_diropq_a:
  26572. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26573. + break;
  26574. + case Opt_diropq_w:
  26575. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26576. + break;
  26577. +
  26578. + case Opt_warn_perm:
  26579. + if (opt->tf)
  26580. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  26581. + else
  26582. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  26583. + break;
  26584. +
  26585. + case Opt_verbose:
  26586. + if (opt->tf)
  26587. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  26588. + else
  26589. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  26590. + break;
  26591. +
  26592. + case Opt_sum:
  26593. + if (opt->tf)
  26594. + au_opt_set(sbinfo->si_mntflags, SUM);
  26595. + else {
  26596. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26597. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  26598. + }
  26599. + break;
  26600. + case Opt_wsum:
  26601. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26602. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  26603. + break;
  26604. +
  26605. + case Opt_wbr_create:
  26606. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  26607. + break;
  26608. + case Opt_wbr_copyup:
  26609. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  26610. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  26611. + break;
  26612. +
  26613. + case Opt_dirwh:
  26614. + sbinfo->si_dirwh = opt->dirwh;
  26615. + break;
  26616. +
  26617. + case Opt_rdcache:
  26618. + sbinfo->si_rdcache
  26619. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  26620. + break;
  26621. + case Opt_rdblk:
  26622. + sbinfo->si_rdblk = opt->rdblk;
  26623. + break;
  26624. + case Opt_rdhash:
  26625. + sbinfo->si_rdhash = opt->rdhash;
  26626. + break;
  26627. +
  26628. + case Opt_shwh:
  26629. + if (opt->tf)
  26630. + au_opt_set(sbinfo->si_mntflags, SHWH);
  26631. + else
  26632. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  26633. + break;
  26634. +
  26635. + case Opt_dirperm1:
  26636. + if (opt->tf)
  26637. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  26638. + else
  26639. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  26640. + break;
  26641. +
  26642. + case Opt_trunc_xino:
  26643. + if (opt->tf)
  26644. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  26645. + else
  26646. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  26647. + break;
  26648. +
  26649. + case Opt_trunc_xino_path:
  26650. + case Opt_itrunc_xino:
  26651. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
  26652. + /*idx_begin*/0);
  26653. + if (!err)
  26654. + err = 1;
  26655. + break;
  26656. +
  26657. + case Opt_trunc_xib:
  26658. + if (opt->tf)
  26659. + au_fset_opts(opts->flags, TRUNC_XIB);
  26660. + else
  26661. + au_fclr_opts(opts->flags, TRUNC_XIB);
  26662. + break;
  26663. +
  26664. + case Opt_dirren:
  26665. + err = 1;
  26666. + if (opt->tf) {
  26667. + if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26668. + err = au_dr_opt_set(sb);
  26669. + if (!err)
  26670. + err = 1;
  26671. + }
  26672. + if (err == 1)
  26673. + au_opt_set(sbinfo->si_mntflags, DIRREN);
  26674. + } else {
  26675. + if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26676. + err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
  26677. + DR_FLUSHED));
  26678. + if (!err)
  26679. + err = 1;
  26680. + }
  26681. + if (err == 1)
  26682. + au_opt_clr(sbinfo->si_mntflags, DIRREN);
  26683. + }
  26684. + break;
  26685. +
  26686. + case Opt_acl:
  26687. + if (opt->tf)
  26688. + sb->s_flags |= SB_POSIXACL;
  26689. + else
  26690. + sb->s_flags &= ~SB_POSIXACL;
  26691. + break;
  26692. +
  26693. + default:
  26694. + err = 0;
  26695. + break;
  26696. + }
  26697. +
  26698. + return err;
  26699. +}
  26700. +
  26701. +/*
  26702. + * returns tri-state.
  26703. + * plus: processed without an error
  26704. + * zero: unprocessed
  26705. + * minus: error
  26706. + */
  26707. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  26708. + struct au_opts *opts)
  26709. +{
  26710. + int err, do_refresh;
  26711. +
  26712. + err = 0;
  26713. + switch (opt->type) {
  26714. + case Opt_append:
  26715. + opt->add.bindex = au_sbbot(sb) + 1;
  26716. + if (opt->add.bindex < 0)
  26717. + opt->add.bindex = 0;
  26718. + goto add;
  26719. + /* Always goto add, not fallthrough */
  26720. + case Opt_prepend:
  26721. + opt->add.bindex = 0;
  26722. + fallthrough;
  26723. + case Opt_add:
  26724. + add: /* indented label */
  26725. + err = au_br_add(sb, &opt->add,
  26726. + au_ftest_opts(opts->flags, REMOUNT));
  26727. + if (!err) {
  26728. + err = 1;
  26729. + au_fset_opts(opts->flags, REFRESH);
  26730. + }
  26731. + break;
  26732. +
  26733. + case Opt_del:
  26734. + case Opt_idel:
  26735. + err = au_br_del(sb, &opt->del,
  26736. + au_ftest_opts(opts->flags, REMOUNT));
  26737. + if (!err) {
  26738. + err = 1;
  26739. + au_fset_opts(opts->flags, TRUNC_XIB);
  26740. + au_fset_opts(opts->flags, REFRESH);
  26741. + }
  26742. + break;
  26743. +
  26744. + case Opt_mod:
  26745. + case Opt_imod:
  26746. + err = au_br_mod(sb, &opt->mod,
  26747. + au_ftest_opts(opts->flags, REMOUNT),
  26748. + &do_refresh);
  26749. + if (!err) {
  26750. + err = 1;
  26751. + if (do_refresh)
  26752. + au_fset_opts(opts->flags, REFRESH);
  26753. + }
  26754. + break;
  26755. + }
  26756. + return err;
  26757. +}
  26758. +
  26759. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  26760. + struct au_opt_xino **opt_xino,
  26761. + struct au_opts *opts)
  26762. +{
  26763. + int err;
  26764. +
  26765. + err = 0;
  26766. + switch (opt->type) {
  26767. + case Opt_xino:
  26768. + err = au_xino_set(sb, &opt->xino,
  26769. + !!au_ftest_opts(opts->flags, REMOUNT));
  26770. + if (!err)
  26771. + *opt_xino = &opt->xino;
  26772. + break;
  26773. + case Opt_noxino:
  26774. + au_xino_clr(sb);
  26775. + *opt_xino = (void *)-1;
  26776. + break;
  26777. + }
  26778. +
  26779. + return err;
  26780. +}
  26781. +
  26782. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26783. + unsigned int pending)
  26784. +{
  26785. + int err, fhsm;
  26786. + aufs_bindex_t bindex, bbot;
  26787. + unsigned char do_plink, skip, do_free, can_no_dreval;
  26788. + struct au_branch *br;
  26789. + struct au_wbr *wbr;
  26790. + struct dentry *root, *dentry;
  26791. + struct inode *dir, *h_dir;
  26792. + struct au_sbinfo *sbinfo;
  26793. + struct au_hinode *hdir;
  26794. +
  26795. + SiMustAnyLock(sb);
  26796. +
  26797. + sbinfo = au_sbi(sb);
  26798. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  26799. +
  26800. + if (!(sb_flags & SB_RDONLY)) {
  26801. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  26802. + pr_warn("first branch should be rw\n");
  26803. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  26804. + pr_warn_once("shwh should be used with ro\n");
  26805. + }
  26806. +
  26807. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  26808. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  26809. + pr_warn_once("udba=*notify requires xino\n");
  26810. +
  26811. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  26812. + pr_warn_once("dirperm1 breaks the protection"
  26813. + " by the permission bits on the lower branch\n");
  26814. +
  26815. + err = 0;
  26816. + fhsm = 0;
  26817. + root = sb->s_root;
  26818. + dir = d_inode(root);
  26819. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  26820. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  26821. + UDBA_NONE);
  26822. + bbot = au_sbbot(sb);
  26823. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  26824. + skip = 0;
  26825. + h_dir = au_h_iptr(dir, bindex);
  26826. + br = au_sbr(sb, bindex);
  26827. +
  26828. + if ((br->br_perm & AuBrAttr_ICEX)
  26829. + && !h_dir->i_op->listxattr)
  26830. + br->br_perm &= ~AuBrAttr_ICEX;
  26831. +#if 0 /* untested */
  26832. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  26833. + && (au_br_sb(br)->s_flags & SB_NOSEC))
  26834. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  26835. +#endif
  26836. +
  26837. + do_free = 0;
  26838. + wbr = br->br_wbr;
  26839. + if (wbr)
  26840. + wbr_wh_read_lock(wbr);
  26841. +
  26842. + if (!au_br_writable(br->br_perm)) {
  26843. + do_free = !!wbr;
  26844. + skip = (!wbr
  26845. + || (!wbr->wbr_whbase
  26846. + && !wbr->wbr_plink
  26847. + && !wbr->wbr_orph));
  26848. + } else if (!au_br_wh_linkable(br->br_perm)) {
  26849. + /* skip = (!br->br_whbase && !br->br_orph); */
  26850. + skip = (!wbr || !wbr->wbr_whbase);
  26851. + if (skip && wbr) {
  26852. + if (do_plink)
  26853. + skip = !!wbr->wbr_plink;
  26854. + else
  26855. + skip = !wbr->wbr_plink;
  26856. + }
  26857. + } else {
  26858. + /* skip = (br->br_whbase && br->br_ohph); */
  26859. + skip = (wbr && wbr->wbr_whbase);
  26860. + if (skip) {
  26861. + if (do_plink)
  26862. + skip = !!wbr->wbr_plink;
  26863. + else
  26864. + skip = !wbr->wbr_plink;
  26865. + }
  26866. + }
  26867. + if (wbr)
  26868. + wbr_wh_read_unlock(wbr);
  26869. +
  26870. + if (can_no_dreval) {
  26871. + dentry = br->br_path.dentry;
  26872. + spin_lock(&dentry->d_lock);
  26873. + if (dentry->d_flags &
  26874. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  26875. + can_no_dreval = 0;
  26876. + spin_unlock(&dentry->d_lock);
  26877. + }
  26878. +
  26879. + if (au_br_fhsm(br->br_perm)) {
  26880. + fhsm++;
  26881. + AuDebugOn(!br->br_fhsm);
  26882. + }
  26883. +
  26884. + if (skip)
  26885. + continue;
  26886. +
  26887. + hdir = au_hi(dir, bindex);
  26888. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  26889. + if (wbr)
  26890. + wbr_wh_write_lock(wbr);
  26891. + err = au_wh_init(br, sb);
  26892. + if (wbr)
  26893. + wbr_wh_write_unlock(wbr);
  26894. + au_hn_inode_unlock(hdir);
  26895. +
  26896. + if (!err && do_free) {
  26897. + au_kfree_rcu(wbr);
  26898. + br->br_wbr = NULL;
  26899. + }
  26900. + }
  26901. +
  26902. + if (can_no_dreval)
  26903. + au_fset_si(sbinfo, NO_DREVAL);
  26904. + else
  26905. + au_fclr_si(sbinfo, NO_DREVAL);
  26906. +
  26907. + if (fhsm >= 2) {
  26908. + au_fset_si(sbinfo, FHSM);
  26909. + for (bindex = bbot; bindex >= 0; bindex--) {
  26910. + br = au_sbr(sb, bindex);
  26911. + if (au_br_fhsm(br->br_perm)) {
  26912. + au_fhsm_set_bottom(sb, bindex);
  26913. + break;
  26914. + }
  26915. + }
  26916. + } else {
  26917. + au_fclr_si(sbinfo, FHSM);
  26918. + au_fhsm_set_bottom(sb, -1);
  26919. + }
  26920. +
  26921. + return err;
  26922. +}
  26923. +
  26924. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  26925. +{
  26926. + int err;
  26927. + unsigned int tmp;
  26928. + aufs_bindex_t bindex, bbot;
  26929. + struct au_opt *opt;
  26930. + struct au_opt_xino *opt_xino, xino;
  26931. + struct au_sbinfo *sbinfo;
  26932. + struct au_branch *br;
  26933. + struct inode *dir;
  26934. +
  26935. + SiMustWriteLock(sb);
  26936. +
  26937. + err = 0;
  26938. + opt_xino = NULL;
  26939. + opt = opts->opt;
  26940. + while (err >= 0 && opt->type != Opt_tail)
  26941. + err = au_opt_simple(sb, opt++, opts);
  26942. + if (err > 0)
  26943. + err = 0;
  26944. + else if (unlikely(err < 0))
  26945. + goto out;
  26946. +
  26947. + /* disable xino and udba temporary */
  26948. + sbinfo = au_sbi(sb);
  26949. + tmp = sbinfo->si_mntflags;
  26950. + au_opt_clr(sbinfo->si_mntflags, XINO);
  26951. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  26952. +
  26953. + opt = opts->opt;
  26954. + while (err >= 0 && opt->type != Opt_tail)
  26955. + err = au_opt_br(sb, opt++, opts);
  26956. + if (err > 0)
  26957. + err = 0;
  26958. + else if (unlikely(err < 0))
  26959. + goto out;
  26960. +
  26961. + bbot = au_sbbot(sb);
  26962. + if (unlikely(bbot < 0)) {
  26963. + err = -EINVAL;
  26964. + pr_err("no branches\n");
  26965. + goto out;
  26966. + }
  26967. +
  26968. + if (au_opt_test(tmp, XINO))
  26969. + au_opt_set(sbinfo->si_mntflags, XINO);
  26970. + opt = opts->opt;
  26971. + while (!err && opt->type != Opt_tail)
  26972. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  26973. + if (unlikely(err))
  26974. + goto out;
  26975. +
  26976. + err = au_opts_verify(sb, sb->s_flags, tmp);
  26977. + if (unlikely(err))
  26978. + goto out;
  26979. +
  26980. + /* restore xino */
  26981. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  26982. + xino.file = au_xino_def(sb);
  26983. + err = PTR_ERR(xino.file);
  26984. + if (IS_ERR(xino.file))
  26985. + goto out;
  26986. +
  26987. + err = au_xino_set(sb, &xino, /*remount*/0);
  26988. + fput(xino.file);
  26989. + if (unlikely(err))
  26990. + goto out;
  26991. + }
  26992. +
  26993. + /* restore udba */
  26994. + tmp &= AuOptMask_UDBA;
  26995. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26996. + sbinfo->si_mntflags |= tmp;
  26997. + bbot = au_sbbot(sb);
  26998. + for (bindex = 0; bindex <= bbot; bindex++) {
  26999. + br = au_sbr(sb, bindex);
  27000. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  27001. + if (unlikely(err))
  27002. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  27003. + bindex, err);
  27004. + /* go on even if err */
  27005. + }
  27006. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  27007. + dir = d_inode(sb->s_root);
  27008. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  27009. + }
  27010. +
  27011. +out:
  27012. + return err;
  27013. +}
  27014. +
  27015. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  27016. +{
  27017. + int err, rerr;
  27018. + unsigned char no_dreval;
  27019. + struct inode *dir;
  27020. + struct au_opt_xino *opt_xino;
  27021. + struct au_opt *opt;
  27022. + struct au_sbinfo *sbinfo;
  27023. +
  27024. + SiMustWriteLock(sb);
  27025. +
  27026. + err = au_dr_opt_flush(sb);
  27027. + if (unlikely(err))
  27028. + goto out;
  27029. + au_fset_opts(opts->flags, DR_FLUSHED);
  27030. +
  27031. + dir = d_inode(sb->s_root);
  27032. + sbinfo = au_sbi(sb);
  27033. + opt_xino = NULL;
  27034. + opt = opts->opt;
  27035. + while (err >= 0 && opt->type != Opt_tail) {
  27036. + err = au_opt_simple(sb, opt, opts);
  27037. + if (!err)
  27038. + err = au_opt_br(sb, opt, opts);
  27039. + if (!err)
  27040. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  27041. + opt++;
  27042. + }
  27043. + if (err > 0)
  27044. + err = 0;
  27045. + AuTraceErr(err);
  27046. + /* go on even err */
  27047. +
  27048. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  27049. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  27050. + if (unlikely(rerr && !err))
  27051. + err = rerr;
  27052. +
  27053. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  27054. + au_fset_opts(opts->flags, REFRESH_IDOP);
  27055. +
  27056. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  27057. + rerr = au_xib_trunc(sb);
  27058. + if (unlikely(rerr && !err))
  27059. + err = rerr;
  27060. + }
  27061. +
  27062. + /* will be handled by the caller */
  27063. + if (!au_ftest_opts(opts->flags, REFRESH)
  27064. + && (opts->given_udba
  27065. + || au_opt_test(sbinfo->si_mntflags, XINO)
  27066. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  27067. + ))
  27068. + au_fset_opts(opts->flags, REFRESH);
  27069. +
  27070. + AuDbg("status 0x%x\n", opts->flags);
  27071. +
  27072. +out:
  27073. + return err;
  27074. +}
  27075. +
  27076. +/* ---------------------------------------------------------------------- */
  27077. +
  27078. +unsigned int au_opt_udba(struct super_block *sb)
  27079. +{
  27080. + return au_mntflags(sb) & AuOptMask_UDBA;
  27081. +}
  27082. diff -Naur null/fs/aufs/opts.h linux-6.6.63/fs/aufs/opts.h
  27083. --- /dev/null
  27084. +++ linux-6.6.63/fs/aufs/opts.h 2024-12-18 15:12:59.298519551 +0300
  27085. @@ -0,0 +1,263 @@
  27086. +/* SPDX-License-Identifier: GPL-2.0 */
  27087. +/*
  27088. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  27089. + *
  27090. + * This program is free software; you can redistribute it and/or modify
  27091. + * it under the terms of the GNU General Public License as published by
  27092. + * the Free Software Foundation; either version 2 of the License, or
  27093. + * (at your option) any later version.
  27094. + *
  27095. + * This program is distributed in the hope that it will be useful,
  27096. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27097. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27098. + * GNU General Public License for more details.
  27099. + *
  27100. + * You should have received a copy of the GNU General Public License
  27101. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27102. + */
  27103. +
  27104. +/*
  27105. + * mount options/flags
  27106. + */
  27107. +
  27108. +#ifndef __AUFS_OPTS_H__
  27109. +#define __AUFS_OPTS_H__
  27110. +
  27111. +#ifdef __KERNEL__
  27112. +
  27113. +#include <linux/fs_parser.h>
  27114. +#include <linux/namei.h>
  27115. +#include <linux/path.h>
  27116. +
  27117. +enum {
  27118. + Opt_br,
  27119. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  27120. + Opt_idel, Opt_imod,
  27121. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  27122. + Opt_xino, Opt_noxino,
  27123. + Opt_trunc_xino, Opt_trunc_xino_v,
  27124. + Opt_trunc_xino_path, Opt_itrunc_xino,
  27125. + Opt_trunc_xib,
  27126. + Opt_shwh,
  27127. + Opt_plink, Opt_list_plink,
  27128. + Opt_udba,
  27129. + Opt_dio,
  27130. + Opt_diropq, Opt_diropq_a, Opt_diropq_w,
  27131. + Opt_warn_perm,
  27132. + Opt_wbr_copyup, Opt_wbr_create,
  27133. + Opt_fhsm_sec,
  27134. + Opt_verbose, Opt_noverbose,
  27135. + Opt_sum, Opt_wsum,
  27136. + Opt_dirperm1,
  27137. + Opt_dirren,
  27138. + Opt_acl,
  27139. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  27140. +};
  27141. +
  27142. +/* ---------------------------------------------------------------------- */
  27143. +
  27144. +/* mount flags */
  27145. +#define AuOpt_XINO 1 /* external inode number bitmap
  27146. + and translation table */
  27147. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  27148. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  27149. +#define AuOpt_UDBA_REVAL (1 << 3)
  27150. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  27151. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  27152. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  27153. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  27154. + bits */
  27155. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  27156. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  27157. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  27158. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  27159. +#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
  27160. +#define AuOpt_DIO (1 << 14) /* direct io */
  27161. +#define AuOpt_DIRREN (1 << 15) /* directory rename */
  27162. +
  27163. +#ifndef CONFIG_AUFS_HNOTIFY
  27164. +#undef AuOpt_UDBA_HNOTIFY
  27165. +#define AuOpt_UDBA_HNOTIFY 0
  27166. +#endif
  27167. +#ifndef CONFIG_AUFS_DIRREN
  27168. +#undef AuOpt_DIRREN
  27169. +#define AuOpt_DIRREN 0
  27170. +#endif
  27171. +#ifndef CONFIG_AUFS_SHWH
  27172. +#undef AuOpt_SHWH
  27173. +#define AuOpt_SHWH 0
  27174. +#endif
  27175. +
  27176. +#define AuOpt_Def (AuOpt_XINO \
  27177. + | AuOpt_UDBA_REVAL \
  27178. + | AuOpt_PLINK \
  27179. + /* | AuOpt_DIRPERM1 */ \
  27180. + | AuOpt_WARN_PERM)
  27181. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  27182. + | AuOpt_UDBA_REVAL \
  27183. + | AuOpt_UDBA_HNOTIFY)
  27184. +
  27185. +#define AuOpt_LkupDirFlags (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
  27186. +
  27187. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  27188. +#define au_opt_set(flags, name) do { \
  27189. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  27190. + ((flags) |= AuOpt_##name); \
  27191. +} while (0)
  27192. +#define au_opt_set_udba(flags, name) do { \
  27193. + (flags) &= ~AuOptMask_UDBA; \
  27194. + ((flags) |= AuOpt_##name); \
  27195. +} while (0)
  27196. +#define au_opt_clr(flags, name) do { \
  27197. + ((flags) &= ~AuOpt_##name); \
  27198. +} while (0)
  27199. +
  27200. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  27201. +{
  27202. +#ifdef CONFIG_PROC_FS
  27203. + return mntflags;
  27204. +#else
  27205. + return mntflags & ~AuOpt_PLINK;
  27206. +#endif
  27207. +}
  27208. +
  27209. +/* ---------------------------------------------------------------------- */
  27210. +
  27211. +/* policies to select one among multiple writable branches */
  27212. +enum {
  27213. + AuWbrCreate_TDP, /* top down parent */
  27214. + AuWbrCreate_RR, /* round robin */
  27215. + AuWbrCreate_MFS, /* most free space */
  27216. + AuWbrCreate_MFSV, /* mfs with seconds */
  27217. + AuWbrCreate_MFSRR, /* mfs then rr */
  27218. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  27219. + AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
  27220. + AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
  27221. + AuWbrCreate_PMFS, /* parent and mfs */
  27222. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  27223. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  27224. + AuWbrCreate_PMFSRRV, /* plus seconds */
  27225. +
  27226. + AuWbrCreate_Def = AuWbrCreate_TDP
  27227. +};
  27228. +
  27229. +enum {
  27230. + AuWbrCopyup_TDP, /* top down parent */
  27231. + AuWbrCopyup_BUP, /* bottom up parent */
  27232. + AuWbrCopyup_BU, /* bottom up */
  27233. +
  27234. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  27235. +};
  27236. +
  27237. +/* ---------------------------------------------------------------------- */
  27238. +
  27239. +struct file;
  27240. +
  27241. +struct au_opt_add {
  27242. + aufs_bindex_t bindex;
  27243. + char *pathname;
  27244. + int perm;
  27245. + struct path path;
  27246. +};
  27247. +
  27248. +struct au_opt_del {
  27249. + char *pathname;
  27250. + struct path h_path;
  27251. +};
  27252. +
  27253. +struct au_opt_mod {
  27254. + char *path;
  27255. + int perm;
  27256. + struct dentry *h_root;
  27257. +};
  27258. +
  27259. +struct au_opt_xino {
  27260. + char *path;
  27261. + struct file *file;
  27262. +};
  27263. +
  27264. +struct au_opt_xino_itrunc {
  27265. + aufs_bindex_t bindex;
  27266. +};
  27267. +
  27268. +struct au_opt_wbr_create {
  27269. + int wbr_create;
  27270. + int mfs_second;
  27271. + unsigned long long mfsrr_watermark;
  27272. +};
  27273. +
  27274. +struct au_opt {
  27275. + int type;
  27276. + union {
  27277. + struct au_opt_xino xino;
  27278. + struct au_opt_xino_itrunc xino_itrunc;
  27279. + struct au_opt_add add;
  27280. + struct au_opt_del del;
  27281. + struct au_opt_mod mod;
  27282. + int dirwh;
  27283. + int rdcache;
  27284. + unsigned int rdblk;
  27285. + unsigned int rdhash;
  27286. + int udba;
  27287. + struct au_opt_wbr_create wbr_create;
  27288. + int wbr_copyup;
  27289. + unsigned int fhsm_second;
  27290. + bool tf; /* generic flag, true or false */
  27291. + };
  27292. +};
  27293. +
  27294. +/* opts flags */
  27295. +#define AuOpts_REMOUNT 1
  27296. +#define AuOpts_REFRESH (1 << 1)
  27297. +#define AuOpts_TRUNC_XIB (1 << 2)
  27298. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  27299. +#define AuOpts_REFRESH_IDOP (1 << 4)
  27300. +#define AuOpts_DR_FLUSHED (1 << 5)
  27301. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  27302. +#define au_fset_opts(flags, name) \
  27303. + do { (flags) |= AuOpts_##name; } while (0)
  27304. +#define au_fclr_opts(flags, name) \
  27305. + do { (flags) &= ~AuOpts_##name; } while (0)
  27306. +
  27307. +#ifndef CONFIG_AUFS_DIRREN
  27308. +#undef AuOpts_DR_FLUSHED
  27309. +#define AuOpts_DR_FLUSHED 0
  27310. +#endif
  27311. +
  27312. +struct au_opts {
  27313. + struct au_opt *opt;
  27314. + int max_opt;
  27315. +
  27316. + unsigned int given_udba;
  27317. + unsigned int flags;
  27318. + unsigned long sb_flags;
  27319. +};
  27320. +
  27321. +/* ---------------------------------------------------------------------- */
  27322. +
  27323. +/* opts.c */
  27324. +int au_br_perm_val(char *perm);
  27325. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  27326. +int au_udba_val(char *str);
  27327. +const char *au_optstr_udba(int udba);
  27328. +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
  27329. +const char *au_optstr_wbr_create(int wbr_create);
  27330. +int au_wbr_copyup_val(char *str);
  27331. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  27332. +
  27333. +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  27334. + aufs_bindex_t bindex);
  27335. +struct super_block;
  27336. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  27337. + unsigned int pending);
  27338. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  27339. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  27340. +
  27341. +unsigned int au_opt_udba(struct super_block *sb);
  27342. +
  27343. +/* fsctx.c */
  27344. +int aufs_fsctx_init(struct fs_context *fc);
  27345. +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
  27346. +
  27347. +#endif /* __KERNEL__ */
  27348. +#endif /* __AUFS_OPTS_H__ */
  27349. diff -Naur null/fs/aufs/plink.c linux-6.6.63/fs/aufs/plink.c
  27350. --- /dev/null
  27351. +++ linux-6.6.63/fs/aufs/plink.c 2024-12-18 15:12:59.299519538 +0300
  27352. @@ -0,0 +1,516 @@
  27353. +// SPDX-License-Identifier: GPL-2.0
  27354. +/*
  27355. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  27356. + *
  27357. + * This program is free software; you can redistribute it and/or modify
  27358. + * it under the terms of the GNU General Public License as published by
  27359. + * the Free Software Foundation; either version 2 of the License, or
  27360. + * (at your option) any later version.
  27361. + *
  27362. + * This program is distributed in the hope that it will be useful,
  27363. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27364. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27365. + * GNU General Public License for more details.
  27366. + *
  27367. + * You should have received a copy of the GNU General Public License
  27368. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27369. + */
  27370. +
  27371. +/*
  27372. + * pseudo-link
  27373. + */
  27374. +
  27375. +#include "aufs.h"
  27376. +
  27377. +/*
  27378. + * the pseudo-link maintenance mode.
  27379. + * during a user process maintains the pseudo-links,
  27380. + * prohibit adding a new plink and branch manipulation.
  27381. + *
  27382. + * Flags
  27383. + * NOPLM:
  27384. + * For entry functions which will handle plink, and i_mutex is already held
  27385. + * in VFS.
  27386. + * They cannot wait and should return an error at once.
  27387. + * Callers has to check the error.
  27388. + * NOPLMW:
  27389. + * For entry functions which will handle plink, but i_mutex is not held
  27390. + * in VFS.
  27391. + * They can wait the plink maintenance mode to finish.
  27392. + *
  27393. + * They behave like F_SETLK and F_SETLKW.
  27394. + * If the caller never handle plink, then both flags are unnecessary.
  27395. + */
  27396. +
  27397. +int au_plink_maint(struct super_block *sb, int flags)
  27398. +{
  27399. + int err;
  27400. + pid_t pid, ppid;
  27401. + struct task_struct *parent, *prev;
  27402. + struct au_sbinfo *sbi;
  27403. +
  27404. + SiMustAnyLock(sb);
  27405. +
  27406. + err = 0;
  27407. + if (!au_opt_test(au_mntflags(sb), PLINK))
  27408. + goto out;
  27409. +
  27410. + sbi = au_sbi(sb);
  27411. + pid = sbi->si_plink_maint_pid;
  27412. + if (!pid || pid == current->pid)
  27413. + goto out;
  27414. +
  27415. + /* todo: it highly depends upon /sbin/mount.aufs */
  27416. + prev = NULL;
  27417. + parent = current;
  27418. + ppid = 0;
  27419. + rcu_read_lock();
  27420. + while (1) {
  27421. + parent = rcu_dereference(parent->real_parent);
  27422. + if (parent == prev)
  27423. + break;
  27424. + ppid = task_pid_vnr(parent);
  27425. + if (pid == ppid) {
  27426. + rcu_read_unlock();
  27427. + goto out;
  27428. + }
  27429. + prev = parent;
  27430. + }
  27431. + rcu_read_unlock();
  27432. +
  27433. + if (au_ftest_lock(flags, NOPLMW)) {
  27434. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  27435. + /* AuDebugOn(!lockdep_depth(current)); */
  27436. + while (sbi->si_plink_maint_pid) {
  27437. + si_read_unlock(sb);
  27438. + /* gave up wake_up_bit() */
  27439. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  27440. +
  27441. + if (au_ftest_lock(flags, FLUSH))
  27442. + au_nwt_flush(&sbi->si_nowait);
  27443. + si_noflush_read_lock(sb);
  27444. + }
  27445. + } else if (au_ftest_lock(flags, NOPLM)) {
  27446. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  27447. + err = -EAGAIN;
  27448. + }
  27449. +
  27450. +out:
  27451. + return err;
  27452. +}
  27453. +
  27454. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  27455. +{
  27456. + spin_lock(&sbinfo->si_plink_maint_lock);
  27457. + sbinfo->si_plink_maint_pid = 0;
  27458. + spin_unlock(&sbinfo->si_plink_maint_lock);
  27459. + wake_up_all(&sbinfo->si_plink_wq);
  27460. +}
  27461. +
  27462. +int au_plink_maint_enter(struct super_block *sb)
  27463. +{
  27464. + int err;
  27465. + struct au_sbinfo *sbinfo;
  27466. +
  27467. + err = 0;
  27468. + sbinfo = au_sbi(sb);
  27469. + /* make sure i am the only one in this fs */
  27470. + si_write_lock(sb, AuLock_FLUSH);
  27471. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  27472. + spin_lock(&sbinfo->si_plink_maint_lock);
  27473. + if (!sbinfo->si_plink_maint_pid)
  27474. + sbinfo->si_plink_maint_pid = current->pid;
  27475. + else
  27476. + err = -EBUSY;
  27477. + spin_unlock(&sbinfo->si_plink_maint_lock);
  27478. + }
  27479. + si_write_unlock(sb);
  27480. +
  27481. + return err;
  27482. +}
  27483. +
  27484. +/* ---------------------------------------------------------------------- */
  27485. +
  27486. +#ifdef CONFIG_AUFS_DEBUG
  27487. +void au_plink_list(struct super_block *sb)
  27488. +{
  27489. + int i;
  27490. + struct au_sbinfo *sbinfo;
  27491. + struct hlist_bl_head *hbl;
  27492. + struct hlist_bl_node *pos;
  27493. + struct au_icntnr *icntnr;
  27494. +
  27495. + SiMustAnyLock(sb);
  27496. +
  27497. + sbinfo = au_sbi(sb);
  27498. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27499. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27500. +
  27501. + for (i = 0; i < AuPlink_NHASH; i++) {
  27502. + hbl = sbinfo->si_plink + i;
  27503. + hlist_bl_lock(hbl);
  27504. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  27505. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  27506. + hlist_bl_unlock(hbl);
  27507. + }
  27508. +}
  27509. +#endif
  27510. +
  27511. +/* is the inode pseudo-linked? */
  27512. +int au_plink_test(struct inode *inode)
  27513. +{
  27514. + int found, i;
  27515. + struct au_sbinfo *sbinfo;
  27516. + struct hlist_bl_head *hbl;
  27517. + struct hlist_bl_node *pos;
  27518. + struct au_icntnr *icntnr;
  27519. +
  27520. + sbinfo = au_sbi(inode->i_sb);
  27521. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  27522. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  27523. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  27524. +
  27525. + found = 0;
  27526. + i = au_plink_hash(inode->i_ino);
  27527. + hbl = sbinfo->si_plink + i;
  27528. + hlist_bl_lock(hbl);
  27529. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  27530. + if (&icntnr->vfs_inode == inode) {
  27531. + found = 1;
  27532. + break;
  27533. + }
  27534. + hlist_bl_unlock(hbl);
  27535. + return found;
  27536. +}
  27537. +
  27538. +/* ---------------------------------------------------------------------- */
  27539. +
  27540. +/*
  27541. + * generate a name for plink.
  27542. + * the file will be stored under AUFS_WH_PLINKDIR.
  27543. + */
  27544. +/* 20 is max digits length of ulong 64 */
  27545. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  27546. +
  27547. +static int plink_name(char *name, int len, struct inode *inode,
  27548. + aufs_bindex_t bindex)
  27549. +{
  27550. + int rlen;
  27551. + struct inode *h_inode;
  27552. +
  27553. + h_inode = au_h_iptr(inode, bindex);
  27554. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  27555. + return rlen;
  27556. +}
  27557. +
  27558. +struct au_do_plink_lkup_args {
  27559. + struct dentry **errp;
  27560. + struct qstr *tgtname;
  27561. + struct path *h_ppath;
  27562. +};
  27563. +
  27564. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  27565. + struct path *h_ppath)
  27566. +{
  27567. + struct dentry *h_dentry;
  27568. + struct inode *h_inode;
  27569. +
  27570. + h_inode = d_inode(h_ppath->dentry);
  27571. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
  27572. + h_dentry = vfsub_lkup_one(tgtname, h_ppath);
  27573. + inode_unlock_shared(h_inode);
  27574. +
  27575. + return h_dentry;
  27576. +}
  27577. +
  27578. +static void au_call_do_plink_lkup(void *args)
  27579. +{
  27580. + struct au_do_plink_lkup_args *a = args;
  27581. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
  27582. +}
  27583. +
  27584. +/* lookup the plink-ed @inode under the branch at @bindex */
  27585. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  27586. +{
  27587. + struct dentry *h_dentry;
  27588. + struct au_branch *br;
  27589. + struct path h_ppath;
  27590. + int wkq_err;
  27591. + char a[PLINK_NAME_LEN];
  27592. + struct qstr tgtname = QSTR_INIT(a, 0);
  27593. +
  27594. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  27595. +
  27596. + br = au_sbr(inode->i_sb, bindex);
  27597. + h_ppath.dentry = br->br_wbr->wbr_plink;
  27598. + h_ppath.mnt = au_br_mnt(br);
  27599. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  27600. +
  27601. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27602. + struct au_do_plink_lkup_args args = {
  27603. + .errp = &h_dentry,
  27604. + .tgtname = &tgtname,
  27605. + .h_ppath = &h_ppath
  27606. + };
  27607. +
  27608. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  27609. + if (unlikely(wkq_err))
  27610. + h_dentry = ERR_PTR(wkq_err);
  27611. + } else
  27612. + h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
  27613. +
  27614. + return h_dentry;
  27615. +}
  27616. +
  27617. +/* create a pseudo-link */
  27618. +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
  27619. + struct dentry *h_dentry)
  27620. +{
  27621. + int err;
  27622. + struct path h_path;
  27623. + struct inode *h_dir, *delegated;
  27624. +
  27625. + h_dir = d_inode(h_ppath->dentry);
  27626. + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
  27627. + h_path.mnt = h_ppath->mnt;
  27628. +again:
  27629. + h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
  27630. + err = PTR_ERR(h_path.dentry);
  27631. + if (IS_ERR(h_path.dentry))
  27632. + goto out;
  27633. +
  27634. + err = 0;
  27635. + /* wh.plink dir is not monitored */
  27636. + /* todo: is it really safe? */
  27637. + if (d_is_positive(h_path.dentry)
  27638. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  27639. + delegated = NULL;
  27640. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  27641. + if (unlikely(err == -EWOULDBLOCK)) {
  27642. + pr_warn("cannot retry for NFSv4 delegation"
  27643. + " for an internal unlink\n");
  27644. + iput(delegated);
  27645. + }
  27646. + dput(h_path.dentry);
  27647. + h_path.dentry = NULL;
  27648. + if (!err)
  27649. + goto again;
  27650. + }
  27651. + if (!err && d_is_negative(h_path.dentry)) {
  27652. + delegated = NULL;
  27653. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  27654. + if (unlikely(err == -EWOULDBLOCK)) {
  27655. + pr_warn("cannot retry for NFSv4 delegation"
  27656. + " for an internal link\n");
  27657. + iput(delegated);
  27658. + }
  27659. + }
  27660. + dput(h_path.dentry);
  27661. +
  27662. +out:
  27663. + inode_unlock(h_dir);
  27664. + return err;
  27665. +}
  27666. +
  27667. +struct do_whplink_args {
  27668. + int *errp;
  27669. + struct qstr *tgt;
  27670. + struct path *h_ppath;
  27671. + struct dentry *h_dentry;
  27672. +};
  27673. +
  27674. +static void call_do_whplink(void *args)
  27675. +{
  27676. + struct do_whplink_args *a = args;
  27677. + *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
  27678. +}
  27679. +
  27680. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  27681. + aufs_bindex_t bindex)
  27682. +{
  27683. + int err, wkq_err;
  27684. + struct au_branch *br;
  27685. + struct au_wbr *wbr;
  27686. + struct path h_ppath;
  27687. + char a[PLINK_NAME_LEN];
  27688. + struct qstr tgtname = QSTR_INIT(a, 0);
  27689. +
  27690. + br = au_sbr(inode->i_sb, bindex);
  27691. + wbr = br->br_wbr;
  27692. + h_ppath.dentry = wbr->wbr_plink;
  27693. + h_ppath.mnt = au_br_mnt(br);
  27694. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  27695. +
  27696. + /* always superio. */
  27697. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27698. + struct do_whplink_args args = {
  27699. + .errp = &err,
  27700. + .tgt = &tgtname,
  27701. + .h_ppath = &h_ppath,
  27702. + .h_dentry = h_dentry
  27703. + };
  27704. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  27705. + if (unlikely(wkq_err))
  27706. + err = wkq_err;
  27707. + } else
  27708. + err = do_whplink(&tgtname, &h_ppath, h_dentry);
  27709. +
  27710. + return err;
  27711. +}
  27712. +
  27713. +/*
  27714. + * create a new pseudo-link for @h_dentry on @bindex.
  27715. + * the linked inode is held in aufs @inode.
  27716. + */
  27717. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  27718. + struct dentry *h_dentry)
  27719. +{
  27720. + struct super_block *sb;
  27721. + struct au_sbinfo *sbinfo;
  27722. + struct hlist_bl_head *hbl;
  27723. + struct hlist_bl_node *pos;
  27724. + struct au_icntnr *icntnr;
  27725. + int found, err, cnt, i;
  27726. +
  27727. + sb = inode->i_sb;
  27728. + sbinfo = au_sbi(sb);
  27729. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27730. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27731. +
  27732. + found = au_plink_test(inode);
  27733. + if (found)
  27734. + return;
  27735. +
  27736. + i = au_plink_hash(inode->i_ino);
  27737. + hbl = sbinfo->si_plink + i;
  27738. + au_igrab(inode);
  27739. +
  27740. + hlist_bl_lock(hbl);
  27741. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
  27742. + if (&icntnr->vfs_inode == inode) {
  27743. + found = 1;
  27744. + break;
  27745. + }
  27746. + }
  27747. + if (!found) {
  27748. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  27749. + hlist_bl_add_head(&icntnr->plink, hbl);
  27750. + }
  27751. + hlist_bl_unlock(hbl);
  27752. + if (!found) {
  27753. + cnt = au_hbl_count(hbl);
  27754. +#define msg "unexpectedly unbalanced or too many pseudo-links"
  27755. + if (cnt > AUFS_PLINK_WARN)
  27756. + AuWarn1(msg ", %d\n", cnt);
  27757. +#undef msg
  27758. + err = whplink(h_dentry, inode, bindex);
  27759. + if (unlikely(err)) {
  27760. + pr_warn("err %d, damaged pseudo link.\n", err);
  27761. + au_hbl_del(&icntnr->plink, hbl);
  27762. + iput(&icntnr->vfs_inode);
  27763. + }
  27764. + } else
  27765. + iput(&icntnr->vfs_inode);
  27766. +}
  27767. +
  27768. +/* free all plinks */
  27769. +void au_plink_put(struct super_block *sb, int verbose)
  27770. +{
  27771. + int i, warned;
  27772. + struct au_sbinfo *sbinfo;
  27773. + struct hlist_bl_head *hbl;
  27774. + struct hlist_bl_node *pos, *tmp;
  27775. + struct au_icntnr *icntnr;
  27776. +
  27777. + SiMustWriteLock(sb);
  27778. +
  27779. + sbinfo = au_sbi(sb);
  27780. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27781. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27782. +
  27783. + /* no spin_lock since sbinfo is write-locked */
  27784. + warned = 0;
  27785. + for (i = 0; i < AuPlink_NHASH; i++) {
  27786. + hbl = sbinfo->si_plink + i;
  27787. + if (!warned && verbose && !hlist_bl_empty(hbl)) {
  27788. + pr_warn("pseudo-link is not flushed");
  27789. + warned = 1;
  27790. + }
  27791. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
  27792. + iput(&icntnr->vfs_inode);
  27793. + INIT_HLIST_BL_HEAD(hbl);
  27794. + }
  27795. +}
  27796. +
  27797. +void au_plink_clean(struct super_block *sb, int verbose)
  27798. +{
  27799. + struct dentry *root;
  27800. +
  27801. + root = sb->s_root;
  27802. + aufs_write_lock(root);
  27803. + if (au_opt_test(au_mntflags(sb), PLINK))
  27804. + au_plink_put(sb, verbose);
  27805. + aufs_write_unlock(root);
  27806. +}
  27807. +
  27808. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  27809. +{
  27810. + int do_put;
  27811. + aufs_bindex_t btop, bbot, bindex;
  27812. +
  27813. + do_put = 0;
  27814. + btop = au_ibtop(inode);
  27815. + bbot = au_ibbot(inode);
  27816. + if (btop >= 0) {
  27817. + for (bindex = btop; bindex <= bbot; bindex++) {
  27818. + if (!au_h_iptr(inode, bindex)
  27819. + || au_ii_br_id(inode, bindex) != br_id)
  27820. + continue;
  27821. + au_set_h_iptr(inode, bindex, NULL, 0);
  27822. + do_put = 1;
  27823. + break;
  27824. + }
  27825. + if (do_put)
  27826. + for (bindex = btop; bindex <= bbot; bindex++)
  27827. + if (au_h_iptr(inode, bindex)) {
  27828. + do_put = 0;
  27829. + break;
  27830. + }
  27831. + } else
  27832. + do_put = 1;
  27833. +
  27834. + return do_put;
  27835. +}
  27836. +
  27837. +/* free the plinks on a branch specified by @br_id */
  27838. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  27839. +{
  27840. + struct au_sbinfo *sbinfo;
  27841. + struct hlist_bl_head *hbl;
  27842. + struct hlist_bl_node *pos, *tmp;
  27843. + struct au_icntnr *icntnr;
  27844. + struct inode *inode;
  27845. + int i, do_put;
  27846. +
  27847. + SiMustWriteLock(sb);
  27848. +
  27849. + sbinfo = au_sbi(sb);
  27850. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27851. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27852. +
  27853. + /* no bit_lock since sbinfo is write-locked */
  27854. + for (i = 0; i < AuPlink_NHASH; i++) {
  27855. + hbl = sbinfo->si_plink + i;
  27856. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
  27857. + inode = au_igrab(&icntnr->vfs_inode);
  27858. + ii_write_lock_child(inode);
  27859. + do_put = au_plink_do_half_refresh(inode, br_id);
  27860. + if (do_put) {
  27861. + hlist_bl_del(&icntnr->plink);
  27862. + iput(inode);
  27863. + }
  27864. + ii_write_unlock(inode);
  27865. + iput(inode);
  27866. + }
  27867. + }
  27868. +}
  27869. diff -Naur null/fs/aufs/poll.c linux-6.6.63/fs/aufs/poll.c
  27870. --- /dev/null
  27871. +++ linux-6.6.63/fs/aufs/poll.c 2024-12-18 15:12:59.299519538 +0300
  27872. @@ -0,0 +1,51 @@
  27873. +// SPDX-License-Identifier: GPL-2.0
  27874. +/*
  27875. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  27876. + *
  27877. + * This program is free software; you can redistribute it and/or modify
  27878. + * it under the terms of the GNU General Public License as published by
  27879. + * the Free Software Foundation; either version 2 of the License, or
  27880. + * (at your option) any later version.
  27881. + *
  27882. + * This program is distributed in the hope that it will be useful,
  27883. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27884. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27885. + * GNU General Public License for more details.
  27886. + *
  27887. + * You should have received a copy of the GNU General Public License
  27888. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27889. + */
  27890. +
  27891. +/*
  27892. + * poll operation
  27893. + * There is only one filesystem which implements ->poll operation, currently.
  27894. + */
  27895. +
  27896. +#include "aufs.h"
  27897. +
  27898. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
  27899. +{
  27900. + __poll_t mask;
  27901. + struct file *h_file;
  27902. + struct super_block *sb;
  27903. +
  27904. + /* We should pretend an error happened. */
  27905. + mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
  27906. + sb = file->f_path.dentry->d_sb;
  27907. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  27908. +
  27909. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  27910. + if (IS_ERR(h_file)) {
  27911. + AuDbg("h_file %ld\n", PTR_ERR(h_file));
  27912. + goto out;
  27913. + }
  27914. +
  27915. + mask = vfs_poll(h_file, pt);
  27916. + fput(h_file); /* instead of au_read_post() */
  27917. +
  27918. +out:
  27919. + si_read_unlock(sb);
  27920. + if (mask & EPOLLERR)
  27921. + AuDbg("mask 0x%x\n", mask);
  27922. + return mask;
  27923. +}
  27924. diff -Naur null/fs/aufs/posix_acl.c linux-6.6.63/fs/aufs/posix_acl.c
  27925. --- /dev/null
  27926. +++ linux-6.6.63/fs/aufs/posix_acl.c 2024-12-18 15:12:59.299519538 +0300
  27927. @@ -0,0 +1,108 @@
  27928. +// SPDX-License-Identifier: GPL-2.0
  27929. +/*
  27930. + * Copyright (C) 2014-2022 Junjiro R. Okajima
  27931. + *
  27932. + * This program is free software; you can redistribute it and/or modify
  27933. + * it under the terms of the GNU General Public License as published by
  27934. + * the Free Software Foundation; either version 2 of the License, or
  27935. + * (at your option) any later version.
  27936. + *
  27937. + * This program is distributed in the hope that it will be useful,
  27938. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27939. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27940. + * GNU General Public License for more details.
  27941. + *
  27942. + * You should have received a copy of the GNU General Public License
  27943. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27944. + */
  27945. +
  27946. +/*
  27947. + * posix acl operations
  27948. + */
  27949. +
  27950. +#include <linux/fs.h>
  27951. +#include "aufs.h"
  27952. +
  27953. +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
  27954. +{
  27955. + struct posix_acl *acl;
  27956. + int err;
  27957. + aufs_bindex_t bindex;
  27958. + struct inode *h_inode;
  27959. + struct super_block *sb;
  27960. +
  27961. + acl = ERR_PTR(-ECHILD);
  27962. + if (rcu)
  27963. + goto out;
  27964. +
  27965. + acl = NULL;
  27966. + sb = inode->i_sb;
  27967. + si_read_lock(sb, AuLock_FLUSH);
  27968. + ii_read_lock_child(inode);
  27969. + if (!(sb->s_flags & SB_POSIXACL))
  27970. + goto unlock;
  27971. +
  27972. + bindex = au_ibtop(inode);
  27973. + h_inode = au_h_iptr(inode, bindex);
  27974. + if (unlikely(!h_inode
  27975. + || ((h_inode->i_mode & S_IFMT)
  27976. + != (inode->i_mode & S_IFMT)))) {
  27977. + err = au_busy_or_stale();
  27978. + acl = ERR_PTR(err);
  27979. + goto unlock;
  27980. + }
  27981. +
  27982. + /* always topmost only */
  27983. + acl = get_inode_acl(h_inode, type);
  27984. + if (IS_ERR(acl))
  27985. + forget_cached_acl(inode, type);
  27986. + else
  27987. + set_cached_acl(inode, type, acl);
  27988. +
  27989. +unlock:
  27990. + ii_read_unlock(inode);
  27991. + si_read_unlock(sb);
  27992. +
  27993. +out:
  27994. + AuTraceErrPtr(acl);
  27995. + return acl;
  27996. +}
  27997. +
  27998. +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
  27999. + struct dentry *dentry, int type)
  28000. +{
  28001. + struct posix_acl *acl;
  28002. + struct inode *inode;
  28003. +
  28004. + inode = d_inode(dentry);
  28005. + acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
  28006. +
  28007. + return acl;
  28008. +}
  28009. +
  28010. +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  28011. + struct posix_acl *acl, int type)
  28012. +{
  28013. + int err;
  28014. + ssize_t ssz;
  28015. + struct inode *inode;
  28016. + struct au_sxattr arg = {
  28017. + .type = AU_ACL_SET,
  28018. + .u.acl_set = {
  28019. + .acl = acl,
  28020. + .type = type
  28021. + },
  28022. + };
  28023. +
  28024. + inode = d_inode(dentry);
  28025. + IMustLock(inode);
  28026. +
  28027. + ssz = au_sxattr(dentry, inode, &arg);
  28028. + /* forget even it if succeeds since the branch might set differently */
  28029. + forget_cached_acl(inode, type);
  28030. + err = ssz;
  28031. + if (ssz >= 0)
  28032. + err = 0;
  28033. +
  28034. + return err;
  28035. +}
  28036. diff -Naur null/fs/aufs/procfs.c linux-6.6.63/fs/aufs/procfs.c
  28037. --- /dev/null
  28038. +++ linux-6.6.63/fs/aufs/procfs.c 2024-12-18 15:12:59.299519538 +0300
  28039. @@ -0,0 +1,170 @@
  28040. +// SPDX-License-Identifier: GPL-2.0
  28041. +/*
  28042. + * Copyright (C) 2010-2022 Junjiro R. Okajima
  28043. + *
  28044. + * This program is free software; you can redistribute it and/or modify
  28045. + * it under the terms of the GNU General Public License as published by
  28046. + * the Free Software Foundation; either version 2 of the License, or
  28047. + * (at your option) any later version.
  28048. + *
  28049. + * This program is distributed in the hope that it will be useful,
  28050. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28051. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28052. + * GNU General Public License for more details.
  28053. + *
  28054. + * You should have received a copy of the GNU General Public License
  28055. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28056. + */
  28057. +
  28058. +/*
  28059. + * procfs interfaces
  28060. + */
  28061. +
  28062. +#include <linux/proc_fs.h>
  28063. +#include "aufs.h"
  28064. +
  28065. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  28066. +{
  28067. + struct au_sbinfo *sbinfo;
  28068. +
  28069. + sbinfo = file->private_data;
  28070. + if (sbinfo) {
  28071. + au_plink_maint_leave(sbinfo);
  28072. + kobject_put(&sbinfo->si_kobj);
  28073. + }
  28074. +
  28075. + return 0;
  28076. +}
  28077. +
  28078. +static void au_procfs_plm_write_clean(struct file *file)
  28079. +{
  28080. + struct au_sbinfo *sbinfo;
  28081. +
  28082. + sbinfo = file->private_data;
  28083. + if (sbinfo)
  28084. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  28085. +}
  28086. +
  28087. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  28088. +{
  28089. + int err;
  28090. + struct super_block *sb;
  28091. + struct au_sbinfo *sbinfo;
  28092. + struct hlist_bl_node *pos;
  28093. +
  28094. + err = -EBUSY;
  28095. + if (unlikely(file->private_data))
  28096. + goto out;
  28097. +
  28098. + sb = NULL;
  28099. + /* don't use au_sbilist_lock() here */
  28100. + hlist_bl_lock(&au_sbilist);
  28101. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  28102. + if (id == sysaufs_si_id(sbinfo)) {
  28103. + if (kobject_get_unless_zero(&sbinfo->si_kobj))
  28104. + sb = sbinfo->si_sb;
  28105. + break;
  28106. + }
  28107. + hlist_bl_unlock(&au_sbilist);
  28108. +
  28109. + err = -EINVAL;
  28110. + if (unlikely(!sb))
  28111. + goto out;
  28112. +
  28113. + err = au_plink_maint_enter(sb);
  28114. + if (!err)
  28115. + /* keep kobject_get() */
  28116. + file->private_data = sbinfo;
  28117. + else
  28118. + kobject_put(&sbinfo->si_kobj);
  28119. +out:
  28120. + return err;
  28121. +}
  28122. +
  28123. +/*
  28124. + * Accept a valid "si=xxxx" only.
  28125. + * Once it is accepted successfully, accept "clean" too.
  28126. + */
  28127. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  28128. + size_t count, loff_t *ppos)
  28129. +{
  28130. + ssize_t err;
  28131. + unsigned long id;
  28132. + /* last newline is allowed */
  28133. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  28134. +
  28135. + err = -EACCES;
  28136. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  28137. + goto out;
  28138. +
  28139. + err = -EINVAL;
  28140. + if (unlikely(count > sizeof(buf)))
  28141. + goto out;
  28142. +
  28143. + err = copy_from_user(buf, ubuf, count);
  28144. + if (unlikely(err)) {
  28145. + err = -EFAULT;
  28146. + goto out;
  28147. + }
  28148. + buf[count] = 0;
  28149. +
  28150. + err = -EINVAL;
  28151. + if (!strcmp("clean", buf)) {
  28152. + au_procfs_plm_write_clean(file);
  28153. + goto out_success;
  28154. + } else if (unlikely(strncmp("si=", buf, 3)))
  28155. + goto out;
  28156. +
  28157. + err = kstrtoul(buf + 3, 16, &id);
  28158. + if (unlikely(err))
  28159. + goto out;
  28160. +
  28161. + err = au_procfs_plm_write_si(file, id);
  28162. + if (unlikely(err))
  28163. + goto out;
  28164. +
  28165. +out_success:
  28166. + err = count; /* success */
  28167. +out:
  28168. + return err;
  28169. +}
  28170. +
  28171. +static const struct proc_ops au_procfs_plm_op = {
  28172. + .proc_write = au_procfs_plm_write,
  28173. + .proc_release = au_procfs_plm_release
  28174. +};
  28175. +
  28176. +/* ---------------------------------------------------------------------- */
  28177. +
  28178. +static struct proc_dir_entry *au_procfs_dir;
  28179. +
  28180. +void au_procfs_fin(void)
  28181. +{
  28182. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  28183. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  28184. +}
  28185. +
  28186. +int __init au_procfs_init(void)
  28187. +{
  28188. + int err;
  28189. + struct proc_dir_entry *entry;
  28190. +
  28191. + err = -ENOMEM;
  28192. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  28193. + if (unlikely(!au_procfs_dir))
  28194. + goto out;
  28195. +
  28196. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
  28197. + au_procfs_dir, &au_procfs_plm_op);
  28198. + if (unlikely(!entry))
  28199. + goto out_dir;
  28200. +
  28201. + err = 0;
  28202. + goto out; /* success */
  28203. +
  28204. +
  28205. +out_dir:
  28206. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  28207. +out:
  28208. + return err;
  28209. +}
  28210. diff -Naur null/fs/aufs/rdu.c linux-6.6.63/fs/aufs/rdu.c
  28211. --- /dev/null
  28212. +++ linux-6.6.63/fs/aufs/rdu.c 2024-12-18 15:12:59.299519538 +0300
  28213. @@ -0,0 +1,384 @@
  28214. +// SPDX-License-Identifier: GPL-2.0
  28215. +/*
  28216. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  28217. + *
  28218. + * This program is free software; you can redistribute it and/or modify
  28219. + * it under the terms of the GNU General Public License as published by
  28220. + * the Free Software Foundation; either version 2 of the License, or
  28221. + * (at your option) any later version.
  28222. + *
  28223. + * This program is distributed in the hope that it will be useful,
  28224. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28225. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28226. + * GNU General Public License for more details.
  28227. + *
  28228. + * You should have received a copy of the GNU General Public License
  28229. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28230. + */
  28231. +
  28232. +/*
  28233. + * readdir in userspace.
  28234. + */
  28235. +
  28236. +#include <linux/compat.h>
  28237. +#include <linux/fs_stack.h>
  28238. +#include <linux/security.h>
  28239. +#include "aufs.h"
  28240. +
  28241. +/* bits for struct aufs_rdu.flags */
  28242. +#define AuRdu_CALLED 1
  28243. +#define AuRdu_CONT (1 << 1)
  28244. +#define AuRdu_FULL (1 << 2)
  28245. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  28246. +#define au_fset_rdu(flags, name) \
  28247. + do { (flags) |= AuRdu_##name; } while (0)
  28248. +#define au_fclr_rdu(flags, name) \
  28249. + do { (flags) &= ~AuRdu_##name; } while (0)
  28250. +
  28251. +struct au_rdu_arg {
  28252. + struct dir_context ctx;
  28253. + struct aufs_rdu *rdu;
  28254. + union au_rdu_ent_ul ent;
  28255. + unsigned long end;
  28256. +
  28257. + struct super_block *sb;
  28258. + int err;
  28259. +};
  28260. +
  28261. +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  28262. + loff_t offset, u64 h_ino, unsigned int d_type)
  28263. +{
  28264. + int err, len;
  28265. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  28266. + struct aufs_rdu *rdu = arg->rdu;
  28267. + struct au_rdu_ent ent;
  28268. +
  28269. + err = 0;
  28270. + arg->err = 0;
  28271. + au_fset_rdu(rdu->cookie.flags, CALLED);
  28272. + len = au_rdu_len(nlen);
  28273. + if (arg->ent.ul + len < arg->end) {
  28274. + ent.ino = h_ino;
  28275. + ent.bindex = rdu->cookie.bindex;
  28276. + ent.type = d_type;
  28277. + ent.nlen = nlen;
  28278. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  28279. + ent.type = DT_UNKNOWN;
  28280. +
  28281. + /* unnecessary to support mmap_sem since this is a dir */
  28282. + err = -EFAULT;
  28283. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  28284. + goto out;
  28285. + if (copy_to_user(arg->ent.e->name, name, nlen))
  28286. + goto out;
  28287. + /* the terminating NULL */
  28288. + if (__put_user(0, arg->ent.e->name + nlen))
  28289. + goto out;
  28290. + err = 0;
  28291. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  28292. + arg->ent.ul += len;
  28293. + rdu->rent++;
  28294. + } else {
  28295. + err = -EFAULT;
  28296. + au_fset_rdu(rdu->cookie.flags, FULL);
  28297. + rdu->full = 1;
  28298. + rdu->tail = arg->ent;
  28299. + }
  28300. +
  28301. +out:
  28302. + /* AuTraceErr(err); */
  28303. + return !err;
  28304. +}
  28305. +
  28306. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  28307. +{
  28308. + int err;
  28309. + loff_t offset;
  28310. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  28311. +
  28312. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  28313. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  28314. + err = offset;
  28315. + if (unlikely(offset != cookie->h_pos))
  28316. + goto out;
  28317. +
  28318. + err = 0;
  28319. + do {
  28320. + arg->err = 0;
  28321. + au_fclr_rdu(cookie->flags, CALLED);
  28322. + /* smp_mb(); */
  28323. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  28324. + if (err >= 0)
  28325. + err = arg->err;
  28326. + } while (!err
  28327. + && au_ftest_rdu(cookie->flags, CALLED)
  28328. + && !au_ftest_rdu(cookie->flags, FULL));
  28329. + cookie->h_pos = h_file->f_pos;
  28330. +
  28331. +out:
  28332. + AuTraceErr(err);
  28333. + return err;
  28334. +}
  28335. +
  28336. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  28337. +{
  28338. + int err;
  28339. + aufs_bindex_t bbot;
  28340. + struct au_rdu_arg arg = {
  28341. + .ctx = {
  28342. + .actor = au_rdu_fill
  28343. + }
  28344. + };
  28345. + struct dentry *dentry;
  28346. + struct inode *inode;
  28347. + struct file *h_file;
  28348. + struct au_rdu_cookie *cookie = &rdu->cookie;
  28349. +
  28350. + /* VERIFY_WRITE */
  28351. + err = !access_ok(rdu->ent.e, rdu->sz);
  28352. + if (unlikely(err)) {
  28353. + err = -EFAULT;
  28354. + AuTraceErr(err);
  28355. + goto out;
  28356. + }
  28357. + rdu->rent = 0;
  28358. + rdu->tail = rdu->ent;
  28359. + rdu->full = 0;
  28360. + arg.rdu = rdu;
  28361. + arg.ent = rdu->ent;
  28362. + arg.end = arg.ent.ul;
  28363. + arg.end += rdu->sz;
  28364. +
  28365. + err = -ENOTDIR;
  28366. + if (unlikely(!file->f_op->iterate_shared))
  28367. + goto out;
  28368. +
  28369. + err = security_file_permission(file, MAY_READ);
  28370. + AuTraceErr(err);
  28371. + if (unlikely(err))
  28372. + goto out;
  28373. +
  28374. + dentry = file->f_path.dentry;
  28375. + inode = d_inode(dentry);
  28376. + inode_lock_shared(inode);
  28377. +
  28378. + arg.sb = inode->i_sb;
  28379. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  28380. + if (unlikely(err))
  28381. + goto out_mtx;
  28382. + err = au_alive_dir(dentry);
  28383. + if (unlikely(err))
  28384. + goto out_si;
  28385. + /* todo: reval? */
  28386. + fi_read_lock(file);
  28387. +
  28388. + err = -EAGAIN;
  28389. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  28390. + && cookie->generation != au_figen(file)))
  28391. + goto out_unlock;
  28392. +
  28393. + err = 0;
  28394. + if (!rdu->blk) {
  28395. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  28396. + if (!rdu->blk)
  28397. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  28398. + }
  28399. + bbot = au_fbtop(file);
  28400. + if (cookie->bindex < bbot)
  28401. + cookie->bindex = bbot;
  28402. + bbot = au_fbbot_dir(file);
  28403. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  28404. + for (; !err && cookie->bindex <= bbot;
  28405. + cookie->bindex++, cookie->h_pos = 0) {
  28406. + h_file = au_hf_dir(file, cookie->bindex);
  28407. + if (!h_file)
  28408. + continue;
  28409. +
  28410. + au_fclr_rdu(cookie->flags, FULL);
  28411. + err = au_rdu_do(h_file, &arg);
  28412. + AuTraceErr(err);
  28413. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  28414. + break;
  28415. + }
  28416. + AuDbg("rent %llu\n", rdu->rent);
  28417. +
  28418. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  28419. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  28420. + au_fset_rdu(cookie->flags, CONT);
  28421. + cookie->generation = au_figen(file);
  28422. + }
  28423. +
  28424. + ii_read_lock_child(inode);
  28425. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  28426. + ii_read_unlock(inode);
  28427. +
  28428. +out_unlock:
  28429. + fi_read_unlock(file);
  28430. +out_si:
  28431. + si_read_unlock(arg.sb);
  28432. +out_mtx:
  28433. + inode_unlock_shared(inode);
  28434. +out:
  28435. + AuTraceErr(err);
  28436. + return err;
  28437. +}
  28438. +
  28439. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  28440. +{
  28441. + int err;
  28442. + ino_t ino;
  28443. + unsigned long long nent;
  28444. + union au_rdu_ent_ul *u;
  28445. + struct au_rdu_ent ent;
  28446. + struct super_block *sb;
  28447. +
  28448. + err = 0;
  28449. + nent = rdu->nent;
  28450. + u = &rdu->ent;
  28451. + sb = file->f_path.dentry->d_sb;
  28452. + si_read_lock(sb, AuLock_FLUSH);
  28453. + while (nent-- > 0) {
  28454. + /* unnecessary to support mmap_sem since this is a dir */
  28455. + err = copy_from_user(&ent, u->e, sizeof(ent));
  28456. + if (!err)
  28457. + /* VERIFY_WRITE */
  28458. + err = !access_ok(&u->e->ino, sizeof(ino));
  28459. + if (unlikely(err)) {
  28460. + err = -EFAULT;
  28461. + AuTraceErr(err);
  28462. + break;
  28463. + }
  28464. +
  28465. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  28466. + if (!ent.wh)
  28467. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  28468. + else
  28469. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  28470. + &ino);
  28471. + if (unlikely(err)) {
  28472. + AuTraceErr(err);
  28473. + break;
  28474. + }
  28475. +
  28476. + err = __put_user(ino, &u->e->ino);
  28477. + if (unlikely(err)) {
  28478. + err = -EFAULT;
  28479. + AuTraceErr(err);
  28480. + break;
  28481. + }
  28482. + u->ul += au_rdu_len(ent.nlen);
  28483. + }
  28484. + si_read_unlock(sb);
  28485. +
  28486. + return err;
  28487. +}
  28488. +
  28489. +/* ---------------------------------------------------------------------- */
  28490. +
  28491. +static int au_rdu_verify(struct aufs_rdu *rdu)
  28492. +{
  28493. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  28494. + "%llu, b%d, 0x%x, g%u}\n",
  28495. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  28496. + rdu->blk,
  28497. + rdu->rent, rdu->shwh, rdu->full,
  28498. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  28499. + rdu->cookie.generation);
  28500. +
  28501. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  28502. + return 0;
  28503. +
  28504. + AuDbg("%u:%u\n",
  28505. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  28506. + return -EINVAL;
  28507. +}
  28508. +
  28509. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  28510. +{
  28511. + long err, e;
  28512. + struct aufs_rdu rdu;
  28513. + void __user *p = (void __user *)arg;
  28514. +
  28515. + err = copy_from_user(&rdu, p, sizeof(rdu));
  28516. + if (unlikely(err)) {
  28517. + err = -EFAULT;
  28518. + AuTraceErr(err);
  28519. + goto out;
  28520. + }
  28521. + err = au_rdu_verify(&rdu);
  28522. + if (unlikely(err))
  28523. + goto out;
  28524. +
  28525. + switch (cmd) {
  28526. + case AUFS_CTL_RDU:
  28527. + err = au_rdu(file, &rdu);
  28528. + if (unlikely(err))
  28529. + break;
  28530. +
  28531. + e = copy_to_user(p, &rdu, sizeof(rdu));
  28532. + if (unlikely(e)) {
  28533. + err = -EFAULT;
  28534. + AuTraceErr(err);
  28535. + }
  28536. + break;
  28537. + case AUFS_CTL_RDU_INO:
  28538. + err = au_rdu_ino(file, &rdu);
  28539. + break;
  28540. +
  28541. + default:
  28542. + /* err = -ENOTTY; */
  28543. + err = -EINVAL;
  28544. + }
  28545. +
  28546. +out:
  28547. + AuTraceErr(err);
  28548. + return err;
  28549. +}
  28550. +
  28551. +#ifdef CONFIG_COMPAT
  28552. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  28553. +{
  28554. + long err, e;
  28555. + struct aufs_rdu rdu;
  28556. + void __user *p = compat_ptr(arg);
  28557. +
  28558. + /* todo: get_user()? */
  28559. + err = copy_from_user(&rdu, p, sizeof(rdu));
  28560. + if (unlikely(err)) {
  28561. + err = -EFAULT;
  28562. + AuTraceErr(err);
  28563. + goto out;
  28564. + }
  28565. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  28566. + err = au_rdu_verify(&rdu);
  28567. + if (unlikely(err))
  28568. + goto out;
  28569. +
  28570. + switch (cmd) {
  28571. + case AUFS_CTL_RDU:
  28572. + err = au_rdu(file, &rdu);
  28573. + if (unlikely(err))
  28574. + break;
  28575. +
  28576. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  28577. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  28578. + e = copy_to_user(p, &rdu, sizeof(rdu));
  28579. + if (unlikely(e)) {
  28580. + err = -EFAULT;
  28581. + AuTraceErr(err);
  28582. + }
  28583. + break;
  28584. + case AUFS_CTL_RDU_INO:
  28585. + err = au_rdu_ino(file, &rdu);
  28586. + break;
  28587. +
  28588. + default:
  28589. + /* err = -ENOTTY; */
  28590. + err = -EINVAL;
  28591. + }
  28592. +
  28593. +out:
  28594. + AuTraceErr(err);
  28595. + return err;
  28596. +}
  28597. +#endif
  28598. diff -Naur null/fs/aufs/rwsem.h linux-6.6.63/fs/aufs/rwsem.h
  28599. --- /dev/null
  28600. +++ linux-6.6.63/fs/aufs/rwsem.h 2024-12-18 15:12:59.299519538 +0300
  28601. @@ -0,0 +1,85 @@
  28602. +/* SPDX-License-Identifier: GPL-2.0 */
  28603. +/*
  28604. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  28605. + *
  28606. + * This program is free software; you can redistribute it and/or modify
  28607. + * it under the terms of the GNU General Public License as published by
  28608. + * the Free Software Foundation; either version 2 of the License, or
  28609. + * (at your option) any later version.
  28610. + *
  28611. + * This program is distributed in the hope that it will be useful,
  28612. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28613. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28614. + * GNU General Public License for more details.
  28615. + *
  28616. + * You should have received a copy of the GNU General Public License
  28617. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28618. + */
  28619. +
  28620. +/*
  28621. + * simple read-write semaphore wrappers
  28622. + */
  28623. +
  28624. +#ifndef __AUFS_RWSEM_H__
  28625. +#define __AUFS_RWSEM_H__
  28626. +
  28627. +#ifdef __KERNEL__
  28628. +
  28629. +#include "debug.h"
  28630. +
  28631. +/* in the future, the name 'au_rwsem' will be totally gone */
  28632. +#define au_rwsem rw_semaphore
  28633. +
  28634. +/* to debug easier, do not make them inlined functions */
  28635. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
  28636. +
  28637. +#ifdef CONFIG_LOCKDEP
  28638. +/* rwsem_is_locked() is unusable */
  28639. +#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28640. + && !lockdep_recursing(current) \
  28641. + && debug_locks \
  28642. + && !lockdep_is_held_type(rw, 1))
  28643. +#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28644. + && !lockdep_recursing(current) \
  28645. + && debug_locks \
  28646. + && !lockdep_is_held_type(rw, 0))
  28647. +#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28648. + && !lockdep_recursing(current) \
  28649. + && debug_locks \
  28650. + && !lockdep_is_held(rw))
  28651. +#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28652. + && !lockdep_recursing(current) \
  28653. + && debug_locks \
  28654. + && lockdep_is_held(rw))
  28655. +#else
  28656. +AuStubVoid(AuRwMustReadLock, struct rw_semaphore *rw)
  28657. +AuStubVoid(AuRwMustWriteLock, struct rw_semaphore *rw)
  28658. +AuStubVoid(AuRwMustAnyLock, struct rw_semaphore *rw)
  28659. +AuStubVoid(AuRwDestroy, struct rw_semaphore *rw)
  28660. +#endif
  28661. +
  28662. +#define au_rw_init(rw) init_rwsem(rw)
  28663. +
  28664. +#define au_rw_init_wlock(rw) do { \
  28665. + au_rw_init(rw); \
  28666. + down_write(rw); \
  28667. + } while (0)
  28668. +
  28669. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  28670. + au_rw_init(rw); \
  28671. + down_write_nested(rw, lsc); \
  28672. + } while (0)
  28673. +
  28674. +#define au_rw_read_lock(rw) down_read(rw)
  28675. +#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
  28676. +#define au_rw_read_unlock(rw) up_read(rw)
  28677. +#define au_rw_dgrade_lock(rw) downgrade_write(rw)
  28678. +#define au_rw_write_lock(rw) down_write(rw)
  28679. +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
  28680. +#define au_rw_write_unlock(rw) up_write(rw)
  28681. +/* why is not _nested version defined? */
  28682. +#define au_rw_read_trylock(rw) down_read_trylock(rw)
  28683. +#define au_rw_write_trylock(rw) down_write_trylock(rw)
  28684. +
  28685. +#endif /* __KERNEL__ */
  28686. +#endif /* __AUFS_RWSEM_H__ */
  28687. diff -Naur null/fs/aufs/sbinfo.c linux-6.6.63/fs/aufs/sbinfo.c
  28688. --- /dev/null
  28689. +++ linux-6.6.63/fs/aufs/sbinfo.c 2024-12-18 15:12:59.299519538 +0300
  28690. @@ -0,0 +1,316 @@
  28691. +// SPDX-License-Identifier: GPL-2.0
  28692. +/*
  28693. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  28694. + *
  28695. + * This program is free software; you can redistribute it and/or modify
  28696. + * it under the terms of the GNU General Public License as published by
  28697. + * the Free Software Foundation; either version 2 of the License, or
  28698. + * (at your option) any later version.
  28699. + *
  28700. + * This program is distributed in the hope that it will be useful,
  28701. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28702. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28703. + * GNU General Public License for more details.
  28704. + *
  28705. + * You should have received a copy of the GNU General Public License
  28706. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28707. + */
  28708. +
  28709. +/*
  28710. + * superblock private data
  28711. + */
  28712. +
  28713. +#include <linux/iversion.h>
  28714. +#include "aufs.h"
  28715. +
  28716. +/*
  28717. + * they are necessary regardless sysfs is disabled.
  28718. + */
  28719. +void au_si_free(struct kobject *kobj)
  28720. +{
  28721. + int i;
  28722. + struct au_sbinfo *sbinfo;
  28723. + char *locked __maybe_unused; /* debug only */
  28724. +
  28725. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  28726. + for (i = 0; i < AuPlink_NHASH; i++)
  28727. + AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
  28728. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  28729. +
  28730. + AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
  28731. + au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
  28732. + AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
  28733. + au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
  28734. +
  28735. + dbgaufs_si_fin(sbinfo);
  28736. + au_rw_write_lock(&sbinfo->si_rwsem);
  28737. + au_br_free(sbinfo);
  28738. + au_rw_write_unlock(&sbinfo->si_rwsem);
  28739. +
  28740. + au_kfree_try_rcu(sbinfo->si_branch);
  28741. + mutex_destroy(&sbinfo->si_xib_mtx);
  28742. + AuRwDestroy(&sbinfo->si_rwsem);
  28743. +
  28744. + au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
  28745. + /* si_nfiles is waited too */
  28746. + au_kfree_rcu(sbinfo);
  28747. +}
  28748. +
  28749. +struct au_sbinfo *au_si_alloc(struct super_block *sb)
  28750. +{
  28751. + struct au_sbinfo *sbinfo;
  28752. + int err, i;
  28753. +
  28754. + err = -ENOMEM;
  28755. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  28756. + if (unlikely(!sbinfo))
  28757. + goto out;
  28758. +
  28759. + /* will be reallocated separately */
  28760. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  28761. + if (unlikely(!sbinfo->si_branch))
  28762. + goto out_sbinfo;
  28763. +
  28764. + err = sysaufs_si_init(sbinfo);
  28765. + if (!err) {
  28766. + dbgaufs_si_null(sbinfo);
  28767. + err = dbgaufs_si_init(sbinfo);
  28768. + if (unlikely(err))
  28769. + kobject_put(&sbinfo->si_kobj);
  28770. + }
  28771. + if (unlikely(err))
  28772. + goto out_br;
  28773. +
  28774. + au_nwt_init(&sbinfo->si_nowait);
  28775. + au_rw_init_wlock(&sbinfo->si_rwsem);
  28776. +
  28777. + au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
  28778. + au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
  28779. +
  28780. + sbinfo->si_bbot = -1;
  28781. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  28782. +
  28783. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  28784. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  28785. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  28786. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  28787. +
  28788. + au_fhsm_init(sbinfo);
  28789. +
  28790. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  28791. +
  28792. + sbinfo->si_xino_jiffy = jiffies;
  28793. + sbinfo->si_xino_expire
  28794. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  28795. + mutex_init(&sbinfo->si_xib_mtx);
  28796. + /* leave si_xib_last_pindex and si_xib_next_bit */
  28797. +
  28798. + INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
  28799. +
  28800. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  28801. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  28802. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  28803. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  28804. +
  28805. + for (i = 0; i < AuPlink_NHASH; i++)
  28806. + INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
  28807. + init_waitqueue_head(&sbinfo->si_plink_wq);
  28808. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  28809. +
  28810. + INIT_HLIST_BL_HEAD(&sbinfo->si_files);
  28811. +
  28812. + /* with getattr by default */
  28813. + sbinfo->si_iop_array = aufs_iop;
  28814. +
  28815. + /* leave other members for sysaufs and si_mnt. */
  28816. + sbinfo->si_sb = sb;
  28817. + if (sb) {
  28818. + sb->s_fs_info = sbinfo;
  28819. + si_pid_set(sb);
  28820. + }
  28821. + return sbinfo; /* success */
  28822. +
  28823. +out_br:
  28824. + au_kfree_try_rcu(sbinfo->si_branch);
  28825. +out_sbinfo:
  28826. + au_kfree_rcu(sbinfo);
  28827. +out:
  28828. + return ERR_PTR(err);
  28829. +}
  28830. +
  28831. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
  28832. +{
  28833. + int err, sz;
  28834. + struct au_branch **brp;
  28835. +
  28836. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  28837. +
  28838. + err = -ENOMEM;
  28839. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  28840. + if (unlikely(!sz))
  28841. + sz = sizeof(*brp);
  28842. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
  28843. + may_shrink);
  28844. + if (brp) {
  28845. + sbinfo->si_branch = brp;
  28846. + err = 0;
  28847. + }
  28848. +
  28849. + return err;
  28850. +}
  28851. +
  28852. +/* ---------------------------------------------------------------------- */
  28853. +
  28854. +unsigned int au_sigen_inc(struct super_block *sb)
  28855. +{
  28856. + unsigned int gen;
  28857. + struct inode *inode;
  28858. +
  28859. + SiMustWriteLock(sb);
  28860. +
  28861. + gen = ++au_sbi(sb)->si_generation;
  28862. + au_update_digen(sb->s_root);
  28863. + inode = d_inode(sb->s_root);
  28864. + au_update_iigen(inode, /*half*/0);
  28865. + inode_inc_iversion(inode);
  28866. + return gen;
  28867. +}
  28868. +
  28869. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  28870. +{
  28871. + aufs_bindex_t br_id;
  28872. + int i;
  28873. + struct au_sbinfo *sbinfo;
  28874. +
  28875. + SiMustWriteLock(sb);
  28876. +
  28877. + sbinfo = au_sbi(sb);
  28878. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  28879. + br_id = ++sbinfo->si_last_br_id;
  28880. + AuDebugOn(br_id < 0);
  28881. + if (br_id && au_br_index(sb, br_id) < 0)
  28882. + return br_id;
  28883. + }
  28884. +
  28885. + return -1;
  28886. +}
  28887. +
  28888. +/* ---------------------------------------------------------------------- */
  28889. +
  28890. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  28891. +int si_read_lock(struct super_block *sb, int flags)
  28892. +{
  28893. + int err;
  28894. +
  28895. + err = 0;
  28896. + if (au_ftest_lock(flags, FLUSH))
  28897. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28898. +
  28899. + si_noflush_read_lock(sb);
  28900. + err = au_plink_maint(sb, flags);
  28901. + if (unlikely(err))
  28902. + si_read_unlock(sb);
  28903. +
  28904. + return err;
  28905. +}
  28906. +
  28907. +int si_write_lock(struct super_block *sb, int flags)
  28908. +{
  28909. + int err;
  28910. +
  28911. + if (au_ftest_lock(flags, FLUSH))
  28912. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28913. +
  28914. + si_noflush_write_lock(sb);
  28915. + err = au_plink_maint(sb, flags);
  28916. + if (unlikely(err))
  28917. + si_write_unlock(sb);
  28918. +
  28919. + return err;
  28920. +}
  28921. +
  28922. +/* dentry and super_block lock. call at entry point */
  28923. +int aufs_read_lock(struct dentry *dentry, int flags)
  28924. +{
  28925. + int err;
  28926. + struct super_block *sb;
  28927. +
  28928. + sb = dentry->d_sb;
  28929. + err = si_read_lock(sb, flags);
  28930. + if (unlikely(err))
  28931. + goto out;
  28932. +
  28933. + if (au_ftest_lock(flags, DW))
  28934. + di_write_lock_child(dentry);
  28935. + else
  28936. + di_read_lock_child(dentry, flags);
  28937. +
  28938. + if (au_ftest_lock(flags, GEN)) {
  28939. + err = au_digen_test(dentry, au_sigen(sb));
  28940. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  28941. + AuDebugOn(!err && au_dbrange_test(dentry));
  28942. + else if (!err)
  28943. + err = au_dbrange_test(dentry);
  28944. + if (unlikely(err))
  28945. + aufs_read_unlock(dentry, flags);
  28946. + }
  28947. +
  28948. +out:
  28949. + return err;
  28950. +}
  28951. +
  28952. +void aufs_read_unlock(struct dentry *dentry, int flags)
  28953. +{
  28954. + if (au_ftest_lock(flags, DW))
  28955. + di_write_unlock(dentry);
  28956. + else
  28957. + di_read_unlock(dentry, flags);
  28958. + si_read_unlock(dentry->d_sb);
  28959. +}
  28960. +
  28961. +void aufs_write_lock(struct dentry *dentry)
  28962. +{
  28963. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  28964. + di_write_lock_child(dentry);
  28965. +}
  28966. +
  28967. +void aufs_write_unlock(struct dentry *dentry)
  28968. +{
  28969. + di_write_unlock(dentry);
  28970. + si_write_unlock(dentry->d_sb);
  28971. +}
  28972. +
  28973. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  28974. +{
  28975. + int err;
  28976. + unsigned int sigen;
  28977. + struct super_block *sb;
  28978. +
  28979. + sb = d1->d_sb;
  28980. + err = si_read_lock(sb, flags);
  28981. + if (unlikely(err))
  28982. + goto out;
  28983. +
  28984. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  28985. +
  28986. + if (au_ftest_lock(flags, GEN)) {
  28987. + sigen = au_sigen(sb);
  28988. + err = au_digen_test(d1, sigen);
  28989. + AuDebugOn(!err && au_dbrange_test(d1));
  28990. + if (!err) {
  28991. + err = au_digen_test(d2, sigen);
  28992. + AuDebugOn(!err && au_dbrange_test(d2));
  28993. + }
  28994. + if (unlikely(err))
  28995. + aufs_read_and_write_unlock2(d1, d2);
  28996. + }
  28997. +
  28998. +out:
  28999. + return err;
  29000. +}
  29001. +
  29002. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  29003. +{
  29004. + di_write_unlock2(d1, d2);
  29005. + si_read_unlock(d1->d_sb);
  29006. +}
  29007. diff -Naur null/fs/aufs/super.c linux-6.6.63/fs/aufs/super.c
  29008. --- /dev/null
  29009. +++ linux-6.6.63/fs/aufs/super.c 2024-12-18 15:12:59.299519538 +0300
  29010. @@ -0,0 +1,871 @@
  29011. +// SPDX-License-Identifier: GPL-2.0
  29012. +/*
  29013. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  29014. + *
  29015. + * This program is free software; you can redistribute it and/or modify
  29016. + * it under the terms of the GNU General Public License as published by
  29017. + * the Free Software Foundation; either version 2 of the License, or
  29018. + * (at your option) any later version.
  29019. + *
  29020. + * This program is distributed in the hope that it will be useful,
  29021. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29022. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29023. + * GNU General Public License for more details.
  29024. + *
  29025. + * You should have received a copy of the GNU General Public License
  29026. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29027. + */
  29028. +
  29029. +/*
  29030. + * mount and super_block operations
  29031. + */
  29032. +
  29033. +#include <linux/iversion.h>
  29034. +#include <linux/mm.h>
  29035. +#include <linux/seq_file.h>
  29036. +#include <linux/statfs.h>
  29037. +#include <linux/vmalloc.h>
  29038. +#include "aufs.h"
  29039. +
  29040. +/*
  29041. + * super_operations
  29042. + */
  29043. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  29044. +{
  29045. + struct au_icntnr *c;
  29046. +
  29047. + c = au_cache_alloc_icntnr(sb);
  29048. + if (c) {
  29049. + au_icntnr_init(c);
  29050. + inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
  29051. + c->iinfo.ii_hinode = NULL;
  29052. + return &c->vfs_inode;
  29053. + }
  29054. + return NULL;
  29055. +}
  29056. +
  29057. +static void aufs_destroy_inode(struct inode *inode)
  29058. +{
  29059. + if (!au_is_bad_inode(inode))
  29060. + au_iinfo_fin(inode);
  29061. +}
  29062. +
  29063. +static void aufs_free_inode(struct inode *inode)
  29064. +{
  29065. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  29066. +}
  29067. +
  29068. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  29069. +{
  29070. + struct inode *inode;
  29071. + int err;
  29072. +
  29073. + inode = iget_locked(sb, ino);
  29074. + if (unlikely(!inode)) {
  29075. + inode = ERR_PTR(-ENOMEM);
  29076. + goto out;
  29077. + }
  29078. + if (!(inode->i_state & I_NEW))
  29079. + goto out;
  29080. +
  29081. + err = au_xigen_new(inode);
  29082. + if (!err)
  29083. + err = au_iinfo_init(inode);
  29084. + if (!err)
  29085. + inode_inc_iversion(inode);
  29086. + else {
  29087. + iget_failed(inode);
  29088. + inode = ERR_PTR(err);
  29089. + }
  29090. +
  29091. +out:
  29092. + /* never return NULL */
  29093. + AuDebugOn(!inode);
  29094. + AuTraceErrPtr(inode);
  29095. + return inode;
  29096. +}
  29097. +
  29098. +/* lock free root dinfo */
  29099. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  29100. +{
  29101. + int err;
  29102. + aufs_bindex_t bindex, bbot;
  29103. + struct path path;
  29104. + struct au_hdentry *hdp;
  29105. + struct au_branch *br;
  29106. + au_br_perm_str_t perm;
  29107. +
  29108. + err = 0;
  29109. + bbot = au_sbbot(sb);
  29110. + bindex = 0;
  29111. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  29112. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  29113. + br = au_sbr(sb, bindex);
  29114. + path.mnt = au_br_mnt(br);
  29115. + path.dentry = hdp->hd_dentry;
  29116. + err = au_seq_path(seq, &path);
  29117. + if (!err) {
  29118. + au_optstr_br_perm(&perm, br->br_perm);
  29119. + seq_printf(seq, "=%s", perm.a);
  29120. + if (bindex != bbot)
  29121. + seq_putc(seq, ':');
  29122. + }
  29123. + }
  29124. + if (unlikely(err || seq_has_overflowed(seq)))
  29125. + err = -E2BIG;
  29126. +
  29127. + return err;
  29128. +}
  29129. +
  29130. +static void au_gen_fmt(char *fmt, int len, const char *pat,
  29131. + const char *append)
  29132. +{
  29133. + char *p;
  29134. +
  29135. + p = fmt;
  29136. + while (*pat != ':')
  29137. + *p++ = *pat++;
  29138. + *p++ = *pat++;
  29139. + strscpy(p, append, len - (p - fmt));
  29140. + AuDebugOn(strlen(fmt) >= len);
  29141. +}
  29142. +
  29143. +static void au_show_wbr_create(struct seq_file *m, int v,
  29144. + struct au_sbinfo *sbinfo)
  29145. +{
  29146. + const char *pat;
  29147. + char fmt[32];
  29148. + struct au_wbr_mfs *mfs;
  29149. +
  29150. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  29151. +
  29152. + seq_puts(m, ",create=");
  29153. + pat = au_optstr_wbr_create(v);
  29154. + mfs = &sbinfo->si_wbr_mfs;
  29155. + switch (v) {
  29156. + case AuWbrCreate_TDP:
  29157. + case AuWbrCreate_RR:
  29158. + case AuWbrCreate_MFS:
  29159. + case AuWbrCreate_PMFS:
  29160. + seq_puts(m, pat);
  29161. + break;
  29162. + case AuWbrCreate_MFSRR:
  29163. + case AuWbrCreate_TDMFS:
  29164. + case AuWbrCreate_PMFSRR:
  29165. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
  29166. + seq_printf(m, fmt, mfs->mfsrr_watermark);
  29167. + break;
  29168. + case AuWbrCreate_MFSV:
  29169. + case AuWbrCreate_PMFSV:
  29170. + au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
  29171. + seq_printf(m, fmt,
  29172. + jiffies_to_msecs(mfs->mfs_expire)
  29173. + / MSEC_PER_SEC);
  29174. + break;
  29175. + case AuWbrCreate_MFSRRV:
  29176. + case AuWbrCreate_TDMFSV:
  29177. + case AuWbrCreate_PMFSRRV:
  29178. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
  29179. + seq_printf(m, fmt, mfs->mfsrr_watermark,
  29180. + jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
  29181. + break;
  29182. + default:
  29183. + BUG();
  29184. + }
  29185. +}
  29186. +
  29187. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  29188. +{
  29189. +#ifdef CONFIG_SYSFS
  29190. + return 0;
  29191. +#else
  29192. + int err;
  29193. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  29194. + aufs_bindex_t bindex, brid;
  29195. + struct qstr *name;
  29196. + struct file *f;
  29197. + struct dentry *d, *h_root;
  29198. + struct au_branch *br;
  29199. +
  29200. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  29201. +
  29202. + err = 0;
  29203. + f = au_sbi(sb)->si_xib;
  29204. + if (!f)
  29205. + goto out;
  29206. +
  29207. + /* stop printing the default xino path on the first writable branch */
  29208. + h_root = NULL;
  29209. + bindex = au_xi_root(sb, f->f_path.dentry);
  29210. + if (bindex >= 0) {
  29211. + br = au_sbr_sb(sb, bindex);
  29212. + h_root = au_br_dentry(br);
  29213. + }
  29214. +
  29215. + d = f->f_path.dentry;
  29216. + name = &d->d_name;
  29217. + /* safe ->d_parent because the file is unlinked */
  29218. + if (d->d_parent == h_root
  29219. + && name->len == len
  29220. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  29221. + goto out;
  29222. +
  29223. + seq_puts(seq, ",xino=");
  29224. + err = au_xino_path(seq, f);
  29225. +
  29226. +out:
  29227. + return err;
  29228. +#endif
  29229. +}
  29230. +
  29231. +/* seq_file will re-call me in case of too long string */
  29232. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  29233. +{
  29234. + int err;
  29235. + unsigned int mnt_flags, v;
  29236. + struct super_block *sb;
  29237. + struct au_sbinfo *sbinfo;
  29238. +
  29239. +#define AuBool(name, str) do { \
  29240. + v = au_opt_test(mnt_flags, name); \
  29241. + if (v != au_opt_test(AuOpt_Def, name)) \
  29242. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  29243. +} while (0)
  29244. +
  29245. +#define AuStr(name, str) do { \
  29246. + v = mnt_flags & AuOptMask_##name; \
  29247. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  29248. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  29249. +} while (0)
  29250. +
  29251. +#define AuUInt(name, str, val) do { \
  29252. + if (val != AUFS_##name##_DEF) \
  29253. + seq_printf(m, "," #str "=%u", val); \
  29254. +} while (0)
  29255. +
  29256. + sb = dentry->d_sb;
  29257. + if (sb->s_flags & SB_POSIXACL)
  29258. + seq_puts(m, ",acl");
  29259. +#if 0 /* reserved for future use */
  29260. + if (sb->s_flags & SB_I_VERSION)
  29261. + seq_puts(m, ",i_version");
  29262. +#endif
  29263. +
  29264. + /* lock free root dinfo */
  29265. + si_noflush_read_lock(sb);
  29266. + sbinfo = au_sbi(sb);
  29267. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  29268. +
  29269. + mnt_flags = au_mntflags(sb);
  29270. + if (au_opt_test(mnt_flags, XINO)) {
  29271. + err = au_show_xino(m, sb);
  29272. + if (unlikely(err))
  29273. + goto out;
  29274. + } else
  29275. + seq_puts(m, ",noxino");
  29276. +
  29277. + AuBool(TRUNC_XINO, trunc_xino);
  29278. + AuStr(UDBA, udba);
  29279. + AuBool(SHWH, shwh);
  29280. + AuBool(PLINK, plink);
  29281. + AuBool(DIO, dio);
  29282. + AuBool(DIRPERM1, dirperm1);
  29283. +
  29284. + v = sbinfo->si_wbr_create;
  29285. + if (v != AuWbrCreate_Def)
  29286. + au_show_wbr_create(m, v, sbinfo);
  29287. +
  29288. + v = sbinfo->si_wbr_copyup;
  29289. + if (v != AuWbrCopyup_Def)
  29290. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  29291. +
  29292. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  29293. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  29294. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  29295. +
  29296. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  29297. +
  29298. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  29299. + AuUInt(RDCACHE, rdcache, v);
  29300. +
  29301. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  29302. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  29303. +
  29304. + au_fhsm_show(m, sbinfo);
  29305. +
  29306. + AuBool(DIRREN, dirren);
  29307. + AuBool(SUM, sum);
  29308. + /* AuBool(SUM_W, wsum); */
  29309. + AuBool(WARN_PERM, warn_perm);
  29310. + AuBool(VERBOSE, verbose);
  29311. +
  29312. +out:
  29313. + /* be sure to print "br:" last */
  29314. + if (!sysaufs_brs) {
  29315. + seq_puts(m, ",br:");
  29316. + au_show_brs(m, sb);
  29317. + }
  29318. + si_read_unlock(sb);
  29319. + return 0;
  29320. +
  29321. +#undef AuBool
  29322. +#undef AuStr
  29323. +#undef AuUInt
  29324. +}
  29325. +
  29326. +/* ---------------------------------------------------------------------- */
  29327. +
  29328. +/* sum mode which returns the summation for statfs(2) */
  29329. +
  29330. +static u64 au_add_till_max(u64 a, u64 b)
  29331. +{
  29332. + u64 old;
  29333. +
  29334. + old = a;
  29335. + a += b;
  29336. + if (old <= a)
  29337. + return a;
  29338. + return ULLONG_MAX;
  29339. +}
  29340. +
  29341. +static u64 au_mul_till_max(u64 a, long mul)
  29342. +{
  29343. + u64 old;
  29344. +
  29345. + old = a;
  29346. + a *= mul;
  29347. + if (old <= a)
  29348. + return a;
  29349. + return ULLONG_MAX;
  29350. +}
  29351. +
  29352. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  29353. +{
  29354. + int err;
  29355. + long bsize, factor;
  29356. + u64 blocks, bfree, bavail, files, ffree;
  29357. + aufs_bindex_t bbot, bindex, i;
  29358. + unsigned char shared;
  29359. + struct path h_path;
  29360. + struct super_block *h_sb;
  29361. +
  29362. + err = 0;
  29363. + bsize = LONG_MAX;
  29364. + files = 0;
  29365. + ffree = 0;
  29366. + blocks = 0;
  29367. + bfree = 0;
  29368. + bavail = 0;
  29369. + bbot = au_sbbot(sb);
  29370. + for (bindex = 0; bindex <= bbot; bindex++) {
  29371. + h_path.mnt = au_sbr_mnt(sb, bindex);
  29372. + h_sb = h_path.mnt->mnt_sb;
  29373. + shared = 0;
  29374. + for (i = 0; !shared && i < bindex; i++)
  29375. + shared = (au_sbr_sb(sb, i) == h_sb);
  29376. + if (shared)
  29377. + continue;
  29378. +
  29379. + /* sb->s_root for NFS is unreliable */
  29380. + h_path.dentry = h_path.mnt->mnt_root;
  29381. + err = vfs_statfs(&h_path, buf);
  29382. + if (unlikely(err))
  29383. + goto out;
  29384. +
  29385. + if (bsize > buf->f_bsize) {
  29386. + /*
  29387. + * we will reduce bsize, so we have to expand blocks
  29388. + * etc. to match them again
  29389. + */
  29390. + factor = (bsize / buf->f_bsize);
  29391. + blocks = au_mul_till_max(blocks, factor);
  29392. + bfree = au_mul_till_max(bfree, factor);
  29393. + bavail = au_mul_till_max(bavail, factor);
  29394. + bsize = buf->f_bsize;
  29395. + }
  29396. +
  29397. + factor = (buf->f_bsize / bsize);
  29398. + blocks = au_add_till_max(blocks,
  29399. + au_mul_till_max(buf->f_blocks, factor));
  29400. + bfree = au_add_till_max(bfree,
  29401. + au_mul_till_max(buf->f_bfree, factor));
  29402. + bavail = au_add_till_max(bavail,
  29403. + au_mul_till_max(buf->f_bavail, factor));
  29404. + files = au_add_till_max(files, buf->f_files);
  29405. + ffree = au_add_till_max(ffree, buf->f_ffree);
  29406. + }
  29407. +
  29408. + buf->f_bsize = bsize;
  29409. + buf->f_blocks = blocks;
  29410. + buf->f_bfree = bfree;
  29411. + buf->f_bavail = bavail;
  29412. + buf->f_files = files;
  29413. + buf->f_ffree = ffree;
  29414. + buf->f_frsize = 0;
  29415. +
  29416. +out:
  29417. + return err;
  29418. +}
  29419. +
  29420. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  29421. +{
  29422. + int err;
  29423. + struct path h_path;
  29424. + struct super_block *sb;
  29425. +
  29426. + /* lock free root dinfo */
  29427. + sb = dentry->d_sb;
  29428. + si_noflush_read_lock(sb);
  29429. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  29430. + /* sb->s_root for NFS is unreliable */
  29431. + h_path.mnt = au_sbr_mnt(sb, 0);
  29432. + h_path.dentry = h_path.mnt->mnt_root;
  29433. + err = vfs_statfs(&h_path, buf);
  29434. + } else
  29435. + err = au_statfs_sum(sb, buf);
  29436. + si_read_unlock(sb);
  29437. +
  29438. + if (!err) {
  29439. + buf->f_type = AUFS_SUPER_MAGIC;
  29440. + buf->f_namelen = AUFS_MAX_NAMELEN;
  29441. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  29442. + }
  29443. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  29444. +
  29445. + return err;
  29446. +}
  29447. +
  29448. +/* ---------------------------------------------------------------------- */
  29449. +
  29450. +static int aufs_sync_fs(struct super_block *sb, int wait)
  29451. +{
  29452. + int err, e;
  29453. + aufs_bindex_t bbot, bindex;
  29454. + struct au_branch *br;
  29455. + struct super_block *h_sb;
  29456. +
  29457. + err = 0;
  29458. + si_noflush_read_lock(sb);
  29459. + bbot = au_sbbot(sb);
  29460. + for (bindex = 0; bindex <= bbot; bindex++) {
  29461. + br = au_sbr(sb, bindex);
  29462. + if (!au_br_writable(br->br_perm))
  29463. + continue;
  29464. +
  29465. + h_sb = au_sbr_sb(sb, bindex);
  29466. + e = vfsub_sync_filesystem(h_sb);
  29467. + if (unlikely(e && !err))
  29468. + err = e;
  29469. + /* go on even if an error happens */
  29470. + }
  29471. + si_read_unlock(sb);
  29472. +
  29473. + return err;
  29474. +}
  29475. +
  29476. +/* ---------------------------------------------------------------------- */
  29477. +
  29478. +/* final actions when unmounting a file system */
  29479. +static void aufs_put_super(struct super_block *sb)
  29480. +{
  29481. + struct au_sbinfo *sbinfo;
  29482. +
  29483. + sbinfo = au_sbi(sb);
  29484. + if (sbinfo)
  29485. + kobject_put(&sbinfo->si_kobj);
  29486. +}
  29487. +
  29488. +/* ---------------------------------------------------------------------- */
  29489. +
  29490. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  29491. + struct super_block *sb, void *arg)
  29492. +{
  29493. + void *array;
  29494. + unsigned long long n, sz;
  29495. +
  29496. + array = NULL;
  29497. + n = 0;
  29498. + if (!*hint)
  29499. + goto out;
  29500. +
  29501. + if (*hint > ULLONG_MAX / sizeof(array)) {
  29502. + array = ERR_PTR(-EMFILE);
  29503. + pr_err("hint %llu\n", *hint);
  29504. + goto out;
  29505. + }
  29506. +
  29507. + sz = sizeof(array) * *hint;
  29508. + array = kzalloc(sz, GFP_NOFS);
  29509. + if (unlikely(!array))
  29510. + array = vzalloc(sz);
  29511. + if (unlikely(!array)) {
  29512. + array = ERR_PTR(-ENOMEM);
  29513. + goto out;
  29514. + }
  29515. +
  29516. + n = cb(sb, array, *hint, arg);
  29517. + AuDebugOn(n > *hint);
  29518. +
  29519. +out:
  29520. + *hint = n;
  29521. + return array;
  29522. +}
  29523. +
  29524. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  29525. + unsigned long long max __maybe_unused,
  29526. + void *arg)
  29527. +{
  29528. + unsigned long long n;
  29529. + struct inode **p, *inode;
  29530. + struct list_head *head;
  29531. +
  29532. + n = 0;
  29533. + p = a;
  29534. + head = arg;
  29535. + spin_lock(&sb->s_inode_list_lock);
  29536. + list_for_each_entry(inode, head, i_sb_list) {
  29537. + if (!au_is_bad_inode(inode)
  29538. + && au_ii(inode)->ii_btop >= 0) {
  29539. + spin_lock(&inode->i_lock);
  29540. + if (atomic_read(&inode->i_count)) {
  29541. + au_igrab(inode);
  29542. + *p++ = inode;
  29543. + n++;
  29544. + AuDebugOn(n > max);
  29545. + }
  29546. + spin_unlock(&inode->i_lock);
  29547. + }
  29548. + }
  29549. + spin_unlock(&sb->s_inode_list_lock);
  29550. +
  29551. + return n;
  29552. +}
  29553. +
  29554. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  29555. +{
  29556. + struct au_sbinfo *sbi;
  29557. +
  29558. + sbi = au_sbi(sb);
  29559. + *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
  29560. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  29561. +}
  29562. +
  29563. +void au_iarray_free(struct inode **a, unsigned long long max)
  29564. +{
  29565. + unsigned long long ull;
  29566. +
  29567. + for (ull = 0; ull < max; ull++)
  29568. + iput(a[ull]);
  29569. + kvfree(a);
  29570. +}
  29571. +
  29572. +/* ---------------------------------------------------------------------- */
  29573. +
  29574. +/*
  29575. + * refresh dentry and inode at remount time.
  29576. + */
  29577. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  29578. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  29579. + struct dentry *parent)
  29580. +{
  29581. + int err;
  29582. +
  29583. + di_write_lock_child(dentry);
  29584. + di_read_lock_parent(parent, AuLock_IR);
  29585. + err = au_refresh_dentry(dentry, parent);
  29586. + if (!err && dir_flags)
  29587. + au_hn_reset(d_inode(dentry), dir_flags);
  29588. + di_read_unlock(parent, AuLock_IR);
  29589. + di_write_unlock(dentry);
  29590. +
  29591. + return err;
  29592. +}
  29593. +
  29594. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  29595. + struct au_sbinfo *sbinfo,
  29596. + const unsigned int dir_flags, unsigned int do_idop)
  29597. +{
  29598. + int err;
  29599. + struct dentry *parent;
  29600. +
  29601. + err = 0;
  29602. + parent = dget_parent(dentry);
  29603. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  29604. + if (d_really_is_positive(dentry)) {
  29605. + if (!d_is_dir(dentry))
  29606. + err = au_do_refresh(dentry, /*dir_flags*/0,
  29607. + parent);
  29608. + else {
  29609. + err = au_do_refresh(dentry, dir_flags, parent);
  29610. + if (unlikely(err))
  29611. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  29612. + }
  29613. + } else
  29614. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  29615. + AuDbgDentry(dentry);
  29616. + }
  29617. + dput(parent);
  29618. +
  29619. + if (!err) {
  29620. + if (do_idop)
  29621. + au_refresh_dop(dentry, /*force_reval*/0);
  29622. + } else
  29623. + au_refresh_dop(dentry, /*force_reval*/1);
  29624. +
  29625. + AuTraceErr(err);
  29626. + return err;
  29627. +}
  29628. +
  29629. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  29630. +{
  29631. + int err, i, j, ndentry, e;
  29632. + unsigned int sigen;
  29633. + struct au_dcsub_pages dpages;
  29634. + struct au_dpage *dpage;
  29635. + struct dentry **dentries, *d;
  29636. + struct au_sbinfo *sbinfo;
  29637. + struct dentry *root = sb->s_root;
  29638. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  29639. +
  29640. + if (do_idop)
  29641. + au_refresh_dop(root, /*force_reval*/0);
  29642. +
  29643. + err = au_dpages_init(&dpages, GFP_NOFS);
  29644. + if (unlikely(err))
  29645. + goto out;
  29646. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  29647. + if (unlikely(err))
  29648. + goto out_dpages;
  29649. +
  29650. + sigen = au_sigen(sb);
  29651. + sbinfo = au_sbi(sb);
  29652. + for (i = 0; i < dpages.ndpage; i++) {
  29653. + dpage = dpages.dpages + i;
  29654. + dentries = dpage->dentries;
  29655. + ndentry = dpage->ndentry;
  29656. + for (j = 0; j < ndentry; j++) {
  29657. + d = dentries[j];
  29658. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  29659. + do_idop);
  29660. + if (unlikely(e && !err))
  29661. + err = e;
  29662. + /* go on even err */
  29663. + }
  29664. + }
  29665. +
  29666. +out_dpages:
  29667. + au_dpages_free(&dpages);
  29668. +out:
  29669. + return err;
  29670. +}
  29671. +
  29672. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  29673. +{
  29674. + int err, e;
  29675. + unsigned int sigen;
  29676. + unsigned long long max, ull;
  29677. + struct inode *inode, **array;
  29678. +
  29679. + array = au_iarray_alloc(sb, &max);
  29680. + err = PTR_ERR(array);
  29681. + if (IS_ERR(array))
  29682. + goto out;
  29683. +
  29684. + err = 0;
  29685. + sigen = au_sigen(sb);
  29686. + for (ull = 0; ull < max; ull++) {
  29687. + inode = array[ull];
  29688. + if (unlikely(!inode))
  29689. + break;
  29690. +
  29691. + e = 0;
  29692. + ii_write_lock_child(inode);
  29693. + if (au_iigen(inode, NULL) != sigen) {
  29694. + e = au_refresh_hinode_self(inode);
  29695. + if (unlikely(e)) {
  29696. + au_refresh_iop(inode, /*force_getattr*/1);
  29697. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  29698. + if (!err)
  29699. + err = e;
  29700. + /* go on even if err */
  29701. + }
  29702. + }
  29703. + if (!e && do_idop)
  29704. + au_refresh_iop(inode, /*force_getattr*/0);
  29705. + ii_write_unlock(inode);
  29706. + }
  29707. +
  29708. + au_iarray_free(array, max);
  29709. +
  29710. +out:
  29711. + return err;
  29712. +}
  29713. +
  29714. +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  29715. +{
  29716. + int err, e;
  29717. + unsigned int udba;
  29718. + aufs_bindex_t bindex, bbot;
  29719. + struct dentry *root;
  29720. + struct inode *inode;
  29721. + struct au_branch *br;
  29722. + struct au_sbinfo *sbi;
  29723. +
  29724. + au_sigen_inc(sb);
  29725. + sbi = au_sbi(sb);
  29726. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  29727. +
  29728. + root = sb->s_root;
  29729. + DiMustNoWaiters(root);
  29730. + inode = d_inode(root);
  29731. + IiMustNoWaiters(inode);
  29732. +
  29733. + udba = au_opt_udba(sb);
  29734. + bbot = au_sbbot(sb);
  29735. + for (bindex = 0; bindex <= bbot; bindex++) {
  29736. + br = au_sbr(sb, bindex);
  29737. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  29738. + if (unlikely(err))
  29739. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  29740. + bindex, err);
  29741. + /* go on even if err */
  29742. + }
  29743. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  29744. +
  29745. + if (do_idop) {
  29746. + if (au_ftest_si(sbi, NO_DREVAL)) {
  29747. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  29748. + sb->s_d_op = &aufs_dop_noreval;
  29749. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  29750. + sbi->si_iop_array = aufs_iop_nogetattr;
  29751. + } else {
  29752. + AuDebugOn(sb->s_d_op == &aufs_dop);
  29753. + sb->s_d_op = &aufs_dop;
  29754. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  29755. + sbi->si_iop_array = aufs_iop;
  29756. + }
  29757. + pr_info("reset to %ps and %ps\n",
  29758. + sb->s_d_op, sbi->si_iop_array);
  29759. + }
  29760. +
  29761. + di_write_unlock(root);
  29762. + err = au_refresh_d(sb, do_idop);
  29763. + e = au_refresh_i(sb, do_idop);
  29764. + if (unlikely(e && !err))
  29765. + err = e;
  29766. + /* aufs_write_lock() calls ..._child() */
  29767. + di_write_lock_child(root);
  29768. +
  29769. + au_cpup_attr_all(inode, /*force*/1);
  29770. +
  29771. + if (unlikely(err))
  29772. + AuIOErr("refresh failed, ignored, %d\n", err);
  29773. +}
  29774. +
  29775. +const struct super_operations aufs_sop = {
  29776. + .alloc_inode = aufs_alloc_inode,
  29777. + .destroy_inode = aufs_destroy_inode,
  29778. + .free_inode = aufs_free_inode,
  29779. + /* always deleting, no clearing */
  29780. + .drop_inode = generic_delete_inode,
  29781. + .show_options = aufs_show_options,
  29782. + .statfs = aufs_statfs,
  29783. + .put_super = aufs_put_super,
  29784. + .sync_fs = aufs_sync_fs
  29785. +};
  29786. +
  29787. +/* ---------------------------------------------------------------------- */
  29788. +
  29789. +int au_alloc_root(struct super_block *sb)
  29790. +{
  29791. + int err;
  29792. + struct inode *inode;
  29793. + struct dentry *root;
  29794. +
  29795. + err = -ENOMEM;
  29796. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  29797. + err = PTR_ERR(inode);
  29798. + if (IS_ERR(inode))
  29799. + goto out;
  29800. +
  29801. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  29802. + inode->i_fop = &aufs_dir_fop;
  29803. + inode->i_mode = S_IFDIR;
  29804. + vfsub_inode_nlink_init(inode, 2);
  29805. + unlock_new_inode(inode);
  29806. +
  29807. + root = d_make_root(inode);
  29808. + if (unlikely(!root))
  29809. + goto out;
  29810. + err = PTR_ERR(root);
  29811. + if (IS_ERR(root))
  29812. + goto out;
  29813. +
  29814. + err = au_di_init(root);
  29815. + if (!err) {
  29816. + sb->s_root = root;
  29817. + return 0; /* success */
  29818. + }
  29819. + dput(root);
  29820. +
  29821. +out:
  29822. + return err;
  29823. +}
  29824. +
  29825. +/* ---------------------------------------------------------------------- */
  29826. +
  29827. +static void aufs_kill_sb(struct super_block *sb)
  29828. +{
  29829. + struct au_sbinfo *sbinfo;
  29830. + struct dentry *root;
  29831. +
  29832. + sbinfo = au_sbi(sb);
  29833. + if (!sbinfo)
  29834. + goto out;
  29835. +
  29836. + au_sbilist_del(sb);
  29837. +
  29838. + root = sb->s_root;
  29839. + if (root)
  29840. + aufs_write_lock(root);
  29841. + else
  29842. + __si_write_lock(sb);
  29843. +
  29844. + au_fhsm_fin(sb);
  29845. + if (sbinfo->si_wbr_create_ops->fin)
  29846. + sbinfo->si_wbr_create_ops->fin(sb);
  29847. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  29848. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  29849. + au_remount_refresh(sb, /*do_idop*/0);
  29850. + }
  29851. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  29852. + au_plink_put(sb, /*verbose*/1);
  29853. + au_xino_clr(sb);
  29854. + if (root)
  29855. + au_dr_opt_flush(sb);
  29856. +
  29857. + if (root)
  29858. + aufs_write_unlock(root);
  29859. + else
  29860. + __si_write_unlock(sb);
  29861. +
  29862. + sbinfo->si_sb = NULL;
  29863. + au_nwt_flush(&sbinfo->si_nowait);
  29864. +
  29865. +out:
  29866. + kill_anon_super(sb);
  29867. +}
  29868. +
  29869. +struct file_system_type aufs_fs_type = {
  29870. + .name = AUFS_FSTYPE,
  29871. + /* a race between rename and others */
  29872. + .fs_flags = FS_RENAME_DOES_D_MOVE
  29873. + /* untested */
  29874. + /*| FS_ALLOW_IDMAP*/
  29875. + ,
  29876. + .init_fs_context = aufs_fsctx_init,
  29877. + .parameters = aufs_fsctx_paramspec,
  29878. + .kill_sb = aufs_kill_sb,
  29879. + /* no need to __module_get() and module_put(). */
  29880. + .owner = THIS_MODULE,
  29881. +};
  29882. diff -Naur null/fs/aufs/super.h linux-6.6.63/fs/aufs/super.h
  29883. --- /dev/null
  29884. +++ linux-6.6.63/fs/aufs/super.h 2024-12-18 15:12:59.299519538 +0300
  29885. @@ -0,0 +1,592 @@
  29886. +/* SPDX-License-Identifier: GPL-2.0 */
  29887. +/*
  29888. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  29889. + *
  29890. + * This program is free software; you can redistribute it and/or modify
  29891. + * it under the terms of the GNU General Public License as published by
  29892. + * the Free Software Foundation; either version 2 of the License, or
  29893. + * (at your option) any later version.
  29894. + *
  29895. + * This program is distributed in the hope that it will be useful,
  29896. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29897. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29898. + * GNU General Public License for more details.
  29899. + *
  29900. + * You should have received a copy of the GNU General Public License
  29901. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29902. + */
  29903. +
  29904. +/*
  29905. + * super_block operations
  29906. + */
  29907. +
  29908. +#ifndef __AUFS_SUPER_H__
  29909. +#define __AUFS_SUPER_H__
  29910. +
  29911. +#ifdef __KERNEL__
  29912. +
  29913. +#include <linux/fs.h>
  29914. +#include <linux/kobject.h>
  29915. +#include "hbl.h"
  29916. +#include "lcnt.h"
  29917. +#include "rwsem.h"
  29918. +#include "wkq.h"
  29919. +
  29920. +/* policies to select one among multiple writable branches */
  29921. +struct au_wbr_copyup_operations {
  29922. + int (*copyup)(struct dentry *dentry);
  29923. +};
  29924. +
  29925. +#define AuWbr_DIR 1 /* target is a dir */
  29926. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  29927. +
  29928. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  29929. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  29930. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  29931. +
  29932. +struct au_wbr_create_operations {
  29933. + int (*create)(struct dentry *dentry, unsigned int flags);
  29934. + int (*init)(struct super_block *sb);
  29935. + int (*fin)(struct super_block *sb);
  29936. +};
  29937. +
  29938. +struct au_wbr_mfs {
  29939. + struct mutex mfs_lock; /* protect this structure */
  29940. + unsigned long mfs_jiffy;
  29941. + unsigned long mfs_expire;
  29942. + aufs_bindex_t mfs_bindex;
  29943. +
  29944. + unsigned long long mfsrr_bytes;
  29945. + unsigned long long mfsrr_watermark;
  29946. +};
  29947. +
  29948. +#define AuPlink_NHASH 100
  29949. +static inline int au_plink_hash(ino_t ino)
  29950. +{
  29951. + return ino % AuPlink_NHASH;
  29952. +}
  29953. +
  29954. +/* File-based Hierarchical Storage Management */
  29955. +struct au_fhsm {
  29956. +#ifdef CONFIG_AUFS_FHSM
  29957. + /* allow only one process who can receive the notification */
  29958. + spinlock_t fhsm_spin;
  29959. + pid_t fhsm_pid;
  29960. + wait_queue_head_t fhsm_wqh;
  29961. + atomic_t fhsm_readable;
  29962. +
  29963. + /* these are protected by si_rwsem */
  29964. + unsigned long fhsm_expire;
  29965. + aufs_bindex_t fhsm_bottom;
  29966. +#endif
  29967. +};
  29968. +
  29969. +struct au_branch;
  29970. +struct au_sbinfo {
  29971. + /* nowait tasks in the system-wide workqueue */
  29972. + struct au_nowait_tasks si_nowait;
  29973. +
  29974. + /*
  29975. + * tried sb->s_umount, but failed due to the dependency between i_mutex.
  29976. + * rwsem for au_sbinfo is necessary.
  29977. + */
  29978. + struct au_rwsem si_rwsem;
  29979. +
  29980. + /*
  29981. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  29982. + * remount.
  29983. + */
  29984. + au_lcnt_t si_ninodes, si_nfiles;
  29985. +
  29986. + /* branch management */
  29987. + unsigned int si_generation;
  29988. +
  29989. + /* see AuSi_ flags */
  29990. + unsigned char au_si_status;
  29991. +
  29992. + aufs_bindex_t si_bbot;
  29993. +
  29994. + /* dirty trick to keep br_id plus */
  29995. + unsigned int si_last_br_id :
  29996. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  29997. + struct au_branch **si_branch;
  29998. +
  29999. + /* policy to select a writable branch */
  30000. + unsigned char si_wbr_copyup;
  30001. + unsigned char si_wbr_create;
  30002. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  30003. + struct au_wbr_create_operations *si_wbr_create_ops;
  30004. +
  30005. + /* round robin */
  30006. + atomic_t si_wbr_rr_next;
  30007. +
  30008. + /* most free space */
  30009. + struct au_wbr_mfs si_wbr_mfs;
  30010. +
  30011. + /* File-based Hierarchical Storage Management */
  30012. + struct au_fhsm si_fhsm;
  30013. +
  30014. + /* mount flags */
  30015. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  30016. + unsigned int si_mntflags;
  30017. +
  30018. + /* external inode number (bitmap and translation table) */
  30019. + loff_t si_ximaxent; /* max entries in a xino */
  30020. +
  30021. + struct file *si_xib;
  30022. + struct mutex si_xib_mtx; /* protect xib members */
  30023. + unsigned long *si_xib_buf;
  30024. + unsigned long si_xib_last_pindex;
  30025. + int si_xib_next_bit;
  30026. +
  30027. + unsigned long si_xino_jiffy;
  30028. + unsigned long si_xino_expire;
  30029. + /* reserved for future use */
  30030. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  30031. +
  30032. +#ifdef CONFIG_AUFS_EXPORT
  30033. + /* i_generation */
  30034. + /* todo: make xigen file an array to support many inode numbers */
  30035. + struct file *si_xigen;
  30036. + atomic_t si_xigen_next;
  30037. +#endif
  30038. +
  30039. + /* dirty trick to support atomic_open */
  30040. + struct hlist_bl_head si_aopen;
  30041. +
  30042. + /* vdir parameters */
  30043. + unsigned long si_rdcache; /* max cache time in jiffies */
  30044. + unsigned int si_rdblk; /* deblk size */
  30045. + unsigned int si_rdhash; /* hash size */
  30046. +
  30047. + /*
  30048. + * If the number of whiteouts are larger than si_dirwh, leave all of
  30049. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  30050. + * future fsck.aufs or kernel thread will remove them later.
  30051. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  30052. + */
  30053. + unsigned int si_dirwh;
  30054. +
  30055. + /* pseudo_link list */
  30056. + struct hlist_bl_head si_plink[AuPlink_NHASH];
  30057. + wait_queue_head_t si_plink_wq;
  30058. + spinlock_t si_plink_maint_lock;
  30059. + pid_t si_plink_maint_pid;
  30060. +
  30061. + /* file list */
  30062. + struct hlist_bl_head si_files;
  30063. +
  30064. + /* with/without getattr, brother of sb->s_d_op */
  30065. + const struct inode_operations *si_iop_array;
  30066. +
  30067. + /*
  30068. + * sysfs and lifetime management.
  30069. + * this is not a small structure and it may be a waste of memory in case
  30070. + * of sysfs is disabled, particularly when many aufs-es are mounted.
  30071. + * but using sysfs is majority.
  30072. + */
  30073. + struct kobject si_kobj;
  30074. +#ifdef CONFIG_DEBUG_FS
  30075. + struct dentry *si_dbgaufs;
  30076. + struct dentry *si_dbgaufs_plink;
  30077. + struct dentry *si_dbgaufs_xib;
  30078. +#ifdef CONFIG_AUFS_EXPORT
  30079. + struct dentry *si_dbgaufs_xigen;
  30080. +#endif
  30081. +#endif
  30082. +
  30083. +#ifdef CONFIG_AUFS_SBILIST
  30084. + struct hlist_bl_node si_list;
  30085. +#endif
  30086. +
  30087. + /* dirty, necessary for unmounting, sysfs and sysrq */
  30088. + struct super_block *si_sb;
  30089. +};
  30090. +
  30091. +/* sbinfo status flags */
  30092. +/*
  30093. + * set true when refresh_dirs() failed at remount time.
  30094. + * then try refreshing dirs at access time again.
  30095. + * if it is false, refreshing dirs at access time is unnecessary
  30096. + */
  30097. +#define AuSi_FAILED_REFRESH_DIR 1
  30098. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  30099. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  30100. +
  30101. +#ifndef CONFIG_AUFS_FHSM
  30102. +#undef AuSi_FHSM
  30103. +#define AuSi_FHSM 0
  30104. +#endif
  30105. +
  30106. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  30107. + unsigned int flag)
  30108. +{
  30109. + AuRwMustAnyLock(&sbi->si_rwsem);
  30110. + return sbi->au_si_status & flag;
  30111. +}
  30112. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  30113. +#define au_fset_si(sbinfo, name) do { \
  30114. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  30115. + (sbinfo)->au_si_status |= AuSi_##name; \
  30116. +} while (0)
  30117. +#define au_fclr_si(sbinfo, name) do { \
  30118. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  30119. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  30120. +} while (0)
  30121. +
  30122. +/* ---------------------------------------------------------------------- */
  30123. +
  30124. +/* policy to select one among writable branches */
  30125. +#define AuWbrCopyup(sbinfo, ...) \
  30126. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  30127. +#define AuWbrCreate(sbinfo, ...) \
  30128. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  30129. +
  30130. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  30131. +#define AuLock_DW 1 /* write-lock dentry */
  30132. +#define AuLock_IR (1 << 1) /* read-lock inode */
  30133. +#define AuLock_IW (1 << 2) /* write-lock inode */
  30134. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  30135. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  30136. + /* except RENAME_EXCHANGE */
  30137. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  30138. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  30139. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  30140. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  30141. +#define au_fset_lock(flags, name) \
  30142. + do { (flags) |= AuLock_##name; } while (0)
  30143. +#define au_fclr_lock(flags, name) \
  30144. + do { (flags) &= ~AuLock_##name; } while (0)
  30145. +
  30146. +/* ---------------------------------------------------------------------- */
  30147. +
  30148. +/* super.c */
  30149. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  30150. +
  30151. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  30152. + unsigned long long max, void *arg);
  30153. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  30154. + struct super_block *sb, void *arg);
  30155. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  30156. +void au_iarray_free(struct inode **a, unsigned long long max);
  30157. +
  30158. +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
  30159. +extern const struct super_operations aufs_sop;
  30160. +int au_alloc_root(struct super_block *sb);
  30161. +extern struct file_system_type aufs_fs_type;
  30162. +
  30163. +/* sbinfo.c */
  30164. +void au_si_free(struct kobject *kobj);
  30165. +struct au_sbinfo *au_si_alloc(struct super_block *sb);
  30166. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
  30167. +
  30168. +unsigned int au_sigen_inc(struct super_block *sb);
  30169. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  30170. +
  30171. +int si_read_lock(struct super_block *sb, int flags);
  30172. +int si_write_lock(struct super_block *sb, int flags);
  30173. +int aufs_read_lock(struct dentry *dentry, int flags);
  30174. +void aufs_read_unlock(struct dentry *dentry, int flags);
  30175. +void aufs_write_lock(struct dentry *dentry);
  30176. +void aufs_write_unlock(struct dentry *dentry);
  30177. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  30178. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  30179. +
  30180. +/* wbr_policy.c */
  30181. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  30182. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  30183. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  30184. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  30185. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  30186. +
  30187. +/* mvdown.c */
  30188. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  30189. +
  30190. +#ifdef CONFIG_AUFS_FHSM
  30191. +/* fhsm.c */
  30192. +
  30193. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  30194. +{
  30195. + pid_t pid;
  30196. +
  30197. + spin_lock(&fhsm->fhsm_spin);
  30198. + pid = fhsm->fhsm_pid;
  30199. + spin_unlock(&fhsm->fhsm_spin);
  30200. +
  30201. + return pid;
  30202. +}
  30203. +
  30204. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  30205. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  30206. +int au_fhsm_fd(struct super_block *sb, int oflags);
  30207. +int au_fhsm_br_alloc(struct au_branch *br);
  30208. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  30209. +void au_fhsm_fin(struct super_block *sb);
  30210. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  30211. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  30212. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  30213. +#else
  30214. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  30215. + int force)
  30216. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  30217. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  30218. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  30219. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  30220. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  30221. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  30222. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  30223. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  30224. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  30225. +#endif
  30226. +
  30227. +/* ---------------------------------------------------------------------- */
  30228. +
  30229. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  30230. +{
  30231. + return sb->s_fs_info;
  30232. +}
  30233. +
  30234. +/* ---------------------------------------------------------------------- */
  30235. +
  30236. +#ifdef CONFIG_AUFS_EXPORT
  30237. +int au_test_nfsd(void);
  30238. +void au_export_init(struct super_block *sb);
  30239. +void au_xigen_inc(struct inode *inode);
  30240. +int au_xigen_new(struct inode *inode);
  30241. +int au_xigen_set(struct super_block *sb, struct path *path);
  30242. +void au_xigen_clr(struct super_block *sb);
  30243. +
  30244. +static inline int au_busy_or_stale(void)
  30245. +{
  30246. + if (!au_test_nfsd())
  30247. + return -EBUSY;
  30248. + return -ESTALE;
  30249. +}
  30250. +#else
  30251. +AuStubInt0(au_test_nfsd, void)
  30252. +AuStubVoid(au_export_init, struct super_block *sb)
  30253. +AuStubVoid(au_xigen_inc, struct inode *inode)
  30254. +AuStubInt0(au_xigen_new, struct inode *inode)
  30255. +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
  30256. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  30257. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  30258. +#endif /* CONFIG_AUFS_EXPORT */
  30259. +
  30260. +/* ---------------------------------------------------------------------- */
  30261. +
  30262. +#ifdef CONFIG_AUFS_SBILIST
  30263. +/* module.c */
  30264. +extern struct hlist_bl_head au_sbilist;
  30265. +
  30266. +static inline void au_sbilist_init(void)
  30267. +{
  30268. + INIT_HLIST_BL_HEAD(&au_sbilist);
  30269. +}
  30270. +
  30271. +static inline void au_sbilist_add(struct super_block *sb)
  30272. +{
  30273. + au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
  30274. +}
  30275. +
  30276. +static inline void au_sbilist_del(struct super_block *sb)
  30277. +{
  30278. + au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
  30279. +}
  30280. +
  30281. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  30282. +static inline void au_sbilist_lock(void)
  30283. +{
  30284. + hlist_bl_lock(&au_sbilist);
  30285. +}
  30286. +
  30287. +static inline void au_sbilist_unlock(void)
  30288. +{
  30289. + hlist_bl_unlock(&au_sbilist);
  30290. +}
  30291. +#define AuGFP_SBILIST GFP_ATOMIC
  30292. +#else
  30293. +AuStubVoid(au_sbilist_lock, void)
  30294. +AuStubVoid(au_sbilist_unlock, void)
  30295. +#define AuGFP_SBILIST GFP_NOFS
  30296. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  30297. +#else
  30298. +AuStubVoid(au_sbilist_init, void)
  30299. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  30300. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  30301. +AuStubVoid(au_sbilist_lock, void)
  30302. +AuStubVoid(au_sbilist_unlock, void)
  30303. +#define AuGFP_SBILIST GFP_NOFS
  30304. +#endif
  30305. +
  30306. +/* ---------------------------------------------------------------------- */
  30307. +
  30308. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  30309. +{
  30310. + /*
  30311. + * This function is a dynamic '__init' function actually,
  30312. + * so the tiny check for si_rwsem is unnecessary.
  30313. + */
  30314. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  30315. +#ifdef CONFIG_DEBUG_FS
  30316. + sbinfo->si_dbgaufs = NULL;
  30317. + sbinfo->si_dbgaufs_plink = NULL;
  30318. + sbinfo->si_dbgaufs_xib = NULL;
  30319. +#ifdef CONFIG_AUFS_EXPORT
  30320. + sbinfo->si_dbgaufs_xigen = NULL;
  30321. +#endif
  30322. +#endif
  30323. +}
  30324. +
  30325. +/* ---------------------------------------------------------------------- */
  30326. +
  30327. +/* current->atomic_flags */
  30328. +/* this value should never corrupt the ones defined in linux/sched.h */
  30329. +#define PFA_AUFS 0x10
  30330. +
  30331. +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
  30332. +TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
  30333. +TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
  30334. +
  30335. +static inline int si_pid_test(struct super_block *sb)
  30336. +{
  30337. + return !!task_test_aufs(current);
  30338. +}
  30339. +
  30340. +static inline void si_pid_clr(struct super_block *sb)
  30341. +{
  30342. + AuDebugOn(!task_test_aufs(current));
  30343. + task_clear_aufs(current);
  30344. +}
  30345. +
  30346. +static inline void si_pid_set(struct super_block *sb)
  30347. +{
  30348. + AuDebugOn(task_test_aufs(current));
  30349. + task_set_aufs(current);
  30350. +}
  30351. +
  30352. +/* ---------------------------------------------------------------------- */
  30353. +
  30354. +/* lock superblock. mainly for entry point functions */
  30355. +#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
  30356. +#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
  30357. +#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
  30358. +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
  30359. +/*
  30360. +#define __si_read_trylock_nested(sb) \
  30361. + au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
  30362. +#define __si_write_trylock_nested(sb) \
  30363. + au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
  30364. +*/
  30365. +
  30366. +#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
  30367. +#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
  30368. +#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
  30369. +
  30370. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  30371. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  30372. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  30373. +
  30374. +static inline void si_noflush_read_lock(struct super_block *sb)
  30375. +{
  30376. + __si_read_lock(sb);
  30377. + si_pid_set(sb);
  30378. +}
  30379. +
  30380. +static inline int si_noflush_read_trylock(struct super_block *sb)
  30381. +{
  30382. + int locked;
  30383. +
  30384. + locked = __si_read_trylock(sb);
  30385. + if (locked)
  30386. + si_pid_set(sb);
  30387. + return locked;
  30388. +}
  30389. +
  30390. +static inline void si_noflush_write_lock(struct super_block *sb)
  30391. +{
  30392. + __si_write_lock(sb);
  30393. + si_pid_set(sb);
  30394. +}
  30395. +
  30396. +static inline int si_noflush_write_trylock(struct super_block *sb)
  30397. +{
  30398. + int locked;
  30399. +
  30400. + locked = __si_write_trylock(sb);
  30401. + if (locked)
  30402. + si_pid_set(sb);
  30403. + return locked;
  30404. +}
  30405. +
  30406. +#if 0 /* reserved */
  30407. +static inline int si_read_trylock(struct super_block *sb, int flags)
  30408. +{
  30409. + if (au_ftest_lock(flags, FLUSH))
  30410. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  30411. + return si_noflush_read_trylock(sb);
  30412. +}
  30413. +#endif
  30414. +
  30415. +static inline void si_read_unlock(struct super_block *sb)
  30416. +{
  30417. + si_pid_clr(sb);
  30418. + __si_read_unlock(sb);
  30419. +}
  30420. +
  30421. +#if 0 /* reserved */
  30422. +static inline int si_write_trylock(struct super_block *sb, int flags)
  30423. +{
  30424. + if (au_ftest_lock(flags, FLUSH))
  30425. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  30426. + return si_noflush_write_trylock(sb);
  30427. +}
  30428. +#endif
  30429. +
  30430. +static inline void si_write_unlock(struct super_block *sb)
  30431. +{
  30432. + si_pid_clr(sb);
  30433. + __si_write_unlock(sb);
  30434. +}
  30435. +
  30436. +#if 0 /* reserved */
  30437. +static inline void si_downgrade_lock(struct super_block *sb)
  30438. +{
  30439. + __si_downgrade_lock(sb);
  30440. +}
  30441. +#endif
  30442. +
  30443. +/* ---------------------------------------------------------------------- */
  30444. +
  30445. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  30446. +{
  30447. + SiMustAnyLock(sb);
  30448. + return au_sbi(sb)->si_bbot;
  30449. +}
  30450. +
  30451. +static inline unsigned int au_mntflags(struct super_block *sb)
  30452. +{
  30453. + SiMustAnyLock(sb);
  30454. + return au_sbi(sb)->si_mntflags;
  30455. +}
  30456. +
  30457. +static inline unsigned int au_sigen(struct super_block *sb)
  30458. +{
  30459. + SiMustAnyLock(sb);
  30460. + return au_sbi(sb)->si_generation;
  30461. +}
  30462. +
  30463. +static inline struct au_branch *au_sbr(struct super_block *sb,
  30464. + aufs_bindex_t bindex)
  30465. +{
  30466. + SiMustAnyLock(sb);
  30467. + return au_sbi(sb)->si_branch[0 + bindex];
  30468. +}
  30469. +
  30470. +static inline loff_t au_xi_maxent(struct super_block *sb)
  30471. +{
  30472. + SiMustAnyLock(sb);
  30473. + return au_sbi(sb)->si_ximaxent;
  30474. +}
  30475. +
  30476. +#endif /* __KERNEL__ */
  30477. +#endif /* __AUFS_SUPER_H__ */
  30478. diff -Naur null/fs/aufs/sysaufs.c linux-6.6.63/fs/aufs/sysaufs.c
  30479. --- /dev/null
  30480. +++ linux-6.6.63/fs/aufs/sysaufs.c 2024-12-18 15:12:59.299519538 +0300
  30481. @@ -0,0 +1,94 @@
  30482. +// SPDX-License-Identifier: GPL-2.0
  30483. +/*
  30484. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  30485. + *
  30486. + * This program is free software; you can redistribute it and/or modify
  30487. + * it under the terms of the GNU General Public License as published by
  30488. + * the Free Software Foundation; either version 2 of the License, or
  30489. + * (at your option) any later version.
  30490. + *
  30491. + * This program is distributed in the hope that it will be useful,
  30492. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30493. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30494. + * GNU General Public License for more details.
  30495. + *
  30496. + * You should have received a copy of the GNU General Public License
  30497. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30498. + */
  30499. +
  30500. +/*
  30501. + * sysfs interface and lifetime management
  30502. + * they are necessary regardless sysfs is disabled.
  30503. + */
  30504. +
  30505. +#include <linux/random.h>
  30506. +#include "aufs.h"
  30507. +
  30508. +unsigned long sysaufs_si_mask;
  30509. +struct kset *sysaufs_kset;
  30510. +
  30511. +#define AuSiAttr(_name) { \
  30512. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  30513. + .show = sysaufs_si_##_name, \
  30514. +}
  30515. +
  30516. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  30517. +struct attribute *sysaufs_si_attrs[] = {
  30518. + &sysaufs_si_attr_xi_path.attr,
  30519. + NULL,
  30520. +};
  30521. +ATTRIBUTE_GROUPS(sysaufs_si);
  30522. +
  30523. +static const struct sysfs_ops au_sbi_ops = {
  30524. + .show = sysaufs_si_show
  30525. +};
  30526. +
  30527. +static const struct kobj_type au_sbi_ktype = {
  30528. + .release = au_si_free,
  30529. + .sysfs_ops = &au_sbi_ops,
  30530. + .default_groups = sysaufs_si_groups
  30531. +};
  30532. +
  30533. +/* ---------------------------------------------------------------------- */
  30534. +
  30535. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  30536. +{
  30537. + int err;
  30538. +
  30539. + sbinfo->si_kobj.kset = sysaufs_kset;
  30540. + /* cf. sysaufs_name() */
  30541. + err = kobject_init_and_add
  30542. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  30543. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  30544. +
  30545. + return err;
  30546. +}
  30547. +
  30548. +void sysaufs_fin(void)
  30549. +{
  30550. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30551. + kset_unregister(sysaufs_kset);
  30552. +}
  30553. +
  30554. +int __init sysaufs_init(void)
  30555. +{
  30556. + int err;
  30557. +
  30558. + do {
  30559. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  30560. + } while (!sysaufs_si_mask);
  30561. +
  30562. + err = -EINVAL;
  30563. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  30564. + if (unlikely(!sysaufs_kset))
  30565. + goto out;
  30566. + err = PTR_ERR(sysaufs_kset);
  30567. + if (IS_ERR(sysaufs_kset))
  30568. + goto out;
  30569. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30570. + if (unlikely(err))
  30571. + kset_unregister(sysaufs_kset);
  30572. +
  30573. +out:
  30574. + return err;
  30575. +}
  30576. diff -Naur null/fs/aufs/sysaufs.h linux-6.6.63/fs/aufs/sysaufs.h
  30577. --- /dev/null
  30578. +++ linux-6.6.63/fs/aufs/sysaufs.h 2024-12-18 15:12:59.299519538 +0300
  30579. @@ -0,0 +1,102 @@
  30580. +/* SPDX-License-Identifier: GPL-2.0 */
  30581. +/*
  30582. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  30583. + *
  30584. + * This program is free software; you can redistribute it and/or modify
  30585. + * it under the terms of the GNU General Public License as published by
  30586. + * the Free Software Foundation; either version 2 of the License, or
  30587. + * (at your option) any later version.
  30588. + *
  30589. + * This program is distributed in the hope that it will be useful,
  30590. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30591. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30592. + * GNU General Public License for more details.
  30593. + *
  30594. + * You should have received a copy of the GNU General Public License
  30595. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30596. + */
  30597. +
  30598. +/*
  30599. + * sysfs interface and mount lifetime management
  30600. + */
  30601. +
  30602. +#ifndef __SYSAUFS_H__
  30603. +#define __SYSAUFS_H__
  30604. +
  30605. +#ifdef __KERNEL__
  30606. +
  30607. +#include <linux/sysfs.h>
  30608. +#include "module.h"
  30609. +
  30610. +struct super_block;
  30611. +struct au_sbinfo;
  30612. +
  30613. +struct sysaufs_si_attr {
  30614. + struct attribute attr;
  30615. + int (*show)(struct seq_file *seq, struct super_block *sb);
  30616. +};
  30617. +
  30618. +/* ---------------------------------------------------------------------- */
  30619. +
  30620. +/* sysaufs.c */
  30621. +extern unsigned long sysaufs_si_mask;
  30622. +extern struct kset *sysaufs_kset;
  30623. +extern struct attribute *sysaufs_si_attrs[];
  30624. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  30625. +int __init sysaufs_init(void);
  30626. +void sysaufs_fin(void);
  30627. +
  30628. +/* ---------------------------------------------------------------------- */
  30629. +
  30630. +/* some people doesn't like to show a pointer in kernel */
  30631. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  30632. +{
  30633. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  30634. +}
  30635. +
  30636. +#define SysaufsSiNamePrefix "si_"
  30637. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  30638. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  30639. +{
  30640. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  30641. + sysaufs_si_id(sbinfo));
  30642. +}
  30643. +
  30644. +struct au_branch;
  30645. +#ifdef CONFIG_SYSFS
  30646. +/* sysfs.c */
  30647. +extern struct attribute_group *sysaufs_attr_group;
  30648. +
  30649. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  30650. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30651. + char *buf);
  30652. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  30653. +#ifdef CONFIG_COMPAT
  30654. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  30655. +#endif
  30656. +
  30657. +void sysaufs_br_init(struct au_branch *br);
  30658. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  30659. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  30660. +
  30661. +#define sysaufs_brs_init() do {} while (0)
  30662. +
  30663. +#else
  30664. +#define sysaufs_attr_group NULL
  30665. +
  30666. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  30667. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  30668. + struct attribute *attr, char *buf)
  30669. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  30670. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  30671. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  30672. +
  30673. +static inline void sysaufs_brs_init(void)
  30674. +{
  30675. + sysaufs_brs = 0;
  30676. +}
  30677. +
  30678. +#endif /* CONFIG_SYSFS */
  30679. +
  30680. +#endif /* __KERNEL__ */
  30681. +#endif /* __SYSAUFS_H__ */
  30682. diff -Naur null/fs/aufs/sysfs.c linux-6.6.63/fs/aufs/sysfs.c
  30683. --- /dev/null
  30684. +++ linux-6.6.63/fs/aufs/sysfs.c 2024-12-18 15:12:59.299519538 +0300
  30685. @@ -0,0 +1,374 @@
  30686. +// SPDX-License-Identifier: GPL-2.0
  30687. +/*
  30688. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  30689. + *
  30690. + * This program is free software; you can redistribute it and/or modify
  30691. + * it under the terms of the GNU General Public License as published by
  30692. + * the Free Software Foundation; either version 2 of the License, or
  30693. + * (at your option) any later version.
  30694. + *
  30695. + * This program is distributed in the hope that it will be useful,
  30696. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30697. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30698. + * GNU General Public License for more details.
  30699. + *
  30700. + * You should have received a copy of the GNU General Public License
  30701. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30702. + */
  30703. +
  30704. +/*
  30705. + * sysfs interface
  30706. + */
  30707. +
  30708. +#include <linux/compat.h>
  30709. +#include <linux/seq_file.h>
  30710. +#include "aufs.h"
  30711. +
  30712. +#ifdef CONFIG_AUFS_FS_MODULE
  30713. +/* this entry violates the "one line per file" policy of sysfs */
  30714. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  30715. + char *buf)
  30716. +{
  30717. + ssize_t err;
  30718. + static char *conf =
  30719. +/* this file is generated at compiling */
  30720. +#include "conf.str"
  30721. + ;
  30722. +
  30723. + err = snprintf(buf, PAGE_SIZE, conf);
  30724. + if (unlikely(err >= PAGE_SIZE))
  30725. + err = -EFBIG;
  30726. + return err;
  30727. +}
  30728. +
  30729. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  30730. +#endif
  30731. +
  30732. +static struct attribute *au_attr[] = {
  30733. +#ifdef CONFIG_AUFS_FS_MODULE
  30734. + &au_config_attr.attr,
  30735. +#endif
  30736. + NULL, /* need to NULL terminate the list of attributes */
  30737. +};
  30738. +
  30739. +static struct attribute_group sysaufs_attr_group_body = {
  30740. + .attrs = au_attr
  30741. +};
  30742. +
  30743. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  30744. +
  30745. +/* ---------------------------------------------------------------------- */
  30746. +
  30747. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  30748. +{
  30749. + int err;
  30750. +
  30751. + SiMustAnyLock(sb);
  30752. +
  30753. + err = 0;
  30754. + if (au_opt_test(au_mntflags(sb), XINO)) {
  30755. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  30756. + seq_putc(seq, '\n');
  30757. + }
  30758. + return err;
  30759. +}
  30760. +
  30761. +/*
  30762. + * the lifetime of branch is independent from the entry under sysfs.
  30763. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  30764. + * unlinked.
  30765. + */
  30766. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  30767. + aufs_bindex_t bindex, int idx)
  30768. +{
  30769. + int err;
  30770. + struct path path;
  30771. + struct dentry *root;
  30772. + struct au_branch *br;
  30773. + au_br_perm_str_t perm;
  30774. +
  30775. + AuDbg("b%d\n", bindex);
  30776. +
  30777. + err = 0;
  30778. + root = sb->s_root;
  30779. + di_read_lock_parent(root, !AuLock_IR);
  30780. + br = au_sbr(sb, bindex);
  30781. +
  30782. + switch (idx) {
  30783. + case AuBrSysfs_BR:
  30784. + path.mnt = au_br_mnt(br);
  30785. + path.dentry = au_h_dptr(root, bindex);
  30786. + err = au_seq_path(seq, &path);
  30787. + if (!err) {
  30788. + au_optstr_br_perm(&perm, br->br_perm);
  30789. + seq_printf(seq, "=%s\n", perm.a);
  30790. + }
  30791. + break;
  30792. + case AuBrSysfs_BRID:
  30793. + seq_printf(seq, "%d\n", br->br_id);
  30794. + break;
  30795. + }
  30796. + di_read_unlock(root, !AuLock_IR);
  30797. + if (unlikely(err || seq_has_overflowed(seq)))
  30798. + err = -E2BIG;
  30799. +
  30800. + return err;
  30801. +}
  30802. +
  30803. +/* ---------------------------------------------------------------------- */
  30804. +
  30805. +static struct seq_file *au_seq(char *p, ssize_t len)
  30806. +{
  30807. + struct seq_file *seq;
  30808. +
  30809. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  30810. + if (seq) {
  30811. + /* mutex_init(&seq.lock); */
  30812. + seq->buf = p;
  30813. + seq->size = len;
  30814. + return seq; /* success */
  30815. + }
  30816. +
  30817. + seq = ERR_PTR(-ENOMEM);
  30818. + return seq;
  30819. +}
  30820. +
  30821. +#define SysaufsBr_PREFIX "br"
  30822. +#define SysaufsBrid_PREFIX "brid"
  30823. +
  30824. +/* todo: file size may exceed PAGE_SIZE */
  30825. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30826. + char *buf)
  30827. +{
  30828. + ssize_t err;
  30829. + int idx;
  30830. + long l;
  30831. + aufs_bindex_t bbot;
  30832. + struct au_sbinfo *sbinfo;
  30833. + struct super_block *sb;
  30834. + struct seq_file *seq;
  30835. + char *name;
  30836. + struct attribute **cattr;
  30837. +
  30838. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  30839. + sb = sbinfo->si_sb;
  30840. +
  30841. + /*
  30842. + * prevent a race condition between sysfs and aufs.
  30843. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  30844. + * prohibits maintaining the sysfs entries.
  30845. + * hew we acquire read lock after sysfs_get_active_two().
  30846. + * on the other hand, the remount process may maintain the sysfs/aufs
  30847. + * entries after acquiring write lock.
  30848. + * it can cause a deadlock.
  30849. + * simply we gave up processing read here.
  30850. + */
  30851. + err = -EBUSY;
  30852. + if (unlikely(!si_noflush_read_trylock(sb)))
  30853. + goto out;
  30854. +
  30855. + seq = au_seq(buf, PAGE_SIZE);
  30856. + err = PTR_ERR(seq);
  30857. + if (IS_ERR(seq))
  30858. + goto out_unlock;
  30859. +
  30860. + name = (void *)attr->name;
  30861. + cattr = sysaufs_si_attrs;
  30862. + while (*cattr) {
  30863. + if (!strcmp(name, (*cattr)->name)) {
  30864. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  30865. + ->show(seq, sb);
  30866. + goto out_seq;
  30867. + }
  30868. + cattr++;
  30869. + }
  30870. +
  30871. + if (!strncmp(name, SysaufsBrid_PREFIX,
  30872. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  30873. + idx = AuBrSysfs_BRID;
  30874. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  30875. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  30876. + sizeof(SysaufsBr_PREFIX) - 1)) {
  30877. + idx = AuBrSysfs_BR;
  30878. + name += sizeof(SysaufsBr_PREFIX) - 1;
  30879. + } else
  30880. + BUG();
  30881. +
  30882. + err = kstrtol(name, 10, &l);
  30883. + if (!err) {
  30884. + bbot = au_sbbot(sb);
  30885. + if (l <= bbot)
  30886. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  30887. + else
  30888. + err = -ENOENT;
  30889. + }
  30890. +
  30891. +out_seq:
  30892. + if (!err) {
  30893. + err = seq->count;
  30894. + /* sysfs limit */
  30895. + if (unlikely(err == PAGE_SIZE))
  30896. + err = -EFBIG;
  30897. + }
  30898. + au_kfree_rcu(seq);
  30899. +out_unlock:
  30900. + si_read_unlock(sb);
  30901. +out:
  30902. + return err;
  30903. +}
  30904. +
  30905. +/* ---------------------------------------------------------------------- */
  30906. +
  30907. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  30908. +{
  30909. + int err;
  30910. + int16_t brid;
  30911. + aufs_bindex_t bindex, bbot;
  30912. + size_t sz;
  30913. + char *buf;
  30914. + struct seq_file *seq;
  30915. + struct au_branch *br;
  30916. +
  30917. + si_read_lock(sb, AuLock_FLUSH);
  30918. + bbot = au_sbbot(sb);
  30919. + err = bbot + 1;
  30920. + if (!arg)
  30921. + goto out;
  30922. +
  30923. + err = -ENOMEM;
  30924. + buf = (void *)__get_free_page(GFP_NOFS);
  30925. + if (unlikely(!buf))
  30926. + goto out;
  30927. +
  30928. + seq = au_seq(buf, PAGE_SIZE);
  30929. + err = PTR_ERR(seq);
  30930. + if (IS_ERR(seq))
  30931. + goto out_buf;
  30932. +
  30933. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  30934. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  30935. + /* VERIFY_WRITE */
  30936. + err = !access_ok(arg, sizeof(*arg));
  30937. + if (unlikely(err))
  30938. + break;
  30939. +
  30940. + br = au_sbr(sb, bindex);
  30941. + brid = br->br_id;
  30942. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  30943. + err = __put_user(brid, &arg->id);
  30944. + if (unlikely(err))
  30945. + break;
  30946. +
  30947. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  30948. + err = __put_user(br->br_perm, &arg->perm);
  30949. + if (unlikely(err))
  30950. + break;
  30951. +
  30952. + err = au_seq_path(seq, &br->br_path);
  30953. + if (unlikely(err))
  30954. + break;
  30955. + seq_putc(seq, '\0');
  30956. + if (!seq_has_overflowed(seq)) {
  30957. + err = copy_to_user(arg->path, seq->buf, seq->count);
  30958. + seq->count = 0;
  30959. + if (unlikely(err))
  30960. + break;
  30961. + } else {
  30962. + err = -E2BIG;
  30963. + goto out_seq;
  30964. + }
  30965. + }
  30966. + if (unlikely(err))
  30967. + err = -EFAULT;
  30968. +
  30969. +out_seq:
  30970. + au_kfree_rcu(seq);
  30971. +out_buf:
  30972. + free_page((unsigned long)buf);
  30973. +out:
  30974. + si_read_unlock(sb);
  30975. + return err;
  30976. +}
  30977. +
  30978. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  30979. +{
  30980. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  30981. +}
  30982. +
  30983. +#ifdef CONFIG_COMPAT
  30984. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  30985. +{
  30986. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  30987. +}
  30988. +#endif
  30989. +
  30990. +/* ---------------------------------------------------------------------- */
  30991. +
  30992. +void sysaufs_br_init(struct au_branch *br)
  30993. +{
  30994. + int i;
  30995. + struct au_brsysfs *br_sysfs;
  30996. + struct attribute *attr;
  30997. +
  30998. + br_sysfs = br->br_sysfs;
  30999. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  31000. + attr = &br_sysfs->attr;
  31001. + sysfs_attr_init(attr);
  31002. + attr->name = br_sysfs->name;
  31003. + attr->mode = 0444;
  31004. + br_sysfs++;
  31005. + }
  31006. +}
  31007. +
  31008. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  31009. +{
  31010. + struct au_branch *br;
  31011. + struct kobject *kobj;
  31012. + struct au_brsysfs *br_sysfs;
  31013. + int i;
  31014. + aufs_bindex_t bbot;
  31015. +
  31016. + if (!sysaufs_brs)
  31017. + return;
  31018. +
  31019. + kobj = &au_sbi(sb)->si_kobj;
  31020. + bbot = au_sbbot(sb);
  31021. + for (; bindex <= bbot; bindex++) {
  31022. + br = au_sbr(sb, bindex);
  31023. + br_sysfs = br->br_sysfs;
  31024. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  31025. + sysfs_remove_file(kobj, &br_sysfs->attr);
  31026. + br_sysfs++;
  31027. + }
  31028. + }
  31029. +}
  31030. +
  31031. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  31032. +{
  31033. + int err, i;
  31034. + aufs_bindex_t bbot;
  31035. + struct kobject *kobj;
  31036. + struct au_branch *br;
  31037. + struct au_brsysfs *br_sysfs;
  31038. +
  31039. + if (!sysaufs_brs)
  31040. + return;
  31041. +
  31042. + kobj = &au_sbi(sb)->si_kobj;
  31043. + bbot = au_sbbot(sb);
  31044. + for (; bindex <= bbot; bindex++) {
  31045. + br = au_sbr(sb, bindex);
  31046. + br_sysfs = br->br_sysfs;
  31047. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  31048. + SysaufsBr_PREFIX "%d", bindex);
  31049. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  31050. + SysaufsBrid_PREFIX "%d", bindex);
  31051. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  31052. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  31053. + if (unlikely(err))
  31054. + pr_warn("failed %s under sysfs(%d)\n",
  31055. + br_sysfs->name, err);
  31056. + br_sysfs++;
  31057. + }
  31058. + }
  31059. +}
  31060. diff -Naur null/fs/aufs/sysrq.c linux-6.6.63/fs/aufs/sysrq.c
  31061. --- /dev/null
  31062. +++ linux-6.6.63/fs/aufs/sysrq.c 2024-12-18 15:12:59.299519538 +0300
  31063. @@ -0,0 +1,149 @@
  31064. +// SPDX-License-Identifier: GPL-2.0
  31065. +/*
  31066. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  31067. + *
  31068. + * This program is free software; you can redistribute it and/or modify
  31069. + * it under the terms of the GNU General Public License as published by
  31070. + * the Free Software Foundation; either version 2 of the License, or
  31071. + * (at your option) any later version.
  31072. + *
  31073. + * This program is distributed in the hope that it will be useful,
  31074. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31075. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31076. + * GNU General Public License for more details.
  31077. + *
  31078. + * You should have received a copy of the GNU General Public License
  31079. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31080. + */
  31081. +
  31082. +/*
  31083. + * magic sysrq handler
  31084. + */
  31085. +
  31086. +/* #include <linux/sysrq.h> */
  31087. +#include <linux/writeback.h>
  31088. +#include "aufs.h"
  31089. +
  31090. +/* ---------------------------------------------------------------------- */
  31091. +
  31092. +static void sysrq_sb(struct super_block *sb)
  31093. +{
  31094. + char *plevel;
  31095. + struct au_sbinfo *sbinfo;
  31096. + struct file *file;
  31097. + struct hlist_bl_head *files;
  31098. + struct hlist_bl_node *pos;
  31099. + struct au_finfo *finfo;
  31100. + struct inode *i;
  31101. +
  31102. + plevel = au_plevel;
  31103. + au_plevel = KERN_WARNING;
  31104. +
  31105. + /* since we define pr_fmt, call printk directly */
  31106. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  31107. +
  31108. + sbinfo = au_sbi(sb);
  31109. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  31110. + pr("superblock\n");
  31111. + au_dpri_sb(sb);
  31112. +
  31113. +#if 0 /* reserved */
  31114. + do {
  31115. + int err, i, j, ndentry;
  31116. + struct au_dcsub_pages dpages;
  31117. + struct au_dpage *dpage;
  31118. +
  31119. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  31120. + if (unlikely(err))
  31121. + break;
  31122. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  31123. + if (!err)
  31124. + for (i = 0; i < dpages.ndpage; i++) {
  31125. + dpage = dpages.dpages + i;
  31126. + ndentry = dpage->ndentry;
  31127. + for (j = 0; j < ndentry; j++)
  31128. + au_dpri_dentry(dpage->dentries[j]);
  31129. + }
  31130. + au_dpages_free(&dpages);
  31131. + } while (0);
  31132. +#endif
  31133. +
  31134. + pr("isolated inode\n");
  31135. + spin_lock(&sb->s_inode_list_lock);
  31136. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  31137. + spin_lock(&i->i_lock);
  31138. + if (hlist_empty(&i->i_dentry))
  31139. + au_dpri_inode(i);
  31140. + spin_unlock(&i->i_lock);
  31141. + }
  31142. + spin_unlock(&sb->s_inode_list_lock);
  31143. +
  31144. + pr("files\n");
  31145. + files = &au_sbi(sb)->si_files;
  31146. + hlist_bl_lock(files);
  31147. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  31148. + umode_t mode;
  31149. +
  31150. + file = finfo->fi_file;
  31151. + mode = file_inode(file)->i_mode;
  31152. + if (!special_file(mode))
  31153. + au_dpri_file(file);
  31154. + }
  31155. + hlist_bl_unlock(files);
  31156. + pr("done\n");
  31157. +
  31158. +#undef pr
  31159. + au_plevel = plevel;
  31160. +}
  31161. +
  31162. +/* ---------------------------------------------------------------------- */
  31163. +
  31164. +/* module parameter */
  31165. +static char *aufs_sysrq_key = "a";
  31166. +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
  31167. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  31168. +
  31169. +static void au_sysrq(u8 key __maybe_unused)
  31170. +{
  31171. + struct au_sbinfo *sbinfo;
  31172. + struct hlist_bl_node *pos;
  31173. +
  31174. + lockdep_off();
  31175. + au_sbilist_lock();
  31176. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  31177. + sysrq_sb(sbinfo->si_sb);
  31178. + au_sbilist_unlock();
  31179. + lockdep_on();
  31180. +}
  31181. +
  31182. +static struct sysrq_key_op au_sysrq_op = {
  31183. + .handler = au_sysrq,
  31184. + .help_msg = "Aufs",
  31185. + .action_msg = "Aufs",
  31186. + .enable_mask = SYSRQ_ENABLE_DUMP
  31187. +};
  31188. +
  31189. +/* ---------------------------------------------------------------------- */
  31190. +
  31191. +int __init au_sysrq_init(void)
  31192. +{
  31193. + int err;
  31194. + char key;
  31195. +
  31196. + err = -1;
  31197. + key = *aufs_sysrq_key;
  31198. + if ('a' <= key && key <= 'z')
  31199. + err = register_sysrq_key(key, &au_sysrq_op);
  31200. + if (unlikely(err))
  31201. + pr_err("err %d, sysrq=%c\n", err, key);
  31202. + return err;
  31203. +}
  31204. +
  31205. +void au_sysrq_fin(void)
  31206. +{
  31207. + int err;
  31208. +
  31209. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  31210. + if (unlikely(err))
  31211. + pr_err("err %d (ignored)\n", err);
  31212. +}
  31213. diff -Naur null/fs/aufs/vdir.c linux-6.6.63/fs/aufs/vdir.c
  31214. --- /dev/null
  31215. +++ linux-6.6.63/fs/aufs/vdir.c 2024-12-18 15:12:59.300519526 +0300
  31216. @@ -0,0 +1,896 @@
  31217. +// SPDX-License-Identifier: GPL-2.0
  31218. +/*
  31219. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  31220. + *
  31221. + * This program is free software; you can redistribute it and/or modify
  31222. + * it under the terms of the GNU General Public License as published by
  31223. + * the Free Software Foundation; either version 2 of the License, or
  31224. + * (at your option) any later version.
  31225. + *
  31226. + * This program is distributed in the hope that it will be useful,
  31227. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31228. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31229. + * GNU General Public License for more details.
  31230. + *
  31231. + * You should have received a copy of the GNU General Public License
  31232. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31233. + */
  31234. +
  31235. +/*
  31236. + * virtual or vertical directory
  31237. + */
  31238. +
  31239. +#include <linux/iversion.h>
  31240. +#include "aufs.h"
  31241. +
  31242. +static unsigned int calc_size(int nlen)
  31243. +{
  31244. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  31245. +}
  31246. +
  31247. +static int set_deblk_end(union au_vdir_deblk_p *p,
  31248. + union au_vdir_deblk_p *deblk_end)
  31249. +{
  31250. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  31251. + p->de->de_str.len = 0;
  31252. + /* smp_mb(); */
  31253. + return 0;
  31254. + }
  31255. + return -1; /* error */
  31256. +}
  31257. +
  31258. +/* returns true or false */
  31259. +static int is_deblk_end(union au_vdir_deblk_p *p,
  31260. + union au_vdir_deblk_p *deblk_end)
  31261. +{
  31262. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  31263. + return !p->de->de_str.len;
  31264. + return 1;
  31265. +}
  31266. +
  31267. +static unsigned char *last_deblk(struct au_vdir *vdir)
  31268. +{
  31269. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  31270. +}
  31271. +
  31272. +/* ---------------------------------------------------------------------- */
  31273. +
  31274. +/* estimate the appropriate size for name hash table */
  31275. +unsigned int au_rdhash_est(loff_t sz)
  31276. +{
  31277. + unsigned int n;
  31278. +
  31279. + n = UINT_MAX;
  31280. + sz >>= 10;
  31281. + if (sz < n)
  31282. + n = sz;
  31283. + if (sz < AUFS_RDHASH_DEF)
  31284. + n = AUFS_RDHASH_DEF;
  31285. + /* pr_info("n %u\n", n); */
  31286. + return n;
  31287. +}
  31288. +
  31289. +/*
  31290. + * the allocated memory has to be freed by
  31291. + * au_nhash_wh_free() or au_nhash_de_free().
  31292. + */
  31293. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  31294. +{
  31295. + struct hlist_head *head;
  31296. + unsigned int u;
  31297. + size_t sz;
  31298. +
  31299. + sz = sizeof(*nhash->nh_head) * num_hash;
  31300. + head = kmalloc(sz, gfp);
  31301. + if (head) {
  31302. + nhash->nh_num = num_hash;
  31303. + nhash->nh_head = head;
  31304. + for (u = 0; u < num_hash; u++)
  31305. + INIT_HLIST_HEAD(head++);
  31306. + return 0; /* success */
  31307. + }
  31308. +
  31309. + return -ENOMEM;
  31310. +}
  31311. +
  31312. +static void nhash_count(struct hlist_head *head)
  31313. +{
  31314. +#if 0 /* debugging */
  31315. + unsigned long n;
  31316. + struct hlist_node *pos;
  31317. +
  31318. + n = 0;
  31319. + hlist_for_each(pos, head)
  31320. + n++;
  31321. + pr_info("%lu\n", n);
  31322. +#endif
  31323. +}
  31324. +
  31325. +static void au_nhash_wh_do_free(struct hlist_head *head)
  31326. +{
  31327. + struct au_vdir_wh *pos;
  31328. + struct hlist_node *node;
  31329. +
  31330. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  31331. + au_kfree_rcu(pos);
  31332. +}
  31333. +
  31334. +static void au_nhash_de_do_free(struct hlist_head *head)
  31335. +{
  31336. + struct au_vdir_dehstr *pos;
  31337. + struct hlist_node *node;
  31338. +
  31339. + hlist_for_each_entry_safe(pos, node, head, hash)
  31340. + au_cache_free_vdir_dehstr(pos);
  31341. +}
  31342. +
  31343. +static void au_nhash_do_free(struct au_nhash *nhash,
  31344. + void (*free)(struct hlist_head *head))
  31345. +{
  31346. + unsigned int n;
  31347. + struct hlist_head *head;
  31348. +
  31349. + n = nhash->nh_num;
  31350. + if (!n)
  31351. + return;
  31352. +
  31353. + head = nhash->nh_head;
  31354. + while (n-- > 0) {
  31355. + nhash_count(head);
  31356. + free(head++);
  31357. + }
  31358. + au_kfree_try_rcu(nhash->nh_head);
  31359. +}
  31360. +
  31361. +void au_nhash_wh_free(struct au_nhash *whlist)
  31362. +{
  31363. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  31364. +}
  31365. +
  31366. +static void au_nhash_de_free(struct au_nhash *delist)
  31367. +{
  31368. + au_nhash_do_free(delist, au_nhash_de_do_free);
  31369. +}
  31370. +
  31371. +/* ---------------------------------------------------------------------- */
  31372. +
  31373. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  31374. + int limit)
  31375. +{
  31376. + int num;
  31377. + unsigned int u, n;
  31378. + struct hlist_head *head;
  31379. + struct au_vdir_wh *pos;
  31380. +
  31381. + num = 0;
  31382. + n = whlist->nh_num;
  31383. + head = whlist->nh_head;
  31384. + for (u = 0; u < n; u++, head++)
  31385. + hlist_for_each_entry(pos, head, wh_hash)
  31386. + if (pos->wh_bindex == btgt && ++num > limit)
  31387. + return 1;
  31388. + return 0;
  31389. +}
  31390. +
  31391. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  31392. + unsigned char *name,
  31393. + unsigned int len)
  31394. +{
  31395. + unsigned int v;
  31396. + /* const unsigned int magic_bit = 12; */
  31397. +
  31398. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  31399. +
  31400. + v = 0;
  31401. + if (len > 8)
  31402. + len = 8;
  31403. + while (len--)
  31404. + v += *name++;
  31405. + /* v = hash_long(v, magic_bit); */
  31406. + v %= nhash->nh_num;
  31407. + return nhash->nh_head + v;
  31408. +}
  31409. +
  31410. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  31411. + int nlen)
  31412. +{
  31413. + return str->len == nlen && !memcmp(str->name, name, nlen);
  31414. +}
  31415. +
  31416. +/* returns found or not */
  31417. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  31418. +{
  31419. + struct hlist_head *head;
  31420. + struct au_vdir_wh *pos;
  31421. + struct au_vdir_destr *str;
  31422. +
  31423. + head = au_name_hash(whlist, name, nlen);
  31424. + hlist_for_each_entry(pos, head, wh_hash) {
  31425. + str = &pos->wh_str;
  31426. + AuDbg("%.*s\n", str->len, str->name);
  31427. + if (au_nhash_test_name(str, name, nlen))
  31428. + return 1;
  31429. + }
  31430. + return 0;
  31431. +}
  31432. +
  31433. +/* returns found(true) or not */
  31434. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  31435. +{
  31436. + struct hlist_head *head;
  31437. + struct au_vdir_dehstr *pos;
  31438. + struct au_vdir_destr *str;
  31439. +
  31440. + head = au_name_hash(delist, name, nlen);
  31441. + hlist_for_each_entry(pos, head, hash) {
  31442. + str = pos->str;
  31443. + AuDbg("%.*s\n", str->len, str->name);
  31444. + if (au_nhash_test_name(str, name, nlen))
  31445. + return 1;
  31446. + }
  31447. + return 0;
  31448. +}
  31449. +
  31450. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  31451. + unsigned char d_type)
  31452. +{
  31453. +#ifdef CONFIG_AUFS_SHWH
  31454. + wh->wh_ino = ino;
  31455. + wh->wh_type = d_type;
  31456. +#endif
  31457. +}
  31458. +
  31459. +/* ---------------------------------------------------------------------- */
  31460. +
  31461. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  31462. + unsigned int d_type, aufs_bindex_t bindex,
  31463. + unsigned char shwh)
  31464. +{
  31465. + int err;
  31466. + struct au_vdir_destr *str;
  31467. + struct au_vdir_wh *wh;
  31468. +
  31469. + AuDbg("%.*s\n", nlen, name);
  31470. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  31471. +
  31472. + err = -ENOMEM;
  31473. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  31474. + if (unlikely(!wh))
  31475. + goto out;
  31476. +
  31477. + err = 0;
  31478. + wh->wh_bindex = bindex;
  31479. + if (shwh)
  31480. + au_shwh_init_wh(wh, ino, d_type);
  31481. + str = &wh->wh_str;
  31482. + str->len = nlen;
  31483. + memcpy(str->name, name, nlen);
  31484. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  31485. + /* smp_mb(); */
  31486. +
  31487. +out:
  31488. + return err;
  31489. +}
  31490. +
  31491. +static int append_deblk(struct au_vdir *vdir)
  31492. +{
  31493. + int err;
  31494. + unsigned long ul;
  31495. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31496. + union au_vdir_deblk_p p, deblk_end;
  31497. + unsigned char **o;
  31498. +
  31499. + err = -ENOMEM;
  31500. + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  31501. + GFP_NOFS, /*may_shrink*/0);
  31502. + if (unlikely(!o))
  31503. + goto out;
  31504. +
  31505. + vdir->vd_deblk = o;
  31506. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  31507. + if (p.deblk) {
  31508. + ul = vdir->vd_nblk++;
  31509. + vdir->vd_deblk[ul] = p.deblk;
  31510. + vdir->vd_last.ul = ul;
  31511. + vdir->vd_last.p.deblk = p.deblk;
  31512. + deblk_end.deblk = p.deblk + deblk_sz;
  31513. + err = set_deblk_end(&p, &deblk_end);
  31514. + }
  31515. +
  31516. +out:
  31517. + return err;
  31518. +}
  31519. +
  31520. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  31521. + unsigned int d_type, struct au_nhash *delist)
  31522. +{
  31523. + int err;
  31524. + unsigned int sz;
  31525. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31526. + union au_vdir_deblk_p p, *room, deblk_end;
  31527. + struct au_vdir_dehstr *dehstr;
  31528. +
  31529. + p.deblk = last_deblk(vdir);
  31530. + deblk_end.deblk = p.deblk + deblk_sz;
  31531. + room = &vdir->vd_last.p;
  31532. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  31533. + || !is_deblk_end(room, &deblk_end));
  31534. +
  31535. + sz = calc_size(nlen);
  31536. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  31537. + err = append_deblk(vdir);
  31538. + if (unlikely(err))
  31539. + goto out;
  31540. +
  31541. + p.deblk = last_deblk(vdir);
  31542. + deblk_end.deblk = p.deblk + deblk_sz;
  31543. + /* smp_mb(); */
  31544. + AuDebugOn(room->deblk != p.deblk);
  31545. + }
  31546. +
  31547. + err = -ENOMEM;
  31548. + dehstr = au_cache_alloc_vdir_dehstr();
  31549. + if (unlikely(!dehstr))
  31550. + goto out;
  31551. +
  31552. + dehstr->str = &room->de->de_str;
  31553. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  31554. + room->de->de_ino = ino;
  31555. + room->de->de_type = d_type;
  31556. + room->de->de_str.len = nlen;
  31557. + memcpy(room->de->de_str.name, name, nlen);
  31558. +
  31559. + err = 0;
  31560. + room->deblk += sz;
  31561. + if (unlikely(set_deblk_end(room, &deblk_end)))
  31562. + err = append_deblk(vdir);
  31563. + /* smp_mb(); */
  31564. +
  31565. +out:
  31566. + return err;
  31567. +}
  31568. +
  31569. +/* ---------------------------------------------------------------------- */
  31570. +
  31571. +void au_vdir_free(struct au_vdir *vdir)
  31572. +{
  31573. + unsigned char **deblk;
  31574. +
  31575. + deblk = vdir->vd_deblk;
  31576. + while (vdir->vd_nblk--)
  31577. + au_kfree_try_rcu(*deblk++);
  31578. + au_kfree_try_rcu(vdir->vd_deblk);
  31579. + au_cache_free_vdir(vdir);
  31580. +}
  31581. +
  31582. +static struct au_vdir *alloc_vdir(struct file *file)
  31583. +{
  31584. + struct au_vdir *vdir;
  31585. + struct super_block *sb;
  31586. + int err;
  31587. +
  31588. + sb = file->f_path.dentry->d_sb;
  31589. + SiMustAnyLock(sb);
  31590. +
  31591. + err = -ENOMEM;
  31592. + vdir = au_cache_alloc_vdir();
  31593. + if (unlikely(!vdir))
  31594. + goto out;
  31595. +
  31596. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  31597. + if (unlikely(!vdir->vd_deblk))
  31598. + goto out_free;
  31599. +
  31600. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  31601. + if (!vdir->vd_deblk_sz) {
  31602. + /* estimate the appropriate size for deblk */
  31603. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  31604. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  31605. + }
  31606. + vdir->vd_nblk = 0;
  31607. + vdir->vd_version = 0;
  31608. + vdir->vd_jiffy = 0;
  31609. + err = append_deblk(vdir);
  31610. + if (!err)
  31611. + return vdir; /* success */
  31612. +
  31613. + au_kfree_try_rcu(vdir->vd_deblk);
  31614. +
  31615. +out_free:
  31616. + au_cache_free_vdir(vdir);
  31617. +out:
  31618. + vdir = ERR_PTR(err);
  31619. + return vdir;
  31620. +}
  31621. +
  31622. +static int reinit_vdir(struct au_vdir *vdir)
  31623. +{
  31624. + int err;
  31625. + union au_vdir_deblk_p p, deblk_end;
  31626. +
  31627. + while (vdir->vd_nblk > 1) {
  31628. + au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
  31629. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  31630. + vdir->vd_nblk--;
  31631. + }
  31632. + p.deblk = vdir->vd_deblk[0];
  31633. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  31634. + err = set_deblk_end(&p, &deblk_end);
  31635. + /* keep vd_dblk_sz */
  31636. + vdir->vd_last.ul = 0;
  31637. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31638. + vdir->vd_version = 0;
  31639. + vdir->vd_jiffy = 0;
  31640. + /* smp_mb(); */
  31641. + return err;
  31642. +}
  31643. +
  31644. +/* ---------------------------------------------------------------------- */
  31645. +
  31646. +#define AuFillVdir_CALLED 1
  31647. +#define AuFillVdir_WHABLE (1 << 1)
  31648. +#define AuFillVdir_SHWH (1 << 2)
  31649. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  31650. +#define au_fset_fillvdir(flags, name) \
  31651. + do { (flags) |= AuFillVdir_##name; } while (0)
  31652. +#define au_fclr_fillvdir(flags, name) \
  31653. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  31654. +
  31655. +#ifndef CONFIG_AUFS_SHWH
  31656. +#undef AuFillVdir_SHWH
  31657. +#define AuFillVdir_SHWH 0
  31658. +#endif
  31659. +
  31660. +struct fillvdir_arg {
  31661. + struct dir_context ctx;
  31662. + struct file *file;
  31663. + struct au_vdir *vdir;
  31664. + struct au_nhash delist;
  31665. + struct au_nhash whlist;
  31666. + aufs_bindex_t bindex;
  31667. + unsigned int flags;
  31668. + int err;
  31669. +};
  31670. +
  31671. +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  31672. + loff_t offset __maybe_unused, u64 h_ino,
  31673. + unsigned int d_type)
  31674. +{
  31675. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  31676. + char *name = (void *)__name;
  31677. + struct super_block *sb;
  31678. + ino_t ino;
  31679. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  31680. +
  31681. + arg->err = 0;
  31682. + sb = arg->file->f_path.dentry->d_sb;
  31683. + au_fset_fillvdir(arg->flags, CALLED);
  31684. + /* smp_mb(); */
  31685. + if (nlen <= AUFS_WH_PFX_LEN
  31686. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  31687. + if (test_known(&arg->delist, name, nlen)
  31688. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31689. + goto out; /* already exists or whiteouted */
  31690. +
  31691. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  31692. + if (!arg->err) {
  31693. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  31694. + d_type = DT_UNKNOWN;
  31695. + arg->err = append_de(arg->vdir, name, nlen, ino,
  31696. + d_type, &arg->delist);
  31697. + }
  31698. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  31699. + name += AUFS_WH_PFX_LEN;
  31700. + nlen -= AUFS_WH_PFX_LEN;
  31701. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31702. + goto out; /* already whiteouted */
  31703. +
  31704. + ino = 0; /* just to suppress a warning */
  31705. + if (shwh)
  31706. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  31707. + &ino);
  31708. + if (!arg->err) {
  31709. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  31710. + d_type = DT_UNKNOWN;
  31711. + arg->err = au_nhash_append_wh
  31712. + (&arg->whlist, name, nlen, ino, d_type,
  31713. + arg->bindex, shwh);
  31714. + }
  31715. + }
  31716. +
  31717. +out:
  31718. + if (!arg->err)
  31719. + arg->vdir->vd_jiffy = jiffies;
  31720. + /* smp_mb(); */
  31721. + AuTraceErr(arg->err);
  31722. + return !arg->err;
  31723. +}
  31724. +
  31725. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  31726. + struct au_nhash *whlist, struct au_nhash *delist)
  31727. +{
  31728. +#ifdef CONFIG_AUFS_SHWH
  31729. + int err;
  31730. + unsigned int nh, u;
  31731. + struct hlist_head *head;
  31732. + struct au_vdir_wh *pos;
  31733. + struct hlist_node *n;
  31734. + char *p, *o;
  31735. + struct au_vdir_destr *destr;
  31736. +
  31737. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  31738. +
  31739. + err = -ENOMEM;
  31740. + o = p = (void *)__get_free_page(GFP_NOFS);
  31741. + if (unlikely(!p))
  31742. + goto out;
  31743. +
  31744. + err = 0;
  31745. + nh = whlist->nh_num;
  31746. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31747. + p += AUFS_WH_PFX_LEN;
  31748. + for (u = 0; u < nh; u++) {
  31749. + head = whlist->nh_head + u;
  31750. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  31751. + destr = &pos->wh_str;
  31752. + memcpy(p, destr->name, destr->len);
  31753. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  31754. + pos->wh_ino, pos->wh_type, delist);
  31755. + if (unlikely(err))
  31756. + break;
  31757. + }
  31758. + }
  31759. +
  31760. + free_page((unsigned long)o);
  31761. +
  31762. +out:
  31763. + AuTraceErr(err);
  31764. + return err;
  31765. +#else
  31766. + return 0;
  31767. +#endif
  31768. +}
  31769. +
  31770. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  31771. +{
  31772. + int err;
  31773. + unsigned int rdhash;
  31774. + loff_t offset;
  31775. + aufs_bindex_t bbot, bindex, btop;
  31776. + unsigned char shwh;
  31777. + struct file *hf, *file;
  31778. + struct super_block *sb;
  31779. +
  31780. + file = arg->file;
  31781. + sb = file->f_path.dentry->d_sb;
  31782. + SiMustAnyLock(sb);
  31783. +
  31784. + rdhash = au_sbi(sb)->si_rdhash;
  31785. + if (!rdhash)
  31786. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  31787. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  31788. + if (unlikely(err))
  31789. + goto out;
  31790. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  31791. + if (unlikely(err))
  31792. + goto out_delist;
  31793. +
  31794. + err = 0;
  31795. + arg->flags = 0;
  31796. + shwh = 0;
  31797. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  31798. + shwh = 1;
  31799. + au_fset_fillvdir(arg->flags, SHWH);
  31800. + }
  31801. + btop = au_fbtop(file);
  31802. + bbot = au_fbbot_dir(file);
  31803. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  31804. + hf = au_hf_dir(file, bindex);
  31805. + if (!hf)
  31806. + continue;
  31807. +
  31808. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  31809. + err = offset;
  31810. + if (unlikely(offset))
  31811. + break;
  31812. +
  31813. + arg->bindex = bindex;
  31814. + au_fclr_fillvdir(arg->flags, WHABLE);
  31815. + if (shwh
  31816. + || (bindex != bbot
  31817. + && au_br_whable(au_sbr_perm(sb, bindex))))
  31818. + au_fset_fillvdir(arg->flags, WHABLE);
  31819. + do {
  31820. + arg->err = 0;
  31821. + au_fclr_fillvdir(arg->flags, CALLED);
  31822. + /* smp_mb(); */
  31823. + err = vfsub_iterate_dir(hf, &arg->ctx);
  31824. + if (err >= 0)
  31825. + err = arg->err;
  31826. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  31827. +
  31828. + /*
  31829. + * dir_relax() may be good for concurrency, but aufs should not
  31830. + * use it since it will cause a lockdep problem.
  31831. + */
  31832. + }
  31833. +
  31834. + if (!err && shwh)
  31835. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  31836. +
  31837. + au_nhash_wh_free(&arg->whlist);
  31838. +
  31839. +out_delist:
  31840. + au_nhash_de_free(&arg->delist);
  31841. +out:
  31842. + return err;
  31843. +}
  31844. +
  31845. +static int read_vdir(struct file *file, int may_read)
  31846. +{
  31847. + int err;
  31848. + unsigned long expire;
  31849. + unsigned char do_read;
  31850. + struct fillvdir_arg arg = {
  31851. + .ctx = {
  31852. + .actor = fillvdir
  31853. + }
  31854. + };
  31855. + struct inode *inode;
  31856. + struct au_vdir *vdir, *allocated;
  31857. +
  31858. + err = 0;
  31859. + inode = file_inode(file);
  31860. + IMustLock(inode);
  31861. + IiMustWriteLock(inode);
  31862. + SiMustAnyLock(inode->i_sb);
  31863. +
  31864. + allocated = NULL;
  31865. + do_read = 0;
  31866. + expire = au_sbi(inode->i_sb)->si_rdcache;
  31867. + vdir = au_ivdir(inode);
  31868. + if (!vdir) {
  31869. + do_read = 1;
  31870. + vdir = alloc_vdir(file);
  31871. + err = PTR_ERR(vdir);
  31872. + if (IS_ERR(vdir))
  31873. + goto out;
  31874. + err = 0;
  31875. + allocated = vdir;
  31876. + } else if (may_read
  31877. + && (!inode_eq_iversion(inode, vdir->vd_version)
  31878. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  31879. + do_read = 1;
  31880. + err = reinit_vdir(vdir);
  31881. + if (unlikely(err))
  31882. + goto out;
  31883. + }
  31884. +
  31885. + if (!do_read)
  31886. + return 0; /* success */
  31887. +
  31888. + arg.file = file;
  31889. + arg.vdir = vdir;
  31890. + err = au_do_read_vdir(&arg);
  31891. + if (!err) {
  31892. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  31893. + vdir->vd_version = inode_query_iversion(inode);
  31894. + vdir->vd_last.ul = 0;
  31895. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31896. + if (allocated)
  31897. + au_set_ivdir(inode, allocated);
  31898. + } else if (allocated)
  31899. + au_vdir_free(allocated);
  31900. +
  31901. +out:
  31902. + return err;
  31903. +}
  31904. +
  31905. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  31906. +{
  31907. + int err, rerr;
  31908. + unsigned long ul, n;
  31909. + const unsigned int deblk_sz = src->vd_deblk_sz;
  31910. +
  31911. + AuDebugOn(tgt->vd_nblk != 1);
  31912. +
  31913. + err = -ENOMEM;
  31914. + if (tgt->vd_nblk < src->vd_nblk) {
  31915. + unsigned char **p;
  31916. +
  31917. + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  31918. + GFP_NOFS, /*may_shrink*/0);
  31919. + if (unlikely(!p))
  31920. + goto out;
  31921. + tgt->vd_deblk = p;
  31922. + }
  31923. +
  31924. + if (tgt->vd_deblk_sz != deblk_sz) {
  31925. + unsigned char *p;
  31926. +
  31927. + tgt->vd_deblk_sz = deblk_sz;
  31928. + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
  31929. + /*may_shrink*/1);
  31930. + if (unlikely(!p))
  31931. + goto out;
  31932. + tgt->vd_deblk[0] = p;
  31933. + }
  31934. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  31935. + tgt->vd_version = src->vd_version;
  31936. + tgt->vd_jiffy = src->vd_jiffy;
  31937. +
  31938. + n = src->vd_nblk;
  31939. + for (ul = 1; ul < n; ul++) {
  31940. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  31941. + GFP_NOFS);
  31942. + if (unlikely(!tgt->vd_deblk[ul]))
  31943. + goto out;
  31944. + tgt->vd_nblk++;
  31945. + }
  31946. + tgt->vd_nblk = n;
  31947. + tgt->vd_last.ul = tgt->vd_last.ul;
  31948. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  31949. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  31950. + - src->vd_deblk[src->vd_last.ul];
  31951. + /* smp_mb(); */
  31952. + return 0; /* success */
  31953. +
  31954. +out:
  31955. + rerr = reinit_vdir(tgt);
  31956. + BUG_ON(rerr);
  31957. + return err;
  31958. +}
  31959. +
  31960. +int au_vdir_init(struct file *file)
  31961. +{
  31962. + int err;
  31963. + struct inode *inode;
  31964. + struct au_vdir *vdir_cache, *allocated;
  31965. +
  31966. + /* test file->f_pos here instead of ctx->pos */
  31967. + err = read_vdir(file, !file->f_pos);
  31968. + if (unlikely(err))
  31969. + goto out;
  31970. +
  31971. + allocated = NULL;
  31972. + vdir_cache = au_fvdir_cache(file);
  31973. + if (!vdir_cache) {
  31974. + vdir_cache = alloc_vdir(file);
  31975. + err = PTR_ERR(vdir_cache);
  31976. + if (IS_ERR(vdir_cache))
  31977. + goto out;
  31978. + allocated = vdir_cache;
  31979. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  31980. + /* test file->f_pos here instead of ctx->pos */
  31981. + err = reinit_vdir(vdir_cache);
  31982. + if (unlikely(err))
  31983. + goto out;
  31984. + } else
  31985. + return 0; /* success */
  31986. +
  31987. + inode = file_inode(file);
  31988. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  31989. + if (!err) {
  31990. + file->f_version = inode_query_iversion(inode);
  31991. + if (allocated)
  31992. + au_set_fvdir_cache(file, allocated);
  31993. + } else if (allocated)
  31994. + au_vdir_free(allocated);
  31995. +
  31996. +out:
  31997. + return err;
  31998. +}
  31999. +
  32000. +static loff_t calc_offset(struct au_vdir *vdir)
  32001. +{
  32002. + loff_t offset;
  32003. + union au_vdir_deblk_p p;
  32004. +
  32005. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  32006. + offset = vdir->vd_last.p.deblk - p.deblk;
  32007. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  32008. + return offset;
  32009. +}
  32010. +
  32011. +/* returns true or false */
  32012. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  32013. +{
  32014. + int valid;
  32015. + unsigned int deblk_sz;
  32016. + unsigned long ul, n;
  32017. + loff_t offset;
  32018. + union au_vdir_deblk_p p, deblk_end;
  32019. + struct au_vdir *vdir_cache;
  32020. +
  32021. + valid = 1;
  32022. + vdir_cache = au_fvdir_cache(file);
  32023. + offset = calc_offset(vdir_cache);
  32024. + AuDbg("offset %lld\n", offset);
  32025. + if (ctx->pos == offset)
  32026. + goto out;
  32027. +
  32028. + vdir_cache->vd_last.ul = 0;
  32029. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  32030. + if (!ctx->pos)
  32031. + goto out;
  32032. +
  32033. + valid = 0;
  32034. + deblk_sz = vdir_cache->vd_deblk_sz;
  32035. + ul = div64_u64(ctx->pos, deblk_sz);
  32036. + AuDbg("ul %lu\n", ul);
  32037. + if (ul >= vdir_cache->vd_nblk)
  32038. + goto out;
  32039. +
  32040. + n = vdir_cache->vd_nblk;
  32041. + for (; ul < n; ul++) {
  32042. + p.deblk = vdir_cache->vd_deblk[ul];
  32043. + deblk_end.deblk = p.deblk + deblk_sz;
  32044. + offset = ul;
  32045. + offset *= deblk_sz;
  32046. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  32047. + unsigned int l;
  32048. +
  32049. + l = calc_size(p.de->de_str.len);
  32050. + offset += l;
  32051. + p.deblk += l;
  32052. + }
  32053. + if (!is_deblk_end(&p, &deblk_end)) {
  32054. + valid = 1;
  32055. + vdir_cache->vd_last.ul = ul;
  32056. + vdir_cache->vd_last.p = p;
  32057. + break;
  32058. + }
  32059. + }
  32060. +
  32061. +out:
  32062. + /* smp_mb(); */
  32063. + if (!valid)
  32064. + AuDbg("valid %d\n", !valid);
  32065. + return valid;
  32066. +}
  32067. +
  32068. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  32069. +{
  32070. + unsigned int l, deblk_sz;
  32071. + union au_vdir_deblk_p deblk_end;
  32072. + struct au_vdir *vdir_cache;
  32073. + struct au_vdir_de *de;
  32074. +
  32075. + if (!seek_vdir(file, ctx))
  32076. + return 0;
  32077. +
  32078. + vdir_cache = au_fvdir_cache(file);
  32079. + deblk_sz = vdir_cache->vd_deblk_sz;
  32080. + while (1) {
  32081. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  32082. + deblk_end.deblk += deblk_sz;
  32083. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  32084. + de = vdir_cache->vd_last.p.de;
  32085. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  32086. + de->de_str.len, de->de_str.name, ctx->pos,
  32087. + (unsigned long)de->de_ino, de->de_type);
  32088. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  32089. + de->de_str.len, de->de_ino,
  32090. + de->de_type))) {
  32091. + /* todo: ignore the error caused by udba? */
  32092. + /* return err; */
  32093. + return 0;
  32094. + }
  32095. +
  32096. + l = calc_size(de->de_str.len);
  32097. + vdir_cache->vd_last.p.deblk += l;
  32098. + ctx->pos += l;
  32099. + }
  32100. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  32101. + vdir_cache->vd_last.ul++;
  32102. + vdir_cache->vd_last.p.deblk
  32103. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  32104. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  32105. + continue;
  32106. + }
  32107. + break;
  32108. + }
  32109. +
  32110. + /* smp_mb(); */
  32111. + return 0;
  32112. +}
  32113. diff -Naur null/fs/aufs/vfsub.c linux-6.6.63/fs/aufs/vfsub.c
  32114. --- /dev/null
  32115. +++ linux-6.6.63/fs/aufs/vfsub.c 2024-12-18 15:12:59.300519526 +0300
  32116. @@ -0,0 +1,964 @@
  32117. +// SPDX-License-Identifier: GPL-2.0
  32118. +/*
  32119. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  32120. + *
  32121. + * This program is free software; you can redistribute it and/or modify
  32122. + * it under the terms of the GNU General Public License as published by
  32123. + * the Free Software Foundation; either version 2 of the License, or
  32124. + * (at your option) any later version.
  32125. + *
  32126. + * This program is distributed in the hope that it will be useful,
  32127. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32128. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32129. + * GNU General Public License for more details.
  32130. + *
  32131. + * You should have received a copy of the GNU General Public License
  32132. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32133. + */
  32134. +
  32135. +/*
  32136. + * sub-routines for VFS
  32137. + */
  32138. +
  32139. +#include <linux/mnt_namespace.h>
  32140. +#include <linux/nsproxy.h>
  32141. +#include <linux/security.h>
  32142. +#include <linux/splice.h>
  32143. +#include "aufs.h"
  32144. +
  32145. +#ifdef CONFIG_AUFS_BR_FUSE
  32146. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  32147. +{
  32148. + if (!au_test_fuse(h_sb) || !au_userns)
  32149. + return 0;
  32150. +
  32151. + return is_current_mnt_ns(mnt) ? 0 : -EACCES;
  32152. +}
  32153. +#endif
  32154. +
  32155. +int vfsub_sync_filesystem(struct super_block *h_sb)
  32156. +{
  32157. + int err;
  32158. +
  32159. + lockdep_off();
  32160. + down_read(&h_sb->s_umount);
  32161. + err = sync_filesystem(h_sb);
  32162. + up_read(&h_sb->s_umount);
  32163. + lockdep_on();
  32164. +
  32165. + return err;
  32166. +}
  32167. +
  32168. +/* ---------------------------------------------------------------------- */
  32169. +
  32170. +unsigned int vfsub_inode_nlink_aufs(struct inode *inode)
  32171. +{
  32172. + unsigned int nlink;
  32173. +
  32174. + au_nlink_lock(inode);
  32175. + nlink = inode->i_nlink;
  32176. + au_nlink_unlock(inode);
  32177. +
  32178. + return nlink;
  32179. +}
  32180. +
  32181. +void vfsub_inc_nlink(struct inode *inode)
  32182. +{
  32183. + au_nlink_lock(inode);
  32184. + inc_nlink(inode);
  32185. + au_nlink_unlock(inode);
  32186. +}
  32187. +
  32188. +void vfsub_drop_nlink(struct inode *inode)
  32189. +{
  32190. + au_nlink_lock(inode);
  32191. + AuDebugOn(!inode->i_nlink);
  32192. + drop_nlink(inode);
  32193. + au_nlink_unlock(inode);
  32194. +}
  32195. +
  32196. +void vfsub_clear_nlink(struct inode *inode)
  32197. +{
  32198. + au_nlink_lock(inode);
  32199. + AuDebugOn(!inode->i_nlink);
  32200. + clear_nlink(inode);
  32201. + au_nlink_unlock(inode);
  32202. +}
  32203. +
  32204. +void vfsub_set_nlink(struct inode *inode, unsigned int nlink)
  32205. +{
  32206. + /*
  32207. + * stop setting the value equal to the current one, in order to stop
  32208. + * a useless warning from vfs:destroy_inode() about sb->s_remove_count.
  32209. + */
  32210. + au_nlink_lock(inode);
  32211. + if (nlink != inode->i_nlink)
  32212. + set_nlink(inode, nlink);
  32213. + au_nlink_unlock(inode);
  32214. +}
  32215. +
  32216. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  32217. +{
  32218. + int err;
  32219. + struct kstat st;
  32220. + struct super_block *h_sb;
  32221. +
  32222. + /*
  32223. + * Always needs h_path->mnt for LSM or FUSE branch.
  32224. + */
  32225. + AuDebugOn(!h_path->mnt);
  32226. +
  32227. + /* for remote fs, leave work for its getattr or d_revalidate */
  32228. + /* for bad i_attr fs, handle them in aufs_getattr() */
  32229. + /* still some fs may acquire i_mutex. we need to skip them */
  32230. + err = 0;
  32231. + if (!did)
  32232. + did = &err;
  32233. + h_sb = h_path->dentry->d_sb;
  32234. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  32235. + if (*did)
  32236. + err = vfsub_getattr(h_path, &st);
  32237. +
  32238. + return err;
  32239. +}
  32240. +
  32241. +/* ---------------------------------------------------------------------- */
  32242. +
  32243. +struct file *vfsub_dentry_open(struct path *path, int flags)
  32244. +{
  32245. + return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  32246. + current_cred());
  32247. +}
  32248. +
  32249. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  32250. +{
  32251. + struct file *file;
  32252. +
  32253. + lockdep_off();
  32254. + file = filp_open(path,
  32255. + oflags /* | __FMODE_NONOTIFY */,
  32256. + mode);
  32257. + lockdep_on();
  32258. + if (IS_ERR(file))
  32259. + goto out;
  32260. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32261. +
  32262. +out:
  32263. + return file;
  32264. +}
  32265. +
  32266. +/*
  32267. + * Ideally this function should call VFS:do_last() in order to keep all its
  32268. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  32269. + * structure such as nameidata. This is a second (or third) best approach.
  32270. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  32271. + */
  32272. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  32273. + struct vfsub_aopen_args *args)
  32274. +{
  32275. + int err;
  32276. + struct au_branch *br = args->br;
  32277. + struct file *file = args->file;
  32278. + /* copied from linux/fs/namei.c:atomic_open() */
  32279. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  32280. +
  32281. + IMustLock(dir);
  32282. + AuDebugOn(!dir->i_op->atomic_open);
  32283. +
  32284. + err = au_br_test_oflag(args->open_flag, br);
  32285. + if (unlikely(err))
  32286. + goto out;
  32287. +
  32288. + au_lcnt_inc(&br->br_nfiles);
  32289. + file->f_path.dentry = DENTRY_NOT_SET;
  32290. + file->f_path.mnt = au_br_mnt(br);
  32291. + AuDbg("%ps\n", dir->i_op->atomic_open);
  32292. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  32293. + args->create_mode);
  32294. + if (unlikely(err < 0)) {
  32295. + au_lcnt_dec(&br->br_nfiles);
  32296. + goto out;
  32297. + }
  32298. +
  32299. + /* temporary workaround for nfsv4 branch */
  32300. + if (au_test_nfs(dir->i_sb))
  32301. + nfs_mark_for_revalidate(dir);
  32302. +
  32303. + if (file->f_mode & FMODE_CREATED)
  32304. + fsnotify_create(dir, dentry);
  32305. + if (!(file->f_mode & FMODE_OPENED)) {
  32306. + au_lcnt_dec(&br->br_nfiles);
  32307. + goto out;
  32308. + }
  32309. +
  32310. + /* todo: call VFS:may_open() here */
  32311. + /* todo: ima_file_check() too? */
  32312. + if (!err && (args->open_flag & __FMODE_EXEC))
  32313. + err = deny_write_access(file);
  32314. + if (!err)
  32315. + fsnotify_open(file);
  32316. + else
  32317. + au_lcnt_dec(&br->br_nfiles);
  32318. + /* note that the file is created and still opened */
  32319. +
  32320. +out:
  32321. + return err;
  32322. +}
  32323. +
  32324. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  32325. +{
  32326. + int err;
  32327. +
  32328. + err = kern_path(name, flags, path);
  32329. + if (!err && d_is_positive(path->dentry))
  32330. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32331. + return err;
  32332. +}
  32333. +
  32334. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  32335. + struct path *ppath, int len)
  32336. +{
  32337. + struct path path;
  32338. +
  32339. + path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
  32340. + if (IS_ERR(path.dentry))
  32341. + goto out;
  32342. + if (d_is_positive(path.dentry)) {
  32343. + path.mnt = ppath->mnt;
  32344. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  32345. + }
  32346. +
  32347. +out:
  32348. + AuTraceErrPtr(path.dentry);
  32349. + return path.dentry;
  32350. +}
  32351. +
  32352. +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
  32353. + int len)
  32354. +{
  32355. + struct path path;
  32356. +
  32357. + /* VFS checks it too, but by WARN_ON_ONCE() */
  32358. + IMustLock(d_inode(ppath->dentry));
  32359. +
  32360. + path.dentry = lookup_one_len(name, ppath->dentry, len);
  32361. + if (IS_ERR(path.dentry))
  32362. + goto out;
  32363. + if (d_is_positive(path.dentry)) {
  32364. + path.mnt = ppath->mnt;
  32365. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  32366. + }
  32367. +
  32368. +out:
  32369. + AuTraceErrPtr(path.dentry);
  32370. + return path.dentry;
  32371. +}
  32372. +
  32373. +void vfsub_call_lkup_one(void *args)
  32374. +{
  32375. + struct vfsub_lkup_one_args *a = args;
  32376. + *a->errp = vfsub_lkup_one(a->name, a->ppath);
  32377. +}
  32378. +
  32379. +/* ---------------------------------------------------------------------- */
  32380. +
  32381. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32382. + struct dentry *d2, struct au_hinode *hdir2)
  32383. +{
  32384. + struct dentry *d;
  32385. +
  32386. + lockdep_off();
  32387. + d = lock_rename(d1, d2);
  32388. + lockdep_on();
  32389. + au_hn_suspend(hdir1);
  32390. + if (hdir1 != hdir2)
  32391. + au_hn_suspend(hdir2);
  32392. +
  32393. + return d;
  32394. +}
  32395. +
  32396. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32397. + struct dentry *d2, struct au_hinode *hdir2)
  32398. +{
  32399. + au_hn_resume(hdir1);
  32400. + if (hdir1 != hdir2)
  32401. + au_hn_resume(hdir2);
  32402. + lockdep_off();
  32403. + unlock_rename(d1, d2);
  32404. + lockdep_on();
  32405. +}
  32406. +
  32407. +/* ---------------------------------------------------------------------- */
  32408. +
  32409. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  32410. +{
  32411. + int err;
  32412. + struct dentry *d;
  32413. + struct mnt_idmap *idmap;
  32414. +
  32415. + IMustLock(dir);
  32416. +
  32417. + d = path->dentry;
  32418. + path->dentry = d->d_parent;
  32419. + err = security_path_mknod(path, d, mode, 0);
  32420. + path->dentry = d;
  32421. + if (unlikely(err))
  32422. + goto out;
  32423. + idmap = mnt_idmap(path->mnt);
  32424. +
  32425. + lockdep_off();
  32426. + err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
  32427. + lockdep_on();
  32428. + if (!err) {
  32429. + struct path tmp = *path;
  32430. + int did;
  32431. +
  32432. + vfsub_update_h_iattr(&tmp, &did);
  32433. + if (did) {
  32434. + tmp.dentry = path->dentry->d_parent;
  32435. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32436. + }
  32437. + /*ignore*/
  32438. + }
  32439. +
  32440. +out:
  32441. + return err;
  32442. +}
  32443. +
  32444. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  32445. +{
  32446. + int err;
  32447. + struct dentry *d;
  32448. + struct mnt_idmap *idmap;
  32449. +
  32450. + IMustLock(dir);
  32451. +
  32452. + d = path->dentry;
  32453. + path->dentry = d->d_parent;
  32454. + err = security_path_symlink(path, d, symname);
  32455. + path->dentry = d;
  32456. + if (unlikely(err))
  32457. + goto out;
  32458. + idmap = mnt_idmap(path->mnt);
  32459. +
  32460. + lockdep_off();
  32461. + err = vfs_symlink(idmap, dir, path->dentry, symname);
  32462. + lockdep_on();
  32463. + if (!err) {
  32464. + struct path tmp = *path;
  32465. + int did;
  32466. +
  32467. + vfsub_update_h_iattr(&tmp, &did);
  32468. + if (did) {
  32469. + tmp.dentry = path->dentry->d_parent;
  32470. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32471. + }
  32472. + /*ignore*/
  32473. + }
  32474. +
  32475. +out:
  32476. + return err;
  32477. +}
  32478. +
  32479. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  32480. +{
  32481. + int err;
  32482. + struct dentry *d;
  32483. + struct mnt_idmap *idmap;
  32484. +
  32485. + IMustLock(dir);
  32486. +
  32487. + d = path->dentry;
  32488. + path->dentry = d->d_parent;
  32489. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  32490. + path->dentry = d;
  32491. + if (unlikely(err))
  32492. + goto out;
  32493. + idmap = mnt_idmap(path->mnt);
  32494. +
  32495. + lockdep_off();
  32496. + err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
  32497. + lockdep_on();
  32498. + if (!err) {
  32499. + struct path tmp = *path;
  32500. + int did;
  32501. +
  32502. + vfsub_update_h_iattr(&tmp, &did);
  32503. + if (did) {
  32504. + tmp.dentry = path->dentry->d_parent;
  32505. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32506. + }
  32507. + /*ignore*/
  32508. + }
  32509. +
  32510. +out:
  32511. + return err;
  32512. +}
  32513. +
  32514. +static int au_test_nlink(struct inode *inode)
  32515. +{
  32516. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  32517. +
  32518. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  32519. + || vfsub_inode_nlink(inode, AU_I_BRANCH) < link_max)
  32520. + return 0;
  32521. + return -EMLINK;
  32522. +}
  32523. +
  32524. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  32525. + struct inode **delegated_inode)
  32526. +{
  32527. + int err;
  32528. + struct dentry *d;
  32529. + struct mnt_idmap *idmap;
  32530. +
  32531. + IMustLock(dir);
  32532. +
  32533. + err = au_test_nlink(d_inode(src_dentry));
  32534. + if (unlikely(err))
  32535. + return err;
  32536. +
  32537. + /* we don't call may_linkat() */
  32538. + d = path->dentry;
  32539. + path->dentry = d->d_parent;
  32540. + err = security_path_link(src_dentry, path, d);
  32541. + path->dentry = d;
  32542. + if (unlikely(err))
  32543. + goto out;
  32544. + idmap = mnt_idmap(path->mnt);
  32545. +
  32546. + lockdep_off();
  32547. + err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
  32548. + lockdep_on();
  32549. + if (!err) {
  32550. + struct path tmp = *path;
  32551. + int did;
  32552. +
  32553. + /* fuse has different memory inode for the same inumber */
  32554. + vfsub_update_h_iattr(&tmp, &did);
  32555. + if (did) {
  32556. + tmp.dentry = path->dentry->d_parent;
  32557. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32558. + tmp.dentry = src_dentry;
  32559. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32560. + }
  32561. + /*ignore*/
  32562. + }
  32563. +
  32564. +out:
  32565. + return err;
  32566. +}
  32567. +
  32568. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  32569. + struct inode *dir, struct path *path,
  32570. + struct inode **delegated_inode, unsigned int flags)
  32571. +{
  32572. + int err;
  32573. + struct renamedata rd;
  32574. + struct path tmp = {
  32575. + .mnt = path->mnt
  32576. + };
  32577. + struct dentry *d;
  32578. +
  32579. + IMustLock(dir);
  32580. + IMustLock(src_dir);
  32581. +
  32582. + d = path->dentry;
  32583. + path->dentry = d->d_parent;
  32584. + tmp.dentry = src_dentry->d_parent;
  32585. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  32586. + path->dentry = d;
  32587. + if (unlikely(err))
  32588. + goto out;
  32589. +
  32590. + rd.old_mnt_idmap = mnt_idmap(path->mnt);
  32591. + rd.old_dir = src_dir;
  32592. + rd.old_dentry = src_dentry;
  32593. + rd.new_mnt_idmap = rd.old_mnt_idmap;
  32594. + rd.new_dir = dir;
  32595. + rd.new_dentry = path->dentry;
  32596. + rd.delegated_inode = delegated_inode;
  32597. + rd.flags = flags;
  32598. + lockdep_off();
  32599. + err = vfs_rename(&rd);
  32600. + lockdep_on();
  32601. + if (!err) {
  32602. + int did;
  32603. +
  32604. + tmp.dentry = d->d_parent;
  32605. + vfsub_update_h_iattr(&tmp, &did);
  32606. + if (did) {
  32607. + tmp.dentry = src_dentry;
  32608. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32609. + tmp.dentry = src_dentry->d_parent;
  32610. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32611. + }
  32612. + /*ignore*/
  32613. + }
  32614. +
  32615. +out:
  32616. + return err;
  32617. +}
  32618. +
  32619. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  32620. +{
  32621. + int err;
  32622. + struct dentry *d;
  32623. + struct mnt_idmap *idmap;
  32624. +
  32625. + IMustLock(dir);
  32626. +
  32627. + d = path->dentry;
  32628. + path->dentry = d->d_parent;
  32629. + err = security_path_mkdir(path, d, mode);
  32630. + path->dentry = d;
  32631. + if (unlikely(err))
  32632. + goto out;
  32633. + idmap = mnt_idmap(path->mnt);
  32634. +
  32635. + lockdep_off();
  32636. + err = vfs_mkdir(idmap, dir, path->dentry, mode);
  32637. + lockdep_on();
  32638. + if (!err) {
  32639. + struct path tmp = *path;
  32640. + int did;
  32641. +
  32642. + vfsub_update_h_iattr(&tmp, &did);
  32643. + if (did) {
  32644. + tmp.dentry = path->dentry->d_parent;
  32645. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32646. + }
  32647. + /*ignore*/
  32648. + }
  32649. +
  32650. +out:
  32651. + return err;
  32652. +}
  32653. +
  32654. +int vfsub_rmdir(struct inode *dir, struct path *path)
  32655. +{
  32656. + int err;
  32657. + struct dentry *d;
  32658. + struct mnt_idmap *idmap;
  32659. +
  32660. + IMustLock(dir);
  32661. +
  32662. + d = path->dentry;
  32663. + path->dentry = d->d_parent;
  32664. + err = security_path_rmdir(path, d);
  32665. + path->dentry = d;
  32666. + if (unlikely(err))
  32667. + goto out;
  32668. + idmap = mnt_idmap(path->mnt);
  32669. +
  32670. + lockdep_off();
  32671. + err = vfs_rmdir(idmap, dir, path->dentry);
  32672. + lockdep_on();
  32673. + if (!err) {
  32674. + struct path tmp = {
  32675. + .dentry = path->dentry->d_parent,
  32676. + .mnt = path->mnt
  32677. + };
  32678. +
  32679. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32680. + }
  32681. +
  32682. +out:
  32683. + return err;
  32684. +}
  32685. +
  32686. +/* ---------------------------------------------------------------------- */
  32687. +
  32688. +/* todo: support mmap_sem? */
  32689. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32690. + loff_t *ppos)
  32691. +{
  32692. + ssize_t err;
  32693. +
  32694. + lockdep_off();
  32695. + err = vfs_read(file, ubuf, count, ppos);
  32696. + lockdep_on();
  32697. + if (err >= 0)
  32698. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32699. + return err;
  32700. +}
  32701. +
  32702. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32703. + loff_t *ppos)
  32704. +{
  32705. + ssize_t err;
  32706. +
  32707. + lockdep_off();
  32708. + err = kernel_read(file, kbuf, count, ppos);
  32709. + lockdep_on();
  32710. + AuTraceErr(err);
  32711. + if (err >= 0)
  32712. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32713. + return err;
  32714. +}
  32715. +
  32716. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32717. + loff_t *ppos)
  32718. +{
  32719. + ssize_t err;
  32720. +
  32721. + lockdep_off();
  32722. + err = vfs_write(file, ubuf, count, ppos);
  32723. + lockdep_on();
  32724. + if (err >= 0)
  32725. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32726. + return err;
  32727. +}
  32728. +
  32729. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  32730. +{
  32731. + ssize_t err;
  32732. +
  32733. + lockdep_off();
  32734. + err = kernel_write(file, kbuf, count, ppos);
  32735. + lockdep_on();
  32736. + if (err >= 0)
  32737. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32738. + return err;
  32739. +}
  32740. +
  32741. +int vfsub_flush(struct file *file, fl_owner_t id)
  32742. +{
  32743. + int err;
  32744. +
  32745. + err = 0;
  32746. + if (file->f_op->flush) {
  32747. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  32748. + err = file->f_op->flush(file, id);
  32749. + else {
  32750. + lockdep_off();
  32751. + err = file->f_op->flush(file, id);
  32752. + lockdep_on();
  32753. + }
  32754. + if (!err)
  32755. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  32756. + /*ignore*/
  32757. + }
  32758. + return err;
  32759. +}
  32760. +
  32761. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  32762. +{
  32763. + int err;
  32764. +
  32765. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  32766. +
  32767. + lockdep_off();
  32768. + err = iterate_dir(file, ctx);
  32769. + lockdep_on();
  32770. + if (err >= 0)
  32771. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32772. +
  32773. + return err;
  32774. +}
  32775. +
  32776. +long vfsub_splice_read(struct file *in, loff_t *ppos,
  32777. + struct pipe_inode_info *pipe, size_t len,
  32778. + unsigned int flags)
  32779. +{
  32780. + long err;
  32781. +
  32782. + lockdep_off();
  32783. + err = vfs_splice_read(in, ppos, pipe, len, flags);
  32784. + lockdep_on();
  32785. + file_accessed(in);
  32786. + if (err >= 0)
  32787. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  32788. + return err;
  32789. +}
  32790. +
  32791. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32792. + loff_t *ppos, size_t len, unsigned int flags)
  32793. +{
  32794. + long err;
  32795. +
  32796. + lockdep_off();
  32797. + err = do_splice_from(pipe, out, ppos, len, flags);
  32798. + lockdep_on();
  32799. + if (err >= 0)
  32800. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  32801. + return err;
  32802. +}
  32803. +
  32804. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  32805. +{
  32806. + int err;
  32807. +
  32808. + /* file can be NULL */
  32809. + lockdep_off();
  32810. + err = vfs_fsync(file, datasync);
  32811. + lockdep_on();
  32812. + if (!err) {
  32813. + if (!path) {
  32814. + AuDebugOn(!file);
  32815. + path = &file->f_path;
  32816. + }
  32817. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32818. + }
  32819. + return err;
  32820. +}
  32821. +
  32822. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  32823. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32824. + struct file *h_file)
  32825. +{
  32826. + int err;
  32827. + struct inode *h_inode;
  32828. + struct super_block *h_sb;
  32829. + struct mnt_idmap *h_idmap;
  32830. +
  32831. + if (!h_file) {
  32832. + err = vfsub_truncate(h_path, length);
  32833. + goto out;
  32834. + }
  32835. +
  32836. + h_inode = d_inode(h_path->dentry);
  32837. + h_sb = h_inode->i_sb;
  32838. + lockdep_off();
  32839. + sb_start_write(h_sb);
  32840. + lockdep_on();
  32841. + err = security_file_truncate(h_file);
  32842. + if (!err) {
  32843. + h_idmap = mnt_idmap(h_path->mnt);
  32844. + lockdep_off();
  32845. + err = do_truncate(h_idmap, h_path->dentry, length, attr,
  32846. + h_file);
  32847. + lockdep_on();
  32848. + }
  32849. + lockdep_off();
  32850. + sb_end_write(h_sb);
  32851. + lockdep_on();
  32852. +
  32853. +out:
  32854. + return err;
  32855. +}
  32856. +
  32857. +/* ---------------------------------------------------------------------- */
  32858. +
  32859. +struct au_vfsub_mkdir_args {
  32860. + int *errp;
  32861. + struct inode *dir;
  32862. + struct path *path;
  32863. + int mode;
  32864. +};
  32865. +
  32866. +static void au_call_vfsub_mkdir(void *args)
  32867. +{
  32868. + struct au_vfsub_mkdir_args *a = args;
  32869. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  32870. +}
  32871. +
  32872. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  32873. +{
  32874. + int err, do_sio, wkq_err;
  32875. + struct mnt_idmap *idmap;
  32876. +
  32877. + idmap = mnt_idmap(path->mnt);
  32878. + do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
  32879. + if (!do_sio) {
  32880. + lockdep_off();
  32881. + err = vfsub_mkdir(dir, path, mode);
  32882. + lockdep_on();
  32883. + } else {
  32884. + struct au_vfsub_mkdir_args args = {
  32885. + .errp = &err,
  32886. + .dir = dir,
  32887. + .path = path,
  32888. + .mode = mode
  32889. + };
  32890. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  32891. + if (unlikely(wkq_err))
  32892. + err = wkq_err;
  32893. + }
  32894. +
  32895. + return err;
  32896. +}
  32897. +
  32898. +struct au_vfsub_rmdir_args {
  32899. + int *errp;
  32900. + struct inode *dir;
  32901. + struct path *path;
  32902. +};
  32903. +
  32904. +static void au_call_vfsub_rmdir(void *args)
  32905. +{
  32906. + struct au_vfsub_rmdir_args *a = args;
  32907. + *a->errp = vfsub_rmdir(a->dir, a->path);
  32908. +}
  32909. +
  32910. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  32911. +{
  32912. + int err, do_sio, wkq_err;
  32913. + struct mnt_idmap *idmap;
  32914. +
  32915. + idmap = mnt_idmap(path->mnt);
  32916. + do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
  32917. + if (!do_sio) {
  32918. + lockdep_off();
  32919. + err = vfsub_rmdir(dir, path);
  32920. + lockdep_on();
  32921. + } else {
  32922. + struct au_vfsub_rmdir_args args = {
  32923. + .errp = &err,
  32924. + .dir = dir,
  32925. + .path = path
  32926. + };
  32927. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  32928. + if (unlikely(wkq_err))
  32929. + err = wkq_err;
  32930. + }
  32931. +
  32932. + return err;
  32933. +}
  32934. +
  32935. +/* ---------------------------------------------------------------------- */
  32936. +
  32937. +struct notify_change_args {
  32938. + int *errp;
  32939. + struct path *path;
  32940. + struct iattr *ia;
  32941. + struct inode **delegated_inode;
  32942. +};
  32943. +
  32944. +static void call_notify_change(void *args)
  32945. +{
  32946. + struct notify_change_args *a = args;
  32947. + struct inode *h_inode;
  32948. + struct mnt_idmap *idmap;
  32949. +
  32950. + h_inode = d_inode(a->path->dentry);
  32951. + IMustLock(h_inode);
  32952. +
  32953. + *a->errp = -EPERM;
  32954. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  32955. + idmap = mnt_idmap(a->path->mnt);
  32956. + lockdep_off();
  32957. + *a->errp = notify_change(idmap, a->path->dentry, a->ia,
  32958. + a->delegated_inode);
  32959. + lockdep_on();
  32960. + if (!*a->errp)
  32961. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  32962. + }
  32963. + AuTraceErr(*a->errp);
  32964. +}
  32965. +
  32966. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32967. + struct inode **delegated_inode)
  32968. +{
  32969. + int err;
  32970. + struct notify_change_args args = {
  32971. + .errp = &err,
  32972. + .path = path,
  32973. + .ia = ia,
  32974. + .delegated_inode = delegated_inode
  32975. + };
  32976. +
  32977. + call_notify_change(&args);
  32978. +
  32979. + return err;
  32980. +}
  32981. +
  32982. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32983. + struct inode **delegated_inode)
  32984. +{
  32985. + int err, wkq_err;
  32986. + struct notify_change_args args = {
  32987. + .errp = &err,
  32988. + .path = path,
  32989. + .ia = ia,
  32990. + .delegated_inode = delegated_inode
  32991. + };
  32992. +
  32993. + wkq_err = au_wkq_wait(call_notify_change, &args);
  32994. + if (unlikely(wkq_err))
  32995. + err = wkq_err;
  32996. +
  32997. + return err;
  32998. +}
  32999. +
  33000. +/* ---------------------------------------------------------------------- */
  33001. +
  33002. +struct unlink_args {
  33003. + int *errp;
  33004. + struct inode *dir;
  33005. + struct path *path;
  33006. + struct inode **delegated_inode;
  33007. +};
  33008. +
  33009. +static void call_unlink(void *args)
  33010. +{
  33011. + struct unlink_args *a = args;
  33012. + struct dentry *d = a->path->dentry;
  33013. + struct inode *h_inode;
  33014. + struct mnt_idmap *idmap;
  33015. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  33016. + && au_dcount(d) == 1);
  33017. +
  33018. + IMustLock(a->dir);
  33019. +
  33020. + a->path->dentry = d->d_parent;
  33021. + *a->errp = security_path_unlink(a->path, d);
  33022. + a->path->dentry = d;
  33023. + if (unlikely(*a->errp))
  33024. + return;
  33025. +
  33026. + if (!stop_sillyrename)
  33027. + dget(d);
  33028. + h_inode = NULL;
  33029. + if (d_is_positive(d)) {
  33030. + h_inode = d_inode(d);
  33031. + ihold(h_inode);
  33032. + }
  33033. +
  33034. + idmap = mnt_idmap(a->path->mnt);
  33035. + lockdep_off();
  33036. + *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
  33037. + lockdep_on();
  33038. + if (!*a->errp) {
  33039. + struct path tmp = {
  33040. + .dentry = d->d_parent,
  33041. + .mnt = a->path->mnt
  33042. + };
  33043. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  33044. + }
  33045. +
  33046. + if (!stop_sillyrename)
  33047. + dput(d);
  33048. + if (h_inode)
  33049. + iput(h_inode);
  33050. +
  33051. + AuTraceErr(*a->errp);
  33052. +}
  33053. +
  33054. +/*
  33055. + * @dir: must be locked.
  33056. + * @dentry: target dentry.
  33057. + */
  33058. +int vfsub_unlink(struct inode *dir, struct path *path,
  33059. + struct inode **delegated_inode, int force)
  33060. +{
  33061. + int err;
  33062. + struct unlink_args args = {
  33063. + .errp = &err,
  33064. + .dir = dir,
  33065. + .path = path,
  33066. + .delegated_inode = delegated_inode
  33067. + };
  33068. +
  33069. + if (!force)
  33070. + call_unlink(&args);
  33071. + else {
  33072. + int wkq_err;
  33073. +
  33074. + wkq_err = au_wkq_wait(call_unlink, &args);
  33075. + if (unlikely(wkq_err))
  33076. + err = wkq_err;
  33077. + }
  33078. +
  33079. + return err;
  33080. +}
  33081. diff -Naur null/fs/aufs/vfsub.h linux-6.6.63/fs/aufs/vfsub.h
  33082. --- /dev/null
  33083. +++ linux-6.6.63/fs/aufs/vfsub.h 2024-12-18 15:12:59.300519526 +0300
  33084. @@ -0,0 +1,442 @@
  33085. +/* SPDX-License-Identifier: GPL-2.0 */
  33086. +/*
  33087. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  33088. + *
  33089. + * This program is free software; you can redistribute it and/or modify
  33090. + * it under the terms of the GNU General Public License as published by
  33091. + * the Free Software Foundation; either version 2 of the License, or
  33092. + * (at your option) any later version.
  33093. + *
  33094. + * This program is distributed in the hope that it will be useful,
  33095. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33096. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33097. + * GNU General Public License for more details.
  33098. + *
  33099. + * You should have received a copy of the GNU General Public License
  33100. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33101. + */
  33102. +
  33103. +/*
  33104. + * sub-routines for VFS
  33105. + */
  33106. +
  33107. +#ifndef __AUFS_VFSUB_H__
  33108. +#define __AUFS_VFSUB_H__
  33109. +
  33110. +#ifdef __KERNEL__
  33111. +
  33112. +#include <linux/fs.h>
  33113. +#include <linux/mount.h>
  33114. +#include <linux/posix_acl.h>
  33115. +#include <linux/xattr.h>
  33116. +#include "debug.h"
  33117. +#include "fstype.h"
  33118. +
  33119. +/* copied from linux/fs/internal.h */
  33120. +/* todo: BAD approach!! */
  33121. +extern void __mnt_drop_write(struct vfsmount *);
  33122. +extern struct file *alloc_empty_file(int, const struct cred *);
  33123. +
  33124. +/* ---------------------------------------------------------------------- */
  33125. +
  33126. +/* lock subclass for lower inode */
  33127. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  33128. +/* reduce? gave up. */
  33129. +enum {
  33130. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  33131. + AuLsc_I_PARENT, /* lower inode, parent first */
  33132. + AuLsc_I_PARENT2, /* copyup dirs */
  33133. + AuLsc_I_PARENT3, /* copyup wh */
  33134. + AuLsc_I_CHILD,
  33135. + AuLsc_I_CHILD2,
  33136. + AuLsc_I_End
  33137. +};
  33138. +
  33139. +/* to debug easier, do not make them inlined functions */
  33140. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  33141. +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
  33142. +
  33143. +/* ---------------------------------------------------------------------- */
  33144. +
  33145. +unsigned int vfsub_inode_nlink_aufs(struct inode *inode);
  33146. +
  33147. +enum au_inode_type {
  33148. + AU_I_AUFS,
  33149. + AU_I_BRANCH,
  33150. + AU_I_UNKNOWN
  33151. +};
  33152. +
  33153. +static inline unsigned int vfsub_inode_nlink(struct inode *inode,
  33154. + enum au_inode_type type)
  33155. +{
  33156. + unsigned int nlink;
  33157. +
  33158. + switch (type) {
  33159. + case AU_I_AUFS:
  33160. + nlink = vfsub_inode_nlink_aufs(inode);
  33161. + break;
  33162. + case AU_I_BRANCH: /* aufs cannot be a branch of another aufs mount */
  33163. + AuDebugOn(au_test_aufs(inode->i_sb));
  33164. + nlink = inode->i_nlink;
  33165. + break;
  33166. + case AU_I_UNKNOWN:
  33167. + if (au_test_aufs(inode->i_sb))
  33168. + nlink = vfsub_inode_nlink_aufs(inode);
  33169. + else
  33170. + nlink = inode->i_nlink;
  33171. + break;
  33172. + };
  33173. +
  33174. + return nlink;
  33175. +}
  33176. +
  33177. +void vfsub_inc_nlink(struct inode *inode);
  33178. +void vfsub_drop_nlink(struct inode *inode);
  33179. +void vfsub_clear_nlink(struct inode *inode);
  33180. +void vfsub_set_nlink(struct inode *inode, unsigned int nlink);
  33181. +
  33182. +static inline void vfsub_inode_nlink_init(struct inode *inode,
  33183. + unsigned int nlink)
  33184. +{
  33185. + /* to ignore sb->s_remove_count, do not use set_nlink() */
  33186. + inode->__i_nlink = nlink;
  33187. +}
  33188. +
  33189. +static inline void vfsub_dead_dir(struct inode *inode)
  33190. +{
  33191. + AuDebugOn(!S_ISDIR(inode->i_mode));
  33192. + inode->i_flags |= S_DEAD;
  33193. + vfsub_clear_nlink(inode);
  33194. +}
  33195. +
  33196. +static inline int vfsub_native_ro(struct inode *inode)
  33197. +{
  33198. + return sb_rdonly(inode->i_sb)
  33199. + || IS_RDONLY(inode)
  33200. + /* || IS_APPEND(inode) */
  33201. + || IS_IMMUTABLE(inode);
  33202. +}
  33203. +
  33204. +#ifdef CONFIG_AUFS_BR_FUSE
  33205. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  33206. +#else
  33207. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  33208. +#endif
  33209. +
  33210. +int vfsub_sync_filesystem(struct super_block *h_sb);
  33211. +
  33212. +/* ---------------------------------------------------------------------- */
  33213. +
  33214. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  33215. +struct file *vfsub_dentry_open(struct path *path, int flags);
  33216. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  33217. +struct au_branch;
  33218. +struct vfsub_aopen_args {
  33219. + struct file *file;
  33220. + unsigned int open_flag;
  33221. + umode_t create_mode;
  33222. + struct au_branch *br;
  33223. +};
  33224. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  33225. + struct vfsub_aopen_args *args);
  33226. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  33227. +
  33228. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  33229. + struct path *ppath, int len);
  33230. +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
  33231. + int len);
  33232. +
  33233. +struct vfsub_lkup_one_args {
  33234. + struct dentry **errp;
  33235. + struct qstr *name;
  33236. + struct path *ppath;
  33237. +};
  33238. +
  33239. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  33240. + struct path *ppath)
  33241. +{
  33242. + return vfsub_lookup_one_len(name->name, ppath, name->len);
  33243. +}
  33244. +
  33245. +void vfsub_call_lkup_one(void *args);
  33246. +
  33247. +/* ---------------------------------------------------------------------- */
  33248. +
  33249. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  33250. +{
  33251. + int err;
  33252. +
  33253. + lockdep_off();
  33254. + err = mnt_want_write(mnt);
  33255. + lockdep_on();
  33256. + return err;
  33257. +}
  33258. +
  33259. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  33260. +{
  33261. + lockdep_off();
  33262. + mnt_drop_write(mnt);
  33263. + lockdep_on();
  33264. +}
  33265. +
  33266. +#if 0 /* reserved */
  33267. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  33268. +{
  33269. + lockdep_off();
  33270. + mnt_drop_write_file(file);
  33271. + lockdep_on();
  33272. +}
  33273. +#endif
  33274. +
  33275. +static inline void vfsub_file_start_write(struct file *file)
  33276. +{
  33277. + lockdep_off();
  33278. + file_start_write(file);
  33279. + lockdep_on();
  33280. +}
  33281. +
  33282. +static inline void vfsub_file_end_write(struct file *file)
  33283. +{
  33284. + lockdep_off();
  33285. + file_end_write(file);
  33286. + lockdep_on();
  33287. +}
  33288. +
  33289. +/* ---------------------------------------------------------------------- */
  33290. +
  33291. +struct au_hinode;
  33292. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  33293. + struct dentry *d2, struct au_hinode *hdir2);
  33294. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  33295. + struct dentry *d2, struct au_hinode *hdir2);
  33296. +
  33297. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  33298. + bool want_excl);
  33299. +int vfsub_symlink(struct inode *dir, struct path *path,
  33300. + const char *symname);
  33301. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  33302. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  33303. + struct path *path, struct inode **delegated_inode);
  33304. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  33305. + struct inode *hdir, struct path *path,
  33306. + struct inode **delegated_inode, unsigned int flags);
  33307. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  33308. +int vfsub_rmdir(struct inode *dir, struct path *path);
  33309. +
  33310. +/* ---------------------------------------------------------------------- */
  33311. +
  33312. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  33313. + loff_t *ppos);
  33314. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  33315. + loff_t *ppos);
  33316. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  33317. + loff_t *ppos);
  33318. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  33319. + loff_t *ppos);
  33320. +int vfsub_flush(struct file *file, fl_owner_t id);
  33321. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  33322. +
  33323. +static inline loff_t vfsub_f_size_read(struct file *file)
  33324. +{
  33325. + return i_size_read(file_inode(file));
  33326. +}
  33327. +
  33328. +static inline unsigned int vfsub_file_flags(struct file *file)
  33329. +{
  33330. + unsigned int flags;
  33331. +
  33332. + spin_lock(&file->f_lock);
  33333. + flags = file->f_flags;
  33334. + spin_unlock(&file->f_lock);
  33335. +
  33336. + return flags;
  33337. +}
  33338. +
  33339. +static inline int vfsub_file_execed(struct file *file)
  33340. +{
  33341. + /* todo: direct access f_flags */
  33342. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  33343. +}
  33344. +
  33345. +#if 0 /* reserved */
  33346. +static inline void vfsub_file_accessed(struct file *h_file)
  33347. +{
  33348. + file_accessed(h_file);
  33349. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  33350. +}
  33351. +#endif
  33352. +
  33353. +#if 0 /* reserved */
  33354. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  33355. + struct dentry *h_dentry)
  33356. +{
  33357. + struct path h_path = {
  33358. + .dentry = h_dentry,
  33359. + .mnt = h_mnt
  33360. + };
  33361. + touch_atime(&h_path);
  33362. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  33363. +}
  33364. +#endif
  33365. +
  33366. +static inline int vfsub_update_time(struct inode *h_inode, int flags)
  33367. +{
  33368. + return inode_update_time(h_inode, flags);
  33369. + /* no vfsub_update_h_iattr() since we don't have struct path */
  33370. +}
  33371. +
  33372. +#ifdef CONFIG_FS_POSIX_ACL
  33373. +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
  33374. + struct dentry *h_dentry, umode_t h_mode)
  33375. +{
  33376. + int err;
  33377. +
  33378. + err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
  33379. + if (err == -EOPNOTSUPP)
  33380. + err = 0;
  33381. + return err;
  33382. +}
  33383. +#else
  33384. +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
  33385. + struct dentry *h_dentry, umode_t h_mode);
  33386. +#endif
  33387. +
  33388. +long vfsub_splice_read(struct file *in, loff_t *ppos,
  33389. + struct pipe_inode_info *pipe, size_t len,
  33390. + unsigned int flags);
  33391. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  33392. + loff_t *ppos, size_t len, unsigned int flags);
  33393. +
  33394. +static inline long vfsub_truncate(struct path *path, loff_t length)
  33395. +{
  33396. + long err;
  33397. +
  33398. + lockdep_off();
  33399. + err = vfs_truncate(path, length);
  33400. + lockdep_on();
  33401. + return err;
  33402. +}
  33403. +
  33404. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  33405. + struct file *h_file);
  33406. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  33407. +
  33408. +/*
  33409. + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
  33410. + * ioctl.
  33411. + */
  33412. +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
  33413. + loff_t len)
  33414. +{
  33415. + loff_t err;
  33416. +
  33417. + lockdep_off();
  33418. + err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
  33419. + lockdep_on();
  33420. +
  33421. + return err;
  33422. +}
  33423. +
  33424. +/* copy_file_range(2) is a systemcall */
  33425. +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
  33426. + struct file *dst, loff_t dst_pos,
  33427. + size_t len, unsigned int flags)
  33428. +{
  33429. + ssize_t ssz;
  33430. +
  33431. + lockdep_off();
  33432. + ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
  33433. + lockdep_on();
  33434. +
  33435. + return ssz;
  33436. +}
  33437. +
  33438. +/* ---------------------------------------------------------------------- */
  33439. +
  33440. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  33441. +{
  33442. + loff_t err;
  33443. +
  33444. + lockdep_off();
  33445. + err = vfs_llseek(file, offset, origin);
  33446. + lockdep_on();
  33447. + return err;
  33448. +}
  33449. +
  33450. +/* ---------------------------------------------------------------------- */
  33451. +
  33452. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  33453. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  33454. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  33455. + struct inode **delegated_inode);
  33456. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  33457. + struct inode **delegated_inode);
  33458. +int vfsub_unlink(struct inode *dir, struct path *path,
  33459. + struct inode **delegated_inode, int force);
  33460. +
  33461. +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
  33462. +{
  33463. + return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  33464. +}
  33465. +
  33466. +/* ---------------------------------------------------------------------- */
  33467. +
  33468. +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
  33469. + struct dentry *dentry, const char *name,
  33470. + const void *value, size_t size, int flags)
  33471. +{
  33472. + int err;
  33473. +
  33474. + lockdep_off();
  33475. + err = vfs_setxattr(idmap, dentry, name, value, size, flags);
  33476. + lockdep_on();
  33477. +
  33478. + return err;
  33479. +}
  33480. +
  33481. +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
  33482. + struct dentry *dentry, const char *name)
  33483. +{
  33484. + int err;
  33485. +
  33486. + lockdep_off();
  33487. + err = vfs_removexattr(idmap, dentry, name);
  33488. + lockdep_on();
  33489. +
  33490. + return err;
  33491. +}
  33492. +
  33493. +#ifdef CONFIG_FS_POSIX_ACL
  33494. +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
  33495. + struct dentry *dentry, const char *name,
  33496. + struct posix_acl *acl)
  33497. +{
  33498. + int err;
  33499. +
  33500. + lockdep_off();
  33501. + err = vfs_set_acl(idmap, dentry, name, acl);
  33502. + lockdep_on();
  33503. +
  33504. + return err;
  33505. +}
  33506. +
  33507. +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
  33508. + struct dentry *dentry, const char *name)
  33509. +{
  33510. + int err;
  33511. +
  33512. + lockdep_off();
  33513. + err = vfs_remove_acl(idmap, dentry, name);
  33514. + lockdep_on();
  33515. +
  33516. + return err;
  33517. +}
  33518. +#else
  33519. +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
  33520. + const char *name, struct posix_acl *acl);
  33521. +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
  33522. + struct dentry *dentry, const char *name);
  33523. +#endif
  33524. +
  33525. +#endif /* __KERNEL__ */
  33526. +#endif /* __AUFS_VFSUB_H__ */
  33527. diff -Naur null/fs/aufs/wbr_policy.c linux-6.6.63/fs/aufs/wbr_policy.c
  33528. --- /dev/null
  33529. +++ linux-6.6.63/fs/aufs/wbr_policy.c 2024-12-18 15:12:59.300519526 +0300
  33530. @@ -0,0 +1,830 @@
  33531. +// SPDX-License-Identifier: GPL-2.0
  33532. +/*
  33533. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  33534. + *
  33535. + * This program is free software; you can redistribute it and/or modify
  33536. + * it under the terms of the GNU General Public License as published by
  33537. + * the Free Software Foundation; either version 2 of the License, or
  33538. + * (at your option) any later version.
  33539. + *
  33540. + * This program is distributed in the hope that it will be useful,
  33541. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33542. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33543. + * GNU General Public License for more details.
  33544. + *
  33545. + * You should have received a copy of the GNU General Public License
  33546. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33547. + */
  33548. +
  33549. +/*
  33550. + * policies for selecting one among multiple writable branches
  33551. + */
  33552. +
  33553. +#include <linux/statfs.h>
  33554. +#include "aufs.h"
  33555. +
  33556. +/* subset of cpup_attr() */
  33557. +static noinline_for_stack
  33558. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  33559. +{
  33560. + int err, sbits;
  33561. + struct iattr ia;
  33562. + struct inode *h_isrc;
  33563. +
  33564. + h_isrc = d_inode(h_src);
  33565. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  33566. + ia.ia_mode = h_isrc->i_mode;
  33567. + ia.ia_uid = h_isrc->i_uid;
  33568. + ia.ia_gid = h_isrc->i_gid;
  33569. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  33570. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  33571. + /* no delegation since it is just created */
  33572. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  33573. +
  33574. + /* is this nfs only? */
  33575. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  33576. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  33577. + ia.ia_mode = h_isrc->i_mode;
  33578. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  33579. + }
  33580. +
  33581. + return err;
  33582. +}
  33583. +
  33584. +#define AuCpdown_PARENT_OPQ 1
  33585. +#define AuCpdown_WHED (1 << 1)
  33586. +#define AuCpdown_MADE_DIR (1 << 2)
  33587. +#define AuCpdown_DIROPQ (1 << 3)
  33588. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  33589. +#define au_fset_cpdown(flags, name) \
  33590. + do { (flags) |= AuCpdown_##name; } while (0)
  33591. +#define au_fclr_cpdown(flags, name) \
  33592. + do { (flags) &= ~AuCpdown_##name; } while (0)
  33593. +
  33594. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  33595. + unsigned int *flags)
  33596. +{
  33597. + int err;
  33598. + struct dentry *opq_dentry;
  33599. +
  33600. + opq_dentry = au_diropq_create(dentry, bdst);
  33601. + err = PTR_ERR(opq_dentry);
  33602. + if (IS_ERR(opq_dentry))
  33603. + goto out;
  33604. + dput(opq_dentry);
  33605. + au_fset_cpdown(*flags, DIROPQ);
  33606. +
  33607. +out:
  33608. + return err;
  33609. +}
  33610. +
  33611. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  33612. + struct inode *dir, aufs_bindex_t bdst)
  33613. +{
  33614. + int err;
  33615. + struct path h_path;
  33616. + struct au_branch *br;
  33617. +
  33618. + br = au_sbr(dentry->d_sb, bdst);
  33619. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  33620. + err = PTR_ERR(h_path.dentry);
  33621. + if (IS_ERR(h_path.dentry))
  33622. + goto out;
  33623. +
  33624. + err = 0;
  33625. + if (d_is_positive(h_path.dentry)) {
  33626. + h_path.mnt = au_br_mnt(br);
  33627. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  33628. + dentry);
  33629. + }
  33630. + dput(h_path.dentry);
  33631. +
  33632. +out:
  33633. + return err;
  33634. +}
  33635. +
  33636. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  33637. + struct au_pin *pin,
  33638. + struct dentry *h_parent, void *arg)
  33639. +{
  33640. + int err, rerr;
  33641. + aufs_bindex_t bopq, btop;
  33642. + struct path h_path;
  33643. + struct dentry *parent;
  33644. + struct inode *h_dir, *h_inode, *inode, *dir;
  33645. + unsigned int *flags = arg;
  33646. +
  33647. + btop = au_dbtop(dentry);
  33648. + /* dentry is di-locked */
  33649. + parent = dget_parent(dentry);
  33650. + dir = d_inode(parent);
  33651. + h_dir = d_inode(h_parent);
  33652. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  33653. + IMustLock(h_dir);
  33654. +
  33655. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  33656. + if (unlikely(err < 0))
  33657. + goto out;
  33658. + h_path.dentry = au_h_dptr(dentry, bdst);
  33659. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  33660. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
  33661. + if (unlikely(err))
  33662. + goto out_put;
  33663. + au_fset_cpdown(*flags, MADE_DIR);
  33664. +
  33665. + bopq = au_dbdiropq(dentry);
  33666. + au_fclr_cpdown(*flags, WHED);
  33667. + au_fclr_cpdown(*flags, DIROPQ);
  33668. + if (au_dbwh(dentry) == bdst)
  33669. + au_fset_cpdown(*flags, WHED);
  33670. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  33671. + au_fset_cpdown(*flags, PARENT_OPQ);
  33672. + h_inode = d_inode(h_path.dentry);
  33673. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33674. + if (au_ftest_cpdown(*flags, WHED)) {
  33675. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  33676. + if (unlikely(err)) {
  33677. + inode_unlock(h_inode);
  33678. + goto out_dir;
  33679. + }
  33680. + }
  33681. +
  33682. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  33683. + inode_unlock(h_inode);
  33684. + if (unlikely(err))
  33685. + goto out_opq;
  33686. +
  33687. + if (au_ftest_cpdown(*flags, WHED)) {
  33688. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  33689. + if (unlikely(err))
  33690. + goto out_opq;
  33691. + }
  33692. +
  33693. + inode = d_inode(dentry);
  33694. + if (au_ibbot(inode) < bdst)
  33695. + au_set_ibbot(inode, bdst);
  33696. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  33697. + au_hi_flags(inode, /*isdir*/1));
  33698. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  33699. + goto out; /* success */
  33700. +
  33701. + /* revert */
  33702. +out_opq:
  33703. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  33704. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33705. + rerr = au_diropq_remove(dentry, bdst);
  33706. + inode_unlock(h_inode);
  33707. + if (unlikely(rerr)) {
  33708. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  33709. + dentry, bdst, rerr);
  33710. + err = -EIO;
  33711. + goto out;
  33712. + }
  33713. + }
  33714. +out_dir:
  33715. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  33716. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  33717. + if (unlikely(rerr)) {
  33718. + AuIOErr("failed removing %pd b%d (%d)\n",
  33719. + dentry, bdst, rerr);
  33720. + err = -EIO;
  33721. + }
  33722. + }
  33723. +out_put:
  33724. + au_set_h_dptr(dentry, bdst, NULL);
  33725. + if (au_dbbot(dentry) == bdst)
  33726. + au_update_dbbot(dentry);
  33727. +out:
  33728. + dput(parent);
  33729. + return err;
  33730. +}
  33731. +
  33732. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  33733. +{
  33734. + int err;
  33735. + unsigned int flags;
  33736. +
  33737. + flags = 0;
  33738. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  33739. +
  33740. + return err;
  33741. +}
  33742. +
  33743. +/* ---------------------------------------------------------------------- */
  33744. +
  33745. +/* policies for create */
  33746. +
  33747. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  33748. +{
  33749. + int err, i, j, ndentry;
  33750. + aufs_bindex_t bopq;
  33751. + struct au_dcsub_pages dpages;
  33752. + struct au_dpage *dpage;
  33753. + struct dentry **dentries, *parent, *d;
  33754. +
  33755. + err = au_dpages_init(&dpages, GFP_NOFS);
  33756. + if (unlikely(err))
  33757. + goto out;
  33758. + parent = dget_parent(dentry);
  33759. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  33760. + if (unlikely(err))
  33761. + goto out_free;
  33762. +
  33763. + err = bindex;
  33764. + for (i = 0; i < dpages.ndpage; i++) {
  33765. + dpage = dpages.dpages + i;
  33766. + dentries = dpage->dentries;
  33767. + ndentry = dpage->ndentry;
  33768. + for (j = 0; j < ndentry; j++) {
  33769. + d = dentries[j];
  33770. + di_read_lock_parent2(d, !AuLock_IR);
  33771. + bopq = au_dbdiropq(d);
  33772. + di_read_unlock(d, !AuLock_IR);
  33773. + if (bopq >= 0 && bopq < err)
  33774. + err = bopq;
  33775. + }
  33776. + }
  33777. +
  33778. +out_free:
  33779. + dput(parent);
  33780. + au_dpages_free(&dpages);
  33781. +out:
  33782. + return err;
  33783. +}
  33784. +
  33785. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  33786. +{
  33787. + for (; bindex >= 0; bindex--)
  33788. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  33789. + return bindex;
  33790. + return -EROFS;
  33791. +}
  33792. +
  33793. +/* top down parent */
  33794. +static int au_wbr_create_tdp(struct dentry *dentry,
  33795. + unsigned int flags __maybe_unused)
  33796. +{
  33797. + int err;
  33798. + aufs_bindex_t btop, bindex;
  33799. + struct super_block *sb;
  33800. + struct dentry *parent, *h_parent;
  33801. +
  33802. + sb = dentry->d_sb;
  33803. + btop = au_dbtop(dentry);
  33804. + err = btop;
  33805. + if (!au_br_rdonly(au_sbr(sb, btop)))
  33806. + goto out;
  33807. +
  33808. + err = -EROFS;
  33809. + parent = dget_parent(dentry);
  33810. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  33811. + h_parent = au_h_dptr(parent, bindex);
  33812. + if (!h_parent || d_is_negative(h_parent))
  33813. + continue;
  33814. +
  33815. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33816. + err = bindex;
  33817. + break;
  33818. + }
  33819. + }
  33820. + dput(parent);
  33821. +
  33822. + /* bottom up here */
  33823. + if (unlikely(err < 0)) {
  33824. + err = au_wbr_bu(sb, btop - 1);
  33825. + if (err >= 0)
  33826. + err = au_wbr_nonopq(dentry, err);
  33827. + }
  33828. +
  33829. +out:
  33830. + AuDbg("b%d\n", err);
  33831. + return err;
  33832. +}
  33833. +
  33834. +/* ---------------------------------------------------------------------- */
  33835. +
  33836. +/* an exception for the policy other than tdp */
  33837. +static int au_wbr_create_exp(struct dentry *dentry)
  33838. +{
  33839. + int err;
  33840. + aufs_bindex_t bwh, bdiropq;
  33841. + struct dentry *parent;
  33842. +
  33843. + err = -1;
  33844. + bwh = au_dbwh(dentry);
  33845. + parent = dget_parent(dentry);
  33846. + bdiropq = au_dbdiropq(parent);
  33847. + if (bwh >= 0) {
  33848. + if (bdiropq >= 0)
  33849. + err = min(bdiropq, bwh);
  33850. + else
  33851. + err = bwh;
  33852. + AuDbg("%d\n", err);
  33853. + } else if (bdiropq >= 0) {
  33854. + err = bdiropq;
  33855. + AuDbg("%d\n", err);
  33856. + }
  33857. + dput(parent);
  33858. +
  33859. + if (err >= 0)
  33860. + err = au_wbr_nonopq(dentry, err);
  33861. +
  33862. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  33863. + err = -1;
  33864. +
  33865. + AuDbg("%d\n", err);
  33866. + return err;
  33867. +}
  33868. +
  33869. +/* ---------------------------------------------------------------------- */
  33870. +
  33871. +/* round robin */
  33872. +static int au_wbr_create_init_rr(struct super_block *sb)
  33873. +{
  33874. + int err;
  33875. +
  33876. + err = au_wbr_bu(sb, au_sbbot(sb));
  33877. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  33878. + /* smp_mb(); */
  33879. +
  33880. + AuDbg("b%d\n", err);
  33881. + return err;
  33882. +}
  33883. +
  33884. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  33885. +{
  33886. + int err, nbr;
  33887. + unsigned int u;
  33888. + aufs_bindex_t bindex, bbot;
  33889. + struct super_block *sb;
  33890. + atomic_t *next;
  33891. +
  33892. + err = au_wbr_create_exp(dentry);
  33893. + if (err >= 0)
  33894. + goto out;
  33895. +
  33896. + sb = dentry->d_sb;
  33897. + next = &au_sbi(sb)->si_wbr_rr_next;
  33898. + bbot = au_sbbot(sb);
  33899. + nbr = bbot + 1;
  33900. + for (bindex = 0; bindex <= bbot; bindex++) {
  33901. + if (!au_ftest_wbr(flags, DIR)) {
  33902. + err = atomic_dec_return(next) + 1;
  33903. + /* modulo for 0 is meaningless */
  33904. + if (unlikely(!err))
  33905. + err = atomic_dec_return(next) + 1;
  33906. + } else
  33907. + err = atomic_read(next);
  33908. + AuDbg("%d\n", err);
  33909. + u = err;
  33910. + err = u % nbr;
  33911. + AuDbg("%d\n", err);
  33912. + if (!au_br_rdonly(au_sbr(sb, err)))
  33913. + break;
  33914. + err = -EROFS;
  33915. + }
  33916. +
  33917. + if (err >= 0)
  33918. + err = au_wbr_nonopq(dentry, err);
  33919. +
  33920. +out:
  33921. + AuDbg("%d\n", err);
  33922. + return err;
  33923. +}
  33924. +
  33925. +/* ---------------------------------------------------------------------- */
  33926. +
  33927. +/* most free space */
  33928. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  33929. +{
  33930. + struct super_block *sb;
  33931. + struct au_branch *br;
  33932. + struct au_wbr_mfs *mfs;
  33933. + struct dentry *h_parent;
  33934. + aufs_bindex_t bindex, bbot;
  33935. + int err;
  33936. + unsigned long long b, bavail;
  33937. + struct path h_path;
  33938. + /* reduce the stack usage */
  33939. + struct kstatfs *st;
  33940. +
  33941. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33942. + if (unlikely(!st)) {
  33943. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  33944. + return;
  33945. + }
  33946. +
  33947. + bavail = 0;
  33948. + sb = dentry->d_sb;
  33949. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33950. + MtxMustLock(&mfs->mfs_lock);
  33951. + mfs->mfs_bindex = -EROFS;
  33952. + mfs->mfsrr_bytes = 0;
  33953. + if (!parent) {
  33954. + bindex = 0;
  33955. + bbot = au_sbbot(sb);
  33956. + } else {
  33957. + bindex = au_dbtop(parent);
  33958. + bbot = au_dbtaildir(parent);
  33959. + }
  33960. +
  33961. + for (; bindex <= bbot; bindex++) {
  33962. + if (parent) {
  33963. + h_parent = au_h_dptr(parent, bindex);
  33964. + if (!h_parent || d_is_negative(h_parent))
  33965. + continue;
  33966. + }
  33967. + br = au_sbr(sb, bindex);
  33968. + if (au_br_rdonly(br))
  33969. + continue;
  33970. +
  33971. + /* sb->s_root for NFS is unreliable */
  33972. + h_path.mnt = au_br_mnt(br);
  33973. + h_path.dentry = h_path.mnt->mnt_root;
  33974. + err = vfs_statfs(&h_path, st);
  33975. + if (unlikely(err)) {
  33976. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  33977. + continue;
  33978. + }
  33979. +
  33980. + /* when the available size is equal, select the lower one */
  33981. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  33982. + || sizeof(b) < sizeof(st->f_bsize));
  33983. + b = st->f_bavail * st->f_bsize;
  33984. + br->br_wbr->wbr_bytes = b;
  33985. + if (b >= bavail) {
  33986. + bavail = b;
  33987. + mfs->mfs_bindex = bindex;
  33988. + mfs->mfs_jiffy = jiffies;
  33989. + }
  33990. + }
  33991. +
  33992. + mfs->mfsrr_bytes = bavail;
  33993. + AuDbg("b%d\n", mfs->mfs_bindex);
  33994. + au_kfree_rcu(st);
  33995. +}
  33996. +
  33997. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  33998. +{
  33999. + int err;
  34000. + struct dentry *parent;
  34001. + struct super_block *sb;
  34002. + struct au_wbr_mfs *mfs;
  34003. +
  34004. + err = au_wbr_create_exp(dentry);
  34005. + if (err >= 0)
  34006. + goto out;
  34007. +
  34008. + sb = dentry->d_sb;
  34009. + parent = NULL;
  34010. + if (au_ftest_wbr(flags, PARENT))
  34011. + parent = dget_parent(dentry);
  34012. + mfs = &au_sbi(sb)->si_wbr_mfs;
  34013. + mutex_lock(&mfs->mfs_lock);
  34014. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  34015. + || mfs->mfs_bindex < 0
  34016. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  34017. + au_mfs(dentry, parent);
  34018. + mutex_unlock(&mfs->mfs_lock);
  34019. + err = mfs->mfs_bindex;
  34020. + dput(parent);
  34021. +
  34022. + if (err >= 0)
  34023. + err = au_wbr_nonopq(dentry, err);
  34024. +
  34025. +out:
  34026. + AuDbg("b%d\n", err);
  34027. + return err;
  34028. +}
  34029. +
  34030. +static int au_wbr_create_init_mfs(struct super_block *sb)
  34031. +{
  34032. + struct au_wbr_mfs *mfs;
  34033. +
  34034. + mfs = &au_sbi(sb)->si_wbr_mfs;
  34035. + mutex_init(&mfs->mfs_lock);
  34036. + mfs->mfs_jiffy = 0;
  34037. + mfs->mfs_bindex = -EROFS;
  34038. +
  34039. + return 0;
  34040. +}
  34041. +
  34042. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  34043. +{
  34044. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  34045. + return 0;
  34046. +}
  34047. +
  34048. +/* ---------------------------------------------------------------------- */
  34049. +
  34050. +/* top down regardless parent, and then mfs */
  34051. +static int au_wbr_create_tdmfs(struct dentry *dentry,
  34052. + unsigned int flags __maybe_unused)
  34053. +{
  34054. + int err;
  34055. + aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
  34056. + unsigned long long watermark;
  34057. + struct super_block *sb;
  34058. + struct au_wbr_mfs *mfs;
  34059. + struct au_branch *br;
  34060. + struct dentry *parent;
  34061. +
  34062. + sb = dentry->d_sb;
  34063. + mfs = &au_sbi(sb)->si_wbr_mfs;
  34064. + mutex_lock(&mfs->mfs_lock);
  34065. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  34066. + || mfs->mfs_bindex < 0)
  34067. + au_mfs(dentry, /*parent*/NULL);
  34068. + watermark = mfs->mfsrr_watermark;
  34069. + bmfs = mfs->mfs_bindex;
  34070. + mutex_unlock(&mfs->mfs_lock);
  34071. +
  34072. + /* another style of au_wbr_create_exp() */
  34073. + bwh = au_dbwh(dentry);
  34074. + parent = dget_parent(dentry);
  34075. + btail = au_dbtaildir(parent);
  34076. + if (bwh >= 0 && bwh < btail)
  34077. + btail = bwh;
  34078. +
  34079. + err = au_wbr_nonopq(dentry, btail);
  34080. + if (unlikely(err < 0))
  34081. + goto out;
  34082. + btail = err;
  34083. + bfound = -1;
  34084. + for (bindex = 0; bindex <= btail; bindex++) {
  34085. + br = au_sbr(sb, bindex);
  34086. + if (au_br_rdonly(br))
  34087. + continue;
  34088. + if (br->br_wbr->wbr_bytes > watermark) {
  34089. + bfound = bindex;
  34090. + break;
  34091. + }
  34092. + }
  34093. + err = bfound;
  34094. + if (err < 0)
  34095. + err = bmfs;
  34096. +
  34097. +out:
  34098. + dput(parent);
  34099. + AuDbg("b%d\n", err);
  34100. + return err;
  34101. +}
  34102. +
  34103. +/* ---------------------------------------------------------------------- */
  34104. +
  34105. +/* most free space and then round robin */
  34106. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  34107. +{
  34108. + int err;
  34109. + struct au_wbr_mfs *mfs;
  34110. +
  34111. + err = au_wbr_create_mfs(dentry, flags);
  34112. + if (err >= 0) {
  34113. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  34114. + mutex_lock(&mfs->mfs_lock);
  34115. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  34116. + err = au_wbr_create_rr(dentry, flags);
  34117. + mutex_unlock(&mfs->mfs_lock);
  34118. + }
  34119. +
  34120. + AuDbg("b%d\n", err);
  34121. + return err;
  34122. +}
  34123. +
  34124. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  34125. +{
  34126. + int err;
  34127. +
  34128. + au_wbr_create_init_mfs(sb); /* ignore */
  34129. + err = au_wbr_create_init_rr(sb);
  34130. +
  34131. + return err;
  34132. +}
  34133. +
  34134. +/* ---------------------------------------------------------------------- */
  34135. +
  34136. +/* top down parent and most free space */
  34137. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  34138. +{
  34139. + int err, e2;
  34140. + unsigned long long b;
  34141. + aufs_bindex_t bindex, btop, bbot;
  34142. + struct super_block *sb;
  34143. + struct dentry *parent, *h_parent;
  34144. + struct au_branch *br;
  34145. +
  34146. + err = au_wbr_create_tdp(dentry, flags);
  34147. + if (unlikely(err < 0))
  34148. + goto out;
  34149. + parent = dget_parent(dentry);
  34150. + btop = au_dbtop(parent);
  34151. + bbot = au_dbtaildir(parent);
  34152. + if (btop == bbot)
  34153. + goto out_parent; /* success */
  34154. +
  34155. + e2 = au_wbr_create_mfs(dentry, flags);
  34156. + if (e2 < 0)
  34157. + goto out_parent; /* success */
  34158. +
  34159. + /* when the available size is equal, select upper one */
  34160. + sb = dentry->d_sb;
  34161. + br = au_sbr(sb, err);
  34162. + b = br->br_wbr->wbr_bytes;
  34163. + AuDbg("b%d, %llu\n", err, b);
  34164. +
  34165. + for (bindex = btop; bindex <= bbot; bindex++) {
  34166. + h_parent = au_h_dptr(parent, bindex);
  34167. + if (!h_parent || d_is_negative(h_parent))
  34168. + continue;
  34169. +
  34170. + br = au_sbr(sb, bindex);
  34171. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  34172. + b = br->br_wbr->wbr_bytes;
  34173. + err = bindex;
  34174. + AuDbg("b%d, %llu\n", err, b);
  34175. + }
  34176. + }
  34177. +
  34178. + if (err >= 0)
  34179. + err = au_wbr_nonopq(dentry, err);
  34180. +
  34181. +out_parent:
  34182. + dput(parent);
  34183. +out:
  34184. + AuDbg("b%d\n", err);
  34185. + return err;
  34186. +}
  34187. +
  34188. +/* ---------------------------------------------------------------------- */
  34189. +
  34190. +/*
  34191. + * - top down parent
  34192. + * - most free space with parent
  34193. + * - most free space round-robin regardless parent
  34194. + */
  34195. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  34196. +{
  34197. + int err;
  34198. + unsigned long long watermark;
  34199. + struct super_block *sb;
  34200. + struct au_branch *br;
  34201. + struct au_wbr_mfs *mfs;
  34202. +
  34203. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  34204. + if (unlikely(err < 0))
  34205. + goto out;
  34206. +
  34207. + sb = dentry->d_sb;
  34208. + br = au_sbr(sb, err);
  34209. + mfs = &au_sbi(sb)->si_wbr_mfs;
  34210. + mutex_lock(&mfs->mfs_lock);
  34211. + watermark = mfs->mfsrr_watermark;
  34212. + mutex_unlock(&mfs->mfs_lock);
  34213. + if (br->br_wbr->wbr_bytes < watermark)
  34214. + /* regardless the parent dir */
  34215. + err = au_wbr_create_mfsrr(dentry, flags);
  34216. +
  34217. +out:
  34218. + AuDbg("b%d\n", err);
  34219. + return err;
  34220. +}
  34221. +
  34222. +/* ---------------------------------------------------------------------- */
  34223. +
  34224. +/* policies for copyup */
  34225. +
  34226. +/* top down parent */
  34227. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  34228. +{
  34229. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  34230. +}
  34231. +
  34232. +/* bottom up parent */
  34233. +static int au_wbr_copyup_bup(struct dentry *dentry)
  34234. +{
  34235. + int err;
  34236. + aufs_bindex_t bindex, btop;
  34237. + struct dentry *parent, *h_parent;
  34238. + struct super_block *sb;
  34239. +
  34240. + err = -EROFS;
  34241. + sb = dentry->d_sb;
  34242. + parent = dget_parent(dentry);
  34243. + btop = au_dbtop(parent);
  34244. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  34245. + h_parent = au_h_dptr(parent, bindex);
  34246. + if (!h_parent || d_is_negative(h_parent))
  34247. + continue;
  34248. +
  34249. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  34250. + err = bindex;
  34251. + break;
  34252. + }
  34253. + }
  34254. + dput(parent);
  34255. +
  34256. + /* bottom up here */
  34257. + if (unlikely(err < 0))
  34258. + err = au_wbr_bu(sb, btop - 1);
  34259. +
  34260. + AuDbg("b%d\n", err);
  34261. + return err;
  34262. +}
  34263. +
  34264. +/* bottom up */
  34265. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  34266. +{
  34267. + int err;
  34268. +
  34269. + err = au_wbr_bu(dentry->d_sb, btop);
  34270. + AuDbg("b%d\n", err);
  34271. + if (err > btop)
  34272. + err = au_wbr_nonopq(dentry, err);
  34273. +
  34274. + AuDbg("b%d\n", err);
  34275. + return err;
  34276. +}
  34277. +
  34278. +static int au_wbr_copyup_bu(struct dentry *dentry)
  34279. +{
  34280. + int err;
  34281. + aufs_bindex_t btop;
  34282. +
  34283. + btop = au_dbtop(dentry);
  34284. + err = au_wbr_do_copyup_bu(dentry, btop);
  34285. + return err;
  34286. +}
  34287. +
  34288. +/* ---------------------------------------------------------------------- */
  34289. +
  34290. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  34291. + [AuWbrCopyup_TDP] = {
  34292. + .copyup = au_wbr_copyup_tdp
  34293. + },
  34294. + [AuWbrCopyup_BUP] = {
  34295. + .copyup = au_wbr_copyup_bup
  34296. + },
  34297. + [AuWbrCopyup_BU] = {
  34298. + .copyup = au_wbr_copyup_bu
  34299. + }
  34300. +};
  34301. +
  34302. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  34303. + [AuWbrCreate_TDP] = {
  34304. + .create = au_wbr_create_tdp
  34305. + },
  34306. + [AuWbrCreate_RR] = {
  34307. + .create = au_wbr_create_rr,
  34308. + .init = au_wbr_create_init_rr
  34309. + },
  34310. + [AuWbrCreate_MFS] = {
  34311. + .create = au_wbr_create_mfs,
  34312. + .init = au_wbr_create_init_mfs,
  34313. + .fin = au_wbr_create_fin_mfs
  34314. + },
  34315. + [AuWbrCreate_MFSV] = {
  34316. + .create = au_wbr_create_mfs,
  34317. + .init = au_wbr_create_init_mfs,
  34318. + .fin = au_wbr_create_fin_mfs
  34319. + },
  34320. + [AuWbrCreate_MFSRR] = {
  34321. + .create = au_wbr_create_mfsrr,
  34322. + .init = au_wbr_create_init_mfsrr,
  34323. + .fin = au_wbr_create_fin_mfs
  34324. + },
  34325. + [AuWbrCreate_MFSRRV] = {
  34326. + .create = au_wbr_create_mfsrr,
  34327. + .init = au_wbr_create_init_mfsrr,
  34328. + .fin = au_wbr_create_fin_mfs
  34329. + },
  34330. + [AuWbrCreate_TDMFS] = {
  34331. + .create = au_wbr_create_tdmfs,
  34332. + .init = au_wbr_create_init_mfs,
  34333. + .fin = au_wbr_create_fin_mfs
  34334. + },
  34335. + [AuWbrCreate_TDMFSV] = {
  34336. + .create = au_wbr_create_tdmfs,
  34337. + .init = au_wbr_create_init_mfs,
  34338. + .fin = au_wbr_create_fin_mfs
  34339. + },
  34340. + [AuWbrCreate_PMFS] = {
  34341. + .create = au_wbr_create_pmfs,
  34342. + .init = au_wbr_create_init_mfs,
  34343. + .fin = au_wbr_create_fin_mfs
  34344. + },
  34345. + [AuWbrCreate_PMFSV] = {
  34346. + .create = au_wbr_create_pmfs,
  34347. + .init = au_wbr_create_init_mfs,
  34348. + .fin = au_wbr_create_fin_mfs
  34349. + },
  34350. + [AuWbrCreate_PMFSRR] = {
  34351. + .create = au_wbr_create_pmfsrr,
  34352. + .init = au_wbr_create_init_mfsrr,
  34353. + .fin = au_wbr_create_fin_mfs
  34354. + },
  34355. + [AuWbrCreate_PMFSRRV] = {
  34356. + .create = au_wbr_create_pmfsrr,
  34357. + .init = au_wbr_create_init_mfsrr,
  34358. + .fin = au_wbr_create_fin_mfs
  34359. + }
  34360. +};
  34361. diff -Naur null/fs/aufs/whout.c linux-6.6.63/fs/aufs/whout.c
  34362. --- /dev/null
  34363. +++ linux-6.6.63/fs/aufs/whout.c 2024-12-18 15:12:59.300519526 +0300
  34364. @@ -0,0 +1,1072 @@
  34365. +// SPDX-License-Identifier: GPL-2.0
  34366. +/*
  34367. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  34368. + *
  34369. + * This program is free software; you can redistribute it and/or modify
  34370. + * it under the terms of the GNU General Public License as published by
  34371. + * the Free Software Foundation; either version 2 of the License, or
  34372. + * (at your option) any later version.
  34373. + *
  34374. + * This program is distributed in the hope that it will be useful,
  34375. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34376. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34377. + * GNU General Public License for more details.
  34378. + *
  34379. + * You should have received a copy of the GNU General Public License
  34380. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34381. + */
  34382. +
  34383. +/*
  34384. + * whiteout for logical deletion and opaque directory
  34385. + */
  34386. +
  34387. +#include "aufs.h"
  34388. +
  34389. +#define WH_MASK 0444
  34390. +
  34391. +/*
  34392. + * If a directory contains this file, then it is opaque. We start with the
  34393. + * .wh. flag so that it is blocked by lookup.
  34394. + */
  34395. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  34396. + sizeof(AUFS_WH_DIROPQ) - 1);
  34397. +
  34398. +/*
  34399. + * generate whiteout name, which is NOT terminated by NULL.
  34400. + * @name: original d_name.name
  34401. + * @len: original d_name.len
  34402. + * @wh: whiteout qstr
  34403. + * returns zero when succeeds, otherwise error.
  34404. + * succeeded value as wh->name should be freed by kfree().
  34405. + */
  34406. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  34407. +{
  34408. + char *p;
  34409. +
  34410. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  34411. + return -ENAMETOOLONG;
  34412. +
  34413. + wh->len = name->len + AUFS_WH_PFX_LEN;
  34414. + p = kmalloc(wh->len, GFP_NOFS);
  34415. + wh->name = p;
  34416. + if (p) {
  34417. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  34418. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  34419. + /* smp_mb(); */
  34420. + return 0;
  34421. + }
  34422. + return -ENOMEM;
  34423. +}
  34424. +
  34425. +/* ---------------------------------------------------------------------- */
  34426. +
  34427. +/*
  34428. + * test if the @wh_name exists under @h_ppath.
  34429. + * @try_sio specifies the necessary of super-io.
  34430. + */
  34431. +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
  34432. + struct qstr *wh_name, int try_sio)
  34433. +{
  34434. + int err;
  34435. + struct dentry *wh_dentry;
  34436. +
  34437. + if (!try_sio)
  34438. + wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
  34439. + else
  34440. + wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
  34441. + err = PTR_ERR(wh_dentry);
  34442. + if (IS_ERR(wh_dentry)) {
  34443. + if (err == -ENAMETOOLONG)
  34444. + err = 0;
  34445. + goto out;
  34446. + }
  34447. +
  34448. + err = 0;
  34449. + if (d_is_negative(wh_dentry))
  34450. + goto out_wh; /* success */
  34451. +
  34452. + err = 1;
  34453. + if (d_is_reg(wh_dentry))
  34454. + goto out_wh; /* success */
  34455. +
  34456. + err = -EIO;
  34457. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  34458. + wh_dentry, d_inode(wh_dentry)->i_mode);
  34459. +
  34460. +out_wh:
  34461. + dput(wh_dentry);
  34462. +out:
  34463. + return err;
  34464. +}
  34465. +
  34466. +/*
  34467. + * test if the @h_path->dentry sets opaque or not.
  34468. + */
  34469. +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
  34470. +{
  34471. + int err;
  34472. + struct inode *h_dir;
  34473. +
  34474. + h_dir = d_inode(h_path->dentry);
  34475. + err = au_wh_test(h_idmap, h_path, &diropq_name,
  34476. + au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
  34477. + return err;
  34478. +}
  34479. +
  34480. +/*
  34481. + * returns a negative dentry whose name is unique and temporary.
  34482. + */
  34483. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  34484. + struct qstr *prefix)
  34485. +{
  34486. + struct dentry *dentry;
  34487. + int i;
  34488. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  34489. + *name, *p;
  34490. + /* strict atomic_t is unnecessary here */
  34491. + static unsigned short cnt;
  34492. + struct qstr qs;
  34493. + struct path h_ppath;
  34494. + struct mnt_idmap *h_idmap;
  34495. +
  34496. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  34497. +
  34498. + name = defname;
  34499. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  34500. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  34501. + dentry = ERR_PTR(-ENAMETOOLONG);
  34502. + if (unlikely(qs.len > NAME_MAX))
  34503. + goto out;
  34504. + dentry = ERR_PTR(-ENOMEM);
  34505. + name = kmalloc(qs.len + 1, GFP_NOFS);
  34506. + if (unlikely(!name))
  34507. + goto out;
  34508. + }
  34509. +
  34510. + /* doubly whiteout-ed */
  34511. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  34512. + p = name + AUFS_WH_PFX_LEN * 2;
  34513. + memcpy(p, prefix->name, prefix->len);
  34514. + p += prefix->len;
  34515. + *p++ = '.';
  34516. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  34517. +
  34518. + h_ppath.dentry = h_parent;
  34519. + h_ppath.mnt = au_br_mnt(br);
  34520. + h_idmap = au_br_idmap(br);
  34521. + qs.name = name;
  34522. + for (i = 0; i < 3; i++) {
  34523. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  34524. + dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
  34525. + if (IS_ERR(dentry) || d_is_negative(dentry))
  34526. + goto out_name;
  34527. + dput(dentry);
  34528. + }
  34529. + /* pr_warn("could not get random name\n"); */
  34530. + dentry = ERR_PTR(-EEXIST);
  34531. + AuDbg("%.*s\n", AuLNPair(&qs));
  34532. + BUG();
  34533. +
  34534. +out_name:
  34535. + if (name != defname)
  34536. + au_kfree_try_rcu(name);
  34537. +out:
  34538. + AuTraceErrPtr(dentry);
  34539. + return dentry;
  34540. +}
  34541. +
  34542. +/*
  34543. + * rename the @h_dentry on @br to the whiteouted temporary name.
  34544. + */
  34545. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  34546. +{
  34547. + int err;
  34548. + struct path h_path = {
  34549. + .mnt = au_br_mnt(br)
  34550. + };
  34551. + struct inode *h_dir, *delegated;
  34552. + struct dentry *h_parent;
  34553. +
  34554. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  34555. + h_dir = d_inode(h_parent);
  34556. + IMustLock(h_dir);
  34557. +
  34558. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  34559. + err = PTR_ERR(h_path.dentry);
  34560. + if (IS_ERR(h_path.dentry))
  34561. + goto out;
  34562. +
  34563. + /* under the same dir, no need to lock_rename() */
  34564. + delegated = NULL;
  34565. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
  34566. + /*flags*/0);
  34567. + AuTraceErr(err);
  34568. + if (unlikely(err == -EWOULDBLOCK)) {
  34569. + pr_warn("cannot retry for NFSv4 delegation"
  34570. + " for an internal rename\n");
  34571. + iput(delegated);
  34572. + }
  34573. + dput(h_path.dentry);
  34574. +
  34575. +out:
  34576. + AuTraceErr(err);
  34577. + return err;
  34578. +}
  34579. +
  34580. +/* ---------------------------------------------------------------------- */
  34581. +/*
  34582. + * functions for removing a whiteout
  34583. + */
  34584. +
  34585. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  34586. +{
  34587. + int err, force;
  34588. + struct inode *delegated;
  34589. +
  34590. + /*
  34591. + * forces superio when the dir has a sticky bit.
  34592. + * this may be a violation of unix fs semantics.
  34593. + */
  34594. + force = (h_dir->i_mode & S_ISVTX)
  34595. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  34596. + delegated = NULL;
  34597. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  34598. + if (unlikely(err == -EWOULDBLOCK)) {
  34599. + pr_warn("cannot retry for NFSv4 delegation"
  34600. + " for an internal unlink\n");
  34601. + iput(delegated);
  34602. + }
  34603. + return err;
  34604. +}
  34605. +
  34606. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  34607. + struct dentry *dentry)
  34608. +{
  34609. + int err;
  34610. +
  34611. + err = do_unlink_wh(h_dir, h_path);
  34612. + if (!err && dentry)
  34613. + au_set_dbwh(dentry, -1);
  34614. +
  34615. + return err;
  34616. +}
  34617. +
  34618. +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
  34619. +{
  34620. + int err;
  34621. + struct path h_path;
  34622. +
  34623. + err = 0;
  34624. + h_path.dentry = vfsub_lkup_one(wh, h_ppath);
  34625. + if (IS_ERR(h_path.dentry))
  34626. + err = PTR_ERR(h_path.dentry);
  34627. + else {
  34628. + if (d_is_reg(h_path.dentry)) {
  34629. + h_path.mnt = h_ppath->mnt;
  34630. + err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
  34631. + }
  34632. + dput(h_path.dentry);
  34633. + }
  34634. +
  34635. + return err;
  34636. +}
  34637. +
  34638. +/* ---------------------------------------------------------------------- */
  34639. +/*
  34640. + * initialize/clean whiteout for a branch
  34641. + */
  34642. +
  34643. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  34644. + const int isdir)
  34645. +{
  34646. + int err;
  34647. + struct inode *delegated;
  34648. +
  34649. + if (d_is_negative(whpath->dentry))
  34650. + return;
  34651. +
  34652. + if (isdir)
  34653. + err = vfsub_rmdir(h_dir, whpath);
  34654. + else {
  34655. + delegated = NULL;
  34656. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  34657. + if (unlikely(err == -EWOULDBLOCK)) {
  34658. + pr_warn("cannot retry for NFSv4 delegation"
  34659. + " for an internal unlink\n");
  34660. + iput(delegated);
  34661. + }
  34662. + }
  34663. + if (unlikely(err))
  34664. + pr_warn("failed removing %pd (%d), ignored.\n",
  34665. + whpath->dentry, err);
  34666. +}
  34667. +
  34668. +static int test_linkable(struct dentry *h_root)
  34669. +{
  34670. + struct inode *h_dir = d_inode(h_root);
  34671. +
  34672. + if (h_dir->i_op->link)
  34673. + return 0;
  34674. +
  34675. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  34676. + h_root, au_sbtype(h_root->d_sb));
  34677. + return -ENOSYS; /* the branch doesn't have its ->link() */
  34678. +}
  34679. +
  34680. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  34681. +static int au_whdir(struct inode *h_dir, struct path *path)
  34682. +{
  34683. + int err;
  34684. +
  34685. + err = -EEXIST;
  34686. + if (d_is_negative(path->dentry)) {
  34687. + int mode = 0700;
  34688. +
  34689. + if (au_test_nfs(path->dentry->d_sb))
  34690. + mode |= 0111;
  34691. + err = vfsub_mkdir(h_dir, path, mode);
  34692. + } else if (d_is_dir(path->dentry))
  34693. + err = 0;
  34694. + else
  34695. + pr_err("unknown %pd exists\n", path->dentry);
  34696. +
  34697. + return err;
  34698. +}
  34699. +
  34700. +struct au_wh_base {
  34701. + const struct qstr *name;
  34702. + struct dentry *dentry;
  34703. +};
  34704. +
  34705. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  34706. + struct path *h_path)
  34707. +{
  34708. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34709. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34710. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34711. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34712. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34713. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34714. +}
  34715. +
  34716. +/*
  34717. + * returns tri-state,
  34718. + * minus: error, caller should print the message
  34719. + * zero: success
  34720. + * plus: error, caller should NOT print the message
  34721. + */
  34722. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  34723. + int do_plink, struct au_wh_base base[],
  34724. + struct path *h_path)
  34725. +{
  34726. + int err;
  34727. + struct inode *h_dir;
  34728. +
  34729. + h_dir = d_inode(h_root);
  34730. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34731. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34732. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34733. + if (do_plink) {
  34734. + err = test_linkable(h_root);
  34735. + if (unlikely(err)) {
  34736. + err = 1;
  34737. + goto out;
  34738. + }
  34739. +
  34740. + err = au_whdir(h_dir, h_path);
  34741. + if (unlikely(err))
  34742. + goto out;
  34743. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34744. + } else
  34745. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34746. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34747. + err = au_whdir(h_dir, h_path);
  34748. + if (unlikely(err))
  34749. + goto out;
  34750. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34751. +
  34752. +out:
  34753. + return err;
  34754. +}
  34755. +
  34756. +/*
  34757. + * for the moment, aufs supports the branch filesystem which does not support
  34758. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  34759. + * copyup failed. finally, such filesystem will not be used as the writable
  34760. + * branch.
  34761. + *
  34762. + * returns tri-state, see above.
  34763. + */
  34764. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  34765. + int do_plink, struct au_wh_base base[],
  34766. + struct path *h_path)
  34767. +{
  34768. + int err;
  34769. + struct inode *h_dir;
  34770. +
  34771. + WbrWhMustWriteLock(wbr);
  34772. +
  34773. + err = test_linkable(h_root);
  34774. + if (unlikely(err)) {
  34775. + err = 1;
  34776. + goto out;
  34777. + }
  34778. +
  34779. + /*
  34780. + * todo: should this create be done in /sbin/mount.aufs helper?
  34781. + */
  34782. + err = -EEXIST;
  34783. + h_dir = d_inode(h_root);
  34784. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  34785. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34786. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  34787. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  34788. + err = 0;
  34789. + else
  34790. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  34791. + if (unlikely(err))
  34792. + goto out;
  34793. +
  34794. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34795. + if (do_plink) {
  34796. + err = au_whdir(h_dir, h_path);
  34797. + if (unlikely(err))
  34798. + goto out;
  34799. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34800. + } else
  34801. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34802. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  34803. +
  34804. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34805. + err = au_whdir(h_dir, h_path);
  34806. + if (unlikely(err))
  34807. + goto out;
  34808. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34809. +
  34810. +out:
  34811. + return err;
  34812. +}
  34813. +
  34814. +/*
  34815. + * initialize the whiteout base file/dir for @br.
  34816. + */
  34817. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  34818. +{
  34819. + int err, i;
  34820. + const unsigned char do_plink
  34821. + = !!au_opt_test(au_mntflags(sb), PLINK);
  34822. + struct inode *h_dir;
  34823. + struct path path = br->br_path;
  34824. + struct dentry *h_root = path.dentry;
  34825. + struct au_wbr *wbr = br->br_wbr;
  34826. + static const struct qstr base_name[] = {
  34827. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  34828. + sizeof(AUFS_BASE_NAME) - 1),
  34829. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  34830. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  34831. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  34832. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  34833. + };
  34834. + struct au_wh_base base[] = {
  34835. + [AuBrWh_BASE] = {
  34836. + .name = base_name + AuBrWh_BASE,
  34837. + .dentry = NULL
  34838. + },
  34839. + [AuBrWh_PLINK] = {
  34840. + .name = base_name + AuBrWh_PLINK,
  34841. + .dentry = NULL
  34842. + },
  34843. + [AuBrWh_ORPH] = {
  34844. + .name = base_name + AuBrWh_ORPH,
  34845. + .dentry = NULL
  34846. + }
  34847. + };
  34848. +
  34849. + if (wbr)
  34850. + WbrWhMustWriteLock(wbr);
  34851. +
  34852. + for (i = 0; i < AuBrWh_Last; i++) {
  34853. + /* doubly whiteouted */
  34854. + struct dentry *d;
  34855. +
  34856. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  34857. + err = PTR_ERR(d);
  34858. + if (IS_ERR(d))
  34859. + goto out;
  34860. +
  34861. + base[i].dentry = d;
  34862. + AuDebugOn(wbr
  34863. + && wbr->wbr_wh[i]
  34864. + && wbr->wbr_wh[i] != base[i].dentry);
  34865. + }
  34866. +
  34867. + if (wbr)
  34868. + for (i = 0; i < AuBrWh_Last; i++) {
  34869. + dput(wbr->wbr_wh[i]);
  34870. + wbr->wbr_wh[i] = NULL;
  34871. + }
  34872. +
  34873. + err = 0;
  34874. + if (!au_br_writable(br->br_perm)) {
  34875. + h_dir = d_inode(h_root);
  34876. + au_wh_init_ro(h_dir, base, &path);
  34877. + } else if (!au_br_wh_linkable(br->br_perm)) {
  34878. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  34879. + if (err > 0)
  34880. + goto out;
  34881. + else if (err)
  34882. + goto out_err;
  34883. + } else {
  34884. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  34885. + if (err > 0)
  34886. + goto out;
  34887. + else if (err)
  34888. + goto out_err;
  34889. + }
  34890. + goto out; /* success */
  34891. +
  34892. +out_err:
  34893. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  34894. + err, h_root, au_sbtype(h_root->d_sb));
  34895. +out:
  34896. + for (i = 0; i < AuBrWh_Last; i++)
  34897. + dput(base[i].dentry);
  34898. + return err;
  34899. +}
  34900. +
  34901. +/* ---------------------------------------------------------------------- */
  34902. +/*
  34903. + * whiteouts are all hard-linked usually.
  34904. + * when its link count reaches a ceiling, we create a new whiteout base
  34905. + * asynchronously.
  34906. + */
  34907. +
  34908. +struct reinit_br_wh {
  34909. + struct super_block *sb;
  34910. + struct au_branch *br;
  34911. +};
  34912. +
  34913. +static void reinit_br_wh(void *arg)
  34914. +{
  34915. + int err;
  34916. + aufs_bindex_t bindex;
  34917. + struct path h_path;
  34918. + struct reinit_br_wh *a = arg;
  34919. + struct au_wbr *wbr;
  34920. + struct inode *dir, *delegated;
  34921. + struct dentry *h_root;
  34922. + struct au_hinode *hdir;
  34923. +
  34924. + err = 0;
  34925. + wbr = a->br->br_wbr;
  34926. + /* big aufs lock */
  34927. + si_noflush_write_lock(a->sb);
  34928. + if (!au_br_writable(a->br->br_perm))
  34929. + goto out;
  34930. + bindex = au_br_index(a->sb, a->br->br_id);
  34931. + if (unlikely(bindex < 0))
  34932. + goto out;
  34933. +
  34934. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  34935. + dir = d_inode(a->sb->s_root);
  34936. + hdir = au_hi(dir, bindex);
  34937. + h_root = au_h_dptr(a->sb->s_root, bindex);
  34938. + AuDebugOn(h_root != au_br_dentry(a->br));
  34939. +
  34940. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34941. + wbr_wh_write_lock(wbr);
  34942. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  34943. + h_root, a->br);
  34944. + if (!err) {
  34945. + h_path.dentry = wbr->wbr_whbase;
  34946. + h_path.mnt = au_br_mnt(a->br);
  34947. + delegated = NULL;
  34948. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  34949. + /*force*/0);
  34950. + if (unlikely(err == -EWOULDBLOCK)) {
  34951. + pr_warn("cannot retry for NFSv4 delegation"
  34952. + " for an internal unlink\n");
  34953. + iput(delegated);
  34954. + }
  34955. + } else {
  34956. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  34957. + err = 0;
  34958. + }
  34959. + dput(wbr->wbr_whbase);
  34960. + wbr->wbr_whbase = NULL;
  34961. + if (!err)
  34962. + err = au_wh_init(a->br, a->sb);
  34963. + wbr_wh_write_unlock(wbr);
  34964. + au_hn_inode_unlock(hdir);
  34965. + di_read_unlock(a->sb->s_root, AuLock_IR);
  34966. + if (!err)
  34967. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  34968. +
  34969. +out:
  34970. + if (wbr)
  34971. + atomic_dec(&wbr->wbr_wh_running);
  34972. + au_lcnt_dec(&a->br->br_count);
  34973. + si_write_unlock(a->sb);
  34974. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  34975. + au_kfree_rcu(a);
  34976. + if (unlikely(err))
  34977. + AuIOErr("err %d\n", err);
  34978. +}
  34979. +
  34980. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  34981. +{
  34982. + int do_dec, wkq_err;
  34983. + struct reinit_br_wh *arg;
  34984. +
  34985. + do_dec = 1;
  34986. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  34987. + goto out;
  34988. +
  34989. + /* ignore ENOMEM */
  34990. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  34991. + if (arg) {
  34992. + /*
  34993. + * dec(wh_running), kfree(arg) and dec(br_count)
  34994. + * in reinit function
  34995. + */
  34996. + arg->sb = sb;
  34997. + arg->br = br;
  34998. + au_lcnt_inc(&br->br_count);
  34999. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  35000. + if (unlikely(wkq_err)) {
  35001. + atomic_dec(&br->br_wbr->wbr_wh_running);
  35002. + au_lcnt_dec(&br->br_count);
  35003. + au_kfree_rcu(arg);
  35004. + }
  35005. + do_dec = 0;
  35006. + }
  35007. +
  35008. +out:
  35009. + if (do_dec)
  35010. + atomic_dec(&br->br_wbr->wbr_wh_running);
  35011. +}
  35012. +
  35013. +/* ---------------------------------------------------------------------- */
  35014. +
  35015. +/*
  35016. + * create the whiteout @wh.
  35017. + */
  35018. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  35019. + struct dentry *wh)
  35020. +{
  35021. + int err;
  35022. + struct path h_path = {
  35023. + .dentry = wh
  35024. + };
  35025. + struct au_branch *br;
  35026. + struct au_wbr *wbr;
  35027. + struct dentry *h_parent;
  35028. + struct inode *h_dir, *delegated;
  35029. +
  35030. + h_parent = wh->d_parent; /* dir inode is locked */
  35031. + h_dir = d_inode(h_parent);
  35032. + IMustLock(h_dir);
  35033. +
  35034. + br = au_sbr(sb, bindex);
  35035. + h_path.mnt = au_br_mnt(br);
  35036. + wbr = br->br_wbr;
  35037. + wbr_wh_read_lock(wbr);
  35038. + if (wbr->wbr_whbase) {
  35039. + delegated = NULL;
  35040. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  35041. + if (unlikely(err == -EWOULDBLOCK)) {
  35042. + pr_warn("cannot retry for NFSv4 delegation"
  35043. + " for an internal link\n");
  35044. + iput(delegated);
  35045. + }
  35046. + if (!err || err != -EMLINK)
  35047. + goto out;
  35048. +
  35049. + /* link count full. re-initialize br_whbase. */
  35050. + kick_reinit_br_wh(sb, br);
  35051. + }
  35052. +
  35053. + /* return this error in this context */
  35054. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  35055. + if (!err)
  35056. + au_fhsm_wrote(sb, bindex, /*force*/0);
  35057. +
  35058. +out:
  35059. + wbr_wh_read_unlock(wbr);
  35060. + return err;
  35061. +}
  35062. +
  35063. +/* ---------------------------------------------------------------------- */
  35064. +
  35065. +/*
  35066. + * create or remove the diropq.
  35067. + */
  35068. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  35069. + unsigned int flags)
  35070. +{
  35071. + struct dentry *opq_dentry;
  35072. + struct super_block *sb;
  35073. + struct au_branch *br;
  35074. + struct path h_path;
  35075. + int err;
  35076. +
  35077. + sb = dentry->d_sb;
  35078. + br = au_sbr(sb, bindex);
  35079. + h_path.dentry = au_h_dptr(dentry, bindex);
  35080. + h_path.mnt = au_br_mnt(br);
  35081. + opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
  35082. + if (IS_ERR(opq_dentry))
  35083. + goto out;
  35084. +
  35085. + if (au_ftest_diropq(flags, CREATE)) {
  35086. + err = link_or_create_wh(sb, bindex, opq_dentry);
  35087. + if (!err) {
  35088. + au_set_dbdiropq(dentry, bindex);
  35089. + goto out; /* success */
  35090. + }
  35091. + } else {
  35092. + h_path.dentry = opq_dentry;
  35093. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
  35094. + if (!err)
  35095. + au_set_dbdiropq(dentry, -1);
  35096. + }
  35097. + dput(opq_dentry);
  35098. + opq_dentry = ERR_PTR(err);
  35099. +
  35100. +out:
  35101. + return opq_dentry;
  35102. +}
  35103. +
  35104. +struct do_diropq_args {
  35105. + struct dentry **errp;
  35106. + struct dentry *dentry;
  35107. + aufs_bindex_t bindex;
  35108. + unsigned int flags;
  35109. +};
  35110. +
  35111. +static void call_do_diropq(void *args)
  35112. +{
  35113. + struct do_diropq_args *a = args;
  35114. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  35115. +}
  35116. +
  35117. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  35118. + unsigned int flags)
  35119. +{
  35120. + struct dentry *diropq, *h_dentry;
  35121. + struct mnt_idmap *h_idmap;
  35122. +
  35123. + h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
  35124. + h_dentry = au_h_dptr(dentry, bindex);
  35125. + if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
  35126. + MAY_EXEC | MAY_WRITE))
  35127. + diropq = do_diropq(dentry, bindex, flags);
  35128. + else {
  35129. + int wkq_err;
  35130. + struct do_diropq_args args = {
  35131. + .errp = &diropq,
  35132. + .dentry = dentry,
  35133. + .bindex = bindex,
  35134. + .flags = flags
  35135. + };
  35136. +
  35137. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  35138. + if (unlikely(wkq_err))
  35139. + diropq = ERR_PTR(wkq_err);
  35140. + }
  35141. +
  35142. + return diropq;
  35143. +}
  35144. +
  35145. +/* ---------------------------------------------------------------------- */
  35146. +
  35147. +/*
  35148. + * lookup whiteout dentry.
  35149. + * @h_parent: lower parent dentry which must exist and be locked
  35150. + * @base_name: name of dentry which will be whiteouted
  35151. + * returns dentry for whiteout.
  35152. + */
  35153. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  35154. + struct au_branch *br)
  35155. +{
  35156. + int err;
  35157. + struct qstr wh_name;
  35158. + struct dentry *wh_dentry;
  35159. + struct path h_path;
  35160. +
  35161. + err = au_wh_name_alloc(&wh_name, base_name);
  35162. + wh_dentry = ERR_PTR(err);
  35163. + if (!err) {
  35164. + h_path.dentry = h_parent;
  35165. + h_path.mnt = au_br_mnt(br);
  35166. + wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
  35167. + au_kfree_try_rcu(wh_name.name);
  35168. + }
  35169. + return wh_dentry;
  35170. +}
  35171. +
  35172. +/*
  35173. + * link/create a whiteout for @dentry on @bindex.
  35174. + */
  35175. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  35176. + struct dentry *h_parent)
  35177. +{
  35178. + struct dentry *wh_dentry;
  35179. + struct super_block *sb;
  35180. + int err;
  35181. +
  35182. + sb = dentry->d_sb;
  35183. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  35184. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  35185. + err = link_or_create_wh(sb, bindex, wh_dentry);
  35186. + if (!err) {
  35187. + au_set_dbwh(dentry, bindex);
  35188. + au_fhsm_wrote(sb, bindex, /*force*/0);
  35189. + } else {
  35190. + dput(wh_dentry);
  35191. + wh_dentry = ERR_PTR(err);
  35192. + }
  35193. + }
  35194. +
  35195. + return wh_dentry;
  35196. +}
  35197. +
  35198. +/* ---------------------------------------------------------------------- */
  35199. +
  35200. +/* Delete all whiteouts in this directory on branch bindex. */
  35201. +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
  35202. + aufs_bindex_t bindex)
  35203. +{
  35204. + int err;
  35205. + unsigned long ul, n;
  35206. + struct qstr wh_name;
  35207. + char *p;
  35208. + struct hlist_head *head;
  35209. + struct au_vdir_wh *pos;
  35210. + struct au_vdir_destr *str;
  35211. +
  35212. + err = -ENOMEM;
  35213. + p = (void *)__get_free_page(GFP_NOFS);
  35214. + wh_name.name = p;
  35215. + if (unlikely(!wh_name.name))
  35216. + goto out;
  35217. +
  35218. + err = 0;
  35219. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  35220. + p += AUFS_WH_PFX_LEN;
  35221. + n = whlist->nh_num;
  35222. + head = whlist->nh_head;
  35223. + for (ul = 0; !err && ul < n; ul++, head++) {
  35224. + hlist_for_each_entry(pos, head, wh_hash) {
  35225. + if (pos->wh_bindex != bindex)
  35226. + continue;
  35227. +
  35228. + str = &pos->wh_str;
  35229. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  35230. + memcpy(p, str->name, str->len);
  35231. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  35232. + err = unlink_wh_name(h_path, &wh_name);
  35233. + if (!err)
  35234. + continue;
  35235. + break;
  35236. + }
  35237. + AuIOErr("whiteout name too long %.*s\n",
  35238. + str->len, str->name);
  35239. + err = -EIO;
  35240. + break;
  35241. + }
  35242. + }
  35243. + free_page((unsigned long)wh_name.name);
  35244. +
  35245. +out:
  35246. + return err;
  35247. +}
  35248. +
  35249. +struct del_wh_children_args {
  35250. + int *errp;
  35251. + struct path *h_path;
  35252. + struct au_nhash *whlist;
  35253. + aufs_bindex_t bindex;
  35254. +};
  35255. +
  35256. +static void call_del_wh_children(void *args)
  35257. +{
  35258. + struct del_wh_children_args *a = args;
  35259. + *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
  35260. +}
  35261. +
  35262. +/* ---------------------------------------------------------------------- */
  35263. +
  35264. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  35265. +{
  35266. + struct au_whtmp_rmdir *whtmp;
  35267. + int err;
  35268. + unsigned int rdhash;
  35269. +
  35270. + SiMustAnyLock(sb);
  35271. +
  35272. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  35273. + if (unlikely(!whtmp)) {
  35274. + whtmp = ERR_PTR(-ENOMEM);
  35275. + goto out;
  35276. + }
  35277. +
  35278. + /* no estimation for dir size */
  35279. + rdhash = au_sbi(sb)->si_rdhash;
  35280. + if (!rdhash)
  35281. + rdhash = AUFS_RDHASH_DEF;
  35282. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  35283. + if (unlikely(err)) {
  35284. + au_kfree_rcu(whtmp);
  35285. + whtmp = ERR_PTR(err);
  35286. + }
  35287. +
  35288. +out:
  35289. + return whtmp;
  35290. +}
  35291. +
  35292. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  35293. +{
  35294. + if (whtmp->br)
  35295. + au_lcnt_dec(&whtmp->br->br_count);
  35296. + dput(whtmp->wh_dentry);
  35297. + iput(whtmp->dir);
  35298. + au_nhash_wh_free(&whtmp->whlist);
  35299. + au_kfree_rcu(whtmp);
  35300. +}
  35301. +
  35302. +/*
  35303. + * rmdir the whiteouted temporary named dir @h_dentry.
  35304. + * @whlist: whiteouted children.
  35305. + */
  35306. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35307. + struct dentry *wh_dentry, struct au_nhash *whlist)
  35308. +{
  35309. + int err;
  35310. + unsigned int h_nlink;
  35311. + struct path wh_path;
  35312. + struct inode *wh_inode, *h_dir;
  35313. + struct au_branch *br;
  35314. + struct mnt_idmap *h_idmap;
  35315. +
  35316. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  35317. + IMustLock(h_dir);
  35318. +
  35319. + br = au_sbr(dir->i_sb, bindex);
  35320. + wh_path.dentry = wh_dentry;
  35321. + wh_path.mnt = au_br_mnt(br);
  35322. + h_idmap = au_br_idmap(br);
  35323. + wh_inode = d_inode(wh_dentry);
  35324. + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
  35325. +
  35326. + /*
  35327. + * someone else might change some whiteouts while we were sleeping.
  35328. + * it means this whlist may have an obsoleted entry.
  35329. + */
  35330. + if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
  35331. + err = del_wh_children(&wh_path, whlist, bindex);
  35332. + else {
  35333. + int wkq_err;
  35334. + struct del_wh_children_args args = {
  35335. + .errp = &err,
  35336. + .h_path = &wh_path,
  35337. + .whlist = whlist,
  35338. + .bindex = bindex
  35339. + };
  35340. +
  35341. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  35342. + if (unlikely(wkq_err))
  35343. + err = wkq_err;
  35344. + }
  35345. + inode_unlock(wh_inode);
  35346. +
  35347. + if (!err) {
  35348. + h_nlink = vfsub_inode_nlink(h_dir, AU_I_BRANCH);
  35349. + err = vfsub_rmdir(h_dir, &wh_path);
  35350. + /* some fs doesn't change the parent nlink in some cases */
  35351. + h_nlink -= vfsub_inode_nlink(h_dir, AU_I_BRANCH);
  35352. + }
  35353. +
  35354. + if (!err) {
  35355. + if (au_ibtop(dir) == bindex) {
  35356. + /* todo: dir->i_mutex is necessary */
  35357. + au_cpup_attr_timesizes(dir);
  35358. + if (h_nlink)
  35359. + vfsub_drop_nlink(dir);
  35360. + }
  35361. + return 0; /* success */
  35362. + }
  35363. +
  35364. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  35365. + return err;
  35366. +}
  35367. +
  35368. +static void call_rmdir_whtmp(void *args)
  35369. +{
  35370. + int err;
  35371. + aufs_bindex_t bindex;
  35372. + struct au_whtmp_rmdir *a = args;
  35373. + struct super_block *sb;
  35374. + struct dentry *h_parent;
  35375. + struct inode *h_dir;
  35376. + struct au_hinode *hdir;
  35377. +
  35378. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  35379. + /* inode_lock(a->dir); */
  35380. + err = -EROFS;
  35381. + sb = a->dir->i_sb;
  35382. + si_read_lock(sb, !AuLock_FLUSH);
  35383. + if (!au_br_writable(a->br->br_perm))
  35384. + goto out;
  35385. + bindex = au_br_index(sb, a->br->br_id);
  35386. + if (unlikely(bindex < 0))
  35387. + goto out;
  35388. +
  35389. + err = -EIO;
  35390. + ii_write_lock_parent(a->dir);
  35391. + h_parent = dget_parent(a->wh_dentry);
  35392. + h_dir = d_inode(h_parent);
  35393. + hdir = au_hi(a->dir, bindex);
  35394. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  35395. + if (unlikely(err))
  35396. + goto out_mnt;
  35397. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  35398. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  35399. + a->br);
  35400. + if (!err)
  35401. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  35402. + au_hn_inode_unlock(hdir);
  35403. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  35404. +
  35405. +out_mnt:
  35406. + dput(h_parent);
  35407. + ii_write_unlock(a->dir);
  35408. +out:
  35409. + /* inode_unlock(a->dir); */
  35410. + au_whtmp_rmdir_free(a);
  35411. + si_read_unlock(sb);
  35412. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35413. + if (unlikely(err))
  35414. + AuIOErr("err %d\n", err);
  35415. +}
  35416. +
  35417. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35418. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  35419. +{
  35420. + int wkq_err;
  35421. + struct super_block *sb;
  35422. +
  35423. + IMustLock(dir);
  35424. +
  35425. + /* all post-process will be done in do_rmdir_whtmp(). */
  35426. + sb = dir->i_sb;
  35427. + args->dir = au_igrab(dir);
  35428. + args->br = au_sbr(sb, bindex);
  35429. + au_lcnt_inc(&args->br->br_count);
  35430. + args->wh_dentry = dget(wh_dentry);
  35431. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  35432. + if (unlikely(wkq_err)) {
  35433. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  35434. + au_whtmp_rmdir_free(args);
  35435. + }
  35436. +}
  35437. diff -Naur null/fs/aufs/whout.h linux-6.6.63/fs/aufs/whout.h
  35438. --- /dev/null
  35439. +++ linux-6.6.63/fs/aufs/whout.h 2024-12-18 15:12:59.300519526 +0300
  35440. @@ -0,0 +1,87 @@
  35441. +/* SPDX-License-Identifier: GPL-2.0 */
  35442. +/*
  35443. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  35444. + *
  35445. + * This program is free software; you can redistribute it and/or modify
  35446. + * it under the terms of the GNU General Public License as published by
  35447. + * the Free Software Foundation; either version 2 of the License, or
  35448. + * (at your option) any later version.
  35449. + *
  35450. + * This program is distributed in the hope that it will be useful,
  35451. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35452. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35453. + * GNU General Public License for more details.
  35454. + *
  35455. + * You should have received a copy of the GNU General Public License
  35456. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35457. + */
  35458. +
  35459. +/*
  35460. + * whiteout for logical deletion and opaque directory
  35461. + */
  35462. +
  35463. +#ifndef __AUFS_WHOUT_H__
  35464. +#define __AUFS_WHOUT_H__
  35465. +
  35466. +#ifdef __KERNEL__
  35467. +
  35468. +#include "dir.h"
  35469. +
  35470. +/* whout.c */
  35471. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  35472. +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
  35473. + struct qstr *wh_name, int try_sio);
  35474. +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
  35475. +struct au_branch;
  35476. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  35477. + struct qstr *prefix);
  35478. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  35479. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  35480. + struct dentry *dentry);
  35481. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  35482. +
  35483. +/* diropq flags */
  35484. +#define AuDiropq_CREATE 1
  35485. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  35486. +#define au_fset_diropq(flags, name) \
  35487. + do { (flags) |= AuDiropq_##name; } while (0)
  35488. +#define au_fclr_diropq(flags, name) \
  35489. + do { (flags) &= ~AuDiropq_##name; } while (0)
  35490. +
  35491. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  35492. + unsigned int flags);
  35493. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  35494. + struct au_branch *br);
  35495. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  35496. + struct dentry *h_parent);
  35497. +
  35498. +/* real rmdir for the whiteout-ed dir */
  35499. +struct au_whtmp_rmdir {
  35500. + struct inode *dir;
  35501. + struct au_branch *br;
  35502. + struct dentry *wh_dentry;
  35503. + struct au_nhash whlist;
  35504. +};
  35505. +
  35506. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  35507. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  35508. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35509. + struct dentry *wh_dentry, struct au_nhash *whlist);
  35510. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35511. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  35512. +
  35513. +/* ---------------------------------------------------------------------- */
  35514. +
  35515. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  35516. + aufs_bindex_t bindex)
  35517. +{
  35518. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  35519. +}
  35520. +
  35521. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  35522. +{
  35523. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  35524. +}
  35525. +
  35526. +#endif /* __KERNEL__ */
  35527. +#endif /* __AUFS_WHOUT_H__ */
  35528. diff -Naur null/fs/aufs/wkq.c linux-6.6.63/fs/aufs/wkq.c
  35529. --- /dev/null
  35530. +++ linux-6.6.63/fs/aufs/wkq.c 2024-12-18 15:12:59.300519526 +0300
  35531. @@ -0,0 +1,370 @@
  35532. +// SPDX-License-Identifier: GPL-2.0
  35533. +/*
  35534. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  35535. + *
  35536. + * This program is free software; you can redistribute it and/or modify
  35537. + * it under the terms of the GNU General Public License as published by
  35538. + * the Free Software Foundation; either version 2 of the License, or
  35539. + * (at your option) any later version.
  35540. + *
  35541. + * This program is distributed in the hope that it will be useful,
  35542. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35543. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35544. + * GNU General Public License for more details.
  35545. + *
  35546. + * You should have received a copy of the GNU General Public License
  35547. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35548. + */
  35549. +
  35550. +/*
  35551. + * workqueue for asynchronous/super-io operations
  35552. + * todo: try new credential scheme
  35553. + */
  35554. +
  35555. +#include <linux/module.h>
  35556. +#include "aufs.h"
  35557. +
  35558. +/* internal workqueue named AUFS_WKQ_NAME */
  35559. +
  35560. +static struct workqueue_struct *au_wkq;
  35561. +
  35562. +struct au_wkinfo {
  35563. + struct work_struct wk;
  35564. + struct kobject *kobj;
  35565. +
  35566. + unsigned int flags; /* see wkq.h */
  35567. +
  35568. + au_wkq_func_t func;
  35569. + void *args;
  35570. +
  35571. +#ifdef CONFIG_LOCKDEP
  35572. + int dont_check;
  35573. + struct held_lock **hlock;
  35574. +#endif
  35575. +
  35576. + struct completion *comp;
  35577. +};
  35578. +
  35579. +/* ---------------------------------------------------------------------- */
  35580. +/*
  35581. + * Aufs passes some operations to the workqueue such as the internal copyup.
  35582. + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
  35583. + * job run by workqueue depends upon the locks acquired in the other task.
  35584. + * Delegating a small operation to the workqueue, aufs passes its lockdep
  35585. + * information too. And the job in the workqueue restores the info in order to
  35586. + * pretend as if it acquired those locks. This is just to make LOCKDEP work
  35587. + * correctly and expectedly.
  35588. + */
  35589. +
  35590. +#ifndef CONFIG_LOCKDEP
  35591. +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
  35592. +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
  35593. +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
  35594. +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
  35595. +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
  35596. +#else
  35597. +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
  35598. +{
  35599. + wkinfo->hlock = NULL;
  35600. + wkinfo->dont_check = 0;
  35601. +}
  35602. +
  35603. +/*
  35604. + * 1: matched
  35605. + * 0: unmatched
  35606. + */
  35607. +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
  35608. +{
  35609. + static DEFINE_SPINLOCK(spin);
  35610. + static struct {
  35611. + char *name;
  35612. + struct lock_class_key *key;
  35613. + } a[] = {
  35614. + { .name = "&sbinfo->si_rwsem" },
  35615. + { .name = "&finfo->fi_rwsem" },
  35616. + { .name = "&dinfo->di_rwsem" },
  35617. + { .name = "&iinfo->ii_rwsem" }
  35618. + };
  35619. + static int set;
  35620. + int i;
  35621. +
  35622. + /* lockless read from 'set.' see below */
  35623. + if (set == ARRAY_SIZE(a)) {
  35624. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35625. + if (a[i].key == key)
  35626. + goto match;
  35627. + goto unmatch;
  35628. + }
  35629. +
  35630. + spin_lock(&spin);
  35631. + if (set)
  35632. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35633. + if (a[i].key == key) {
  35634. + spin_unlock(&spin);
  35635. + goto match;
  35636. + }
  35637. + for (i = 0; i < ARRAY_SIZE(a); i++) {
  35638. + if (a[i].key) {
  35639. + if (unlikely(a[i].key == key)) { /* rare but possible */
  35640. + spin_unlock(&spin);
  35641. + goto match;
  35642. + } else
  35643. + continue;
  35644. + }
  35645. + if (strstr(a[i].name, name)) {
  35646. + /*
  35647. + * the order of these three lines is important for the
  35648. + * lockless read above.
  35649. + */
  35650. + a[i].key = key;
  35651. + spin_unlock(&spin);
  35652. + set++;
  35653. + /* AuDbg("%d, %s\n", set, name); */
  35654. + goto match;
  35655. + }
  35656. + }
  35657. + spin_unlock(&spin);
  35658. + goto unmatch;
  35659. +
  35660. +match:
  35661. + return 1;
  35662. +unmatch:
  35663. + return 0;
  35664. +}
  35665. +
  35666. +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
  35667. +{
  35668. + int err, n;
  35669. + struct task_struct *curr;
  35670. + struct held_lock **hl, *held_locks, *p;
  35671. +
  35672. + err = 0;
  35673. + curr = current;
  35674. + wkinfo->dont_check = lockdep_recursing(curr);
  35675. + if (wkinfo->dont_check)
  35676. + goto out;
  35677. + n = curr->lockdep_depth;
  35678. + if (!n)
  35679. + goto out;
  35680. +
  35681. + err = -ENOMEM;
  35682. + wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
  35683. + if (unlikely(!wkinfo->hlock))
  35684. + goto out;
  35685. +
  35686. + err = 0;
  35687. +#if 0 /* left for debugging */
  35688. + if (0 && au_debug_test())
  35689. + lockdep_print_held_locks(curr);
  35690. +#endif
  35691. + held_locks = curr->held_locks;
  35692. + hl = wkinfo->hlock;
  35693. + while (n--) {
  35694. + p = held_locks++;
  35695. + if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
  35696. + *hl++ = p;
  35697. + }
  35698. + *hl = NULL;
  35699. +
  35700. +out:
  35701. + return err;
  35702. +}
  35703. +
  35704. +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
  35705. +{
  35706. + au_kfree_try_rcu(wkinfo->hlock);
  35707. +}
  35708. +
  35709. +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
  35710. +{
  35711. + struct held_lock *p, **hl = wkinfo->hlock;
  35712. + int subclass;
  35713. +
  35714. + if (wkinfo->dont_check)
  35715. + lockdep_off();
  35716. + if (!hl)
  35717. + return;
  35718. + while ((p = *hl++)) { /* assignment */
  35719. + subclass = lockdep_hlock_class(p)->subclass;
  35720. + /* AuDbg("%s, %d\n", p->instance->name, subclass); */
  35721. + if (p->read)
  35722. + rwsem_acquire_read(p->instance, subclass, 0,
  35723. + /*p->acquire_ip*/_RET_IP_);
  35724. + else
  35725. + rwsem_acquire(p->instance, subclass, 0,
  35726. + /*p->acquire_ip*/_RET_IP_);
  35727. + }
  35728. +}
  35729. +
  35730. +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
  35731. +{
  35732. + struct held_lock *p, **hl = wkinfo->hlock;
  35733. +
  35734. + if (wkinfo->dont_check)
  35735. + lockdep_on();
  35736. + if (!hl)
  35737. + return;
  35738. + while ((p = *hl++)) /* assignment */
  35739. + rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
  35740. +}
  35741. +#endif
  35742. +
  35743. +static void wkq_func(struct work_struct *wk)
  35744. +{
  35745. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  35746. +
  35747. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  35748. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  35749. +
  35750. + au_wkq_lockdep_pre(wkinfo);
  35751. + wkinfo->func(wkinfo->args);
  35752. + au_wkq_lockdep_post(wkinfo);
  35753. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  35754. + complete(wkinfo->comp);
  35755. + else {
  35756. + kobject_put(wkinfo->kobj);
  35757. + module_put(THIS_MODULE); /* todo: ?? */
  35758. + au_kfree_rcu(wkinfo);
  35759. + }
  35760. +}
  35761. +
  35762. +/*
  35763. + * Since struct completion is large, try allocating it dynamically.
  35764. + */
  35765. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  35766. +{
  35767. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  35768. + if (*comp) {
  35769. + init_completion(*comp);
  35770. + wkinfo->comp = *comp;
  35771. + return 0;
  35772. + }
  35773. + return -ENOMEM;
  35774. +}
  35775. +
  35776. +static void au_wkq_comp_free(struct completion *comp)
  35777. +{
  35778. + au_kfree_rcu(comp);
  35779. +}
  35780. +
  35781. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  35782. +{
  35783. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  35784. + if (au_wkq_test()) {
  35785. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  35786. + " due to a dead dir by UDBA,"
  35787. + " or async xino write?\n");
  35788. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  35789. + }
  35790. + } else
  35791. + au_dbg_verify_kthread();
  35792. +
  35793. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  35794. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  35795. + queue_work(au_wkq, &wkinfo->wk);
  35796. + } else {
  35797. + INIT_WORK(&wkinfo->wk, wkq_func);
  35798. + schedule_work(&wkinfo->wk);
  35799. + }
  35800. +}
  35801. +
  35802. +/*
  35803. + * Be careful. It is easy to make deadlock happen.
  35804. + * processA: lock, wkq and wait
  35805. + * processB: wkq and wait, lock in wkq
  35806. + * --> deadlock
  35807. + */
  35808. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  35809. +{
  35810. + int err;
  35811. + struct completion *comp = NULL;
  35812. + struct au_wkinfo wkinfo = {
  35813. + .flags = flags,
  35814. + .func = func,
  35815. + .args = args
  35816. + };
  35817. +
  35818. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  35819. + if (unlikely(err))
  35820. + goto out;
  35821. + err = au_wkq_lockdep_alloc(&wkinfo);
  35822. + if (unlikely(err))
  35823. + goto out_comp;
  35824. + if (!err) {
  35825. + au_wkq_run(&wkinfo);
  35826. + /* no timeout, no interrupt */
  35827. + wait_for_completion(wkinfo.comp);
  35828. + }
  35829. + au_wkq_lockdep_free(&wkinfo);
  35830. +
  35831. +out_comp:
  35832. + au_wkq_comp_free(comp);
  35833. +out:
  35834. + destroy_work_on_stack(&wkinfo.wk);
  35835. + return err;
  35836. +}
  35837. +
  35838. +/*
  35839. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  35840. + * problem in a concurrent umounting.
  35841. + */
  35842. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35843. + unsigned int flags)
  35844. +{
  35845. + int err;
  35846. + struct au_wkinfo *wkinfo;
  35847. +
  35848. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  35849. +
  35850. + /*
  35851. + * wkq_func() must free this wkinfo.
  35852. + * it highly depends upon the implementation of workqueue.
  35853. + */
  35854. + err = 0;
  35855. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  35856. + if (wkinfo) {
  35857. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  35858. + wkinfo->flags = flags & ~AuWkq_WAIT;
  35859. + wkinfo->func = func;
  35860. + wkinfo->args = args;
  35861. + wkinfo->comp = NULL;
  35862. + au_wkq_lockdep_init(wkinfo);
  35863. + kobject_get(wkinfo->kobj);
  35864. + __module_get(THIS_MODULE); /* todo: ?? */
  35865. +
  35866. + au_wkq_run(wkinfo);
  35867. + } else {
  35868. + err = -ENOMEM;
  35869. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35870. + }
  35871. +
  35872. + return err;
  35873. +}
  35874. +
  35875. +/* ---------------------------------------------------------------------- */
  35876. +
  35877. +void au_nwt_init(struct au_nowait_tasks *nwt)
  35878. +{
  35879. + atomic_set(&nwt->nw_len, 0);
  35880. + /* smp_mb(); */ /* atomic_set */
  35881. + init_waitqueue_head(&nwt->nw_wq);
  35882. +}
  35883. +
  35884. +void au_wkq_fin(void)
  35885. +{
  35886. + destroy_workqueue(au_wkq);
  35887. +}
  35888. +
  35889. +int __init au_wkq_init(void)
  35890. +{
  35891. + int err;
  35892. +
  35893. + err = 0;
  35894. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  35895. + if (IS_ERR(au_wkq))
  35896. + err = PTR_ERR(au_wkq);
  35897. + else if (!au_wkq)
  35898. + err = -ENOMEM;
  35899. +
  35900. + return err;
  35901. +}
  35902. diff -Naur null/fs/aufs/wkq.h linux-6.6.63/fs/aufs/wkq.h
  35903. --- /dev/null
  35904. +++ linux-6.6.63/fs/aufs/wkq.h 2024-12-18 15:12:59.300519526 +0300
  35905. @@ -0,0 +1,89 @@
  35906. +/* SPDX-License-Identifier: GPL-2.0 */
  35907. +/*
  35908. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  35909. + *
  35910. + * This program is free software; you can redistribute it and/or modify
  35911. + * it under the terms of the GNU General Public License as published by
  35912. + * the Free Software Foundation; either version 2 of the License, or
  35913. + * (at your option) any later version.
  35914. + *
  35915. + * This program is distributed in the hope that it will be useful,
  35916. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35917. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35918. + * GNU General Public License for more details.
  35919. + *
  35920. + * You should have received a copy of the GNU General Public License
  35921. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35922. + */
  35923. +
  35924. +/*
  35925. + * workqueue for asynchronous/super-io operations
  35926. + * todo: try new credentials management scheme
  35927. + */
  35928. +
  35929. +#ifndef __AUFS_WKQ_H__
  35930. +#define __AUFS_WKQ_H__
  35931. +
  35932. +#ifdef __KERNEL__
  35933. +
  35934. +#include <linux/wait.h>
  35935. +
  35936. +struct super_block;
  35937. +
  35938. +/* ---------------------------------------------------------------------- */
  35939. +
  35940. +/*
  35941. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  35942. + */
  35943. +struct au_nowait_tasks {
  35944. + atomic_t nw_len;
  35945. + wait_queue_head_t nw_wq;
  35946. +};
  35947. +
  35948. +/* ---------------------------------------------------------------------- */
  35949. +
  35950. +typedef void (*au_wkq_func_t)(void *args);
  35951. +
  35952. +/* wkq flags */
  35953. +#define AuWkq_WAIT 1
  35954. +#define AuWkq_NEST (1 << 1)
  35955. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  35956. +#define au_fset_wkq(flags, name) \
  35957. + do { (flags) |= AuWkq_##name; } while (0)
  35958. +#define au_fclr_wkq(flags, name) \
  35959. + do { (flags) &= ~AuWkq_##name; } while (0)
  35960. +
  35961. +/* wkq.c */
  35962. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  35963. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35964. + unsigned int flags);
  35965. +void au_nwt_init(struct au_nowait_tasks *nwt);
  35966. +int __init au_wkq_init(void);
  35967. +void au_wkq_fin(void);
  35968. +
  35969. +/* ---------------------------------------------------------------------- */
  35970. +
  35971. +static inline int au_wkq_test(void)
  35972. +{
  35973. + return current->flags & PF_WQ_WORKER;
  35974. +}
  35975. +
  35976. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  35977. +{
  35978. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  35979. +}
  35980. +
  35981. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  35982. +{
  35983. + if (atomic_dec_and_test(&nwt->nw_len))
  35984. + wake_up_all(&nwt->nw_wq);
  35985. +}
  35986. +
  35987. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  35988. +{
  35989. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  35990. + return 0;
  35991. +}
  35992. +
  35993. +#endif /* __KERNEL__ */
  35994. +#endif /* __AUFS_WKQ_H__ */
  35995. diff -Naur null/fs/aufs/xattr.c linux-6.6.63/fs/aufs/xattr.c
  35996. --- /dev/null
  35997. +++ linux-6.6.63/fs/aufs/xattr.c 2024-12-18 15:12:59.301519513 +0300
  35998. @@ -0,0 +1,360 @@
  35999. +// SPDX-License-Identifier: GPL-2.0
  36000. +/*
  36001. + * Copyright (C) 2014-2022 Junjiro R. Okajima
  36002. + *
  36003. + * This program is free software; you can redistribute it and/or modify
  36004. + * it under the terms of the GNU General Public License as published by
  36005. + * the Free Software Foundation; either version 2 of the License, or
  36006. + * (at your option) any later version.
  36007. + *
  36008. + * This program is distributed in the hope that it will be useful,
  36009. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36010. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36011. + * GNU General Public License for more details.
  36012. + *
  36013. + * You should have received a copy of the GNU General Public License
  36014. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36015. + */
  36016. +
  36017. +/*
  36018. + * handling xattr functions
  36019. + */
  36020. +
  36021. +#include <linux/fs.h>
  36022. +#include <linux/xattr.h>
  36023. +#include "aufs.h"
  36024. +
  36025. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  36026. +{
  36027. + if (!ignore_flags)
  36028. + goto out;
  36029. + switch (err) {
  36030. + case -ENOMEM:
  36031. + case -EDQUOT:
  36032. + goto out;
  36033. + }
  36034. +
  36035. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  36036. + err = 0;
  36037. + goto out;
  36038. + }
  36039. +
  36040. +#define cmp(brattr, prefix) do { \
  36041. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  36042. + XATTR_##prefix##_PREFIX_LEN)) { \
  36043. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  36044. + err = 0; \
  36045. + goto out; \
  36046. + } \
  36047. + } while (0)
  36048. +
  36049. + cmp(SEC, SECURITY);
  36050. + cmp(SYS, SYSTEM);
  36051. + cmp(TR, TRUSTED);
  36052. + cmp(USR, USER);
  36053. +#undef cmp
  36054. +
  36055. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  36056. + err = 0;
  36057. +
  36058. +out:
  36059. + return err;
  36060. +}
  36061. +
  36062. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  36063. +
  36064. +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
  36065. + char *name, char **buf, unsigned int ignore_flags,
  36066. + unsigned int verbose)
  36067. +{
  36068. + int err, is_acl;
  36069. + ssize_t ssz;
  36070. + struct inode *h_idst;
  36071. + struct dentry *h_dst_dentry, *h_src_dentry;
  36072. + struct mnt_idmap *h_dst_idmap, *h_src_idmap;
  36073. + struct posix_acl *acl;
  36074. +
  36075. + is_acl = !!is_posix_acl_xattr(name);
  36076. + h_src_idmap = mnt_idmap(h_src->mnt);
  36077. + h_src_dentry = h_src->dentry;
  36078. + if (is_acl) {
  36079. + acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
  36080. + AuDebugOn(!acl);
  36081. + if (IS_ERR(acl)) {
  36082. + err = PTR_ERR(acl);
  36083. + if (err == -ENODATA)
  36084. + err = 0;
  36085. + else if (err == -EOPNOTSUPP
  36086. + && au_test_nfs_noacl(d_inode(h_src_dentry)))
  36087. + err = 0;
  36088. + else if (verbose || au_debug_test())
  36089. + pr_err("%s, err %d\n", name, err);
  36090. + goto out;
  36091. + }
  36092. + } else {
  36093. + ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
  36094. + 0, GFP_NOFS);
  36095. + if (unlikely(ssz <= 0)) {
  36096. + err = ssz;
  36097. + if (err == -ENODATA)
  36098. + err = 0;
  36099. + else if (err == -EOPNOTSUPP
  36100. + && (ignore_flags & au_xattr_out_of_list))
  36101. + err = 0;
  36102. + else if (err && (verbose || au_debug_test()))
  36103. + pr_err("%s, err %d\n", name, err);
  36104. + goto out;
  36105. + }
  36106. + }
  36107. +
  36108. + /* unlock it temporary */
  36109. + h_dst_idmap = mnt_idmap(h_dst->mnt);
  36110. + h_dst_dentry = h_dst->dentry;
  36111. + h_idst = d_inode(h_dst_dentry);
  36112. + inode_unlock(h_idst);
  36113. + if (is_acl) {
  36114. + err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
  36115. + posix_acl_release(acl);
  36116. + } else
  36117. + err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
  36118. + ssz, /*flags*/0);
  36119. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  36120. + if (unlikely(err)) {
  36121. + if (verbose || au_debug_test())
  36122. + pr_err("%s, err %d\n", name, err);
  36123. + err = au_xattr_ignore(err, name, ignore_flags);
  36124. + }
  36125. +
  36126. +out:
  36127. + return err;
  36128. +}
  36129. +
  36130. +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
  36131. + unsigned int verbose)
  36132. +{
  36133. + int err, unlocked;
  36134. + ssize_t ssz;
  36135. + struct dentry *h_dst_dentry, *h_src_dentry;
  36136. + struct inode *h_isrc, *h_idst;
  36137. + char *value, *p, *o, *e;
  36138. +
  36139. + /* try stopping to update the source inode while we are referencing */
  36140. + /* there should not be the parent-child relationship between them */
  36141. + h_dst_dentry = h_dst->dentry;
  36142. + h_idst = d_inode(h_dst_dentry);
  36143. + h_src_dentry = h_src->dentry;
  36144. + h_isrc = d_inode(h_src_dentry);
  36145. + inode_unlock(h_idst);
  36146. + inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
  36147. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  36148. + unlocked = 0;
  36149. +
  36150. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  36151. + ssz = vfs_listxattr(h_src_dentry, NULL, 0);
  36152. + err = ssz;
  36153. + if (unlikely(err < 0)) {
  36154. + AuTraceErr(err);
  36155. + if (err == -ENODATA
  36156. + || err == -EOPNOTSUPP)
  36157. + err = 0; /* ignore */
  36158. + goto out;
  36159. + }
  36160. +
  36161. + err = 0;
  36162. + p = NULL;
  36163. + o = NULL;
  36164. + if (ssz) {
  36165. + err = -ENOMEM;
  36166. + p = kmalloc(ssz, GFP_NOFS);
  36167. + o = p;
  36168. + if (unlikely(!p))
  36169. + goto out;
  36170. + err = vfs_listxattr(h_src_dentry, p, ssz);
  36171. + }
  36172. + inode_unlock_shared(h_isrc);
  36173. + unlocked = 1;
  36174. + AuDbg("err %d, ssz %zd\n", err, ssz);
  36175. + if (unlikely(err < 0))
  36176. + goto out_free;
  36177. +
  36178. + err = 0;
  36179. + e = p + ssz;
  36180. + value = NULL;
  36181. + ignore_flags |= au_xattr_out_of_list;
  36182. + while (!err && p < e) {
  36183. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  36184. + verbose);
  36185. + p += strlen(p) + 1;
  36186. + }
  36187. + au_kfree_try_rcu(value);
  36188. +
  36189. +out_free:
  36190. + au_kfree_try_rcu(o);
  36191. +out:
  36192. + if (!unlocked)
  36193. + inode_unlock_shared(h_isrc);
  36194. + AuTraceErr(err);
  36195. + return err;
  36196. +}
  36197. +
  36198. +/* ---------------------------------------------------------------------- */
  36199. +
  36200. +static int au_smack_reentering(struct super_block *sb)
  36201. +{
  36202. +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
  36203. + /*
  36204. + * as a part of lookup, smack_d_instantiate() is called, and it calls
  36205. + * i_op->getxattr(). ouch.
  36206. + */
  36207. + return si_pid_test(sb);
  36208. +#else
  36209. + return 0;
  36210. +#endif
  36211. +}
  36212. +
  36213. +enum {
  36214. + AU_XATTR_LIST,
  36215. + AU_XATTR_GET
  36216. +};
  36217. +
  36218. +struct au_lgxattr {
  36219. + int type;
  36220. + union {
  36221. + struct {
  36222. + char *list;
  36223. + size_t size;
  36224. + } list;
  36225. + struct {
  36226. + const char *name;
  36227. + void *value;
  36228. + size_t size;
  36229. + } get;
  36230. + } u;
  36231. +};
  36232. +
  36233. +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
  36234. + struct au_lgxattr *arg)
  36235. +{
  36236. + ssize_t err;
  36237. + int reenter;
  36238. + struct path h_path;
  36239. + struct super_block *sb;
  36240. +
  36241. + sb = dentry->d_sb;
  36242. + reenter = au_smack_reentering(sb);
  36243. + if (!reenter) {
  36244. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  36245. + if (unlikely(err))
  36246. + goto out;
  36247. + }
  36248. + err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
  36249. + if (unlikely(err))
  36250. + goto out_si;
  36251. + if (unlikely(!h_path.dentry))
  36252. + /* illegally overlapped or something */
  36253. + goto out_di; /* pretending success */
  36254. +
  36255. + /* always topmost entry only */
  36256. + switch (arg->type) {
  36257. + case AU_XATTR_LIST:
  36258. + err = vfs_listxattr(h_path.dentry,
  36259. + arg->u.list.list, arg->u.list.size);
  36260. + break;
  36261. + case AU_XATTR_GET:
  36262. + AuDebugOn(d_is_negative(h_path.dentry));
  36263. + err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
  36264. + arg->u.get.name, arg->u.get.value,
  36265. + arg->u.get.size);
  36266. + break;
  36267. + }
  36268. +
  36269. +out_di:
  36270. + if (!reenter)
  36271. + di_read_unlock(dentry, AuLock_IR);
  36272. +out_si:
  36273. + if (!reenter)
  36274. + si_read_unlock(sb);
  36275. +out:
  36276. + AuTraceErr(err);
  36277. + return err;
  36278. +}
  36279. +
  36280. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  36281. +{
  36282. + struct au_lgxattr arg = {
  36283. + .type = AU_XATTR_LIST,
  36284. + .u.list = {
  36285. + .list = list,
  36286. + .size = size
  36287. + },
  36288. + };
  36289. +
  36290. + return au_lgxattr(dentry, /*inode*/NULL, &arg);
  36291. +}
  36292. +
  36293. +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
  36294. + const char *name, void *value, size_t size)
  36295. +{
  36296. + struct au_lgxattr arg = {
  36297. + .type = AU_XATTR_GET,
  36298. + .u.get = {
  36299. + .name = name,
  36300. + .value = value,
  36301. + .size = size
  36302. + },
  36303. + };
  36304. +
  36305. + return au_lgxattr(dentry, inode, &arg);
  36306. +}
  36307. +
  36308. +static int au_setxattr(struct dentry *dentry, struct inode *inode,
  36309. + const char *name, const void *value, size_t size,
  36310. + int flags)
  36311. +{
  36312. + struct au_sxattr arg = {
  36313. + .type = AU_XATTR_SET,
  36314. + .u.set = {
  36315. + .name = name,
  36316. + .value = value,
  36317. + .size = size,
  36318. + .flags = flags
  36319. + },
  36320. + };
  36321. +
  36322. + return au_sxattr(dentry, inode, &arg);
  36323. +}
  36324. +
  36325. +/* ---------------------------------------------------------------------- */
  36326. +
  36327. +static int au_xattr_get(const struct xattr_handler *handler,
  36328. + struct dentry *dentry, struct inode *inode,
  36329. + const char *name, void *buffer, size_t size)
  36330. +{
  36331. + return au_getxattr(dentry, inode, name, buffer, size);
  36332. +}
  36333. +
  36334. +static int au_xattr_set(const struct xattr_handler *handler,
  36335. + struct mnt_idmap *idmap,
  36336. + struct dentry *dentry, struct inode *inode,
  36337. + const char *name, const void *value, size_t size,
  36338. + int flags)
  36339. +{
  36340. + return au_setxattr(dentry, inode, name, value, size, flags);
  36341. +}
  36342. +
  36343. +static const struct xattr_handler au_xattr_handler = {
  36344. + .name = "",
  36345. + .prefix = "",
  36346. + .get = au_xattr_get,
  36347. + .set = au_xattr_set
  36348. +};
  36349. +
  36350. +static const struct xattr_handler *au_xattr_handlers[] = {
  36351. + &au_xattr_handler, /* must be last */
  36352. + NULL
  36353. +};
  36354. +
  36355. +void au_xattr_init(struct super_block *sb)
  36356. +{
  36357. + sb->s_xattr = au_xattr_handlers;
  36358. +}
  36359. diff -Naur null/fs/aufs/xino.c linux-6.6.63/fs/aufs/xino.c
  36360. --- /dev/null
  36361. +++ linux-6.6.63/fs/aufs/xino.c 2024-12-18 15:12:59.301519513 +0300
  36362. @@ -0,0 +1,1926 @@
  36363. +// SPDX-License-Identifier: GPL-2.0
  36364. +/*
  36365. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  36366. + *
  36367. + * This program is free software; you can redistribute it and/or modify
  36368. + * it under the terms of the GNU General Public License as published by
  36369. + * the Free Software Foundation; either version 2 of the License, or
  36370. + * (at your option) any later version.
  36371. + *
  36372. + * This program is distributed in the hope that it will be useful,
  36373. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36374. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36375. + * GNU General Public License for more details.
  36376. + *
  36377. + * You should have received a copy of the GNU General Public License
  36378. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36379. + */
  36380. +
  36381. +/*
  36382. + * external inode number translation table and bitmap
  36383. + *
  36384. + * things to consider
  36385. + * - the lifetime
  36386. + * + au_xino object
  36387. + * + XINO files (xino, xib, xigen)
  36388. + * + dynamic debugfs entries (xiN)
  36389. + * + static debugfs entries (xib, xigen)
  36390. + * + static sysfs entry (xi_path)
  36391. + * - several entry points to handle them.
  36392. + * + mount(2) without xino option (default)
  36393. + * + mount(2) with xino option
  36394. + * + mount(2) with noxino option
  36395. + * + umount(2)
  36396. + * + remount with add/del branches
  36397. + * + remount with xino/noxino options
  36398. + */
  36399. +
  36400. +#include <linux/seq_file.h>
  36401. +#include <linux/statfs.h>
  36402. +#include "aufs.h"
  36403. +
  36404. +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
  36405. + aufs_bindex_t bbot,
  36406. + struct super_block *h_sb)
  36407. +{
  36408. + /* todo: try binary-search if the branches are many */
  36409. + for (; btop <= bbot; btop++)
  36410. + if (h_sb == au_sbr_sb(sb, btop))
  36411. + return btop;
  36412. + return -1;
  36413. +}
  36414. +
  36415. +/*
  36416. + * find another branch who is on the same filesystem of the specified
  36417. + * branch{@btgt}. search until @bbot.
  36418. + */
  36419. +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  36420. + aufs_bindex_t bbot)
  36421. +{
  36422. + aufs_bindex_t bindex;
  36423. + struct super_block *tgt_sb;
  36424. +
  36425. + tgt_sb = au_sbr_sb(sb, btgt);
  36426. + bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
  36427. + if (bindex < 0)
  36428. + bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
  36429. +
  36430. + return bindex;
  36431. +}
  36432. +
  36433. +/* ---------------------------------------------------------------------- */
  36434. +
  36435. +/*
  36436. + * stop unnecessary notify events at creating xino files
  36437. + */
  36438. +
  36439. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
  36440. +{
  36441. + aufs_bindex_t bfound, bindex, bbot;
  36442. + struct dentry *parent;
  36443. + struct au_branch *br;
  36444. +
  36445. + bfound = -1;
  36446. + parent = dentry->d_parent; /* safe d_parent access */
  36447. + bbot = au_sbbot(sb);
  36448. + for (bindex = 0; bindex <= bbot; bindex++) {
  36449. + br = au_sbr(sb, bindex);
  36450. + if (au_br_dentry(br) == parent) {
  36451. + bfound = bindex;
  36452. + break;
  36453. + }
  36454. + }
  36455. +
  36456. + AuDbg("bfound b%d\n", bfound);
  36457. + return bfound;
  36458. +}
  36459. +
  36460. +struct au_xino_lock_dir {
  36461. + struct au_hinode *hdir;
  36462. + struct dentry *parent;
  36463. + struct inode *dir;
  36464. +};
  36465. +
  36466. +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
  36467. + unsigned int lsc)
  36468. +{
  36469. + struct dentry *parent;
  36470. + struct inode *dir;
  36471. +
  36472. + parent = dget_parent(dentry);
  36473. + dir = d_inode(parent);
  36474. + inode_lock_nested(dir, lsc);
  36475. +#if 0 /* it should not happen */
  36476. + spin_lock(&dentry->d_lock);
  36477. + if (unlikely(dentry->d_parent != parent)) {
  36478. + spin_unlock(&dentry->d_lock);
  36479. + inode_unlock(dir);
  36480. + dput(parent);
  36481. + parent = NULL;
  36482. + goto out;
  36483. + }
  36484. + spin_unlock(&dentry->d_lock);
  36485. +
  36486. +out:
  36487. +#endif
  36488. + return parent;
  36489. +}
  36490. +
  36491. +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
  36492. + struct au_xino_lock_dir *ldir)
  36493. +{
  36494. + aufs_bindex_t bindex;
  36495. +
  36496. + ldir->hdir = NULL;
  36497. + bindex = au_xi_root(sb, xipath->dentry);
  36498. + if (bindex >= 0) {
  36499. + /* rw branch root */
  36500. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  36501. + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  36502. + } else {
  36503. + /* other */
  36504. + ldir->parent = au_dget_parent_lock(xipath->dentry,
  36505. + AuLsc_I_PARENT);
  36506. + ldir->dir = d_inode(ldir->parent);
  36507. + }
  36508. +}
  36509. +
  36510. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  36511. +{
  36512. + if (ldir->hdir)
  36513. + au_hn_inode_unlock(ldir->hdir);
  36514. + else {
  36515. + inode_unlock(ldir->dir);
  36516. + dput(ldir->parent);
  36517. + }
  36518. +}
  36519. +
  36520. +/* ---------------------------------------------------------------------- */
  36521. +
  36522. +/*
  36523. + * create and set a new xino file
  36524. + */
  36525. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
  36526. + int wbrtop)
  36527. +{
  36528. + struct file *file;
  36529. + struct dentry *h_parent, *d;
  36530. + struct inode *h_dir, *inode;
  36531. + int err;
  36532. + static DEFINE_MUTEX(mtx);
  36533. +
  36534. + /*
  36535. + * at mount-time, and the xino file is the default path,
  36536. + * hnotify is disabled so we have no notify events to ignore.
  36537. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  36538. + */
  36539. + if (!wbrtop)
  36540. + mutex_lock(&mtx);
  36541. + file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  36542. + /* | __FMODE_NONOTIFY */,
  36543. + 0666);
  36544. + if (IS_ERR(file)) {
  36545. + if (!wbrtop)
  36546. + mutex_unlock(&mtx);
  36547. + if (!silent)
  36548. + pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
  36549. + return file;
  36550. + }
  36551. +
  36552. + /* keep file count */
  36553. + err = 0;
  36554. + d = file->f_path.dentry;
  36555. + h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
  36556. + if (!wbrtop)
  36557. + mutex_unlock(&mtx);
  36558. + /* mnt_want_write() is unnecessary here */
  36559. + h_dir = d_inode(h_parent);
  36560. + inode = file_inode(file);
  36561. + /* no delegation since it is just created */
  36562. + if (vfsub_inode_nlink(inode, AU_I_BRANCH))
  36563. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  36564. + /*force*/0);
  36565. + inode_unlock(h_dir);
  36566. + dput(h_parent);
  36567. + if (unlikely(err)) {
  36568. + if (!silent)
  36569. + pr_err("unlink %s(%d)\n", fpath, err);
  36570. + goto out;
  36571. + }
  36572. +
  36573. + err = -EINVAL;
  36574. + if (unlikely(sb && sb == d->d_sb)) {
  36575. + if (!silent)
  36576. + pr_err("%s must be outside\n", fpath);
  36577. + goto out;
  36578. + }
  36579. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  36580. + if (!silent)
  36581. + pr_err("xino doesn't support %s(%s)\n",
  36582. + fpath, au_sbtype(d->d_sb));
  36583. + goto out;
  36584. + }
  36585. + return file; /* success */
  36586. +
  36587. +out:
  36588. + fput(file);
  36589. + file = ERR_PTR(err);
  36590. + return file;
  36591. +}
  36592. +
  36593. +/*
  36594. + * create a new xinofile at the same place/path as @base.
  36595. + */
  36596. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  36597. + struct file *copy_src)
  36598. +{
  36599. + struct file *file;
  36600. + struct dentry *dentry;
  36601. + struct inode *dir, *delegated;
  36602. + struct qstr *name;
  36603. + struct path ppath, path;
  36604. + int err, do_unlock;
  36605. + struct au_xino_lock_dir ldir;
  36606. +
  36607. + do_unlock = 1;
  36608. + au_xino_lock_dir(sb, base, &ldir);
  36609. + dentry = base->dentry;
  36610. + ppath.dentry = dentry->d_parent; /* dir inode is locked */
  36611. + ppath.mnt = base->mnt;
  36612. + dir = d_inode(ppath.dentry);
  36613. + IMustLock(dir);
  36614. +
  36615. + name = &dentry->d_name;
  36616. + path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
  36617. + if (IS_ERR(path.dentry)) {
  36618. + file = (void *)path.dentry;
  36619. + pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
  36620. + goto out;
  36621. + }
  36622. +
  36623. + /* no need to mnt_want_write() since we call dentry_open() later */
  36624. + err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
  36625. + if (unlikely(err)) {
  36626. + file = ERR_PTR(err);
  36627. + pr_err("%pd create err %d\n", dentry, err);
  36628. + goto out_dput;
  36629. + }
  36630. +
  36631. + path.mnt = base->mnt;
  36632. + file = vfsub_dentry_open(&path,
  36633. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  36634. + /* | __FMODE_NONOTIFY */);
  36635. + if (IS_ERR(file)) {
  36636. + pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
  36637. + goto out_dput;
  36638. + }
  36639. +
  36640. + delegated = NULL;
  36641. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  36642. + au_xino_unlock_dir(&ldir);
  36643. + do_unlock = 0;
  36644. + if (unlikely(err == -EWOULDBLOCK)) {
  36645. + pr_warn("cannot retry for NFSv4 delegation"
  36646. + " for an internal unlink\n");
  36647. + iput(delegated);
  36648. + }
  36649. + if (unlikely(err)) {
  36650. + pr_err("%pd unlink err %d\n", dentry, err);
  36651. + goto out_fput;
  36652. + }
  36653. +
  36654. + if (copy_src) {
  36655. + /* no one can touch copy_src xino */
  36656. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  36657. + if (unlikely(err)) {
  36658. + pr_err("%pd copy err %d\n", dentry, err);
  36659. + goto out_fput;
  36660. + }
  36661. + }
  36662. + goto out_dput; /* success */
  36663. +
  36664. +out_fput:
  36665. + fput(file);
  36666. + file = ERR_PTR(err);
  36667. +out_dput:
  36668. + dput(path.dentry);
  36669. +out:
  36670. + if (do_unlock)
  36671. + au_xino_unlock_dir(&ldir);
  36672. + return file;
  36673. +}
  36674. +
  36675. +struct file *au_xino_file1(struct au_xino *xi)
  36676. +{
  36677. + struct file *file;
  36678. + unsigned int u, nfile;
  36679. +
  36680. + file = NULL;
  36681. + nfile = xi->xi_nfile;
  36682. + for (u = 0; u < nfile; u++) {
  36683. + file = xi->xi_file[u];
  36684. + if (file)
  36685. + break;
  36686. + }
  36687. +
  36688. + return file;
  36689. +}
  36690. +
  36691. +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
  36692. +{
  36693. + int err;
  36694. + struct file *f;
  36695. + void *p;
  36696. +
  36697. + if (file)
  36698. + get_file(file);
  36699. +
  36700. + err = 0;
  36701. + f = NULL;
  36702. + if (idx < xi->xi_nfile) {
  36703. + f = xi->xi_file[idx];
  36704. + if (f)
  36705. + fput(f);
  36706. + } else {
  36707. + p = au_kzrealloc(xi->xi_file,
  36708. + sizeof(*xi->xi_file) * xi->xi_nfile,
  36709. + sizeof(*xi->xi_file) * (idx + 1),
  36710. + GFP_NOFS, /*may_shrink*/0);
  36711. + if (p) {
  36712. + MtxMustLock(&xi->xi_mtx);
  36713. + xi->xi_file = p;
  36714. + xi->xi_nfile = idx + 1;
  36715. + } else {
  36716. + err = -ENOMEM;
  36717. + if (file)
  36718. + fput(file);
  36719. + goto out;
  36720. + }
  36721. + }
  36722. + xi->xi_file[idx] = file;
  36723. +
  36724. +out:
  36725. + return err;
  36726. +}
  36727. +
  36728. +/*
  36729. + * if @xinew->xi is not set, then create new xigen file.
  36730. + */
  36731. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
  36732. +{
  36733. + struct file *file;
  36734. + int err;
  36735. +
  36736. + SiMustAnyLock(sb);
  36737. +
  36738. + file = au_xino_create2(sb, xinew->base, xinew->copy_src);
  36739. + if (IS_ERR(file)) {
  36740. + err = PTR_ERR(file);
  36741. + pr_err("%s[%d], err %d\n",
  36742. + xinew->xi ? "xino" : "xigen",
  36743. + xinew->idx, err);
  36744. + goto out;
  36745. + }
  36746. +
  36747. + if (xinew->xi)
  36748. + err = au_xino_file_set(xinew->xi, xinew->idx, file);
  36749. + else {
  36750. + BUG();
  36751. + /* todo: make xigen file an array */
  36752. + /* err = au_xigen_file_set(sb, xinew->idx, file); */
  36753. + }
  36754. + fput(file);
  36755. + if (unlikely(err))
  36756. + file = ERR_PTR(err);
  36757. +
  36758. +out:
  36759. + return file;
  36760. +}
  36761. +
  36762. +/* ---------------------------------------------------------------------- */
  36763. +
  36764. +/*
  36765. + * truncate xino files
  36766. + */
  36767. +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
  36768. + int idx, struct kstatfs *st)
  36769. +{
  36770. + int err;
  36771. + blkcnt_t blocks;
  36772. + struct file *file, *new_xino;
  36773. + struct au_xi_new xinew = {
  36774. + .idx = idx
  36775. + };
  36776. +
  36777. + err = 0;
  36778. + xinew.xi = au_sbr(sb, bindex)->br_xino;
  36779. + file = au_xino_file(xinew.xi, idx);
  36780. + if (!file)
  36781. + goto out;
  36782. +
  36783. + xinew.base = &file->f_path;
  36784. + err = vfs_statfs(xinew.base, st);
  36785. + if (unlikely(err)) {
  36786. + AuErr1("statfs err %d, ignored\n", err);
  36787. + err = 0;
  36788. + goto out;
  36789. + }
  36790. +
  36791. + blocks = file_inode(file)->i_blocks;
  36792. + pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36793. + bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
  36794. +
  36795. + xinew.copy_src = file;
  36796. + new_xino = au_xi_new(sb, &xinew);
  36797. + if (IS_ERR(new_xino)) {
  36798. + err = PTR_ERR(new_xino);
  36799. + pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
  36800. + goto out;
  36801. + }
  36802. +
  36803. + err = vfs_statfs(&new_xino->f_path, st);
  36804. + if (!err)
  36805. + pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36806. + bindex, idx, (u64)file_inode(new_xino)->i_blocks,
  36807. + st->f_bfree, st->f_blocks);
  36808. + else {
  36809. + AuErr1("statfs err %d, ignored\n", err);
  36810. + err = 0;
  36811. + }
  36812. +
  36813. +out:
  36814. + return err;
  36815. +}
  36816. +
  36817. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
  36818. +{
  36819. + int err, i;
  36820. + unsigned long jiffy;
  36821. + aufs_bindex_t bbot;
  36822. + struct kstatfs *st;
  36823. + struct au_branch *br;
  36824. + struct au_xino *xi;
  36825. +
  36826. + err = -ENOMEM;
  36827. + st = kmalloc(sizeof(*st), GFP_NOFS);
  36828. + if (unlikely(!st))
  36829. + goto out;
  36830. +
  36831. + err = -EINVAL;
  36832. + bbot = au_sbbot(sb);
  36833. + if (unlikely(bindex < 0 || bbot < bindex))
  36834. + goto out_st;
  36835. +
  36836. + err = 0;
  36837. + jiffy = jiffies;
  36838. + br = au_sbr(sb, bindex);
  36839. + xi = br->br_xino;
  36840. + for (i = idx_begin; !err && i < xi->xi_nfile; i++)
  36841. + err = au_xino_do_trunc(sb, bindex, i, st);
  36842. + if (!err)
  36843. + au_sbi(sb)->si_xino_jiffy = jiffy;
  36844. +
  36845. +out_st:
  36846. + au_kfree_rcu(st);
  36847. +out:
  36848. + return err;
  36849. +}
  36850. +
  36851. +struct xino_do_trunc_args {
  36852. + struct super_block *sb;
  36853. + struct au_branch *br;
  36854. + int idx;
  36855. +};
  36856. +
  36857. +static void xino_do_trunc(void *_args)
  36858. +{
  36859. + struct xino_do_trunc_args *args = _args;
  36860. + struct super_block *sb;
  36861. + struct au_branch *br;
  36862. + struct inode *dir;
  36863. + int err, idx;
  36864. + aufs_bindex_t bindex;
  36865. +
  36866. + err = 0;
  36867. + sb = args->sb;
  36868. + dir = d_inode(sb->s_root);
  36869. + br = args->br;
  36870. + idx = args->idx;
  36871. +
  36872. + si_noflush_write_lock(sb);
  36873. + ii_read_lock_parent(dir);
  36874. + bindex = au_br_index(sb, br->br_id);
  36875. + err = au_xino_trunc(sb, bindex, idx);
  36876. + ii_read_unlock(dir);
  36877. + if (unlikely(err))
  36878. + pr_warn("err b%d, (%d)\n", bindex, err);
  36879. + atomic_dec(&br->br_xino->xi_truncating);
  36880. + au_lcnt_dec(&br->br_count);
  36881. + si_write_unlock(sb);
  36882. + au_nwt_done(&au_sbi(sb)->si_nowait);
  36883. + au_kfree_rcu(args);
  36884. +}
  36885. +
  36886. +/*
  36887. + * returns the index in the xi_file array whose corresponding file is necessary
  36888. + * to truncate, or -1 which means no need to truncate.
  36889. + */
  36890. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  36891. +{
  36892. + int err;
  36893. + unsigned int u;
  36894. + struct kstatfs st;
  36895. + struct au_sbinfo *sbinfo;
  36896. + struct au_xino *xi;
  36897. + struct file *file;
  36898. +
  36899. + /* todo: si_xino_expire and the ratio should be customizable */
  36900. + sbinfo = au_sbi(sb);
  36901. + if (time_before(jiffies,
  36902. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  36903. + return -1;
  36904. +
  36905. + /* truncation border */
  36906. + xi = br->br_xino;
  36907. + for (u = 0; u < xi->xi_nfile; u++) {
  36908. + file = au_xino_file(xi, u);
  36909. + if (!file)
  36910. + continue;
  36911. +
  36912. + err = vfs_statfs(&file->f_path, &st);
  36913. + if (unlikely(err)) {
  36914. + AuErr1("statfs err %d, ignored\n", err);
  36915. + return -1;
  36916. + }
  36917. + if (div64_u64(st.f_bfree * 100, st.f_blocks)
  36918. + >= AUFS_XINO_DEF_TRUNC)
  36919. + return u;
  36920. + }
  36921. +
  36922. + return -1;
  36923. +}
  36924. +
  36925. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  36926. +{
  36927. + int idx;
  36928. + struct xino_do_trunc_args *args;
  36929. + int wkq_err;
  36930. +
  36931. + idx = xino_trunc_test(sb, br);
  36932. + if (idx < 0)
  36933. + return;
  36934. +
  36935. + if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
  36936. + goto out;
  36937. +
  36938. + /* lock and kfree() will be called in trunc_xino() */
  36939. + args = kmalloc(sizeof(*args), GFP_NOFS);
  36940. + if (unlikely(!args)) {
  36941. + AuErr1("no memory\n");
  36942. + goto out;
  36943. + }
  36944. +
  36945. + au_lcnt_inc(&br->br_count);
  36946. + args->sb = sb;
  36947. + args->br = br;
  36948. + args->idx = idx;
  36949. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  36950. + if (!wkq_err)
  36951. + return; /* success */
  36952. +
  36953. + pr_err("wkq %d\n", wkq_err);
  36954. + au_lcnt_dec(&br->br_count);
  36955. + au_kfree_rcu(args);
  36956. +
  36957. +out:
  36958. + atomic_dec(&br->br_xino->xi_truncating);
  36959. +}
  36960. +
  36961. +/* ---------------------------------------------------------------------- */
  36962. +
  36963. +struct au_xi_calc {
  36964. + int idx;
  36965. + loff_t pos;
  36966. +};
  36967. +
  36968. +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
  36969. + struct au_xi_calc *calc)
  36970. +{
  36971. + loff_t maxent;
  36972. +
  36973. + maxent = au_xi_maxent(sb);
  36974. + calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
  36975. + calc->pos *= sizeof(ino_t);
  36976. +}
  36977. +
  36978. +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
  36979. + struct au_xi_calc *calc)
  36980. +{
  36981. + int err;
  36982. + struct file *file;
  36983. + struct au_xino *xi = br->br_xino;
  36984. + struct au_xi_new xinew = {
  36985. + .xi = xi
  36986. + };
  36987. +
  36988. + SiMustAnyLock(sb);
  36989. +
  36990. + err = 0;
  36991. + if (!xi)
  36992. + goto out;
  36993. +
  36994. + mutex_lock(&xi->xi_mtx);
  36995. + file = au_xino_file(xi, calc->idx);
  36996. + if (file)
  36997. + goto out_mtx;
  36998. +
  36999. + file = au_xino_file(xi, /*idx*/-1);
  37000. + AuDebugOn(!file);
  37001. + xinew.idx = calc->idx;
  37002. + xinew.base = &file->f_path;
  37003. + /* xinew.copy_src = NULL; */
  37004. + file = au_xi_new(sb, &xinew);
  37005. + if (IS_ERR(file))
  37006. + err = PTR_ERR(file);
  37007. +
  37008. +out_mtx:
  37009. + mutex_unlock(&xi->xi_mtx);
  37010. +out:
  37011. + return err;
  37012. +}
  37013. +
  37014. +struct au_xino_do_new_async_args {
  37015. + struct super_block *sb;
  37016. + struct au_branch *br;
  37017. + struct au_xi_calc calc;
  37018. + ino_t ino;
  37019. +};
  37020. +
  37021. +struct au_xi_writing {
  37022. + struct hlist_bl_node node;
  37023. + ino_t h_ino, ino;
  37024. +};
  37025. +
  37026. +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
  37027. + ino_t ino);
  37028. +
  37029. +static void au_xino_call_do_new_async(void *args)
  37030. +{
  37031. + struct au_xino_do_new_async_args *a = args;
  37032. + struct au_branch *br;
  37033. + struct super_block *sb;
  37034. + struct au_sbinfo *sbi;
  37035. + struct inode *root;
  37036. + struct file *file;
  37037. + struct au_xi_writing *del, *p;
  37038. + struct hlist_bl_head *hbl;
  37039. + struct hlist_bl_node *pos;
  37040. + int err;
  37041. +
  37042. + br = a->br;
  37043. + sb = a->sb;
  37044. + sbi = au_sbi(sb);
  37045. + si_noflush_read_lock(sb);
  37046. + root = d_inode(sb->s_root);
  37047. + ii_read_lock_child(root);
  37048. + err = au_xino_do_new_async(sb, br, &a->calc);
  37049. + if (unlikely(err)) {
  37050. + AuIOErr("err %d\n", err);
  37051. + goto out;
  37052. + }
  37053. +
  37054. + file = au_xino_file(br->br_xino, a->calc.idx);
  37055. + AuDebugOn(!file);
  37056. + err = au_xino_do_write(file, &a->calc, a->ino);
  37057. + if (unlikely(err)) {
  37058. + AuIOErr("err %d\n", err);
  37059. + goto out;
  37060. + }
  37061. +
  37062. + del = NULL;
  37063. + hbl = &br->br_xino->xi_writing;
  37064. + hlist_bl_lock(hbl);
  37065. + au_hbl_for_each(pos, hbl) {
  37066. + p = container_of(pos, struct au_xi_writing, node);
  37067. + if (p->ino == a->ino) {
  37068. + del = p;
  37069. + hlist_bl_del(&p->node);
  37070. + break;
  37071. + }
  37072. + }
  37073. + hlist_bl_unlock(hbl);
  37074. + au_kfree_rcu(del);
  37075. +
  37076. +out:
  37077. + au_lcnt_dec(&br->br_count);
  37078. + ii_read_unlock(root);
  37079. + si_read_unlock(sb);
  37080. + au_nwt_done(&sbi->si_nowait);
  37081. + au_kfree_rcu(a);
  37082. +}
  37083. +
  37084. +/*
  37085. + * create a new xino file asynchronously
  37086. + */
  37087. +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
  37088. + struct au_xi_calc *calc, ino_t ino)
  37089. +{
  37090. + int err;
  37091. + struct au_xino_do_new_async_args *arg;
  37092. +
  37093. + err = -ENOMEM;
  37094. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  37095. + if (unlikely(!arg))
  37096. + goto out;
  37097. +
  37098. + arg->sb = sb;
  37099. + arg->br = br;
  37100. + arg->calc = *calc;
  37101. + arg->ino = ino;
  37102. + au_lcnt_inc(&br->br_count);
  37103. + err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
  37104. + if (unlikely(err)) {
  37105. + pr_err("wkq %d\n", err);
  37106. + au_lcnt_dec(&br->br_count);
  37107. + au_kfree_rcu(arg);
  37108. + }
  37109. +
  37110. +out:
  37111. + return err;
  37112. +}
  37113. +
  37114. +/*
  37115. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  37116. + * at the position of @h_ino.
  37117. + */
  37118. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  37119. + ino_t *ino)
  37120. +{
  37121. + int err;
  37122. + ssize_t sz;
  37123. + struct au_xi_calc calc;
  37124. + struct au_sbinfo *sbinfo;
  37125. + struct file *file;
  37126. + struct au_xino *xi;
  37127. + struct hlist_bl_head *hbl;
  37128. + struct hlist_bl_node *pos;
  37129. + struct au_xi_writing *p;
  37130. +
  37131. + *ino = 0;
  37132. + if (!au_opt_test(au_mntflags(sb), XINO))
  37133. + return 0; /* no xino */
  37134. +
  37135. + err = 0;
  37136. + au_xi_calc(sb, h_ino, &calc);
  37137. + xi = au_sbr(sb, bindex)->br_xino;
  37138. + file = au_xino_file(xi, calc.idx);
  37139. + if (!file) {
  37140. + hbl = &xi->xi_writing;
  37141. + hlist_bl_lock(hbl);
  37142. + au_hbl_for_each(pos, hbl) {
  37143. + p = container_of(pos, struct au_xi_writing, node);
  37144. + if (p->h_ino == h_ino) {
  37145. + AuDbg("hi%llu, i%llu, found\n",
  37146. + (u64)p->h_ino, (u64)p->ino);
  37147. + *ino = p->ino;
  37148. + break;
  37149. + }
  37150. + }
  37151. + hlist_bl_unlock(hbl);
  37152. + return 0;
  37153. + } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
  37154. + return 0; /* no xino */
  37155. +
  37156. + sbinfo = au_sbi(sb);
  37157. + sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
  37158. + if (sz == sizeof(*ino))
  37159. + return 0; /* success */
  37160. +
  37161. + err = sz;
  37162. + if (unlikely(sz >= 0)) {
  37163. + err = -EIO;
  37164. + AuIOErr("xino read error (%zd)\n", sz);
  37165. + }
  37166. + return err;
  37167. +}
  37168. +
  37169. +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
  37170. + ino_t ino)
  37171. +{
  37172. + ssize_t sz;
  37173. +
  37174. + sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
  37175. + if (sz == sizeof(ino))
  37176. + return 0; /* success */
  37177. +
  37178. + AuIOErr("write failed (%zd)\n", sz);
  37179. + return -EIO;
  37180. +}
  37181. +
  37182. +/*
  37183. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  37184. + * at the position of @h_ino.
  37185. + * even if @ino is zero, it is written to the xinofile and means no entry.
  37186. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  37187. + * try truncating it.
  37188. + */
  37189. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  37190. + ino_t ino)
  37191. +{
  37192. + int err;
  37193. + unsigned int mnt_flags;
  37194. + struct au_xi_calc calc;
  37195. + struct file *file;
  37196. + struct au_branch *br;
  37197. + struct au_xino *xi;
  37198. + struct au_xi_writing *p;
  37199. +
  37200. + SiMustAnyLock(sb);
  37201. +
  37202. + mnt_flags = au_mntflags(sb);
  37203. + if (!au_opt_test(mnt_flags, XINO))
  37204. + return 0;
  37205. +
  37206. + au_xi_calc(sb, h_ino, &calc);
  37207. + br = au_sbr(sb, bindex);
  37208. + xi = br->br_xino;
  37209. + file = au_xino_file(xi, calc.idx);
  37210. + if (!file) {
  37211. + /* store the inum pair into the list */
  37212. + p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
  37213. + p->h_ino = h_ino;
  37214. + p->ino = ino;
  37215. + au_hbl_add(&p->node, &xi->xi_writing);
  37216. +
  37217. + /* create and write a new xino file asynchronously */
  37218. + err = au_xino_new_async(sb, br, &calc, ino);
  37219. + if (!err)
  37220. + return 0; /* success */
  37221. + goto out;
  37222. + }
  37223. +
  37224. + err = au_xino_do_write(file, &calc, ino);
  37225. + if (!err) {
  37226. + br = au_sbr(sb, bindex);
  37227. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  37228. + && au_test_fs_trunc_xino(au_br_sb(br)))
  37229. + xino_try_trunc(sb, br);
  37230. + return 0; /* success */
  37231. + }
  37232. +
  37233. +out:
  37234. + AuIOErr("write failed (%d)\n", err);
  37235. + return -EIO;
  37236. +}
  37237. +
  37238. +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
  37239. + loff_t *pos);
  37240. +
  37241. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  37242. +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
  37243. +{
  37244. + ssize_t err;
  37245. + int i;
  37246. + const int prevent_endless = 10;
  37247. +
  37248. + i = 0;
  37249. + do {
  37250. + err = vfsub_read_k(file, kbuf, size, pos);
  37251. + if (err == -EINTR
  37252. + && !au_wkq_test()
  37253. + && fatal_signal_pending(current)) {
  37254. + err = xino_fread_wkq(file, kbuf, size, pos);
  37255. + BUG_ON(err == -EINTR);
  37256. + }
  37257. + } while (i++ < prevent_endless
  37258. + && (err == -EAGAIN || err == -EINTR));
  37259. +
  37260. +#if 0 /* reserved for future use */
  37261. + if (err > 0)
  37262. + fsnotify_access(file->f_path.dentry);
  37263. +#endif
  37264. +
  37265. + return err;
  37266. +}
  37267. +
  37268. +struct xino_fread_args {
  37269. + ssize_t *errp;
  37270. + struct file *file;
  37271. + void *buf;
  37272. + size_t size;
  37273. + loff_t *pos;
  37274. +};
  37275. +
  37276. +static void call_xino_fread(void *args)
  37277. +{
  37278. + struct xino_fread_args *a = args;
  37279. + *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
  37280. +}
  37281. +
  37282. +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
  37283. + loff_t *pos)
  37284. +{
  37285. + ssize_t err;
  37286. + int wkq_err;
  37287. + struct xino_fread_args args = {
  37288. + .errp = &err,
  37289. + .file = file,
  37290. + .buf = buf,
  37291. + .size = size,
  37292. + .pos = pos
  37293. + };
  37294. +
  37295. + wkq_err = au_wkq_wait(call_xino_fread, &args);
  37296. + if (unlikely(wkq_err))
  37297. + err = wkq_err;
  37298. +
  37299. + return err;
  37300. +}
  37301. +
  37302. +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
  37303. + loff_t *pos);
  37304. +
  37305. +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
  37306. + loff_t *pos)
  37307. +{
  37308. + ssize_t err;
  37309. + int i;
  37310. + const int prevent_endless = 10;
  37311. +
  37312. + i = 0;
  37313. + do {
  37314. + err = vfsub_write_k(file, kbuf, size, pos);
  37315. + if (err == -EINTR
  37316. + && !au_wkq_test()
  37317. + && fatal_signal_pending(current)) {
  37318. + err = xino_fwrite_wkq(file, kbuf, size, pos);
  37319. + BUG_ON(err == -EINTR);
  37320. + }
  37321. + } while (i++ < prevent_endless
  37322. + && (err == -EAGAIN || err == -EINTR));
  37323. +
  37324. +#if 0 /* reserved for future use */
  37325. + if (err > 0)
  37326. + fsnotify_modify(file->f_path.dentry);
  37327. +#endif
  37328. +
  37329. + return err;
  37330. +}
  37331. +
  37332. +struct do_xino_fwrite_args {
  37333. + ssize_t *errp;
  37334. + struct file *file;
  37335. + void *buf;
  37336. + size_t size;
  37337. + loff_t *pos;
  37338. +};
  37339. +
  37340. +static void call_do_xino_fwrite(void *args)
  37341. +{
  37342. + struct do_xino_fwrite_args *a = args;
  37343. + *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
  37344. +}
  37345. +
  37346. +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
  37347. + loff_t *pos)
  37348. +{
  37349. + ssize_t err;
  37350. + int wkq_err;
  37351. + struct do_xino_fwrite_args args = {
  37352. + .errp = &err,
  37353. + .file = file,
  37354. + .buf = buf,
  37355. + .size = size,
  37356. + .pos = pos
  37357. + };
  37358. +
  37359. + /*
  37360. + * it breaks RLIMIT_FSIZE and normal user's limit,
  37361. + * users should care about quota and real 'filesystem full.'
  37362. + */
  37363. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  37364. + if (unlikely(wkq_err))
  37365. + err = wkq_err;
  37366. +
  37367. + return err;
  37368. +}
  37369. +
  37370. +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
  37371. +{
  37372. + ssize_t err;
  37373. +
  37374. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  37375. + lockdep_off();
  37376. + err = do_xino_fwrite(file, buf, size, pos);
  37377. + lockdep_on();
  37378. + } else {
  37379. + lockdep_off();
  37380. + err = xino_fwrite_wkq(file, buf, size, pos);
  37381. + lockdep_on();
  37382. + }
  37383. +
  37384. + return err;
  37385. +}
  37386. +
  37387. +/* ---------------------------------------------------------------------- */
  37388. +
  37389. +/*
  37390. + * inode number bitmap
  37391. + */
  37392. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  37393. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  37394. +{
  37395. + ino_t ino;
  37396. +
  37397. + AuDebugOn(bit < 0 || page_bits <= bit);
  37398. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  37399. + return ino;
  37400. +}
  37401. +
  37402. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  37403. +{
  37404. + AuDebugOn(ino < AUFS_FIRST_INO);
  37405. + ino -= AUFS_FIRST_INO;
  37406. + *pindex = ino / page_bits;
  37407. + *bit = ino % page_bits;
  37408. +}
  37409. +
  37410. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  37411. +{
  37412. + int err;
  37413. + loff_t pos;
  37414. + ssize_t sz;
  37415. + struct au_sbinfo *sbinfo;
  37416. + struct file *xib;
  37417. + unsigned long *p;
  37418. +
  37419. + sbinfo = au_sbi(sb);
  37420. + MtxMustLock(&sbinfo->si_xib_mtx);
  37421. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  37422. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  37423. +
  37424. + if (pindex == sbinfo->si_xib_last_pindex)
  37425. + return 0;
  37426. +
  37427. + xib = sbinfo->si_xib;
  37428. + p = sbinfo->si_xib_buf;
  37429. + pos = sbinfo->si_xib_last_pindex;
  37430. + pos *= PAGE_SIZE;
  37431. + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
  37432. + if (unlikely(sz != PAGE_SIZE))
  37433. + goto out;
  37434. +
  37435. + pos = pindex;
  37436. + pos *= PAGE_SIZE;
  37437. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  37438. + sz = xino_fread(xib, p, PAGE_SIZE, &pos);
  37439. + else {
  37440. + memset(p, 0, PAGE_SIZE);
  37441. + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
  37442. + }
  37443. + if (sz == PAGE_SIZE) {
  37444. + sbinfo->si_xib_last_pindex = pindex;
  37445. + return 0; /* success */
  37446. + }
  37447. +
  37448. +out:
  37449. + AuIOErr1("write failed (%zd)\n", sz);
  37450. + err = sz;
  37451. + if (sz >= 0)
  37452. + err = -EIO;
  37453. + return err;
  37454. +}
  37455. +
  37456. +static void au_xib_clear_bit(struct inode *inode)
  37457. +{
  37458. + int err, bit;
  37459. + unsigned long pindex;
  37460. + struct super_block *sb;
  37461. + struct au_sbinfo *sbinfo;
  37462. +
  37463. + AuDebugOn(vfsub_inode_nlink(inode, AU_I_AUFS));
  37464. +
  37465. + sb = inode->i_sb;
  37466. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  37467. + AuDebugOn(page_bits <= bit);
  37468. + sbinfo = au_sbi(sb);
  37469. + mutex_lock(&sbinfo->si_xib_mtx);
  37470. + err = xib_pindex(sb, pindex);
  37471. + if (!err) {
  37472. + clear_bit(bit, sbinfo->si_xib_buf);
  37473. + sbinfo->si_xib_next_bit = bit;
  37474. + }
  37475. + mutex_unlock(&sbinfo->si_xib_mtx);
  37476. +}
  37477. +
  37478. +/* ---------------------------------------------------------------------- */
  37479. +
  37480. +/*
  37481. + * truncate a xino bitmap file
  37482. + */
  37483. +
  37484. +/* todo: slow */
  37485. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  37486. +{
  37487. + int err, bit;
  37488. + ssize_t sz;
  37489. + unsigned long pindex;
  37490. + loff_t pos, pend;
  37491. + struct au_sbinfo *sbinfo;
  37492. + ino_t *ino;
  37493. + unsigned long *p;
  37494. +
  37495. + err = 0;
  37496. + sbinfo = au_sbi(sb);
  37497. + MtxMustLock(&sbinfo->si_xib_mtx);
  37498. + p = sbinfo->si_xib_buf;
  37499. + pend = vfsub_f_size_read(file);
  37500. + pos = 0;
  37501. + while (pos < pend) {
  37502. + sz = xino_fread(file, page, PAGE_SIZE, &pos);
  37503. + err = sz;
  37504. + if (unlikely(sz <= 0))
  37505. + goto out;
  37506. +
  37507. + err = 0;
  37508. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  37509. + if (unlikely(*ino < AUFS_FIRST_INO))
  37510. + continue;
  37511. +
  37512. + xib_calc_bit(*ino, &pindex, &bit);
  37513. + AuDebugOn(page_bits <= bit);
  37514. + err = xib_pindex(sb, pindex);
  37515. + if (!err)
  37516. + set_bit(bit, p);
  37517. + else
  37518. + goto out;
  37519. + }
  37520. + }
  37521. +
  37522. +out:
  37523. + return err;
  37524. +}
  37525. +
  37526. +static int xib_restore(struct super_block *sb)
  37527. +{
  37528. + int err, i;
  37529. + unsigned int nfile;
  37530. + aufs_bindex_t bindex, bbot;
  37531. + void *page;
  37532. + struct au_branch *br;
  37533. + struct au_xino *xi;
  37534. + struct file *file;
  37535. +
  37536. + err = -ENOMEM;
  37537. + page = (void *)__get_free_page(GFP_NOFS);
  37538. + if (unlikely(!page))
  37539. + goto out;
  37540. +
  37541. + err = 0;
  37542. + bbot = au_sbbot(sb);
  37543. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  37544. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
  37545. + br = au_sbr(sb, bindex);
  37546. + xi = br->br_xino;
  37547. + nfile = xi->xi_nfile;
  37548. + for (i = 0; i < nfile; i++) {
  37549. + file = au_xino_file(xi, i);
  37550. + if (file)
  37551. + err = do_xib_restore(sb, file, page);
  37552. + }
  37553. + } else
  37554. + AuDbg("skip shared b%d\n", bindex);
  37555. + free_page((unsigned long)page);
  37556. +
  37557. +out:
  37558. + return err;
  37559. +}
  37560. +
  37561. +int au_xib_trunc(struct super_block *sb)
  37562. +{
  37563. + int err;
  37564. + ssize_t sz;
  37565. + loff_t pos;
  37566. + struct au_sbinfo *sbinfo;
  37567. + unsigned long *p;
  37568. + struct file *file;
  37569. +
  37570. + SiMustWriteLock(sb);
  37571. +
  37572. + err = 0;
  37573. + sbinfo = au_sbi(sb);
  37574. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  37575. + goto out;
  37576. +
  37577. + file = sbinfo->si_xib;
  37578. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  37579. + goto out;
  37580. +
  37581. + file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
  37582. + err = PTR_ERR(file);
  37583. + if (IS_ERR(file))
  37584. + goto out;
  37585. + fput(sbinfo->si_xib);
  37586. + sbinfo->si_xib = file;
  37587. +
  37588. + p = sbinfo->si_xib_buf;
  37589. + memset(p, 0, PAGE_SIZE);
  37590. + pos = 0;
  37591. + sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
  37592. + if (unlikely(sz != PAGE_SIZE)) {
  37593. + err = sz;
  37594. + AuIOErr("err %d\n", err);
  37595. + if (sz >= 0)
  37596. + err = -EIO;
  37597. + goto out;
  37598. + }
  37599. +
  37600. + mutex_lock(&sbinfo->si_xib_mtx);
  37601. + /* mnt_want_write() is unnecessary here */
  37602. + err = xib_restore(sb);
  37603. + mutex_unlock(&sbinfo->si_xib_mtx);
  37604. +
  37605. +out:
  37606. + return err;
  37607. +}
  37608. +
  37609. +/* ---------------------------------------------------------------------- */
  37610. +
  37611. +struct au_xino *au_xino_alloc(unsigned int nfile)
  37612. +{
  37613. + struct au_xino *xi;
  37614. +
  37615. + xi = kzalloc(sizeof(*xi), GFP_NOFS);
  37616. + if (unlikely(!xi))
  37617. + goto out;
  37618. + xi->xi_nfile = nfile;
  37619. + xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
  37620. + if (unlikely(!xi->xi_file))
  37621. + goto out_free;
  37622. +
  37623. + xi->xi_nondir.total = 8; /* initial size */
  37624. + xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
  37625. + GFP_NOFS);
  37626. + if (unlikely(!xi->xi_nondir.array))
  37627. + goto out_file;
  37628. +
  37629. + spin_lock_init(&xi->xi_nondir.spin);
  37630. + init_waitqueue_head(&xi->xi_nondir.wqh);
  37631. + mutex_init(&xi->xi_mtx);
  37632. + INIT_HLIST_BL_HEAD(&xi->xi_writing);
  37633. + atomic_set(&xi->xi_truncating, 0);
  37634. + kref_init(&xi->xi_kref);
  37635. + goto out; /* success */
  37636. +
  37637. +out_file:
  37638. + au_kfree_try_rcu(xi->xi_file);
  37639. +out_free:
  37640. + au_kfree_rcu(xi);
  37641. + xi = NULL;
  37642. +out:
  37643. + return xi;
  37644. +}
  37645. +
  37646. +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
  37647. +{
  37648. + int err;
  37649. + struct au_xino *xi;
  37650. +
  37651. + err = 0;
  37652. + xi = au_xino_alloc(idx + 1);
  37653. + if (unlikely(!xi)) {
  37654. + err = -ENOMEM;
  37655. + goto out;
  37656. + }
  37657. +
  37658. + if (file)
  37659. + get_file(file);
  37660. + xi->xi_file[idx] = file;
  37661. + AuDebugOn(br->br_xino);
  37662. + br->br_xino = xi;
  37663. +
  37664. +out:
  37665. + return err;
  37666. +}
  37667. +
  37668. +static void au_xino_release(struct kref *kref)
  37669. +{
  37670. + struct au_xino *xi;
  37671. + int i;
  37672. + unsigned long ul;
  37673. + struct hlist_bl_head *hbl;
  37674. + struct hlist_bl_node *pos, *n;
  37675. + struct au_xi_writing *p;
  37676. +
  37677. + xi = container_of(kref, struct au_xino, xi_kref);
  37678. + for (i = 0; i < xi->xi_nfile; i++)
  37679. + if (xi->xi_file[i])
  37680. + fput(xi->xi_file[i]);
  37681. + for (i = xi->xi_nondir.total - 1; i >= 0; i--)
  37682. + AuDebugOn(xi->xi_nondir.array[i]);
  37683. + mutex_destroy(&xi->xi_mtx);
  37684. + hbl = &xi->xi_writing;
  37685. + ul = au_hbl_count(hbl);
  37686. + if (unlikely(ul)) {
  37687. + pr_warn("xi_writing %lu\n", ul);
  37688. + hlist_bl_lock(hbl);
  37689. + hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
  37690. + hlist_bl_del(&p->node);
  37691. + /* kmemleak reported au_kfree_rcu() doesn't free it */
  37692. + kfree(p);
  37693. + }
  37694. + hlist_bl_unlock(hbl);
  37695. + }
  37696. + au_kfree_try_rcu(xi->xi_file);
  37697. + au_kfree_try_rcu(xi->xi_nondir.array);
  37698. + au_kfree_rcu(xi);
  37699. +}
  37700. +
  37701. +int au_xino_put(struct au_branch *br)
  37702. +{
  37703. + int ret;
  37704. + struct au_xino *xi;
  37705. +
  37706. + ret = 0;
  37707. + xi = br->br_xino;
  37708. + if (xi) {
  37709. + br->br_xino = NULL;
  37710. + ret = kref_put(&xi->xi_kref, au_xino_release);
  37711. + }
  37712. +
  37713. + return ret;
  37714. +}
  37715. +
  37716. +/* ---------------------------------------------------------------------- */
  37717. +
  37718. +/*
  37719. + * xino mount option handlers
  37720. + */
  37721. +
  37722. +/* xino bitmap */
  37723. +static void xino_clear_xib(struct super_block *sb)
  37724. +{
  37725. + struct au_sbinfo *sbinfo;
  37726. +
  37727. + SiMustWriteLock(sb);
  37728. +
  37729. + sbinfo = au_sbi(sb);
  37730. + if (sbinfo->si_xib)
  37731. + fput(sbinfo->si_xib);
  37732. + sbinfo->si_xib = NULL;
  37733. + if (sbinfo->si_xib_buf)
  37734. + free_page((unsigned long)sbinfo->si_xib_buf);
  37735. + sbinfo->si_xib_buf = NULL;
  37736. +}
  37737. +
  37738. +static int au_xino_set_xib(struct super_block *sb, struct path *path)
  37739. +{
  37740. + int err;
  37741. + loff_t pos;
  37742. + struct au_sbinfo *sbinfo;
  37743. + struct file *file;
  37744. + struct super_block *xi_sb;
  37745. +
  37746. + SiMustWriteLock(sb);
  37747. +
  37748. + sbinfo = au_sbi(sb);
  37749. + file = au_xino_create2(sb, path, sbinfo->si_xib);
  37750. + err = PTR_ERR(file);
  37751. + if (IS_ERR(file))
  37752. + goto out;
  37753. + if (sbinfo->si_xib)
  37754. + fput(sbinfo->si_xib);
  37755. + sbinfo->si_xib = file;
  37756. + xi_sb = file_inode(file)->i_sb;
  37757. + sbinfo->si_ximaxent = xi_sb->s_maxbytes;
  37758. + if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
  37759. + err = -EIO;
  37760. + pr_err("s_maxbytes(%llu) on %s is too small\n",
  37761. + (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
  37762. + goto out_unset;
  37763. + }
  37764. + sbinfo->si_ximaxent /= sizeof(ino_t);
  37765. +
  37766. + err = -ENOMEM;
  37767. + if (!sbinfo->si_xib_buf)
  37768. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  37769. + if (unlikely(!sbinfo->si_xib_buf))
  37770. + goto out_unset;
  37771. +
  37772. + sbinfo->si_xib_last_pindex = 0;
  37773. + sbinfo->si_xib_next_bit = 0;
  37774. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  37775. + pos = 0;
  37776. + err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
  37777. + if (unlikely(err != PAGE_SIZE))
  37778. + goto out_free;
  37779. + }
  37780. + err = 0;
  37781. + goto out; /* success */
  37782. +
  37783. +out_free:
  37784. + if (sbinfo->si_xib_buf)
  37785. + free_page((unsigned long)sbinfo->si_xib_buf);
  37786. + sbinfo->si_xib_buf = NULL;
  37787. + if (err >= 0)
  37788. + err = -EIO;
  37789. +out_unset:
  37790. + fput(sbinfo->si_xib);
  37791. + sbinfo->si_xib = NULL;
  37792. +out:
  37793. + AuTraceErr(err);
  37794. + return err;
  37795. +}
  37796. +
  37797. +/* xino for each branch */
  37798. +static void xino_clear_br(struct super_block *sb)
  37799. +{
  37800. + aufs_bindex_t bindex, bbot;
  37801. + struct au_branch *br;
  37802. +
  37803. + bbot = au_sbbot(sb);
  37804. + for (bindex = 0; bindex <= bbot; bindex++) {
  37805. + br = au_sbr(sb, bindex);
  37806. + AuDebugOn(!br);
  37807. + au_xino_put(br);
  37808. + }
  37809. +}
  37810. +
  37811. +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
  37812. + aufs_bindex_t bshared)
  37813. +{
  37814. + struct au_branch *brshared;
  37815. +
  37816. + brshared = au_sbr(sb, bshared);
  37817. + AuDebugOn(!brshared->br_xino);
  37818. + AuDebugOn(!brshared->br_xino->xi_file);
  37819. + if (br->br_xino != brshared->br_xino) {
  37820. + au_xino_get(brshared);
  37821. + au_xino_put(br);
  37822. + br->br_xino = brshared->br_xino;
  37823. + }
  37824. +}
  37825. +
  37826. +struct au_xino_do_set_br {
  37827. + struct au_branch *br;
  37828. + ino_t h_ino;
  37829. + aufs_bindex_t bshared;
  37830. +};
  37831. +
  37832. +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
  37833. + struct au_xino_do_set_br *args)
  37834. +{
  37835. + int err;
  37836. + struct au_xi_calc calc;
  37837. + struct file *file;
  37838. + struct au_branch *br;
  37839. + struct au_xi_new xinew = {
  37840. + .base = path
  37841. + };
  37842. +
  37843. + br = args->br;
  37844. + xinew.xi = br->br_xino;
  37845. + au_xi_calc(sb, args->h_ino, &calc);
  37846. + xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
  37847. + if (args->bshared >= 0)
  37848. + /* shared xino */
  37849. + au_xino_set_br_shared(sb, br, args->bshared);
  37850. + else if (!xinew.xi) {
  37851. + /* new xino */
  37852. + err = au_xino_init(br, calc.idx, xinew.copy_src);
  37853. + if (unlikely(err))
  37854. + goto out;
  37855. + }
  37856. +
  37857. + /* force re-creating */
  37858. + xinew.xi = br->br_xino;
  37859. + xinew.idx = calc.idx;
  37860. + mutex_lock(&xinew.xi->xi_mtx);
  37861. + file = au_xi_new(sb, &xinew);
  37862. + mutex_unlock(&xinew.xi->xi_mtx);
  37863. + err = PTR_ERR(file);
  37864. + if (IS_ERR(file))
  37865. + goto out;
  37866. + AuDebugOn(!file);
  37867. +
  37868. + err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
  37869. + if (unlikely(err))
  37870. + au_xino_put(br);
  37871. +
  37872. +out:
  37873. + AuTraceErr(err);
  37874. + return err;
  37875. +}
  37876. +
  37877. +static int au_xino_set_br(struct super_block *sb, struct path *path)
  37878. +{
  37879. + int err;
  37880. + aufs_bindex_t bindex, bbot;
  37881. + struct au_xino_do_set_br args;
  37882. + struct inode *inode;
  37883. +
  37884. + SiMustWriteLock(sb);
  37885. +
  37886. + bbot = au_sbbot(sb);
  37887. + inode = d_inode(sb->s_root);
  37888. + for (bindex = 0; bindex <= bbot; bindex++) {
  37889. + args.h_ino = au_h_iptr(inode, bindex)->i_ino;
  37890. + args.br = au_sbr(sb, bindex);
  37891. + args.bshared = is_sb_shared(sb, bindex, bindex - 1);
  37892. + err = au_xino_do_set_br(sb, path, &args);
  37893. + if (unlikely(err))
  37894. + break;
  37895. + }
  37896. +
  37897. + AuTraceErr(err);
  37898. + return err;
  37899. +}
  37900. +
  37901. +void au_xino_clr(struct super_block *sb)
  37902. +{
  37903. + struct au_sbinfo *sbinfo;
  37904. +
  37905. + au_xigen_clr(sb);
  37906. + xino_clear_xib(sb);
  37907. + xino_clear_br(sb);
  37908. + dbgaufs_brs_del(sb, 0);
  37909. + sbinfo = au_sbi(sb);
  37910. + /* lvalue, do not call au_mntflags() */
  37911. + au_opt_clr(sbinfo->si_mntflags, XINO);
  37912. +}
  37913. +
  37914. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
  37915. +{
  37916. + int err, skip;
  37917. + struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
  37918. + struct qstr *dname, *cur_name;
  37919. + struct file *cur_xino;
  37920. + struct au_sbinfo *sbinfo;
  37921. + struct path *path, *cur_path;
  37922. +
  37923. + SiMustWriteLock(sb);
  37924. +
  37925. + err = 0;
  37926. + sbinfo = au_sbi(sb);
  37927. + path = &xiopt->file->f_path;
  37928. + dentry = path->dentry;
  37929. + parent = dget_parent(dentry);
  37930. + if (remount) {
  37931. + skip = 0;
  37932. + cur_xino = sbinfo->si_xib;
  37933. + if (cur_xino) {
  37934. + cur_path = &cur_xino->f_path;
  37935. + cur_dentry = cur_path->dentry;
  37936. + cur_parent = dget_parent(cur_dentry);
  37937. + cur_name = &cur_dentry->d_name;
  37938. + dname = &dentry->d_name;
  37939. + skip = (cur_parent == parent
  37940. + && au_qstreq(dname, cur_name));
  37941. + dput(cur_parent);
  37942. + }
  37943. + if (skip)
  37944. + goto out;
  37945. + }
  37946. +
  37947. + au_opt_set(sbinfo->si_mntflags, XINO);
  37948. + err = au_xino_set_xib(sb, path);
  37949. + /* si_x{read,write} are set */
  37950. + if (!err)
  37951. + err = au_xigen_set(sb, path);
  37952. + if (!err)
  37953. + err = au_xino_set_br(sb, path);
  37954. + if (!err) {
  37955. + dbgaufs_brs_add(sb, 0, /*topdown*/1);
  37956. + goto out; /* success */
  37957. + }
  37958. +
  37959. + /* reset all */
  37960. + AuIOErr("failed setting xino(%d).\n", err);
  37961. + au_xino_clr(sb);
  37962. +
  37963. +out:
  37964. + dput(parent);
  37965. + return err;
  37966. +}
  37967. +
  37968. +/*
  37969. + * create a xinofile at the default place/path.
  37970. + */
  37971. +struct file *au_xino_def(struct super_block *sb)
  37972. +{
  37973. + struct file *file;
  37974. + char *page, *p;
  37975. + struct au_branch *br;
  37976. + struct super_block *h_sb;
  37977. + struct path path;
  37978. + aufs_bindex_t bbot, bindex, bwr;
  37979. +
  37980. + br = NULL;
  37981. + bbot = au_sbbot(sb);
  37982. + bwr = -1;
  37983. + for (bindex = 0; bindex <= bbot; bindex++) {
  37984. + br = au_sbr(sb, bindex);
  37985. + if (au_br_writable(br->br_perm)
  37986. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  37987. + bwr = bindex;
  37988. + break;
  37989. + }
  37990. + }
  37991. +
  37992. + if (bwr >= 0) {
  37993. + file = ERR_PTR(-ENOMEM);
  37994. + page = (void *)__get_free_page(GFP_NOFS);
  37995. + if (unlikely(!page))
  37996. + goto out;
  37997. + path.mnt = au_br_mnt(br);
  37998. + path.dentry = au_h_dptr(sb->s_root, bwr);
  37999. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  38000. + file = (void *)p;
  38001. + if (!IS_ERR(p)) {
  38002. + strcat(p, "/" AUFS_XINO_FNAME);
  38003. + AuDbg("%s\n", p);
  38004. + file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
  38005. + }
  38006. + free_page((unsigned long)page);
  38007. + } else {
  38008. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
  38009. + /*wbrtop*/0);
  38010. + if (IS_ERR(file))
  38011. + goto out;
  38012. + h_sb = file->f_path.dentry->d_sb;
  38013. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  38014. + pr_err("xino doesn't support %s(%s)\n",
  38015. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  38016. + fput(file);
  38017. + file = ERR_PTR(-EINVAL);
  38018. + }
  38019. + }
  38020. +
  38021. +out:
  38022. + return file;
  38023. +}
  38024. +
  38025. +/* ---------------------------------------------------------------------- */
  38026. +
  38027. +/*
  38028. + * initialize the xinofile for the specified branch @br
  38029. + * at the place/path where @base_file indicates.
  38030. + * test whether another branch is on the same filesystem or not,
  38031. + * if found then share the xinofile with another branch.
  38032. + */
  38033. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  38034. + struct path *base)
  38035. +{
  38036. + int err;
  38037. + struct au_xino_do_set_br args = {
  38038. + .h_ino = h_ino,
  38039. + .br = br
  38040. + };
  38041. +
  38042. + args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
  38043. + au_br_sb(br));
  38044. + err = au_xino_do_set_br(sb, base, &args);
  38045. + if (unlikely(err))
  38046. + au_xino_put(br);
  38047. +
  38048. + return err;
  38049. +}
  38050. +
  38051. +/* ---------------------------------------------------------------------- */
  38052. +
  38053. +/*
  38054. + * get an unused inode number from bitmap
  38055. + */
  38056. +ino_t au_xino_new_ino(struct super_block *sb)
  38057. +{
  38058. + ino_t ino;
  38059. + unsigned long *p, pindex, ul, pend;
  38060. + struct au_sbinfo *sbinfo;
  38061. + struct file *file;
  38062. + int free_bit, err;
  38063. +
  38064. + if (!au_opt_test(au_mntflags(sb), XINO))
  38065. + return iunique(sb, AUFS_FIRST_INO);
  38066. +
  38067. + sbinfo = au_sbi(sb);
  38068. + mutex_lock(&sbinfo->si_xib_mtx);
  38069. + p = sbinfo->si_xib_buf;
  38070. + free_bit = sbinfo->si_xib_next_bit;
  38071. + if (free_bit < page_bits && !test_bit(free_bit, p))
  38072. + goto out; /* success */
  38073. + free_bit = find_first_zero_bit(p, page_bits);
  38074. + if (free_bit < page_bits)
  38075. + goto out; /* success */
  38076. +
  38077. + pindex = sbinfo->si_xib_last_pindex;
  38078. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  38079. + err = xib_pindex(sb, ul);
  38080. + if (unlikely(err))
  38081. + goto out_err;
  38082. + free_bit = find_first_zero_bit(p, page_bits);
  38083. + if (free_bit < page_bits)
  38084. + goto out; /* success */
  38085. + }
  38086. +
  38087. + file = sbinfo->si_xib;
  38088. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  38089. + for (ul = pindex + 1; ul <= pend; ul++) {
  38090. + err = xib_pindex(sb, ul);
  38091. + if (unlikely(err))
  38092. + goto out_err;
  38093. + free_bit = find_first_zero_bit(p, page_bits);
  38094. + if (free_bit < page_bits)
  38095. + goto out; /* success */
  38096. + }
  38097. + BUG();
  38098. +
  38099. +out:
  38100. + set_bit(free_bit, p);
  38101. + sbinfo->si_xib_next_bit = free_bit + 1;
  38102. + pindex = sbinfo->si_xib_last_pindex;
  38103. + mutex_unlock(&sbinfo->si_xib_mtx);
  38104. + ino = xib_calc_ino(pindex, free_bit);
  38105. + AuDbg("i%lu\n", (unsigned long)ino);
  38106. + return ino;
  38107. +out_err:
  38108. + mutex_unlock(&sbinfo->si_xib_mtx);
  38109. + AuDbg("i0\n");
  38110. + return 0;
  38111. +}
  38112. +
  38113. +/* for s_op->delete_inode() */
  38114. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  38115. +{
  38116. + int err;
  38117. + unsigned int mnt_flags;
  38118. + aufs_bindex_t bindex, bbot, bi;
  38119. + unsigned char try_trunc;
  38120. + struct au_iinfo *iinfo;
  38121. + struct super_block *sb;
  38122. + struct au_hinode *hi;
  38123. + struct inode *h_inode;
  38124. + struct au_branch *br;
  38125. + struct au_xi_calc calc;
  38126. + struct file *file;
  38127. +
  38128. + AuDebugOn(au_is_bad_inode(inode));
  38129. +
  38130. + sb = inode->i_sb;
  38131. + mnt_flags = au_mntflags(sb);
  38132. + if (!au_opt_test(mnt_flags, XINO)
  38133. + || inode->i_ino == AUFS_ROOT_INO)
  38134. + return;
  38135. +
  38136. + if (unlinked) {
  38137. + au_xigen_inc(inode);
  38138. + au_xib_clear_bit(inode);
  38139. + }
  38140. +
  38141. + iinfo = au_ii(inode);
  38142. + bindex = iinfo->ii_btop;
  38143. + if (bindex < 0)
  38144. + return;
  38145. +
  38146. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  38147. + hi = au_hinode(iinfo, bindex);
  38148. + bbot = iinfo->ii_bbot;
  38149. + for (; bindex <= bbot; bindex++, hi++) {
  38150. + h_inode = hi->hi_inode;
  38151. + if (!h_inode
  38152. + || (!unlinked && vfsub_inode_nlink(h_inode, AU_I_BRANCH)))
  38153. + continue;
  38154. +
  38155. + /* inode may not be revalidated */
  38156. + bi = au_br_index(sb, hi->hi_id);
  38157. + if (bi < 0)
  38158. + continue;
  38159. +
  38160. + br = au_sbr(sb, bi);
  38161. + au_xi_calc(sb, h_inode->i_ino, &calc);
  38162. + file = au_xino_file(br->br_xino, calc.idx);
  38163. + if (IS_ERR_OR_NULL(file))
  38164. + continue;
  38165. +
  38166. + err = au_xino_do_write(file, &calc, /*ino*/0);
  38167. + if (!err && try_trunc
  38168. + && au_test_fs_trunc_xino(au_br_sb(br)))
  38169. + xino_try_trunc(sb, br);
  38170. + }
  38171. +}
  38172. +
  38173. +/* ---------------------------------------------------------------------- */
  38174. +
  38175. +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
  38176. +{
  38177. + int found, total, i;
  38178. +
  38179. + found = -1;
  38180. + total = xi->xi_nondir.total;
  38181. + for (i = 0; i < total; i++) {
  38182. + if (xi->xi_nondir.array[i] != h_ino)
  38183. + continue;
  38184. + found = i;
  38185. + break;
  38186. + }
  38187. +
  38188. + return found;
  38189. +}
  38190. +
  38191. +static int au_xinondir_expand(struct au_xino *xi)
  38192. +{
  38193. + int err, sz;
  38194. + ino_t *p;
  38195. +
  38196. + BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
  38197. +
  38198. + err = -ENOMEM;
  38199. + sz = xi->xi_nondir.total * sizeof(ino_t);
  38200. + if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
  38201. + goto out;
  38202. + p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
  38203. + /*may_shrink*/0);
  38204. + if (p) {
  38205. + xi->xi_nondir.array = p;
  38206. + xi->xi_nondir.total <<= 1;
  38207. + AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
  38208. + err = 0;
  38209. + }
  38210. +
  38211. +out:
  38212. + return err;
  38213. +}
  38214. +
  38215. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  38216. + ino_t h_ino, int idx)
  38217. +{
  38218. + struct au_xino *xi;
  38219. +
  38220. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  38221. + xi = au_sbr(sb, bindex)->br_xino;
  38222. + AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
  38223. +
  38224. + spin_lock(&xi->xi_nondir.spin);
  38225. + AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
  38226. + xi->xi_nondir.array[idx] = 0;
  38227. + spin_unlock(&xi->xi_nondir.spin);
  38228. + wake_up_all(&xi->xi_nondir.wqh);
  38229. +}
  38230. +
  38231. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  38232. + int *idx)
  38233. +{
  38234. + int err, found, empty;
  38235. + struct au_xino *xi;
  38236. +
  38237. + err = 0;
  38238. + *idx = -1;
  38239. + if (!au_opt_test(au_mntflags(sb), XINO))
  38240. + goto out; /* no xino */
  38241. +
  38242. + xi = au_sbr(sb, bindex)->br_xino;
  38243. +
  38244. +again:
  38245. + spin_lock(&xi->xi_nondir.spin);
  38246. + found = au_xinondir_find(xi, h_ino);
  38247. + if (found == -1) {
  38248. + empty = au_xinondir_find(xi, /*h_ino*/0);
  38249. + if (empty == -1) {
  38250. + empty = xi->xi_nondir.total;
  38251. + err = au_xinondir_expand(xi);
  38252. + if (unlikely(err))
  38253. + goto out_unlock;
  38254. + }
  38255. + xi->xi_nondir.array[empty] = h_ino;
  38256. + *idx = empty;
  38257. + } else {
  38258. + spin_unlock(&xi->xi_nondir.spin);
  38259. + wait_event(xi->xi_nondir.wqh,
  38260. + xi->xi_nondir.array[found] != h_ino);
  38261. + goto again;
  38262. + }
  38263. +
  38264. +out_unlock:
  38265. + spin_unlock(&xi->xi_nondir.spin);
  38266. +out:
  38267. + return err;
  38268. +}
  38269. +
  38270. +/* ---------------------------------------------------------------------- */
  38271. +
  38272. +int au_xino_path(struct seq_file *seq, struct file *file)
  38273. +{
  38274. + int err;
  38275. +
  38276. + err = au_seq_path(seq, &file->f_path);
  38277. + if (unlikely(err))
  38278. + goto out;
  38279. +
  38280. +#define Deleted "\\040(deleted)"
  38281. + seq->count -= sizeof(Deleted) - 1;
  38282. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  38283. + sizeof(Deleted) - 1));
  38284. +#undef Deleted
  38285. +
  38286. +out:
  38287. + return err;
  38288. +}
  38289. diff -Naur null/include/uapi/linux/aufs_type.h linux-6.6.63/include/uapi/linux/aufs_type.h
  38290. --- /dev/null
  38291. +++ linux-6.6.63/include/uapi/linux/aufs_type.h 2024-12-18 15:12:59.301519513 +0300
  38292. @@ -0,0 +1,452 @@
  38293. +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  38294. +/*
  38295. + * Copyright (C) 2005-2022 Junjiro R. Okajima
  38296. + *
  38297. + * This program is free software; you can redistribute it and/or modify
  38298. + * it under the terms of the GNU General Public License as published by
  38299. + * the Free Software Foundation; either version 2 of the License, or
  38300. + * (at your option) any later version.
  38301. + *
  38302. + * This program is distributed in the hope that it will be useful,
  38303. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38304. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38305. + * GNU General Public License for more details.
  38306. + *
  38307. + * You should have received a copy of the GNU General Public License
  38308. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  38309. + */
  38310. +
  38311. +#ifndef __AUFS_TYPE_H__
  38312. +#define __AUFS_TYPE_H__
  38313. +
  38314. +#define AUFS_NAME "aufs"
  38315. +
  38316. +#ifdef __KERNEL__
  38317. +/*
  38318. + * define it before including all other headers.
  38319. + * sched.h may use pr_* macros before defining "current", so define the
  38320. + * no-current version first, and re-define later.
  38321. + */
  38322. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  38323. +#include <linux/sched.h>
  38324. +#undef pr_fmt
  38325. +#define pr_fmt(fmt) \
  38326. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  38327. + (int)sizeof(current->comm), current->comm, current->pid
  38328. +#include <linux/limits.h>
  38329. +#else
  38330. +#include <stdint.h>
  38331. +#include <sys/types.h>
  38332. +#include <limits.h>
  38333. +#endif /* __KERNEL__ */
  38334. +
  38335. +#define AUFS_VERSION "6.6.63-20241202"
  38336. +
  38337. +/* todo? move this to linux-2.6.19/include/magic.h */
  38338. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  38339. +
  38340. +/* ---------------------------------------------------------------------- */
  38341. +
  38342. +#ifdef __KERNEL__
  38343. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  38344. +typedef int8_t aufs_bindex_t;
  38345. +#define AUFS_BRANCH_MAX 127
  38346. +#else
  38347. +typedef int16_t aufs_bindex_t;
  38348. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  38349. +#define AUFS_BRANCH_MAX 511
  38350. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  38351. +#define AUFS_BRANCH_MAX 1023
  38352. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  38353. +#define AUFS_BRANCH_MAX 32767
  38354. +#endif
  38355. +#endif
  38356. +
  38357. +#ifndef AUFS_BRANCH_MAX
  38358. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  38359. +#endif
  38360. +#endif /* __KERNEL__ */
  38361. +
  38362. +/* ---------------------------------------------------------------------- */
  38363. +
  38364. +#define AUFS_FSTYPE AUFS_NAME
  38365. +
  38366. +#define AUFS_ROOT_INO 2
  38367. +#define AUFS_FIRST_INO 11
  38368. +
  38369. +#define AUFS_WH_PFX ".wh."
  38370. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  38371. +#define AUFS_WH_TMP_LEN 4
  38372. +/* a limit for rmdir/rename a dir and copyup */
  38373. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  38374. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  38375. + - 1 /* dot */\
  38376. + - AUFS_WH_TMP_LEN) /* hex */
  38377. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  38378. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  38379. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  38380. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  38381. +#define AUFS_DIRWH_DEF 3
  38382. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  38383. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  38384. +#define AUFS_RDBLK_DEF 512 /* bytes */
  38385. +#define AUFS_RDHASH_DEF 32
  38386. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  38387. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  38388. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  38389. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  38390. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  38391. +
  38392. +/* pseudo-link maintenace under /proc */
  38393. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  38394. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  38395. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  38396. +
  38397. +/* dirren, renamed dir */
  38398. +#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
  38399. +#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
  38400. +/* whiteouted doubly */
  38401. +#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
  38402. +#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
  38403. +
  38404. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  38405. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  38406. +
  38407. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  38408. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  38409. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  38410. +
  38411. +/* doubly whiteouted */
  38412. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  38413. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  38414. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  38415. +
  38416. +/* branch permissions and attributes */
  38417. +#define AUFS_BRPERM_RW "rw"
  38418. +#define AUFS_BRPERM_RO "ro"
  38419. +#define AUFS_BRPERM_RR "rr"
  38420. +#define AUFS_BRATTR_COO_REG "coo_reg"
  38421. +#define AUFS_BRATTR_COO_ALL "coo_all"
  38422. +#define AUFS_BRATTR_FHSM "fhsm"
  38423. +#define AUFS_BRATTR_UNPIN "unpin"
  38424. +#define AUFS_BRATTR_ICEX "icex"
  38425. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  38426. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  38427. +#define AUFS_BRATTR_ICEX_TR "icextr"
  38428. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  38429. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  38430. +#define AUFS_BRRATTR_WH "wh"
  38431. +#define AUFS_BRWATTR_NLWH "nolwh"
  38432. +#define AUFS_BRWATTR_MOO "moo"
  38433. +
  38434. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  38435. +#define AuBrPerm_RO (1 << 1) /* readonly */
  38436. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  38437. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  38438. +
  38439. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  38440. +#define AuBrAttr_COO_ALL (1 << 4)
  38441. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  38442. +
  38443. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  38444. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  38445. + branch. meaningless since
  38446. + linux-3.18-rc1 */
  38447. +
  38448. +/* ignore error in copying XATTR */
  38449. +#define AuBrAttr_ICEX_SEC (1 << 7)
  38450. +#define AuBrAttr_ICEX_SYS (1 << 8)
  38451. +#define AuBrAttr_ICEX_TR (1 << 9)
  38452. +#define AuBrAttr_ICEX_USR (1 << 10)
  38453. +#define AuBrAttr_ICEX_OTH (1 << 11)
  38454. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  38455. + | AuBrAttr_ICEX_SYS \
  38456. + | AuBrAttr_ICEX_TR \
  38457. + | AuBrAttr_ICEX_USR \
  38458. + | AuBrAttr_ICEX_OTH)
  38459. +
  38460. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  38461. +#define AuBrRAttr_Mask AuBrRAttr_WH
  38462. +
  38463. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  38464. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  38465. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  38466. +
  38467. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  38468. +
  38469. +/* #warning test userspace */
  38470. +#ifdef __KERNEL__
  38471. +#ifndef CONFIG_AUFS_FHSM
  38472. +#undef AuBrAttr_FHSM
  38473. +#define AuBrAttr_FHSM 0
  38474. +#endif
  38475. +#ifndef CONFIG_AUFS_XATTR
  38476. +#undef AuBrAttr_ICEX
  38477. +#define AuBrAttr_ICEX 0
  38478. +#undef AuBrAttr_ICEX_SEC
  38479. +#define AuBrAttr_ICEX_SEC 0
  38480. +#undef AuBrAttr_ICEX_SYS
  38481. +#define AuBrAttr_ICEX_SYS 0
  38482. +#undef AuBrAttr_ICEX_TR
  38483. +#define AuBrAttr_ICEX_TR 0
  38484. +#undef AuBrAttr_ICEX_USR
  38485. +#define AuBrAttr_ICEX_USR 0
  38486. +#undef AuBrAttr_ICEX_OTH
  38487. +#define AuBrAttr_ICEX_OTH 0
  38488. +#endif
  38489. +#endif
  38490. +
  38491. +/* the longest combination */
  38492. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  38493. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  38494. + "+" AUFS_BRATTR_COO_REG \
  38495. + "+" AUFS_BRATTR_FHSM \
  38496. + "+" AUFS_BRATTR_UNPIN \
  38497. + "+" AUFS_BRATTR_ICEX_SEC \
  38498. + "+" AUFS_BRATTR_ICEX_SYS \
  38499. + "+" AUFS_BRATTR_ICEX_USR \
  38500. + "+" AUFS_BRATTR_ICEX_OTH \
  38501. + "+" AUFS_BRWATTR_NLWH)
  38502. +
  38503. +typedef struct {
  38504. + char a[AuBrPermStrSz];
  38505. +} au_br_perm_str_t;
  38506. +
  38507. +static inline int au_br_writable(int brperm)
  38508. +{
  38509. + return brperm & AuBrPerm_RW;
  38510. +}
  38511. +
  38512. +static inline int au_br_whable(int brperm)
  38513. +{
  38514. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  38515. +}
  38516. +
  38517. +static inline int au_br_wh_linkable(int brperm)
  38518. +{
  38519. + return !(brperm & AuBrWAttr_NoLinkWH);
  38520. +}
  38521. +
  38522. +static inline int au_br_cmoo(int brperm)
  38523. +{
  38524. + return brperm & AuBrAttr_CMOO_Mask;
  38525. +}
  38526. +
  38527. +static inline int au_br_fhsm(int brperm)
  38528. +{
  38529. + return brperm & AuBrAttr_FHSM;
  38530. +}
  38531. +
  38532. +/* ---------------------------------------------------------------------- */
  38533. +
  38534. +/* ioctl */
  38535. +enum {
  38536. + /* readdir in userspace */
  38537. + AuCtl_RDU,
  38538. + AuCtl_RDU_INO,
  38539. +
  38540. + AuCtl_WBR_FD, /* pathconf wrapper */
  38541. + AuCtl_IBUSY, /* busy inode */
  38542. + AuCtl_MVDOWN, /* move-down */
  38543. + AuCtl_BR, /* info about branches */
  38544. + AuCtl_FHSM_FD /* connection for fhsm */
  38545. +};
  38546. +
  38547. +/* borrowed from linux/include/linux/kernel.h */
  38548. +#ifndef ALIGN
  38549. +#ifdef _GNU_SOURCE
  38550. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  38551. +#else
  38552. +#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  38553. +#endif
  38554. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  38555. +#endif
  38556. +
  38557. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  38558. +#ifndef __aligned
  38559. +#define __aligned(x) __attribute__((aligned(x)))
  38560. +#endif
  38561. +
  38562. +#ifdef __KERNEL__
  38563. +#ifndef __packed
  38564. +#define __packed __attribute__((packed))
  38565. +#endif
  38566. +#endif
  38567. +
  38568. +struct au_rdu_cookie {
  38569. + uint64_t h_pos;
  38570. + int16_t bindex;
  38571. + uint8_t flags;
  38572. + uint8_t pad;
  38573. + uint32_t generation;
  38574. +} __aligned(8);
  38575. +
  38576. +struct au_rdu_ent {
  38577. + uint64_t ino;
  38578. + int16_t bindex;
  38579. + uint8_t type;
  38580. + uint8_t nlen;
  38581. + uint8_t wh;
  38582. + char name[];
  38583. +} __aligned(8);
  38584. +
  38585. +static inline int au_rdu_len(int nlen)
  38586. +{
  38587. + /* include the terminating NULL */
  38588. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  38589. + sizeof(uint64_t));
  38590. +}
  38591. +
  38592. +union au_rdu_ent_ul {
  38593. + struct au_rdu_ent __user *e;
  38594. + uint64_t ul;
  38595. +};
  38596. +
  38597. +enum {
  38598. + AufsCtlRduV_SZ,
  38599. + AufsCtlRduV_End
  38600. +};
  38601. +
  38602. +struct aufs_rdu {
  38603. + /* input */
  38604. + union {
  38605. + uint64_t sz; /* AuCtl_RDU */
  38606. + uint64_t nent; /* AuCtl_RDU_INO */
  38607. + };
  38608. + union au_rdu_ent_ul ent;
  38609. + uint16_t verify[AufsCtlRduV_End];
  38610. +
  38611. + /* input/output */
  38612. + uint32_t blk;
  38613. +
  38614. + /* output */
  38615. + union au_rdu_ent_ul tail;
  38616. + /* number of entries which were added in a single call */
  38617. + uint64_t rent;
  38618. + uint8_t full;
  38619. + uint8_t shwh;
  38620. +
  38621. + struct au_rdu_cookie cookie;
  38622. +} __aligned(8);
  38623. +
  38624. +/* ---------------------------------------------------------------------- */
  38625. +
  38626. +/* dirren. the branch is identified by the filename who contains this */
  38627. +struct au_drinfo {
  38628. + uint64_t ino;
  38629. + union {
  38630. + uint8_t oldnamelen;
  38631. + uint64_t _padding;
  38632. + };
  38633. + uint8_t oldname[];
  38634. +} __aligned(8);
  38635. +
  38636. +struct au_drinfo_fdata {
  38637. + uint32_t magic;
  38638. + struct au_drinfo drinfo;
  38639. +} __aligned(8);
  38640. +
  38641. +#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
  38642. +/* future */
  38643. +#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
  38644. +
  38645. +/* ---------------------------------------------------------------------- */
  38646. +
  38647. +struct aufs_wbr_fd {
  38648. + uint32_t oflags;
  38649. + int16_t brid;
  38650. +} __aligned(8);
  38651. +
  38652. +/* ---------------------------------------------------------------------- */
  38653. +
  38654. +struct aufs_ibusy {
  38655. + uint64_t ino, h_ino;
  38656. + int16_t bindex;
  38657. +} __aligned(8);
  38658. +
  38659. +/* ---------------------------------------------------------------------- */
  38660. +
  38661. +/* error code for move-down */
  38662. +/* the actual message strings are implemented in aufs-util.git */
  38663. +enum {
  38664. + EAU_MVDOWN_OPAQUE = 1,
  38665. + EAU_MVDOWN_WHITEOUT,
  38666. + EAU_MVDOWN_UPPER,
  38667. + EAU_MVDOWN_BOTTOM,
  38668. + EAU_MVDOWN_NOUPPER,
  38669. + EAU_MVDOWN_NOLOWERBR,
  38670. + EAU_Last
  38671. +};
  38672. +
  38673. +/* flags for move-down */
  38674. +#define AUFS_MVDOWN_DMSG 1
  38675. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  38676. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  38677. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  38678. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  38679. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  38680. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  38681. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  38682. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  38683. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  38684. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  38685. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  38686. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  38687. +
  38688. +/* index for move-down */
  38689. +enum {
  38690. + AUFS_MVDOWN_UPPER,
  38691. + AUFS_MVDOWN_LOWER,
  38692. + AUFS_MVDOWN_NARRAY
  38693. +};
  38694. +
  38695. +/*
  38696. + * additional info of move-down
  38697. + * number of free blocks and inodes.
  38698. + * subset of struct kstatfs, but smaller and always 64bit.
  38699. + */
  38700. +struct aufs_stfs {
  38701. + uint64_t f_blocks;
  38702. + uint64_t f_bavail;
  38703. + uint64_t f_files;
  38704. + uint64_t f_ffree;
  38705. +};
  38706. +
  38707. +struct aufs_stbr {
  38708. + int16_t brid; /* optional input */
  38709. + int16_t bindex; /* output */
  38710. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  38711. +} __aligned(8);
  38712. +
  38713. +struct aufs_mvdown {
  38714. + uint32_t flags; /* input/output */
  38715. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  38716. + int8_t au_errno; /* output */
  38717. +} __aligned(8);
  38718. +
  38719. +/* ---------------------------------------------------------------------- */
  38720. +
  38721. +union aufs_brinfo {
  38722. + /* PATH_MAX may differ between kernel-space and user-space */
  38723. + char _spacer[4096];
  38724. + struct {
  38725. + int16_t id;
  38726. + int perm;
  38727. + char path[];
  38728. + };
  38729. +} __aligned(8);
  38730. +
  38731. +/* ---------------------------------------------------------------------- */
  38732. +
  38733. +#define AuCtlType 'A'
  38734. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  38735. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  38736. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  38737. + struct aufs_wbr_fd)
  38738. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  38739. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  38740. + struct aufs_mvdown)
  38741. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  38742. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  38743. +
  38744. +#endif /* __AUFS_TYPE_H__ */